1 /* linker.c -- BFD linker routines
2 Copyright (C) 1993-2014 Free Software Foundation, Inc.
3 Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
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
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
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.
64 @* Creating a Linker Hash Table::
65 @* Adding Symbols to the Hash Table::
66 @* Performing the Final Link::
70 Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
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
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.
108 Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
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
124 The a.out version of this entry point is
125 <<NAME(aout,link_add_symbols)>>.
128 @* Differing file formats::
129 @* Adding symbols from an object file::
130 @* Adding symbols from an archive::
134 Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
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 output bfd
153 xvec must be checked to make sure that the hash table was
154 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 output bfd before saving information (in this
167 case, the ECOFF external symbol debugging information) in a
171 Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
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.
215 Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
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. (The
225 callback may in fact indicate that a replacement BFD should be
226 used, in which case the symbols from that BFD should be added
227 to the linker hash table instead.)
229 @findex _bfd_generic_link_add_archive_symbols
230 In most cases the work of looking through the symbols in the
231 archive should be done by the
232 <<_bfd_generic_link_add_archive_symbols>> function. This
233 function builds a hash table from the archive symbol table and
234 looks through the list of undefined symbols to see which
235 elements should be included.
236 <<_bfd_generic_link_add_archive_symbols>> is passed a function
237 to call to make the final decision about adding an archive
238 element to the link and to do the actual work of adding the
239 symbols to the linker hash table.
241 The function passed to
242 <<_bfd_generic_link_add_archive_symbols>> must read the
243 symbols of the archive element and decide whether the archive
244 element should be included in the link. If the element is to
245 be included, the <<add_archive_element>> linker callback
246 routine must be called with the element as an argument, and
247 the element's symbols must be added to the linker hash table
248 just as though the element had itself been passed to the
249 <<_bfd_link_add_symbols>> function. The <<add_archive_element>>
250 callback has the option to indicate that it would like to
251 replace the element archive with a substitute BFD, in which
252 case it is the symbols of that substitute BFD that must be
253 added to the linker hash table instead.
255 When the a.out <<_bfd_link_add_symbols>> function receives an
256 archive, it calls <<_bfd_generic_link_add_archive_symbols>>
257 passing <<aout_link_check_archive_element>> as the function
258 argument. <<aout_link_check_archive_element>> calls
259 <<aout_link_check_ar_symbols>>. If the latter decides to add
260 the element (an element is only added if it provides a real,
261 non-common, definition for a previously undefined or common
262 symbol) it calls the <<add_archive_element>> callback and then
263 <<aout_link_check_archive_element>> calls
264 <<aout_link_add_symbols>> to actually add the symbols to the
265 linker hash table - possibly those of a substitute BFD, if the
266 <<add_archive_element>> callback avails itself of that option.
268 The ECOFF back end is unusual in that it does not normally
269 call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
270 archives already contain a hash table of symbols. The ECOFF
271 back end searches the archive itself to avoid the overhead of
272 creating a new hash table.
275 Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions
277 Performing the final link
279 @cindex _bfd_link_final_link in target vector
280 @cindex target vector (_bfd_final_link)
281 When all the input files have been processed, the linker calls
282 the <<_bfd_final_link>> entry point of the output BFD. This
283 routine is responsible for producing the final output file,
284 which has several aspects. It must relocate the contents of
285 the input sections and copy the data into the output sections.
286 It must build an output symbol table including any local
287 symbols from the input files and the global symbols from the
288 hash table. When producing relocatable output, it must
289 modify the input relocs and write them into the output file.
290 There may also be object format dependent work to be done.
292 The linker will also call the <<write_object_contents>> entry
293 point when the BFD is closed. The two entry points must work
294 together in order to produce the correct output file.
296 The details of how this works are inevitably dependent upon
297 the specific object file format. The a.out
298 <<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
301 @* Information provided by the linker::
302 @* Relocating the section contents::
303 @* Writing the symbol table::
307 Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
309 Information provided by the linker
311 Before the linker calls the <<_bfd_final_link>> entry point,
312 it sets up some data structures for the function to use.
314 The <<input_bfds>> field of the <<bfd_link_info>> structure
315 will point to a list of all the input files included in the
316 link. These files are linked through the <<link.next>> field
317 of the <<bfd>> structure.
319 Each section in the output file will have a list of
320 <<link_order>> structures attached to the <<map_head.link_order>>
321 field (the <<link_order>> structure is defined in
322 <<bfdlink.h>>). These structures describe how to create the
323 contents of the output section in terms of the contents of
324 various input sections, fill constants, and, eventually, other
325 types of information. They also describe relocs that must be
326 created by the BFD backend, but do not correspond to any input
327 file; this is used to support -Ur, which builds constructors
328 while generating a relocatable object file.
331 Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
333 Relocating the section contents
335 The <<_bfd_final_link>> function should look through the
336 <<link_order>> structures attached to each section of the
337 output file. Each <<link_order>> structure should either be
338 handled specially, or it should be passed to the function
339 <<_bfd_default_link_order>> which will do the right thing
340 (<<_bfd_default_link_order>> is defined in <<linker.c>>).
342 For efficiency, a <<link_order>> of type
343 <<bfd_indirect_link_order>> whose associated section belongs
344 to a BFD of the same format as the output BFD must be handled
345 specially. This type of <<link_order>> describes part of an
346 output section in terms of a section belonging to one of the
347 input files. The <<_bfd_final_link>> function should read the
348 contents of the section and any associated relocs, apply the
349 relocs to the section contents, and write out the modified
350 section contents. If performing a relocatable link, the
351 relocs themselves must also be modified and written out.
353 @findex _bfd_relocate_contents
354 @findex _bfd_final_link_relocate
355 The functions <<_bfd_relocate_contents>> and
356 <<_bfd_final_link_relocate>> provide some general support for
357 performing the actual relocations, notably overflow checking.
358 Their arguments include information about the symbol the
359 relocation is against and a <<reloc_howto_type>> argument
360 which describes the relocation to perform. These functions
361 are defined in <<reloc.c>>.
363 The a.out function which handles reading, relocating, and
364 writing section contents is <<aout_link_input_section>>. The
365 actual relocation is done in <<aout_link_input_section_std>>
366 and <<aout_link_input_section_ext>>.
369 Writing the symbol table, , Relocating the section contents, Performing the Final Link
371 Writing the symbol table
373 The <<_bfd_final_link>> function must gather all the symbols
374 in the input files and write them out. It must also write out
375 all the symbols in the global hash table. This must be
376 controlled by the <<strip>> and <<discard>> fields of the
377 <<bfd_link_info>> structure.
379 The local symbols of the input files will not have been
380 entered into the linker hash table. The <<_bfd_final_link>>
381 routine must consider each input file and include the symbols
382 in the output file. It may be convenient to do this when
383 looking through the <<link_order>> structures, or it may be
384 done by stepping through the <<input_bfds>> list.
386 The <<_bfd_final_link>> routine must also traverse the global
387 hash table to gather all the externally visible symbols. It
388 is possible that most of the externally visible symbols may be
389 written out when considering the symbols of each input file,
390 but it is still necessary to traverse the hash table since the
391 linker script may have defined some symbols that are not in
392 any of the input files.
394 The <<strip>> field of the <<bfd_link_info>> structure
395 controls which symbols are written out. The possible values
396 are listed in <<bfdlink.h>>. If the value is <<strip_some>>,
397 then the <<keep_hash>> field of the <<bfd_link_info>>
398 structure is a hash table of symbols to keep; each symbol
399 should be looked up in this hash table, and only symbols which
400 are present should be included in the output file.
402 If the <<strip>> field of the <<bfd_link_info>> structure
403 permits local symbols to be written out, the <<discard>> field
404 is used to further controls which local symbols are included
405 in the output file. If the value is <<discard_l>>, then all
406 local symbols which begin with a certain prefix are discarded;
407 this is controlled by the <<bfd_is_local_label_name>> entry point.
409 The a.out backend handles symbols by calling
410 <<aout_link_write_symbols>> on each input BFD and then
411 traversing the global hash table with the function
412 <<aout_link_write_other_symbol>>. It builds a string table
413 while writing out the symbols, which is written to the output
414 file at the end of <<NAME(aout,final_link)>>.
417 static bfd_boolean generic_link_add_object_symbols
418 (bfd *, struct bfd_link_info *, bfd_boolean collect);
419 static bfd_boolean generic_link_add_symbols
420 (bfd *, struct bfd_link_info *, bfd_boolean);
421 static bfd_boolean generic_link_check_archive_element_no_collect
422 (bfd *, struct bfd_link_info *, bfd_boolean *);
423 static bfd_boolean generic_link_check_archive_element_collect
424 (bfd *, struct bfd_link_info *, bfd_boolean *);
425 static bfd_boolean generic_link_check_archive_element
426 (bfd *, struct bfd_link_info *, bfd_boolean *, bfd_boolean);
427 static bfd_boolean generic_link_add_symbol_list
428 (bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **,
430 static bfd_boolean generic_add_output_symbol
431 (bfd *, size_t *psymalloc, asymbol *);
432 static bfd_boolean default_data_link_order
433 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
434 static bfd_boolean default_indirect_link_order
435 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *,
438 /* The link hash table structure is defined in bfdlink.h. It provides
439 a base hash table which the backend specific hash tables are built
442 /* Routine to create an entry in the link hash table. */
444 struct bfd_hash_entry *
445 _bfd_link_hash_newfunc (struct bfd_hash_entry *entry,
446 struct bfd_hash_table *table,
449 /* Allocate the structure if it has not already been allocated by a
453 entry = (struct bfd_hash_entry *)
454 bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry));
459 /* Call the allocation method of the superclass. */
460 entry = bfd_hash_newfunc (entry, table, string);
463 struct bfd_link_hash_entry *h = (struct bfd_link_hash_entry *) entry;
465 /* Initialize the local fields. */
466 memset ((char *) &h->root + sizeof (h->root), 0,
467 sizeof (*h) - sizeof (h->root));
473 /* Initialize a link hash table. The BFD argument is the one
474 responsible for creating this table. */
477 _bfd_link_hash_table_init
478 (struct bfd_link_hash_table *table,
479 bfd *abfd ATTRIBUTE_UNUSED,
480 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
481 struct bfd_hash_table *,
483 unsigned int entsize)
487 BFD_ASSERT (!abfd->is_linker_output && !abfd->link.hash);
488 table->undefs = NULL;
489 table->undefs_tail = NULL;
490 table->type = bfd_link_generic_hash_table;
492 ret = bfd_hash_table_init (&table->table, newfunc, entsize);
495 /* Arrange for destruction of this hash table on closing ABFD. */
496 table->hash_table_free = _bfd_generic_link_hash_table_free;
497 abfd->link.hash = table;
498 abfd->is_linker_output = TRUE;
503 /* Look up a symbol in a link hash table. If follow is TRUE, we
504 follow bfd_link_hash_indirect and bfd_link_hash_warning links to
507 struct bfd_link_hash_entry *
508 bfd_link_hash_lookup (struct bfd_link_hash_table *table,
514 struct bfd_link_hash_entry *ret;
516 ret = ((struct bfd_link_hash_entry *)
517 bfd_hash_lookup (&table->table, string, create, copy));
519 if (follow && ret != NULL)
521 while (ret->type == bfd_link_hash_indirect
522 || ret->type == bfd_link_hash_warning)
529 /* Look up a symbol in the main linker hash table if the symbol might
530 be wrapped. This should only be used for references to an
531 undefined symbol, not for definitions of a symbol. */
533 struct bfd_link_hash_entry *
534 bfd_wrapped_link_hash_lookup (bfd *abfd,
535 struct bfd_link_info *info,
543 if (info->wrap_hash != NULL)
549 if (*l == bfd_get_symbol_leading_char (abfd) || *l == info->wrap_char)
556 #define WRAP "__wrap_"
558 if (bfd_hash_lookup (info->wrap_hash, l, FALSE, FALSE) != NULL)
561 struct bfd_link_hash_entry *h;
563 /* This symbol is being wrapped. We want to replace all
564 references to SYM with references to __wrap_SYM. */
566 amt = strlen (l) + sizeof WRAP + 1;
567 n = (char *) bfd_malloc (amt);
575 h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
581 #define REAL "__real_"
584 && CONST_STRNEQ (l, REAL)
585 && bfd_hash_lookup (info->wrap_hash, l + sizeof REAL - 1,
586 FALSE, FALSE) != NULL)
589 struct bfd_link_hash_entry *h;
591 /* This is a reference to __real_SYM, where SYM is being
592 wrapped. We want to replace all references to __real_SYM
593 with references to SYM. */
595 amt = strlen (l + sizeof REAL - 1) + 2;
596 n = (char *) bfd_malloc (amt);
602 strcat (n, l + sizeof REAL - 1);
603 h = bfd_link_hash_lookup (info->hash, n, create, TRUE, follow);
611 return bfd_link_hash_lookup (info->hash, string, create, copy, follow);
614 /* If H is a wrapped symbol, ie. the symbol name starts with "__wrap_"
615 and the remainder is found in wrap_hash, return the real symbol. */
617 struct bfd_link_hash_entry *
618 unwrap_hash_lookup (struct bfd_link_info *info,
620 struct bfd_link_hash_entry *h)
622 const char *l = h->root.string;
624 if (*l == bfd_get_symbol_leading_char (input_bfd)
625 || *l == info->wrap_char)
628 if (CONST_STRNEQ (l, WRAP))
630 l += sizeof WRAP - 1;
632 if (bfd_hash_lookup (info->wrap_hash, l, FALSE, FALSE) != NULL)
635 if (l - (sizeof WRAP - 1) != h->root.string)
639 *(char *) l = *h->root.string;
641 h = bfd_link_hash_lookup (info->hash, l, FALSE, FALSE, FALSE);
650 /* Traverse a generic link hash table. Differs from bfd_hash_traverse
651 in the treatment of warning symbols. When warning symbols are
652 created they replace the real symbol, so you don't get to see the
653 real symbol in a bfd_hash_travere. This traversal calls func with
657 bfd_link_hash_traverse
658 (struct bfd_link_hash_table *htab,
659 bfd_boolean (*func) (struct bfd_link_hash_entry *, void *),
664 htab->table.frozen = 1;
665 for (i = 0; i < htab->table.size; i++)
667 struct bfd_link_hash_entry *p;
669 p = (struct bfd_link_hash_entry *) htab->table.table[i];
670 for (; p != NULL; p = (struct bfd_link_hash_entry *) p->root.next)
671 if (!(*func) (p->type == bfd_link_hash_warning ? p->u.i.link : p, info))
675 htab->table.frozen = 0;
678 /* Add a symbol to the linker hash table undefs list. */
681 bfd_link_add_undef (struct bfd_link_hash_table *table,
682 struct bfd_link_hash_entry *h)
684 BFD_ASSERT (h->u.undef.next == NULL);
685 if (table->undefs_tail != NULL)
686 table->undefs_tail->u.undef.next = h;
687 if (table->undefs == NULL)
689 table->undefs_tail = h;
692 /* The undefs list was designed so that in normal use we don't need to
693 remove entries. However, if symbols on the list are changed from
694 bfd_link_hash_undefined to either bfd_link_hash_undefweak or
695 bfd_link_hash_new for some reason, then they must be removed from the
696 list. Failure to do so might result in the linker attempting to add
697 the symbol to the list again at a later stage. */
700 bfd_link_repair_undef_list (struct bfd_link_hash_table *table)
702 struct bfd_link_hash_entry **pun;
704 pun = &table->undefs;
707 struct bfd_link_hash_entry *h = *pun;
709 if (h->type == bfd_link_hash_new
710 || h->type == bfd_link_hash_undefweak)
712 *pun = h->u.undef.next;
713 h->u.undef.next = NULL;
714 if (h == table->undefs_tail)
716 if (pun == &table->undefs)
717 table->undefs_tail = NULL;
719 /* pun points at an u.undef.next field. Go back to
720 the start of the link_hash_entry. */
721 table->undefs_tail = (struct bfd_link_hash_entry *)
722 ((char *) pun - ((char *) &h->u.undef.next - (char *) h));
727 pun = &h->u.undef.next;
731 /* Routine to create an entry in a generic link hash table. */
733 struct bfd_hash_entry *
734 _bfd_generic_link_hash_newfunc (struct bfd_hash_entry *entry,
735 struct bfd_hash_table *table,
738 /* Allocate the structure if it has not already been allocated by a
742 entry = (struct bfd_hash_entry *)
743 bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry));
748 /* Call the allocation method of the superclass. */
749 entry = _bfd_link_hash_newfunc (entry, table, string);
752 struct generic_link_hash_entry *ret;
754 /* Set local fields. */
755 ret = (struct generic_link_hash_entry *) entry;
756 ret->written = FALSE;
763 /* Create a generic link hash table. */
765 struct bfd_link_hash_table *
766 _bfd_generic_link_hash_table_create (bfd *abfd)
768 struct generic_link_hash_table *ret;
769 bfd_size_type amt = sizeof (struct generic_link_hash_table);
771 ret = (struct generic_link_hash_table *) bfd_malloc (amt);
774 if (! _bfd_link_hash_table_init (&ret->root, abfd,
775 _bfd_generic_link_hash_newfunc,
776 sizeof (struct generic_link_hash_entry)))
785 _bfd_generic_link_hash_table_free (bfd *obfd)
787 struct generic_link_hash_table *ret;
789 BFD_ASSERT (obfd->is_linker_output && obfd->link.hash);
790 ret = (struct generic_link_hash_table *) obfd->link.hash;
791 bfd_hash_table_free (&ret->root.table);
793 obfd->link.hash = NULL;
794 obfd->is_linker_output = FALSE;
797 /* Grab the symbols for an object file when doing a generic link. We
798 store the symbols in the outsymbols field. We need to keep them
799 around for the entire link to ensure that we only read them once.
800 If we read them multiple times, we might wind up with relocs and
801 the hash table pointing to different instances of the symbol
805 bfd_generic_link_read_symbols (bfd *abfd)
807 if (bfd_get_outsymbols (abfd) == NULL)
812 symsize = bfd_get_symtab_upper_bound (abfd);
815 bfd_get_outsymbols (abfd) = (struct bfd_symbol **) bfd_alloc (abfd,
817 if (bfd_get_outsymbols (abfd) == NULL && symsize != 0)
819 symcount = bfd_canonicalize_symtab (abfd, bfd_get_outsymbols (abfd));
822 bfd_get_symcount (abfd) = symcount;
828 /* Generic function to add symbols to from an object file to the
829 global hash table. This version does not automatically collect
830 constructors by name. */
833 _bfd_generic_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
835 return generic_link_add_symbols (abfd, info, FALSE);
838 /* Generic function to add symbols from an object file to the global
839 hash table. This version automatically collects constructors by
840 name, as the collect2 program does. It should be used for any
841 target which does not provide some other mechanism for setting up
842 constructors and destructors; these are approximately those targets
843 for which gcc uses collect2 and do not support stabs. */
846 _bfd_generic_link_add_symbols_collect (bfd *abfd, struct bfd_link_info *info)
848 return generic_link_add_symbols (abfd, info, TRUE);
851 /* Indicate that we are only retrieving symbol values from this
852 section. We want the symbols to act as though the values in the
853 file are absolute. */
856 _bfd_generic_link_just_syms (asection *sec,
857 struct bfd_link_info *info ATTRIBUTE_UNUSED)
859 sec->sec_info_type = SEC_INFO_TYPE_JUST_SYMS;
860 sec->output_section = bfd_abs_section_ptr;
861 sec->output_offset = sec->vma;
864 /* Copy the symbol type and other attributes for a linker script
865 assignment from HSRC to HDEST.
866 The default implementation does nothing. */
868 _bfd_generic_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
869 struct bfd_link_hash_entry *hdest ATTRIBUTE_UNUSED,
870 struct bfd_link_hash_entry *hsrc ATTRIBUTE_UNUSED)
874 /* Add symbols from an object file to the global hash table. */
877 generic_link_add_symbols (bfd *abfd,
878 struct bfd_link_info *info,
883 switch (bfd_get_format (abfd))
886 ret = generic_link_add_object_symbols (abfd, info, collect);
889 ret = (_bfd_generic_link_add_archive_symbols
892 ? generic_link_check_archive_element_collect
893 : generic_link_check_archive_element_no_collect)));
896 bfd_set_error (bfd_error_wrong_format);
903 /* Add symbols from an object file to the global hash table. */
906 generic_link_add_object_symbols (bfd *abfd,
907 struct bfd_link_info *info,
910 bfd_size_type symcount;
911 struct bfd_symbol **outsyms;
913 if (!bfd_generic_link_read_symbols (abfd))
915 symcount = _bfd_generic_link_get_symcount (abfd);
916 outsyms = _bfd_generic_link_get_symbols (abfd);
917 return generic_link_add_symbol_list (abfd, info, symcount, outsyms, collect);
920 /* We build a hash table of all symbols defined in an archive. */
922 /* An archive symbol may be defined by multiple archive elements.
923 This linked list is used to hold the elements. */
927 struct archive_list *next;
931 /* An entry in an archive hash table. */
933 struct archive_hash_entry
935 struct bfd_hash_entry root;
936 /* Where the symbol is defined. */
937 struct archive_list *defs;
940 /* An archive hash table itself. */
942 struct archive_hash_table
944 struct bfd_hash_table table;
947 /* Create a new entry for an archive hash table. */
949 static struct bfd_hash_entry *
950 archive_hash_newfunc (struct bfd_hash_entry *entry,
951 struct bfd_hash_table *table,
954 struct archive_hash_entry *ret = (struct archive_hash_entry *) entry;
956 /* Allocate the structure if it has not already been allocated by a
959 ret = (struct archive_hash_entry *)
960 bfd_hash_allocate (table, sizeof (struct archive_hash_entry));
964 /* Call the allocation method of the superclass. */
965 ret = ((struct archive_hash_entry *)
966 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
970 /* Initialize the local fields. */
977 /* Initialize an archive hash table. */
980 archive_hash_table_init
981 (struct archive_hash_table *table,
982 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
983 struct bfd_hash_table *,
985 unsigned int entsize)
987 return bfd_hash_table_init (&table->table, newfunc, entsize);
990 /* Look up an entry in an archive hash table. */
992 #define archive_hash_lookup(t, string, create, copy) \
993 ((struct archive_hash_entry *) \
994 bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
996 /* Allocate space in an archive hash table. */
998 #define archive_hash_allocate(t, size) bfd_hash_allocate (&(t)->table, (size))
1000 /* Free an archive hash table. */
1002 #define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table)
1004 /* Generic function to add symbols from an archive file to the global
1005 hash file. This function presumes that the archive symbol table
1006 has already been read in (this is normally done by the
1007 bfd_check_format entry point). It looks through the undefined and
1008 common symbols and searches the archive symbol table for them. If
1009 it finds an entry, it includes the associated object file in the
1012 The old linker looked through the archive symbol table for
1013 undefined symbols. We do it the other way around, looking through
1014 undefined symbols for symbols defined in the archive. The
1015 advantage of the newer scheme is that we only have to look through
1016 the list of undefined symbols once, whereas the old method had to
1017 re-search the symbol table each time a new object file was added.
1019 The CHECKFN argument is used to see if an object file should be
1020 included. CHECKFN should set *PNEEDED to TRUE if the object file
1021 should be included, and must also call the bfd_link_info
1022 add_archive_element callback function and handle adding the symbols
1023 to the global hash table. CHECKFN must notice if the callback
1024 indicates a substitute BFD, and arrange to add those symbols instead
1025 if it does so. CHECKFN should only return FALSE if some sort of
1028 For some formats, such as a.out, it is possible to look through an
1029 object file but not actually include it in the link. The
1030 archive_pass field in a BFD is used to avoid checking the symbols
1031 of an object files too many times. When an object is included in
1032 the link, archive_pass is set to -1. If an object is scanned but
1033 not included, archive_pass is set to the pass number. The pass
1034 number is incremented each time a new object file is included. The
1035 pass number is used because when a new object file is included it
1036 may create new undefined symbols which cause a previously examined
1037 object file to be included. */
1040 _bfd_generic_link_add_archive_symbols
1042 struct bfd_link_info *info,
1043 bfd_boolean (*checkfn) (bfd *, struct bfd_link_info *, bfd_boolean *))
1047 register carsym *arsym;
1049 struct archive_hash_table arsym_hash;
1051 struct bfd_link_hash_entry **pundef;
1053 if (! bfd_has_map (abfd))
1055 /* An empty archive is a special case. */
1056 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
1058 bfd_set_error (bfd_error_no_armap);
1062 arsyms = bfd_ardata (abfd)->symdefs;
1063 arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
1065 /* In order to quickly determine whether an symbol is defined in
1066 this archive, we build a hash table of the symbols. */
1067 if (! archive_hash_table_init (&arsym_hash, archive_hash_newfunc,
1068 sizeof (struct archive_hash_entry)))
1070 for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
1072 struct archive_hash_entry *arh;
1073 struct archive_list *l, **pp;
1075 arh = archive_hash_lookup (&arsym_hash, arsym->name, TRUE, FALSE);
1078 l = ((struct archive_list *)
1079 archive_hash_allocate (&arsym_hash, sizeof (struct archive_list)));
1083 for (pp = &arh->defs; *pp != NULL; pp = &(*pp)->next)
1089 /* The archive_pass field in the archive itself is used to
1090 initialize PASS, sine we may search the same archive multiple
1092 pass = abfd->archive_pass + 1;
1094 /* New undefined symbols are added to the end of the list, so we
1095 only need to look through it once. */
1096 pundef = &info->hash->undefs;
1097 while (*pundef != NULL)
1099 struct bfd_link_hash_entry *h;
1100 struct archive_hash_entry *arh;
1101 struct archive_list *l;
1105 /* When a symbol is defined, it is not necessarily removed from
1107 if (h->type != bfd_link_hash_undefined
1108 && h->type != bfd_link_hash_common)
1110 /* Remove this entry from the list, for general cleanliness
1111 and because we are going to look through the list again
1112 if we search any more libraries. We can't remove the
1113 entry if it is the tail, because that would lose any
1114 entries we add to the list later on (it would also cause
1115 us to lose track of whether the symbol has been
1117 if (*pundef != info->hash->undefs_tail)
1118 *pundef = (*pundef)->u.undef.next;
1120 pundef = &(*pundef)->u.undef.next;
1124 /* Look for this symbol in the archive symbol map. */
1125 arh = archive_hash_lookup (&arsym_hash, h->root.string, FALSE, FALSE);
1128 /* If we haven't found the exact symbol we're looking for,
1129 let's look for its import thunk */
1130 if (info->pei386_auto_import)
1132 bfd_size_type amt = strlen (h->root.string) + 10;
1133 char *buf = (char *) bfd_malloc (amt);
1137 sprintf (buf, "__imp_%s", h->root.string);
1138 arh = archive_hash_lookup (&arsym_hash, buf, FALSE, FALSE);
1143 pundef = &(*pundef)->u.undef.next;
1147 /* Look at all the objects which define this symbol. */
1148 for (l = arh->defs; l != NULL; l = l->next)
1153 /* If the symbol has gotten defined along the way, quit. */
1154 if (h->type != bfd_link_hash_undefined
1155 && h->type != bfd_link_hash_common)
1158 element = bfd_get_elt_at_index (abfd, l->indx);
1159 if (element == NULL)
1162 /* If we've already included this element, or if we've
1163 already checked it on this pass, continue. */
1164 if (element->archive_pass == -1
1165 || element->archive_pass == pass)
1168 /* If we can't figure this element out, just ignore it. */
1169 if (! bfd_check_format (element, bfd_object))
1171 element->archive_pass = -1;
1175 /* CHECKFN will see if this element should be included, and
1176 go ahead and include it if appropriate. */
1177 if (! (*checkfn) (element, info, &needed))
1181 element->archive_pass = pass;
1184 element->archive_pass = -1;
1186 /* Increment the pass count to show that we may need to
1187 recheck object files which were already checked. */
1192 pundef = &(*pundef)->u.undef.next;
1195 archive_hash_table_free (&arsym_hash);
1197 /* Save PASS in case we are called again. */
1198 abfd->archive_pass = pass;
1203 archive_hash_table_free (&arsym_hash);
1207 /* See if we should include an archive element. This version is used
1208 when we do not want to automatically collect constructors based on
1209 the symbol name, presumably because we have some other mechanism
1210 for finding them. */
1213 generic_link_check_archive_element_no_collect (
1215 struct bfd_link_info *info,
1216 bfd_boolean *pneeded)
1218 return generic_link_check_archive_element (abfd, info, pneeded, FALSE);
1221 /* See if we should include an archive element. This version is used
1222 when we want to automatically collect constructors based on the
1223 symbol name, as collect2 does. */
1226 generic_link_check_archive_element_collect (bfd *abfd,
1227 struct bfd_link_info *info,
1228 bfd_boolean *pneeded)
1230 return generic_link_check_archive_element (abfd, info, pneeded, TRUE);
1233 /* See if we should include an archive element. Optionally collect
1237 generic_link_check_archive_element (bfd *abfd,
1238 struct bfd_link_info *info,
1239 bfd_boolean *pneeded,
1240 bfd_boolean collect)
1242 asymbol **pp, **ppend;
1246 if (!bfd_generic_link_read_symbols (abfd))
1249 pp = _bfd_generic_link_get_symbols (abfd);
1250 ppend = pp + _bfd_generic_link_get_symcount (abfd);
1251 for (; pp < ppend; pp++)
1254 struct bfd_link_hash_entry *h;
1258 /* We are only interested in globally visible symbols. */
1259 if (! bfd_is_com_section (p->section)
1260 && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0)
1263 /* We are only interested if we know something about this
1264 symbol, and it is undefined or common. An undefined weak
1265 symbol (type bfd_link_hash_undefweak) is not considered to be
1266 a reference when pulling files out of an archive. See the
1267 SVR4 ABI, p. 4-27. */
1268 h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), FALSE,
1271 || (h->type != bfd_link_hash_undefined
1272 && h->type != bfd_link_hash_common))
1275 /* P is a symbol we are looking for. */
1277 if (! bfd_is_com_section (p->section))
1279 bfd_size_type symcount;
1283 /* This object file defines this symbol, so pull it in. */
1284 if (!(*info->callbacks
1285 ->add_archive_element) (info, abfd, bfd_asymbol_name (p),
1288 /* Potentially, the add_archive_element hook may have set a
1289 substitute BFD for us. */
1291 && !bfd_generic_link_read_symbols (abfd))
1293 symcount = _bfd_generic_link_get_symcount (abfd);
1294 symbols = _bfd_generic_link_get_symbols (abfd);
1295 if (! generic_link_add_symbol_list (abfd, info, symcount,
1302 /* P is a common symbol. */
1304 if (h->type == bfd_link_hash_undefined)
1310 symbfd = h->u.undef.abfd;
1313 /* This symbol was created as undefined from outside
1314 BFD. We assume that we should link in the object
1315 file. This is for the -u option in the linker. */
1316 if (!(*info->callbacks
1317 ->add_archive_element) (info, abfd, bfd_asymbol_name (p),
1320 /* Potentially, the add_archive_element hook may have set a
1321 substitute BFD for us. But no symbols are going to get
1322 registered by anything we're returning to from here. */
1327 /* Turn the symbol into a common symbol but do not link in
1328 the object file. This is how a.out works. Object
1329 formats that require different semantics must implement
1330 this function differently. This symbol is already on the
1331 undefs list. We add the section to a common section
1332 attached to symbfd to ensure that it is in a BFD which
1333 will be linked in. */
1334 h->type = bfd_link_hash_common;
1335 h->u.c.p = (struct bfd_link_hash_common_entry *)
1336 bfd_hash_allocate (&info->hash->table,
1337 sizeof (struct bfd_link_hash_common_entry));
1338 if (h->u.c.p == NULL)
1341 size = bfd_asymbol_value (p);
1344 power = bfd_log2 (size);
1347 h->u.c.p->alignment_power = power;
1349 if (p->section == bfd_com_section_ptr)
1350 h->u.c.p->section = bfd_make_section_old_way (symbfd, "COMMON");
1352 h->u.c.p->section = bfd_make_section_old_way (symbfd,
1354 h->u.c.p->section->flags |= SEC_ALLOC;
1358 /* Adjust the size of the common symbol if necessary. This
1359 is how a.out works. Object formats that require
1360 different semantics must implement this function
1362 if (bfd_asymbol_value (p) > h->u.c.size)
1363 h->u.c.size = bfd_asymbol_value (p);
1367 /* This archive element is not needed. */
1371 /* Add the symbols from an object file to the global hash table. ABFD
1372 is the object file. INFO is the linker information. SYMBOL_COUNT
1373 is the number of symbols. SYMBOLS is the list of symbols. COLLECT
1374 is TRUE if constructors should be automatically collected by name
1375 as is done by collect2. */
1378 generic_link_add_symbol_list (bfd *abfd,
1379 struct bfd_link_info *info,
1380 bfd_size_type symbol_count,
1382 bfd_boolean collect)
1384 asymbol **pp, **ppend;
1387 ppend = symbols + symbol_count;
1388 for (; pp < ppend; pp++)
1394 if ((p->flags & (BSF_INDIRECT
1399 || bfd_is_und_section (bfd_get_section (p))
1400 || bfd_is_com_section (bfd_get_section (p))
1401 || bfd_is_ind_section (bfd_get_section (p)))
1405 struct generic_link_hash_entry *h;
1406 struct bfd_link_hash_entry *bh;
1408 string = name = bfd_asymbol_name (p);
1409 if (((p->flags & BSF_INDIRECT) != 0
1410 || bfd_is_ind_section (p->section))
1414 string = bfd_asymbol_name (*pp);
1416 else if ((p->flags & BSF_WARNING) != 0
1419 /* The name of P is actually the warning string, and the
1420 next symbol is the one to warn about. */
1422 name = bfd_asymbol_name (*pp);
1426 if (! (_bfd_generic_link_add_one_symbol
1427 (info, abfd, name, p->flags, bfd_get_section (p),
1428 p->value, string, FALSE, collect, &bh)))
1430 h = (struct generic_link_hash_entry *) bh;
1432 /* If this is a constructor symbol, and the linker didn't do
1433 anything with it, then we want to just pass the symbol
1434 through to the output file. This will happen when
1436 if ((p->flags & BSF_CONSTRUCTOR) != 0
1437 && (h == NULL || h->root.type == bfd_link_hash_new))
1443 /* Save the BFD symbol so that we don't lose any backend
1444 specific information that may be attached to it. We only
1445 want this one if it gives more information than the
1446 existing one; we don't want to replace a defined symbol
1447 with an undefined one. This routine may be called with a
1448 hash table other than the generic hash table, so we only
1449 do this if we are certain that the hash table is a
1451 if (info->output_bfd->xvec == abfd->xvec)
1454 || (! bfd_is_und_section (bfd_get_section (p))
1455 && (! bfd_is_com_section (bfd_get_section (p))
1456 || bfd_is_und_section (bfd_get_section (h->sym)))))
1459 /* BSF_OLD_COMMON is a hack to support COFF reloc
1460 reading, and it should go away when the COFF
1461 linker is switched to the new version. */
1462 if (bfd_is_com_section (bfd_get_section (p)))
1463 p->flags |= BSF_OLD_COMMON;
1467 /* Store a back pointer from the symbol to the hash
1468 table entry for the benefit of relaxation code until
1469 it gets rewritten to not use asymbol structures.
1470 Setting this is also used to check whether these
1471 symbols were set up by the generic linker. */
1479 /* We use a state table to deal with adding symbols from an object
1480 file. The first index into the state table describes the symbol
1481 from the object file. The second index into the state table is the
1482 type of the symbol in the hash table. */
1484 /* The symbol from the object file is turned into one of these row
1489 UNDEF_ROW, /* Undefined. */
1490 UNDEFW_ROW, /* Weak undefined. */
1491 DEF_ROW, /* Defined. */
1492 DEFW_ROW, /* Weak defined. */
1493 COMMON_ROW, /* Common. */
1494 INDR_ROW, /* Indirect. */
1495 WARN_ROW, /* Warning. */
1496 SET_ROW /* Member of set. */
1499 /* apparently needed for Hitachi 3050R(HI-UX/WE2)? */
1502 /* The actions to take in the state table. */
1507 UND, /* Mark symbol undefined. */
1508 WEAK, /* Mark symbol weak undefined. */
1509 DEF, /* Mark symbol defined. */
1510 DEFW, /* Mark symbol weak defined. */
1511 COM, /* Mark symbol common. */
1512 REF, /* Mark defined symbol referenced. */
1513 CREF, /* Possibly warn about common reference to defined symbol. */
1514 CDEF, /* Define existing common symbol. */
1515 NOACT, /* No action. */
1516 BIG, /* Mark symbol common using largest size. */
1517 MDEF, /* Multiple definition error. */
1518 MIND, /* Multiple indirect symbols. */
1519 IND, /* Make indirect symbol. */
1520 CIND, /* Make indirect symbol from existing common symbol. */
1521 SET, /* Add value to set. */
1522 MWARN, /* Make warning symbol. */
1523 WARN, /* Issue warning. */
1524 CWARN, /* Warn if referenced, else MWARN. */
1525 CYCLE, /* Repeat with symbol pointed to. */
1526 REFC, /* Mark indirect symbol referenced and then CYCLE. */
1527 WARNC /* Issue warning and then CYCLE. */
1530 /* The state table itself. The first index is a link_row and the
1531 second index is a bfd_link_hash_type. */
1533 static const enum link_action link_action[8][8] =
1535 /* current\prev new undef undefw def defw com indr warn */
1536 /* UNDEF_ROW */ {UND, NOACT, UND, REF, REF, NOACT, REFC, WARNC },
1537 /* UNDEFW_ROW */ {WEAK, NOACT, NOACT, REF, REF, NOACT, REFC, WARNC },
1538 /* DEF_ROW */ {DEF, DEF, DEF, MDEF, DEF, CDEF, MDEF, CYCLE },
1539 /* DEFW_ROW */ {DEFW, DEFW, DEFW, NOACT, NOACT, NOACT, NOACT, CYCLE },
1540 /* COMMON_ROW */ {COM, COM, COM, CREF, COM, BIG, REFC, WARNC },
1541 /* INDR_ROW */ {IND, IND, IND, MDEF, IND, CIND, MIND, CYCLE },
1542 /* WARN_ROW */ {MWARN, WARN, WARN, CWARN, CWARN, WARN, CWARN, NOACT },
1543 /* SET_ROW */ {SET, SET, SET, SET, SET, SET, CYCLE, CYCLE }
1546 /* Most of the entries in the LINK_ACTION table are straightforward,
1547 but a few are somewhat subtle.
1549 A reference to an indirect symbol (UNDEF_ROW/indr or
1550 UNDEFW_ROW/indr) is counted as a reference both to the indirect
1551 symbol and to the symbol the indirect symbol points to.
1553 A reference to a warning symbol (UNDEF_ROW/warn or UNDEFW_ROW/warn)
1554 causes the warning to be issued.
1556 A common definition of an indirect symbol (COMMON_ROW/indr) is
1557 treated as a multiple definition error. Likewise for an indirect
1558 definition of a common symbol (INDR_ROW/com).
1560 An indirect definition of a warning (INDR_ROW/warn) does not cause
1561 the warning to be issued.
1563 If a warning is created for an indirect symbol (WARN_ROW/indr) no
1564 warning is created for the symbol the indirect symbol points to.
1566 Adding an entry to a set does not count as a reference to a set,
1567 and no warning is issued (SET_ROW/warn). */
1569 /* Return the BFD in which a hash entry has been defined, if known. */
1572 hash_entry_bfd (struct bfd_link_hash_entry *h)
1574 while (h->type == bfd_link_hash_warning)
1580 case bfd_link_hash_undefined:
1581 case bfd_link_hash_undefweak:
1582 return h->u.undef.abfd;
1583 case bfd_link_hash_defined:
1584 case bfd_link_hash_defweak:
1585 return h->u.def.section->owner;
1586 case bfd_link_hash_common:
1587 return h->u.c.p->section->owner;
1592 /* Add a symbol to the global hash table.
1593 ABFD is the BFD the symbol comes from.
1594 NAME is the name of the symbol.
1595 FLAGS is the BSF_* bits associated with the symbol.
1596 SECTION is the section in which the symbol is defined; this may be
1597 bfd_und_section_ptr or bfd_com_section_ptr.
1598 VALUE is the value of the symbol, relative to the section.
1599 STRING is used for either an indirect symbol, in which case it is
1600 the name of the symbol to indirect to, or a warning symbol, in
1601 which case it is the warning string.
1602 COPY is TRUE if NAME or STRING must be copied into locally
1603 allocated memory if they need to be saved.
1604 COLLECT is TRUE if we should automatically collect gcc constructor
1605 or destructor names as collect2 does.
1606 HASHP, if not NULL, is a place to store the created hash table
1607 entry; if *HASHP is not NULL, the caller has already looked up
1608 the hash table entry, and stored it in *HASHP. */
1611 _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
1619 bfd_boolean collect,
1620 struct bfd_link_hash_entry **hashp)
1623 struct bfd_link_hash_entry *h;
1626 BFD_ASSERT (section != NULL);
1628 if (bfd_is_ind_section (section)
1629 || (flags & BSF_INDIRECT) != 0)
1631 else if ((flags & BSF_WARNING) != 0)
1633 else if ((flags & BSF_CONSTRUCTOR) != 0)
1635 else if (bfd_is_und_section (section))
1637 if ((flags & BSF_WEAK) != 0)
1642 else if ((flags & BSF_WEAK) != 0)
1644 else if (bfd_is_com_section (section))
1647 if (strcmp (name, "__gnu_lto_slim") == 0)
1648 (*_bfd_error_handler)
1649 (_("%s: plugin needed to handle lto object"),
1650 bfd_get_filename (abfd));
1655 if (hashp != NULL && *hashp != NULL)
1659 if (row == UNDEF_ROW || row == UNDEFW_ROW)
1660 h = bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE);
1662 h = bfd_link_hash_lookup (info->hash, name, TRUE, copy, FALSE);
1671 if (info->notice_all
1672 || (info->notice_hash != NULL
1673 && bfd_hash_lookup (info->notice_hash, name, FALSE, FALSE) != NULL))
1675 if (! (*info->callbacks->notice) (info, h,
1676 abfd, section, value, flags, string))
1685 enum link_action action;
1688 action = link_action[(int) row][(int) h->type];
1699 /* Make a new undefined symbol. */
1700 h->type = bfd_link_hash_undefined;
1701 h->u.undef.abfd = abfd;
1702 bfd_link_add_undef (info->hash, h);
1706 /* Make a new weak undefined symbol. */
1707 h->type = bfd_link_hash_undefweak;
1708 h->u.undef.abfd = abfd;
1712 /* We have found a definition for a symbol which was
1713 previously common. */
1714 BFD_ASSERT (h->type == bfd_link_hash_common);
1715 if (! ((*info->callbacks->multiple_common)
1716 (info, h, abfd, bfd_link_hash_defined, 0)))
1722 enum bfd_link_hash_type oldtype;
1724 /* Define a symbol. */
1727 h->type = bfd_link_hash_defweak;
1729 h->type = bfd_link_hash_defined;
1730 h->u.def.section = section;
1731 h->u.def.value = value;
1733 /* If we have been asked to, we act like collect2 and
1734 identify all functions that might be global
1735 constructors and destructors and pass them up in a
1736 callback. We only do this for certain object file
1737 types, since many object file types can handle this
1739 if (collect && name[0] == '_')
1743 /* A constructor or destructor name starts like this:
1744 _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
1745 the second are the same character (we accept any
1746 character there, in case a new object file format
1747 comes along with even worse naming restrictions). */
1749 #define CONS_PREFIX "GLOBAL_"
1750 #define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
1755 if (s[0] == 'G' && CONST_STRNEQ (s, CONS_PREFIX))
1759 c = s[CONS_PREFIX_LEN + 1];
1760 if ((c == 'I' || c == 'D')
1761 && s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
1763 /* If this is a definition of a symbol which
1764 was previously weakly defined, we are in
1765 trouble. We have already added a
1766 constructor entry for the weak defined
1767 symbol, and now we are trying to add one
1768 for the new symbol. Fortunately, this case
1769 should never arise in practice. */
1770 if (oldtype == bfd_link_hash_defweak)
1773 if (! ((*info->callbacks->constructor)
1775 h->root.string, abfd, section, value)))
1785 /* We have found a common definition for a symbol. */
1786 if (h->type == bfd_link_hash_new)
1787 bfd_link_add_undef (info->hash, h);
1788 h->type = bfd_link_hash_common;
1789 h->u.c.p = (struct bfd_link_hash_common_entry *)
1790 bfd_hash_allocate (&info->hash->table,
1791 sizeof (struct bfd_link_hash_common_entry));
1792 if (h->u.c.p == NULL)
1795 h->u.c.size = value;
1797 /* Select a default alignment based on the size. This may
1798 be overridden by the caller. */
1802 power = bfd_log2 (value);
1805 h->u.c.p->alignment_power = power;
1808 /* The section of a common symbol is only used if the common
1809 symbol is actually allocated. It basically provides a
1810 hook for the linker script to decide which output section
1811 the common symbols should be put in. In most cases, the
1812 section of a common symbol will be bfd_com_section_ptr,
1813 the code here will choose a common symbol section named
1814 "COMMON", and the linker script will contain *(COMMON) in
1815 the appropriate place. A few targets use separate common
1816 sections for small symbols, and they require special
1818 if (section == bfd_com_section_ptr)
1820 h->u.c.p->section = bfd_make_section_old_way (abfd, "COMMON");
1821 h->u.c.p->section->flags |= SEC_ALLOC;
1823 else if (section->owner != abfd)
1825 h->u.c.p->section = bfd_make_section_old_way (abfd,
1827 h->u.c.p->section->flags |= SEC_ALLOC;
1830 h->u.c.p->section = section;
1834 /* A reference to a defined symbol. */
1835 if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
1836 h->u.undef.next = h;
1840 /* We have found a common definition for a symbol which
1841 already had a common definition. Use the maximum of the
1842 two sizes, and use the section required by the larger symbol. */
1843 BFD_ASSERT (h->type == bfd_link_hash_common);
1844 if (! ((*info->callbacks->multiple_common)
1845 (info, h, abfd, bfd_link_hash_common, value)))
1847 if (value > h->u.c.size)
1851 h->u.c.size = value;
1853 /* Select a default alignment based on the size. This may
1854 be overridden by the caller. */
1855 power = bfd_log2 (value);
1858 h->u.c.p->alignment_power = power;
1860 /* Some systems have special treatment for small commons,
1861 hence we want to select the section used by the larger
1862 symbol. This makes sure the symbol does not go in a
1863 small common section if it is now too large. */
1864 if (section == bfd_com_section_ptr)
1867 = bfd_make_section_old_way (abfd, "COMMON");
1868 h->u.c.p->section->flags |= SEC_ALLOC;
1870 else if (section->owner != abfd)
1873 = bfd_make_section_old_way (abfd, section->name);
1874 h->u.c.p->section->flags |= SEC_ALLOC;
1877 h->u.c.p->section = section;
1882 /* We have found a common definition for a symbol which
1883 was already defined. */
1884 if (! ((*info->callbacks->multiple_common)
1885 (info, h, abfd, bfd_link_hash_common, value)))
1890 /* Multiple indirect symbols. This is OK if they both point
1891 to the same symbol. */
1892 if (strcmp (h->u.i.link->root.string, string) == 0)
1896 /* Handle a multiple definition. */
1897 if (! ((*info->callbacks->multiple_definition)
1898 (info, h, abfd, section, value)))
1903 /* Create an indirect symbol from an existing common symbol. */
1904 BFD_ASSERT (h->type == bfd_link_hash_common);
1905 if (! ((*info->callbacks->multiple_common)
1906 (info, h, abfd, bfd_link_hash_indirect, 0)))
1910 /* Create an indirect symbol. */
1912 struct bfd_link_hash_entry *inh;
1914 /* STRING is the name of the symbol we want to indirect
1916 inh = bfd_wrapped_link_hash_lookup (abfd, info, string, TRUE,
1920 if (inh->type == bfd_link_hash_indirect
1921 && inh->u.i.link == h)
1923 (*_bfd_error_handler)
1924 (_("%B: indirect symbol `%s' to `%s' is a loop"),
1925 abfd, name, string);
1926 bfd_set_error (bfd_error_invalid_operation);
1929 if (inh->type == bfd_link_hash_new)
1931 inh->type = bfd_link_hash_undefined;
1932 inh->u.undef.abfd = abfd;
1933 bfd_link_add_undef (info->hash, inh);
1936 /* If the indirect symbol has been referenced, we need to
1937 push the reference down to the symbol we are
1939 if (h->type != bfd_link_hash_new)
1945 h->type = bfd_link_hash_indirect;
1951 /* Add an entry to a set. */
1952 if (! (*info->callbacks->add_to_set) (info, h, BFD_RELOC_CTOR,
1953 abfd, section, value))
1958 /* Issue a warning and cycle. */
1959 if (h->u.i.warning != NULL)
1961 if (! (*info->callbacks->warning) (info, h->u.i.warning,
1962 h->root.string, abfd,
1965 /* Only issue a warning once. */
1966 h->u.i.warning = NULL;
1970 /* Try again with the referenced symbol. */
1976 /* A reference to an indirect symbol. */
1977 if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
1978 h->u.undef.next = h;
1984 /* Issue a warning. */
1985 if (! (*info->callbacks->warning) (info, string, h->root.string,
1986 hash_entry_bfd (h), NULL, 0))
1991 /* Warn if this symbol has been referenced already,
1992 otherwise add a warning. A symbol has been referenced if
1993 the u.undef.next field is not NULL, or it is the tail of the
1994 undefined symbol list. The REF case above helps to
1996 if (h->u.undef.next != NULL || info->hash->undefs_tail == h)
1998 if (! (*info->callbacks->warning) (info, string, h->root.string,
1999 hash_entry_bfd (h), NULL, 0))
2005 /* Make a warning symbol. */
2007 struct bfd_link_hash_entry *sub;
2009 /* STRING is the warning to give. */
2010 sub = ((struct bfd_link_hash_entry *)
2011 ((*info->hash->table.newfunc)
2012 (NULL, &info->hash->table, h->root.string)));
2016 sub->type = bfd_link_hash_warning;
2019 sub->u.i.warning = string;
2023 size_t len = strlen (string) + 1;
2025 w = (char *) bfd_hash_allocate (&info->hash->table, len);
2028 memcpy (w, string, len);
2029 sub->u.i.warning = w;
2032 bfd_hash_replace (&info->hash->table,
2033 (struct bfd_hash_entry *) h,
2034 (struct bfd_hash_entry *) sub);
2046 /* Generic final link routine. */
2049 _bfd_generic_final_link (bfd *abfd, struct bfd_link_info *info)
2053 struct bfd_link_order *p;
2055 struct generic_write_global_symbol_info wginfo;
2057 bfd_get_outsymbols (abfd) = NULL;
2058 bfd_get_symcount (abfd) = 0;
2061 /* Mark all sections which will be included in the output file. */
2062 for (o = abfd->sections; o != NULL; o = o->next)
2063 for (p = o->map_head.link_order; p != NULL; p = p->next)
2064 if (p->type == bfd_indirect_link_order)
2065 p->u.indirect.section->linker_mark = TRUE;
2067 /* Build the output symbol table. */
2068 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
2069 if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
2072 /* Accumulate the global symbols. */
2074 wginfo.output_bfd = abfd;
2075 wginfo.psymalloc = &outsymalloc;
2076 _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
2077 _bfd_generic_link_write_global_symbol,
2080 /* Make sure we have a trailing NULL pointer on OUTSYMBOLS. We
2081 shouldn't really need one, since we have SYMCOUNT, but some old
2082 code still expects one. */
2083 if (! generic_add_output_symbol (abfd, &outsymalloc, NULL))
2086 if (info->relocatable)
2088 /* Allocate space for the output relocs for each section. */
2089 for (o = abfd->sections; o != NULL; o = o->next)
2092 for (p = o->map_head.link_order; p != NULL; p = p->next)
2094 if (p->type == bfd_section_reloc_link_order
2095 || p->type == bfd_symbol_reloc_link_order)
2097 else if (p->type == bfd_indirect_link_order)
2099 asection *input_section;
2106 input_section = p->u.indirect.section;
2107 input_bfd = input_section->owner;
2108 relsize = bfd_get_reloc_upper_bound (input_bfd,
2112 relocs = (arelent **) bfd_malloc (relsize);
2113 if (!relocs && relsize != 0)
2115 symbols = _bfd_generic_link_get_symbols (input_bfd);
2116 reloc_count = bfd_canonicalize_reloc (input_bfd,
2121 if (reloc_count < 0)
2123 BFD_ASSERT ((unsigned long) reloc_count
2124 == input_section->reloc_count);
2125 o->reloc_count += reloc_count;
2128 if (o->reloc_count > 0)
2132 amt = o->reloc_count;
2133 amt *= sizeof (arelent *);
2134 o->orelocation = (struct reloc_cache_entry **) bfd_alloc (abfd, amt);
2135 if (!o->orelocation)
2137 o->flags |= SEC_RELOC;
2138 /* Reset the count so that it can be used as an index
2139 when putting in the output relocs. */
2145 /* Handle all the link order information for the sections. */
2146 for (o = abfd->sections; o != NULL; o = o->next)
2148 for (p = o->map_head.link_order; p != NULL; p = p->next)
2152 case bfd_section_reloc_link_order:
2153 case bfd_symbol_reloc_link_order:
2154 if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
2157 case bfd_indirect_link_order:
2158 if (! default_indirect_link_order (abfd, info, o, p, TRUE))
2162 if (! _bfd_default_link_order (abfd, info, o, p))
2172 /* Add an output symbol to the output BFD. */
2175 generic_add_output_symbol (bfd *output_bfd, size_t *psymalloc, asymbol *sym)
2177 if (bfd_get_symcount (output_bfd) >= *psymalloc)
2182 if (*psymalloc == 0)
2187 amt *= sizeof (asymbol *);
2188 newsyms = (asymbol **) bfd_realloc (bfd_get_outsymbols (output_bfd), amt);
2189 if (newsyms == NULL)
2191 bfd_get_outsymbols (output_bfd) = newsyms;
2194 bfd_get_outsymbols (output_bfd) [bfd_get_symcount (output_bfd)] = sym;
2196 ++ bfd_get_symcount (output_bfd);
2201 /* Handle the symbols for an input BFD. */
2204 _bfd_generic_link_output_symbols (bfd *output_bfd,
2206 struct bfd_link_info *info,
2212 if (!bfd_generic_link_read_symbols (input_bfd))
2215 /* Create a filename symbol if we are supposed to. */
2216 if (info->create_object_symbols_section != NULL)
2220 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
2222 if (sec->output_section == info->create_object_symbols_section)
2226 newsym = bfd_make_empty_symbol (input_bfd);
2229 newsym->name = input_bfd->filename;
2231 newsym->flags = BSF_LOCAL | BSF_FILE;
2232 newsym->section = sec;
2234 if (! generic_add_output_symbol (output_bfd, psymalloc,
2243 /* Adjust the values of the globally visible symbols, and write out
2245 sym_ptr = _bfd_generic_link_get_symbols (input_bfd);
2246 sym_end = sym_ptr + _bfd_generic_link_get_symcount (input_bfd);
2247 for (; sym_ptr < sym_end; sym_ptr++)
2250 struct generic_link_hash_entry *h;
2255 if ((sym->flags & (BSF_INDIRECT
2260 || bfd_is_und_section (bfd_get_section (sym))
2261 || bfd_is_com_section (bfd_get_section (sym))
2262 || bfd_is_ind_section (bfd_get_section (sym)))
2264 if (sym->udata.p != NULL)
2265 h = (struct generic_link_hash_entry *) sym->udata.p;
2266 else if ((sym->flags & BSF_CONSTRUCTOR) != 0)
2268 /* This case normally means that the main linker code
2269 deliberately ignored this constructor symbol. We
2270 should just pass it through. This will screw up if
2271 the constructor symbol is from a different,
2272 non-generic, object file format, but the case will
2273 only arise when linking with -r, which will probably
2274 fail anyhow, since there will be no way to represent
2275 the relocs in the output format being used. */
2278 else if (bfd_is_und_section (bfd_get_section (sym)))
2279 h = ((struct generic_link_hash_entry *)
2280 bfd_wrapped_link_hash_lookup (output_bfd, info,
2281 bfd_asymbol_name (sym),
2282 FALSE, FALSE, TRUE));
2284 h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
2285 bfd_asymbol_name (sym),
2286 FALSE, FALSE, TRUE);
2290 /* Force all references to this symbol to point to
2291 the same area in memory. It is possible that
2292 this routine will be called with a hash table
2293 other than a generic hash table, so we double
2295 if (info->output_bfd->xvec == input_bfd->xvec)
2298 *sym_ptr = sym = h->sym;
2301 switch (h->root.type)
2304 case bfd_link_hash_new:
2306 case bfd_link_hash_undefined:
2308 case bfd_link_hash_undefweak:
2309 sym->flags |= BSF_WEAK;
2311 case bfd_link_hash_indirect:
2312 h = (struct generic_link_hash_entry *) h->root.u.i.link;
2314 case bfd_link_hash_defined:
2315 sym->flags |= BSF_GLOBAL;
2316 sym->flags &=~ BSF_CONSTRUCTOR;
2317 sym->value = h->root.u.def.value;
2318 sym->section = h->root.u.def.section;
2320 case bfd_link_hash_defweak:
2321 sym->flags |= BSF_WEAK;
2322 sym->flags &=~ BSF_CONSTRUCTOR;
2323 sym->value = h->root.u.def.value;
2324 sym->section = h->root.u.def.section;
2326 case bfd_link_hash_common:
2327 sym->value = h->root.u.c.size;
2328 sym->flags |= BSF_GLOBAL;
2329 if (! bfd_is_com_section (sym->section))
2331 BFD_ASSERT (bfd_is_und_section (sym->section));
2332 sym->section = bfd_com_section_ptr;
2334 /* We do not set the section of the symbol to
2335 h->root.u.c.p->section. That value was saved so
2336 that we would know where to allocate the symbol
2337 if it was defined. In this case the type is
2338 still bfd_link_hash_common, so we did not define
2339 it, so we do not want to use that section. */
2345 /* This switch is straight from the old code in
2346 write_file_locals in ldsym.c. */
2347 if (info->strip == strip_all
2348 || (info->strip == strip_some
2349 && bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym),
2350 FALSE, FALSE) == NULL))
2352 else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
2354 /* If this symbol is marked as occurring now, rather
2355 than at the end, output it now. This is used for
2356 COFF C_EXT FCN symbols. FIXME: There must be a
2358 if (bfd_asymbol_bfd (sym) == input_bfd
2359 && (sym->flags & BSF_NOT_AT_END) != 0)
2364 else if (bfd_is_ind_section (sym->section))
2366 else if ((sym->flags & BSF_DEBUGGING) != 0)
2368 if (info->strip == strip_none)
2373 else if (bfd_is_und_section (sym->section)
2374 || bfd_is_com_section (sym->section))
2376 else if ((sym->flags & BSF_LOCAL) != 0)
2378 if ((sym->flags & BSF_WARNING) != 0)
2382 switch (info->discard)
2388 case discard_sec_merge:
2390 if (info->relocatable
2391 || ! (sym->section->flags & SEC_MERGE))
2395 if (bfd_is_local_label (input_bfd, sym))
2406 else if ((sym->flags & BSF_CONSTRUCTOR))
2408 if (info->strip != strip_all)
2413 else if (sym->flags == 0
2414 && (sym->section->owner->flags & BFD_PLUGIN) != 0)
2415 /* LTO doesn't set symbol information. We get here with the
2416 generic linker for a symbol that was "common" but no longer
2417 needs to be global. */
2422 /* If this symbol is in a section which is not being included
2423 in the output file, then we don't want to output the
2425 if (!bfd_is_abs_section (sym->section)
2426 && bfd_section_removed_from_list (output_bfd,
2427 sym->section->output_section))
2432 if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
2442 /* Set the section and value of a generic BFD symbol based on a linker
2443 hash table entry. */
2446 set_symbol_from_hash (asymbol *sym, struct bfd_link_hash_entry *h)
2453 case bfd_link_hash_new:
2454 /* This can happen when a constructor symbol is seen but we are
2455 not building constructors. */
2456 if (sym->section != NULL)
2458 BFD_ASSERT ((sym->flags & BSF_CONSTRUCTOR) != 0);
2462 sym->flags |= BSF_CONSTRUCTOR;
2463 sym->section = bfd_abs_section_ptr;
2467 case bfd_link_hash_undefined:
2468 sym->section = bfd_und_section_ptr;
2471 case bfd_link_hash_undefweak:
2472 sym->section = bfd_und_section_ptr;
2474 sym->flags |= BSF_WEAK;
2476 case bfd_link_hash_defined:
2477 sym->section = h->u.def.section;
2478 sym->value = h->u.def.value;
2480 case bfd_link_hash_defweak:
2481 sym->flags |= BSF_WEAK;
2482 sym->section = h->u.def.section;
2483 sym->value = h->u.def.value;
2485 case bfd_link_hash_common:
2486 sym->value = h->u.c.size;
2487 if (sym->section == NULL)
2488 sym->section = bfd_com_section_ptr;
2489 else if (! bfd_is_com_section (sym->section))
2491 BFD_ASSERT (bfd_is_und_section (sym->section));
2492 sym->section = bfd_com_section_ptr;
2494 /* Do not set the section; see _bfd_generic_link_output_symbols. */
2496 case bfd_link_hash_indirect:
2497 case bfd_link_hash_warning:
2498 /* FIXME: What should we do here? */
2503 /* Write out a global symbol, if it hasn't already been written out.
2504 This is called for each symbol in the hash table. */
2507 _bfd_generic_link_write_global_symbol (struct generic_link_hash_entry *h,
2510 struct generic_write_global_symbol_info *wginfo =
2511 (struct generic_write_global_symbol_info *) data;
2519 if (wginfo->info->strip == strip_all
2520 || (wginfo->info->strip == strip_some
2521 && bfd_hash_lookup (wginfo->info->keep_hash, h->root.root.string,
2522 FALSE, FALSE) == NULL))
2529 sym = bfd_make_empty_symbol (wginfo->output_bfd);
2532 sym->name = h->root.root.string;
2536 set_symbol_from_hash (sym, &h->root);
2538 sym->flags |= BSF_GLOBAL;
2540 if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
2543 /* FIXME: No way to return failure. */
2550 /* Create a relocation. */
2553 _bfd_generic_reloc_link_order (bfd *abfd,
2554 struct bfd_link_info *info,
2556 struct bfd_link_order *link_order)
2560 if (! info->relocatable)
2562 if (sec->orelocation == NULL)
2565 r = (arelent *) bfd_alloc (abfd, sizeof (arelent));
2569 r->address = link_order->offset;
2570 r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
2573 bfd_set_error (bfd_error_bad_value);
2577 /* Get the symbol to use for the relocation. */
2578 if (link_order->type == bfd_section_reloc_link_order)
2579 r->sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
2582 struct generic_link_hash_entry *h;
2584 h = ((struct generic_link_hash_entry *)
2585 bfd_wrapped_link_hash_lookup (abfd, info,
2586 link_order->u.reloc.p->u.name,
2587 FALSE, FALSE, TRUE));
2591 if (! ((*info->callbacks->unattached_reloc)
2592 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
2594 bfd_set_error (bfd_error_bad_value);
2597 r->sym_ptr_ptr = &h->sym;
2600 /* If this is an inplace reloc, write the addend to the object file.
2601 Otherwise, store it in the reloc addend. */
2602 if (! r->howto->partial_inplace)
2603 r->addend = link_order->u.reloc.p->addend;
2607 bfd_reloc_status_type rstat;
2612 size = bfd_get_reloc_size (r->howto);
2613 buf = (bfd_byte *) bfd_zmalloc (size);
2616 rstat = _bfd_relocate_contents (r->howto, abfd,
2617 (bfd_vma) link_order->u.reloc.p->addend,
2624 case bfd_reloc_outofrange:
2626 case bfd_reloc_overflow:
2627 if (! ((*info->callbacks->reloc_overflow)
2629 (link_order->type == bfd_section_reloc_link_order
2630 ? bfd_section_name (abfd, link_order->u.reloc.p->u.section)
2631 : link_order->u.reloc.p->u.name),
2632 r->howto->name, link_order->u.reloc.p->addend,
2640 loc = link_order->offset * bfd_octets_per_byte (abfd);
2641 ok = bfd_set_section_contents (abfd, sec, buf, loc, size);
2649 sec->orelocation[sec->reloc_count] = r;
2655 /* Allocate a new link_order for a section. */
2657 struct bfd_link_order *
2658 bfd_new_link_order (bfd *abfd, asection *section)
2660 bfd_size_type amt = sizeof (struct bfd_link_order);
2661 struct bfd_link_order *new_lo;
2663 new_lo = (struct bfd_link_order *) bfd_zalloc (abfd, amt);
2667 new_lo->type = bfd_undefined_link_order;
2669 if (section->map_tail.link_order != NULL)
2670 section->map_tail.link_order->next = new_lo;
2672 section->map_head.link_order = new_lo;
2673 section->map_tail.link_order = new_lo;
2678 /* Default link order processing routine. Note that we can not handle
2679 the reloc_link_order types here, since they depend upon the details
2680 of how the particular backends generates relocs. */
2683 _bfd_default_link_order (bfd *abfd,
2684 struct bfd_link_info *info,
2686 struct bfd_link_order *link_order)
2688 switch (link_order->type)
2690 case bfd_undefined_link_order:
2691 case bfd_section_reloc_link_order:
2692 case bfd_symbol_reloc_link_order:
2695 case bfd_indirect_link_order:
2696 return default_indirect_link_order (abfd, info, sec, link_order,
2698 case bfd_data_link_order:
2699 return default_data_link_order (abfd, info, sec, link_order);
2703 /* Default routine to handle a bfd_data_link_order. */
2706 default_data_link_order (bfd *abfd,
2707 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2709 struct bfd_link_order *link_order)
2717 BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
2719 size = link_order->size;
2723 fill = link_order->u.data.contents;
2724 fill_size = link_order->u.data.size;
2727 fill = abfd->arch_info->fill (size, bfd_big_endian (abfd),
2728 (sec->flags & SEC_CODE) != 0);
2732 else if (fill_size < size)
2735 fill = (bfd_byte *) bfd_malloc (size);
2740 memset (p, (int) link_order->u.data.contents[0], (size_t) size);
2745 memcpy (p, link_order->u.data.contents, fill_size);
2749 while (size >= fill_size);
2751 memcpy (p, link_order->u.data.contents, (size_t) size);
2752 size = link_order->size;
2756 loc = link_order->offset * bfd_octets_per_byte (abfd);
2757 result = bfd_set_section_contents (abfd, sec, fill, loc, size);
2759 if (fill != link_order->u.data.contents)
2764 /* Default routine to handle a bfd_indirect_link_order. */
2767 default_indirect_link_order (bfd *output_bfd,
2768 struct bfd_link_info *info,
2769 asection *output_section,
2770 struct bfd_link_order *link_order,
2771 bfd_boolean generic_linker)
2773 asection *input_section;
2775 bfd_byte *contents = NULL;
2776 bfd_byte *new_contents;
2777 bfd_size_type sec_size;
2780 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
2782 input_section = link_order->u.indirect.section;
2783 input_bfd = input_section->owner;
2784 if (input_section->size == 0)
2787 BFD_ASSERT (input_section->output_section == output_section);
2788 BFD_ASSERT (input_section->output_offset == link_order->offset);
2789 BFD_ASSERT (input_section->size == link_order->size);
2791 if (info->relocatable
2792 && input_section->reloc_count > 0
2793 && output_section->orelocation == NULL)
2795 /* Space has not been allocated for the output relocations.
2796 This can happen when we are called by a specific backend
2797 because somebody is attempting to link together different
2798 types of object files. Handling this case correctly is
2799 difficult, and sometimes impossible. */
2800 (*_bfd_error_handler)
2801 (_("Attempt to do relocatable link with %s input and %s output"),
2802 bfd_get_target (input_bfd), bfd_get_target (output_bfd));
2803 bfd_set_error (bfd_error_wrong_format);
2807 if (! generic_linker)
2812 /* Get the canonical symbols. The generic linker will always
2813 have retrieved them by this point, but we are being called by
2814 a specific linker, presumably because we are linking
2815 different types of object files together. */
2816 if (!bfd_generic_link_read_symbols (input_bfd))
2819 /* Since we have been called by a specific linker, rather than
2820 the generic linker, the values of the symbols will not be
2821 right. They will be the values as seen in the input file,
2822 not the values of the final link. We need to fix them up
2823 before we can relocate the section. */
2824 sympp = _bfd_generic_link_get_symbols (input_bfd);
2825 symppend = sympp + _bfd_generic_link_get_symcount (input_bfd);
2826 for (; sympp < symppend; sympp++)
2829 struct bfd_link_hash_entry *h;
2833 if ((sym->flags & (BSF_INDIRECT
2838 || bfd_is_und_section (bfd_get_section (sym))
2839 || bfd_is_com_section (bfd_get_section (sym))
2840 || bfd_is_ind_section (bfd_get_section (sym)))
2842 /* sym->udata may have been set by
2843 generic_link_add_symbol_list. */
2844 if (sym->udata.p != NULL)
2845 h = (struct bfd_link_hash_entry *) sym->udata.p;
2846 else if (bfd_is_und_section (bfd_get_section (sym)))
2847 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
2848 bfd_asymbol_name (sym),
2849 FALSE, FALSE, TRUE);
2851 h = bfd_link_hash_lookup (info->hash,
2852 bfd_asymbol_name (sym),
2853 FALSE, FALSE, TRUE);
2855 set_symbol_from_hash (sym, h);
2860 if ((output_section->flags & (SEC_GROUP | SEC_LINKER_CREATED)) == SEC_GROUP
2861 && input_section->size != 0)
2863 /* Group section contents are set by bfd_elf_set_group_contents. */
2864 if (!output_bfd->output_has_begun)
2866 /* FIXME: This hack ensures bfd_elf_set_group_contents is called. */
2867 if (!bfd_set_section_contents (output_bfd, output_section, "", 0, 1))
2870 new_contents = output_section->contents;
2871 BFD_ASSERT (new_contents != NULL);
2872 BFD_ASSERT (input_section->output_offset == 0);
2876 /* Get and relocate the section contents. */
2877 sec_size = (input_section->rawsize > input_section->size
2878 ? input_section->rawsize
2879 : input_section->size);
2880 contents = (bfd_byte *) bfd_malloc (sec_size);
2881 if (contents == NULL && sec_size != 0)
2883 new_contents = (bfd_get_relocated_section_contents
2884 (output_bfd, info, link_order, contents,
2886 _bfd_generic_link_get_symbols (input_bfd)));
2891 /* Output the section contents. */
2892 loc = input_section->output_offset * bfd_octets_per_byte (output_bfd);
2893 if (! bfd_set_section_contents (output_bfd, output_section,
2894 new_contents, loc, input_section->size))
2897 if (contents != NULL)
2902 if (contents != NULL)
2907 /* A little routine to count the number of relocs in a link_order
2911 _bfd_count_link_order_relocs (struct bfd_link_order *link_order)
2913 register unsigned int c;
2914 register struct bfd_link_order *l;
2917 for (l = link_order; l != NULL; l = l->next)
2919 if (l->type == bfd_section_reloc_link_order
2920 || l->type == bfd_symbol_reloc_link_order)
2929 bfd_link_split_section
2932 bfd_boolean bfd_link_split_section (bfd *abfd, asection *sec);
2935 Return nonzero if @var{sec} should be split during a
2936 reloceatable or final link.
2938 .#define bfd_link_split_section(abfd, sec) \
2939 . BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
2945 _bfd_generic_link_split_section (bfd *abfd ATTRIBUTE_UNUSED,
2946 asection *sec ATTRIBUTE_UNUSED)
2953 bfd_section_already_linked
2956 bfd_boolean bfd_section_already_linked (bfd *abfd,
2958 struct bfd_link_info *info);
2961 Check if @var{data} has been already linked during a reloceatable
2962 or final link. Return TRUE if it has.
2964 .#define bfd_section_already_linked(abfd, sec, info) \
2965 . BFD_SEND (abfd, _section_already_linked, (abfd, sec, info))
2970 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
2971 once into the output. This routine checks each section, and
2972 arrange to discard it if a section of the same name has already
2973 been linked. This code assumes that all relevant sections have the
2974 SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
2975 section name. bfd_section_already_linked is called via
2976 bfd_map_over_sections. */
2978 /* The hash table. */
2980 static struct bfd_hash_table _bfd_section_already_linked_table;
2982 /* Support routines for the hash table used by section_already_linked,
2983 initialize the table, traverse, lookup, fill in an entry and remove
2987 bfd_section_already_linked_table_traverse
2988 (bfd_boolean (*func) (struct bfd_section_already_linked_hash_entry *,
2989 void *), void *info)
2991 bfd_hash_traverse (&_bfd_section_already_linked_table,
2992 (bfd_boolean (*) (struct bfd_hash_entry *,
2997 struct bfd_section_already_linked_hash_entry *
2998 bfd_section_already_linked_table_lookup (const char *name)
3000 return ((struct bfd_section_already_linked_hash_entry *)
3001 bfd_hash_lookup (&_bfd_section_already_linked_table, name,
3006 bfd_section_already_linked_table_insert
3007 (struct bfd_section_already_linked_hash_entry *already_linked_list,
3010 struct bfd_section_already_linked *l;
3012 /* Allocate the memory from the same obstack as the hash table is
3014 l = (struct bfd_section_already_linked *)
3015 bfd_hash_allocate (&_bfd_section_already_linked_table, sizeof *l);
3019 l->next = already_linked_list->entry;
3020 already_linked_list->entry = l;
3024 static struct bfd_hash_entry *
3025 already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
3026 struct bfd_hash_table *table,
3027 const char *string ATTRIBUTE_UNUSED)
3029 struct bfd_section_already_linked_hash_entry *ret =
3030 (struct bfd_section_already_linked_hash_entry *)
3031 bfd_hash_allocate (table, sizeof *ret);
3042 bfd_section_already_linked_table_init (void)
3044 return bfd_hash_table_init_n (&_bfd_section_already_linked_table,
3045 already_linked_newfunc,
3046 sizeof (struct bfd_section_already_linked_hash_entry),
3051 bfd_section_already_linked_table_free (void)
3053 bfd_hash_table_free (&_bfd_section_already_linked_table);
3056 /* Report warnings as appropriate for duplicate section SEC.
3057 Return FALSE if we decide to keep SEC after all. */
3060 _bfd_handle_already_linked (asection *sec,
3061 struct bfd_section_already_linked *l,
3062 struct bfd_link_info *info)
3064 switch (sec->flags & SEC_LINK_DUPLICATES)
3069 case SEC_LINK_DUPLICATES_DISCARD:
3070 /* If we found an LTO IR match for this comdat group on
3071 the first pass, replace it with the LTO output on the
3072 second pass. We can't simply choose real object
3073 files over IR because the first pass may contain a
3074 mix of LTO and normal objects and we must keep the
3075 first match, be it IR or real. */
3076 if (info->loading_lto_outputs
3077 && (l->sec->owner->flags & BFD_PLUGIN) != 0)
3084 case SEC_LINK_DUPLICATES_ONE_ONLY:
3085 info->callbacks->einfo
3086 (_("%B: ignoring duplicate section `%A'\n"),
3090 case SEC_LINK_DUPLICATES_SAME_SIZE:
3091 if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
3093 else if (sec->size != l->sec->size)
3094 info->callbacks->einfo
3095 (_("%B: duplicate section `%A' has different size\n"),
3099 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
3100 if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
3102 else if (sec->size != l->sec->size)
3103 info->callbacks->einfo
3104 (_("%B: duplicate section `%A' has different size\n"),
3106 else if (sec->size != 0)
3108 bfd_byte *sec_contents, *l_sec_contents = NULL;
3110 if (!bfd_malloc_and_get_section (sec->owner, sec, &sec_contents))
3111 info->callbacks->einfo
3112 (_("%B: could not read contents of section `%A'\n"),
3114 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
3116 info->callbacks->einfo
3117 (_("%B: could not read contents of section `%A'\n"),
3118 l->sec->owner, l->sec);
3119 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
3120 info->callbacks->einfo
3121 (_("%B: duplicate section `%A' has different contents\n"),
3125 free (sec_contents);
3127 free (l_sec_contents);
3132 /* Set the output_section field so that lang_add_section
3133 does not create a lang_input_section structure for this
3134 section. Since there might be a symbol in the section
3135 being discarded, we must retain a pointer to the section
3136 which we are really going to use. */
3137 sec->output_section = bfd_abs_section_ptr;
3138 sec->kept_section = l->sec;
3142 /* This is used on non-ELF inputs. */
3145 _bfd_generic_section_already_linked (bfd *abfd ATTRIBUTE_UNUSED,
3147 struct bfd_link_info *info)
3150 struct bfd_section_already_linked *l;
3151 struct bfd_section_already_linked_hash_entry *already_linked_list;
3153 if ((sec->flags & SEC_LINK_ONCE) == 0)
3156 /* The generic linker doesn't handle section groups. */
3157 if ((sec->flags & SEC_GROUP) != 0)
3160 /* FIXME: When doing a relocatable link, we may have trouble
3161 copying relocations in other sections that refer to local symbols
3162 in the section being discarded. Those relocations will have to
3163 be converted somehow; as of this writing I'm not sure that any of
3164 the backends handle that correctly.
3166 It is tempting to instead not discard link once sections when
3167 doing a relocatable link (technically, they should be discarded
3168 whenever we are building constructors). However, that fails,
3169 because the linker winds up combining all the link once sections
3170 into a single large link once section, which defeats the purpose
3171 of having link once sections in the first place. */
3173 name = bfd_get_section_name (abfd, sec);
3175 already_linked_list = bfd_section_already_linked_table_lookup (name);
3177 l = already_linked_list->entry;
3180 /* The section has already been linked. See if we should
3182 return _bfd_handle_already_linked (sec, l, info);
3185 /* This is the first section with this name. Record it. */
3186 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
3187 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
3191 /* Choose a neighbouring section to S in OBFD that will be output, or
3192 the absolute section if ADDR is out of bounds of the neighbours. */
3195 _bfd_nearby_section (bfd *obfd, asection *s, bfd_vma addr)
3197 asection *next, *prev, *best;
3199 /* Find preceding kept section. */
3200 for (prev = s->prev; prev != NULL; prev = prev->prev)
3201 if ((prev->flags & SEC_EXCLUDE) == 0
3202 && !bfd_section_removed_from_list (obfd, prev))
3205 /* Find following kept section. Start at prev->next because
3206 other sections may have been added after S was removed. */
3207 if (s->prev != NULL)
3208 next = s->prev->next;
3210 next = s->owner->sections;
3211 for (; next != NULL; next = next->next)
3212 if ((next->flags & SEC_EXCLUDE) == 0
3213 && !bfd_section_removed_from_list (obfd, next))
3216 /* Choose better of two sections, based on flags. The idea
3217 is to choose a section that will be in the same segment
3218 as S would have been if it was kept. */
3223 best = bfd_abs_section_ptr;
3225 else if (next == NULL)
3227 else if (((prev->flags ^ next->flags)
3228 & (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_LOAD)) != 0)
3230 if (((next->flags ^ s->flags)
3231 & (SEC_ALLOC | SEC_THREAD_LOCAL)) != 0
3232 /* We prefer to choose a loaded section. Section S
3233 doesn't have SEC_LOAD set (it being excluded, that
3234 part of the flag processing didn't happen) so we
3235 can't compare that flag to those of NEXT and PREV. */
3236 || ((prev->flags & SEC_LOAD) != 0
3237 && (next->flags & SEC_LOAD) == 0))
3240 else if (((prev->flags ^ next->flags) & SEC_READONLY) != 0)
3242 if (((next->flags ^ s->flags) & SEC_READONLY) != 0)
3245 else if (((prev->flags ^ next->flags) & SEC_CODE) != 0)
3247 if (((next->flags ^ s->flags) & SEC_CODE) != 0)
3252 /* Flags we care about are the same. Prefer the following
3253 section if that will result in a positive valued sym. */
3254 if (addr < next->vma)
3261 /* Convert symbols in excluded output sections to use a kept section. */
3264 fix_syms (struct bfd_link_hash_entry *h, void *data)
3266 bfd *obfd = (bfd *) data;
3268 if (h->type == bfd_link_hash_defined
3269 || h->type == bfd_link_hash_defweak)
3271 asection *s = h->u.def.section;
3273 && s->output_section != NULL
3274 && (s->output_section->flags & SEC_EXCLUDE) != 0
3275 && bfd_section_removed_from_list (obfd, s->output_section))
3279 h->u.def.value += s->output_offset + s->output_section->vma;
3280 op = _bfd_nearby_section (obfd, s->output_section, h->u.def.value);
3281 h->u.def.value -= op->vma;
3282 h->u.def.section = op;
3290 _bfd_fix_excluded_sec_syms (bfd *obfd, struct bfd_link_info *info)
3292 bfd_link_hash_traverse (info->hash, fix_syms, obfd);
3297 bfd_generic_define_common_symbol
3300 bfd_boolean bfd_generic_define_common_symbol
3301 (bfd *output_bfd, struct bfd_link_info *info,
3302 struct bfd_link_hash_entry *h);
3305 Convert common symbol @var{h} into a defined symbol.
3306 Return TRUE on success and FALSE on failure.
3308 .#define bfd_define_common_symbol(output_bfd, info, h) \
3309 . BFD_SEND (output_bfd, _bfd_define_common_symbol, (output_bfd, info, h))
3314 bfd_generic_define_common_symbol (bfd *output_bfd,
3315 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3316 struct bfd_link_hash_entry *h)
3318 unsigned int power_of_two;
3319 bfd_vma alignment, size;
3322 BFD_ASSERT (h != NULL && h->type == bfd_link_hash_common);
3325 power_of_two = h->u.c.p->alignment_power;
3326 section = h->u.c.p->section;
3328 /* Increase the size of the section to align the common symbol.
3329 The alignment must be a power of two. */
3330 alignment = bfd_octets_per_byte (output_bfd) << power_of_two;
3331 BFD_ASSERT (alignment != 0 && (alignment & -alignment) == alignment);
3332 section->size += alignment - 1;
3333 section->size &= -alignment;
3335 /* Adjust the section's overall alignment if necessary. */
3336 if (power_of_two > section->alignment_power)
3337 section->alignment_power = power_of_two;
3339 /* Change the symbol from common to defined. */
3340 h->type = bfd_link_hash_defined;
3341 h->u.def.section = section;
3342 h->u.def.value = section->size;
3344 /* Increase the size of the section. */
3345 section->size += size;
3347 /* Make sure the section is allocated in memory, and make sure that
3348 it is no longer a common section. */
3349 section->flags |= SEC_ALLOC;
3350 section->flags &= ~SEC_IS_COMMON;
3356 bfd_find_version_for_sym
3359 struct bfd_elf_version_tree * bfd_find_version_for_sym
3360 (struct bfd_elf_version_tree *verdefs,
3361 const char *sym_name, bfd_boolean *hide);
3364 Search an elf version script tree for symbol versioning
3365 info and export / don't-export status for a given symbol.
3366 Return non-NULL on success and NULL on failure; also sets
3367 the output @samp{hide} boolean parameter.
3371 struct bfd_elf_version_tree *
3372 bfd_find_version_for_sym (struct bfd_elf_version_tree *verdefs,
3373 const char *sym_name,
3376 struct bfd_elf_version_tree *t;
3377 struct bfd_elf_version_tree *local_ver, *global_ver, *exist_ver;
3378 struct bfd_elf_version_tree *star_local_ver, *star_global_ver;
3382 star_local_ver = NULL;
3383 star_global_ver = NULL;
3385 for (t = verdefs; t != NULL; t = t->next)
3387 if (t->globals.list != NULL)
3389 struct bfd_elf_version_expr *d = NULL;
3391 while ((d = (*t->match) (&t->globals, d, sym_name)) != NULL)
3393 if (d->literal || strcmp (d->pattern, "*") != 0)
3396 star_global_ver = t;
3400 /* If the match is a wildcard pattern, keep looking for
3401 a more explicit, perhaps even local, match. */
3410 if (t->locals.list != NULL)
3412 struct bfd_elf_version_expr *d = NULL;
3414 while ((d = (*t->match) (&t->locals, d, sym_name)) != NULL)
3416 if (d->literal || strcmp (d->pattern, "*") != 0)
3420 /* If the match is a wildcard pattern, keep looking for
3421 a more explicit, perhaps even global, match. */
3424 /* An exact match overrides a global wildcard. */
3426 star_global_ver = NULL;
3436 if (global_ver == NULL && local_ver == NULL)
3437 global_ver = star_global_ver;
3439 if (global_ver != NULL)
3441 /* If we already have a versioned symbol that matches the
3442 node for this symbol, then we don't want to create a
3443 duplicate from the unversioned symbol. Instead hide the
3444 unversioned symbol. */
3445 *hide = exist_ver == global_ver;
3449 if (local_ver == NULL)
3450 local_ver = star_local_ver;
3452 if (local_ver != NULL)
3463 bfd_hide_sym_by_version
3466 bfd_boolean bfd_hide_sym_by_version
3467 (struct bfd_elf_version_tree *verdefs, const char *sym_name);
3470 Search an elf version script tree for symbol versioning
3471 info for a given symbol. Return TRUE if the symbol is hidden.
3476 bfd_hide_sym_by_version (struct bfd_elf_version_tree *verdefs,
3477 const char *sym_name)
3479 bfd_boolean hidden = FALSE;
3480 bfd_find_version_for_sym (verdefs, sym_name, &hidden);