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. */
28 All symbol handling for the linker
41 extern bfd *output_bfd;
42 /* Head and tail of global symbol table chronological list */
44 ldsym_type *symbol_head = (ldsym_type *)NULL;
45 ldsym_type **symbol_tail_ptr = &symbol_head;
48 incremented for each symbol in the ldsym_type table
49 no matter what flavour it is
51 unsigned int global_symbol_count;
55 extern boolean option_longmap ;
59 static ldsym_type *global_symbol_hash_table[TABSIZE];
61 /* Compute the hash code for symbol name KEY. */
73 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
78 /* Get the symbol table entry for the global symbol named KEY.
79 Create one if there is none. */
81 DEFUN(ldsym_get,(key),
85 register ldsym_type *bp;
87 /* Determine the proper bucket. */
89 hashval = hash_string (key) % TABSIZE;
91 /* Search the bucket. */
93 for (bp = global_symbol_hash_table[hashval]; bp; bp = bp->link)
94 if (! strcmp (key, bp->name))
97 /* Nothing was found; create a new symbol table entry. */
99 bp = (ldsym_type *) ldmalloc (sizeof (ldsym_type));
100 bp->srefs_chain = (asymbol **)NULL;
101 bp->sdefs_chain = (asymbol **)NULL;
102 bp->scoms_chain = (asymbol **)NULL;
103 bp->name = buystring(key);
105 /* Add the entry to the bucket. */
107 bp->link = global_symbol_hash_table[hashval];
108 global_symbol_hash_table[hashval] = bp;
110 /* Keep the chronological list up to date too */
111 *symbol_tail_ptr = bp;
112 symbol_tail_ptr = &bp->next;
114 global_symbol_count++;
119 /* Like `ldsym_get' but return 0 if the symbol is not already known. */
122 DEFUN(ldsym_get_soft,(key),
125 register int hashval;
126 register ldsym_type *bp;
128 /* Determine which bucket. */
130 hashval = hash_string (key) % TABSIZE;
132 /* Search the bucket. */
134 for (bp = global_symbol_hash_table[hashval]; bp; bp = bp->link)
135 if (! strcmp (key, bp->name))
146 list_file_locals (entry)
147 lang_input_statement_type *entry;
150 fprintf (stderr, "\nLocal symbols of ");
152 fprintf (stderr, ":\n\n");
153 if (entry->asymbols) {
154 for (q = entry->asymbols; *q; q++)
157 /* If this is a definition,
158 update it if necessary by this file's start address. */
159 if (p->flags & BSF_LOCAL)
160 info(" %V %s\n",p->value, p->name);
168 lang_input_statement_type *f;
170 fprintf (stderr, " %s", f->filename);
171 fprintf (stderr, " ");
172 if (f->just_syms_flag)
174 fprintf (stderr, " symbols only\n");
179 if (option_longmap) {
180 for (s = f->the_bfd->sections;
181 s != (asection *)NULL;
183 fprintf (stderr, "%08lx %08x 2**%2ud %s\n",
185 (unsigned)s->size, s->alignment_power, s->name);
189 for (s = f->the_bfd->sections;
190 s != (asection *)NULL;
192 fprintf (stderr, "%s %lx(%x) ",
197 fprintf (stderr, "hex \n");
203 ldsym_print_symbol_table ()
205 fprintf (stderr, "\nFiles:\n\n");
207 lang_for_each_file(print_file_stuff);
209 fprintf (stderr, "\nGlobal symbols:\n\n");
211 register ldsym_type *sp;
213 for (sp = symbol_head; sp; sp = sp->next)
217 asymbol *defsym = *(sp->sdefs_chain);
218 asection *defsec = bfd_get_section(defsym);
219 fprintf(stderr,"%08lx ",defsym->value);
222 fprintf(stderr,"%08lx ",defsym->value+defsec->vma);
225 bfd_section_name(output_bfd,
231 fprintf(stderr," .......");
236 fprintf(stderr,"undefined");
240 if (sp->scoms_chain) {
241 fprintf(stderr, " common size %5lu %s",
242 (*(sp->scoms_chain))->value, sp->name);
244 if (sp->sdefs_chain) {
245 fprintf(stderr, " symbol def %08lx %s",
246 (*(sp->sdefs_chain))->value,
250 fprintf(stderr, " undefined %s",
253 fprintf(stderr, "\n");
257 lang_for_each_file(list_file_locals);
260 extern lang_output_section_statement_type *create_object_symbols;
263 write_file_locals(output_buffer)
264 asymbol **output_buffer;
266 LANG_FOR_EACH_INPUT_STATEMENT(entry)
268 /* Run trough the symbols and work out what to do with them */
271 /* Add one for the filename symbol if needed */
272 if (create_object_symbols
273 != (lang_output_section_statement_type *)NULL) {
275 for (s = entry->the_bfd->sections;
276 s != (asection *)NULL;
278 if (s->output_section == create_object_symbols->bfd_section) {
279 /* Add symbol to this section */
281 (asymbol *)bfd_make_empty_symbol(entry->the_bfd);
282 newsym->name = entry->local_sym_name;
283 /* The symbol belongs to the output file's text section */
285 /* The value is the start of this section in the output file*/
287 newsym->flags = BSF_LOCAL;
289 *output_buffer++ = newsym;
294 for (i = 0; i < entry->symbol_count; i++)
296 asymbol *p = entry->asymbols[i];
298 if (flag_is_global(p->flags) || flag_is_absolute(p->flags))
300 /* We are only interested in outputting
301 globals at this stage in special circumstances */
302 if (p->the_bfd == entry->the_bfd
303 && flag_is_not_at_end(p->flags)) {
304 /* And this is one of them */
305 *(output_buffer++) = p;
306 p->flags |= BSF_KEEP;
310 if (flag_is_ordinary_local(p->flags))
312 if (discard_locals == DISCARD_ALL)
314 else if (discard_locals == DISCARD_L &&
315 (p->name[0] == lprefix))
317 else if (p->flags == BSF_WARNING)
320 { *output_buffer++ = p; }
322 else if (flag_is_debugger(p->flags))
324 /* Only keep the debugger symbols if no stripping required */
325 if (strip_symbols == STRIP_NONE) {
326 *output_buffer++ = p;
329 else if (flag_is_undefined(p->flags))
330 { /* This must be global */
332 else if (flag_is_common(p->flags)) {
333 /* And so must this */
335 else if (p->flags & BSF_CTOR) {
347 return output_buffer;
352 write_file_globals(symbol_table)
353 asymbol **symbol_table;
357 if (sp->sdefs_chain != (asymbol **)NULL) {
358 asymbol *bufp = (*(sp->sdefs_chain));
360 if ((bufp->flags & BSF_KEEP) ==0) {
361 ASSERT(bufp != (asymbol *)NULL);
363 bufp->name = sp->name;
365 if (sp->scoms_chain != (asymbol **)NULL)
369 defined as common but not allocated, this happens
370 only with -r and not -d, write out a common
373 bufp = *(sp->scoms_chain);
375 *symbol_table++ = bufp;
378 else if (sp->scoms_chain != (asymbol **)NULL) {
379 /* This symbol is a common - just output */
380 asymbol *bufp = (*(sp->scoms_chain));
381 *symbol_table++ = bufp;
383 else if (sp->srefs_chain != (asymbol **)NULL) {
384 /* This symbol is undefined but has a reference */
385 asymbol *bufp = (*(sp->srefs_chain));
386 *symbol_table++ = bufp;
390 This symbol has neither defs nor refs, it must have come
391 from the command line, since noone has used it it has no
392 data attatched, so we'll ignore it
404 if (strip_symbols != STRIP_ALL) {
405 /* We know the maximum size of the symbol table -
406 it's the size of all the global symbols ever seen +
407 the size of all the symbols from all the files +
408 the number of files (for the per file symbols)
409 +1 (for the null at the end)
411 extern unsigned int total_files_seen;
412 extern unsigned int total_symbols_seen;
414 asymbol ** symbol_table = (asymbol **)
415 ldmalloc ((size_t)(global_symbol_count +
417 total_symbols_seen + 1) * sizeof (asymbol *));
418 asymbol ** tablep = write_file_locals(symbol_table);
420 tablep = write_file_globals(tablep);
422 *tablep = (asymbol *)NULL;
423 bfd_set_symtab(output_bfd, symbol_table, (unsigned)( tablep - symbol_table));
428 return true if the supplied symbol name is not in the
432 DEFUN(ldsym_undefined,(sym),
435 ldsym_type *from_table = ldsym_get_soft(sym);
436 if (from_table != (ldsym_type *)NULL) {
437 if (from_table->sdefs_chain != (asymbol **)NULL) return false;