Convert to C90 and a few tweaks.
[binutils.git] / bfd / linker.c
blobb7c557608d606b6aec79b5b47a20969d24a13b1d
1 /* linker.c -- BFD linker routines
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "bfdlink.h"
26 #include "genlink.h"
29 SECTION
30 Linker Functions
32 @cindex Linker
33 The linker uses three special entry points in the BFD target
34 vector. It is not necessary to write special routines for
35 these entry points when creating a new BFD back end, since
36 generic versions are provided. However, writing them can
37 speed up linking and make it use significantly less runtime
38 memory.
40 The first routine creates a hash table used by the other
41 routines. The second routine adds the symbols from an object
42 file to the hash table. The third routine takes all the
43 object files and links them together to create the output
44 file. These routines are designed so that the linker proper
45 does not need to know anything about the symbols in the object
46 files that it is linking. The linker merely arranges the
47 sections as directed by the linker script and lets BFD handle
48 the details of symbols and relocs.
50 The second routine and third routines are passed a pointer to
51 a <<struct bfd_link_info>> structure (defined in
52 <<bfdlink.h>>) which holds information relevant to the link,
53 including the linker hash table (which was created by the
54 first routine) and a set of callback functions to the linker
55 proper.
57 The generic linker routines are in <<linker.c>>, and use the
58 header file <<genlink.h>>. As of this writing, the only back
59 ends which have implemented versions of these routines are
60 a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>). The a.out
61 routines are used as examples throughout this section.
63 @menu
64 @* Creating a Linker Hash Table::
65 @* Adding Symbols to the Hash Table::
66 @* Performing the Final Link::
67 @end menu
69 INODE
70 Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
71 SUBSECTION
72 Creating a linker hash table
74 @cindex _bfd_link_hash_table_create in target vector
75 @cindex target vector (_bfd_link_hash_table_create)
76 The linker routines must create a hash table, which must be
77 derived from <<struct bfd_link_hash_table>> described in
78 <<bfdlink.c>>. @xref{Hash Tables}, for information on how to
79 create a derived hash table. This entry point is called using
80 the target vector of the linker output file.
82 The <<_bfd_link_hash_table_create>> entry point must allocate
83 and initialize an instance of the desired hash table. If the
84 back end does not require any additional information to be
85 stored with the entries in the hash table, the entry point may
86 simply create a <<struct bfd_link_hash_table>>. Most likely,
87 however, some additional information will be needed.
89 For example, with each entry in the hash table the a.out
90 linker keeps the index the symbol has in the final output file
91 (this index number is used so that when doing a relocatable
92 link the symbol index used in the output file can be quickly
93 filled in when copying over a reloc). The a.out linker code
94 defines the required structures and functions for a hash table
95 derived from <<struct bfd_link_hash_table>>. The a.out linker
96 hash table is created by the function
97 <<NAME(aout,link_hash_table_create)>>; it simply allocates
98 space for the hash table, initializes it, and returns a
99 pointer to it.
101 When writing the linker routines for a new back end, you will
102 generally not know exactly which fields will be required until
103 you have finished. You should simply create a new hash table
104 which defines no additional fields, and then simply add fields
105 as they become necessary.
107 INODE
108 Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
109 SUBSECTION
110 Adding symbols to the hash table
112 @cindex _bfd_link_add_symbols in target vector
113 @cindex target vector (_bfd_link_add_symbols)
114 The linker proper will call the <<_bfd_link_add_symbols>>
115 entry point for each object file or archive which is to be
116 linked (typically these are the files named on the command
117 line, but some may also come from the linker script). The
118 entry point is responsible for examining the file. For an
119 object file, BFD must add any relevant symbol information to
120 the hash table. For an archive, BFD must determine which
121 elements of the archive should be used and adding them to the
122 link.
124 The a.out version of this entry point is
125 <<NAME(aout,link_add_symbols)>>.
127 @menu
128 @* Differing file formats::
129 @* Adding symbols from an object file::
130 @* Adding symbols from an archive::
131 @end menu
133 INODE
134 Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
135 SUBSUBSECTION
136 Differing file formats
138 Normally all the files involved in a link will be of the same
139 format, but it is also possible to link together different
140 format object files, and the back end must support that. The
141 <<_bfd_link_add_symbols>> entry point is called via the target
142 vector of the file to be added. This has an important
143 consequence: the function may not assume that the hash table
144 is the type created by the corresponding
145 <<_bfd_link_hash_table_create>> vector. All the
146 <<_bfd_link_add_symbols>> function can assume about the hash
147 table is that it is derived from <<struct
148 bfd_link_hash_table>>.
150 Sometimes the <<_bfd_link_add_symbols>> function must store
151 some information in the hash table entry to be used by the
152 <<_bfd_final_link>> function. In such a case the <<creator>>
153 field of the hash table must be checked to make sure that the
154 hash table was created by an object file of the same format.
156 The <<_bfd_final_link>> routine must be prepared to handle a
157 hash entry without any extra information added by the
158 <<_bfd_link_add_symbols>> function. A hash entry without
159 extra information will also occur when the linker script
160 directs the linker to create a symbol. Note that, regardless
161 of how a hash table entry is added, all the fields will be
162 initialized to some sort of null value by the hash table entry
163 initialization function.
165 See <<ecoff_link_add_externals>> for an example of how to
166 check the <<creator>> field before saving information (in this
167 case, the ECOFF external symbol debugging information) in a
168 hash table entry.
170 INODE
171 Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
172 SUBSUBSECTION
173 Adding symbols from an object file
175 When the <<_bfd_link_add_symbols>> routine is passed an object
176 file, it must add all externally visible symbols in that
177 object file to the hash table. The actual work of adding the
178 symbol to the hash table is normally handled by the function
179 <<_bfd_generic_link_add_one_symbol>>. The
180 <<_bfd_link_add_symbols>> routine is responsible for reading
181 all the symbols from the object file and passing the correct
182 information to <<_bfd_generic_link_add_one_symbol>>.
184 The <<_bfd_link_add_symbols>> routine should not use
185 <<bfd_canonicalize_symtab>> to read the symbols. The point of
186 providing this routine is to avoid the overhead of converting
187 the symbols into generic <<asymbol>> structures.
189 @findex _bfd_generic_link_add_one_symbol
190 <<_bfd_generic_link_add_one_symbol>> handles the details of
191 combining common symbols, warning about multiple definitions,
192 and so forth. It takes arguments which describe the symbol to
193 add, notably symbol flags, a section, and an offset. The
194 symbol flags include such things as <<BSF_WEAK>> or
195 <<BSF_INDIRECT>>. The section is a section in the object
196 file, or something like <<bfd_und_section_ptr>> for an undefined
197 symbol or <<bfd_com_section_ptr>> for a common symbol.
199 If the <<_bfd_final_link>> routine is also going to need to
200 read the symbol information, the <<_bfd_link_add_symbols>>
201 routine should save it somewhere attached to the object file
202 BFD. However, the information should only be saved if the
203 <<keep_memory>> field of the <<info>> argument is TRUE, so
204 that the <<-no-keep-memory>> linker switch is effective.
206 The a.out function which adds symbols from an object file is
207 <<aout_link_add_object_symbols>>, and most of the interesting
208 work is in <<aout_link_add_symbols>>. The latter saves
209 pointers to the hash tables entries created by
210 <<_bfd_generic_link_add_one_symbol>> indexed by symbol number,
211 so that the <<_bfd_final_link>> routine does not have to call
212 the hash table lookup routine to locate the entry.
214 INODE
215 Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
216 SUBSUBSECTION
217 Adding symbols from an archive
219 When the <<_bfd_link_add_symbols>> routine is passed an
220 archive, it must look through the symbols defined by the
221 archive and decide which elements of the archive should be
222 included in the link. For each such element it must call the
223 <<add_archive_element>> linker callback, and it must add the
224 symbols from the object file to the linker hash table.
226 @findex _bfd_generic_link_add_archive_symbols
227 In most cases the work of looking through the symbols in the
228 archive should be done by the
229 <<_bfd_generic_link_add_archive_symbols>> function. This
230 function builds a hash table from the archive symbol table and
231 looks through the list of undefined symbols to see which
232 elements should be included.
233 <<_bfd_generic_link_add_archive_symbols>> is passed a function
234 to call to make the final decision about adding an archive
235 element to the link and to do the actual work of adding the
236 symbols to the linker hash table.
238 The function passed to
239 <<_bfd_generic_link_add_archive_symbols>> must read the
240 symbols of the archive element and decide whether the archive
241 element should be included in the link. If the element is to
242 be included, the <<add_archive_element>> linker callback
243 routine must be called with the element as an argument, and
244 the elements symbols must be added to the linker hash table
245 just as though the element had itself been passed to the
246 <<_bfd_link_add_symbols>> function.
248 When the a.out <<_bfd_link_add_symbols>> function receives an
249 archive, it calls <<_bfd_generic_link_add_archive_symbols>>
250 passing <<aout_link_check_archive_element>> as the function
251 argument. <<aout_link_check_archive_element>> calls
252 <<aout_link_check_ar_symbols>>. If the latter decides to add
253 the element (an element is only added if it provides a real,
254 non-common, definition for a previously undefined or common
255 symbol) it calls the <<add_archive_element>> callback and then
256 <<aout_link_check_archive_element>> calls
257 <<aout_link_add_symbols>> to actually add the symbols to the
258 linker hash table.
260 The ECOFF back end is unusual in that it does not normally
261 call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
262 archives already contain a hash table of symbols. The ECOFF
263 back end searches the archive itself to avoid the overhead of
264 creating a new hash table.
266 INODE
267 Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions
268 SUBSECTION
269 Performing the final link
271 @cindex _bfd_link_final_link in target vector
272 @cindex target vector (_bfd_final_link)
273 When all the input files have been processed, the linker calls
274 the <<_bfd_final_link>> entry point of the output BFD. This
275 routine is responsible for producing the final output file,
276 which has several aspects. It must relocate the contents of
277 the input sections and copy the data into the output sections.
278 It must build an output symbol table including any local
279 symbols from the input files and the global symbols from the
280 hash table. When producing relocatable output, it must
281 modify the input relocs and write them into the output file.
282 There may also be object format dependent work to be done.
284 The linker will also call the <<write_object_contents>> entry
285 point when the BFD is closed. The two entry points must work
286 together in order to produce the correct output file.
288 The details of how this works are inevitably dependent upon
289 the specific object file format. The a.out
290 <<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
292 @menu
293 @* Information provided by the linker::
294 @* Relocating the section contents::
295 @* Writing the symbol table::
296 @end menu
298 INODE
299 Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
300 SUBSUBSECTION
301 Information provided by the linker
303 Before the linker calls the <<_bfd_final_link>> entry point,
304 it sets up some data structures for the function to use.
306 The <<input_bfds>> field of the <<bfd_link_info>> structure
307 will point to a list of all the input files included in the
308 link. These files are linked through the <<link_next>> field
309 of the <<bfd>> structure.
311 Each section in the output file will have a list of
312 <<link_order>> structures attached to the <<link_order_head>>
313 field (the <<link_order>> structure is defined in
314 <<bfdlink.h>>). These structures describe how to create the
315 contents of the output section in terms of the contents of
316 various input sections, fill constants, and, eventually, other
317 types of information. They also describe relocs that must be
318 created by the BFD backend, but do not correspond to any input
319 file; this is used to support -Ur, which builds constructors
320 while generating a relocatable object file.
322 INODE
323 Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
324 SUBSUBSECTION
325 Relocating the section contents
327 The <<_bfd_final_link>> function should look through the
328 <<link_order>> structures attached to each section of the
329 output file. Each <<link_order>> structure should either be
330 handled specially, or it should be passed to the function
331 <<_bfd_default_link_order>> which will do the right thing
332 (<<_bfd_default_link_order>> is defined in <<linker.c>>).
334 For efficiency, a <<link_order>> of type
335 <<bfd_indirect_link_order>> whose associated section belongs
336 to a BFD of the same format as the output BFD must be handled
337 specially. This type of <<link_order>> describes part of an
338 output section in terms of a section belonging to one of the
339 input files. The <<_bfd_final_link>> function should read the
340 contents of the section and any associated relocs, apply the
341 relocs to the section contents, and write out the modified
342 section contents. If performing a relocatable link, the
343 relocs themselves must also be modified and written out.
345 @findex _bfd_relocate_contents
346 @findex _bfd_final_link_relocate
347 The functions <<_bfd_relocate_contents>> and
348 <<_bfd_final_link_relocate>> provide some general support for
349 performing the actual relocations, notably overflow checking.
350 Their arguments include information about the symbol the
351 relocation is against and a <<reloc_howto_type>> argument
352 which describes the relocation to perform. These functions
353 are defined in <<reloc.c>>.
355 The a.out function which handles reading, relocating, and
356 writing section contents is <<aout_link_input_section>>. The
357 actual relocation is done in <<aout_link_input_section_std>>
358 and <<aout_link_input_section_ext>>.
360 INODE
361 Writing the symbol table, , Relocating the section contents, Performing the Final Link
362 SUBSUBSECTION
363 Writing the symbol table
365 The <<_bfd_final_link>> function must gather all the symbols
366 in the input files and write them out. It must also write out
367 all the symbols in the global hash table. This must be
368 controlled by the <<strip>> and <<discard>> fields of the
369 <<bfd_link_info>> structure.
371 The local symbols of the input files will not have been
372 entered into the linker hash table. The <<_bfd_final_link>>
373 routine must consider each input file and include the symbols
374 in the output file. It may be convenient to do this when
375 looking through the <<link_order>> structures, or it may be
376 done by stepping through the <<input_bfds>> list.
378 The <<_bfd_final_link>> routine must also traverse the global
379 hash table to gather all the externally visible symbols. It
380 is possible that most of the externally visible symbols may be
381 written out when considering the symbols of each input file,
382 but it is still necessary to traverse the hash table since the
383 linker script may have defined some symbols that are not in
384 any of the input files.
386 The <<strip>> field of the <<bfd_link_info>> structure
387 controls which symbols are written out. The possible values
388 are listed in <<bfdlink.h>>. If the value is <<strip_some>>,
389 then the <<keep_hash>> field of the <<bfd_link_info>>
390 structure is a hash table of symbols to keep; each symbol
391 should be looked up in this hash table, and only symbols which
392 are present should be included in the output file.
394 If the <<strip>> field of the <<bfd_link_info>> structure
395 permits local symbols to be written out, the <<discard>> field
396 is used to further controls which local symbols are included
397 in the output file. If the value is <<discard_l>>, then all
398 local symbols which begin with a certain prefix are discarded;
399 this is controlled by the <<bfd_is_local_label_name>> entry point.
401 The a.out backend handles symbols by calling
402 <<aout_link_write_symbols>> on each input BFD and then
403 traversing the global hash table with the function
404 <<aout_link_write_other_symbol>>. It builds a string table
405 while writing out the symbols, which is written to the output
406 file at the end of <<NAME(aout,final_link)>>.
409 static bfd_boolean generic_link_add_object_symbols
410 (bfd *, struct bfd_link_info *, bfd_boolean collect);
411 static bfd_boolean generic_link_add_symbols
412 (bfd *, struct bfd_link_info *, bfd_boolean);
413 static bfd_boolean generic_link_check_archive_element_no_collect
414 (bfd *, struct bfd_link_info *, bfd_boolean *);
415 static bfd_boolean generic_link_check_archive_element_collect
416 (bfd *, struct bfd_link_info *, bfd_boolean *);
417 static bfd_boolean generic_link_check_archive_element
418 (bfd *, struct bfd_link_info *, bfd_boolean *, bfd_boolean);
419 static bfd_boolean generic_link_add_symbol_list
420 (bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **,
421 bfd_boolean);
422 static bfd_boolean generic_add_output_symbol
423 (bfd *, size_t *psymalloc, asymbol *);
424 static bfd_boolean default_data_link_order
425 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
426 static bfd_boolean default_indirect_link_order
427 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *,
428 bfd_boolean);
430 /* The link hash table structure is defined in bfdlink.h. It provides
431 a base hash table which the backend specific hash tables are built
432 upon. */
434 /* Routine to create an entry in the link hash table. */
436 struct bfd_hash_entry *
437 _bfd_link_hash_newfunc (struct bfd_hash_entry *entry,
438 struct bfd_hash_table *table,
439 const char *string)
441 /* Allocate the structure if it has not already been allocated by a
442 subclass. */
443 if (entry == NULL)
445 entry = bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry));
446 if (entry == NULL)
447 return entry;
450 /* Call the allocation method of the superclass. */
451 entry = bfd_hash_newfunc (entry, table, string);
452 if (entry)
454 struct bfd_link_hash_entry *h = (struct bfd_link_hash_entry *) entry;
456 /* Initialize the local fields. */
457 h->type = bfd_link_hash_new;
458 h->next = NULL;
461 return entry;
464 /* Initialize a link hash table. The BFD argument is the one
465 responsible for creating this table. */
467 bfd_boolean
468 _bfd_link_hash_table_init
469 (struct bfd_link_hash_table *table,
470 bfd *abfd,
471 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
472 struct bfd_hash_table *,
473 const char *))
475 table->creator = abfd->xvec;
476 table->undefs = NULL;
477 table->undefs_tail = NULL;
478 table->type = bfd_link_generic_hash_table;
480 return bfd_hash_table_init (&table->table, newfunc);
483 /* Look up a symbol in a link hash table. If follow is TRUE, we
484 follow bfd_link_hash_indirect and bfd_link_hash_warning links to
485 the real symbol. */
487 struct bfd_link_hash_entry *
488 bfd_link_hash_lookup (struct bfd_link_hash_table *table,
489 const char *string,
490 bfd_boolean create,
491 bfd_boolean copy,
492 bfd_boolean follow)
494 struct bfd_link_hash_entry *ret;
496 ret = ((struct bfd_link_hash_entry *)
497 bfd_hash_lookup (&table->table, string, create, copy));
499 if (follow && ret != NULL)
501 while (ret->type == bfd_link_hash_indirect
502 || ret->type == bfd_link_hash_warning)
503 ret = ret->u.i.link;
506 return ret;
509 /* Look up a symbol in the main linker hash table if the symbol might
510 be wrapped. This should only be used for references to an
511 undefined symbol, not for definitions of a symbol. */
513 struct bfd_link_hash_entry *
514 bfd_wrapped_link_hash_lookup (bfd *abfd,
515 struct bfd_link_info *info,
516 const char *string,
517 bfd_boolean create,
518 bfd_boolean copy,
519 bfd_boolean follow)
521 bfd_size_type amt;
523 if (info->wrap_hash != NULL)
525 const char *l;
527 l = string;
528 if (*l == bfd_get_symbol_leading_char (abfd))
529 ++l;
531 #undef WRAP
532 #define WRAP "__wrap_"
534 if (bfd_hash_lookup (info->wrap_hash, l, FALSE, FALSE) != NULL)
536 char *n;
537 struct bfd_link_hash_entry *h;
539 /* This symbol is being wrapped. We want to replace all
540 references to SYM with references to __wrap_SYM. */
542 amt = strlen (l) + sizeof WRAP + 1;
543 n = bfd_malloc (amt);
544 if (n == NULL)
545 return NULL;
547 /* Note that symbol_leading_char may be '\0'. */
548 n[0] = bfd_get_symbol_leading_char (abfd);
549 n[1] = '\0';
550 strcat (n, WRAP);
551 strcat (n, l);
552 h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
553 free (n);
554 return h;
557 #undef WRAP
559 #undef REAL
560 #define REAL "__real_"
562 if (*l == '_'
563 && strncmp (l, REAL, sizeof REAL - 1) == 0
564 && bfd_hash_lookup (info->wrap_hash, l + sizeof REAL - 1,
565 FALSE, FALSE) != NULL)
567 char *n;
568 struct bfd_link_hash_entry *h;
570 /* This is a reference to __real_SYM, where SYM is being
571 wrapped. We want to replace all references to __real_SYM
572 with references to SYM. */
574 amt = strlen (l + sizeof REAL - 1) + 2;
575 n = bfd_malloc (amt);
576 if (n == NULL)
577 return NULL;
579 /* Note that symbol_leading_char may be '\0'. */
580 n[0] = bfd_get_symbol_leading_char (abfd);
581 n[1] = '\0';
582 strcat (n, l + sizeof REAL - 1);
583 h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
584 free (n);
585 return h;
588 #undef REAL
591 return bfd_link_hash_lookup (info->hash, string, create, copy, follow);
594 /* Traverse a generic link hash table. The only reason this is not a
595 macro is to do better type checking. This code presumes that an
596 argument passed as a struct bfd_hash_entry * may be caught as a
597 struct bfd_link_hash_entry * with no explicit cast required on the
598 call. */
600 void
601 bfd_link_hash_traverse
602 (struct bfd_link_hash_table *table,
603 bfd_boolean (*func) (struct bfd_link_hash_entry *, void *),
604 void *info)
606 bfd_hash_traverse (&table->table,
607 (bfd_boolean (*) (struct bfd_hash_entry *, void *)) func,
608 info);
611 /* Add a symbol to the linker hash table undefs list. */
613 void
614 bfd_link_add_undef (struct bfd_link_hash_table *table,
615 struct bfd_link_hash_entry *h)
617 BFD_ASSERT (h->next == NULL);
618 if (table->undefs_tail != NULL)
619 table->undefs_tail->next = h;
620 if (table->undefs == NULL)
621 table->undefs = h;
622 table->undefs_tail = h;
625 /* Routine to create an entry in a generic link hash table. */
627 struct bfd_hash_entry *
628 _bfd_generic_link_hash_newfunc (struct bfd_hash_entry *entry,
629 struct bfd_hash_table *table,
630 const char *string)
632 /* Allocate the structure if it has not already been allocated by a
633 subclass. */
634 if (entry == NULL)
636 entry =
637 bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry));
638 if (entry == NULL)
639 return entry;
642 /* Call the allocation method of the superclass. */
643 entry = _bfd_link_hash_newfunc (entry, table, string);
644 if (entry)
646 struct generic_link_hash_entry *ret;
648 /* Set local fields. */
649 ret = (struct generic_link_hash_entry *) entry;
650 ret->written = FALSE;
651 ret->sym = NULL;
654 return entry;
657 /* Create a generic link hash table. */
659 struct bfd_link_hash_table *
660 _bfd_generic_link_hash_table_create (bfd *abfd)
662 struct generic_link_hash_table *ret;
663 bfd_size_type amt = sizeof (struct generic_link_hash_table);
665 ret = bfd_malloc (amt);
666 if (ret == NULL)
667 return NULL;
668 if (! _bfd_link_hash_table_init (&ret->root, abfd,
669 _bfd_generic_link_hash_newfunc))
671 free (ret);
672 return NULL;
674 return &ret->root;
677 void
678 _bfd_generic_link_hash_table_free (struct bfd_link_hash_table *hash)
680 struct generic_link_hash_table *ret
681 = (struct generic_link_hash_table *) hash;
683 bfd_hash_table_free (&ret->root.table);
684 free (ret);
687 /* Grab the symbols for an object file when doing a generic link. We
688 store the symbols in the outsymbols field. We need to keep them
689 around for the entire link to ensure that we only read them once.
690 If we read them multiple times, we might wind up with relocs and
691 the hash table pointing to different instances of the symbol
692 structure. */
694 static bfd_boolean
695 generic_link_read_symbols (bfd *abfd)
697 if (bfd_get_outsymbols (abfd) == NULL)
699 long symsize;
700 long symcount;
702 symsize = bfd_get_symtab_upper_bound (abfd);
703 if (symsize < 0)
704 return FALSE;
705 bfd_get_outsymbols (abfd) = bfd_alloc (abfd, symsize);
706 if (bfd_get_outsymbols (abfd) == NULL && symsize != 0)
707 return FALSE;
708 symcount = bfd_canonicalize_symtab (abfd, bfd_get_outsymbols (abfd));
709 if (symcount < 0)
710 return FALSE;
711 bfd_get_symcount (abfd) = symcount;
714 return TRUE;
717 /* Generic function to add symbols to from an object file to the
718 global hash table. This version does not automatically collect
719 constructors by name. */
721 bfd_boolean
722 _bfd_generic_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
724 return generic_link_add_symbols (abfd, info, FALSE);
727 /* Generic function to add symbols from an object file to the global
728 hash table. This version automatically collects constructors by
729 name, as the collect2 program does. It should be used for any
730 target which does not provide some other mechanism for setting up
731 constructors and destructors; these are approximately those targets
732 for which gcc uses collect2 and do not support stabs. */
734 bfd_boolean
735 _bfd_generic_link_add_symbols_collect (bfd *abfd, struct bfd_link_info *info)
737 return generic_link_add_symbols (abfd, info, TRUE);
740 /* Indicate that we are only retrieving symbol values from this
741 section. We want the symbols to act as though the values in the
742 file are absolute. */
744 void
745 _bfd_generic_link_just_syms (asection *sec,
746 struct bfd_link_info *info ATTRIBUTE_UNUSED)
748 sec->output_section = bfd_abs_section_ptr;
749 sec->output_offset = sec->vma;
752 /* Add symbols from an object file to the global hash table. */
754 static bfd_boolean
755 generic_link_add_symbols (bfd *abfd,
756 struct bfd_link_info *info,
757 bfd_boolean collect)
759 bfd_boolean ret;
761 switch (bfd_get_format (abfd))
763 case bfd_object:
764 ret = generic_link_add_object_symbols (abfd, info, collect);
765 break;
766 case bfd_archive:
767 ret = (_bfd_generic_link_add_archive_symbols
768 (abfd, info,
769 (collect
770 ? generic_link_check_archive_element_collect
771 : generic_link_check_archive_element_no_collect)));
772 break;
773 default:
774 bfd_set_error (bfd_error_wrong_format);
775 ret = FALSE;
778 return ret;
781 /* Add symbols from an object file to the global hash table. */
783 static bfd_boolean
784 generic_link_add_object_symbols (bfd *abfd,
785 struct bfd_link_info *info,
786 bfd_boolean collect)
788 bfd_size_type symcount;
789 struct symbol_cache_entry **outsyms;
791 if (! generic_link_read_symbols (abfd))
792 return FALSE;
793 symcount = _bfd_generic_link_get_symcount (abfd);
794 outsyms = _bfd_generic_link_get_symbols (abfd);
795 return generic_link_add_symbol_list (abfd, info, symcount, outsyms, collect);
798 /* We build a hash table of all symbols defined in an archive. */
800 /* An archive symbol may be defined by multiple archive elements.
801 This linked list is used to hold the elements. */
803 struct archive_list
805 struct archive_list *next;
806 unsigned int indx;
809 /* An entry in an archive hash table. */
811 struct archive_hash_entry
813 struct bfd_hash_entry root;
814 /* Where the symbol is defined. */
815 struct archive_list *defs;
818 /* An archive hash table itself. */
820 struct archive_hash_table
822 struct bfd_hash_table table;
825 /* Create a new entry for an archive hash table. */
827 static struct bfd_hash_entry *
828 archive_hash_newfunc (struct bfd_hash_entry *entry,
829 struct bfd_hash_table *table,
830 const char *string)
832 struct archive_hash_entry *ret = (struct archive_hash_entry *) entry;
834 /* Allocate the structure if it has not already been allocated by a
835 subclass. */
836 if (ret == NULL)
837 ret = bfd_hash_allocate (table, sizeof (struct archive_hash_entry));
838 if (ret == NULL)
839 return NULL;
841 /* Call the allocation method of the superclass. */
842 ret = ((struct archive_hash_entry *)
843 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
845 if (ret)
847 /* Initialize the local fields. */
848 ret->defs = NULL;
851 return &ret->root;
854 /* Initialize an archive hash table. */
856 static bfd_boolean
857 archive_hash_table_init
858 (struct archive_hash_table *table,
859 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
860 struct bfd_hash_table *,
861 const char *))
863 return bfd_hash_table_init (&table->table, newfunc);
866 /* Look up an entry in an archive hash table. */
868 #define archive_hash_lookup(t, string, create, copy) \
869 ((struct archive_hash_entry *) \
870 bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
872 /* Allocate space in an archive hash table. */
874 #define archive_hash_allocate(t, size) bfd_hash_allocate (&(t)->table, (size))
876 /* Free an archive hash table. */
878 #define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table)
880 /* Generic function to add symbols from an archive file to the global
881 hash file. This function presumes that the archive symbol table
882 has already been read in (this is normally done by the
883 bfd_check_format entry point). It looks through the undefined and
884 common symbols and searches the archive symbol table for them. If
885 it finds an entry, it includes the associated object file in the
886 link.
888 The old linker looked through the archive symbol table for
889 undefined symbols. We do it the other way around, looking through
890 undefined symbols for symbols defined in the archive. The
891 advantage of the newer scheme is that we only have to look through
892 the list of undefined symbols once, whereas the old method had to
893 re-search the symbol table each time a new object file was added.
895 The CHECKFN argument is used to see if an object file should be
896 included. CHECKFN should set *PNEEDED to TRUE if the object file
897 should be included, and must also call the bfd_link_info
898 add_archive_element callback function and handle adding the symbols
899 to the global hash table. CHECKFN should only return FALSE if some
900 sort of error occurs.
902 For some formats, such as a.out, it is possible to look through an
903 object file but not actually include it in the link. The
904 archive_pass field in a BFD is used to avoid checking the symbols
905 of an object files too many times. When an object is included in
906 the link, archive_pass is set to -1. If an object is scanned but
907 not included, archive_pass is set to the pass number. The pass
908 number is incremented each time a new object file is included. The
909 pass number is used because when a new object file is included it
910 may create new undefined symbols which cause a previously examined
911 object file to be included. */
913 bfd_boolean
914 _bfd_generic_link_add_archive_symbols
915 (bfd *abfd,
916 struct bfd_link_info *info,
917 bfd_boolean (*checkfn) (bfd *, struct bfd_link_info *, bfd_boolean *))
919 carsym *arsyms;
920 carsym *arsym_end;
921 register carsym *arsym;
922 int pass;
923 struct archive_hash_table arsym_hash;
924 unsigned int indx;
925 struct bfd_link_hash_entry **pundef;
927 if (! bfd_has_map (abfd))
929 /* An empty archive is a special case. */
930 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
931 return TRUE;
932 bfd_set_error (bfd_error_no_armap);
933 return FALSE;
936 arsyms = bfd_ardata (abfd)->symdefs;
937 arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
939 /* In order to quickly determine whether an symbol is defined in
940 this archive, we build a hash table of the symbols. */
941 if (! archive_hash_table_init (&arsym_hash, archive_hash_newfunc))
942 return FALSE;
943 for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
945 struct archive_hash_entry *arh;
946 struct archive_list *l, **pp;
948 arh = archive_hash_lookup (&arsym_hash, arsym->name, TRUE, FALSE);
949 if (arh == NULL)
950 goto error_return;
951 l = ((struct archive_list *)
952 archive_hash_allocate (&arsym_hash, sizeof (struct archive_list)));
953 if (l == NULL)
954 goto error_return;
955 l->indx = indx;
956 for (pp = &arh->defs; *pp != NULL; pp = &(*pp)->next)
958 *pp = l;
959 l->next = NULL;
962 /* The archive_pass field in the archive itself is used to
963 initialize PASS, sine we may search the same archive multiple
964 times. */
965 pass = abfd->archive_pass + 1;
967 /* New undefined symbols are added to the end of the list, so we
968 only need to look through it once. */
969 pundef = &info->hash->undefs;
970 while (*pundef != NULL)
972 struct bfd_link_hash_entry *h;
973 struct archive_hash_entry *arh;
974 struct archive_list *l;
976 h = *pundef;
978 /* When a symbol is defined, it is not necessarily removed from
979 the list. */
980 if (h->type != bfd_link_hash_undefined
981 && h->type != bfd_link_hash_common)
983 /* Remove this entry from the list, for general cleanliness
984 and because we are going to look through the list again
985 if we search any more libraries. We can't remove the
986 entry if it is the tail, because that would lose any
987 entries we add to the list later on (it would also cause
988 us to lose track of whether the symbol has been
989 referenced). */
990 if (*pundef != info->hash->undefs_tail)
991 *pundef = (*pundef)->next;
992 else
993 pundef = &(*pundef)->next;
994 continue;
997 /* Look for this symbol in the archive symbol map. */
998 arh = archive_hash_lookup (&arsym_hash, h->root.string, FALSE, FALSE);
999 if (arh == NULL)
1001 /* If we haven't found the exact symbol we're looking for,
1002 let's look for its import thunk */
1003 if (info->pei386_auto_import)
1005 bfd_size_type amt = strlen (h->root.string) + 10;
1006 char *buf = bfd_malloc (amt);
1007 if (buf == NULL)
1008 return FALSE;
1010 sprintf (buf, "__imp_%s", h->root.string);
1011 arh = archive_hash_lookup (&arsym_hash, buf, FALSE, FALSE);
1012 free(buf);
1014 if (arh == NULL)
1016 pundef = &(*pundef)->next;
1017 continue;
1020 /* Look at all the objects which define this symbol. */
1021 for (l = arh->defs; l != NULL; l = l->next)
1023 bfd *element;
1024 bfd_boolean needed;
1026 /* If the symbol has gotten defined along the way, quit. */
1027 if (h->type != bfd_link_hash_undefined
1028 && h->type != bfd_link_hash_common)
1029 break;
1031 element = bfd_get_elt_at_index (abfd, l->indx);
1032 if (element == NULL)
1033 goto error_return;
1035 /* If we've already included this element, or if we've
1036 already checked it on this pass, continue. */
1037 if (element->archive_pass == -1
1038 || element->archive_pass == pass)
1039 continue;
1041 /* If we can't figure this element out, just ignore it. */
1042 if (! bfd_check_format (element, bfd_object))
1044 element->archive_pass = -1;
1045 continue;
1048 /* CHECKFN will see if this element should be included, and
1049 go ahead and include it if appropriate. */
1050 if (! (*checkfn) (element, info, &needed))
1051 goto error_return;
1053 if (! needed)
1054 element->archive_pass = pass;
1055 else
1057 element->archive_pass = -1;
1059 /* Increment the pass count to show that we may need to
1060 recheck object files which were already checked. */
1061 ++pass;
1065 pundef = &(*pundef)->next;
1068 archive_hash_table_free (&arsym_hash);
1070 /* Save PASS in case we are called again. */
1071 abfd->archive_pass = pass;
1073 return TRUE;
1075 error_return:
1076 archive_hash_table_free (&arsym_hash);
1077 return FALSE;
1080 /* See if we should include an archive element. This version is used
1081 when we do not want to automatically collect constructors based on
1082 the symbol name, presumably because we have some other mechanism
1083 for finding them. */
1085 static bfd_boolean
1086 generic_link_check_archive_element_no_collect (
1087 bfd *abfd,
1088 struct bfd_link_info *info,
1089 bfd_boolean *pneeded)
1091 return generic_link_check_archive_element (abfd, info, pneeded, FALSE);
1094 /* See if we should include an archive element. This version is used
1095 when we want to automatically collect constructors based on the
1096 symbol name, as collect2 does. */
1098 static bfd_boolean
1099 generic_link_check_archive_element_collect (bfd *abfd,
1100 struct bfd_link_info *info,
1101 bfd_boolean *pneeded)
1103 return generic_link_check_archive_element (abfd, info, pneeded, TRUE);
1106 /* See if we should include an archive element. Optionally collect
1107 constructors. */
1109 static bfd_boolean
1110 generic_link_check_archive_element (bfd *abfd,
1111 struct bfd_link_info *info,
1112 bfd_boolean *pneeded,
1113 bfd_boolean collect)
1115 asymbol **pp, **ppend;
1117 *pneeded = FALSE;
1119 if (! generic_link_read_symbols (abfd))
1120 return FALSE;
1122 pp = _bfd_generic_link_get_symbols (abfd);
1123 ppend = pp + _bfd_generic_link_get_symcount (abfd);
1124 for (; pp < ppend; pp++)
1126 asymbol *p;
1127 struct bfd_link_hash_entry *h;
1129 p = *pp;
1131 /* We are only interested in globally visible symbols. */
1132 if (! bfd_is_com_section (p->section)
1133 && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0)
1134 continue;
1136 /* We are only interested if we know something about this
1137 symbol, and it is undefined or common. An undefined weak
1138 symbol (type bfd_link_hash_undefweak) is not considered to be
1139 a reference when pulling files out of an archive. See the
1140 SVR4 ABI, p. 4-27. */
1141 h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), FALSE,
1142 FALSE, TRUE);
1143 if (h == NULL
1144 || (h->type != bfd_link_hash_undefined
1145 && h->type != bfd_link_hash_common))
1146 continue;
1148 /* P is a symbol we are looking for. */
1150 if (! bfd_is_com_section (p->section))
1152 bfd_size_type symcount;
1153 asymbol **symbols;
1155 /* This object file defines this symbol, so pull it in. */
1156 if (! (*info->callbacks->add_archive_element) (info, abfd,
1157 bfd_asymbol_name (p)))
1158 return FALSE;
1159 symcount = _bfd_generic_link_get_symcount (abfd);
1160 symbols = _bfd_generic_link_get_symbols (abfd);
1161 if (! generic_link_add_symbol_list (abfd, info, symcount,
1162 symbols, collect))
1163 return FALSE;
1164 *pneeded = TRUE;
1165 return TRUE;
1168 /* P is a common symbol. */
1170 if (h->type == bfd_link_hash_undefined)
1172 bfd *symbfd;
1173 bfd_vma size;
1174 unsigned int power;
1176 symbfd = h->u.undef.abfd;
1177 if (symbfd == NULL)
1179 /* This symbol was created as undefined from outside
1180 BFD. We assume that we should link in the object
1181 file. This is for the -u option in the linker. */
1182 if (! (*info->callbacks->add_archive_element)
1183 (info, abfd, bfd_asymbol_name (p)))
1184 return FALSE;
1185 *pneeded = TRUE;
1186 return TRUE;
1189 /* Turn the symbol into a common symbol but do not link in
1190 the object file. This is how a.out works. Object
1191 formats that require different semantics must implement
1192 this function differently. This symbol is already on the
1193 undefs list. We add the section to a common section
1194 attached to symbfd to ensure that it is in a BFD which
1195 will be linked in. */
1196 h->type = bfd_link_hash_common;
1197 h->u.c.p =
1198 bfd_hash_allocate (&info->hash->table,
1199 sizeof (struct bfd_link_hash_common_entry));
1200 if (h->u.c.p == NULL)
1201 return FALSE;
1203 size = bfd_asymbol_value (p);
1204 h->u.c.size = size;
1206 power = bfd_log2 (size);
1207 if (power > 4)
1208 power = 4;
1209 h->u.c.p->alignment_power = power;
1211 if (p->section == bfd_com_section_ptr)
1212 h->u.c.p->section = bfd_make_section_old_way (symbfd, "COMMON");
1213 else
1214 h->u.c.p->section = bfd_make_section_old_way (symbfd,
1215 p->section->name);
1216 h->u.c.p->section->flags = SEC_ALLOC;
1218 else
1220 /* Adjust the size of the common symbol if necessary. This
1221 is how a.out works. Object formats that require
1222 different semantics must implement this function
1223 differently. */
1224 if (bfd_asymbol_value (p) > h->u.c.size)
1225 h->u.c.size = bfd_asymbol_value (p);
1229 /* This archive element is not needed. */
1230 return TRUE;
1233 /* Add the symbols from an object file to the global hash table. ABFD
1234 is the object file. INFO is the linker information. SYMBOL_COUNT
1235 is the number of symbols. SYMBOLS is the list of symbols. COLLECT
1236 is TRUE if constructors should be automatically collected by name
1237 as is done by collect2. */
1239 static bfd_boolean
1240 generic_link_add_symbol_list (bfd *abfd,
1241 struct bfd_link_info *info,
1242 bfd_size_type symbol_count,
1243 asymbol **symbols,
1244 bfd_boolean collect)
1246 asymbol **pp, **ppend;
1248 pp = symbols;
1249 ppend = symbols + symbol_count;
1250 for (; pp < ppend; pp++)
1252 asymbol *p;
1254 p = *pp;
1256 if ((p->flags & (BSF_INDIRECT
1257 | BSF_WARNING
1258 | BSF_GLOBAL
1259 | BSF_CONSTRUCTOR
1260 | BSF_WEAK)) != 0
1261 || bfd_is_und_section (bfd_get_section (p))
1262 || bfd_is_com_section (bfd_get_section (p))
1263 || bfd_is_ind_section (bfd_get_section (p)))
1265 const char *name;
1266 const char *string;
1267 struct generic_link_hash_entry *h;
1268 struct bfd_link_hash_entry *bh;
1270 name = bfd_asymbol_name (p);
1271 if (((p->flags & BSF_INDIRECT) != 0
1272 || bfd_is_ind_section (p->section))
1273 && pp + 1 < ppend)
1275 pp++;
1276 string = bfd_asymbol_name (*pp);
1278 else if ((p->flags & BSF_WARNING) != 0
1279 && pp + 1 < ppend)
1281 /* The name of P is actually the warning string, and the
1282 next symbol is the one to warn about. */
1283 string = name;
1284 pp++;
1285 name = bfd_asymbol_name (*pp);
1287 else
1288 string = NULL;
1290 bh = NULL;
1291 if (! (_bfd_generic_link_add_one_symbol
1292 (info, abfd, name, p->flags, bfd_get_section (p),
1293 p->value, string, FALSE, collect, &bh)))
1294 return FALSE;
1295 h = (struct generic_link_hash_entry *) bh;
1297 /* If this is a constructor symbol, and the linker didn't do
1298 anything with it, then we want to just pass the symbol
1299 through to the output file. This will happen when
1300 linking with -r. */
1301 if ((p->flags & BSF_CONSTRUCTOR) != 0
1302 && (h == NULL || h->root.type == bfd_link_hash_new))
1304 p->udata.p = NULL;
1305 continue;
1308 /* Save the BFD symbol so that we don't lose any backend
1309 specific information that may be attached to it. We only
1310 want this one if it gives more information than the
1311 existing one; we don't want to replace a defined symbol
1312 with an undefined one. This routine may be called with a
1313 hash table other than the generic hash table, so we only
1314 do this if we are certain that the hash table is a
1315 generic one. */
1316 if (info->hash->creator == abfd->xvec)
1318 if (h->sym == NULL
1319 || (! bfd_is_und_section (bfd_get_section (p))
1320 && (! bfd_is_com_section (bfd_get_section (p))
1321 || bfd_is_und_section (bfd_get_section (h->sym)))))
1323 h->sym = p;
1324 /* BSF_OLD_COMMON is a hack to support COFF reloc
1325 reading, and it should go away when the COFF
1326 linker is switched to the new version. */
1327 if (bfd_is_com_section (bfd_get_section (p)))
1328 p->flags |= BSF_OLD_COMMON;
1332 /* Store a back pointer from the symbol to the hash
1333 table entry for the benefit of relaxation code until
1334 it gets rewritten to not use asymbol structures.
1335 Setting this is also used to check whether these
1336 symbols were set up by the generic linker. */
1337 p->udata.p = h;
1341 return TRUE;
1344 /* We use a state table to deal with adding symbols from an object
1345 file. The first index into the state table describes the symbol
1346 from the object file. The second index into the state table is the
1347 type of the symbol in the hash table. */
1349 /* The symbol from the object file is turned into one of these row
1350 values. */
1352 enum link_row
1354 UNDEF_ROW, /* Undefined. */
1355 UNDEFW_ROW, /* Weak undefined. */
1356 DEF_ROW, /* Defined. */
1357 DEFW_ROW, /* Weak defined. */
1358 COMMON_ROW, /* Common. */
1359 INDR_ROW, /* Indirect. */
1360 WARN_ROW, /* Warning. */
1361 SET_ROW /* Member of set. */
1364 /* apparently needed for Hitachi 3050R(HI-UX/WE2)? */
1365 #undef FAIL
1367 /* The actions to take in the state table. */
1369 enum link_action
1371 FAIL, /* Abort. */
1372 UND, /* Mark symbol undefined. */
1373 WEAK, /* Mark symbol weak undefined. */
1374 DEF, /* Mark symbol defined. */
1375 DEFW, /* Mark symbol weak defined. */
1376 COM, /* Mark symbol common. */
1377 REF, /* Mark defined symbol referenced. */
1378 CREF, /* Possibly warn about common reference to defined symbol. */
1379 CDEF, /* Define existing common symbol. */
1380 NOACT, /* No action. */
1381 BIG, /* Mark symbol common using largest size. */
1382 MDEF, /* Multiple definition error. */
1383 MIND, /* Multiple indirect symbols. */
1384 IND, /* Make indirect symbol. */
1385 CIND, /* Make indirect symbol from existing common symbol. */
1386 SET, /* Add value to set. */
1387 MWARN, /* Make warning symbol. */
1388 WARN, /* Issue warning. */
1389 CWARN, /* Warn if referenced, else MWARN. */
1390 CYCLE, /* Repeat with symbol pointed to. */
1391 REFC, /* Mark indirect symbol referenced and then CYCLE. */
1392 WARNC /* Issue warning and then CYCLE. */
1395 /* The state table itself. The first index is a link_row and the
1396 second index is a bfd_link_hash_type. */
1398 static const enum link_action link_action[8][8] =
1400 /* current\prev new undef undefw def defw com indr warn */
1401 /* UNDEF_ROW */ {UND, NOACT, UND, REF, REF, NOACT, REFC, WARNC },
1402 /* UNDEFW_ROW */ {WEAK, NOACT, NOACT, REF, REF, NOACT, REFC, WARNC },
1403 /* DEF_ROW */ {DEF, DEF, DEF, MDEF, DEF, CDEF, MDEF, CYCLE },
1404 /* DEFW_ROW */ {DEFW, DEFW, DEFW, NOACT, NOACT, NOACT, NOACT, CYCLE },
1405 /* COMMON_ROW */ {COM, COM, COM, CREF, COM, BIG, REFC, WARNC },
1406 /* INDR_ROW */ {IND, IND, IND, MDEF, IND, CIND, MIND, CYCLE },
1407 /* WARN_ROW */ {MWARN, WARN, WARN, CWARN, CWARN, WARN, CWARN, NOACT },
1408 /* SET_ROW */ {SET, SET, SET, SET, SET, SET, CYCLE, CYCLE }
1411 /* Most of the entries in the LINK_ACTION table are straightforward,
1412 but a few are somewhat subtle.
1414 A reference to an indirect symbol (UNDEF_ROW/indr or
1415 UNDEFW_ROW/indr) is counted as a reference both to the indirect
1416 symbol and to the symbol the indirect symbol points to.
1418 A reference to a warning symbol (UNDEF_ROW/warn or UNDEFW_ROW/warn)
1419 causes the warning to be issued.
1421 A common definition of an indirect symbol (COMMON_ROW/indr) is
1422 treated as a multiple definition error. Likewise for an indirect
1423 definition of a common symbol (INDR_ROW/com).
1425 An indirect definition of a warning (INDR_ROW/warn) does not cause
1426 the warning to be issued.
1428 If a warning is created for an indirect symbol (WARN_ROW/indr) no
1429 warning is created for the symbol the indirect symbol points to.
1431 Adding an entry to a set does not count as a reference to a set,
1432 and no warning is issued (SET_ROW/warn). */
1434 /* Return the BFD in which a hash entry has been defined, if known. */
1436 static bfd *
1437 hash_entry_bfd (struct bfd_link_hash_entry *h)
1439 while (h->type == bfd_link_hash_warning)
1440 h = h->u.i.link;
1441 switch (h->type)
1443 default:
1444 return NULL;
1445 case bfd_link_hash_undefined:
1446 case bfd_link_hash_undefweak:
1447 return h->u.undef.abfd;
1448 case bfd_link_hash_defined:
1449 case bfd_link_hash_defweak:
1450 return h->u.def.section->owner;
1451 case bfd_link_hash_common:
1452 return h->u.c.p->section->owner;
1454 /*NOTREACHED*/
1457 /* Add a symbol to the global hash table.
1458 ABFD is the BFD the symbol comes from.
1459 NAME is the name of the symbol.
1460 FLAGS is the BSF_* bits associated with the symbol.
1461 SECTION is the section in which the symbol is defined; this may be
1462 bfd_und_section_ptr or bfd_com_section_ptr.
1463 VALUE is the value of the symbol, relative to the section.
1464 STRING is used for either an indirect symbol, in which case it is
1465 the name of the symbol to indirect to, or a warning symbol, in
1466 which case it is the warning string.
1467 COPY is TRUE if NAME or STRING must be copied into locally
1468 allocated memory if they need to be saved.
1469 COLLECT is TRUE if we should automatically collect gcc constructor
1470 or destructor names as collect2 does.
1471 HASHP, if not NULL, is a place to store the created hash table
1472 entry; if *HASHP is not NULL, the caller has already looked up
1473 the hash table entry, and stored it in *HASHP. */
1475 bfd_boolean
1476 _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
1477 bfd *abfd,
1478 const char *name,
1479 flagword flags,
1480 asection *section,
1481 bfd_vma value,
1482 const char *string,
1483 bfd_boolean copy,
1484 bfd_boolean collect,
1485 struct bfd_link_hash_entry **hashp)
1487 enum link_row row;
1488 struct bfd_link_hash_entry *h;
1489 bfd_boolean cycle;
1491 if (bfd_is_ind_section (section)
1492 || (flags & BSF_INDIRECT) != 0)
1493 row = INDR_ROW;
1494 else if ((flags & BSF_WARNING) != 0)
1495 row = WARN_ROW;
1496 else if ((flags & BSF_CONSTRUCTOR) != 0)
1497 row = SET_ROW;
1498 else if (bfd_is_und_section (section))
1500 if ((flags & BSF_WEAK) != 0)
1501 row = UNDEFW_ROW;
1502 else
1503 row = UNDEF_ROW;
1505 else if ((flags & BSF_WEAK) != 0)
1506 row = DEFW_ROW;
1507 else if (bfd_is_com_section (section))
1508 row = COMMON_ROW;
1509 else
1510 row = DEF_ROW;
1512 if (hashp != NULL && *hashp != NULL)
1513 h = *hashp;
1514 else
1516 if (row == UNDEF_ROW || row == UNDEFW_ROW)
1517 h = bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE);
1518 else
1519 h = bfd_link_hash_lookup (info->hash, name, TRUE, copy, FALSE);
1520 if (h == NULL)
1522 if (hashp != NULL)
1523 *hashp = NULL;
1524 return FALSE;
1528 if (info->notice_all
1529 || (info->notice_hash != NULL
1530 && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
1532 if (! (*info->callbacks->notice) (info, h->root.string, abfd, section,
1533 value))
1534 return FALSE;
1537 if (hashp != NULL)
1538 *hashp = h;
1542 enum link_action action;
1544 cycle = FALSE;
1545 action = link_action[(int) row][(int) h->type];
1546 switch (action)
1548 case FAIL:
1549 abort ();
1551 case NOACT:
1552 /* Do nothing. */
1553 break;
1555 case UND:
1556 /* Make a new undefined symbol. */
1557 h->type = bfd_link_hash_undefined;
1558 h->u.undef.abfd = abfd;
1559 bfd_link_add_undef (info->hash, h);
1560 break;
1562 case WEAK:
1563 /* Make a new weak undefined symbol. */
1564 h->type = bfd_link_hash_undefweak;
1565 h->u.undef.abfd = abfd;
1566 break;
1568 case CDEF:
1569 /* We have found a definition for a symbol which was
1570 previously common. */
1571 BFD_ASSERT (h->type == bfd_link_hash_common);
1572 if (! ((*info->callbacks->multiple_common)
1573 (info, h->root.string,
1574 h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
1575 abfd, bfd_link_hash_defined, 0)))
1576 return FALSE;
1577 /* Fall through. */
1578 case DEF:
1579 case DEFW:
1581 enum bfd_link_hash_type oldtype;
1583 /* Define a symbol. */
1584 oldtype = h->type;
1585 if (action == DEFW)
1586 h->type = bfd_link_hash_defweak;
1587 else
1588 h->type = bfd_link_hash_defined;
1589 h->u.def.section = section;
1590 h->u.def.value = value;
1592 /* If we have been asked to, we act like collect2 and
1593 identify all functions that might be global
1594 constructors and destructors and pass them up in a
1595 callback. We only do this for certain object file
1596 types, since many object file types can handle this
1597 automatically. */
1598 if (collect && name[0] == '_')
1600 const char *s;
1602 /* A constructor or destructor name starts like this:
1603 _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
1604 the second are the same character (we accept any
1605 character there, in case a new object file format
1606 comes along with even worse naming restrictions). */
1608 #define CONS_PREFIX "GLOBAL_"
1609 #define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
1611 s = name + 1;
1612 while (*s == '_')
1613 ++s;
1614 if (s[0] == 'G'
1615 && strncmp (s, CONS_PREFIX, CONS_PREFIX_LEN - 1) == 0)
1617 char c;
1619 c = s[CONS_PREFIX_LEN + 1];
1620 if ((c == 'I' || c == 'D')
1621 && s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
1623 /* If this is a definition of a symbol which
1624 was previously weakly defined, we are in
1625 trouble. We have already added a
1626 constructor entry for the weak defined
1627 symbol, and now we are trying to add one
1628 for the new symbol. Fortunately, this case
1629 should never arise in practice. */
1630 if (oldtype == bfd_link_hash_defweak)
1631 abort ();
1633 if (! ((*info->callbacks->constructor)
1634 (info, c == 'I',
1635 h->root.string, abfd, section, value)))
1636 return FALSE;
1642 break;
1644 case COM:
1645 /* We have found a common definition for a symbol. */
1646 if (h->type == bfd_link_hash_new)
1647 bfd_link_add_undef (info->hash, h);
1648 h->type = bfd_link_hash_common;
1649 h->u.c.p =
1650 bfd_hash_allocate (&info->hash->table,
1651 sizeof (struct bfd_link_hash_common_entry));
1652 if (h->u.c.p == NULL)
1653 return FALSE;
1655 h->u.c.size = value;
1657 /* Select a default alignment based on the size. This may
1658 be overridden by the caller. */
1660 unsigned int power;
1662 power = bfd_log2 (value);
1663 if (power > 4)
1664 power = 4;
1665 h->u.c.p->alignment_power = power;
1668 /* The section of a common symbol is only used if the common
1669 symbol is actually allocated. It basically provides a
1670 hook for the linker script to decide which output section
1671 the common symbols should be put in. In most cases, the
1672 section of a common symbol will be bfd_com_section_ptr,
1673 the code here will choose a common symbol section named
1674 "COMMON", and the linker script will contain *(COMMON) in
1675 the appropriate place. A few targets use separate common
1676 sections for small symbols, and they require special
1677 handling. */
1678 if (section == bfd_com_section_ptr)
1680 h->u.c.p->section = bfd_make_section_old_way (abfd, "COMMON");
1681 h->u.c.p->section->flags = SEC_ALLOC;
1683 else if (section->owner != abfd)
1685 h->u.c.p->section = bfd_make_section_old_way (abfd,
1686 section->name);
1687 h->u.c.p->section->flags = SEC_ALLOC;
1689 else
1690 h->u.c.p->section = section;
1691 break;
1693 case REF:
1694 /* A reference to a defined symbol. */
1695 if (h->next == NULL && info->hash->undefs_tail != h)
1696 h->next = h;
1697 break;
1699 case BIG:
1700 /* We have found a common definition for a symbol which
1701 already had a common definition. Use the maximum of the
1702 two sizes, and use the section required by the larger symbol. */
1703 BFD_ASSERT (h->type == bfd_link_hash_common);
1704 if (! ((*info->callbacks->multiple_common)
1705 (info, h->root.string,
1706 h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
1707 abfd, bfd_link_hash_common, value)))
1708 return FALSE;
1709 if (value > h->u.c.size)
1711 unsigned int power;
1713 h->u.c.size = value;
1715 /* Select a default alignment based on the size. This may
1716 be overridden by the caller. */
1717 power = bfd_log2 (value);
1718 if (power > 4)
1719 power = 4;
1720 h->u.c.p->alignment_power = power;
1722 /* Some systems have special treatment for small commons,
1723 hence we want to select the section used by the larger
1724 symbol. This makes sure the symbol does not go in a
1725 small common section if it is now too large. */
1726 if (section == bfd_com_section_ptr)
1728 h->u.c.p->section
1729 = bfd_make_section_old_way (abfd, "COMMON");
1730 h->u.c.p->section->flags = SEC_ALLOC;
1732 else if (section->owner != abfd)
1734 h->u.c.p->section
1735 = bfd_make_section_old_way (abfd, section->name);
1736 h->u.c.p->section->flags = SEC_ALLOC;
1738 else
1739 h->u.c.p->section = section;
1741 break;
1743 case CREF:
1745 bfd *obfd;
1747 /* We have found a common definition for a symbol which
1748 was already defined. FIXME: It would nice if we could
1749 report the BFD which defined an indirect symbol, but we
1750 don't have anywhere to store the information. */
1751 if (h->type == bfd_link_hash_defined
1752 || h->type == bfd_link_hash_defweak)
1753 obfd = h->u.def.section->owner;
1754 else
1755 obfd = NULL;
1756 if (! ((*info->callbacks->multiple_common)
1757 (info, h->root.string, obfd, h->type, 0,
1758 abfd, bfd_link_hash_common, value)))
1759 return FALSE;
1761 break;
1763 case MIND:
1764 /* Multiple indirect symbols. This is OK if they both point
1765 to the same symbol. */
1766 if (strcmp (h->u.i.link->root.string, string) == 0)
1767 break;
1768 /* Fall through. */
1769 case MDEF:
1770 /* Handle a multiple definition. */
1771 if (!info->allow_multiple_definition)
1773 asection *msec = NULL;
1774 bfd_vma mval = 0;
1776 switch (h->type)
1778 case bfd_link_hash_defined:
1779 msec = h->u.def.section;
1780 mval = h->u.def.value;
1781 break;
1782 case bfd_link_hash_indirect:
1783 msec = bfd_ind_section_ptr;
1784 mval = 0;
1785 break;
1786 default:
1787 abort ();
1790 /* Ignore a redefinition of an absolute symbol to the
1791 same value; it's harmless. */
1792 if (h->type == bfd_link_hash_defined
1793 && bfd_is_abs_section (msec)
1794 && bfd_is_abs_section (section)
1795 && value == mval)
1796 break;
1798 if (! ((*info->callbacks->multiple_definition)
1799 (info, h->root.string, msec->owner, msec, mval,
1800 abfd, section, value)))
1801 return FALSE;
1803 break;
1805 case CIND:
1806 /* Create an indirect symbol from an existing common symbol. */
1807 BFD_ASSERT (h->type == bfd_link_hash_common);
1808 if (! ((*info->callbacks->multiple_common)
1809 (info, h->root.string,
1810 h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
1811 abfd, bfd_link_hash_indirect, 0)))
1812 return FALSE;
1813 /* Fall through. */
1814 case IND:
1815 /* Create an indirect symbol. */
1817 struct bfd_link_hash_entry *inh;
1819 /* STRING is the name of the symbol we want to indirect
1820 to. */
1821 inh = bfd_wrapped_link_hash_lookup (abfd, info, string, TRUE,
1822 copy, FALSE);
1823 if (inh == NULL)
1824 return FALSE;
1825 if (inh->type == bfd_link_hash_indirect
1826 && inh->u.i.link == h)
1828 (*_bfd_error_handler)
1829 (_("%s: indirect symbol `%s' to `%s' is a loop"),
1830 bfd_archive_filename (abfd), name, string);
1831 bfd_set_error (bfd_error_invalid_operation);
1832 return FALSE;
1834 if (inh->type == bfd_link_hash_new)
1836 inh->type = bfd_link_hash_undefined;
1837 inh->u.undef.abfd = abfd;
1838 bfd_link_add_undef (info->hash, inh);
1841 /* If the indirect symbol has been referenced, we need to
1842 push the reference down to the symbol we are
1843 referencing. */
1844 if (h->type != bfd_link_hash_new)
1846 row = UNDEF_ROW;
1847 cycle = TRUE;
1850 h->type = bfd_link_hash_indirect;
1851 h->u.i.link = inh;
1853 break;
1855 case SET:
1856 /* Add an entry to a set. */
1857 if (! (*info->callbacks->add_to_set) (info, h, BFD_RELOC_CTOR,
1858 abfd, section, value))
1859 return FALSE;
1860 break;
1862 case WARNC:
1863 /* Issue a warning and cycle. */
1864 if (h->u.i.warning != NULL)
1866 if (! (*info->callbacks->warning) (info, h->u.i.warning,
1867 h->root.string, abfd,
1868 NULL, 0))
1869 return FALSE;
1870 /* Only issue a warning once. */
1871 h->u.i.warning = NULL;
1873 /* Fall through. */
1874 case CYCLE:
1875 /* Try again with the referenced symbol. */
1876 h = h->u.i.link;
1877 cycle = TRUE;
1878 break;
1880 case REFC:
1881 /* A reference to an indirect symbol. */
1882 if (h->next == NULL && info->hash->undefs_tail != h)
1883 h->next = h;
1884 h = h->u.i.link;
1885 cycle = TRUE;
1886 break;
1888 case WARN:
1889 /* Issue a warning. */
1890 if (! (*info->callbacks->warning) (info, string, h->root.string,
1891 hash_entry_bfd (h), NULL, 0))
1892 return FALSE;
1893 break;
1895 case CWARN:
1896 /* Warn if this symbol has been referenced already,
1897 otherwise add a warning. A symbol has been referenced if
1898 the next field is not NULL, or it is the tail of the
1899 undefined symbol list. The REF case above helps to
1900 ensure this. */
1901 if (h->next != NULL || info->hash->undefs_tail == h)
1903 if (! (*info->callbacks->warning) (info, string, h->root.string,
1904 hash_entry_bfd (h), NULL, 0))
1905 return FALSE;
1906 break;
1908 /* Fall through. */
1909 case MWARN:
1910 /* Make a warning symbol. */
1912 struct bfd_link_hash_entry *sub;
1914 /* STRING is the warning to give. */
1915 sub = ((struct bfd_link_hash_entry *)
1916 ((*info->hash->table.newfunc)
1917 (NULL, &info->hash->table, h->root.string)));
1918 if (sub == NULL)
1919 return FALSE;
1920 *sub = *h;
1921 sub->type = bfd_link_hash_warning;
1922 sub->u.i.link = h;
1923 if (! copy)
1924 sub->u.i.warning = string;
1925 else
1927 char *w;
1928 size_t len = strlen (string) + 1;
1930 w = bfd_hash_allocate (&info->hash->table, len);
1931 if (w == NULL)
1932 return FALSE;
1933 memcpy (w, string, len);
1934 sub->u.i.warning = w;
1937 bfd_hash_replace (&info->hash->table,
1938 (struct bfd_hash_entry *) h,
1939 (struct bfd_hash_entry *) sub);
1940 if (hashp != NULL)
1941 *hashp = sub;
1943 break;
1946 while (cycle);
1948 return TRUE;
1951 /* Generic final link routine. */
1953 bfd_boolean
1954 _bfd_generic_final_link (bfd *abfd, struct bfd_link_info *info)
1956 bfd *sub;
1957 asection *o;
1958 struct bfd_link_order *p;
1959 size_t outsymalloc;
1960 struct generic_write_global_symbol_info wginfo;
1962 bfd_get_outsymbols (abfd) = NULL;
1963 bfd_get_symcount (abfd) = 0;
1964 outsymalloc = 0;
1966 /* Mark all sections which will be included in the output file. */
1967 for (o = abfd->sections; o != NULL; o = o->next)
1968 for (p = o->link_order_head; p != NULL; p = p->next)
1969 if (p->type == bfd_indirect_link_order)
1970 p->u.indirect.section->linker_mark = TRUE;
1972 /* Build the output symbol table. */
1973 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1974 if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
1975 return FALSE;
1977 /* Accumulate the global symbols. */
1978 wginfo.info = info;
1979 wginfo.output_bfd = abfd;
1980 wginfo.psymalloc = &outsymalloc;
1981 _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
1982 _bfd_generic_link_write_global_symbol,
1983 &wginfo);
1985 /* Make sure we have a trailing NULL pointer on OUTSYMBOLS. We
1986 shouldn't really need one, since we have SYMCOUNT, but some old
1987 code still expects one. */
1988 if (! generic_add_output_symbol (abfd, &outsymalloc, NULL))
1989 return FALSE;
1991 if (info->relocatable)
1993 /* Allocate space for the output relocs for each section. */
1994 for (o = abfd->sections; o != NULL; o = o->next)
1996 o->reloc_count = 0;
1997 for (p = o->link_order_head; p != NULL; p = p->next)
1999 if (p->type == bfd_section_reloc_link_order
2000 || p->type == bfd_symbol_reloc_link_order)
2001 ++o->reloc_count;
2002 else if (p->type == bfd_indirect_link_order)
2004 asection *input_section;
2005 bfd *input_bfd;
2006 long relsize;
2007 arelent **relocs;
2008 asymbol **symbols;
2009 long reloc_count;
2011 input_section = p->u.indirect.section;
2012 input_bfd = input_section->owner;
2013 relsize = bfd_get_reloc_upper_bound (input_bfd,
2014 input_section);
2015 if (relsize < 0)
2016 return FALSE;
2017 relocs = bfd_malloc (relsize);
2018 if (!relocs && relsize != 0)
2019 return FALSE;
2020 symbols = _bfd_generic_link_get_symbols (input_bfd);
2021 reloc_count = bfd_canonicalize_reloc (input_bfd,
2022 input_section,
2023 relocs,
2024 symbols);
2025 free (relocs);
2026 if (reloc_count < 0)
2027 return FALSE;
2028 BFD_ASSERT ((unsigned long) reloc_count
2029 == input_section->reloc_count);
2030 o->reloc_count += reloc_count;
2033 if (o->reloc_count > 0)
2035 bfd_size_type amt;
2037 amt = o->reloc_count;
2038 amt *= sizeof (arelent *);
2039 o->orelocation = bfd_alloc (abfd, amt);
2040 if (!o->orelocation)
2041 return FALSE;
2042 o->flags |= SEC_RELOC;
2043 /* Reset the count so that it can be used as an index
2044 when putting in the output relocs. */
2045 o->reloc_count = 0;
2050 /* Handle all the link order information for the sections. */
2051 for (o = abfd->sections; o != NULL; o = o->next)
2053 for (p = o->link_order_head; p != NULL; p = p->next)
2055 switch (p->type)
2057 case bfd_section_reloc_link_order:
2058 case bfd_symbol_reloc_link_order:
2059 if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
2060 return FALSE;
2061 break;
2062 case bfd_indirect_link_order:
2063 if (! default_indirect_link_order (abfd, info, o, p, TRUE))
2064 return FALSE;
2065 break;
2066 default:
2067 if (! _bfd_default_link_order (abfd, info, o, p))
2068 return FALSE;
2069 break;
2074 return TRUE;
2077 /* Add an output symbol to the output BFD. */
2079 static bfd_boolean
2080 generic_add_output_symbol (bfd *output_bfd, size_t *psymalloc, asymbol *sym)
2082 if (bfd_get_symcount (output_bfd) >= *psymalloc)
2084 asymbol **newsyms;
2085 bfd_size_type amt;
2087 if (*psymalloc == 0)
2088 *psymalloc = 124;
2089 else
2090 *psymalloc *= 2;
2091 amt = *psymalloc;
2092 amt *= sizeof (asymbol *);
2093 newsyms = bfd_realloc (bfd_get_outsymbols (output_bfd), amt);
2094 if (newsyms == NULL)
2095 return FALSE;
2096 bfd_get_outsymbols (output_bfd) = newsyms;
2099 bfd_get_outsymbols (output_bfd) [bfd_get_symcount (output_bfd)] = sym;
2100 if (sym != NULL)
2101 ++ bfd_get_symcount (output_bfd);
2103 return TRUE;
2106 /* Handle the symbols for an input BFD. */
2108 bfd_boolean
2109 _bfd_generic_link_output_symbols (bfd *output_bfd,
2110 bfd *input_bfd,
2111 struct bfd_link_info *info,
2112 size_t *psymalloc)
2114 asymbol **sym_ptr;
2115 asymbol **sym_end;
2117 if (! generic_link_read_symbols (input_bfd))
2118 return FALSE;
2120 /* Create a filename symbol if we are supposed to. */
2121 if (info->create_object_symbols_section != NULL)
2123 asection *sec;
2125 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
2127 if (sec->output_section == info->create_object_symbols_section)
2129 asymbol *newsym;
2131 newsym = bfd_make_empty_symbol (input_bfd);
2132 if (!newsym)
2133 return FALSE;
2134 newsym->name = input_bfd->filename;
2135 newsym->value = 0;
2136 newsym->flags = BSF_LOCAL | BSF_FILE;
2137 newsym->section = sec;
2139 if (! generic_add_output_symbol (output_bfd, psymalloc,
2140 newsym))
2141 return FALSE;
2143 break;
2148 /* Adjust the values of the globally visible symbols, and write out
2149 local symbols. */
2150 sym_ptr = _bfd_generic_link_get_symbols (input_bfd);
2151 sym_end = sym_ptr + _bfd_generic_link_get_symcount (input_bfd);
2152 for (; sym_ptr < sym_end; sym_ptr++)
2154 asymbol *sym;
2155 struct generic_link_hash_entry *h;
2156 bfd_boolean output;
2158 h = NULL;
2159 sym = *sym_ptr;
2160 if ((sym->flags & (BSF_INDIRECT
2161 | BSF_WARNING
2162 | BSF_GLOBAL
2163 | BSF_CONSTRUCTOR
2164 | BSF_WEAK)) != 0
2165 || bfd_is_und_section (bfd_get_section (sym))
2166 || bfd_is_com_section (bfd_get_section (sym))
2167 || bfd_is_ind_section (bfd_get_section (sym)))
2169 if (sym->udata.p != NULL)
2170 h = sym->udata.p;
2171 else if ((sym->flags & BSF_CONSTRUCTOR) != 0)
2173 /* This case normally means that the main linker code
2174 deliberately ignored this constructor symbol. We
2175 should just pass it through. This will screw up if
2176 the constructor symbol is from a different,
2177 non-generic, object file format, but the case will
2178 only arise when linking with -r, which will probably
2179 fail anyhow, since there will be no way to represent
2180 the relocs in the output format being used. */
2181 h = NULL;
2183 else if (bfd_is_und_section (bfd_get_section (sym)))
2184 h = ((struct generic_link_hash_entry *)
2185 bfd_wrapped_link_hash_lookup (output_bfd, info,
2186 bfd_asymbol_name (sym),
2187 FALSE, FALSE, TRUE));
2188 else
2189 h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
2190 bfd_asymbol_name (sym),
2191 FALSE, FALSE, TRUE);
2193 if (h != NULL)
2195 /* Force all references to this symbol to point to
2196 the same area in memory. It is possible that
2197 this routine will be called with a hash table
2198 other than a generic hash table, so we double
2199 check that. */
2200 if (info->hash->creator == input_bfd->xvec)
2202 if (h->sym != NULL)
2203 *sym_ptr = sym = h->sym;
2206 switch (h->root.type)
2208 default:
2209 case bfd_link_hash_new:
2210 abort ();
2211 case bfd_link_hash_undefined:
2212 break;
2213 case bfd_link_hash_undefweak:
2214 sym->flags |= BSF_WEAK;
2215 break;
2216 case bfd_link_hash_indirect:
2217 h = (struct generic_link_hash_entry *) h->root.u.i.link;
2218 /* fall through */
2219 case bfd_link_hash_defined:
2220 sym->flags |= BSF_GLOBAL;
2221 sym->flags &=~ BSF_CONSTRUCTOR;
2222 sym->value = h->root.u.def.value;
2223 sym->section = h->root.u.def.section;
2224 break;
2225 case bfd_link_hash_defweak:
2226 sym->flags |= BSF_WEAK;
2227 sym->flags &=~ BSF_CONSTRUCTOR;
2228 sym->value = h->root.u.def.value;
2229 sym->section = h->root.u.def.section;
2230 break;
2231 case bfd_link_hash_common:
2232 sym->value = h->root.u.c.size;
2233 sym->flags |= BSF_GLOBAL;
2234 if (! bfd_is_com_section (sym->section))
2236 BFD_ASSERT (bfd_is_und_section (sym->section));
2237 sym->section = bfd_com_section_ptr;
2239 /* We do not set the section of the symbol to
2240 h->root.u.c.p->section. That value was saved so
2241 that we would know where to allocate the symbol
2242 if it was defined. In this case the type is
2243 still bfd_link_hash_common, so we did not define
2244 it, so we do not want to use that section. */
2245 break;
2250 /* This switch is straight from the old code in
2251 write_file_locals in ldsym.c. */
2252 if (info->strip == strip_all
2253 || (info->strip == strip_some
2254 && bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym),
2255 FALSE, FALSE) == NULL))
2256 output = FALSE;
2257 else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
2259 /* If this symbol is marked as occurring now, rather
2260 than at the end, output it now. This is used for
2261 COFF C_EXT FCN symbols. FIXME: There must be a
2262 better way. */
2263 if (bfd_asymbol_bfd (sym) == input_bfd
2264 && (sym->flags & BSF_NOT_AT_END) != 0)
2265 output = TRUE;
2266 else
2267 output = FALSE;
2269 else if (bfd_is_ind_section (sym->section))
2270 output = FALSE;
2271 else if ((sym->flags & BSF_DEBUGGING) != 0)
2273 if (info->strip == strip_none)
2274 output = TRUE;
2275 else
2276 output = FALSE;
2278 else if (bfd_is_und_section (sym->section)
2279 || bfd_is_com_section (sym->section))
2280 output = FALSE;
2281 else if ((sym->flags & BSF_LOCAL) != 0)
2283 if ((sym->flags & BSF_WARNING) != 0)
2284 output = FALSE;
2285 else
2287 switch (info->discard)
2289 default:
2290 case discard_all:
2291 output = FALSE;
2292 break;
2293 case discard_sec_merge:
2294 output = TRUE;
2295 if (info->relocatable
2296 || ! (sym->section->flags & SEC_MERGE))
2297 break;
2298 /* FALLTHROUGH */
2299 case discard_l:
2300 if (bfd_is_local_label (input_bfd, sym))
2301 output = FALSE;
2302 else
2303 output = TRUE;
2304 break;
2305 case discard_none:
2306 output = TRUE;
2307 break;
2311 else if ((sym->flags & BSF_CONSTRUCTOR))
2313 if (info->strip != strip_all)
2314 output = TRUE;
2315 else
2316 output = FALSE;
2318 else
2319 abort ();
2321 /* If this symbol is in a section which is not being included
2322 in the output file, then we don't want to output the symbol.
2324 Gross. .bss and similar sections won't have the linker_mark
2325 field set. */
2326 if ((sym->section->flags & SEC_HAS_CONTENTS) != 0
2327 && ! sym->section->linker_mark)
2328 output = FALSE;
2330 if (output)
2332 if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
2333 return FALSE;
2334 if (h != NULL)
2335 h->written = TRUE;
2339 return TRUE;
2342 /* Set the section and value of a generic BFD symbol based on a linker
2343 hash table entry. */
2345 static void
2346 set_symbol_from_hash (asymbol *sym, struct bfd_link_hash_entry *h)
2348 switch (h->type)
2350 default:
2351 abort ();
2352 break;
2353 case bfd_link_hash_new:
2354 /* This can happen when a constructor symbol is seen but we are
2355 not building constructors. */
2356 if (sym->section != NULL)
2358 BFD_ASSERT ((sym->flags & BSF_CONSTRUCTOR) != 0);
2360 else
2362 sym->flags |= BSF_CONSTRUCTOR;
2363 sym->section = bfd_abs_section_ptr;
2364 sym->value = 0;
2366 break;
2367 case bfd_link_hash_undefined:
2368 sym->section = bfd_und_section_ptr;
2369 sym->value = 0;
2370 break;
2371 case bfd_link_hash_undefweak:
2372 sym->section = bfd_und_section_ptr;
2373 sym->value = 0;
2374 sym->flags |= BSF_WEAK;
2375 break;
2376 case bfd_link_hash_defined:
2377 sym->section = h->u.def.section;
2378 sym->value = h->u.def.value;
2379 break;
2380 case bfd_link_hash_defweak:
2381 sym->flags |= BSF_WEAK;
2382 sym->section = h->u.def.section;
2383 sym->value = h->u.def.value;
2384 break;
2385 case bfd_link_hash_common:
2386 sym->value = h->u.c.size;
2387 if (sym->section == NULL)
2388 sym->section = bfd_com_section_ptr;
2389 else if (! bfd_is_com_section (sym->section))
2391 BFD_ASSERT (bfd_is_und_section (sym->section));
2392 sym->section = bfd_com_section_ptr;
2394 /* Do not set the section; see _bfd_generic_link_output_symbols. */
2395 break;
2396 case bfd_link_hash_indirect:
2397 case bfd_link_hash_warning:
2398 /* FIXME: What should we do here? */
2399 break;
2403 /* Write out a global symbol, if it hasn't already been written out.
2404 This is called for each symbol in the hash table. */
2406 bfd_boolean
2407 _bfd_generic_link_write_global_symbol (struct generic_link_hash_entry *h,
2408 void *data)
2410 struct generic_write_global_symbol_info *wginfo = data;
2411 asymbol *sym;
2413 if (h->root.type == bfd_link_hash_warning)
2414 h = (struct generic_link_hash_entry *) h->root.u.i.link;
2416 if (h->written)
2417 return TRUE;
2419 h->written = TRUE;
2421 if (wginfo->info->strip == strip_all
2422 || (wginfo->info->strip == strip_some
2423 && bfd_hash_lookup (wginfo->info->keep_hash, h->root.root.string,
2424 FALSE, FALSE) == NULL))
2425 return TRUE;
2427 if (h->sym != NULL)
2428 sym = h->sym;
2429 else
2431 sym = bfd_make_empty_symbol (wginfo->output_bfd);
2432 if (!sym)
2433 return FALSE;
2434 sym->name = h->root.root.string;
2435 sym->flags = 0;
2438 set_symbol_from_hash (sym, &h->root);
2440 sym->flags |= BSF_GLOBAL;
2442 if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
2443 sym))
2445 /* FIXME: No way to return failure. */
2446 abort ();
2449 return TRUE;
2452 /* Create a relocation. */
2454 bfd_boolean
2455 _bfd_generic_reloc_link_order (bfd *abfd,
2456 struct bfd_link_info *info,
2457 asection *sec,
2458 struct bfd_link_order *link_order)
2460 arelent *r;
2462 if (! info->relocatable)
2463 abort ();
2464 if (sec->orelocation == NULL)
2465 abort ();
2467 r = bfd_alloc (abfd, sizeof (arelent));
2468 if (r == NULL)
2469 return FALSE;
2471 r->address = link_order->offset;
2472 r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
2473 if (r->howto == 0)
2475 bfd_set_error (bfd_error_bad_value);
2476 return FALSE;
2479 /* Get the symbol to use for the relocation. */
2480 if (link_order->type == bfd_section_reloc_link_order)
2481 r->sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
2482 else
2484 struct generic_link_hash_entry *h;
2486 h = ((struct generic_link_hash_entry *)
2487 bfd_wrapped_link_hash_lookup (abfd, info,
2488 link_order->u.reloc.p->u.name,
2489 FALSE, FALSE, TRUE));
2490 if (h == NULL
2491 || ! h->written)
2493 if (! ((*info->callbacks->unattached_reloc)
2494 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
2495 return FALSE;
2496 bfd_set_error (bfd_error_bad_value);
2497 return FALSE;
2499 r->sym_ptr_ptr = &h->sym;
2502 /* If this is an inplace reloc, write the addend to the object file.
2503 Otherwise, store it in the reloc addend. */
2504 if (! r->howto->partial_inplace)
2505 r->addend = link_order->u.reloc.p->addend;
2506 else
2508 bfd_size_type size;
2509 bfd_reloc_status_type rstat;
2510 bfd_byte *buf;
2511 bfd_boolean ok;
2512 file_ptr loc;
2514 size = bfd_get_reloc_size (r->howto);
2515 buf = bfd_zmalloc (size);
2516 if (buf == NULL)
2517 return FALSE;
2518 rstat = _bfd_relocate_contents (r->howto, abfd,
2519 (bfd_vma) link_order->u.reloc.p->addend,
2520 buf);
2521 switch (rstat)
2523 case bfd_reloc_ok:
2524 break;
2525 default:
2526 case bfd_reloc_outofrange:
2527 abort ();
2528 case bfd_reloc_overflow:
2529 if (! ((*info->callbacks->reloc_overflow)
2530 (info,
2531 (link_order->type == bfd_section_reloc_link_order
2532 ? bfd_section_name (abfd, link_order->u.reloc.p->u.section)
2533 : link_order->u.reloc.p->u.name),
2534 r->howto->name, link_order->u.reloc.p->addend,
2535 NULL, NULL, 0)))
2537 free (buf);
2538 return FALSE;
2540 break;
2542 loc = link_order->offset * bfd_octets_per_byte (abfd);
2543 ok = bfd_set_section_contents (abfd, sec, buf, loc, size);
2544 free (buf);
2545 if (! ok)
2546 return FALSE;
2548 r->addend = 0;
2551 sec->orelocation[sec->reloc_count] = r;
2552 ++sec->reloc_count;
2554 return TRUE;
2557 /* Allocate a new link_order for a section. */
2559 struct bfd_link_order *
2560 bfd_new_link_order (bfd *abfd, asection *section)
2562 bfd_size_type amt = sizeof (struct bfd_link_order);
2563 struct bfd_link_order *new;
2565 new = bfd_zalloc (abfd, amt);
2566 if (!new)
2567 return NULL;
2569 new->type = bfd_undefined_link_order;
2571 if (section->link_order_tail != NULL)
2572 section->link_order_tail->next = new;
2573 else
2574 section->link_order_head = new;
2575 section->link_order_tail = new;
2577 return new;
2580 /* Default link order processing routine. Note that we can not handle
2581 the reloc_link_order types here, since they depend upon the details
2582 of how the particular backends generates relocs. */
2584 bfd_boolean
2585 _bfd_default_link_order (bfd *abfd,
2586 struct bfd_link_info *info,
2587 asection *sec,
2588 struct bfd_link_order *link_order)
2590 switch (link_order->type)
2592 case bfd_undefined_link_order:
2593 case bfd_section_reloc_link_order:
2594 case bfd_symbol_reloc_link_order:
2595 default:
2596 abort ();
2597 case bfd_indirect_link_order:
2598 return default_indirect_link_order (abfd, info, sec, link_order,
2599 FALSE);
2600 case bfd_data_link_order:
2601 return default_data_link_order (abfd, info, sec, link_order);
2605 /* Default routine to handle a bfd_data_link_order. */
2607 static bfd_boolean
2608 default_data_link_order (bfd *abfd,
2609 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2610 asection *sec,
2611 struct bfd_link_order *link_order)
2613 bfd_size_type size;
2614 size_t fill_size;
2615 bfd_byte *fill;
2616 file_ptr loc;
2617 bfd_boolean result;
2619 BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
2621 size = link_order->size;
2622 if (size == 0)
2623 return TRUE;
2625 fill = link_order->u.data.contents;
2626 fill_size = link_order->u.data.size;
2627 if (fill_size != 0 && fill_size < size)
2629 bfd_byte *p;
2630 fill = bfd_malloc (size);
2631 if (fill == NULL)
2632 return FALSE;
2633 p = fill;
2634 if (fill_size == 1)
2635 memset (p, (int) link_order->u.data.contents[0], (size_t) size);
2636 else
2640 memcpy (p, link_order->u.data.contents, fill_size);
2641 p += fill_size;
2642 size -= fill_size;
2644 while (size >= fill_size);
2645 if (size != 0)
2646 memcpy (p, link_order->u.data.contents, (size_t) size);
2647 size = link_order->size;
2651 loc = link_order->offset * bfd_octets_per_byte (abfd);
2652 result = bfd_set_section_contents (abfd, sec, fill, loc, size);
2654 if (fill != link_order->u.data.contents)
2655 free (fill);
2656 return result;
2659 /* Default routine to handle a bfd_indirect_link_order. */
2661 static bfd_boolean
2662 default_indirect_link_order (bfd *output_bfd,
2663 struct bfd_link_info *info,
2664 asection *output_section,
2665 struct bfd_link_order *link_order,
2666 bfd_boolean generic_linker)
2668 asection *input_section;
2669 bfd *input_bfd;
2670 bfd_byte *contents = NULL;
2671 bfd_byte *new_contents;
2672 bfd_size_type sec_size;
2673 file_ptr loc;
2675 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
2677 if (link_order->size == 0)
2678 return TRUE;
2680 input_section = link_order->u.indirect.section;
2681 input_bfd = input_section->owner;
2683 BFD_ASSERT (input_section->output_section == output_section);
2684 BFD_ASSERT (input_section->output_offset == link_order->offset);
2685 BFD_ASSERT (input_section->_cooked_size == link_order->size);
2687 if (info->relocatable
2688 && input_section->reloc_count > 0
2689 && output_section->orelocation == NULL)
2691 /* Space has not been allocated for the output relocations.
2692 This can happen when we are called by a specific backend
2693 because somebody is attempting to link together different
2694 types of object files. Handling this case correctly is
2695 difficult, and sometimes impossible. */
2696 (*_bfd_error_handler)
2697 (_("Attempt to do relocatable link with %s input and %s output"),
2698 bfd_get_target (input_bfd), bfd_get_target (output_bfd));
2699 bfd_set_error (bfd_error_wrong_format);
2700 return FALSE;
2703 if (! generic_linker)
2705 asymbol **sympp;
2706 asymbol **symppend;
2708 /* Get the canonical symbols. The generic linker will always
2709 have retrieved them by this point, but we are being called by
2710 a specific linker, presumably because we are linking
2711 different types of object files together. */
2712 if (! generic_link_read_symbols (input_bfd))
2713 return FALSE;
2715 /* Since we have been called by a specific linker, rather than
2716 the generic linker, the values of the symbols will not be
2717 right. They will be the values as seen in the input file,
2718 not the values of the final link. We need to fix them up
2719 before we can relocate the section. */
2720 sympp = _bfd_generic_link_get_symbols (input_bfd);
2721 symppend = sympp + _bfd_generic_link_get_symcount (input_bfd);
2722 for (; sympp < symppend; sympp++)
2724 asymbol *sym;
2725 struct bfd_link_hash_entry *h;
2727 sym = *sympp;
2729 if ((sym->flags & (BSF_INDIRECT
2730 | BSF_WARNING
2731 | BSF_GLOBAL
2732 | BSF_CONSTRUCTOR
2733 | BSF_WEAK)) != 0
2734 || bfd_is_und_section (bfd_get_section (sym))
2735 || bfd_is_com_section (bfd_get_section (sym))
2736 || bfd_is_ind_section (bfd_get_section (sym)))
2738 /* sym->udata may have been set by
2739 generic_link_add_symbol_list. */
2740 if (sym->udata.p != NULL)
2741 h = sym->udata.p;
2742 else if (bfd_is_und_section (bfd_get_section (sym)))
2743 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
2744 bfd_asymbol_name (sym),
2745 FALSE, FALSE, TRUE);
2746 else
2747 h = bfd_link_hash_lookup (info->hash,
2748 bfd_asymbol_name (sym),
2749 FALSE, FALSE, TRUE);
2750 if (h != NULL)
2751 set_symbol_from_hash (sym, h);
2756 /* Get and relocate the section contents. */
2757 sec_size = bfd_section_size (input_bfd, input_section);
2758 contents = bfd_malloc (sec_size);
2759 if (contents == NULL && sec_size != 0)
2760 goto error_return;
2761 new_contents = (bfd_get_relocated_section_contents
2762 (output_bfd, info, link_order, contents, info->relocatable,
2763 _bfd_generic_link_get_symbols (input_bfd)));
2764 if (!new_contents)
2765 goto error_return;
2767 /* Output the section contents. */
2768 loc = link_order->offset * bfd_octets_per_byte (output_bfd);
2769 if (! bfd_set_section_contents (output_bfd, output_section,
2770 new_contents, loc, link_order->size))
2771 goto error_return;
2773 if (contents != NULL)
2774 free (contents);
2775 return TRUE;
2777 error_return:
2778 if (contents != NULL)
2779 free (contents);
2780 return FALSE;
2783 /* A little routine to count the number of relocs in a link_order
2784 list. */
2786 unsigned int
2787 _bfd_count_link_order_relocs (struct bfd_link_order *link_order)
2789 register unsigned int c;
2790 register struct bfd_link_order *l;
2792 c = 0;
2793 for (l = link_order; l != NULL; l = l->next)
2795 if (l->type == bfd_section_reloc_link_order
2796 || l->type == bfd_symbol_reloc_link_order)
2797 ++c;
2800 return c;
2804 FUNCTION
2805 bfd_link_split_section
2807 SYNOPSIS
2808 bfd_boolean bfd_link_split_section (bfd *abfd, asection *sec);
2810 DESCRIPTION
2811 Return nonzero if @var{sec} should be split during a
2812 reloceatable or final link.
2814 .#define bfd_link_split_section(abfd, sec) \
2815 . BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
2820 bfd_boolean
2821 _bfd_generic_link_split_section (bfd *abfd ATTRIBUTE_UNUSED,
2822 asection *sec ATTRIBUTE_UNUSED)
2824 return FALSE;