1 | /*****************************************************************************/ |
2 | /* */ |
3 | /* exports.c */ |
4 | /* */ |
5 | /* Duplicate export checking for the ar65 archiver */ |
6 | /* */ |
7 | /* */ |
8 | /* */ |
9 | /* (C) 1998-2012, Ullrich von Bassewitz */ |
10 | /* Roemerstrasse 52 */ |
11 | /* D-70794 Filderstadt */ |
12 | /* EMail: uz@cc65.org */ |
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 "hashfunc.h" |
40 | #include "xmalloc.h" |
41 | |
42 | /* ar65 */ |
43 | #include "error.h" |
44 | #include "library.h" |
45 | #include "objdata.h" |
46 | #include "exports.h" |
47 | |
48 | |
49 | |
50 | /*****************************************************************************/ |
51 | /* Data */ |
52 | /*****************************************************************************/ |
53 | |
54 | |
55 | |
56 | /* A hash table entry */ |
57 | typedef struct HashEntry HashEntry; |
58 | struct HashEntry { |
59 | HashEntry* Next; /* Next in list */ |
60 | const ObjData* Module; /* Pointer to object module */ |
61 | char Name [1]; /* Name of identifier */ |
62 | }; |
63 | |
64 | /* Hash table */ |
65 | #define HASHTAB_SIZE 4783 |
66 | static HashEntry* HashTab [HASHTAB_SIZE]; |
67 | |
68 | |
69 | |
70 | /*****************************************************************************/ |
71 | /* Code */ |
72 | /*****************************************************************************/ |
73 | |
74 | |
75 | |
76 | static HashEntry* NewHashEntry (const char* Name, const ObjData* Module) |
77 | /* Create and return a new hash entry */ |
78 | { |
79 | /* Get the length of the name */ |
80 | unsigned Len = strlen (Name); |
81 | |
82 | /* Get memory for the struct */ |
83 | HashEntry* H = xmalloc (sizeof (HashEntry) + Len); |
84 | |
85 | /* Initialize the fields and return it */ |
86 | H->Next = 0; |
87 | H->Module = Module; |
88 | memcpy (H->Name, Name, Len); |
89 | H->Name [Len] = '\0'; |
90 | return H; |
91 | } |
92 | |
93 | |
94 | |
95 | void ExpInsert (const char* Name, const ObjData* Module) |
96 | /* Insert an exported identifier and check if it's already in the list */ |
97 | { |
98 | HashEntry* L; |
99 | |
100 | /* Create a hash value for the given name */ |
101 | unsigned HashVal = HashStr (Name) % HASHTAB_SIZE; |
102 | |
103 | /* Create a new hash entry */ |
104 | HashEntry* H = NewHashEntry (Name, Module); |
105 | |
106 | /* Search through the list in that slot and print matching duplicates */ |
107 | if (HashTab [HashVal] == 0) { |
108 | /* The slot is empty */ |
109 | HashTab [HashVal] = H; |
110 | return; |
111 | } |
112 | L = HashTab [HashVal]; |
113 | while (1) { |
114 | if (strcmp (L->Name, Name) == 0) { |
115 | /* Duplicate entry */ |
116 | Warning ("External symbol '%s' in module '%s', library '%s', " |
117 | "is duplicated in module '%s'" , |
118 | Name, L->Module->Name, LibName, Module->Name); |
119 | } |
120 | if (L->Next == 0) { |
121 | break; |
122 | } else { |
123 | L = L->Next; |
124 | } |
125 | } |
126 | L->Next = H; |
127 | } |
128 | |
129 | |
130 | |
131 | const ObjData* ExpFind (const char* Name) |
132 | /* Check for an identifier in the list. Return NULL if not found, otherwise |
133 | ** return a pointer to the module, that exports the identifer. |
134 | */ |
135 | { |
136 | /* Get a pointer to the list with the symbols hash value */ |
137 | HashEntry* L = HashTab [HashStr (Name) % HASHTAB_SIZE]; |
138 | while (L) { |
139 | /* Search through the list in that slot */ |
140 | if (strcmp (L->Name, Name) == 0) { |
141 | /* Entry found */ |
142 | return L->Module; |
143 | } |
144 | L = L->Next; |
145 | } |
146 | |
147 | /* Not found */ |
148 | return 0; |
149 | } |
150 | |