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, unsigned char AddrSize, ObjData* O)
74 /* Create a new DbgSym and return it */
77 DbgSym* D = xmalloc (sizeof (DbgSym));
79 /* Initialize the fields */
86 D->AddrSize = AddrSize;
88 /* Return the new entry */
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.
99 /* Create the hash. We hash over the symbol value */
100 unsigned Hash = ((Val >> 24) & 0xFF) ^
101 ((Val >> 16) & 0xFF) ^
102 ((Val >> 8) & 0xFF) ^
105 /* Check for this symbol */
106 DbgSym* Sym = DbgSymPool[Hash];
108 /* Is this symbol identical? */
109 if (Sym->Name == D->Name && EqualExpr (Sym->Expr, D->Expr)) {
118 /* This is the first symbol of it's kind */
124 static void InsertDbgSym (DbgSym* D, long Val)
125 /* Insert the symbol into the hashed symbol pool */
127 /* Create the hash. We hash over the symbol value */
128 unsigned Hash = ((Val >> 24) & 0xFF) ^
129 ((Val >> 16) & 0xFF) ^
130 ((Val >> 8) & 0xFF) ^
133 /* Insert the symbol */
134 D->Next = DbgSymPool [Hash];
135 DbgSymPool [Hash] = D;
140 DbgSym* ReadDbgSym (FILE* F, ObjData* O)
141 /* Read a debug symbol from a file, insert and return it */
143 /* Read the type and address size */
144 unsigned char Type = Read8 (F);
145 unsigned char AddrSize = Read8 (F);
147 /* Create a new debug symbol */
148 DbgSym* D = NewDbgSym (Type, AddrSize, O);
150 /* Read and assign the name */
151 D->Name = MakeGlobalStringId (O, ReadVar (F));
154 if (IS_EXP_EXPR (D->Type)) {
155 D->Expr = ReadExpr (F, O);
157 D->Expr = LiteralExpr (Read32 (F), O);
160 /* Last is the file position where the definition was done */
161 ReadFilePos (F, &D->Pos);
163 /* Return the new DbgSym */
169 static void ClearDbgSymTable (void)
170 /* Clear the debug symbol table */
173 for (I = 0; I < sizeof (DbgSymPool) / sizeof (DbgSymPool[0]); ++I) {
174 DbgSym* Sym = DbgSymPool[I];
177 DbgSym* NextSym = Sym->Next;
186 long GetDbgSymVal (DbgSym* D)
187 /* Get the value of this symbol */
189 CHECK (D->Expr != 0);
190 return GetExprVal (D->Expr);
195 void PrintDbgSyms (ObjData* O, FILE* F)
196 /* Print the debug symbols in a debug file */
200 /* Clear the debug sym table */
203 /* Walk through all debug symbols in this module */
204 for (I = 0; I < O->DbgSymCount; ++I) {
208 /* Get the next debug symbol */
209 DbgSym* D = O->DbgSyms [I];
211 /* Get the symbol value */
212 Val = GetDbgSymVal (D);
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.
218 if (GetDbgSym (D, Val) == 0) {
220 /* Emit the debug file line */
222 "sym\t\"%s\", %08lX, %s\n",
225 IS_EXP_LABEL (D->Type)? "label" : "equate");
227 /* Insert the symbol into the table */
228 InsertDbgSym (D, Val);
235 void PrintDbgSymLabels (ObjData* O, FILE* F)
236 /* Print the debug symbols in a VICE label file */
240 /* Clear the debug sym table */
243 /* Walk through all debug symbols in this module */
244 for (I = 0; I < O->DbgSymCount; ++I) {
248 /* Get the next debug symbol */
249 DbgSym* D = O->DbgSyms [I];
251 /* Emit this symbol only if it is a label (ignore equates) */
252 if (IS_EXP_EQUATE (D->Type)) {
256 /* Get the symbol value */
257 Val = GetDbgSymVal (D);
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.
263 if (GetDbgSym (D, Val) == 0) {
265 /* Emit the VICE label line */
266 fprintf (F, "al %06lX .%s\n", Val, GetString (D->Name));
268 /* Insert the symbol into the table */
269 InsertDbgSym (D, Val);