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