]> git.sur5r.net Git - cc65/blob - src/ld65/dbgsyms.c
Added the io module
[cc65] / src / ld65 / dbgsyms.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 dbgsyms.c                                 */
4 /*                                                                           */
5 /*                 Debug symbol handing for the ld65 linker                  */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 #include "../common/symdefs.h"
39 #include "../common/xmalloc.h"
40
41 #include "global.h"
42 #include "error.h"
43 #include "fileio.h"
44 #include "objdata.h"
45 #include "expr.h"
46 #include "dbgsyms.h"
47
48
49
50 /*****************************************************************************/
51 /*                                   Data                                    */
52 /*****************************************************************************/
53
54
55
56 /* We will collect all debug symbols in the following array and remove
57  * duplicates before outputing them.
58  */
59 static DbgSym*  DbgSymPool [256];
60
61
62
63 /*****************************************************************************/
64 /*                                   Code                                    */
65 /*****************************************************************************/
66
67
68
69 static DbgSym* NewDbgSym (unsigned char Type, const char* Name, ObjData* O)
70 /* Create a new DbgSym and return it */
71 {
72     /* Get the length of the symbol name */
73     unsigned Len = strlen (Name);
74
75     /* Allocate memory */
76     DbgSym* D = xmalloc (sizeof (DbgSym) + Len);
77
78     /* Initialize the fields */
79     D->Next     = 0;
80     D->Flags    = 0;
81     D->Obj      = O;
82     D->Expr     = 0;
83     D->Type     = Type;
84     memcpy (D->Name, Name, Len);
85     D->Name [Len] = '\0';
86
87     /* Return the new entry */
88     return D;
89 }
90
91
92
93 static DbgSym* GetDbgSym (DbgSym* D, long Val)
94 /* Check if we find the same debug symbol in the table. If we find it, return
95  * a pointer to the other occurrence, if we didn't find it, return NULL.
96  */
97 {
98     /* Create the hash. We hash over the symbol value */
99     unsigned Hash = ((Val >> 24) & 0xFF) ^
100                     ((Val >> 16) & 0xFF) ^
101                     ((Val >>  8) & 0xFF) ^
102                     ((Val >>  0) & 0xFF);
103
104     /* Check for this symbol */
105     DbgSym* Sym = DbgSymPool [Hash];
106     while (Sym) {
107         /* Is this symbol identical? */
108         if (strcmp (Sym->Name, D->Name) == 0 && EqualExpr (Sym->Expr, D->Expr)) {
109             /* Found */
110             return Sym;
111         }
112
113         /* Next symbol */
114         Sym = Sym->Next;
115     }
116
117     /* This is the first symbol of it's kind */
118     return 0;
119 }
120
121
122
123 static void InsertDbgSym (DbgSym* D, long Val)
124 /* Insert the symbol into the hashed symbol pool */
125 {
126     /* Create the hash. We hash over the symbol value */
127     unsigned Hash = ((Val >> 24) & 0xFF) ^
128                     ((Val >> 16) & 0xFF) ^
129                     ((Val >>  8) & 0xFF) ^
130                     ((Val >>  0) & 0xFF);
131
132     /* Insert the symbol */
133     D->Next = DbgSymPool [Hash];
134     DbgSymPool [Hash] = D;
135 }
136
137
138
139 DbgSym* ReadDbgSym (FILE* F, ObjData* O)
140 /* Read a debug symbol from a file, insert and return it */
141 {
142     unsigned char Type;
143     char Name [256];
144     DbgSym* D;
145
146     /* Read the type */
147     Type = Read8 (F);
148
149     /* Read the name */
150     ReadStr (F, Name);
151
152     /* Create a new export */
153     D = NewDbgSym (Type, Name, O);
154
155     /* Read the value */
156     if (Type & EXP_EXPR) {
157         D->Expr = ReadExpr (F, O);
158     } else {
159         D->Expr = LiteralExpr (Read32 (F), O);
160     }
161
162     /* Last is the file position where the definition was done */
163     ReadFilePos (F, &D->Pos);
164
165     /* Return the new DbgSym */
166     return D;
167 }
168
169
170
171 long GetDbgSymVal (DbgSym* D)
172 /* Get the value of this symbol */
173 {
174     CHECK (D->Expr != 0);
175     return GetExprVal (D->Expr);
176 }
177
178
179
180 void PrintDbgSymLabels (ObjData* O, FILE* F)
181 /* Print the debug symbols in a VICE label file */
182 {
183     unsigned I;
184
185     /* Walk through all debug symbols in this module */
186     for (I = 0; I < O->DbgSymCount; ++I) {
187
188         /* Get the next debug symbol */
189         DbgSym* D = O->DbgSyms [I];
190
191         /* Get the symbol value */
192         long Val = GetDbgSymVal (D);
193
194         /* Lookup this symbol in the table. If it is found in the table, it was
195          * already written to the file, so don't emit it twice. If it is not in
196          * the table, insert and output it.
197          */
198         if (GetDbgSym (D, Val) == 0) {
199
200             /* Emit the VICE label line */
201             fprintf (F, "al %06lX .%s\n", Val, D->Name);
202
203             /* Insert the symbol into the table */
204             InsertDbgSym (D, Val);
205         }
206     }
207 }
208
209
210