1 /*****************************************************************************/
5 /* Debug symbol handling for the ld65 linker */
9 /* (C) 1998-2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 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 /*****************************************************************************/
56 /*****************************************************************************/
58 /*****************************************************************************/
62 /* We will collect all debug symbols in the following array and remove
63 * duplicates before outputing them.
65 static DbgSym* DbgSymPool[256];
69 /*****************************************************************************/
71 /*****************************************************************************/
75 static DbgSym* NewDbgSym (unsigned char Type, unsigned char AddrSize, ObjData* O)
76 /* Create a new DbgSym and return it */
79 DbgSym* D = xmalloc (sizeof (DbgSym));
81 /* Initialize the fields */
85 D->LineInfos = EmptyCollection;
91 D->AddrSize = AddrSize;
93 /* Return the new entry */
99 static DbgSym* GetDbgSym (DbgSym* D, long Val)
100 /* Check if we find the same debug symbol in the table. If we find it, return
101 * a pointer to the other occurrence, if we didn't find it, return NULL.
104 /* Create the hash. We hash over the symbol value */
105 unsigned Hash = ((Val >> 24) & 0xFF) ^
106 ((Val >> 16) & 0xFF) ^
107 ((Val >> 8) & 0xFF) ^
110 /* Check for this symbol */
111 DbgSym* Sym = DbgSymPool[Hash];
113 /* Is this symbol identical? */
114 if (Sym->Name == D->Name && EqualExpr (Sym->Expr, D->Expr)) {
123 /* This is the first symbol of it's kind */
129 static void InsertDbgSym (DbgSym* D, long Val)
130 /* Insert the symbol into the hashed symbol pool */
132 /* Create the hash. We hash over the symbol value */
133 unsigned Hash = ((Val >> 24) & 0xFF) ^
134 ((Val >> 16) & 0xFF) ^
135 ((Val >> 8) & 0xFF) ^
138 /* Insert the symbol */
139 D->Next = DbgSymPool [Hash];
140 DbgSymPool [Hash] = D;
145 DbgSym* ReadDbgSym (FILE* F, ObjData* O)
146 /* Read a debug symbol from a file, insert and return it */
148 /* Read the type and address size */
149 unsigned Type = ReadVar (F);
150 unsigned char AddrSize = Read8 (F);
152 /* Create a new debug symbol */
153 DbgSym* D = NewDbgSym (Type, AddrSize, O);
155 /* Read the id of the owner scope/symbol */
156 D->Parent.Id = ReadVar (F);
158 /* Read and assign the name */
159 D->Name = MakeGlobalStringId (O, ReadVar (F));
162 if (SYM_IS_EXPR (D->Type)) {
163 D->Expr = ReadExpr (F, O);
165 D->Expr = LiteralExpr (Read32 (F), O);
169 if (SYM_HAS_SIZE (D->Type)) {
170 D->Size = ReadVar (F);
173 /* Last is the list of line infos for this symbol */
174 ReadLineInfoList (F, O, &D->LineInfos);
176 /* Return the new DbgSym */
182 void ClearDbgSymTable (void)
183 /* Clear the debug symbol table */
186 for (I = 0; I < sizeof (DbgSymPool) / sizeof (DbgSymPool[0]); ++I) {
187 DbgSym* Sym = DbgSymPool[I];
190 DbgSym* NextSym = Sym->Next;
199 long GetDbgSymVal (const DbgSym* D)
200 /* Get the value of this symbol */
202 CHECK (D->Expr != 0);
203 return GetExprVal (D->Expr);
208 void PrintDbgSyms (ObjData* O, FILE* F)
209 /* Print the debug symbols in a debug file */
213 /* Walk through all debug symbols in this module */
214 for (I = 0; I < CollCount (&O->DbgSyms); ++I) {
218 /* Get the next debug symbol */
219 DbgSym* S = CollAt (&O->DbgSyms, I);
221 /* Get the symbol value */
222 Val = GetDbgSymVal (S);
224 /* Lookup this symbol in the table. If it is found in the table, it was
225 * already written to the file, so don't emit it twice. If it is not in
226 * the table, insert and output it.
228 if (GetDbgSym (S, Val) == 0) {
232 /* Emit the base data for the entry */
234 "sym\tname=\"%s\",value=0x%lX,addrsize=%s,type=%s",
237 AddrSizeToStr (S->AddrSize),
238 SYM_IS_LABEL (S->Type)? "label" : "equate");
240 /* Emit the size only if we know it */
242 fprintf (F, ",size=%lu", S->Size);
245 /* Check for a segmented expression and add the segment id to the
246 * debug info if we have one.
248 GetSegExprVal (S->Expr, &D);
249 if (!D.TooComplex && D.Seg != 0) {
250 fprintf (F, ",segment=%u", D.Seg->Id);
253 /* Terminate the output line */
256 /* Insert the symbol into the table */
257 InsertDbgSym (S, Val);
264 void PrintDbgSymLabels (ObjData* O, FILE* F)
265 /* Print the debug symbols in a VICE label file */
269 /* Walk through all debug symbols in this module */
270 for (I = 0; I < CollCount (&O->DbgSyms); ++I) {
274 /* Get the next debug symbol */
275 DbgSym* D = CollAt (&O->DbgSyms, I);
277 /* Emit this symbol only if it is a label (ignore equates) */
278 if (SYM_IS_EQUATE (D->Type)) {
282 /* Get the symbol value */
283 Val = GetDbgSymVal (D);
285 /* Lookup this symbol in the table. If it is found in the table, it was
286 * already written to the file, so don't emit it twice. If it is not in
287 * the table, insert and output it.
289 if (GetDbgSym (D, Val) == 0) {
291 /* Emit the VICE label line */
292 fprintf (F, "al %06lX .%s\n", Val, GetString (D->Name));
294 /* Insert the symbol into the table */
295 InsertDbgSym (D, Val);