]> git.sur5r.net Git - cc65/blob - src/ld65/dbgsyms.c
12d13a83d694baaa50ffdba2e347d62251b157e3
[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, 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
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 (Sym->Name == D->Name && 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     DbgSym* D;
144
145     /* Read the type */
146     Type = Read8 (F);
147
148     /* Create a new debug symbol */
149     D = NewDbgSym (Type, O);
150
151     /* Read and assign the name */
152     D->Name = MakeGlobalStringId (O, ReadVar (F));
153
154     /* Read the value */
155     if (IS_EXP_EXPR (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 long GetDbgSymVal (DbgSym* D)
171 /* Get the value of this symbol */
172 {
173     CHECK (D->Expr != 0);
174     return GetExprVal (D->Expr);
175 }
176
177
178
179 void PrintDbgSymLabels (ObjData* O, FILE* F)
180 /* Print the debug symbols in a VICE label file */
181 {
182     unsigned I;
183
184     /* Walk through all debug symbols in this module */
185     for (I = 0; I < O->DbgSymCount; ++I) {
186
187         /* Get the next debug symbol */
188         DbgSym* D = O->DbgSyms [I];
189
190         /* Get the symbol value */
191         long Val = GetDbgSymVal (D);
192
193         /* Lookup this symbol in the table. If it is found in the table, it was
194          * already written to the file, so don't emit it twice. If it is not in
195          * the table, insert and output it.
196          */
197         if (GetDbgSym (D, Val) == 0) {
198
199             /* Emit the VICE label line */
200             fprintf (F, "al %06lX .%s\n", Val, GetString (D->Name));
201
202             /* Insert the symbol into the table */
203             InsertDbgSym (D, Val);
204         }
205     }
206 }
207
208
209