]> git.sur5r.net Git - cc65/blob - src/ld65/dbgsyms.c
911dedab9239d518311446ba321f550fb14a6651
[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     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, const char* Name, ObjData* O)
73 /* Create a new DbgSym and return it */
74 {
75     /* Get the length of the symbol name */
76     unsigned Len = strlen (Name);
77
78     /* Allocate memory */
79     DbgSym* D = xmalloc (sizeof (DbgSym) + Len);
80
81     /* Initialize the fields */
82     D->Next     = 0;
83     D->Flags    = 0;
84     D->Obj      = O;
85     D->Expr     = 0;
86     D->Type     = Type;
87     memcpy (D->Name, Name, Len);
88     D->Name [Len] = '\0';
89
90     /* Return the new entry */
91     return D;
92 }
93
94
95
96 static DbgSym* GetDbgSym (DbgSym* D, long Val)
97 /* Check if we find the same debug symbol in the table. If we find it, return
98  * a pointer to the other occurrence, if we didn't find it, return NULL.
99  */
100 {
101     /* Create the hash. We hash over the symbol value */
102     unsigned Hash = ((Val >> 24) & 0xFF) ^
103                     ((Val >> 16) & 0xFF) ^
104                     ((Val >>  8) & 0xFF) ^
105                     ((Val >>  0) & 0xFF);
106
107     /* Check for this symbol */
108     DbgSym* Sym = DbgSymPool [Hash];
109     while (Sym) {
110         /* Is this symbol identical? */
111         if (strcmp (Sym->Name, D->Name) == 0 && EqualExpr (Sym->Expr, D->Expr)) {
112             /* Found */
113             return Sym;
114         }
115
116         /* Next symbol */
117         Sym = Sym->Next;
118     }
119
120     /* This is the first symbol of it's kind */
121     return 0;
122 }
123
124
125
126 static void InsertDbgSym (DbgSym* D, long Val)
127 /* Insert the symbol into the hashed symbol pool */
128 {
129     /* Create the hash. We hash over the symbol value */
130     unsigned Hash = ((Val >> 24) & 0xFF) ^
131                     ((Val >> 16) & 0xFF) ^
132                     ((Val >>  8) & 0xFF) ^
133                     ((Val >>  0) & 0xFF);
134
135     /* Insert the symbol */
136     D->Next = DbgSymPool [Hash];
137     DbgSymPool [Hash] = D;
138 }
139
140
141
142 DbgSym* ReadDbgSym (FILE* F, ObjData* O)
143 /* Read a debug symbol from a file, insert and return it */
144 {
145     unsigned char Type;
146     char Name [256];
147     DbgSym* D;
148
149     /* Read the type */
150     Type = Read8 (F);
151
152     /* Read the name */
153     ReadStr (F, Name);
154
155     /* Create a new export */
156     D = NewDbgSym (Type, Name, O);
157
158     /* Read the value */
159     if (Type & EXP_EXPR) {
160         D->Expr = ReadExpr (F, O);
161     } else {
162         D->Expr = LiteralExpr (Read32 (F), O);
163     }
164
165     /* Last is the file position where the definition was done */
166     ReadFilePos (F, &D->Pos);
167
168     /* Return the new DbgSym */
169     return D;
170 }
171
172
173
174 long GetDbgSymVal (DbgSym* D)
175 /* Get the value of this symbol */
176 {
177     CHECK (D->Expr != 0);
178     return GetExprVal (D->Expr);
179 }
180
181
182
183 void PrintDbgSymLabels (ObjData* O, FILE* F)
184 /* Print the debug symbols in a VICE label file */
185 {
186     unsigned I;
187
188     /* Walk through all debug symbols in this module */
189     for (I = 0; I < O->DbgSymCount; ++I) {
190
191         /* Get the next debug symbol */
192         DbgSym* D = O->DbgSyms [I];
193
194         /* Get the symbol value */
195         long Val = GetDbgSymVal (D);
196
197         /* Lookup this symbol in the table. If it is found in the table, it was
198          * already written to the file, so don't emit it twice. If it is not in
199          * the table, insert and output it.
200          */
201         if (GetDbgSym (D, Val) == 0) {
202
203             /* Emit the VICE label line */
204             fprintf (F, "al %06lX .%s\n", Val, D->Name);
205
206             /* Insert the symbol into the table */
207             InsertDbgSym (D, Val);
208         }
209     }
210 }
211
212
213