]> git.sur5r.net Git - cc65/blob - src/ld65/extsyms.c
Fixed a bug
[cc65] / src / ld65 / extsyms.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 extsyms.c                                 */
4 /*                                                                           */
5 /*      Handle program external symbols for relocatable output formats       */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1999-2001 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 "hashstr.h"
40 #include "xmalloc.h"
41
42 /* ld65 */
43 #include "error.h"
44 #include "extsyms.h"
45
46
47
48 /*****************************************************************************/
49 /*                                   Data                                    */
50 /*****************************************************************************/
51
52
53
54 /* Structure holding an external symbol */
55 struct ExtSym {
56     ExtSym*     List;           /* Next entry in list of all symbols */
57     ExtSym*     Next;           /* Next entry in hash list */
58     unsigned    Flags;          /* Generic flags */
59     unsigned    Num;            /* Number of external symbol */
60     char        Name [1];       /* Name - dynamically allocated */
61 };
62
63 /* External symbol table structure */
64 #define HASHTAB_SIZE    53
65 struct ExtSymTab {
66     ExtSym*     Root;           /* List of symbols */
67     ExtSym*     Last;           /* Pointer to last symbol */
68     unsigned    Count;          /* Number of symbols */
69     ExtSym*     HashTab [HASHTAB_SIZE];
70 };
71
72
73
74 /*****************************************************************************/
75 /*                                   Code                                    */
76 /*****************************************************************************/
77
78
79
80 ExtSym* NewExtSym (ExtSymTab* Tab, const char* Name)
81 /* Create a new external symbol and insert it into the table */
82 {
83     /* Get the hash value of the string */
84     unsigned Hash = HashStr (Name) % HASHTAB_SIZE;
85
86     /* Get the length of the name */
87     unsigned Len = strlen (Name);
88
89     /* Check for duplicates */
90     ExtSym* E = GetExtSym (Tab, Name);  /* Don't care about duplicate hash here... */
91     if (E != 0) {
92         /* We do already have a symbol with this name */
93         Error ("Duplicate external symbol `%s'", Name);
94     }
95
96     /* Allocate memory for the structure */
97     E = xmalloc (sizeof (ExtSym) + Len);
98
99     /* Initialize the structure */
100     E->List  = 0;
101     E->Flags = 0;
102     E->Num   = Tab->Count;
103     memcpy (E->Name, Name, Len+1);
104
105     /* Insert the entry into the list of all symbols */
106     if (Tab->Last == 0) {
107         /* List is empty */
108         Tab->Root = E;
109     } else {
110         /* List not empty */
111         Tab->Last->List = E;
112     }
113     Tab->Last = E;
114     ++Tab->Count;
115
116     /* Insert the symbol into the hash table */
117     E->Next = Tab->HashTab [Hash];
118     Tab->HashTab [Hash] = E;
119
120     /* Done, return the created entry */
121     return E;
122 }
123
124
125
126 static void FreeExtSym (ExtSym* E)
127 /* Free an external symbol structure. Will not unlink the entry, so internal
128  * use only.
129  */
130 {
131     xfree (E);
132 }
133
134
135
136 ExtSymTab* NewExtSymTab (void)
137 /* Create a new external symbol table */
138 {
139     unsigned I;
140
141     /* Allocate memory */
142     ExtSymTab* Tab = xmalloc (sizeof (ExtSymTab));
143
144     /* Initialize the fields */
145     Tab->Root   = 0;
146     Tab->Last   = 0;
147     Tab->Count  = 0;
148     for (I = 0; I < HASHTAB_SIZE; ++I) {
149         Tab->HashTab [I] = 0;
150     }
151
152     /* Done, return the hash table */
153     return Tab;
154 }
155
156
157
158 void FreeExtSymTab (ExtSymTab* Tab)
159 /* Free an external symbol structure */
160 {
161     /* Free all entries */
162     while (Tab->Root) {
163         ExtSym* E = Tab->Root;
164         Tab->Root = E->Next;
165         FreeExtSym (E);
166     }
167
168     /* Free the struct itself */
169     xfree (Tab);
170 }
171
172
173
174 ExtSym* GetExtSym (const ExtSymTab* Tab, const char* Name)
175 /* Return the entry for the external symbol with the given name. Return NULL
176  * if there is no such symbol.
177  */
178 {
179     /* Hash the name */
180     unsigned Hash = HashStr (Name) % HASHTAB_SIZE;
181
182     /* Check the linked list */
183     ExtSym* E = Tab->HashTab [Hash];
184     while (E) {
185         if (strcmp (E->Name, Name) == 0) {
186             /* Found it */
187             break;
188         }
189         E = E->Next;
190     }
191
192     /* Return the symbol we found */
193     return E;
194 }
195
196
197
198 unsigned ExtSymCount (const ExtSymTab* Tab)
199 /* Return the number of symbols in the table */
200 {
201     return Tab->Count;
202 }
203
204
205
206 const ExtSym* ExtSymList (const ExtSymTab* Tab)
207 /* Return the start of the symbol list sorted by symbol number. Call
208  * ExtSymNext for the next symbol.
209  */
210 {
211     return Tab->Root;
212 }
213
214
215
216 unsigned ExtSymNum (const ExtSym* E)
217 /* Return the number of an external symbol */
218 {
219     return E->Num;
220 }
221
222
223
224 const char* ExtSymName (const ExtSym* E)
225 /* Return the symbol name */
226 {
227     return E->Name;
228 }
229
230
231
232 const ExtSym* ExtSymNext (const ExtSym* E)
233 /* Return the next symbol in the list */
234 {
235     return E->List;
236 }
237
238
239