Commit | Line | Data |
---|---|---|
1136f72d | 1 | /* |
6a61ea88 | 2 | * $Id: symbol.c,v 1.3 1993/11/22 19:04:45 jkh Exp $ - symbol table routines |
1136f72d PR |
3 | */ |
4 | ||
5 | /* Create the symbol table entries for `etext', `edata' and `end'. */ | |
6 | ||
7 | #include <sys/param.h> | |
8 | #include <stdio.h> | |
9 | #include <stdlib.h> | |
10 | #include <sys/types.h> | |
11 | #include <fcntl.h> | |
12 | #include <a.out.h> | |
13 | #include <stab.h> | |
14 | #include <string.h> | |
15 | ||
16 | #include "ld.h" | |
17 | ||
6a61ea88 JH |
18 | symbol *symtab[SYMTABSIZE]; /* The symbol table. */ |
19 | int num_hash_tab_syms; /* Number of symbols in symbol hash table. */ | |
20 | ||
21 | symbol *edata_symbol; /* the symbol _edata */ | |
22 | symbol *etext_symbol; /* the symbol _etext */ | |
23 | symbol *end_symbol; /* the symbol _end */ | |
24 | symbol *got_symbol; /* the symbol __GLOBAL_OFFSET_TABLE_ */ | |
25 | symbol *dynamic_symbol; /* the symbol __DYNAMIC */ | |
26 | ||
1136f72d PR |
27 | void |
28 | symtab_init (relocatable_output) | |
29 | int relocatable_output; | |
30 | { | |
31 | /* | |
32 | * Put linker reserved symbols into symbol table. | |
33 | */ | |
6a61ea88 JH |
34 | #ifndef nounderscore |
35 | #define ETEXT_SYM "_etext" | |
36 | #define EDATA_SYM "_edata" | |
37 | #define END_SYM "_end" | |
38 | #define DYN_SYM "__DYNAMIC" | |
39 | #define GOT_SYM "__GLOBAL_OFFSET_TABLE_" | |
40 | #else | |
41 | #define ETEXT_SYM "etext" | |
42 | #define EDATA_SYM "edata" | |
43 | #define END_SYM "end" | |
44 | #define DYN_SYM "_DYNAMIC" | |
45 | #define GOT_SYM "_GLOBAL_OFFSET_TABLE_" | |
46 | #endif | |
1136f72d | 47 | |
6a61ea88 | 48 | dynamic_symbol = getsym (DYN_SYM); |
1136f72d | 49 | dynamic_symbol->defined = relocatable_output?N_UNDF:(N_DATA | N_EXT); |
1136f72d | 50 | |
6a61ea88 | 51 | got_symbol = getsym (GOT_SYM); |
1136f72d | 52 | got_symbol->defined = N_DATA | N_EXT; |
1136f72d PR |
53 | |
54 | if (relocatable_output) | |
55 | return; | |
56 | ||
6a61ea88 JH |
57 | etext_symbol = getsym (ETEXT_SYM); |
58 | edata_symbol = getsym (EDATA_SYM); | |
59 | end_symbol = getsym (END_SYM); | |
60 | ||
1136f72d | 61 | etext_symbol->defined = N_TEXT | N_EXT; |
6a61ea88 | 62 | edata_symbol->defined = N_DATA | N_EXT; |
1136f72d PR |
63 | end_symbol->defined = N_BSS | N_EXT; |
64 | ||
6a61ea88 JH |
65 | etext_symbol->flags |= GS_REFERENCED; |
66 | edata_symbol->flags |= GS_REFERENCED; | |
67 | end_symbol->flags |= GS_REFERENCED; | |
1136f72d PR |
68 | } |
69 | ||
70 | /* Compute the hash code for symbol name KEY. */ | |
71 | ||
72 | int | |
73 | hash_string (key) | |
74 | char *key; | |
75 | { | |
76 | register char *cp; | |
77 | register int k; | |
78 | ||
79 | cp = key; | |
80 | k = 0; | |
81 | while (*cp) | |
82 | k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; | |
83 | ||
84 | return k; | |
85 | } | |
86 | ||
87 | /* Get the symbol table entry for the global symbol named KEY. | |
88 | Create one if there is none. */ | |
89 | ||
90 | symbol * | |
91 | getsym(key) | |
92 | char *key; | |
93 | { | |
94 | register int hashval; | |
95 | register symbol *bp; | |
96 | ||
97 | /* Determine the proper bucket. */ | |
6a61ea88 | 98 | hashval = hash_string (key) % SYMTABSIZE; |
1136f72d PR |
99 | |
100 | /* Search the bucket. */ | |
101 | for (bp = symtab[hashval]; bp; bp = bp->link) | |
102 | if (! strcmp (key, bp->name)) | |
103 | return bp; | |
104 | ||
105 | /* Nothing was found; create a new symbol table entry. */ | |
106 | bp = (symbol *) xmalloc (sizeof (symbol)); | |
1136f72d PR |
107 | bp->name = (char *) xmalloc (strlen (key) + 1); |
108 | strcpy (bp->name, key); | |
6a61ea88 | 109 | bp->refs = 0; |
1136f72d | 110 | bp->defined = 0; |
1136f72d | 111 | bp->value = 0; |
6a61ea88 | 112 | bp->common_size = 0; |
1136f72d PR |
113 | bp->warning = 0; |
114 | bp->undef_refs = 0; | |
6a61ea88 | 115 | bp->mult_defs = 0; |
1136f72d PR |
116 | bp->alias = 0; |
117 | bp->setv_count = 0; | |
80f25b52 JH |
118 | bp->symbolnum = 0; |
119 | bp->rrs_symbolnum = 0; | |
1136f72d PR |
120 | |
121 | bp->size = 0; | |
80f25b52 | 122 | bp->aux = 0; |
1136f72d PR |
123 | bp->sorefs = 0; |
124 | bp->so_defined = 0; | |
125 | bp->def_nlist = 0; | |
126 | bp->jmpslot_offset = -1; | |
127 | bp->gotslot_offset = -1; | |
6a61ea88 | 128 | bp->flags = 0; |
1136f72d PR |
129 | |
130 | /* Add the entry to the bucket. */ | |
131 | bp->link = symtab[hashval]; | |
132 | symtab[hashval] = bp; | |
133 | ||
134 | ++num_hash_tab_syms; | |
135 | ||
136 | return bp; | |
137 | } | |
138 | ||
139 | /* Like `getsym' but return 0 if the symbol is not already known. */ | |
140 | ||
141 | symbol * | |
142 | getsym_soft (key) | |
143 | char *key; | |
144 | { | |
145 | register int hashval; | |
146 | register symbol *bp; | |
147 | ||
148 | /* Determine which bucket. */ | |
149 | ||
6a61ea88 | 150 | hashval = hash_string (key) % SYMTABSIZE; |
1136f72d PR |
151 | |
152 | /* Search the bucket. */ | |
153 | ||
154 | for (bp = symtab[hashval]; bp; bp = bp->link) | |
155 | if (! strcmp (key, bp->name)) | |
156 | return bp; | |
157 | ||
158 | return 0; | |
159 | } |