1 /* linker.c -- BFD linker routines
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
35 The linker uses three special entry points in the BFD target
36 vector. It is not necessary to write special routines for
37 these entry points when creating a new BFD back end, since
38 generic versions are provided. However, writing them can
39 speed up linking and make it use significantly less runtime
42 The first routine creates a hash table used by the other
43 routines. The second routine adds the symbols from an object
44 file to the hash table. The third routine takes all the
45 object files and links them together to create the output
46 file. These routines are designed so that the linker proper
47 does not need to know anything about the symbols in the object
48 files that it is linking. The linker merely arranges the
49 sections as directed by the linker script and lets BFD handle
50 the details of symbols and relocs.
52 The second routine and third routines are passed a pointer to
53 a <<struct bfd_link_info>> structure (defined in
54 <<bfdlink.h>>) which holds information relevant to the link,
55 including the linker hash table (which was created by the
56 first routine) and a set of callback functions to the linker
59 The generic linker routines are in <<linker.c>>, and use the
60 header file <<genlink.h>>. As of this writing, the only back
61 ends which have implemented versions of these routines are
62 a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>). The a.out
63 routines are used as examples throughout this section.
66 @* Creating a Linker Hash Table::
67 @* Adding Symbols to the Hash Table::
68 @* Performing the Final Link::
72 Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
74 Creating a linker hash table
76 @cindex _bfd_link_hash_table_create in target vector
77 @cindex target vector (_bfd_link_hash_table_create)
78 The linker routines must create a hash table, which must be
79 derived from <<struct bfd_link_hash_table>> described in
80 <<bfdlink.c>>. @xref{Hash Tables}, for information on how to
81 create a derived hash table. This entry point is called using
82 the target vector of the linker output file.
84 The <<_bfd_link_hash_table_create>> entry point must allocate
85 and initialize an instance of the desired hash table. If the
86 back end does not require any additional information to be
87 stored with the entries in the hash table, the entry point may
88 simply create a <<struct bfd_link_hash_table>>. Most likely,
89 however, some additional information will be needed.
91 For example, with each entry in the hash table the a.out
92 linker keeps the index the symbol has in the final output file
93 (this index number is used so that when doing a relocatable
94 link the symbol index used in the output file can be quickly
95 filled in when copying over a reloc). The a.out linker code
96 defines the required structures and functions for a hash table
97 derived from <<struct bfd_link_hash_table>>. The a.out linker
98 hash table is created by the function
99 <<NAME(aout,link_hash_table_create)>>; it simply allocates
100 space for the hash table, initializes it, and returns a
103 When writing the linker routines for a new back end, you will
104 generally not know exactly which fields will be required until
105 you have finished. You should simply create a new hash table
106 which defines no additional fields, and then simply add fields
107 as they become necessary.
110 Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
112 Adding symbols to the hash table
114 @cindex _bfd_link_add_symbols in target vector
115 @cindex target vector (_bfd_link_add_symbols)
116 The linker proper will call the <<_bfd_link_add_symbols>>
117 entry point for each object file or archive which is to be
118 linked (typically these are the files named on the command
119 line, but some may also come from the linker script). The
120 entry point is responsible for examining the file. For an
121 object file, BFD must add any relevant symbol information to
122 the hash table. For an archive, BFD must determine which
123 elements of the archive should be used and adding them to the
126 The a.out version of this entry point is
127 <<NAME(aout,link_add_symbols)>>.
130 @* Differing file formats::
131 @* Adding symbols from an object file::
132 @* Adding symbols from an archive::
136 Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
138 Differing file formats
140 Normally all the files involved in a link will be of the same
141 format, but it is also possible to link together different
142 format object files, and the back end must support that. The
143 <<_bfd_link_add_symbols>> entry point is called via the target
144 vector of the file to be added. This has an important
145 consequence: the function may not assume that the hash table
146 is the type created by the corresponding
147 <<_bfd_link_hash_table_create>> vector. All the
148 <<_bfd_link_add_symbols>> function can assume about the hash
149 table is that it is derived from <<struct
150 bfd_link_hash_table>>.
152 Sometimes the <<_bfd_link_add_symbols>> function must store
153 some information in the hash table entry to be used by the
154 <<_bfd_final_link>> function. In such a case the output bfd
155 xvec must be checked to make sure that the hash table was
156 created by an object file of the same format.
158 The <<_bfd_final_link>> routine must be prepared to handle a
159 hash entry without any extra information added by the
160 <<_bfd_link_add_symbols>> function. A hash entry without
161 extra information will also occur when the linker script
162 directs the linker to create a symbol. Note that, regardless
163 of how a hash table entry is added, all the fields will be
164 initialized to some sort of null value by the hash table entry
165 initialization function.
167 See <<ecoff_link_add_externals>> for an example of how to
168 check the output bfd before saving information (in this
169 case, the ECOFF external symbol debugging information) in a
173 Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
175 Adding symbols from an object file
177 When the <<_bfd_link_add_symbols>> routine is passed an object
178 file, it must add all externally visible symbols in that
179 object file to the hash table. The actual work of adding the
180 symbol to the hash table is normally handled by the function
181 <<_bfd_generic_link_add_one_symbol>>. The
182 <<_bfd_link_add_symbols>> routine is responsible for reading
183 all the symbols from the object file and passing the correct
184 information to <<_bfd_generic_link_add_one_symbol>>.
186 The <<_bfd_link_add_symbols>> routine should not use
187 <<bfd_canonicalize_symtab>> to read the symbols. The point of
188 providing this routine is to avoid the overhead of converting
189 the symbols into generic <<asymbol>> structures.
191 @findex _bfd_generic_link_add_one_symbol
192 <<_bfd_generic_link_add_one_symbol>> handles the details of
193 combining common symbols, warning about multiple definitions,
194 and so forth. It takes arguments which describe the symbol to
195 add, notably symbol flags, a section, and an offset. The
196 symbol flags include such things as <<BSF_WEAK>> or
197 <<BSF_INDIRECT>>. The section is a section in the object
198 file, or something like <<bfd_und_section_ptr>> for an undefined
199 symbol or <<bfd_com_section_ptr>> for a common symbol.
201 If the <<_bfd_final_link>> routine is also going to need to
202 read the symbol information, the <<_bfd_link_add_symbols>>
203 routine should save it somewhere attached to the object file
204 BFD. However, the information should only be saved if the
205 <<keep_memory>> field of the <<info>> argument is TRUE, so
206 that the <<-no-keep-memory>> linker switch is effective.
208 The a.out function which adds symbols from an object file is
209 <<aout_link_add_object_symbols>>, and most of the interesting
210 work is in <<aout_link_add_symbols>>. The latter saves
211 pointers to the hash tables entries created by
212 <<_bfd_generic_link_add_one_symbol>> indexed by symbol number,
213 so that the <<_bfd_final_link>> routine does not have to call
214 the hash table lookup routine to locate the entry.
217 Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
219 Adding symbols from an archive
221 When the <<_bfd_link_add_symbols>> routine is passed an
222 archive, it must look through the symbols defined by the
223 archive and decide which elements of the archive should be
224 included in the link. For each such element it must call the
225 <<add_archive_element>> linker callback, and it must add the
226 symbols from the object file to the linker hash table. (The
227 callback may in fact indicate that a replacement BFD should be
228 used, in which case the symbols from that BFD should be added
229 to the linker hash table instead.)
231 @findex _bfd_generic_link_add_archive_symbols
232 In most cases the work of looking through the symbols in the
233 archive should be done by the
234 <<_bfd_generic_link_add_archive_symbols>> function. This
235 function builds a hash table from the archive symbol table and
236 looks through the list of undefined symbols to see which
237 elements should be included.
238 <<_bfd_generic_link_add_archive_symbols>> is passed a function
239 to call to make the final decision about adding an archive
240 element to the link and to do the actual work of adding the
241 symbols to the linker hash table.
243 The function passed to
244 <<_bfd_generic_link_add_archive_symbols>> must read the
245 symbols of the archive element and decide whether the archive
246 element should be included in the link. If the element is to
247 be included, the <<add_archive_element>> linker callback
248 routine must be called with the element as an argument, and
249 the element's symbols must be added to the linker hash table
250 just as though the element had itself been passed to the
251 <<_bfd_link_add_symbols>> function. The <<add_archive_element>>
252 callback has the option to indicate that it would like to
253 replace the element archive with a substitute BFD, in which
254 case it is the symbols of that substitute BFD that must be
255 added to the linker hash table instead.
257 When the a.out <<_bfd_link_add_symbols>> function receives an
258 archive, it calls <<_bfd_generic_link_add_archive_symbols>>
259 passing <<aout_link_check_archive_element>> as the function
260 argument. <<aout_link_check_archive_element>> calls
261 <<aout_link_check_ar_symbols>>. If the latter decides to add
262 the element (an element is only added if it provides a real,
263 non-common, definition for a previously undefined or common
264 symbol) it calls the <<add_archive_element>> callback and then
265 <<aout_link_check_archive_element>> calls
266 <<aout_link_add_symbols>> to actually add the symbols to the
267 linker hash table - possibly those of a substitute BFD, if the
268 <<add_archive_element>> callback avails itself of that option.
270 The ECOFF back end is unusual in that it does not normally
271 call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
272 archives already contain a hash table of symbols. The ECOFF
273 back end searches the archive itself to avoid the overhead of
274 creating a new hash table.
277 Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions
279 Performing the final link
281 @cindex _bfd_link_final_link in target vector
282 @cindex target vector (_bfd_final_link)
283 When all the input files have been processed, the linker calls
284 the <<_bfd_final_link>> entry point of the output BFD. This
285 routine is responsible for producing the final output file,
286 which has several aspects. It must relocate the contents of
287 the input sections and copy the data into the output sections.
288 It must build an output symbol table including any local
289 symbols from the input files and the global symbols from the
290 hash table. When producing relocatable output, it must
291 modify the input relocs and write them into the output file.
292 There may also be object format dependent work to be done.
294 The linker will also call the <<write_object_contents>> entry
295 point when the BFD is closed. The two entry points must work
296 together in order to produce the correct output file.
298 The details of how this works are inevitably dependent upon
299 the specific object file format. The a.out
300 <<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
303 @* Information provided by the linker::
304 @* Relocating the section contents::
305 @* Writing the symbol table::
309 Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
311 Information provided by the linker
313 Before the linker calls the <<_bfd_final_link>> entry point,
314 it sets up some data structures for the function to use.
316 The <<input_bfds>> field of the <<bfd_link_info>> structure
317 will point to a list of all the input files included in the
318 link. These files are linked through the <<link_next>> field
319 of the <<bfd>> structure.
321 Each section in the output file will have a list of
322 <<link_order>> structures attached to the <<map_head.link_order>>
323 field (the <<link_order>> structure is defined in
324 <<bfdlink.h>>). These structures describe how to create the
325 contents of the output section in terms of the contents of
326 various input sections, fill constants, and, eventually, other
327 types of information. They also describe relocs that must be
328 created by the BFD backend, but do not correspond to any input
329 file; this is used to support -Ur, which builds constructors
330 while generating a relocatable object file.
333 Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
335 Relocating the section contents
337 The <<_bfd_final_link>> function should look through the
338 <<link_order>> structures attached to each section of the
339 output file. Each <<link_order>> structure should either be
340 handled specially, or it should be passed to the function
341 <<_bfd_default_link_order>> which will do the right thing
342 (<<_bfd_default_link_order>> is defined in <<linker.c>>).
344 For efficiency, a <<link_order>> of type
345 <<bfd_indirect_link_order>> whose associated section belongs
346 to a BFD of the same format as the output BFD must be handled
347 specially. This type of <<link_order>> describes part of an
348 output section in terms of a section belonging to one of the
349 input files. The <<_bfd_final_link>> function should read the
350 contents of the section and any associated relocs, apply the
351 relocs to the section contents, and write out the modified
352 section contents. If performing a relocatable link, the
353 relocs themselves must also be modified and written out.
355 @findex _bfd_relocate_contents
356 @findex _bfd_final_link_relocate
357 The functions <<_bfd_relocate_contents>> and
358 <<_bfd_final_link_relocate>> provide some general support for
359 performing the actual relocations, notably overflow checking.
360 Their arguments include information about the symbol the
361 relocation is against and a <<reloc_howto_type>> argument
362 which describes the relocation to perform. These functions
363 are defined in <<reloc.c>>.
365 The a.out function which handles reading, relocating, and
366 writing section contents is <<aout_link_input_section>>. The
367 actual relocation is done in <<aout_link_input_section_std>>
368 and <<aout_link_input_section_ext>>.
371 Writing the symbol table, , Relocating the section contents, Performing the Final Link
373 Writing the symbol table
375 The <<_bfd_final_link>> function must gather all the symbols
376 in the input files and write them out. It must also write out
377 all the symbols in the global hash table. This must be
378 controlled by the <<strip>> and <<discard>> fields of the
379 <<bfd_link_info>> structure.
381 The local symbols of the input files will not have been
382 entered into the linker hash table. The <<_bfd_final_link>>
383 routine must consider each input file and include the symbols
384 in the output file. It may be convenient to do this when
385 looking through the <<link_order>> structures, or it may be
386 done by stepping through the <<input_bfds>> list.
388 The <<_bfd_final_link>> routine must also traverse the global
389 hash table to gather all the externally visible symbols. It
390 is possible that most of the externally visible symbols may be
391 written out when considering the symbols of each input file,
392 but it is still necessary to traverse the hash table since the
393 linker script may have defined some symbols that are not in
394 any of the input files.
396 The <<strip>> field of the <<bfd_link_info>> structure
397 controls which symbols are written out. The possible values
398 are listed in <<bfdlink.h>>. If the value is <<strip_some>>,
399 then the <<keep_hash>> field of the <<bfd_link_info>>
400 structure is a hash table of symbols to keep; each symbol
401 should be looked up in this hash table, and only symbols which
402 are present should be included in the output file.
404 If the <<strip>> field of the <<bfd_link_info>> structure
405 permits local symbols to be written out, the <<discard>> field
406 is used to further controls which local symbols are included
407 in the output file. If the value is <<discard_l>>, then all
408 local symbols which begin with a certain prefix are discarded;
409 this is controlled by the <<bfd_is_local_label_name>> entry point.
411 The a.out backend handles symbols by calling
412 <<aout_link_write_symbols>> on each input BFD and then
413 traversing the global hash table with the function
414 <<aout_link_write_other_symbol>>. It builds a string table
415 while writing out the symbols, which is written to the output
416 file at the end of <<NAME(aout,final_link)>>.
419 static bfd_boolean generic_link_add_object_symbols
420 (bfd *, struct bfd_link_info *, bfd_boolean collect);
421 static bfd_boolean generic_link_add_symbols
422 (bfd *, struct bfd_link_info *, bfd_boolean);
423 static bfd_boolean generic_link_check_archive_element_no_collect
424 (bfd *, struct bfd_link_info *, bfd_boolean *);
425 static bfd_boolean generic_link_check_archive_element_collect
426 (bfd *, struct bfd_link_info *, bfd_boolean *);
427 static bfd_boolean generic_link_check_archive_element
428 (bfd *, struct bfd_link_info *, bfd_boolean *, bfd_boolean);
429 static bfd_boolean generic_link_add_symbol_list
430 (bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **,
432 static bfd_boolean generic_add_output_symbol
433 (bfd *, size_t *psymalloc, asymbol *);
434 static bfd_boolean default_data_link_order
435 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
436 static bfd_boolean default_indirect_link_order
437 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *,
440 /* The link hash table structure is defined in bfdlink.h. It provides
441 a base hash table which the backend specific hash tables are built
444 /* Routine to create an entry in the link hash table. */
446 struct bfd_hash_entry *
447 _bfd_link_hash_newfunc (struct bfd_hash_entry *entry,
448 struct bfd_hash_table *table,
451 /* Allocate the structure if it has not already been allocated by a
455 entry = (struct bfd_hash_entry *)
456 bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry));
461 /* Call the allocation method of the superclass. */
462 entry = bfd_hash_newfunc (entry, table, string);
465 struct bfd_link_hash_entry *h = (struct bfd_link_hash_entry *) entry;
467 /* Initialize the local fields. */
468 memset ((char *) &h->root + sizeof (h->root), 0,
469 sizeof (*h) - sizeof (h->root));
475 /* Initialize a link hash table. The BFD argument is the one
476 responsible for creating this table. */
479 _bfd_link_hash_table_init
480 (struct bfd_link_hash_table *table,
481 bfd *abfd ATTRIBUTE_UNUSED,
482 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
483 struct bfd_hash_table *,
485 unsigned int entsize)
487 table->undefs = NULL;
488 table->undefs_tail = NULL;
489 table->type = bfd_link_generic_hash_table;
491 return bfd_hash_table_init (&table->table, newfunc, entsize);
494 /* Look up a symbol in a link hash table. If follow is TRUE, we
495 follow bfd_link_hash_indirect and bfd_link_hash_warning links to
498 struct bfd_link_hash_entry *
499 bfd_link_hash_lookup (struct bfd_link_hash_table *table,
505 struct bfd_link_hash_entry *ret;
507 ret = ((struct bfd_link_hash_entry *)
508 bfd_hash_lookup (&table->table, string, create, copy));
510 if (follow && ret != NULL)
512 while (ret->type == bfd_link_hash_indirect
513 || ret->type == bfd_link_hash_warning)
520 /* Look up a symbol in the main linker hash table if the symbol might
521 be wrapped. This should only be used for references to an
522 undefined symbol, not for definitions of a symbol. */
524 struct bfd_link_hash_entry *
525 bfd_wrapped_link_hash_lookup (bfd *abfd,
526 struct bfd_link_info *info,
534 if (info->wrap_hash != NULL)
540 if (*l == bfd_get_symbol_leading_char (abfd) || *l == info->wrap_char)
547 #define WRAP "__wrap_"
549 if (bfd_hash_lookup (info->wrap_hash, l, FALSE, FALSE) != NULL)
552 struct bfd_link_hash_entry *h;
554 /* This symbol is being wrapped. We want to replace all
555 references to SYM with references to __wrap_SYM. */
557 amt = strlen (l) + sizeof WRAP + 1;
558 n = (char *) bfd_malloc (amt);
566 h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
574 #define REAL "__real_"
577 && CONST_STRNEQ (l, REAL)
578 && bfd_hash_lookup (info->wrap_hash, l + sizeof REAL - 1,
579 FALSE, FALSE) != NULL)
582 struct bfd_link_hash_entry *h;
584 /* This is a reference to __real_SYM, where SYM is being
585 wrapped. We want to replace all references to __real_SYM
586 with references to SYM. */
588 amt = strlen (l + sizeof REAL - 1) + 2;
589 n = (char *) bfd_malloc (amt);
595 strcat (n, l + sizeof REAL - 1);
596 h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
604 return bfd_link_hash_lookup (info->hash, string, create, copy, follow);
607 /* Traverse a generic link hash table. Differs from bfd_hash_traverse
608 in the treatment of warning symbols. When warning symbols are
609 created they replace the real symbol, so you don't get to see the
610 real symbol in a bfd_hash_travere. This traversal calls func with
614 bfd_link_hash_traverse
615 (struct bfd_link_hash_table *htab,
616 bfd_boolean (*func) (struct bfd_link_hash_entry *, void *),
621 htab->table.frozen = 1;
622 for (i = 0; i < htab->table.size; i++)
624 struct bfd_link_hash_entry *p;
626 p = (struct bfd_link_hash_entry *) htab->table.table[i];
627 for (; p != NULL; p = (struct bfd_link_hash_entry *) p->root.next)
628 if (!(*func) (p->type == bfd_link_hash_warning ? p->u.i.link : p, info))
632 htab->table.frozen = 0;
635 /* Add a symbol to the linker hash table undefs list. */
638 bfd_link_add_undef (struct bfd_link_hash_table *table,
639 struct bfd_link_hash_entry *h)
641 BFD_ASSERT (h->u.undef.next == NULL);
642 if (table->undefs_tail != NULL)
643 table->undefs_tail->u.undef.next = h;
644 if (table->undefs == NULL)
646 table->undefs_tail = h;
649 /* The undefs list was designed so that in normal use we don't need to
650 remove entries. However, if symbols on the list are changed from
651 bfd_link_hash_undefined to either bfd_link_hash_undefweak or
652 bfd_link_hash_new for some reason, then they must be removed from the
653 list. Failure to do so might result in the linker attempting to add
654 the symbol to the list again at a later stage. */
657 bfd_link_repair_undef_list (struct bfd_link_hash_table *table)
659 struct bfd_link_hash_entry **pun;
661 pun = &table->undefs;
664 struct bfd_link_hash_entry *h = *pun;
666 if (h->type == bfd_link_hash_new
667 || h->type == bfd_link_hash_undefweak)
669 *pun = h->u.undef.next;
670 h->u.undef.next = NULL;
671 if (h == table->undefs_tail)
673 if (pun == &table->undefs)
674 table->undefs_tail = NULL;
676 /* pun points at an u.undef.next field. Go back to
677 the start of the link_hash_entry. */
678 table->undefs_tail = (struct bfd_link_hash_entry *)
679 ((char *) pun - ((char *) &h->u.undef.next - (char *) h));
684 pun = &h->u.undef.next;
688 /* Routine to create an entry in a generic link hash table. */
690 struct bfd_hash_entry *
691 _bfd_generic_link_hash_newfunc (struct bfd_hash_entry *entry,
692 struct bfd_hash_table *table,
695 /* Allocate the structure if it has not already been allocated by a
699 entry = (struct bfd_hash_entry *)
700 bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry));
705 /* Call the allocation method of the superclass. */
706 entry = _bfd_link_hash_newfunc (entry, table, string);
709 struct generic_link_hash_entry *ret;
711 /* Set local fields. */
712 ret = (struct generic_link_hash_entry *) entry;
713 ret->written = FALSE;
720 /* Create a generic link hash table. */
722 struct bfd_link_hash_table *
723 _bfd_generic_link_hash_table_create (bfd *abfd)
725 struct generic_link_hash_table *ret;
726 bfd_size_type amt = sizeof (struct generic_link_hash_table);
728 ret = (struct generic_link_hash_table *) bfd_malloc (amt);
731 if (! _bfd_link_hash_table_init (&ret->root, abfd,
732 _bfd_generic_link_hash_newfunc,
733 sizeof (struct generic_link_hash_entry)))
742 _bfd_generic_link_hash_table_free (struct bfd_link_hash_table *hash)
744 struct generic_link_hash_table *ret
745 = (struct generic_link_hash_table *) hash;
747 bfd_hash_table_free (&ret->root.table);
751 /* Grab the symbols for an object file when doing a generic link. We
752 store the symbols in the outsymbols field. We need to keep them
753 around for the entire link to ensure that we only read them once.
754 If we read them multiple times, we might wind up with relocs and
755 the hash table pointing to different instances of the symbol
759 bfd_generic_link_read_symbols (bfd *abfd)
761 if (bfd_get_outsymbols (abfd) == NULL)
766 symsize = bfd_get_symtab_upper_bound (abfd);
769 bfd_get_outsymbols (abfd) = (struct bfd_symbol **) bfd_alloc (abfd,
771 if (bfd_get_outsymbols (abfd) == NULL && symsize != 0)
773 symcount = bfd_canonicalize_symtab (abfd, bfd_get_outsymbols (abfd));
776 bfd_get_symcount (abfd) = symcount;
782 /* Generic function to add symbols to from an object file to the
783 global hash table. This version does not automatically collect
784 constructors by name. */
787 _bfd_generic_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
789 return generic_link_add_symbols (abfd, info, FALSE);
792 /* Generic function to add symbols from an object file to the global
793 hash table. This version automatically collects constructors by
794 name, as the collect2 program does. It should be used for any
795 target which does not provide some other mechanism for setting up
796 constructors and destructors; these are approximately those targets
797 for which gcc uses collect2 and do not support stabs. */
800 _bfd_generic_link_add_symbols_collect (bfd *abfd, struct bfd_link_info *info)
802 return generic_link_add_symbols (abfd, info, TRUE);
805 /* Indicate that we are only retrieving symbol values from this
806 section. We want the symbols to act as though the values in the
807 file are absolute. */
810 _bfd_generic_link_just_syms (asection *sec,
811 struct bfd_link_info *info ATTRIBUTE_UNUSED)
813 sec->sec_info_type = SEC_INFO_TYPE_JUST_SYMS;
814 sec->output_section = bfd_abs_section_ptr;
815 sec->output_offset = sec->vma;
818 /* Copy the type of a symbol assiciated with a linker hast table entry.
819 Override this so that symbols created in linker scripts get their
820 type from the RHS of the assignment.
821 The default implementation does nothing. */
823 _bfd_generic_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
824 struct bfd_link_hash_entry * hdest ATTRIBUTE_UNUSED,
825 struct bfd_link_hash_entry * hsrc ATTRIBUTE_UNUSED)
829 /* Add symbols from an object file to the global hash table. */
832 generic_link_add_symbols (bfd *abfd,
833 struct bfd_link_info *info,
838 switch (bfd_get_format (abfd))
841 ret = generic_link_add_object_symbols (abfd, info, collect);
844 ret = (_bfd_generic_link_add_archive_symbols
847 ? generic_link_check_archive_element_collect
848 : generic_link_check_archive_element_no_collect)));
851 bfd_set_error (bfd_error_wrong_format);
858 /* Add symbols from an object file to the global hash table. */
861 generic_link_add_object_symbols (bfd *abfd,
862 struct bfd_link_info *info,
865 bfd_size_type symcount;
866 struct bfd_symbol **outsyms;
868 if (!bfd_generic_link_read_symbols (abfd))
870 symcount = _bfd_generic_link_get_symcount (abfd);
871 outsyms = _bfd_generic_link_get_symbols (abfd);
872 return generic_link_add_symbol_list (abfd, info, symcount, outsyms, collect);
875 /* We build a hash table of all symbols defined in an archive. */
877 /* An archive symbol may be defined by multiple archive elements.
878 This linked list is used to hold the elements. */
882 struct archive_list *next;
886 /* An entry in an archive hash table. */
888 struct archive_hash_entry
890 struct bfd_hash_entry root;
891 /* Where the symbol is defined. */
892 struct archive_list *defs;
895 /* An archive hash table itself. */
897 struct archive_hash_table
899 struct bfd_hash_table table;
902 /* Create a new entry for an archive hash table. */
904 static struct bfd_hash_entry *
905 archive_hash_newfunc (struct bfd_hash_entry *entry,
906 struct bfd_hash_table *table,
909 struct archive_hash_entry *ret = (struct archive_hash_entry *) entry;
911 /* Allocate the structure if it has not already been allocated by a
914 ret = (struct archive_hash_entry *)
915 bfd_hash_allocate (table, sizeof (struct archive_hash_entry));
919 /* Call the allocation method of the superclass. */
920 ret = ((struct archive_hash_entry *)
921 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
925 /* Initialize the local fields. */
932 /* Initialize an archive hash table. */
935 archive_hash_table_init
936 (struct archive_hash_table *table,
937 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
938 struct bfd_hash_table *,
940 unsigned int entsize)
942 return bfd_hash_table_init (&table->table, newfunc, entsize);
945 /* Look up an entry in an archive hash table. */
947 #define archive_hash_lookup(t, string, create, copy) \
948 ((struct archive_hash_entry *) \
949 bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
951 /* Allocate space in an archive hash table. */
953 #define archive_hash_allocate(t, size) bfd_hash_allocate (&(t)->table, (size))
955 /* Free an archive hash table. */
957 #define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table)
959 /* Generic function to add symbols from an archive file to the global
960 hash file. This function presumes that the archive symbol table
961 has already been read in (this is normally done by the
962 bfd_check_format entry point). It looks through the undefined and
963 common symbols and searches the archive symbol table for them. If
964 it finds an entry, it includes the associated object file in the
967 The old linker looked through the archive symbol table for
968 undefined symbols. We do it the other way around, looking through
969 undefined symbols for symbols defined in the archive. The
970 advantage of the newer scheme is that we only have to look through
971 the list of undefined symbols once, whereas the old method had to
972 re-search the symbol table each time a new object file was added.
974 The CHECKFN argument is used to see if an object file should be
975 included. CHECKFN should set *PNEEDED to TRUE if the object file
976 should be included, and must also call the bfd_link_info
977 add_archive_element callback function and handle adding the symbols
978 to the global hash table. CHECKFN must notice if the callback
979 indicates a substitute BFD, and arrange to add those symbols instead
980 if it does so. CHECKFN should only return FALSE if some sort of
983 For some formats, such as a.out, it is possible to look through an
984 object file but not actually include it in the link. The
985 archive_pass field in a BFD is used to avoid checking the symbols
986 of an object files too many times. When an object is included in
987 the link, archive_pass is set to -1. If an object is scanned but
988 not included, archive_pass is set to the pass number. The pass
989 number is incremented each time a new object file is included. The
990 pass number is used because when a new object file is included it
991 may create new undefined symbols which cause a previously examined
992 object file to be included. */
995 _bfd_generic_link_add_archive_symbols
997 struct bfd_link_info *info,
998 bfd_boolean (*checkfn) (bfd *, struct bfd_link_info *, bfd_boolean *))
1002 register carsym *arsym;
1004 struct archive_hash_table arsym_hash;
1006 struct bfd_link_hash_entry **pundef;
1008 if (! bfd_has_map (abfd))
1010 /* An empty archive is a special case. */
1011 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
1013 bfd_set_error (bfd_error_no_armap);
1017 arsyms = bfd_ardata (abfd)->symdefs;
1018 arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
1020 /* In order to quickly determine whether an symbol is defined in
1021 this archive, we build a hash table of the symbols. */
1022 if (! archive_hash_table_init (&arsym_hash, archive_hash_newfunc,
1023 sizeof (struct archive_hash_entry)))
1025 for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
1027 struct archive_hash_entry *arh;
1028 struct archive_list *l, **pp;
1030 arh = archive_hash_lookup (&arsym_hash, arsym->name, TRUE, FALSE);
1033 l = ((struct archive_list *)
1034 archive_hash_allocate (&arsym_hash, sizeof (struct archive_list)));
1038 for (pp = &arh->defs; *pp != NULL; pp = &(*pp)->next)
1044 /* The archive_pass field in the archive itself is used to
1045 initialize PASS, sine we may search the same archive multiple
1047 pass = abfd->archive_pass + 1;
1049 /* New undefined symbols are added to the end of the list, so we
1050 only need to look through it once. */
1051 pundef = &info->hash->undefs;
1052 while (*pundef != NULL)
1054 struct bfd_link_hash_entry *h;
1055 struct archive_hash_entry *arh;
1056 struct archive_list *l;
1060 /* When a symbol is defined, it is not necessarily removed from
1062 if (h->type != bfd_link_hash_undefined
1063 && h->type != bfd_link_hash_common)
1065 /* Remove this entry from the list, for general cleanliness
1066 and because we are going to look through the list again
1067 if we search any more libraries. We can't remove the
1068 entry if it is the tail, because that would lose any
1069 entries we add to the list later on (it would also cause
1070 us to lose track of whether the symbol has been
1072 if (*pundef != info->hash->undefs_tail)
1073 *pundef = (*pundef)->u.undef.next;
1075 pundef = &(*pundef)->u.undef.next;
1079 /* Look for this symbol in the archive symbol map. */
1080 arh = archive_hash_lookup (&arsym_hash, h->root.string, FALSE, FALSE);
1083 /* If we haven't found the exact symbol we're looking for,
1084 let's look for its import thunk */
1085 if (info->pei386_auto_import)
1087 bfd_size_type amt = strlen (h->root.string) + 10;
1088 char *buf = (char *) bfd_malloc (amt);
1092 sprintf (buf, "__imp_%s", h->root.string);
1093 arh = archive_hash_lookup (&arsym_hash, buf, FALSE, FALSE);
1098 pundef = &(*pundef)->u.undef.next;
1102 /* Look at all the objects which define this symbol. */
1103 for (l = arh->defs; l != NULL; l = l->next)
1108 /* If the symbol has gotten defined along the way, quit. */
1109 if (h->type != bfd_link_hash_undefined
1110 && h->type != bfd_link_hash_common)
1113 element = bfd_get_elt_at_index (abfd, l->indx);
1114 if (element == NULL)
1117 /* If we've already included this element, or if we've
1118 already checked it on this pass, continue. */
1119 if (element->archive_pass == -1
1120 || element->archive_pass == pass)
1123 /* If we can't figure this element out, just ignore it. */
1124 if (! bfd_check_format (element, bfd_object))
1126 element->archive_pass = -1;
1130 /* CHECKFN will see if this element should be included, and
1131 go ahead and include it if appropriate. */
1132 if (! (*checkfn) (element, info, &needed))
1136 element->archive_pass = pass;
1139 element->archive_pass = -1;
1141 /* Increment the pass count to show that we may need to
1142 recheck object files which were already checked. */
1147 pundef = &(*pundef)->u.undef.next;
1150 archive_hash_table_free (&arsym_hash);
1152 /* Save PASS in case we are called again. */
1153 abfd->archive_pass = pass;
1158 archive_hash_table_free (&arsym_hash);
1162 /* See if we should include an archive element. This version is used
1163 when we do not want to automatically collect constructors based on
1164 the symbol name, presumably because we have some other mechanism
1165 for finding them. */
1168 generic_link_check_archive_element_no_collect (
1170 struct bfd_link_info *info,
1171 bfd_boolean *pneeded)
1173 return generic_link_check_archive_element (abfd, info, pneeded, FALSE);
1176 /* See if we should include an archive element. This version is used
1177 when we want to automatically collect constructors based on the
1178 symbol name, as collect2 does. */
1181 generic_link_check_archive_element_collect (bfd *abfd,
1182 struct bfd_link_info *info,
1183 bfd_boolean *pneeded)
1185 return generic_link_check_archive_element (abfd, info, pneeded, TRUE);
1188 /* See if we should include an archive element. Optionally collect
1192 generic_link_check_archive_element (bfd *abfd,
1193 struct bfd_link_info *info,
1194 bfd_boolean *pneeded,
1195 bfd_boolean collect)
1197 asymbol **pp, **ppend;
1201 if (!bfd_generic_link_read_symbols (abfd))
1204 pp = _bfd_generic_link_get_symbols (abfd);
1205 ppend = pp + _bfd_generic_link_get_symcount (abfd);
1206 for (; pp < ppend; pp++)
1209 struct bfd_link_hash_entry *h;
1213 /* We are only interested in globally visible symbols. */
1214 if (! bfd_is_com_section (p->section)
1215 && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0)
1218 /* We are only interested if we know something about this
1219 symbol, and it is undefined or common. An undefined weak
1220 symbol (type bfd_link_hash_undefweak) is not considered to be
1221 a reference when pulling files out of an archive. See the
1222 SVR4 ABI, p. 4-27. */
1223 h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), FALSE,
1226 || (h->type != bfd_link_hash_undefined
1227 && h->type != bfd_link_hash_common))
1230 /* P is a symbol we are looking for. */
1232 if (! bfd_is_com_section (p->section))
1234 bfd_size_type symcount;
1238 /* This object file defines this symbol, so pull it in. */
1239 if (!(*info->callbacks
1240 ->add_archive_element) (info, abfd, bfd_asymbol_name (p),
1243 /* Potentially, the add_archive_element hook may have set a
1244 substitute BFD for us. */
1246 && !bfd_generic_link_read_symbols (abfd))
1248 symcount = _bfd_generic_link_get_symcount (abfd);
1249 symbols = _bfd_generic_link_get_symbols (abfd);
1250 if (! generic_link_add_symbol_list (abfd, info, symcount,
1257 /* P is a common symbol. */
1259 if (h->type == bfd_link_hash_undefined)
1265 symbfd = h->u.undef.abfd;
1268 /* This symbol was created as undefined from outside
1269 BFD. We assume that we should link in the object
1270 file. This is for the -u option in the linker. */
1271 if (!(*info->callbacks
1272 ->add_archive_element) (info, abfd, bfd_asymbol_name (p),
1275 /* Potentially, the add_archive_element hook may have set a
1276 substitute BFD for us. But no symbols are going to get
1277 registered by anything we're returning to from here. */
1282 /* Turn the symbol into a common symbol but do not link in
1283 the object file. This is how a.out works. Object
1284 formats that require different semantics must implement
1285 this function differently. This symbol is already on the
1286 undefs list. We add the section to a common section
1287 attached to symbfd to ensure that it is in a BFD which
1288 will be linked in. */
1289 h->type = bfd_link_hash_common;
1290 h->u.c.p = (struct bfd_link_hash_common_entry *)
1291 bfd_hash_allocate (&info->hash->table,
1292 sizeof (struct bfd_link_hash_common_entry));
1293 if (h->u.c.p == NULL)
1296 size = bfd_asymbol_value (p);
1299 power = bfd_log2 (size);
1302 h->u.c.p->alignment_power = power;
1304 if (p->section == bfd_com_section_ptr)
1305 h->u.c.p->section = bfd_make_section_old_way (symbfd, "COMMON");
1307 h->u.c.p->section = bfd_make_section_old_way (symbfd,
1309 h->u.c.p->section->flags |= SEC_ALLOC;
1313 /* Adjust the size of the common symbol if necessary. This
1314 is how a.out works. Object formats that require
1315 different semantics must implement this function
1317 if (bfd_asymbol_value (p) > h->u.c.size)
1318 h->u.c.size = bfd_asymbol_value (p);
1322 /* This archive element is not needed. */
1326 /* Add the symbols from an object file to the global hash table. ABFD
1327 is the object file. INFO is the linker information. SYMBOL_COUNT
1328 is the number of symbols. SYMBOLS is the list of symbols. COLLECT
1329 is TRUE if constructors should be automatically collected by name
1330 as is done by collect2. */
1333 generic_link_add_symbol_list (bfd *abfd,
1334 struct bfd_link_info *info,
1335 bfd_size_type symbol_count,
1337 bfd_boolean collect)
1339 asymbol **pp, **ppend;
1342 ppend = symbols + symbol_count;
1343 for (; pp < ppend; pp++)
1349 if ((p->flags & (BSF_INDIRECT
1354 || bfd_is_und_section (bfd_get_section (p))
1355 || bfd_is_com_section (bfd_get_section (p))
1356 || bfd_is_ind_section (bfd_get_section (p)))
1360 struct generic_link_hash_entry *h;
1361 struct bfd_link_hash_entry *bh;
1363 string = name = bfd_asymbol_name (p);
1364 if (((p->flags & BSF_INDIRECT) != 0
1365 || bfd_is_ind_section (p->section))
1369 string = bfd_asymbol_name (*pp);
1371 else if ((p->flags & BSF_WARNING) != 0
1374 /* The name of P is actually the warning string, and the
1375 next symbol is the one to warn about. */
1377 name = bfd_asymbol_name (*pp);
1381 if (! (_bfd_generic_link_add_one_symbol
1382 (info, abfd, name, p->flags, bfd_get_section (p),
1383 p->value, string, FALSE, collect, &bh)))
1385 h = (struct generic_link_hash_entry *) bh;
1387 /* If this is a constructor symbol, and the linker didn't do
1388 anything with it, then we want to just pass the symbol
1389 through to the output file. This will happen when
1391 if ((p->flags & BSF_CONSTRUCTOR) != 0
1392 && (h == NULL || h->root.type == bfd_link_hash_new))
1398 /* Save the BFD symbol so that we don't lose any backend
1399 specific information that may be attached to it. We only
1400 want this one if it gives more information than the
1401 existing one; we don't want to replace a defined symbol
1402 with an undefined one. This routine may be called with a
1403 hash table other than the generic hash table, so we only
1404 do this if we are certain that the hash table is a
1406 if (info->output_bfd->xvec == abfd->xvec)
1409 || (! bfd_is_und_section (bfd_get_section (p))
1410 && (! bfd_is_com_section (bfd_get_section (p))
1411 || bfd_is_und_section (bfd_get_section (h->sym)))))
1414 /* BSF_OLD_COMMON is a hack to support COFF reloc
1415 reading, and it should go away when the COFF
1416 linker is switched to the new version. */
1417 if (bfd_is_com_section (bfd_get_section (p)))
1418 p->flags |= BSF_OLD_COMMON;
1422 /* Store a back pointer from the symbol to the hash
1423 table entry for the benefit of relaxation code until
1424 it gets rewritten to not use asymbol structures.
1425 Setting this is also used to check whether these
1426 symbols were set up by the generic linker. */
1434 /* We use a state table to deal with adding symbols from an object
1435 file. The first index into the state table describes the symbol
1436 from the object file. The second index into the state table is the
1437 type of the symbol in the hash table. */
1439 /* The symbol from the object file is turned into one of these row
1444 UNDEF_ROW, /* Undefined. */
1445 UNDEFW_ROW, /* Weak undefined. */
1446 DEF_ROW, /* Defined. */
1447 DEFW_ROW, /* Weak defined. */
1448 COMMON_ROW, /* Common. */
1449 INDR_ROW, /* Indirect. */
1450 WARN_ROW, /* Warning. */
1451 SET_ROW /* Member of set. */
1454 /* apparently needed for Hitachi 3050R(HI-UX/WE2)? */
1457 /* The actions to take in the state table. */
1462 UND, /* Mark symbol undefined. */
1463 WEAK, /* Mark symbol weak undefined. */
1464 DEF, /* Mark symbol defined. */
1465 DEFW, /* Mark symbol weak defined. */
1466 COM, /* Mark symbol common. */
1467 REF, /* Mark defined symbol referenced. */
1468 CREF, /* Possibly warn about common reference to defined symbol. */
1469 CDEF, /* Define existing common symbol. */
1470 NOACT, /* No action. */
1471 BIG, /* Mark symbol common using largest size. */
1472 MDEF, /* Multiple definition error. */
1473 MIND, /* Multiple indirect symbols. */
1474 IND, /* Make indirect symbol. */
1475 CIND, /* Make indirect symbol from existing common symbol. */
1476 SET, /* Add value to set. */
1477 MWARN, /* Make warning symbol. */
1478 WARN, /* Issue warning. */
1479 CWARN, /* Warn if referenced, else MWARN. */
1480 CYCLE, /* Repeat with symbol pointed to. */
1481 REFC, /* Mark indirect symbol referenced and then CYCLE. */
1482 WARNC /* Issue warning and then CYCLE. */
1485 /* The state table itself. The first index is a link_row and the
1486 second index is a bfd_link_hash_type. */
1488 static const enum link_action link_action[8][8] =
1490 /* current\prev new undef undefw def defw com indr warn */
1491 /* UNDEF_ROW */ {UND, NOACT, UND, REF, REF, NOACT, REFC, WARNC },
1492 /* UNDEFW_ROW */ {WEAK, NOACT, NOACT, REF, REF, NOACT, REFC, WARNC },
1493 /* DEF_ROW */ {DEF, DEF, DEF, MDEF, DEF, CDEF, MDEF, CYCLE },
1494 /* DEFW_ROW */ {DEFW, DEFW, DEFW, NOACT, NOACT, NOACT, NOACT, CYCLE },
1495 /* COMMON_ROW */ {COM, COM, COM, CREF, COM, BIG, REFC, WARNC },
1496 /* INDR_ROW */ {IND, IND, IND, MDEF, IND, CIND, MIND, CYCLE },
1497 /* WARN_ROW */ {MWARN, WARN, WARN, CWARN, CWARN, WARN, CWARN, NOACT },
1498 /* SET_ROW */ {SET, SET, SET, SET, SET, SET, CYCLE, CYCLE }
1501 /* Most of the entries in the LINK_ACTION table are straightforward,
1502 but a few are somewhat subtle.
1504 A reference to an indirect symbol (UNDEF_ROW/indr or
1505 UNDEFW_ROW/indr) is counted as a reference both to the indirect
1506 symbol and to the symbol the indirect symbol points to.
1508 A reference to a warning symbol (UNDEF_ROW/warn or UNDEFW_ROW/warn)
1509 causes the warning to be issued.
1511 A common definition of an indirect symbol (COMMON_ROW/indr) is
1512 treated as a multiple definition error. Likewise for an indirect
1513 definition of a common symbol (INDR_ROW/com).
1515 An indirect definition of a warning (INDR_ROW/warn) does not cause
1516 the warning to be issued.
1518 If a warning is created for an indirect symbol (WARN_ROW/indr) no
1519 warning is created for the symbol the indirect symbol points to.
1521 Adding an entry to a set does not count as a reference to a set,
1522 and no warning is issued (SET_ROW/warn). */
1524 /* Return the BFD in which a hash entry has been defined, if known. */
1527 hash_entry_bfd (struct bfd_link_hash_entry *h)
1529 while (h->type == bfd_link_hash_warning)
1535 case bfd_link_hash_undefined:
1536 case bfd_link_hash_undefweak:
1537 return h->u.undef.abfd;
1538 case bfd_link_hash_defined:
1539 case bfd_link_hash_defweak:
1540 return h->u.def.section->owner;
1541 case bfd_link_hash_common:
1542 return h->u.c.p->section->owner;
1547 /* Add a symbol to the global hash table.
1548 ABFD is the BFD the symbol comes from.
1549 NAME is the name of the symbol.
1550 FLAGS is the BSF_* bits associated with the symbol.
1551 SECTION is the section in which the symbol is defined; this may be
1552 bfd_und_section_ptr or bfd_com_section_ptr.
1553 VALUE is the value of the symbol, relative to the section.
1554 STRING is used for either an indirect symbol, in which case it is
1555 the name of the symbol to indirect to, or a warning symbol, in
1556 which case it is the warning string.
1557 COPY is TRUE if NAME or STRING must be copied into locally
1558 allocated memory if they need to be saved.
1559 COLLECT is TRUE if we should automatically collect gcc constructor
1560 or destructor names as collect2 does.
1561 HASHP, if not NULL, is a place to store the created hash table
1562 entry; if *HASHP is not NULL, the caller has already looked up
1563 the hash table entry, and stored it in *HASHP. */
1566 _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
1574 bfd_boolean collect,
1575 struct bfd_link_hash_entry **hashp)
1578 struct bfd_link_hash_entry *h;
1581 BFD_ASSERT (section != NULL);
1583 if (bfd_is_ind_section (section)
1584 || (flags & BSF_INDIRECT) != 0)
1586 else if ((flags & BSF_WARNING) != 0)
1588 else if ((flags & BSF_CONSTRUCTOR) != 0)
1590 else if (bfd_is_und_section (section))
1592 if ((flags & BSF_WEAK) != 0)
1597 else if ((flags & BSF_WEAK) != 0)
1599 else if (bfd_is_com_section (section))
1604 if (hashp != NULL && *hashp != NULL)
1608 if (row == UNDEF_ROW || row == UNDEFW_ROW)
1609 h = bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE);
1611 h = bfd_link_hash_lookup (info->hash, name, TRUE, copy, FALSE);
1620 if (info->notice_all
1621 || (info->notice_hash != NULL
1622 && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
1624 if (! (*info->callbacks->notice) (info, h,
1625 abfd, section, value, flags, string))
1634 enum link_action action;
1637 action = link_action[(int) row][(int) h->type];
1648 /* Make a new undefined symbol. */
1649 h->type = bfd_link_hash_undefined;
1650 h->u.undef.abfd = abfd;
1651 bfd_link_add_undef (info->hash, h);
1655 /* Make a new weak undefined symbol. */
1656 h->type = bfd_link_hash_undefweak;
1657 h->u.undef.abfd = abfd;
1661 /* We have found a definition for a symbol which was
1662 previously common. */
1663 BFD_ASSERT (h->type == bfd_link_hash_common);
1664 if (! ((*info->callbacks->multiple_common)
1665 (info, h, abfd, bfd_link_hash_defined, 0)))
1671 enum bfd_link_hash_type oldtype;
1673 /* Define a symbol. */
1676 h->type = bfd_link_hash_defweak;
1678 h->type = bfd_link_hash_defined;
1679 h->u.def.section = section;
1680 h->u.def.value = value;
1682 /* If we have been asked to, we act like collect2 and
1683 identify all functions that might be global
1684 constructors and destructors and pass them up in a
1685 callback. We only do this for certain object file
1686 types, since many object file types can handle this
1688 if (collect && name[0] == '_')
1692 /* A constructor or destructor name starts like this:
1693 _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
1694 the second are the same character (we accept any
1695 character there, in case a new object file format
1696 comes along with even worse naming restrictions). */
1698 #define CONS_PREFIX "GLOBAL_"
1699 #define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
1704 if (s[0] == 'G' && CONST_STRNEQ (s, CONS_PREFIX))
1708 c = s[CONS_PREFIX_LEN + 1];
1709 if ((c == 'I' || c == 'D')
1710 && s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
1712 /* If this is a definition of a symbol which
1713 was previously weakly defined, we are in
1714 trouble. We have already added a
1715 constructor entry for the weak defined
1716 symbol, and now we are trying to add one
1717 for the new symbol. Fortunately, this case
1718 should never arise in practice. */
1719 if (oldtype == bfd_link_hash_defweak)
1722 if (! ((*info->callbacks->constructor)
1724 h->root.string, abfd, section, value)))
1734 /* We have found a common definition for a symbol. */
1735 if (h->type == bfd_link_hash_new)
1736 bfd_link_add_undef (info->hash, h);
1737 h->type = bfd_link_hash_common;
1738 h->u.c.p = (struct bfd_link_hash_common_entry *)
1739 bfd_hash_allocate (&info->hash->table,
1740 sizeof (struct bfd_link_hash_common_entry));
1741 if (h->u.c.p == NULL)
1744 h->u.c.size = value;
1746 /* Select a default alignment based on the size. This may
1747 be overridden by the caller. */
1751 power = bfd_log2 (value);
1754 h->u.c.p->alignment_power = power;
1757 /* The section of a common symbol is only used if the common
1758 symbol is actually allocated. It basically provides a
1759 hook for the linker script to decide which output section
1760 the common symbols should be put in. In most cases, the
1761 section of a common symbol will be bfd_com_section_ptr,
1762 the code here will choose a common symbol section named
1763 "COMMON", and the linker script will contain *(COMMON) in
1764 the appropriate place. A few targets use separate common
1765 sections for small symbols, and they require special
1767 if (section == bfd_com_section_ptr)
1769 h->u.c.p->section = bfd_make_section_old_way (abfd, "COMMON");
1770 h->u.c.p->section->flags |= SEC_ALLOC;
1772 else if (section->owner != abfd)
1774 h->u.c.p->section = bfd_make_section_old_way (abfd,
1776 h->u.c.p->section->flags |= SEC_ALLOC;
1779 h->u.c.p->section = section;
1783 /* A reference to a defined symbol. */
1784 if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
1785 h->u.undef.next = h;
1789 /* We have found a common definition for a symbol which
1790 already had a common definition. Use the maximum of the
1791 two sizes, and use the section required by the larger symbol. */
1792 BFD_ASSERT (h->type == bfd_link_hash_common);
1793 if (! ((*info->callbacks->multiple_common)
1794 (info, h, abfd, bfd_link_hash_common, value)))
1796 if (value > h->u.c.size)
1800 h->u.c.size = value;
1802 /* Select a default alignment based on the size. This may
1803 be overridden by the caller. */
1804 power = bfd_log2 (value);
1807 h->u.c.p->alignment_power = power;
1809 /* Some systems have special treatment for small commons,
1810 hence we want to select the section used by the larger
1811 symbol. This makes sure the symbol does not go in a
1812 small common section if it is now too large. */
1813 if (section == bfd_com_section_ptr)
1816 = bfd_make_section_old_way (abfd, "COMMON");
1817 h->u.c.p->section->flags |= SEC_ALLOC;
1819 else if (section->owner != abfd)
1822 = bfd_make_section_old_way (abfd, section->name);
1823 h->u.c.p->section->flags |= SEC_ALLOC;
1826 h->u.c.p->section = section;
1831 /* We have found a common definition for a symbol which
1832 was already defined. */
1833 if (! ((*info->callbacks->multiple_common)
1834 (info, h, abfd, bfd_link_hash_common, value)))
1839 /* Multiple indirect symbols. This is OK if they both point
1840 to the same symbol. */
1841 if (strcmp (h->u.i.link->root.string, string) == 0)
1845 /* Handle a multiple definition. */
1846 if (! ((*info->callbacks->multiple_definition)
1847 (info, h, abfd, section, value)))
1852 /* Create an indirect symbol from an existing common symbol. */
1853 BFD_ASSERT (h->type == bfd_link_hash_common);
1854 if (! ((*info->callbacks->multiple_common)
1855 (info, h, abfd, bfd_link_hash_indirect, 0)))
1859 /* Create an indirect symbol. */
1861 struct bfd_link_hash_entry *inh;
1863 /* STRING is the name of the symbol we want to indirect
1865 inh = bfd_wrapped_link_hash_lookup (abfd, info, string, TRUE,
1869 if (inh->type == bfd_link_hash_indirect
1870 && inh->u.i.link == h)
1872 (*_bfd_error_handler)
1873 (_("%B: indirect symbol `%s' to `%s' is a loop"),
1874 abfd, name, string);
1875 bfd_set_error (bfd_error_invalid_operation);
1878 if (inh->type == bfd_link_hash_new)
1880 inh->type = bfd_link_hash_undefined;
1881 inh->u.undef.abfd = abfd;
1882 bfd_link_add_undef (info->hash, inh);
1885 /* If the indirect symbol has been referenced, we need to
1886 push the reference down to the symbol we are
1888 if (h->type != bfd_link_hash_new)
1894 h->type = bfd_link_hash_indirect;
1900 /* Add an entry to a set. */
1901 if (! (*info->callbacks->add_to_set) (info, h, BFD_RELOC_CTOR,
1902 abfd, section, value))
1907 /* Issue a warning and cycle. */
1908 if (h->u.i.warning != NULL)
1910 if (! (*info->callbacks->warning) (info, h->u.i.warning,
1911 h->root.string, abfd,
1914 /* Only issue a warning once. */
1915 h->u.i.warning = NULL;
1919 /* Try again with the referenced symbol. */
1925 /* A reference to an indirect symbol. */
1926 if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
1927 h->u.undef.next = h;
1933 /* Issue a warning. */
1934 if (! (*info->callbacks->warning) (info, string, h->root.string,
1935 hash_entry_bfd (h), NULL, 0))
1940 /* Warn if this symbol has been referenced already,
1941 otherwise add a warning. A symbol has been referenced if
1942 the u.undef.next field is not NULL, or it is the tail of the
1943 undefined symbol list. The REF case above helps to
1945 if (h->u.undef.next != NULL || info->hash->undefs_tail == h)
1947 if (! (*info->callbacks->warning) (info, string, h->root.string,
1948 hash_entry_bfd (h), NULL, 0))
1954 /* Make a warning symbol. */
1956 struct bfd_link_hash_entry *sub;
1958 /* STRING is the warning to give. */
1959 sub = ((struct bfd_link_hash_entry *)
1960 ((*info->hash->table.newfunc)
1961 (NULL, &info->hash->table, h->root.string)));
1965 sub->type = bfd_link_hash_warning;
1968 sub->u.i.warning = string;
1972 size_t len = strlen (string) + 1;
1974 w = (char *) bfd_hash_allocate (&info->hash->table, len);
1977 memcpy (w, string, len);
1978 sub->u.i.warning = w;
1981 bfd_hash_replace (&info->hash->table,
1982 (struct bfd_hash_entry *) h,
1983 (struct bfd_hash_entry *) sub);
1995 /* Generic final link routine. */
1998 _bfd_generic_final_link (bfd *abfd, struct bfd_link_info *info)
2002 struct bfd_link_order *p;
2004 struct generic_write_global_symbol_info wginfo;
2006 bfd_get_outsymbols (abfd) = NULL;
2007 bfd_get_symcount (abfd) = 0;
2010 /* Mark all sections which will be included in the output file. */
2011 for (o = abfd->sections; o != NULL; o = o->next)
2012 for (p = o->map_head.link_order; p != NULL; p = p->next)
2013 if (p->type == bfd_indirect_link_order)
2014 p->u.indirect.section->linker_mark = TRUE;
2016 /* Build the output symbol table. */
2017 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2018 if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
2021 /* Accumulate the global symbols. */
2023 wginfo.output_bfd = abfd;
2024 wginfo.psymalloc = &outsymalloc;
2025 _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
2026 _bfd_generic_link_write_global_symbol,
2029 /* Make sure we have a trailing NULL pointer on OUTSYMBOLS. We
2030 shouldn't really need one, since we have SYMCOUNT, but some old
2031 code still expects one. */
2032 if (! generic_add_output_symbol (abfd, &outsymalloc, NULL))
2035 if (info->relocatable)
2037 /* Allocate space for the output relocs for each section. */
2038 for (o = abfd->sections; o != NULL; o = o->next)
2041 for (p = o->map_head.link_order; p != NULL; p = p->next)
2043 if (p->type == bfd_section_reloc_link_order
2044 || p->type == bfd_symbol_reloc_link_order)
2046 else if (p->type == bfd_indirect_link_order)
2048 asection *input_section;
2055 input_section = p->u.indirect.section;
2056 input_bfd = input_section->owner;
2057 relsize = bfd_get_reloc_upper_bound (input_bfd,
2061 relocs = (arelent **) bfd_malloc (relsize);
2062 if (!relocs && relsize != 0)
2064 symbols = _bfd_generic_link_get_symbols (input_bfd);
2065 reloc_count = bfd_canonicalize_reloc (input_bfd,
2070 if (reloc_count < 0)
2072 BFD_ASSERT ((unsigned long) reloc_count
2073 == input_section->reloc_count);
2074 o->reloc_count += reloc_count;
2077 if (o->reloc_count > 0)
2081 amt = o->reloc_count;
2082 amt *= sizeof (arelent *);
2083 o->orelocation = (struct reloc_cache_entry **) bfd_alloc (abfd, amt);
2084 if (!o->orelocation)
2086 o->flags |= SEC_RELOC;
2087 /* Reset the count so that it can be used as an index
2088 when putting in the output relocs. */
2094 /* Handle all the link order information for the sections. */
2095 for (o = abfd->sections; o != NULL; o = o->next)
2097 for (p = o->map_head.link_order; p != NULL; p = p->next)
2101 case bfd_section_reloc_link_order:
2102 case bfd_symbol_reloc_link_order:
2103 if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
2106 case bfd_indirect_link_order:
2107 if (! default_indirect_link_order (abfd, info, o, p, TRUE))
2111 if (! _bfd_default_link_order (abfd, info, o, p))
2121 /* Add an output symbol to the output BFD. */
2124 generic_add_output_symbol (bfd *output_bfd, size_t *psymalloc, asymbol *sym)
2126 if (bfd_get_symcount (output_bfd) >= *psymalloc)
2131 if (*psymalloc == 0)
2136 amt *= sizeof (asymbol *);
2137 newsyms = (asymbol **) bfd_realloc (bfd_get_outsymbols (output_bfd), amt);
2138 if (newsyms == NULL)
2140 bfd_get_outsymbols (output_bfd) = newsyms;
2143 bfd_get_outsymbols (output_bfd) [bfd_get_symcount (output_bfd)] = sym;
2145 ++ bfd_get_symcount (output_bfd);
2150 /* Handle the symbols for an input BFD. */
2153 _bfd_generic_link_output_symbols (bfd *output_bfd,
2155 struct bfd_link_info *info,
2161 if (!bfd_generic_link_read_symbols (input_bfd))
2164 /* Create a filename symbol if we are supposed to. */
2165 if (info->create_object_symbols_section != NULL)
2169 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
2171 if (sec->output_section == info->create_object_symbols_section)
2175 newsym = bfd_make_empty_symbol (input_bfd);
2178 newsym->name = input_bfd->filename;
2180 newsym->flags = BSF_LOCAL | BSF_FILE;
2181 newsym->section = sec;
2183 if (! generic_add_output_symbol (output_bfd, psymalloc,
2192 /* Adjust the values of the globally visible symbols, and write out
2194 sym_ptr = _bfd_generic_link_get_symbols (input_bfd);
2195 sym_end = sym_ptr + _bfd_generic_link_get_symcount (input_bfd);
2196 for (; sym_ptr < sym_end; sym_ptr++)
2199 struct generic_link_hash_entry *h;
2204 if ((sym->flags & (BSF_INDIRECT
2209 || bfd_is_und_section (bfd_get_section (sym))
2210 || bfd_is_com_section (bfd_get_section (sym))
2211 || bfd_is_ind_section (bfd_get_section (sym)))
2213 if (sym->udata.p != NULL)
2214 h = (struct generic_link_hash_entry *) sym->udata.p;
2215 else if ((sym->flags & BSF_CONSTRUCTOR) != 0)
2217 /* This case normally means that the main linker code
2218 deliberately ignored this constructor symbol. We
2219 should just pass it through. This will screw up if
2220 the constructor symbol is from a different,
2221 non-generic, object file format, but the case will
2222 only arise when linking with -r, which will probably
2223 fail anyhow, since there will be no way to represent
2224 the relocs in the output format being used. */
2227 else if (bfd_is_und_section (bfd_get_section (sym)))
2228 h = ((struct generic_link_hash_entry *)
2229 bfd_wrapped_link_hash_lookup (output_bfd, info,
2230 bfd_asymbol_name (sym),
2231 FALSE, FALSE, TRUE));
2233 h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
2234 bfd_asymbol_name (sym),
2235 FALSE, FALSE, TRUE);
2239 /* Force all references to this symbol to point to
2240 the same area in memory. It is possible that
2241 this routine will be called with a hash table
2242 other than a generic hash table, so we double
2244 if (info->output_bfd->xvec == input_bfd->xvec)
2247 *sym_ptr = sym = h->sym;
2250 switch (h->root.type)
2253 case bfd_link_hash_new:
2255 case bfd_link_hash_undefined:
2257 case bfd_link_hash_undefweak:
2258 sym->flags |= BSF_WEAK;
2260 case bfd_link_hash_indirect:
2261 h = (struct generic_link_hash_entry *) h->root.u.i.link;
2263 case bfd_link_hash_defined:
2264 sym->flags |= BSF_GLOBAL;
2265 sym->flags &=~ BSF_CONSTRUCTOR;
2266 sym->value = h->root.u.def.value;
2267 sym->section = h->root.u.def.section;
2269 case bfd_link_hash_defweak:
2270 sym->flags |= BSF_WEAK;
2271 sym->flags &=~ BSF_CONSTRUCTOR;
2272 sym->value = h->root.u.def.value;
2273 sym->section = h->root.u.def.section;
2275 case bfd_link_hash_common:
2276 sym->value = h->root.u.c.size;
2277 sym->flags |= BSF_GLOBAL;
2278 if (! bfd_is_com_section (sym->section))
2280 BFD_ASSERT (bfd_is_und_section (sym->section));
2281 sym->section = bfd_com_section_ptr;
2283 /* We do not set the section of the symbol to
2284 h->root.u.c.p->section. That value was saved so
2285 that we would know where to allocate the symbol
2286 if it was defined. In this case the type is
2287 still bfd_link_hash_common, so we did not define
2288 it, so we do not want to use that section. */
2294 /* This switch is straight from the old code in
2295 write_file_locals in ldsym.c. */
2296 if (info->strip == strip_all
2297 || (info->strip == strip_some
2298 && bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym),
2299 FALSE, FALSE) == NULL))
2301 else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
2303 /* If this symbol is marked as occurring now, rather
2304 than at the end, output it now. This is used for
2305 COFF C_EXT FCN symbols. FIXME: There must be a
2307 if (bfd_asymbol_bfd (sym) == input_bfd
2308 && (sym->flags & BSF_NOT_AT_END) != 0)
2313 else if (bfd_is_ind_section (sym->section))
2315 else if ((sym->flags & BSF_DEBUGGING) != 0)
2317 if (info->strip == strip_none)
2322 else if (bfd_is_und_section (sym->section)
2323 || bfd_is_com_section (sym->section))
2325 else if ((sym->flags & BSF_LOCAL) != 0)
2327 if ((sym->flags & BSF_WARNING) != 0)
2331 switch (info->discard)
2337 case discard_sec_merge:
2339 if (info->relocatable
2340 || ! (sym->section->flags & SEC_MERGE))
2344 if (bfd_is_local_label (input_bfd, sym))
2355 else if ((sym->flags & BSF_CONSTRUCTOR))
2357 if (info->strip != strip_all)
2362 else if (sym->flags == 0
2363 && (sym->section->owner->flags & BFD_PLUGIN) != 0)
2364 /* LTO doesn't set symbol information. We get here with the
2365 generic linker for a symbol that was "common" but no longer
2366 needs to be global. */
2371 /* If this symbol is in a section which is not being included
2372 in the output file, then we don't want to output the
2374 if (!bfd_is_abs_section (sym->section)
2375 && bfd_section_removed_from_list (output_bfd,
2376 sym->section->output_section))
2381 if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
2391 /* Set the section and value of a generic BFD symbol based on a linker
2392 hash table entry. */
2395 set_symbol_from_hash (asymbol *sym, struct bfd_link_hash_entry *h)
2402 case bfd_link_hash_new:
2403 /* This can happen when a constructor symbol is seen but we are
2404 not building constructors. */
2405 if (sym->section != NULL)
2407 BFD_ASSERT ((sym->flags & BSF_CONSTRUCTOR) != 0);
2411 sym->flags |= BSF_CONSTRUCTOR;
2412 sym->section = bfd_abs_section_ptr;
2416 case bfd_link_hash_undefined:
2417 sym->section = bfd_und_section_ptr;
2420 case bfd_link_hash_undefweak:
2421 sym->section = bfd_und_section_ptr;
2423 sym->flags |= BSF_WEAK;
2425 case bfd_link_hash_defined:
2426 sym->section = h->u.def.section;
2427 sym->value = h->u.def.value;
2429 case bfd_link_hash_defweak:
2430 sym->flags |= BSF_WEAK;
2431 sym->section = h->u.def.section;
2432 sym->value = h->u.def.value;
2434 case bfd_link_hash_common:
2435 sym->value = h->u.c.size;
2436 if (sym->section == NULL)
2437 sym->section = bfd_com_section_ptr;
2438 else if (! bfd_is_com_section (sym->section))
2440 BFD_ASSERT (bfd_is_und_section (sym->section));
2441 sym->section = bfd_com_section_ptr;
2443 /* Do not set the section; see _bfd_generic_link_output_symbols. */
2445 case bfd_link_hash_indirect:
2446 case bfd_link_hash_warning:
2447 /* FIXME: What should we do here? */
2452 /* Write out a global symbol, if it hasn't already been written out.
2453 This is called for each symbol in the hash table. */
2456 _bfd_generic_link_write_global_symbol (struct generic_link_hash_entry *h,
2459 struct generic_write_global_symbol_info *wginfo =
2460 (struct generic_write_global_symbol_info *) data;
2468 if (wginfo->info->strip == strip_all
2469 || (wginfo->info->strip == strip_some
2470 && bfd_hash_lookup (wginfo->info->keep_hash, h->root.root.string,
2471 FALSE, FALSE) == NULL))
2478 sym = bfd_make_empty_symbol (wginfo->output_bfd);
2481 sym->name = h->root.root.string;
2485 set_symbol_from_hash (sym, &h->root);
2487 sym->flags |= BSF_GLOBAL;
2489 if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
2492 /* FIXME: No way to return failure. */
2499 /* Create a relocation. */
2502 _bfd_generic_reloc_link_order (bfd *abfd,
2503 struct bfd_link_info *info,
2505 struct bfd_link_order *link_order)
2509 if (! info->relocatable)
2511 if (sec->orelocation == NULL)
2514 r = (arelent *) bfd_alloc (abfd, sizeof (arelent));
2518 r->address = link_order->offset;
2519 r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
2522 bfd_set_error (bfd_error_bad_value);
2526 /* Get the symbol to use for the relocation. */
2527 if (link_order->type == bfd_section_reloc_link_order)
2528 r->sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
2531 struct generic_link_hash_entry *h;
2533 h = ((struct generic_link_hash_entry *)
2534 bfd_wrapped_link_hash_lookup (abfd, info,
2535 link_order->u.reloc.p->u.name,
2536 FALSE, FALSE, TRUE));
2540 if (! ((*info->callbacks->unattached_reloc)
2541 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
2543 bfd_set_error (bfd_error_bad_value);
2546 r->sym_ptr_ptr = &h->sym;
2549 /* If this is an inplace reloc, write the addend to the object file.
2550 Otherwise, store it in the reloc addend. */
2551 if (! r->howto->partial_inplace)
2552 r->addend = link_order->u.reloc.p->addend;
2556 bfd_reloc_status_type rstat;
2561 size = bfd_get_reloc_size (r->howto);
2562 buf = (bfd_byte *) bfd_zmalloc (size);
2565 rstat = _bfd_relocate_contents (r->howto, abfd,
2566 (bfd_vma) link_order->u.reloc.p->addend,
2573 case bfd_reloc_outofrange:
2575 case bfd_reloc_overflow:
2576 if (! ((*info->callbacks->reloc_overflow)
2578 (link_order->type == bfd_section_reloc_link_order
2579 ? bfd_section_name (abfd, link_order->u.reloc.p->u.section)
2580 : link_order->u.reloc.p->u.name),
2581 r->howto->name, link_order->u.reloc.p->addend,
2589 loc = link_order->offset * bfd_octets_per_byte (abfd);
2590 ok = bfd_set_section_contents (abfd, sec, buf, loc, size);
2598 sec->orelocation[sec->reloc_count] = r;
2604 /* Allocate a new link_order for a section. */
2606 struct bfd_link_order *
2607 bfd_new_link_order (bfd *abfd, asection *section)
2609 bfd_size_type amt = sizeof (struct bfd_link_order);
2610 struct bfd_link_order *new_lo;
2612 new_lo = (struct bfd_link_order *) bfd_zalloc (abfd, amt);
2616 new_lo->type = bfd_undefined_link_order;
2618 if (section->map_tail.link_order != NULL)
2619 section->map_tail.link_order->next = new_lo;
2621 section->map_head.link_order = new_lo;
2622 section->map_tail.link_order = new_lo;
2627 /* Default link order processing routine. Note that we can not handle
2628 the reloc_link_order types here, since they depend upon the details
2629 of how the particular backends generates relocs. */
2632 _bfd_default_link_order (bfd *abfd,
2633 struct bfd_link_info *info,
2635 struct bfd_link_order *link_order)
2637 switch (link_order->type)
2639 case bfd_undefined_link_order:
2640 case bfd_section_reloc_link_order:
2641 case bfd_symbol_reloc_link_order:
2644 case bfd_indirect_link_order:
2645 return default_indirect_link_order (abfd, info, sec, link_order,
2647 case bfd_data_link_order:
2648 return default_data_link_order (abfd, info, sec, link_order);
2652 /* Default routine to handle a bfd_data_link_order. */
2655 default_data_link_order (bfd *abfd,
2656 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2658 struct bfd_link_order *link_order)
2666 BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
2668 size = link_order->size;
2672 fill = link_order->u.data.contents;
2673 fill_size = link_order->u.data.size;
2676 fill = abfd->arch_info->fill (size, bfd_big_endian (abfd),
2677 (sec->flags & SEC_CODE) != 0);
2681 else if (fill_size < size)
2684 fill = (bfd_byte *) bfd_malloc (size);
2689 memset (p, (int) link_order->u.data.contents[0], (size_t) size);
2694 memcpy (p, link_order->u.data.contents, fill_size);
2698 while (size >= fill_size);
2700 memcpy (p, link_order->u.data.contents, (size_t) size);
2701 size = link_order->size;
2705 loc = link_order->offset * bfd_octets_per_byte (abfd);
2706 result = bfd_set_section_contents (abfd, sec, fill, loc, size);
2708 if (fill != link_order->u.data.contents)
2713 /* Default routine to handle a bfd_indirect_link_order. */
2716 default_indirect_link_order (bfd *output_bfd,
2717 struct bfd_link_info *info,
2718 asection *output_section,
2719 struct bfd_link_order *link_order,
2720 bfd_boolean generic_linker)
2722 asection *input_section;
2724 bfd_byte *contents = NULL;
2725 bfd_byte *new_contents;
2726 bfd_size_type sec_size;
2729 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
2731 input_section = link_order->u.indirect.section;
2732 input_bfd = input_section->owner;
2733 if (input_section->size == 0)
2736 BFD_ASSERT (input_section->output_section == output_section);
2737 BFD_ASSERT (input_section->output_offset == link_order->offset);
2738 BFD_ASSERT (input_section->size == link_order->size);
2740 if (info->relocatable
2741 && input_section->reloc_count > 0
2742 && output_section->orelocation == NULL)
2744 /* Space has not been allocated for the output relocations.
2745 This can happen when we are called by a specific backend
2746 because somebody is attempting to link together different
2747 types of object files. Handling this case correctly is
2748 difficult, and sometimes impossible. */
2749 (*_bfd_error_handler)
2750 (_("Attempt to do relocatable link with %s input and %s output"),
2751 bfd_get_target (input_bfd), bfd_get_target (output_bfd));
2752 bfd_set_error (bfd_error_wrong_format);
2756 if (! generic_linker)
2761 /* Get the canonical symbols. The generic linker will always
2762 have retrieved them by this point, but we are being called by
2763 a specific linker, presumably because we are linking
2764 different types of object files together. */
2765 if (!bfd_generic_link_read_symbols (input_bfd))
2768 /* Since we have been called by a specific linker, rather than
2769 the generic linker, the values of the symbols will not be
2770 right. They will be the values as seen in the input file,
2771 not the values of the final link. We need to fix them up
2772 before we can relocate the section. */
2773 sympp = _bfd_generic_link_get_symbols (input_bfd);
2774 symppend = sympp + _bfd_generic_link_get_symcount (input_bfd);
2775 for (; sympp < symppend; sympp++)
2778 struct bfd_link_hash_entry *h;
2782 if ((sym->flags & (BSF_INDIRECT
2787 || bfd_is_und_section (bfd_get_section (sym))
2788 || bfd_is_com_section (bfd_get_section (sym))
2789 || bfd_is_ind_section (bfd_get_section (sym)))
2791 /* sym->udata may have been set by
2792 generic_link_add_symbol_list. */
2793 if (sym->udata.p != NULL)
2794 h = (struct bfd_link_hash_entry *) sym->udata.p;
2795 else if (bfd_is_und_section (bfd_get_section (sym)))
2796 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
2797 bfd_asymbol_name (sym),
2798 FALSE, FALSE, TRUE);
2800 h = bfd_link_hash_lookup (info->hash,
2801 bfd_asymbol_name (sym),
2802 FALSE, FALSE, TRUE);
2804 set_symbol_from_hash (sym, h);
2809 if ((output_section->flags & (SEC_GROUP | SEC_LINKER_CREATED)) == SEC_GROUP
2810 && input_section->size != 0)
2812 /* Group section contents are set by bfd_elf_set_group_contents. */
2813 if (!output_bfd->output_has_begun)
2815 /* FIXME: This hack ensures bfd_elf_set_group_contents is called. */
2816 if (!bfd_set_section_contents (output_bfd, output_section, "", 0, 1))
2819 new_contents = output_section->contents;
2820 BFD_ASSERT (new_contents != NULL);
2821 BFD_ASSERT (input_section->output_offset == 0);
2825 /* Get and relocate the section contents. */
2826 sec_size = (input_section->rawsize > input_section->size
2827 ? input_section->rawsize
2828 : input_section->size);
2829 contents = (bfd_byte *) bfd_malloc (sec_size);
2830 if (contents == NULL && sec_size != 0)
2832 new_contents = (bfd_get_relocated_section_contents
2833 (output_bfd, info, link_order, contents,
2835 _bfd_generic_link_get_symbols (input_bfd)));
2840 /* Output the section contents. */
2841 loc = input_section->output_offset * bfd_octets_per_byte (output_bfd);
2842 if (! bfd_set_section_contents (output_bfd, output_section,
2843 new_contents, loc, input_section->size))
2846 if (contents != NULL)
2851 if (contents != NULL)
2856 /* A little routine to count the number of relocs in a link_order
2860 _bfd_count_link_order_relocs (struct bfd_link_order *link_order)
2862 register unsigned int c;
2863 register struct bfd_link_order *l;
2866 for (l = link_order; l != NULL; l = l->next)
2868 if (l->type == bfd_section_reloc_link_order
2869 || l->type == bfd_symbol_reloc_link_order)
2878 bfd_link_split_section
2881 bfd_boolean bfd_link_split_section (bfd *abfd, asection *sec);
2884 Return nonzero if @var{sec} should be split during a
2885 reloceatable or final link.
2887 .#define bfd_link_split_section(abfd, sec) \
2888 . BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
2894 _bfd_generic_link_split_section (bfd *abfd ATTRIBUTE_UNUSED,
2895 asection *sec ATTRIBUTE_UNUSED)
2902 bfd_section_already_linked
2905 bfd_boolean bfd_section_already_linked (bfd *abfd,
2907 struct bfd_link_info *info);
2910 Check if @var{data} has been already linked during a reloceatable
2911 or final link. Return TRUE if it has.
2913 .#define bfd_section_already_linked(abfd, sec, info) \
2914 . BFD_SEND (abfd, _section_already_linked, (abfd, sec, info))
2919 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
2920 once into the output. This routine checks each section, and
2921 arrange to discard it if a section of the same name has already
2922 been linked. This code assumes that all relevant sections have the
2923 SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
2924 section name. bfd_section_already_linked is called via
2925 bfd_map_over_sections. */
2927 /* The hash table. */
2929 static struct bfd_hash_table _bfd_section_already_linked_table;
2931 /* Support routines for the hash table used by section_already_linked,
2932 initialize the table, traverse, lookup, fill in an entry and remove
2936 bfd_section_already_linked_table_traverse
2937 (bfd_boolean (*func) (struct bfd_section_already_linked_hash_entry *,
2938 void *), void *info)
2940 bfd_hash_traverse (&_bfd_section_already_linked_table,
2941 (bfd_boolean (*) (struct bfd_hash_entry *,
2946 struct bfd_section_already_linked_hash_entry *
2947 bfd_section_already_linked_table_lookup (const char *name)
2949 return ((struct bfd_section_already_linked_hash_entry *)
2950 bfd_hash_lookup (&_bfd_section_already_linked_table, name,
2955 bfd_section_already_linked_table_insert
2956 (struct bfd_section_already_linked_hash_entry *already_linked_list,
2959 struct bfd_section_already_linked *l;
2961 /* Allocate the memory from the same obstack as the hash table is
2963 l = (struct bfd_section_already_linked *)
2964 bfd_hash_allocate (&_bfd_section_already_linked_table, sizeof *l);
2968 l->next = already_linked_list->entry;
2969 already_linked_list->entry = l;
2973 static struct bfd_hash_entry *
2974 already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
2975 struct bfd_hash_table *table,
2976 const char *string ATTRIBUTE_UNUSED)
2978 struct bfd_section_already_linked_hash_entry *ret =
2979 (struct bfd_section_already_linked_hash_entry *)
2980 bfd_hash_allocate (table, sizeof *ret);
2991 bfd_section_already_linked_table_init (void)
2993 return bfd_hash_table_init_n (&_bfd_section_already_linked_table,
2994 already_linked_newfunc,
2995 sizeof (struct bfd_section_already_linked_hash_entry),
3000 bfd_section_already_linked_table_free (void)
3002 bfd_hash_table_free (&_bfd_section_already_linked_table);
3005 /* Report warnings as appropriate for duplicate section SEC.
3006 Return FALSE if we decide to keep SEC after all. */
3009 _bfd_handle_already_linked (asection *sec,
3010 struct bfd_section_already_linked *l,
3011 struct bfd_link_info *info)
3013 switch (sec->flags & SEC_LINK_DUPLICATES)
3018 case SEC_LINK_DUPLICATES_DISCARD:
3019 /* If we found an LTO IR match for this comdat group on
3020 the first pass, replace it with the LTO output on the
3021 second pass. We can't simply choose real object
3022 files over IR because the first pass may contain a
3023 mix of LTO and normal objects and we must keep the
3024 first match, be it IR or real. */
3025 if (info->loading_lto_outputs
3026 && (l->sec->owner->flags & BFD_PLUGIN) != 0)
3033 case SEC_LINK_DUPLICATES_ONE_ONLY:
3034 info->callbacks->einfo
3035 (_("%B: ignoring duplicate section `%A'\n"),
3039 case SEC_LINK_DUPLICATES_SAME_SIZE:
3040 if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
3042 else if (sec->size != l->sec->size)
3043 info->callbacks->einfo
3044 (_("%B: duplicate section `%A' has different size\n"),
3048 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
3049 if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
3051 else if (sec->size != l->sec->size)
3052 info->callbacks->einfo
3053 (_("%B: duplicate section `%A' has different size\n"),
3055 else if (sec->size != 0)
3057 bfd_byte *sec_contents, *l_sec_contents = NULL;
3059 if (!bfd_malloc_and_get_section (sec->owner, sec, &sec_contents))
3060 info->callbacks->einfo
3061 (_("%B: could not read contents of section `%A'\n"),
3063 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
3065 info->callbacks->einfo
3066 (_("%B: could not read contents of section `%A'\n"),
3067 l->sec->owner, l->sec);
3068 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
3069 info->callbacks->einfo
3070 (_("%B: duplicate section `%A' has different contents\n"),
3074 free (sec_contents);
3076 free (l_sec_contents);
3081 /* Set the output_section field so that lang_add_section
3082 does not create a lang_input_section structure for this
3083 section. Since there might be a symbol in the section
3084 being discarded, we must retain a pointer to the section
3085 which we are really going to use. */
3086 sec->output_section = bfd_abs_section_ptr;
3087 sec->kept_section = l->sec;
3091 /* This is used on non-ELF inputs. */
3094 _bfd_generic_section_already_linked (bfd *abfd ATTRIBUTE_UNUSED,
3096 struct bfd_link_info *info)
3099 struct bfd_section_already_linked *l;
3100 struct bfd_section_already_linked_hash_entry *already_linked_list;
3102 if ((sec->flags & SEC_LINK_ONCE) == 0)
3105 /* The generic linker doesn't handle section groups. */
3106 if ((sec->flags & SEC_GROUP) != 0)
3109 /* FIXME: When doing a relocatable link, we may have trouble
3110 copying relocations in other sections that refer to local symbols
3111 in the section being discarded. Those relocations will have to
3112 be converted somehow; as of this writing I'm not sure that any of
3113 the backends handle that correctly.
3115 It is tempting to instead not discard link once sections when
3116 doing a relocatable link (technically, they should be discarded
3117 whenever we are building constructors). However, that fails,
3118 because the linker winds up combining all the link once sections
3119 into a single large link once section, which defeats the purpose
3120 of having link once sections in the first place. */
3122 name = bfd_get_section_name (abfd, sec);
3124 already_linked_list = bfd_section_already_linked_table_lookup (name);
3126 l = already_linked_list->entry;
3129 /* The section has already been linked. See if we should
3131 return _bfd_handle_already_linked (sec, l, info);
3134 /* This is the first section with this name. Record it. */
3135 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
3136 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
3140 /* Choose a neighbouring section to S in OBFD that will be output, or
3141 the absolute section if ADDR is out of bounds of the neighbours. */
3144 _bfd_nearby_section (bfd *obfd, asection *s, bfd_vma addr)
3146 asection *next, *prev, *best;
3148 /* Find preceding kept section. */
3149 for (prev = s->prev; prev != NULL; prev = prev->prev)
3150 if ((prev->flags & SEC_EXCLUDE) == 0
3151 && !bfd_section_removed_from_list (obfd, prev))
3154 /* Find following kept section. Start at prev->next because
3155 other sections may have been added after S was removed. */
3156 if (s->prev != NULL)
3157 next = s->prev->next;
3159 next = s->owner->sections;
3160 for (; next != NULL; next = next->next)
3161 if ((next->flags & SEC_EXCLUDE) == 0
3162 && !bfd_section_removed_from_list (obfd, next))
3165 /* Choose better of two sections, based on flags. The idea
3166 is to choose a section that will be in the same segment
3167 as S would have been if it was kept. */
3172 best = bfd_abs_section_ptr;
3174 else if (next == NULL)
3176 else if (((prev->flags ^ next->flags)
3177 & (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_LOAD)) != 0)
3179 if (((next->flags ^ s->flags)
3180 & (SEC_ALLOC | SEC_THREAD_LOCAL)) != 0
3181 /* We prefer to choose a loaded section. Section S
3182 doesn't have SEC_LOAD set (it being excluded, that
3183 part of the flag processing didn't happen) so we
3184 can't compare that flag to those of NEXT and PREV. */
3185 || ((prev->flags & SEC_LOAD) != 0
3186 && (next->flags & SEC_LOAD) == 0))
3189 else if (((prev->flags ^ next->flags) & SEC_READONLY) != 0)
3191 if (((next->flags ^ s->flags) & SEC_READONLY) != 0)
3194 else if (((prev->flags ^ next->flags) & SEC_CODE) != 0)
3196 if (((next->flags ^ s->flags) & SEC_CODE) != 0)
3201 /* Flags we care about are the same. Prefer the following
3202 section if that will result in a positive valued sym. */
3203 if (addr < next->vma)
3210 /* Convert symbols in excluded output sections to use a kept section. */
3213 fix_syms (struct bfd_link_hash_entry *h, void *data)
3215 bfd *obfd = (bfd *) data;
3217 if (h->type == bfd_link_hash_defined
3218 || h->type == bfd_link_hash_defweak)
3220 asection *s = h->u.def.section;
3222 && s->output_section != NULL
3223 && (s->output_section->flags & SEC_EXCLUDE) != 0
3224 && bfd_section_removed_from_list (obfd, s->output_section))
3228 h->u.def.value += s->output_offset + s->output_section->vma;
3229 op = _bfd_nearby_section (obfd, s->output_section, h->u.def.value);
3230 h->u.def.value -= op->vma;
3231 h->u.def.section = op;
3239 _bfd_fix_excluded_sec_syms (bfd *obfd, struct bfd_link_info *info)
3241 bfd_link_hash_traverse (info->hash, fix_syms, obfd);
3246 bfd_generic_define_common_symbol
3249 bfd_boolean bfd_generic_define_common_symbol
3250 (bfd *output_bfd, struct bfd_link_info *info,
3251 struct bfd_link_hash_entry *h);
3254 Convert common symbol @var{h} into a defined symbol.
3255 Return TRUE on success and FALSE on failure.
3257 .#define bfd_define_common_symbol(output_bfd, info, h) \
3258 . BFD_SEND (output_bfd, _bfd_define_common_symbol, (output_bfd, info, h))
3263 bfd_generic_define_common_symbol (bfd *output_bfd,
3264 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3265 struct bfd_link_hash_entry *h)
3267 unsigned int power_of_two;
3268 bfd_vma alignment, size;
3271 BFD_ASSERT (h != NULL && h->type == bfd_link_hash_common);
3274 power_of_two = h->u.c.p->alignment_power;
3275 section = h->u.c.p->section;
3277 /* Increase the size of the section to align the common symbol.
3278 The alignment must be a power of two. */
3279 alignment = bfd_octets_per_byte (output_bfd) << power_of_two;
3280 BFD_ASSERT (alignment != 0 && (alignment & -alignment) == alignment);
3281 section->size += alignment - 1;
3282 section->size &= -alignment;
3284 /* Adjust the section's overall alignment if necessary. */
3285 if (power_of_two > section->alignment_power)
3286 section->alignment_power = power_of_two;
3288 /* Change the symbol from common to defined. */
3289 h->type = bfd_link_hash_defined;
3290 h->u.def.section = section;
3291 h->u.def.value = section->size;
3293 /* Increase the size of the section. */
3294 section->size += size;
3296 /* Make sure the section is allocated in memory, and make sure that
3297 it is no longer a common section. */
3298 section->flags |= SEC_ALLOC;
3299 section->flags &= ~SEC_IS_COMMON;
3305 bfd_find_version_for_sym
3308 struct bfd_elf_version_tree * bfd_find_version_for_sym
3309 (struct bfd_elf_version_tree *verdefs,
3310 const char *sym_name, bfd_boolean *hide);
3313 Search an elf version script tree for symbol versioning
3314 info and export / don't-export status for a given symbol.
3315 Return non-NULL on success and NULL on failure; also sets
3316 the output @samp{hide} boolean parameter.
3320 struct bfd_elf_version_tree *
3321 bfd_find_version_for_sym (struct bfd_elf_version_tree *verdefs,
3322 const char *sym_name,
3325 struct bfd_elf_version_tree *t;
3326 struct bfd_elf_version_tree *local_ver, *global_ver, *exist_ver;
3327 struct bfd_elf_version_tree *star_local_ver, *star_global_ver;
3331 star_local_ver = NULL;
3332 star_global_ver = NULL;
3334 for (t = verdefs; t != NULL; t = t->next)
3336 if (t->globals.list != NULL)
3338 struct bfd_elf_version_expr *d = NULL;
3340 while ((d = (*t->match) (&t->globals, d, sym_name)) != NULL)
3342 if (d->literal || strcmp (d->pattern, "*") != 0)
3345 star_global_ver = t;
3349 /* If the match is a wildcard pattern, keep looking for
3350 a more explicit, perhaps even local, match. */
3359 if (t->locals.list != NULL)
3361 struct bfd_elf_version_expr *d = NULL;
3363 while ((d = (*t->match) (&t->locals, d, sym_name)) != NULL)
3365 if (d->literal || strcmp (d->pattern, "*") != 0)
3369 /* If the match is a wildcard pattern, keep looking for
3370 a more explicit, perhaps even global, match. */
3373 /* An exact match overrides a global wildcard. */
3375 star_global_ver = NULL;
3385 if (global_ver == NULL && local_ver == NULL)
3386 global_ver = star_global_ver;
3388 if (global_ver != NULL)
3390 /* If we already have a versioned symbol that matches the
3391 node for this symbol, then we don't want to create a
3392 duplicate from the unversioned symbol. Instead hide the
3393 unversioned symbol. */
3394 *hide = exist_ver == global_ver;
3398 if (local_ver == NULL)
3399 local_ver = star_local_ver;
3401 if (local_ver != NULL)
3412 bfd_hide_sym_by_version
3415 bfd_boolean bfd_hide_sym_by_version
3416 (struct bfd_elf_version_tree *verdefs, const char *sym_name);
3419 Search an elf version script tree for symbol versioning
3420 info for a given symbol. Return TRUE if the symbol is hidden.
3425 bfd_hide_sym_by_version (struct bfd_elf_version_tree *verdefs,
3426 const char *sym_name)
3428 bfd_boolean hidden = FALSE;
3429 bfd_find_version_for_sym (verdefs, sym_name, &hidden);