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