1 /*****************************************************************************/
5 /* Handle program external symbols for relocatable output formats */
9 /* (C) 1999-2001 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 /*****************************************************************************/
48 /*****************************************************************************/
50 /*****************************************************************************/
54 /* Structure holding an external symbol */
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 */
63 /* External symbol table structure */
64 #define HASHTAB_SIZE 53
66 ExtSym* Root; /* List of symbols */
67 ExtSym* Last; /* Pointer to last symbol */
68 unsigned Count; /* Number of symbols */
69 ExtSym* HashTab [HASHTAB_SIZE];
74 /*****************************************************************************/
76 /*****************************************************************************/
80 ExtSym* NewExtSym (ExtSymTab* Tab, const char* Name)
81 /* Create a new external symbol and insert it into the table */
83 /* Get the hash value of the string */
84 unsigned Hash = HashStr (Name) % HASHTAB_SIZE;
86 /* Get the length of the name */
87 unsigned Len = strlen (Name);
89 /* Check for duplicates */
90 ExtSym* E = GetExtSym (Tab, Name); /* Don't care about duplicate hash here... */
92 /* We do already have a symbol with this name */
93 Error ("Duplicate external symbol `%s'", Name);
96 /* Allocate memory for the structure */
97 E = xmalloc (sizeof (ExtSym) + Len);
99 /* Initialize the structure */
103 memcpy (E->Name, Name, Len+1);
105 /* Insert the entry into the list of all symbols */
106 if (Tab->Last == 0) {
116 /* Insert the symbol into the hash table */
117 E->Next = Tab->HashTab [Hash];
118 Tab->HashTab [Hash] = E;
120 /* Done, return the created entry */
126 static void FreeExtSym (ExtSym* E)
127 /* Free an external symbol structure. Will not unlink the entry, so internal
136 ExtSymTab* NewExtSymTab (void)
137 /* Create a new external symbol table */
141 /* Allocate memory */
142 ExtSymTab* Tab = xmalloc (sizeof (ExtSymTab));
144 /* Initialize the fields */
148 for (I = 0; I < HASHTAB_SIZE; ++I) {
149 Tab->HashTab [I] = 0;
152 /* Done, return the hash table */
158 void FreeExtSymTab (ExtSymTab* Tab)
159 /* Free an external symbol structure */
161 /* Free all entries */
163 ExtSym* E = Tab->Root;
168 /* Free the struct itself */
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.
180 unsigned Hash = HashStr (Name) % HASHTAB_SIZE;
182 /* Check the linked list */
183 ExtSym* E = Tab->HashTab [Hash];
185 if (strcmp (E->Name, Name) == 0) {
192 /* Return the symbol we found */
198 unsigned ExtSymCount (const ExtSymTab* Tab)
199 /* Return the number of symbols in the table */
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.
216 unsigned ExtSymNum (const ExtSym* E)
217 /* Return the number of an external symbol */
224 const char* ExtSymName (const ExtSym* E)
225 /* Return the symbol name */
232 const ExtSym* ExtSymNext (const ExtSym* E)
233 /* Return the next symbol in the list */