1 /*****************************************************************************/
5 /* Debug symbol handing for the ld65 linker */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
53 /*****************************************************************************/
55 /*****************************************************************************/
59 /* We will collect all debug symbols in the following array and remove
60 * duplicates before outputing them.
62 static DbgSym* DbgSymPool [256];
66 /*****************************************************************************/
68 /*****************************************************************************/
72 static DbgSym* NewDbgSym (unsigned char Type, ObjData* O)
73 /* Create a new DbgSym and return it */
76 DbgSym* D = xmalloc (sizeof (DbgSym));
78 /* Initialize the fields */
86 /* Return the new entry */
92 static DbgSym* GetDbgSym (DbgSym* D, long Val)
93 /* Check if we find the same debug symbol in the table. If we find it, return
94 * a pointer to the other occurrence, if we didn't find it, return NULL.
97 /* Create the hash. We hash over the symbol value */
98 unsigned Hash = ((Val >> 24) & 0xFF) ^
99 ((Val >> 16) & 0xFF) ^
100 ((Val >> 8) & 0xFF) ^
103 /* Check for this symbol */
104 DbgSym* Sym = DbgSymPool [Hash];
106 /* Is this symbol identical? */
107 if (strcmp (Sym->Name, D->Name) == 0 && EqualExpr (Sym->Expr, D->Expr)) {
116 /* This is the first symbol of it's kind */
122 static void InsertDbgSym (DbgSym* D, long Val)
123 /* Insert the symbol into the hashed symbol pool */
125 /* Create the hash. We hash over the symbol value */
126 unsigned Hash = ((Val >> 24) & 0xFF) ^
127 ((Val >> 16) & 0xFF) ^
128 ((Val >> 8) & 0xFF) ^
131 /* Insert the symbol */
132 D->Next = DbgSymPool [Hash];
133 DbgSymPool [Hash] = D;
138 DbgSym* ReadDbgSym (FILE* F, ObjData* O)
139 /* Read a debug symbol from a file, insert and return it */
147 /* Create a new debug symbol */
148 D = NewDbgSym (Type, O);
150 /* Read and assign the name */
151 D->Name = ReadStr (F);
154 if (IS_EXP_EXPR (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 long GetDbgSymVal (DbgSym* D)
170 /* Get the value of this symbol */
172 CHECK (D->Expr != 0);
173 return GetExprVal (D->Expr);
178 void PrintDbgSymLabels (ObjData* O, FILE* F)
179 /* Print the debug symbols in a VICE label file */
183 /* Walk through all debug symbols in this module */
184 for (I = 0; I < O->DbgSymCount; ++I) {
186 /* Get the next debug symbol */
187 DbgSym* D = O->DbgSyms [I];
189 /* Get the symbol value */
190 long Val = GetDbgSymVal (D);
192 /* Lookup this symbol in the table. If it is found in the table, it was
193 * already written to the file, so don't emit it twice. If it is not in
194 * the table, insert and output it.
196 if (GetDbgSym (D, Val) == 0) {
198 /* Emit the VICE label line */
199 fprintf (F, "al %06lX .%s\n", Val, D->Name);
201 /* Insert the symbol into the table */
202 InsertDbgSym (D, Val);