]> Git Repo - binutils.git/blame - ld/ldsym.c
* Makefile.in: added relax, also made three stage go through a
[binutils.git] / ld / ldsym.c
CommitLineData
2d1a2445
PB
1/* All symbol handling for the linker
2 Copyright (C) 1991 Free Software Foundation, Inc.
3 Written by Steve Chamberlain [email protected]
4
2fa0b342
DHW
5This file is part of GLD, the Gnu Linker.
6
2d1a2445 7This program is free software; you can redistribute it and/or modify
2fa0b342 8it under the terms of the GNU General Public License as published by
2d1a2445
PB
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
2fa0b342 11
2d1a2445 12This program is distributed in the hope that it will be useful,
2fa0b342
DHW
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
2d1a2445
PB
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
2fa0b342
DHW
20
21/*
22 * $Id$
2d1a2445 23 */
2fa0b342
DHW
24
25/*
1af27af8
SC
26 We keep a hash table of global symbols. Each entry in a hash table
27 is called an ldsym_type. Each has three chains; a pointer to a
28 chain of definitions for the symbol (hopefully one long), a pointer
29 to a chain of references to the symbol, and a pointer to a chain of
30 common symbols. Each pointer points into the canonical symbol table
31 provided by bfd, each one of which points to an asymbol. Duringing
32 linkage, the linker uses the udata field to point to the next entry
33 in a canonical table....
34
35
36 ld_sym
37 | |
38 +----------+ +----------+
39 | defs | a canonical symbol table
40 +----------+ +----------+
41 | refs | -----> | one entry| -----> asymbol
42 +----------+ +----------+ | |
43 | coms | | | +---------+
44 +----------+ +----------+ | udata |-----> another canonical symbol
45 +---------+
46
47
48
49 It is very simple to make all the symbol pointers point to the same
50 definition - just run down the chain and make the asymbols pointers
51 within the canonical table point to the asymbol attacthed to the
52 definition of the symbol.
53
54*/
55
2fa0b342 56#include "bfd.h"
f177a611 57#include "sysdep.h"
2fa0b342
DHW
58
59#include "ld.h"
60#include "ldsym.h"
61#include "ldmisc.h"
62#include "ldlang.h"
63/* IMPORT */
64
65extern bfd *output_bfd;
d646b568
SC
66extern strip_symbols_type strip_symbols;
67extern discard_locals_type discard_locals;
2fa0b342
DHW
68/* Head and tail of global symbol table chronological list */
69
70ldsym_type *symbol_head = (ldsym_type *)NULL;
71ldsym_type **symbol_tail_ptr = &symbol_head;
72
73/*
74 incremented for each symbol in the ldsym_type table
75 no matter what flavour it is
76*/
77unsigned int global_symbol_count;
78
79/* IMPORTS */
80
81extern boolean option_longmap ;
82
83/* LOCALS */
84#define TABSIZE 1009
85static ldsym_type *global_symbol_hash_table[TABSIZE];
86
87/* Compute the hash code for symbol name KEY. */
1af27af8
SC
88static
89#ifdef __GNUC__
b773e0e5 90__inline
1af27af8 91#endif
b773e0e5 92
2fa0b342 93int
1af27af8
SC
94DEFUN(hash_string,(key),
95 CONST char *key)
2fa0b342 96{
1af27af8 97 register CONST char *cp;
2fa0b342
DHW
98 register int k;
99
100 cp = key;
101 k = 0;
102 while (*cp)
103 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
104
105 return k;
106}
107
1af27af8
SC
108static
109#ifdef __GNUC__
b773e0e5 110__inline
1af27af8
SC
111#endif ldsym_type *bp;
112ldsym_type *
113DEFUN(search,(key,hashval) ,
114 CONST char *key AND
115 int hashval)
116{
117 ldsym_type *bp;
118 for (bp = global_symbol_hash_table[hashval]; bp; bp = bp->link)
119 if (! strcmp (key, bp->name)) {
120 if (bp->flags & SYM_INDIRECT) {
121 /* Use the symbol we're aliased to instead */
122 return (ldsym_type *)(bp->sdefs_chain);
123 }
124 return bp;
125 }
126 return 0;
127}
128
129
2fa0b342
DHW
130/* Get the symbol table entry for the global symbol named KEY.
131 Create one if there is none. */
132ldsym_type *
99fe4553
SC
133DEFUN(ldsym_get,(key),
134 CONST char *key)
2fa0b342
DHW
135{
136 register int hashval;
137 register ldsym_type *bp;
138
139 /* Determine the proper bucket. */
140
141 hashval = hash_string (key) % TABSIZE;
142
143 /* Search the bucket. */
1af27af8
SC
144 bp = search(key, hashval);
145 if(bp) {
146 return bp;
147 }
2fa0b342
DHW
148
149 /* Nothing was found; create a new symbol table entry. */
150
19b03b7a 151 bp = (ldsym_type *) ldmalloc ((bfd_size_type)(sizeof (ldsym_type)));
2fa0b342
DHW
152 bp->srefs_chain = (asymbol **)NULL;
153 bp->sdefs_chain = (asymbol **)NULL;
154 bp->scoms_chain = (asymbol **)NULL;
99fe4553 155 bp->name = buystring(key);
81016051 156 bp->flags = 0;
2fa0b342
DHW
157 /* Add the entry to the bucket. */
158
159 bp->link = global_symbol_hash_table[hashval];
160 global_symbol_hash_table[hashval] = bp;
161
162 /* Keep the chronological list up to date too */
163 *symbol_tail_ptr = bp;
164 symbol_tail_ptr = &bp->next;
165 bp->next = 0;
166 global_symbol_count++;
167
168 return bp;
169}
170
171/* Like `ldsym_get' but return 0 if the symbol is not already known. */
172
173ldsym_type *
99fe4553
SC
174DEFUN(ldsym_get_soft,(key),
175 CONST char *key)
2fa0b342
DHW
176{
177 register int hashval;
2fa0b342
DHW
178 /* Determine which bucket. */
179
180 hashval = hash_string (key) % TABSIZE;
181
182 /* Search the bucket. */
7fe11a82 183 return search(key, hashval);
2fa0b342
DHW
184}
185
186
187
188
189
190static void
191list_file_locals (entry)
192lang_input_statement_type *entry;
193{
194 asymbol **q;
19b03b7a 195 printf ( "\nLocal symbols of ");
2fa0b342 196 info("%I", entry);
19b03b7a 197 printf (":\n\n");
2fa0b342
DHW
198 if (entry->asymbols) {
199 for (q = entry->asymbols; *q; q++)
200 {
201 asymbol *p = *q;
202 /* If this is a definition,
203 update it if necessary by this file's start address. */
204 if (p->flags & BSF_LOCAL)
205 info(" %V %s\n",p->value, p->name);
206 }
207 }
208}
209
210
211static void
212print_file_stuff(f)
213lang_input_statement_type *f;
214{
48491e2e 215 fprintf (stdout, " %s\n", f->filename);
2fa0b342 216 if (f->just_syms_flag)
c611e285
SC
217 {
218 fprintf (stdout, " symbols only\n");
219 }
2fa0b342 220 else
c611e285
SC
221 {
222 asection *s;
223 if (true || option_longmap) {
224 for (s = f->the_bfd->sections;
225 s != (asection *)NULL;
226 s = s->next) {
48491e2e 227 print_address(s->output_offset);
c611e285
SC
228 if (s->flags & SEC_HAS_CONTENTS)
229 {
230 printf (" %08x 2**%2ud %s\n",
231 (unsigned)bfd_get_section_size_after_reloc(s),
232 s->alignment_power, s->name);
233 }
234
235 else
236 {
237 printf (" %08x 2**%2ud %s\n",
238 (unsigned)bfd_get_section_size_before_reloc(s),
239 s->alignment_power, s->name);
240 }
241
242
243
48491e2e 244 }
c611e285
SC
245 }
246 else {
247 for (s = f->the_bfd->sections;
248 s != (asection *)NULL;
249 s = s->next) {
48491e2e
SC
250 printf("%s ", s->name);
251 print_address(s->output_offset);
c611e285 252 printf("(%x)", (unsigned)bfd_get_section_size_after_reloc(s));
48491e2e 253 }
c611e285 254 printf("hex \n");
2fa0b342 255 }
c611e285 256 }
48491e2e 257 fprintf (stdout, "\n");
2fa0b342
DHW
258}
259
260void
261ldsym_print_symbol_table ()
262{
48491e2e 263 fprintf (stdout, "**FILES**\n\n");
2fa0b342
DHW
264
265 lang_for_each_file(print_file_stuff);
266
48491e2e
SC
267 fprintf(stdout, "**GLOBAL SYMBOLS**\n\n");
268 fprintf(stdout, "offset section offset symbol\n");
2fa0b342
DHW
269 {
270 register ldsym_type *sp;
271
272 for (sp = symbol_head; sp; sp = sp->next)
273 {
1af27af8
SC
274 if (sp->flags & SYM_INDIRECT) {
275 fprintf(stdout,"indirect %s to %s\n",
276 sp->name, (((ldsym_type *)(sp->sdefs_chain))->name));
2fa0b342
DHW
277 }
278 else {
d9c53949
SC
279 if (sp->sdefs_chain)
280 {
281 asymbol *defsym = *(sp->sdefs_chain);
282 asection *defsec = bfd_get_section(defsym);
283 print_address(defsym->value);
284 if (defsec)
285 {
286 printf(" %-10s",
287 bfd_section_name(output_bfd,
288 defsec));
289 print_space();
290 print_address(defsym->value+defsec->vma);
291
292 }
293 else
294 {
295 printf(" .......");
296 }
297
298 }
299
300
301 if (sp->scoms_chain) {
302 printf("common ");
303 print_address((*(sp->scoms_chain))->value);
304 printf(" %s ",sp->name);
305 }
306 else if (sp->sdefs_chain) {
307 printf(" %s ",sp->name);
308 }
309 else {
310 printf("undefined ");
311 printf("%s ",sp->name);
1af27af8 312
d9c53949 313 }
2fa0b342 314 }
19b03b7a 315 print_nl();
2fa0b342
DHW
316
317 }
318 }
d9c53949
SC
319 if (option_longmap) {
320 lang_for_each_file(list_file_locals);
321 }
2fa0b342
DHW
322}
323
324extern lang_output_section_statement_type *create_object_symbols;
325extern char lprefix;
326static asymbol **
327write_file_locals(output_buffer)
328asymbol **output_buffer;
329{
330LANG_FOR_EACH_INPUT_STATEMENT(entry)
331 {
332 /* Run trough the symbols and work out what to do with them */
333 unsigned int i;
334
335 /* Add one for the filename symbol if needed */
336 if (create_object_symbols
337 != (lang_output_section_statement_type *)NULL) {
338 asection *s;
339 for (s = entry->the_bfd->sections;
340 s != (asection *)NULL;
341 s = s->next) {
342 if (s->output_section == create_object_symbols->bfd_section) {
343 /* Add symbol to this section */
344 asymbol * newsym =
345 (asymbol *)bfd_make_empty_symbol(entry->the_bfd);
346 newsym->name = entry->local_sym_name;
347 /* The symbol belongs to the output file's text section */
348
349 /* The value is the start of this section in the output file*/
350 newsym->value = 0;
351 newsym->flags = BSF_LOCAL;
352 newsym->section = s;
353 *output_buffer++ = newsym;
354 break;
355 }
356 }
357 }
358 for (i = 0; i < entry->symbol_count; i++)
359 {
360 asymbol *p = entry->asymbols[i];
361
c611e285 362 if (flag_is_global(p->flags) )
2fa0b342
DHW
363 {
364 /* We are only interested in outputting
365 globals at this stage in special circumstances */
366 if (p->the_bfd == entry->the_bfd
367 && flag_is_not_at_end(p->flags)) {
368 /* And this is one of them */
369 *(output_buffer++) = p;
370 p->flags |= BSF_KEEP;
371 }
372 }
373 else {
374 if (flag_is_ordinary_local(p->flags))
375 {
376 if (discard_locals == DISCARD_ALL)
377 { }
378 else if (discard_locals == DISCARD_L &&
379 (p->name[0] == lprefix))
380 { }
381 else if (p->flags == BSF_WARNING)
382 { }
383 else
384 { *output_buffer++ = p; }
385 }
386 else if (flag_is_debugger(p->flags))
387 {
388 /* Only keep the debugger symbols if no stripping required */
389 if (strip_symbols == STRIP_NONE) {
390 *output_buffer++ = p;
391 }
392 }
c611e285 393 else if (p->section == &bfd_und_section)
2fa0b342
DHW
394 { /* This must be global */
395 }
c611e285 396 else if (p->section == &bfd_com_section) {
2fa0b342
DHW
397 /* And so must this */
398 }
399 else if (p->flags & BSF_CTOR) {
400 /* Throw it away */
401 }
402else
403 {
404 FAIL();
405 }
406 }
407 }
408
409
410 }
411 return output_buffer;
412}
413
414
415static asymbol **
416write_file_globals(symbol_table)
417asymbol **symbol_table;
418{
419 FOR_EACH_LDSYM(sp)
420 {
1af27af8 421 if ((sp->flags & SYM_INDIRECT) == 0 && sp->sdefs_chain != (asymbol **)NULL) {
2fa0b342
DHW
422 asymbol *bufp = (*(sp->sdefs_chain));
423
424 if ((bufp->flags & BSF_KEEP) ==0) {
425 ASSERT(bufp != (asymbol *)NULL);
426
427 bufp->name = sp->name;
428
429 if (sp->scoms_chain != (asymbol **)NULL)
430
431 {
432 /*
433 defined as common but not allocated, this happens
434 only with -r and not -d, write out a common
435 definition
436 */
437 bufp = *(sp->scoms_chain);
438 }
439 *symbol_table++ = bufp;
440 }
441 }
442 else if (sp->scoms_chain != (asymbol **)NULL) {
443 /* This symbol is a common - just output */
444 asymbol *bufp = (*(sp->scoms_chain));
445 *symbol_table++ = bufp;
446 }
447 else if (sp->srefs_chain != (asymbol **)NULL) {
448 /* This symbol is undefined but has a reference */
449 asymbol *bufp = (*(sp->srefs_chain));
450 *symbol_table++ = bufp;
451 }
452 else {
453 /*
454 This symbol has neither defs nor refs, it must have come
455 from the command line, since noone has used it it has no
456 data attatched, so we'll ignore it
457 */
458 }
459 }
460 return symbol_table;
461}
462
463
464
465void
466ldsym_write()
467{
468 if (strip_symbols != STRIP_ALL) {
469 /* We know the maximum size of the symbol table -
470 it's the size of all the global symbols ever seen +
471 the size of all the symbols from all the files +
472 the number of files (for the per file symbols)
473 +1 (for the null at the end)
474 */
475 extern unsigned int total_files_seen;
476 extern unsigned int total_symbols_seen;
477
478 asymbol ** symbol_table = (asymbol **)
19b03b7a 479 ldmalloc ((bfd_size_type)(global_symbol_count +
2fa0b342
DHW
480 total_files_seen +
481 total_symbols_seen + 1) * sizeof (asymbol *));
482 asymbol ** tablep = write_file_locals(symbol_table);
483
484 tablep = write_file_globals(tablep);
485
486 *tablep = (asymbol *)NULL;
487 bfd_set_symtab(output_bfd, symbol_table, (unsigned)( tablep - symbol_table));
488 }
489}
c660714f
SC
490
491/*
492return true if the supplied symbol name is not in the
493linker symbol table
494*/
495boolean
99fe4553
SC
496DEFUN(ldsym_undefined,(sym),
497 CONST char *sym)
c660714f
SC
498{
499 ldsym_type *from_table = ldsym_get_soft(sym);
500 if (from_table != (ldsym_type *)NULL) {
501 if (from_table->sdefs_chain != (asymbol **)NULL) return false;
502 }
503 return true;
504}
This page took 0.161866 seconds and 4 git commands to generate.