This is a test to see if the file is still locked.
[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
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
2fa0b342 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 | | | +---------+
40 +----------+ +----------+ | udata |-----> another canonical symbol
41 +---------+
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 */
60
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
66ldsym_type *symbol_head = (ldsym_type *)NULL;
67ldsym_type **symbol_tail_ptr = &symbol_head;
68
6fd50a20
SC
69extern ld_config_type config;
70
bfbdc80f
SC
71struct obstack global_sym_obstack;
72#define obstack_chunk_alloc ldmalloc
73#define obstack_chunk_free free
74
2fa0b342
DHW
75/*
76 incremented for each symbol in the ldsym_type table
77 no matter what flavour it is
78*/
79unsigned int global_symbol_count;
80
81/* IMPORTS */
82
83extern boolean option_longmap ;
84
85/* LOCALS */
86#define TABSIZE 1009
87static ldsym_type *global_symbol_hash_table[TABSIZE];
88
89/* Compute the hash code for symbol name KEY. */
1af27af8
SC
90static
91#ifdef __GNUC__
b773e0e5 92__inline
1af27af8 93#endif
b773e0e5 94
2fa0b342 95int
1af27af8
SC
96DEFUN(hash_string,(key),
97 CONST char *key)
2fa0b342 98{
1af27af8 99 register CONST char *cp;
2fa0b342
DHW
100 register int k;
101
102 cp = key;
103 k = 0;
104 while (*cp)
105 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
106
107 return k;
108}
109
1af27af8
SC
110static
111#ifdef __GNUC__
b773e0e5 112__inline
1af27af8
SC
113#endif ldsym_type *bp;
114ldsym_type *
115DEFUN(search,(key,hashval) ,
116 CONST char *key AND
117 int hashval)
118{
119 ldsym_type *bp;
120 for (bp = global_symbol_hash_table[hashval]; bp; bp = bp->link)
121 if (! strcmp (key, bp->name)) {
122 if (bp->flags & SYM_INDIRECT) {
123 /* Use the symbol we're aliased to instead */
124 return (ldsym_type *)(bp->sdefs_chain);
125 }
126 return bp;
127 }
128 return 0;
129}
130
131
2fa0b342
DHW
132/* Get the symbol table entry for the global symbol named KEY.
133 Create one if there is none. */
134ldsym_type *
99fe4553
SC
135DEFUN(ldsym_get,(key),
136 CONST char *key)
2fa0b342
DHW
137{
138 register int hashval;
139 register ldsym_type *bp;
140
141 /* Determine the proper bucket. */
142
143 hashval = hash_string (key) % TABSIZE;
144
145 /* Search the bucket. */
1af27af8
SC
146 bp = search(key, hashval);
147 if(bp) {
148 return bp;
149 }
2fa0b342
DHW
150
151 /* Nothing was found; create a new symbol table entry. */
152
bfbdc80f 153 bp = (ldsym_type *) obstack_alloc (&global_sym_obstack, (bfd_size_type)(sizeof (ldsym_type)));
2fa0b342
DHW
154 bp->srefs_chain = (asymbol **)NULL;
155 bp->sdefs_chain = (asymbol **)NULL;
156 bp->scoms_chain = (asymbol **)NULL;
bfbdc80f 157 bp->name = obstack_copy(&global_sym_obstack, key, strlen(key)+1);
81016051 158 bp->flags = 0;
2fa0b342
DHW
159 /* Add the entry to the bucket. */
160
161 bp->link = global_symbol_hash_table[hashval];
162 global_symbol_hash_table[hashval] = bp;
163
164 /* Keep the chronological list up to date too */
165 *symbol_tail_ptr = bp;
166 symbol_tail_ptr = &bp->next;
167 bp->next = 0;
168 global_symbol_count++;
169
170 return bp;
171}
172
173/* Like `ldsym_get' but return 0 if the symbol is not already known. */
174
175ldsym_type *
99fe4553
SC
176DEFUN(ldsym_get_soft,(key),
177 CONST char *key)
2fa0b342
DHW
178{
179 register int hashval;
2fa0b342
DHW
180 /* Determine which bucket. */
181
182 hashval = hash_string (key) % TABSIZE;
183
184 /* Search the bucket. */
7fe11a82 185 return search(key, hashval);
2fa0b342
DHW
186}
187
188
189
190
191
192static void
193list_file_locals (entry)
194lang_input_statement_type *entry;
195{
196 asymbol **q;
6fd50a20
SC
197 fprintf (config.map_file, "\nLocal symbols of ");
198 minfo("%I", entry);
199 fprintf (config.map_file, ":\n\n");
2fa0b342
DHW
200 if (entry->asymbols) {
201 for (q = entry->asymbols; *q; q++)
202 {
203 asymbol *p = *q;
204 /* If this is a definition,
205 update it if necessary by this file's start address. */
206 if (p->flags & BSF_LOCAL)
207 info(" %V %s\n",p->value, p->name);
208 }
209 }
210}
211
212
213static void
6fd50a20
SC
214DEFUN(print_file_stuff,(f),
215 lang_input_statement_type *f)
2fa0b342 216{
6fd50a20 217 fprintf (config.map_file," %s\n", f->filename);
2fa0b342 218 if (f->just_syms_flag)
c611e285 219 {
6fd50a20 220 fprintf (config.map_file, " symbols only\n");
c611e285 221 }
2fa0b342 222 else
c611e285
SC
223 {
224 asection *s;
225 if (true || option_longmap) {
bfbdc80f
SC
226 for (s = f->the_bfd->sections;
227 s != (asection *)NULL;
228 s = s->next) {
229 print_address(s->output_offset);
230 if (s->reloc_done)
231 {
232 fprintf (config.map_file, " %08x 2**%2ud %s\n",
233 (unsigned)bfd_get_section_size_after_reloc(s),
234 s->alignment_power, s->name);
235 }
c611e285 236
bfbdc80f
SC
237 else
238 {
239 fprintf (config.map_file, " %08x 2**%2ud %s\n",
240 (unsigned)bfd_get_section_size_before_reloc(s),
241 s->alignment_power, s->name);
242 }
c611e285 243 }
bfbdc80f
SC
244 }
245 else
246 {
247 for (s = f->the_bfd->sections;
248 s != (asection *)NULL;
249 s = s->next) {
250 fprintf(config.map_file, "%s ", s->name);
251 print_address(s->output_offset);
252 fprintf(config.map_file, "(%x)", (unsigned)bfd_get_section_size_after_reloc(s));
2fa0b342 253 }
bfbdc80f
SC
254 fprintf(config.map_file, "hex \n");
255 }
c611e285 256 }
6fd50a20 257 fprintf (config.map_file, "\n");
2fa0b342
DHW
258}
259
260void
261ldsym_print_symbol_table ()
262{
6fd50a20 263 fprintf (config.map_file, "**FILES**\n\n");
2fa0b342
DHW
264
265 lang_for_each_file(print_file_stuff);
266
6fd50a20
SC
267 fprintf(config.map_file, "**GLOBAL SYMBOLS**\n\n");
268 fprintf(config.map_file, "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 274 if (sp->flags & SYM_INDIRECT) {
6fd50a20 275 fprintf(config.map_file,"indirect %s to %s\n",
1af27af8 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 {
6fd50a20 286 fprintf(config.map_file, " %-10s",
d9c53949
SC
287 bfd_section_name(output_bfd,
288 defsec));
289 print_space();
290 print_address(defsym->value+defsec->vma);
291
292 }
293 else
294 {
6fd50a20 295 fprintf(config.map_file, " .......");
d9c53949
SC
296 }
297
298 }
299
300
301 if (sp->scoms_chain) {
6fd50a20 302 fprintf(config.map_file, "common ");
d9c53949 303 print_address((*(sp->scoms_chain))->value);
6fd50a20 304 fprintf(config.map_file, " %s ",sp->name);
d9c53949
SC
305 }
306 else if (sp->sdefs_chain) {
6fd50a20 307 fprintf(config.map_file, " %s ",sp->name);
d9c53949
SC
308 }
309 else {
6fd50a20
SC
310 fprintf(config.map_file, "undefined ");
311 fprintf(config.map_file, "%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{
bfbdc80f
SC
330 LANG_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++)
2fa0b342 359 {
bfbdc80f
SC
360 asymbol *p = entry->asymbols[i];
361 /* FIXME, temporary hack, since not all of ld knows about the new abs section convention */
362
363 if (p->section == 0)
364 p->section = &bfd_abs_section;
365 if (flag_is_global(p->flags) )
366 {
367 /* We are only interested in outputting
368 globals at this stage in special circumstances */
369 if (p->the_bfd == entry->the_bfd
370 && flag_is_not_at_end(p->flags)) {
371 /* And this is one of them */
372 *(output_buffer++) = p;
373 p->flags |= BSF_KEEP;
2fa0b342
DHW
374 }
375 }
bfbdc80f
SC
376 else {
377 if (flag_is_ordinary_local(p->flags))
2fa0b342 378 {
bfbdc80f
SC
379 if (discard_locals == DISCARD_ALL)
380 { }
381 else if (discard_locals == DISCARD_L &&
382 (p->name[0] == lprefix))
383 { }
384 else if (p->flags == BSF_WARNING)
385 { }
386 else
387 { *output_buffer++ = p; }
388 }
389 else if (flag_is_debugger(p->flags))
390 {
391 /* Only keep the debugger symbols if no stripping required */
392 if (strip_symbols == STRIP_NONE) {
393 *output_buffer++ = p;
2fa0b342
DHW
394 }
395 }
bfbdc80f
SC
396 else if (p->section == &bfd_und_section)
397 { /* This must be global */
398 }
399 else if (p->section == &bfd_com_section) {
400 /* And so must this */
401 }
402 else if (p->flags & BSF_CTOR) {
403 /* Throw it away */
404 }
405 else
406 {
407 FAIL();
408 }
409 }
410 }
2fa0b342
DHW
411
412
bfbdc80f 413 }
2fa0b342
DHW
414 return output_buffer;
415}
416
417
418static asymbol **
419write_file_globals(symbol_table)
420asymbol **symbol_table;
421{
422 FOR_EACH_LDSYM(sp)
423 {
1af27af8 424 if ((sp->flags & SYM_INDIRECT) == 0 && sp->sdefs_chain != (asymbol **)NULL) {
2fa0b342
DHW
425 asymbol *bufp = (*(sp->sdefs_chain));
426
427 if ((bufp->flags & BSF_KEEP) ==0) {
428 ASSERT(bufp != (asymbol *)NULL);
429
430 bufp->name = sp->name;
431
432 if (sp->scoms_chain != (asymbol **)NULL)
433
434 {
435 /*
436 defined as common but not allocated, this happens
437 only with -r and not -d, write out a common
438 definition
439 */
440 bufp = *(sp->scoms_chain);
441 }
442 *symbol_table++ = bufp;
443 }
444 }
445 else if (sp->scoms_chain != (asymbol **)NULL) {
446 /* This symbol is a common - just output */
447 asymbol *bufp = (*(sp->scoms_chain));
448 *symbol_table++ = bufp;
449 }
450 else if (sp->srefs_chain != (asymbol **)NULL) {
451 /* This symbol is undefined but has a reference */
452 asymbol *bufp = (*(sp->srefs_chain));
453 *symbol_table++ = bufp;
454 }
455 else {
456 /*
457 This symbol has neither defs nor refs, it must have come
458 from the command line, since noone has used it it has no
459 data attatched, so we'll ignore it
460 */
461 }
462 }
463 return symbol_table;
464}
465
466
467
468void
469ldsym_write()
470{
471 if (strip_symbols != STRIP_ALL) {
472 /* We know the maximum size of the symbol table -
473 it's the size of all the global symbols ever seen +
474 the size of all the symbols from all the files +
475 the number of files (for the per file symbols)
476 +1 (for the null at the end)
477 */
478 extern unsigned int total_files_seen;
479 extern unsigned int total_symbols_seen;
480
481 asymbol ** symbol_table = (asymbol **)
19b03b7a 482 ldmalloc ((bfd_size_type)(global_symbol_count +
2fa0b342
DHW
483 total_files_seen +
484 total_symbols_seen + 1) * sizeof (asymbol *));
485 asymbol ** tablep = write_file_locals(symbol_table);
486
487 tablep = write_file_globals(tablep);
488
489 *tablep = (asymbol *)NULL;
490 bfd_set_symtab(output_bfd, symbol_table, (unsigned)( tablep - symbol_table));
491 }
492}
c660714f
SC
493
494/*
495return true if the supplied symbol name is not in the
496linker symbol table
497*/
498boolean
99fe4553
SC
499DEFUN(ldsym_undefined,(sym),
500 CONST char *sym)
c660714f
SC
501{
502 ldsym_type *from_table = ldsym_get_soft(sym);
bfbdc80f
SC
503 if (from_table != (ldsym_type *)NULL)
504 {
c660714f
SC
505 if (from_table->sdefs_chain != (asymbol **)NULL) return false;
506 }
507 return true;
508}
bfbdc80f
SC
509
510void
511DEFUN_VOID(ldsym_init)
512{
513 obstack_begin(&global_sym_obstack, 20000);
514}
This page took 0.09716 seconds and 4 git commands to generate.