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