1 /*****************************************************************************/
5 /* Handle program external symbols for relocatable output formats */
9 /* (C) 1999 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
38 #include "../common/hashstr.h"
39 #include "../common/xmalloc.h"
46 /*****************************************************************************/
48 /*****************************************************************************/
52 /* Structure holding an external symbol */
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 */
61 /* External symbol table structure */
62 #define HASHTAB_SIZE 53
64 ExtSym* Root; /* List of symbols */
65 ExtSym* Last; /* Pointer to last symbol */
66 unsigned Count; /* Number of symbols */
67 ExtSym* HashTab [HASHTAB_SIZE];
72 /*****************************************************************************/
74 /*****************************************************************************/
78 ExtSym* NewExtSym (ExtSymTab* Tab, const char* Name)
79 /* Create a new external symbol and insert it into the table */
81 /* Get the hash value of the string */
82 unsigned Hash = HashStr (Name) % HASHTAB_SIZE;
84 /* Get the length of the name */
85 unsigned Len = strlen (Name);
87 /* Check for duplicates */
88 ExtSym* E = GetExtSym (Tab, Name); /* Don't care about duplicate hash here... */
90 /* We do already have a symbol with this name */
91 Error ("Duplicate external symbol `%s'", Name);
94 /* Allocate memory for the structure */
95 E = xmalloc (sizeof (ExtSym) + Len);
97 /* Initialize the structure */
101 memcpy (E->Name, Name, Len+1);
103 /* Insert the entry into the list of all symbols */
104 if (Tab->Last == 0) {
114 /* Insert the symbol into the hash table */
115 E->Next = Tab->HashTab [Hash];
116 Tab->HashTab [Hash] = E;
118 /* Done, return the created entry */
124 static void FreeExtSym (ExtSym* E)
125 /* Free an external symbol structure. Will not unlink the entry, so internal
134 ExtSymTab* NewExtSymTab (void)
135 /* Create a new external symbol table */
139 /* Allocate memory */
140 ExtSymTab* Tab = xmalloc (sizeof (ExtSymTab));
142 /* Initialize the fields */
146 for (I = 0; I < HASHTAB_SIZE; ++I) {
147 Tab->HashTab [I] = 0;
150 /* Done, return the hash table */
156 void FreeExtSymTab (ExtSymTab* Tab)
157 /* Free an external symbol structure */
159 /* Free all entries */
161 ExtSym* E = Tab->Root;
166 /* Free the struct itself */
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.
178 unsigned Hash = HashStr (Name) % HASHTAB_SIZE;
180 /* Check the linked list */
181 ExtSym* E = Tab->HashTab [Hash];
183 if (strcmp (E->Name, Name) == 0) {
190 /* Return the symbol we found */
196 unsigned ExtSymCount (const ExtSymTab* Tab)
197 /* Return the number of symbols in the table */
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.
214 unsigned ExtSymNum (const ExtSym* E)
215 /* Return the number of an external symbol */
222 const char* ExtSymName (const ExtSym* E)
223 /* Return the symbol name */
230 const ExtSym* ExtSymNext (const ExtSym* E)
231 /* Return the next symbol in the list */