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. */
26 Written by Steve Chamberlain steve@cygnus.com
28 All symbol handling for the linker
33 We keep a hash table of global symbols. Each entry in a hash table
34 is called an ldsym_type. Each has three chains; a pointer to a
35 chain of definitions for the symbol (hopefully one long), a pointer
36 to a chain of references to the symbol, and a pointer to a chain of
37 common symbols. Each pointer points into the canonical symbol table
38 provided by bfd, each one of which points to an asymbol. Duringing
39 linkage, the linker uses the udata field to point to the next entry
40 in a canonical table....
45 +----------+ +----------+
46 | defs | a canonical symbol table
47 +----------+ +----------+
48 | refs | -----> | one entry| -----> asymbol
49 +----------+ +----------+ | |
50 | coms | | | +---------+
51 +----------+ +----------+ | udata |-----> another canonical symbol
56 It is very simple to make all the symbol pointers point to the same
57 definition - just run down the chain and make the asymbols pointers
58 within the canonical table point to the asymbol attacthed to the
59 definition of the symbol.
72 extern bfd
*output_bfd
;
73 extern strip_symbols_type strip_symbols
;
74 extern discard_locals_type discard_locals
;
75 /* Head and tail of global symbol table chronological list */
77 ldsym_type
*symbol_head
= (ldsym_type
*)NULL
;
78 ldsym_type
**symbol_tail_ptr
= &symbol_head
;
81 incremented for each symbol in the ldsym_type table
82 no matter what flavour it is
84 unsigned int global_symbol_count
;
88 extern boolean option_longmap
;
92 static ldsym_type
*global_symbol_hash_table
[TABSIZE
];
94 /* Compute the hash code for symbol name KEY. */
100 DEFUN(hash_string
,(key
),
103 register CONST
char *cp
;
109 k
= (((k
<< 1) + (k
>> 14)) ^ (*cp
++)) & 0x3fff;
117 #endif ldsym_type *bp;
119 DEFUN(search
,(key
,hashval
) ,
124 for (bp
= global_symbol_hash_table
[hashval
]; bp
; bp
= bp
->link
)
125 if (! strcmp (key
, bp
->name
)) {
126 if (bp
->flags
& SYM_INDIRECT
) {
127 /* Use the symbol we're aliased to instead */
128 return (ldsym_type
*)(bp
->sdefs_chain
);
136 /* Get the symbol table entry for the global symbol named KEY.
137 Create one if there is none. */
139 DEFUN(ldsym_get
,(key
),
142 register int hashval
;
143 register ldsym_type
*bp
;
145 /* Determine the proper bucket. */
147 hashval
= hash_string (key
) % TABSIZE
;
149 /* Search the bucket. */
150 bp
= search(key
, hashval
);
155 /* Nothing was found; create a new symbol table entry. */
157 bp
= (ldsym_type
*) ldmalloc ((bfd_size_type
)(sizeof (ldsym_type
)));
158 bp
->srefs_chain
= (asymbol
**)NULL
;
159 bp
->sdefs_chain
= (asymbol
**)NULL
;
160 bp
->scoms_chain
= (asymbol
**)NULL
;
161 bp
->name
= buystring(key
);
163 /* Add the entry to the bucket. */
165 bp
->link
= global_symbol_hash_table
[hashval
];
166 global_symbol_hash_table
[hashval
] = bp
;
168 /* Keep the chronological list up to date too */
169 *symbol_tail_ptr
= bp
;
170 symbol_tail_ptr
= &bp
->next
;
172 global_symbol_count
++;
177 /* Like `ldsym_get' but return 0 if the symbol is not already known. */
180 DEFUN(ldsym_get_soft
,(key
),
183 register int hashval
;
184 /* Determine which bucket. */
186 hashval
= hash_string (key
) % TABSIZE
;
188 /* Search the bucket. */
189 return search(key
, hashval
);
197 list_file_locals (entry
)
198 lang_input_statement_type
*entry
;
201 printf ( "\nLocal symbols of ");
204 if (entry
->asymbols
) {
205 for (q
= entry
->asymbols
; *q
; q
++)
208 /* If this is a definition,
209 update it if necessary by this file's start address. */
210 if (p
->flags
& BSF_LOCAL
)
211 info(" %V %s\n",p
->value
, p
->name
);
219 lang_input_statement_type
*f
;
221 fprintf (stdout
, " %s\n", f
->filename
);
222 if (f
->just_syms_flag
)
224 fprintf (stdout
, " symbols only\n");
229 if (true || option_longmap
) {
230 for (s
= f
->the_bfd
->sections
;
231 s
!= (asection
*)NULL
;
233 print_address(s
->output_offset
);
234 printf (" %08x 2**%2ud %s\n",
235 (unsigned)s
->size
, s
->alignment_power
, s
->name
);
239 for (s
= f
->the_bfd
->sections
;
240 s
!= (asection
*)NULL
;
242 printf("%s ", s
->name
);
243 print_address(s
->output_offset
);
244 printf("(%x)", (unsigned)s
->size
);
249 fprintf (stdout
, "\n");
253 ldsym_print_symbol_table ()
255 fprintf (stdout
, "**FILES**\n\n");
257 lang_for_each_file(print_file_stuff
);
259 fprintf(stdout
, "**GLOBAL SYMBOLS**\n\n");
260 fprintf(stdout
, "offset section offset symbol\n");
262 register ldsym_type
*sp
;
264 for (sp
= symbol_head
; sp
; sp
= sp
->next
)
266 if (sp
->flags
& SYM_INDIRECT
) {
267 fprintf(stdout
,"indirect %s to %s\n",
268 sp
->name
, (((ldsym_type
*)(sp
->sdefs_chain
))->name
));
273 asymbol
*defsym
= *(sp
->sdefs_chain
);
274 asection
*defsec
= bfd_get_section(defsym
);
275 print_address(defsym
->value
);
279 bfd_section_name(output_bfd
,
282 print_address(defsym
->value
+defsec
->vma
);
293 if (sp
->scoms_chain
) {
295 print_address((*(sp
->scoms_chain
))->value
);
296 printf(" %s ",sp
->name
);
298 else if (sp
->sdefs_chain
) {
299 printf(" %s ",sp
->name
);
302 printf("undefined ");
303 printf("%s ",sp
->name
);
311 lang_for_each_file(list_file_locals
);
314 extern lang_output_section_statement_type
*create_object_symbols
;
317 write_file_locals(output_buffer
)
318 asymbol
**output_buffer
;
320 LANG_FOR_EACH_INPUT_STATEMENT(entry
)
322 /* Run trough the symbols and work out what to do with them */
325 /* Add one for the filename symbol if needed */
326 if (create_object_symbols
327 != (lang_output_section_statement_type
*)NULL
) {
329 for (s
= entry
->the_bfd
->sections
;
330 s
!= (asection
*)NULL
;
332 if (s
->output_section
== create_object_symbols
->bfd_section
) {
333 /* Add symbol to this section */
335 (asymbol
*)bfd_make_empty_symbol(entry
->the_bfd
);
336 newsym
->name
= entry
->local_sym_name
;
337 /* The symbol belongs to the output file's text section */
339 /* The value is the start of this section in the output file*/
341 newsym
->flags
= BSF_LOCAL
;
343 *output_buffer
++ = newsym
;
348 for (i
= 0; i
< entry
->symbol_count
; i
++)
350 asymbol
*p
= entry
->asymbols
[i
];
352 if (flag_is_global(p
->flags
) || flag_is_absolute(p
->flags
))
354 /* We are only interested in outputting
355 globals at this stage in special circumstances */
356 if (p
->the_bfd
== entry
->the_bfd
357 && flag_is_not_at_end(p
->flags
)) {
358 /* And this is one of them */
359 *(output_buffer
++) = p
;
360 p
->flags
|= BSF_KEEP
;
364 if (flag_is_ordinary_local(p
->flags
))
366 if (discard_locals
== DISCARD_ALL
)
368 else if (discard_locals
== DISCARD_L
&&
369 (p
->name
[0] == lprefix
))
371 else if (p
->flags
== BSF_WARNING
)
374 { *output_buffer
++ = p
; }
376 else if (flag_is_debugger(p
->flags
))
378 /* Only keep the debugger symbols if no stripping required */
379 if (strip_symbols
== STRIP_NONE
) {
380 *output_buffer
++ = p
;
383 else if (flag_is_undefined(p
->flags
))
384 { /* This must be global */
386 else if (flag_is_common(p
->flags
)) {
387 /* And so must this */
389 else if (p
->flags
& BSF_CTOR
) {
401 return output_buffer
;
406 write_file_globals(symbol_table
)
407 asymbol
**symbol_table
;
411 if ((sp
->flags
& SYM_INDIRECT
) == 0 && sp
->sdefs_chain
!= (asymbol
**)NULL
) {
412 asymbol
*bufp
= (*(sp
->sdefs_chain
));
414 if ((bufp
->flags
& BSF_KEEP
) ==0) {
415 ASSERT(bufp
!= (asymbol
*)NULL
);
417 bufp
->name
= sp
->name
;
419 if (sp
->scoms_chain
!= (asymbol
**)NULL
)
423 defined as common but not allocated, this happens
424 only with -r and not -d, write out a common
427 bufp
= *(sp
->scoms_chain
);
429 *symbol_table
++ = bufp
;
432 else if (sp
->scoms_chain
!= (asymbol
**)NULL
) {
433 /* This symbol is a common - just output */
434 asymbol
*bufp
= (*(sp
->scoms_chain
));
435 *symbol_table
++ = bufp
;
437 else if (sp
->srefs_chain
!= (asymbol
**)NULL
) {
438 /* This symbol is undefined but has a reference */
439 asymbol
*bufp
= (*(sp
->srefs_chain
));
440 *symbol_table
++ = bufp
;
444 This symbol has neither defs nor refs, it must have come
445 from the command line, since noone has used it it has no
446 data attatched, so we'll ignore it
458 if (strip_symbols
!= STRIP_ALL
) {
459 /* We know the maximum size of the symbol table -
460 it's the size of all the global symbols ever seen +
461 the size of all the symbols from all the files +
462 the number of files (for the per file symbols)
463 +1 (for the null at the end)
465 extern unsigned int total_files_seen
;
466 extern unsigned int total_symbols_seen
;
468 asymbol
** symbol_table
= (asymbol
**)
469 ldmalloc ((bfd_size_type
)(global_symbol_count
+
471 total_symbols_seen
+ 1) * sizeof (asymbol
*));
472 asymbol
** tablep
= write_file_locals(symbol_table
);
474 tablep
= write_file_globals(tablep
);
476 *tablep
= (asymbol
*)NULL
;
477 bfd_set_symtab(output_bfd
, symbol_table
, (unsigned)( tablep
- symbol_table
));
482 return true if the supplied symbol name is not in the
486 DEFUN(ldsym_undefined
,(sym
),
489 ldsym_type
*from_table
= ldsym_get_soft(sym
);
490 if (from_table
!= (ldsym_type
*)NULL
) {
491 if (from_table
->sdefs_chain
!= (asymbol
**)NULL
) return false;