2 * This code is derived from software copyrighted by the Free Software
5 * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
7 * Modified 1993 by Paul Kranenburg, Erasmus University
10 /* Derived from ld.c: "@(#)ld.c 6.10 (Berkeley) 5/22/91"; */
12 /* Linker `ld' for GNU
13 Copyright (C) 1988 Free Software Foundation, Inc.
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)
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.
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. */
29 /* Written by Richard Stallman with some help from Eric Albert.
30 Set, indirect, and warning symbol features added by Randy Smith. */
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 $
38 /* Create the symbol table entries for `etext', `edata' and `end'. */
40 #include <sys/param.h>
41 #include <sys/types.h>
52 symbol *symtab[SYMTABSIZE]; /* The symbol table. */
53 int num_hash_tab_syms; /* Number of symbols in symbol hash table. */
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 */
62 symtab_init(relocatable_output)
63 int relocatable_output;
66 * Put linker reserved symbols into symbol table.
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_"
75 #define ETEXT_SYM "etext"
76 #define EDATA_SYM "edata"
78 #define DYN_SYM "_DYNAMIC"
79 #define GOT_SYM "_GLOBAL_OFFSET_TABLE_"
82 dynamic_symbol = getsym(DYN_SYM);
83 dynamic_symbol->defined = relocatable_output?N_UNDF:(N_DATA | N_EXT);
85 got_symbol = getsym(GOT_SYM);
86 got_symbol->defined = N_DATA | N_EXT;
88 if (relocatable_output)
91 etext_symbol = getsym(ETEXT_SYM);
92 edata_symbol = getsym(EDATA_SYM);
93 end_symbol = getsym(END_SYM);
95 etext_symbol->defined = N_TEXT | N_EXT;
96 edata_symbol->defined = N_DATA | N_EXT;
97 end_symbol->defined = N_BSS | N_EXT;
99 etext_symbol->flags |= GS_REFERENCED;
100 edata_symbol->flags |= GS_REFERENCED;
101 end_symbol->flags |= GS_REFERENCED;
105 * Compute the hash code for symbol name KEY.
118 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
124 * Get the symbol table entry for the global symbol named KEY.
125 * Create one if there is none.
132 register int hashval;
135 /* Determine the proper bucket. */
136 hashval = hash_string(key) % SYMTABSIZE;
138 /* Search the bucket. */
139 for (bp = symtab[hashval]; bp; bp = bp->link)
140 if (strcmp(key, bp->name) == 0)
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);
157 bp->rrs_symbolnum = 0;
164 bp->jmpslot_offset = -1;
165 bp->gotslot_offset = -1;
168 /* Add the entry to the bucket. */
169 bp->link = symtab[hashval];
170 symtab[hashval] = bp;
177 /* Like `getsym' but return 0 if the symbol is not already known. */
183 register int hashval;
186 /* Determine which bucket. */
187 hashval = hash_string(key) % SYMTABSIZE;
189 /* Search the bucket. */
190 for (bp = symtab[hashval]; bp; bp = bp->link)
191 if (strcmp(key, bp->name) == 0)