1 /*****************************************************************************/
5 /* Debug symbol handling for the ld65 linker */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
54 /*****************************************************************************/
56 /*****************************************************************************/
60 /* We will collect all debug symbols in the following array and remove
61 * duplicates before outputing them.
63 static DbgSym* DbgSymPool[256];
67 /*****************************************************************************/
69 /*****************************************************************************/
73 static DbgSym* NewDbgSym (unsigned char Type, ObjData* O)
74 /* Create a new DbgSym and return it */
77 DbgSym* D = xmalloc (sizeof (DbgSym));
79 /* Initialize the fields */
87 /* Return the new entry */
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.
98 /* Create the hash. We hash over the symbol value */
99 unsigned Hash = ((Val >> 24) & 0xFF) ^
100 ((Val >> 16) & 0xFF) ^
101 ((Val >> 8) & 0xFF) ^
104 /* Check for this symbol */
105 DbgSym* Sym = DbgSymPool[Hash];
107 /* Is this symbol identical? */
108 if (Sym->Name == D->Name && EqualExpr (Sym->Expr, D->Expr)) {
117 /* This is the first symbol of it's kind */
123 static void InsertDbgSym (DbgSym* D, long Val)
124 /* Insert the symbol into the hashed symbol pool */
126 /* Create the hash. We hash over the symbol value */
127 unsigned Hash = ((Val >> 24) & 0xFF) ^
128 ((Val >> 16) & 0xFF) ^
129 ((Val >> 8) & 0xFF) ^
132 /* Insert the symbol */
133 D->Next = DbgSymPool [Hash];
134 DbgSymPool [Hash] = D;
139 DbgSym* ReadDbgSym (FILE* F, ObjData* O)
140 /* Read a debug symbol from a file, insert and return it */
143 unsigned char Type = Read8 (F);
145 /* Create a new debug symbol */
146 DbgSym* D = NewDbgSym (Type, O);
148 /* Read and assign the name */
149 D->Name = MakeGlobalStringId (O, ReadVar (F));
152 if (IS_EXP_EXPR (D->Type)) {
153 D->Expr = ReadExpr (F, O);
155 D->Expr = LiteralExpr (Read32 (F), O);
158 /* Last is the file position where the definition was done */
159 ReadFilePos (F, &D->Pos);
161 /* Return the new DbgSym */
167 static void ClearDbgSymTable (void)
168 /* Clear the debug symbol table */
171 for (I = 0; I < sizeof (DbgSymPool) / sizeof (DbgSymPool[0]); ++I) {
172 DbgSym* Sym = DbgSymPool[I];
175 DbgSym* NextSym = Sym->Next;
184 long GetDbgSymVal (DbgSym* D)
185 /* Get the value of this symbol */
187 CHECK (D->Expr != 0);
188 return GetExprVal (D->Expr);
193 void PrintDbgSyms (ObjData* O, FILE* F)
194 /* Print the debug symbols in a debug file */
198 /* Clear the debug sym table */
201 /* Walk through all debug symbols in this module */
202 for (I = 0; I < O->DbgSymCount; ++I) {
206 /* Get the next debug symbol */
207 DbgSym* D = O->DbgSyms [I];
209 /* Get the symbol value */
210 Val = GetDbgSymVal (D);
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.
216 if (GetDbgSym (D, Val) == 0) {
218 /* Emit the debug file line */
220 "sym\t\"%s\", 0x%02X, 0x%08lX\n",
225 /* Insert the symbol into the table */
226 InsertDbgSym (D, Val);
233 void PrintDbgSymLabels (ObjData* O, FILE* F)
234 /* Print the debug symbols in a VICE label file */
238 /* Clear the debug sym table */
241 /* Walk through all debug symbols in this module */
242 for (I = 0; I < O->DbgSymCount; ++I) {
246 /* Get the next debug symbol */
247 DbgSym* D = O->DbgSyms [I];
249 /* Emit this symbol only if it is a label (ignore equates) */
250 if (IS_EXP_EQUATE (D->Type)) {
254 /* Get the symbol value */
255 Val = GetDbgSymVal (D);
257 /* Lookup this symbol in the table. If it is found in the table, it was
258 * already written to the file, so don't emit it twice. If it is not in
259 * the table, insert and output it.
261 if (GetDbgSym (D, Val) == 0) {
263 /* Emit the VICE label line */
264 fprintf (F, "al %06lX .%s\n", Val, GetString (D->Name));
266 /* Insert the symbol into the table */
267 InsertDbgSym (D, Val);