Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / gnu / usr.bin / ld / symbol.c
1 /*-
2  * This code is derived from software copyrighted by the Free Software
3  * Foundation.
4  *
5  * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
6  *
7  * Modified 1993 by Paul Kranenburg, Erasmus University
8  */
9
10 /* Derived from ld.c: "@(#)ld.c 6.10 (Berkeley) 5/22/91"; */
11
12 /* Linker `ld' for GNU
13    Copyright (C) 1988 Free Software Foundation, Inc.
14
15    This program is free software; you can redistribute it and/or modify
16    it under the terms of the GNU General Public License as published by
17    the Free Software Foundation; either version 1, or (at your option)
18    any later version.
19
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23    GNU General Public License for more details.
24
25    You should have received a copy of the GNU General Public License
26    along with this program; if not, write to the Free Software
27    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
28
29 /* Written by Richard Stallman with some help from Eric Albert.
30    Set, indirect, and warning symbol features added by Randy Smith. */
31
32 /*
33  * symbol table routines
34  * $FreeBSD: src/gnu/usr.bin/ld/symbol.c,v 1.10 1999/08/27 23:36:02 peter Exp $
35  * $DragonFly: src/gnu/usr.bin/ld/Attic/symbol.c,v 1.2 2003/06/17 04:25:46 dillon Exp $
36  */
37
38 /* Create the symbol table entries for `etext', `edata' and `end'.  */
39
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <fcntl.h>
43 #include <a.out.h>
44 #include <stab.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #include "ld.h"
50 #include "dynamic.h"
51
52 symbol  *symtab[SYMTABSIZE];    /* The symbol table. */
53 int     num_hash_tab_syms;      /* Number of symbols in symbol hash table. */
54
55 symbol  *edata_symbol;          /* the symbol _edata */
56 symbol  *etext_symbol;          /* the symbol _etext */
57 symbol  *end_symbol;            /* the symbol _end */
58 symbol  *got_symbol;            /* the symbol __GLOBAL_OFFSET_TABLE_ */
59 symbol  *dynamic_symbol;        /* the symbol __DYNAMIC */
60
61 void
62 symtab_init(relocatable_output)
63         int     relocatable_output;
64 {
65         /*
66          * Put linker reserved symbols into symbol table.
67          */
68 #ifndef nounderscore
69 #define ETEXT_SYM       "_etext"
70 #define EDATA_SYM       "_edata"
71 #define END_SYM         "_end"
72 #define DYN_SYM         "__DYNAMIC"
73 #define GOT_SYM         "__GLOBAL_OFFSET_TABLE_"
74 #else
75 #define ETEXT_SYM       "etext"
76 #define EDATA_SYM       "edata"
77 #define END_SYM         "end"
78 #define DYN_SYM         "_DYNAMIC"
79 #define GOT_SYM         "_GLOBAL_OFFSET_TABLE_"
80 #endif
81
82         dynamic_symbol = getsym(DYN_SYM);
83         dynamic_symbol->defined = relocatable_output?N_UNDF:(N_DATA | N_EXT);
84
85         got_symbol = getsym(GOT_SYM);
86         got_symbol->defined = N_DATA | N_EXT;
87
88         if (relocatable_output)
89                 return;
90
91         etext_symbol = getsym(ETEXT_SYM);
92         edata_symbol = getsym(EDATA_SYM);
93         end_symbol = getsym(END_SYM);
94
95         etext_symbol->defined = N_TEXT | N_EXT;
96         edata_symbol->defined = N_DATA | N_EXT;
97         end_symbol->defined = N_BSS | N_EXT;
98
99         etext_symbol->flags |= GS_REFERENCED;
100         edata_symbol->flags |= GS_REFERENCED;
101         end_symbol->flags |= GS_REFERENCED;
102 }
103
104 /*
105  * Compute the hash code for symbol name KEY.
106  */
107
108 int
109 hash_string (key)
110      char *key;
111 {
112         register char *cp;
113         register int k;
114
115         cp = key;
116         k = 0;
117         while (*cp)
118                 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
119
120         return k;
121 }
122
123 /*
124  * Get the symbol table entry for the global symbol named KEY.
125  * Create one if there is none.
126  */
127
128 symbol *
129 getsym(key)
130         char *key;
131 {
132         register int hashval;
133         register symbol *bp;
134
135         /* Determine the proper bucket.  */
136         hashval = hash_string(key) % SYMTABSIZE;
137
138         /* Search the bucket.  */
139         for (bp = symtab[hashval]; bp; bp = bp->link)
140                 if (strcmp(key, bp->name) == 0)
141                         return bp;
142
143         /* Nothing was found; create a new symbol table entry.  */
144         bp = (symbol *)xmalloc(sizeof(symbol));
145         bp->name = (char *)xmalloc(strlen(key) + 1);
146         strcpy (bp->name, key);
147         bp->refs = 0;
148         bp->defined = 0;
149         bp->value = 0;
150         bp->common_size = 0;
151         bp->warning = 0;
152         bp->undef_refs = 0;
153         bp->mult_defs = 0;
154         bp->alias = 0;
155         bp->setv_count = 0;
156         bp->symbolnum = 0;
157         bp->rrs_symbolnum = 0;
158
159         bp->size = 0;
160         bp->aux = 0;
161         bp->sorefs = 0;
162         bp->so_defined = 0;
163         bp->def_lsp = 0;
164         bp->jmpslot_offset = -1;
165         bp->gotslot_offset = -1;
166         bp->flags = 0;
167
168         /* Add the entry to the bucket.  */
169         bp->link = symtab[hashval];
170         symtab[hashval] = bp;
171
172         ++num_hash_tab_syms;
173
174         return bp;
175 }
176
177 /* Like `getsym' but return 0 if the symbol is not already known.  */
178
179 symbol *
180 getsym_soft (key)
181         char *key;
182 {
183         register int hashval;
184         register symbol *bp;
185
186         /* Determine which bucket. */
187         hashval = hash_string(key) % SYMTABSIZE;
188
189         /* Search the bucket. */
190         for (bp = symtab[hashval]; bp; bp = bp->link)
191                 if (strcmp(key, bp->name) == 0)
192                         return bp;
193
194         return 0;
195 }