1 /* Copyright (C) 1991 Free Software Foundation, Inc.
3 This file is part of GLD, the Gnu Linker.
5 GLD is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 1, or (at your option)
10 GLD is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GLD; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 * Revision 1.3 1991/03/27 02:29:21 steve
24 * *** empty log message ***
26 * Revision 1.2 1991/03/22 23:02:38 steve
27 * Brought up to sync with Intel again.
29 * Revision 1.1 1991/03/13 00:48:32 chrisb
32 * Revision 1.4 1991/03/10 09:31:36 rich
34 * Makefile config.h ld-emul.c ld-emul.h ld-gld.c ld-gld960.c
35 * ld-lnk960.c ld.h lddigest.c ldexp.c ldexp.h ldfile.c ldfile.h
36 * ldgram.y ldinfo.h ldlang.c ldlang.h ldlex.h ldlex.l ldmain.c
37 * ldmain.h ldmisc.c ldmisc.h ldsym.c ldsym.h ldversion.c
38 * ldversion.h ldwarn.h ldwrite.c ldwrite.h y.tab.h
40 * As of this round of changes, ld now builds on all hosts of (Intel960)
41 * interest and copy passes my copy test on big endian hosts again.
43 * Revision 1.3 1991/03/06 02:28:56 sac
46 * Revision 1.2 1991/02/22 17:15:06 sac
47 * Added RCS keywords and copyrights
54 All symbol handling for the linker
67 extern bfd *output_bfd;
68 /* Head and tail of global symbol table chronological list */
70 ldsym_type *symbol_head = (ldsym_type *)NULL;
71 ldsym_type **symbol_tail_ptr = &symbol_head;
74 incremented for each symbol in the ldsym_type table
75 no matter what flavour it is
77 unsigned int global_symbol_count;
81 extern boolean option_longmap ;
85 static ldsym_type *global_symbol_hash_table[TABSIZE];
87 /* Compute the hash code for symbol name KEY. */
99 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
104 /* Get the symbol table entry for the global symbol named KEY.
105 Create one if there is none. */
110 register int hashval;
111 register ldsym_type *bp;
113 /* Determine the proper bucket. */
115 hashval = hash_string (key) % TABSIZE;
117 /* Search the bucket. */
119 for (bp = global_symbol_hash_table[hashval]; bp; bp = bp->link)
120 if (! strcmp (key, bp->name))
123 /* Nothing was found; create a new symbol table entry. */
125 bp = (ldsym_type *) ldmalloc (sizeof (ldsym_type));
126 bp->srefs_chain = (asymbol **)NULL;
127 bp->sdefs_chain = (asymbol **)NULL;
128 bp->scoms_chain = (asymbol **)NULL;
129 bp->name = (char *) ldmalloc (strlen (key) + 1);
130 strcpy (bp->name, key);
135 /* Add the entry to the bucket. */
137 bp->link = global_symbol_hash_table[hashval];
138 global_symbol_hash_table[hashval] = bp;
140 /* Keep the chronological list up to date too */
141 *symbol_tail_ptr = bp;
142 symbol_tail_ptr = &bp->next;
144 global_symbol_count++;
149 /* Like `ldsym_get' but return 0 if the symbol is not already known. */
155 register int hashval;
156 register ldsym_type *bp;
158 /* Determine which bucket. */
160 hashval = hash_string (key) % TABSIZE;
162 /* Search the bucket. */
164 for (bp = global_symbol_hash_table[hashval]; bp; bp = bp->link)
165 if (! strcmp (key, bp->name))
176 list_file_locals (entry)
177 lang_input_statement_type *entry;
180 fprintf (stderr, "\nLocal symbols of ");
182 fprintf (stderr, ":\n\n");
183 if (entry->asymbols) {
184 for (q = entry->asymbols; *q; q++)
187 /* If this is a definition,
188 update it if necessary by this file's start address. */
189 if (p->flags & BSF_LOCAL)
190 info(" %V %s\n",p->value, p->name);
198 lang_input_statement_type *f;
200 fprintf (stderr, " %s", f->filename);
201 fprintf (stderr, " ");
202 if (f->just_syms_flag)
204 fprintf (stderr, " symbols only\n");
209 if (option_longmap) {
210 for (s = f->the_bfd->sections;
211 s != (asection *)NULL;
213 fprintf (stderr, "%08lx %08x 2**%2ud %s\n",
215 (unsigned)s->size, s->alignment_power, s->name);
219 for (s = f->the_bfd->sections;
220 s != (asection *)NULL;
222 fprintf (stderr, "%s %lx(%x) ",
227 fprintf (stderr, "hex \n");
233 ldsym_print_symbol_table ()
235 fprintf (stderr, "\nFiles:\n\n");
237 lang_for_each_file(print_file_stuff);
239 fprintf (stderr, "\nGlobal symbols:\n\n");
241 register ldsym_type *sp;
243 for (sp = symbol_head; sp; sp = sp->next)
247 asymbol *defsym = *(sp->sdefs_chain);
248 asection *defsec = bfd_get_section(defsym);
249 fprintf(stderr,"%08lx ",defsym->value);
252 fprintf(stderr,"%08lx ",defsym->value+defsec->vma);
255 bfd_section_name(output_bfd,
261 fprintf(stderr," .......");
266 fprintf(stderr,"undefined");
270 if (sp->scoms_chain) {
271 fprintf(stderr, " common size %5lu %s",
272 (*(sp->scoms_chain))->value, sp->name);
274 if (sp->sdefs_chain) {
275 fprintf(stderr, " symbol def %08lx %s",
276 (*(sp->sdefs_chain))->value,
280 fprintf(stderr, " undefined %s",
283 fprintf(stderr, "\n");
287 lang_for_each_file(list_file_locals);
290 extern lang_output_section_statement_type *create_object_symbols;
293 write_file_locals(output_buffer)
294 asymbol **output_buffer;
296 LANG_FOR_EACH_INPUT_STATEMENT(entry)
298 /* Run trough the symbols and work out what to do with them */
301 /* Add one for the filename symbol if needed */
302 if (create_object_symbols
303 != (lang_output_section_statement_type *)NULL) {
305 for (s = entry->the_bfd->sections;
306 s != (asection *)NULL;
308 if (s->output_section == create_object_symbols->bfd_section) {
309 /* Add symbol to this section */
311 (asymbol *)bfd_make_empty_symbol(entry->the_bfd);
312 newsym->name = entry->local_sym_name;
313 /* The symbol belongs to the output file's text section */
315 /* The value is the start of this section in the output file*/
317 newsym->flags = BSF_LOCAL;
319 *output_buffer++ = newsym;
324 for (i = 0; i < entry->symbol_count; i++)
326 asymbol *p = entry->asymbols[i];
328 if (flag_is_global(p->flags) || flag_is_absolute(p->flags))
330 /* We are only interested in outputting
331 globals at this stage in special circumstances */
332 if (p->the_bfd == entry->the_bfd
333 && flag_is_not_at_end(p->flags)) {
334 /* And this is one of them */
335 *(output_buffer++) = p;
336 p->flags |= BSF_KEEP;
340 if (flag_is_ordinary_local(p->flags))
342 if (discard_locals == DISCARD_ALL)
344 else if (discard_locals == DISCARD_L &&
345 (p->name[0] == lprefix))
347 else if (p->flags == BSF_WARNING)
350 { *output_buffer++ = p; }
352 else if (flag_is_debugger(p->flags))
354 /* Only keep the debugger symbols if no stripping required */
355 if (strip_symbols == STRIP_NONE) {
356 *output_buffer++ = p;
359 else if (flag_is_undefined(p->flags))
360 { /* This must be global */
362 else if (flag_is_common(p->flags)) {
363 /* And so must this */
365 else if (p->flags & BSF_CTOR) {
377 return output_buffer;
382 write_file_globals(symbol_table)
383 asymbol **symbol_table;
387 if (sp->sdefs_chain != (asymbol **)NULL) {
388 asymbol *bufp = (*(sp->sdefs_chain));
390 if ((bufp->flags & BSF_KEEP) ==0) {
391 ASSERT(bufp != (asymbol *)NULL);
393 bufp->name = sp->name;
395 if (sp->scoms_chain != (asymbol **)NULL)
399 defined as common but not allocated, this happens
400 only with -r and not -d, write out a common
403 bufp = *(sp->scoms_chain);
405 *symbol_table++ = bufp;
408 else if (sp->scoms_chain != (asymbol **)NULL) {
409 /* This symbol is a common - just output */
410 asymbol *bufp = (*(sp->scoms_chain));
411 *symbol_table++ = bufp;
413 else if (sp->srefs_chain != (asymbol **)NULL) {
414 /* This symbol is undefined but has a reference */
415 asymbol *bufp = (*(sp->srefs_chain));
416 *symbol_table++ = bufp;
420 This symbol has neither defs nor refs, it must have come
421 from the command line, since noone has used it it has no
422 data attatched, so we'll ignore it
434 if (strip_symbols != STRIP_ALL) {
435 /* We know the maximum size of the symbol table -
436 it's the size of all the global symbols ever seen +
437 the size of all the symbols from all the files +
438 the number of files (for the per file symbols)
439 +1 (for the null at the end)
441 extern unsigned int total_files_seen;
442 extern unsigned int total_symbols_seen;
444 asymbol ** symbol_table = (asymbol **)
445 ldmalloc ((size_t)(global_symbol_count +
447 total_symbols_seen + 1) * sizeof (asymbol *));
448 asymbol ** tablep = write_file_locals(symbol_table);
450 tablep = write_file_globals(tablep);
452 *tablep = (asymbol *)NULL;
453 bfd_set_symtab(output_bfd, symbol_table, (unsigned)( tablep - symbol_table));
458 return true if the supplied symbol name is not in the
465 ldsym_type *from_table = ldsym_get_soft(sym);
466 if (from_table != (ldsym_type *)NULL) {
467 if (from_table->sdefs_chain != (asymbol **)NULL) return false;