]> git.sur5r.net Git - cc65/blob - src/ld65/dbgsyms.c
Added a method to write variable sized unsigned values. Use this method for
[cc65] / src / ld65 / dbgsyms.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 dbgsyms.c                                 */
4 /*                                                                           */
5 /*                 Debug symbol handing for the ld65 linker                  */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2000 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
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 "global.h"
45 #include "error.h"
46 #include "fileio.h"
47 #include "objdata.h"
48 #include "expr.h"
49 #include "dbgsyms.h"
50
51
52
53 /*****************************************************************************/
54 /*                                   Data                                    */
55 /*****************************************************************************/
56
57
58
59 /* We will collect all debug symbols in the following array and remove
60  * duplicates before outputing them.
61  */
62 static DbgSym*  DbgSymPool [256];
63
64
65
66 /*****************************************************************************/
67 /*                                   Code                                    */
68 /*****************************************************************************/
69
70
71
72 static DbgSym* NewDbgSym (unsigned char Type, ObjData* O)
73 /* Create a new DbgSym and return it */
74 {
75     /* Allocate memory */
76     DbgSym* D = xmalloc (sizeof (DbgSym));
77
78     /* Initialize the fields */
79     D->Next     = 0;
80     D->Flags    = 0;
81     D->Obj      = O;
82     D->Expr     = 0;
83     D->Type     = Type;
84     D->Name     = 0;
85
86     /* Return the new entry */
87     return D;
88 }
89
90
91
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.
95  */
96 {
97     /* Create the hash. We hash over the symbol value */
98     unsigned Hash = ((Val >> 24) & 0xFF) ^
99                     ((Val >> 16) & 0xFF) ^
100                     ((Val >>  8) & 0xFF) ^
101                     ((Val >>  0) & 0xFF);
102
103     /* Check for this symbol */
104     DbgSym* Sym = DbgSymPool [Hash];
105     while (Sym) {
106         /* Is this symbol identical? */
107         if (strcmp (Sym->Name, D->Name) == 0 && EqualExpr (Sym->Expr, D->Expr)) {
108             /* Found */
109             return Sym;
110         }
111
112         /* Next symbol */
113         Sym = Sym->Next;
114     }
115
116     /* This is the first symbol of it's kind */
117     return 0;
118 }
119
120
121
122 static void InsertDbgSym (DbgSym* D, long Val)
123 /* Insert the symbol into the hashed symbol pool */
124 {
125     /* Create the hash. We hash over the symbol value */
126     unsigned Hash = ((Val >> 24) & 0xFF) ^
127                     ((Val >> 16) & 0xFF) ^
128                     ((Val >>  8) & 0xFF) ^
129                     ((Val >>  0) & 0xFF);
130
131     /* Insert the symbol */
132     D->Next = DbgSymPool [Hash];
133     DbgSymPool [Hash] = D;
134 }
135
136
137
138 DbgSym* ReadDbgSym (FILE* F, ObjData* O)
139 /* Read a debug symbol from a file, insert and return it */
140 {
141     unsigned char Type;
142     DbgSym* D;
143
144     /* Read the type */
145     Type = Read8 (F);
146
147     /* Create a new debug symbol */
148     D = NewDbgSym (Type, O);
149
150     /* Read and assign the name */
151     D->Name = ReadStr (F);
152
153     /* Read the value */
154     if (Type & EXP_EXPR) {
155         D->Expr = ReadExpr (F, O);
156     } else {
157         D->Expr = LiteralExpr (Read32 (F), O);
158     }
159
160     /* Last is the file position where the definition was done */
161     ReadFilePos (F, &D->Pos);
162
163     /* Return the new DbgSym */
164     return D;
165 }
166
167
168
169 long GetDbgSymVal (DbgSym* D)
170 /* Get the value of this symbol */
171 {
172     CHECK (D->Expr != 0);
173     return GetExprVal (D->Expr);
174 }
175
176
177
178 void PrintDbgSymLabels (ObjData* O, FILE* F)
179 /* Print the debug symbols in a VICE label file */
180 {
181     unsigned I;
182
183     /* Walk through all debug symbols in this module */
184     for (I = 0; I < O->DbgSymCount; ++I) {
185
186         /* Get the next debug symbol */
187         DbgSym* D = O->DbgSyms [I];
188
189         /* Get the symbol value */
190         long Val = GetDbgSymVal (D);
191
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.
195          */
196         if (GetDbgSym (D, Val) == 0) {
197
198             /* Emit the VICE label line */
199             fprintf (F, "al %06lX .%s\n", Val, D->Name);
200
201             /* Insert the symbol into the table */
202             InsertDbgSym (D, Val);
203         }
204     }
205 }
206
207
208