Pass emulation name without ".sh".
[deliverable/binutils-gdb.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 steve@cygnus.com
29f33467 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 20
29f33467 21/*
1af27af8
SC
22 We keep a hash table of global symbols. Each entry in a hash table
23 is called an ldsym_type. Each has three chains; a pointer to a
24 chain of definitions for the symbol (hopefully one long), a pointer
25 to a chain of references to the symbol, and a pointer to a chain of
26 common symbols. Each pointer points into the canonical symbol table
32846f9c 27 provided by bfd, each one of which points to an asymbol. During
1af27af8
SC
28 linkage, the linker uses the udata field to point to the next entry
29 in a canonical table....
30
31
32 ld_sym
33 | |
34 +----------+ +----------+
35 | defs | a canonical symbol table
36 +----------+ +----------+
37 | refs | -----> | one entry| -----> asymbol
38 +----------+ +----------+ | |
39 | coms | | | +---------+
2a1c2bad
KR
40 +----------+ +----------+ | udata |-----> another canonical
41 +---------+ symbol
1af27af8
SC
42
43
44
45 It is very simple to make all the symbol pointers point to the same
46 definition - just run down the chain and make the asymbols pointers
47 within the canonical table point to the asymbol attacthed to the
48 definition of the symbol.
49
50*/
51
2fa0b342 52#include "bfd.h"
f177a611 53#include "sysdep.h"
2fa0b342
DHW
54
55#include "ld.h"
56#include "ldsym.h"
57#include "ldmisc.h"
58#include "ldlang.h"
59/* IMPORT */
0b5995da 60extern int symbol_truncate;
2fa0b342 61extern bfd *output_bfd;
d646b568
SC
62extern strip_symbols_type strip_symbols;
63extern discard_locals_type discard_locals;
2fa0b342
DHW
64/* Head and tail of global symbol table chronological list */
65
29f33467 66ldsym_type *symbol_head = (ldsym_type *) NULL;
2fa0b342 67ldsym_type **symbol_tail_ptr = &symbol_head;
f3b36ecb
KR
68CONST char *keepsyms_file;
69int kept_syms;
2fa0b342 70
6fd50a20
SC
71extern ld_config_type config;
72
bfbdc80f
SC
73struct obstack global_sym_obstack;
74#define obstack_chunk_alloc ldmalloc
75#define obstack_chunk_free free
76
2fa0b342
DHW
77/*
78 incremented for each symbol in the ldsym_type table
29f33467 79 no matter what flavour it is
2fa0b342
DHW
80*/
81unsigned int global_symbol_count;
82
83/* IMPORTS */
84
2fa0b342
DHW
85/* LOCALS */
86#define TABSIZE 1009
87static ldsym_type *global_symbol_hash_table[TABSIZE];
88
89/* Compute the hash code for symbol name KEY. */
29f33467 90static
1af27af8 91#ifdef __GNUC__
29f33467 92 __inline
1af27af8 93#endif
b773e0e5 94
2fa0b342 95int
8ddef552
DM
96hash_string (key)
97 CONST char *key;
2fa0b342 98{
1af27af8 99 register CONST char *cp;
2fa0b342 100 register int k;
0b5995da 101 register int l = 0;
2fa0b342
DHW
102 cp = key;
103 k = 0;
29f33467
SC
104 while (*cp && l < symbol_truncate)
105 {
106 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
107 l++;
108 }
2fa0b342
DHW
109 return k;
110}
111
1af27af8
SC
112static
113#ifdef __GNUC__
29f33467 114 __inline
04dd7520 115#endif
29f33467 116 ldsym_type *
8ddef552
DM
117search (key, hashval)
118 CONST char *key;
119 int hashval;
1af27af8 120{
29f33467 121 ldsym_type *bp;
1af27af8 122 for (bp = global_symbol_hash_table[hashval]; bp; bp = bp->link)
29f33467
SC
123 if (!strncmp (key, bp->name, symbol_truncate))
124 {
125 if (bp->flags & SYM_INDIRECT)
126 {
127 /* Use the symbol we're aliased to instead */
128 return (ldsym_type *) (bp->sdefs_chain);
129 }
130 return bp;
1af27af8 131 }
1af27af8
SC
132 return 0;
133}
134
135
2fa0b342
DHW
136/* Get the symbol table entry for the global symbol named KEY.
137 Create one if there is none. */
138ldsym_type *
8ddef552
DM
139ldsym_get (key)
140 CONST char *key;
2fa0b342
DHW
141{
142 register int hashval;
143 register ldsym_type *bp;
144
145 /* Determine the proper bucket. */
146
147 hashval = hash_string (key) % TABSIZE;
148
149 /* Search the bucket. */
29f33467
SC
150 bp = search (key, hashval);
151 if (bp)
152 {
153 return bp;
154 }
2fa0b342
DHW
155
156 /* Nothing was found; create a new symbol table entry. */
157
29f33467
SC
158 bp = (ldsym_type *) obstack_alloc (&global_sym_obstack, (bfd_size_type) (sizeof (ldsym_type)));
159 bp->srefs_chain = (asymbol **) NULL;
160 bp->sdefs_chain = (asymbol **) NULL;
161 bp->scoms_chain = (asymbol **) NULL;
162 bp->name = obstack_copy (&global_sym_obstack, key, strlen (key) + 1);
81016051 163 bp->flags = 0;
2fa0b342
DHW
164 /* Add the entry to the bucket. */
165
166 bp->link = global_symbol_hash_table[hashval];
167 global_symbol_hash_table[hashval] = bp;
168
169 /* Keep the chronological list up to date too */
170 *symbol_tail_ptr = bp;
171 symbol_tail_ptr = &bp->next;
172 bp->next = 0;
173 global_symbol_count++;
174
175 return bp;
176}
177
178/* Like `ldsym_get' but return 0 if the symbol is not already known. */
179
180ldsym_type *
8ddef552
DM
181ldsym_get_soft (key)
182 CONST char *key;
2fa0b342
DHW
183{
184 register int hashval;
2fa0b342
DHW
185 /* Determine which bucket. */
186
187 hashval = hash_string (key) % TABSIZE;
188
189 /* Search the bucket. */
29f33467 190 return search (key, hashval);
2fa0b342
DHW
191}
192
f3b36ecb
KR
193static asymbol **
194process_keepsyms (table, size)
29f33467 195 asymbol **table;
f3b36ecb
KR
196 int size;
197{
198 struct obstack obstack;
199 char *start_of_obstack;
200 FILE *ks_file = 0;
201 asymbol **out = table;
202 asymbol **end = table + size;
203 asymbol **sym;
204
205 if (!keepsyms_file || size == 0)
206 return end;
207 obstack_init (&obstack);
208 obstack_alloc (&obstack, 1);
209 obstack_finish (&obstack);
210 start_of_obstack = obstack_alloc (&obstack, 1);
211 ks_file = fopen (keepsyms_file, "r");
212 if (!ks_file)
213 {
214 info ("%X%P: can't open keep-symbols file `%s'\n", keepsyms_file);
215 goto egress;
216 }
217 errno = 0;
2fa0b342 218
f3b36ecb 219#define KEEP(S) \
26483cc6 220 do { asymbol **p = (S), *tmp = *out; *out = *p; *p = tmp; out++; } while (0)
2fa0b342 221
f3b36ecb
KR
222 while (!feof (ks_file) && !ferror (ks_file))
223 {
224 int c;
225 char *ptr;
226 int found = 0;
2fa0b342 227
f3b36ecb
KR
228 obstack_free (&obstack, start_of_obstack);
229 do
230 {
231 c = getc (ks_file);
232 if (c == '\n')
233 c = 0;
234 obstack_1grow (&obstack, c);
235 }
236 while (c > 0);
237 if (c == EOF)
238 {
239 if (!feof (ks_file))
240 /* error occurred */
241 {
242 info ("%X%P: error reading keep-symbols file `%s': %E\n",
243 keepsyms_file);
244 out = end;
245 goto egress;
246 }
247 if (obstack_next_free (&obstack) != obstack_base (&obstack) + 1)
248 /* eof in middle of symbol */
249 {
250 info ("%X%P: eof reached mid-line while reading keep-symbols file `%s'\n",
251 keepsyms_file);
252 out = end;
253 goto egress;
254 }
255 /* All okay -- no incomplete lines, EOF reached. */
256 break;
257 }
258 ptr = obstack_next_free (&obstack) - 2;
259 /* discard trailing trash */
260 while (*ptr == ' '
261 || *ptr == '\t')
262 *ptr-- = 0;
263 ptr = obstack_base (&obstack);
264 for (sym = out; sym < end; sym++)
0b5995da 265 if (!strncmp ((*sym)->name, ptr, symbol_truncate))
f3b36ecb
KR
266 {
267 KEEP (sym);
268 found = 1;
269 }
270 if (!found)
271 info ("%P: symbol `%s' (requested to be kept) not found\n", ptr);
272 }
273 /* It'd be slightly faster to move this pass above the previous one,
274 but that'd mean any symbols preserved in this pass would generate
275 warnings if they were also listed in the keepsyms file. */
276 for (sym = out; sym < end; sym++)
277 {
278 asymbol *s = *sym;
279 if (s->section == &bfd_und_section
8a045e50 280 || bfd_is_com_section (s->section)
f3b36ecb
KR
281 || s->flags & BSF_KEEP_G)
282 KEEP (sym);
283 }
29f33467 284egress:
f3b36ecb
KR
285 obstack_free (&obstack, start_of_obstack);
286 if (ks_file)
287 fclose (ks_file);
288 return out;
289}
2fa0b342
DHW
290
291static void
292list_file_locals (entry)
29f33467 293 lang_input_statement_type *entry;
2fa0b342
DHW
294{
295 asymbol **q;
6fd50a20 296 fprintf (config.map_file, "\nLocal symbols of ");
29f33467 297 minfo ("%I", entry);
6fd50a20 298 fprintf (config.map_file, ":\n\n");
29f33467
SC
299 if (entry->asymbols)
300 {
301 for (q = entry->asymbols; *q; q++)
302 {
303 asymbol *p = *q;
304 /* If this is a definition,
2fa0b342 305 update it if necessary by this file's start address. */
29f33467
SC
306 if (p->flags & BSF_LOCAL)
307 info (" %V %s\n", p->value, p->name);
308 }
309 }
2fa0b342
DHW
310}
311
312
313static void
8ddef552
DM
314print_file_stuff (f)
315 lang_input_statement_type * f;
2fa0b342 316{
29f33467
SC
317 fprintf (config.map_file, " %s\n", f->filename);
318 if (f->just_syms_flag)
319 {
320 fprintf (config.map_file, " symbols only\n");
321 }
322 else
323 {
324 asection *s;
bbd2521f 325 if (true)
bfbdc80f 326 {
29f33467
SC
327 for (s = f->the_bfd->sections;
328 s != (asection *) NULL;
329 s = s->next)
330 {
331 print_address (s->output_offset);
332 if (s->reloc_done)
333 {
334 fprintf (config.map_file, " %08x 2**%2ud %s\n",
335 (unsigned) bfd_get_section_size_after_reloc (s),
336 s->alignment_power, s->name);
337 }
338
339 else
340 {
341 fprintf (config.map_file, " %08x 2**%2ud %s\n",
342 (unsigned) bfd_get_section_size_before_reloc (s),
343 s->alignment_power, s->name);
344 }
345 }
bfbdc80f 346 }
29f33467 347 else
bfbdc80f 348 {
29f33467
SC
349 for (s = f->the_bfd->sections;
350 s != (asection *) NULL;
351 s = s->next)
352 {
353 fprintf (config.map_file, "%s ", s->name);
354 print_address (s->output_offset);
355 fprintf (config.map_file, "(%x)", (unsigned) bfd_get_section_size_after_reloc (s));
356 }
357 fprintf (config.map_file, "hex \n");
bfbdc80f 358 }
bfbdc80f 359 }
6fd50a20 360 fprintf (config.map_file, "\n");
2fa0b342
DHW
361}
362
363void
364ldsym_print_symbol_table ()
365{
6fd50a20 366 fprintf (config.map_file, "**FILES**\n\n");
2fa0b342 367
29f33467 368 lang_for_each_file (print_file_stuff);
2fa0b342 369
29f33467
SC
370 fprintf (config.map_file, "**GLOBAL SYMBOLS**\n\n");
371 fprintf (config.map_file, "offset section offset symbol\n");
2fa0b342
DHW
372 {
373 register ldsym_type *sp;
374
375 for (sp = symbol_head; sp; sp = sp->next)
376 {
29f33467
SC
377 if (sp->flags & SYM_INDIRECT)
378 {
379 fprintf (config.map_file, "indirect %s to %s\n",
380 sp->name, (((ldsym_type *) (sp->sdefs_chain))->name));
d9c53949 381 }
29f33467
SC
382 else
383 {
384 if (sp->sdefs_chain)
385 {
386 asymbol *defsym = *(sp->sdefs_chain);
387 asection *defsec = bfd_get_section (defsym);
388 print_address (defsym->value);
389 if (defsec)
390 {
391 fprintf (config.map_file, " %-10s",
392 bfd_section_name (output_bfd,
393 defsec));
394 print_space ();
395 print_address (defsym->value + defsec->vma);
396
397 }
398 else
399 {
400 fprintf (config.map_file, " .......");
401 }
402
403 }
404
405
406 if (sp->scoms_chain)
407 {
408 fprintf (config.map_file, "common ");
409 print_address ((*(sp->scoms_chain))->value);
410 fprintf (config.map_file, " %s ", sp->name);
411 }
412 else if (sp->sdefs_chain)
413 {
414 fprintf (config.map_file, " %s ", sp->name);
415 }
416 else
417 {
418 fprintf (config.map_file, "undefined ");
419 fprintf (config.map_file, "%s ", sp->name);
420
421 }
d9c53949 422 }
29f33467 423 print_nl ();
2fa0b342
DHW
424
425 }
426 }
2fa0b342
DHW
427}
428
429extern lang_output_section_statement_type *create_object_symbols;
430extern char lprefix;
431static asymbol **
29f33467
SC
432write_file_locals (output_buffer)
433 asymbol **output_buffer;
2fa0b342 434{
29f33467 435 LANG_FOR_EACH_INPUT_STATEMENT (entry)
bfbdc80f
SC
436 {
437 /* Run trough the symbols and work out what to do with them */
438 unsigned int i;
439
440 /* Add one for the filename symbol if needed */
29f33467
SC
441 if (create_object_symbols
442 != (lang_output_section_statement_type *) NULL)
443 {
444 asection *s;
445 for (s = entry->the_bfd->sections;
446 s != (asection *) NULL;
447 s = s->next)
448 {
449 if (s->output_section == create_object_symbols->bfd_section)
450 {
451 /* Add symbol to this section */
452 asymbol *newsym =
453 (asymbol *) bfd_make_empty_symbol (entry->the_bfd);
454 newsym->name = entry->local_sym_name;
455 /* The symbol belongs to the output file's text section */
456
457 /* The value is the start of this section in the output file*/
458 newsym->value = 0;
459 /* FIXME: Usurping BSF_KEEP_G flag, since it's defined as
f3b36ecb
KR
460 "used by the linker" and I can't find any other code that
461 uses it. Should be a cleaner way of doing this (like an
462 "application flags" field in the symbol structure?). */
29f33467
SC
463 newsym->flags = BSF_LOCAL | BSF_KEEP_G;
464 newsym->section = s;
465 *output_buffer++ = newsym;
466 break;
467 }
468 }
bfbdc80f 469 }
29f33467 470 for (i = 0; i < entry->symbol_count; i++)
bfbdc80f 471 {
29f33467
SC
472 asymbol *p = entry->asymbols[i];
473 /* FIXME, temporary hack, since not all of ld knows about the new abs section convention */
474
475 if (p->section == 0)
476 p->section = &bfd_abs_section;
477 if (flag_is_global (p->flags))
478 {
479 /* We are only interested in outputting
bfbdc80f 480 globals at this stage in special circumstances */
29f33467
SC
481 if (bfd_asymbol_bfd (p) == entry->the_bfd
482 && flag_is_not_at_end (p->flags))
483 {
484 /* And this is one of them */
485 *(output_buffer++) = p;
486 p->flags |= BSF_KEEP;
487 }
571c4c26 488 }
bfbdc80f 489 else
29f33467
SC
490 {
491 if (p->section == &bfd_ind_section)
492 {
493 /* Dont think about indirect symbols */
494 }
495 else if (flag_is_debugger (p->flags))
496 {
497 /* Only keep the debugger symbols if no stripping required */
498 if (strip_symbols == STRIP_NONE)
499 {
500 *output_buffer++ = p;
501 }
502 }
503 else if (p->section == &bfd_und_section
504 || bfd_is_com_section (p->section))
505 {
506 /* These must be global. */
507 }
508 else if (flag_is_ordinary_local (p->flags))
509 {
510 if (discard_locals == DISCARD_ALL)
511 {
512 }
513 else if (discard_locals == DISCARD_L &&
514 (p->name[0] == lprefix))
515 {
516 }
517 else if (p->flags == BSF_WARNING)
518 {
519 }
520 else
521 {
522 *output_buffer++ = p;
523 }
524 }
1cf91c69 525 else if (p->flags & BSF_CONSTRUCTOR)
29f33467 526 {
1cf91c69
PB
527 if (strip_symbols != STRIP_ALL)
528 {
529 *output_buffer++ = p;
530 }
29f33467
SC
531 }
532 else
533 {
534 FAIL ();
535 }
536 }
bfbdc80f 537 }
2fa0b342
DHW
538
539
bfbdc80f 540 }
2fa0b342
DHW
541 return output_buffer;
542}
543
544
545static asymbol **
29f33467
SC
546write_file_globals (symbol_table)
547 asymbol **symbol_table;
2fa0b342 548{
29f33467
SC
549 FOR_EACH_LDSYM (sp)
550 {
551 if (sp->flags & SYM_INDIRECT)
552 {
553 asymbol *bufp = (*(sp->srefs_chain));
554 ldsym_type *aliased_to = (ldsym_type *) (sp->sdefs_chain);
555 if (aliased_to->sdefs_chain)
556 {
557 asymbol *p = aliased_to->sdefs_chain[0];
558 bufp->value = p->value;
559 bufp->section = p->section;
560 bufp->flags = p->flags;
561 }
562 else
563 {
564 bufp->value = 0;
565 bufp->flags = 0;
566 bufp->section = &bfd_und_section;
567 }
568 *symbol_table++ = bufp;
569 }
570 else if ((sp->flags & SYM_INDIRECT) == 0 && sp->sdefs_chain != (asymbol **) NULL)
571 {
2fa0b342
DHW
572 asymbol *bufp = (*(sp->sdefs_chain));
573
29f33467
SC
574 if ((bufp->flags & BSF_KEEP) == 0)
575 {
576 ASSERT (bufp != (asymbol *) NULL);
2fa0b342 577
29f33467 578 bufp->name = sp->name;
2fa0b342 579
29f33467 580 if (sp->scoms_chain != (asymbol **) NULL)
2fa0b342 581
29f33467
SC
582 {
583 /*
2fa0b342
DHW
584 defined as common but not allocated, this happens
585 only with -r and not -d, write out a common
586 definition
587 */
29f33467
SC
588 bufp = *(sp->scoms_chain);
589 }
590 *symbol_table++ = bufp;
591 }
2fa0b342 592 }
29f33467
SC
593 else if (sp->scoms_chain != (asymbol **) NULL)
594 {
2fa0b342
DHW
595 /* This symbol is a common - just output */
596 asymbol *bufp = (*(sp->scoms_chain));
597 *symbol_table++ = bufp;
598 }
29f33467
SC
599 else if (sp->srefs_chain != (asymbol **) NULL)
600 {
2fa0b342
DHW
601 /* This symbol is undefined but has a reference */
602 asymbol *bufp = (*(sp->srefs_chain));
603 *symbol_table++ = bufp;
604 }
29f33467
SC
605 else
606 {
2fa0b342
DHW
607 /*
608 This symbol has neither defs nor refs, it must have come
609 from the command line, since noone has used it it has no
29f33467 610 data attatched, so we'll ignore it
2fa0b342
DHW
611 */
612 }
29f33467 613 }
2fa0b342
DHW
614 return symbol_table;
615}
616
2fa0b342 617void
29f33467 618ldsym_write ()
2fa0b342 619{
f3b36ecb
KR
620 if (keepsyms_file != 0
621 && strip_symbols != STRIP_SOME)
622 {
623 info ("%P `-retain-symbols-file' overrides `-s' and `-S'\n");
624 strip_symbols = STRIP_SOME;
625 }
29f33467
SC
626 if (strip_symbols != STRIP_ALL)
627 {
628 /* We know the maximum size of the symbol table -
2fa0b342
DHW
629 it's the size of all the global symbols ever seen +
630 the size of all the symbols from all the files +
631 the number of files (for the per file symbols)
632 +1 (for the null at the end)
633 */
29f33467
SC
634 extern unsigned int total_files_seen;
635 extern unsigned int total_symbols_seen;
2fa0b342 636
29f33467
SC
637 asymbol **symbol_table = (asymbol **)
638 ldmalloc ((bfd_size_type) (global_symbol_count +
639 total_files_seen +
640 total_symbols_seen + 1) * sizeof (asymbol *));
641 asymbol **tablep = write_file_locals (symbol_table);
2fa0b342 642
29f33467
SC
643 tablep = write_file_globals (tablep);
644 tablep = process_keepsyms (symbol_table, tablep - symbol_table);
2fa0b342 645
29f33467
SC
646 *tablep = (asymbol *) NULL;
647 bfd_set_symtab (output_bfd, symbol_table, (unsigned) (tablep - symbol_table));
648 }
2fa0b342 649}
c660714f
SC
650
651/*
29f33467 652return true if the supplied symbol name is not in the
c660714f
SC
653linker symbol table
654*/
29f33467 655boolean
8ddef552
DM
656ldsym_undefined (sym)
657 CONST char *sym;
c660714f 658{
29f33467
SC
659 ldsym_type *from_table = ldsym_get_soft (sym);
660 if (from_table != (ldsym_type *) NULL)
661 {
662 if (from_table->sdefs_chain != (asymbol **) NULL)
663 return false;
664 }
c660714f
SC
665 return true;
666}
bfbdc80f
SC
667
668void
8ddef552 669ldsym_init ()
bfbdc80f 670{
29f33467 671 obstack_begin (&global_sym_obstack, 20000);
bfbdc80f 672}
This page took 0.108601 seconds and 4 git commands to generate.