]> git.sur5r.net Git - cc65/blob - src/ld65/dbgsyms.c
3cccc89f3dc565093c7f7a6aa52bd804cf2e6033
[cc65] / src / ld65 / dbgsyms.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 dbgsyms.c                                 */
4 /*                                                                           */
5 /*                 Debug symbol handling for the ld65 linker                 */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
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 /* common */
39 #include "check.h"
40 #include "symdefs.h"
41 #include "xmalloc.h"
42
43 /* ld65 */
44 #include "dbgsyms.h"
45 #include "error.h"
46 #include "expr.h"
47 #include "fileio.h"
48 #include "global.h"
49 #include "objdata.h"
50 #include "spool.h"
51
52
53
54 /*****************************************************************************/
55 /*                                   Data                                    */
56 /*****************************************************************************/
57
58
59
60 /* We will collect all debug symbols in the following array and remove
61  * duplicates before outputing them.
62  */
63 static DbgSym*  DbgSymPool[256];
64
65
66
67 /*****************************************************************************/
68 /*                                   Code                                    */
69 /*****************************************************************************/
70
71
72
73 static DbgSym* NewDbgSym (unsigned char Type, unsigned char AddrSize, ObjData* O)
74 /* Create a new DbgSym and return it */
75 {
76     /* Allocate memory */
77     DbgSym* D = xmalloc (sizeof (DbgSym));
78
79     /* Initialize the fields */
80     D->Next     = 0;
81     D->Flags    = 0;
82     D->Obj      = O;
83     D->Expr     = 0;
84     D->Name     = 0;
85     D->Type     = Type;
86     D->AddrSize = AddrSize;
87
88     /* Return the new entry */
89     return D;
90 }
91
92
93
94 static DbgSym* GetDbgSym (DbgSym* D, long Val)
95 /* Check if we find the same debug symbol in the table. If we find it, return
96  * a pointer to the other occurrence, if we didn't find it, return NULL.
97  */
98 {
99     /* Create the hash. We hash over the symbol value */
100     unsigned Hash = ((Val >> 24) & 0xFF) ^
101                     ((Val >> 16) & 0xFF) ^
102                     ((Val >>  8) & 0xFF) ^
103                     ((Val >>  0) & 0xFF);
104
105     /* Check for this symbol */
106     DbgSym* Sym = DbgSymPool[Hash];
107     while (Sym) {
108         /* Is this symbol identical? */
109         if (Sym->Name == D->Name && EqualExpr (Sym->Expr, D->Expr)) {
110             /* Found */
111             return Sym;
112         }
113
114         /* Next symbol */
115         Sym = Sym->Next;
116     }
117
118     /* This is the first symbol of it's kind */
119     return 0;
120 }
121
122
123
124 static void InsertDbgSym (DbgSym* D, long Val)
125 /* Insert the symbol into the hashed symbol pool */
126 {
127     /* Create the hash. We hash over the symbol value */
128     unsigned Hash = ((Val >> 24) & 0xFF) ^
129                     ((Val >> 16) & 0xFF) ^
130                     ((Val >>  8) & 0xFF) ^
131                     ((Val >>  0) & 0xFF);
132
133     /* Insert the symbol */
134     D->Next = DbgSymPool [Hash];
135     DbgSymPool [Hash] = D;
136 }
137
138
139
140 DbgSym* ReadDbgSym (FILE* F, ObjData* O)
141 /* Read a debug symbol from a file, insert and return it */
142 {
143     /* Read the type and address size */
144     unsigned char Type = Read8 (F);
145     unsigned char AddrSize = Read8 (F);
146
147     /* Create a new debug symbol */
148     DbgSym* D = NewDbgSym (Type, AddrSize, O);
149
150     /* Read and assign the name */
151     D->Name = MakeGlobalStringId (O, ReadVar (F));
152
153     /* Read the value */
154     if (IS_EXP_EXPR (D->Type)) {
155         D->Expr = ReadExpr (F, O);
156     } else {
157         D->Expr = LiteralExpr (Read32 (F), O);
158     }
159
160     /* Last is the file position where the definition was done */
161     ReadFilePos (F, &D->Pos);
162
163     /* Return the new DbgSym */
164     return D;
165 }
166
167
168
169 static void ClearDbgSymTable (void)
170 /* Clear the debug symbol table */
171 {
172     unsigned I;
173     for (I = 0; I < sizeof (DbgSymPool) / sizeof (DbgSymPool[0]); ++I) {
174         DbgSym* Sym = DbgSymPool[I];
175         DbgSymPool[I] = 0;
176         while (Sym) {
177             DbgSym* NextSym = Sym->Next;
178             Sym->Next = 0;
179             Sym = NextSym;
180         }
181     }
182 }
183
184
185
186 long GetDbgSymVal (DbgSym* D)
187 /* Get the value of this symbol */
188 {
189     CHECK (D->Expr != 0);
190     return GetExprVal (D->Expr);
191 }
192
193
194
195 void PrintDbgSyms (ObjData* O, FILE* F)
196 /* Print the debug symbols in a debug file */
197 {
198     unsigned I;
199
200     /* Clear the debug sym table */
201     ClearDbgSymTable ();
202
203     /* Walk through all debug symbols in this module */
204     for (I = 0; I < O->DbgSymCount; ++I) {
205
206         long Val;
207
208         /* Get the next debug symbol */
209         DbgSym* D = O->DbgSyms [I];
210
211         /* Get the symbol value */
212         Val = GetDbgSymVal (D);
213
214         /* Lookup this symbol in the table. If it is found in the table, it was
215          * already written to the file, so don't emit it twice. If it is not in
216          * the table, insert and output it.
217          */
218         if (GetDbgSym (D, Val) == 0) {
219
220             /* Emit the debug file line */
221             fprintf (F,
222                      "sym\t\"%s\", %08lX, %s\n",
223                      GetString (D->Name),
224                      Val,
225                      IS_EXP_LABEL (D->Type)? "label" : "equate");
226
227             /* Insert the symbol into the table */
228             InsertDbgSym (D, Val);
229         }
230     }
231 }
232
233
234
235 void PrintDbgSymLabels (ObjData* O, FILE* F)
236 /* Print the debug symbols in a VICE label file */
237 {
238     unsigned I;
239
240     /* Clear the debug sym table */
241     ClearDbgSymTable ();
242
243     /* Walk through all debug symbols in this module */
244     for (I = 0; I < O->DbgSymCount; ++I) {
245
246         long Val;
247
248         /* Get the next debug symbol */
249         DbgSym* D = O->DbgSyms [I];
250
251         /* Emit this symbol only if it is a label (ignore equates) */
252         if (IS_EXP_EQUATE (D->Type)) {
253             continue;
254         }
255
256         /* Get the symbol value */
257         Val = GetDbgSymVal (D);
258
259         /* Lookup this symbol in the table. If it is found in the table, it was
260          * already written to the file, so don't emit it twice. If it is not in
261          * the table, insert and output it.
262          */
263         if (GetDbgSym (D, Val) == 0) {
264
265             /* Emit the VICE label line */
266             fprintf (F, "al %06lX .%s\n", Val, GetString (D->Name));
267
268             /* Insert the symbol into the table */
269             InsertDbgSym (D, Val);
270         }
271     }
272 }
273
274
275
276