Add translations for various sub-directories
[binutils-gdb.git] / bfd / linker.c
blob8b3579dc6586fdac894d7172d094284d2eaebc2a
1 /* linker.c -- BFD linker routines
2 Copyright (C) 1993-2025 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. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "bfdlink.h"
26 #include "genlink.h"
29 SECTION
30 Linker Functions
32 @cindex Linker
33 The linker uses three special entry points in the BFD target
34 vector. It is not necessary to write special routines for
35 these entry points when creating a new BFD back end, since
36 generic versions are provided. However, writing them can
37 speed up linking and make it use significantly less runtime
38 memory.
40 The first routine creates a hash table used by the other
41 routines. The second routine adds the symbols from an object
42 file to the hash table. The third routine takes all the
43 object files and links them together to create the output
44 file. These routines are designed so that the linker proper
45 does not need to know anything about the symbols in the object
46 files that it is linking. The linker merely arranges the
47 sections as directed by the linker script and lets BFD handle
48 the details of symbols and relocs.
50 The second routine and third routines are passed a pointer to
51 a <<struct bfd_link_info>> structure (defined in
52 <<bfdlink.h>>) which holds information relevant to the link,
53 including the linker hash table (which was created by the
54 first routine) and a set of callback functions to the linker
55 proper.
57 The generic linker routines are in <<linker.c>>, and use the
58 header file <<genlink.h>>. As of this writing, the only back
59 ends which have implemented versions of these routines are
60 a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>). The a.out
61 routines are used as examples throughout this section.
63 @menu
64 @* Creating a Linker Hash Table::
65 @* Adding Symbols to the Hash Table::
66 @* Performing the Final Link::
67 @end menu
69 INODE
70 Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
71 SUBSECTION
72 Creating a linker hash table
74 @cindex _bfd_link_hash_table_create in target vector
75 @cindex target vector (_bfd_link_hash_table_create)
76 The linker routines must create a hash table, which must be
77 derived from <<struct bfd_link_hash_table>> described in
78 <<bfdlink.c>>. @xref{Hash Tables}, for information on how to
79 create a derived hash table. This entry point is called using
80 the target vector of the linker output file.
82 The <<_bfd_link_hash_table_create>> entry point must allocate
83 and initialize an instance of the desired hash table. If the
84 back end does not require any additional information to be
85 stored with the entries in the hash table, the entry point may
86 simply create a <<struct bfd_link_hash_table>>. Most likely,
87 however, some additional information will be needed.
89 For example, with each entry in the hash table the a.out
90 linker keeps the index the symbol has in the final output file
91 (this index number is used so that when doing a relocatable
92 link the symbol index used in the output file can be quickly
93 filled in when copying over a reloc). The a.out linker code
94 defines the required structures and functions for a hash table
95 derived from <<struct bfd_link_hash_table>>. The a.out linker
96 hash table is created by the function
97 <<NAME(aout,link_hash_table_create)>>; it simply allocates
98 space for the hash table, initializes it, and returns a
99 pointer to it.
101 When writing the linker routines for a new back end, you will
102 generally not know exactly which fields will be required until
103 you have finished. You should simply create a new hash table
104 which defines no additional fields, and then simply add fields
105 as they become necessary.
107 INODE
108 Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
109 SUBSECTION
110 Adding symbols to the hash table
112 @cindex _bfd_link_add_symbols in target vector
113 @cindex target vector (_bfd_link_add_symbols)
114 The linker proper will call the <<_bfd_link_add_symbols>>
115 entry point for each object file or archive which is to be
116 linked (typically these are the files named on the command
117 line, but some may also come from the linker script). The
118 entry point is responsible for examining the file. For an
119 object file, BFD must add any relevant symbol information to
120 the hash table. For an archive, BFD must determine which
121 elements of the archive should be used and adding them to the
122 link.
124 The a.out version of this entry point is
125 <<NAME(aout,link_add_symbols)>>.
127 @menu
128 @* Differing file formats::
129 @* Adding symbols from an object file::
130 @* Adding symbols from an archive::
131 @end menu
133 INODE
134 Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
135 SUBSUBSECTION
136 Differing file formats
138 Normally all the files involved in a link will be of the same
139 format, but it is also possible to link together different
140 format object files, and the back end must support that. The
141 <<_bfd_link_add_symbols>> entry point is called via the target
142 vector of the file to be added. This has an important
143 consequence: the function may not assume that the hash table
144 is the type created by the corresponding
145 <<_bfd_link_hash_table_create>> vector. All the
146 <<_bfd_link_add_symbols>> function can assume about the hash
147 table is that it is derived from <<struct
148 bfd_link_hash_table>>.
150 Sometimes the <<_bfd_link_add_symbols>> function must store
151 some information in the hash table entry to be used by the
152 <<_bfd_final_link>> function. In such a case the 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
168 hash table entry.
170 INODE
171 Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
172 SUBSUBSECTION
173 Adding symbols from an object file
175 When the <<_bfd_link_add_symbols>> routine is passed an object
176 file, it must add all externally visible symbols in that
177 object file to the hash table. The actual work of adding the
178 symbol to the hash table is normally handled by the function
179 <<_bfd_generic_link_add_one_symbol>>. The
180 <<_bfd_link_add_symbols>> routine is responsible for reading
181 all the symbols from the object file and passing the correct
182 information to <<_bfd_generic_link_add_one_symbol>>.
184 The <<_bfd_link_add_symbols>> routine should not use
185 <<bfd_canonicalize_symtab>> to read the symbols. The point of
186 providing this routine is to avoid the overhead of converting
187 the symbols into generic <<asymbol>> structures.
189 @findex _bfd_generic_link_add_one_symbol
190 <<_bfd_generic_link_add_one_symbol>> handles the details of
191 combining common symbols, warning about multiple definitions,
192 and so forth. It takes arguments which describe the symbol to
193 add, notably symbol flags, a section, and an offset. The
194 symbol flags include such things as <<BSF_WEAK>> or
195 <<BSF_INDIRECT>>. The section is a section in the object
196 file, or something like <<bfd_und_section_ptr>> for an undefined
197 symbol or <<bfd_com_section_ptr>> for a common symbol.
199 If the <<_bfd_final_link>> routine is also going to need to
200 read the symbol information, the <<_bfd_link_add_symbols>>
201 routine should save it somewhere attached to the object file
202 BFD. However, the information should only be saved if the
203 <<keep_memory>> field of the <<info>> argument is TRUE, so
204 that the <<-no-keep-memory>> linker switch is effective.
206 The a.out function which adds symbols from an object file is
207 <<aout_link_add_object_symbols>>, and most of the interesting
208 work is in <<aout_link_add_symbols>>. The latter saves
209 pointers to the hash tables entries created by
210 <<_bfd_generic_link_add_one_symbol>> indexed by symbol number,
211 so that the <<_bfd_final_link>> routine does not have to call
212 the hash table lookup routine to locate the entry.
214 INODE
215 Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
216 SUBSUBSECTION
217 Adding symbols from an archive
219 When the <<_bfd_link_add_symbols>> routine is passed an
220 archive, it must look through the symbols defined by the
221 archive and decide which elements of the archive should be
222 included in the link. For each such element it must call the
223 <<add_archive_element>> linker callback, and it must add the
224 symbols from the object file to the linker hash table. (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.
233 <<_bfd_generic_link_add_archive_symbols>> is passed a function
234 to call to make the final decision about adding an archive
235 element to the link and to do the actual work of adding the
236 symbols to the linker hash table. If the element is to
237 be included, the <<add_archive_element>> linker callback
238 routine must be called with the element as an argument, and
239 the element's symbols must be added to the linker hash table
240 just as though the element had itself been passed to the
241 <<_bfd_link_add_symbols>> function.
243 When the a.out <<_bfd_link_add_symbols>> function receives an
244 archive, it calls <<_bfd_generic_link_add_archive_symbols>>
245 passing <<aout_link_check_archive_element>> as the function
246 argument. <<aout_link_check_archive_element>> calls
247 <<aout_link_check_ar_symbols>>. If the latter decides to add
248 the element (an element is only added if it provides a real,
249 non-common, definition for a previously undefined or common
250 symbol) it calls the <<add_archive_element>> callback and then
251 <<aout_link_check_archive_element>> calls
252 <<aout_link_add_symbols>> to actually add the symbols to the
253 linker hash table - possibly those of a substitute BFD, if the
254 <<add_archive_element>> callback avails itself of that option.
256 The ECOFF back end is unusual in that it does not normally
257 call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
258 archives already contain a hash table of symbols. The ECOFF
259 back end searches the archive itself to avoid the overhead of
260 creating a new hash table.
262 INODE
263 Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions
264 SUBSECTION
265 Performing the final link
267 @cindex _bfd_link_final_link in target vector
268 @cindex target vector (_bfd_final_link)
269 When all the input files have been processed, the linker calls
270 the <<_bfd_final_link>> entry point of the output BFD. This
271 routine is responsible for producing the final output file,
272 which has several aspects. It must relocate the contents of
273 the input sections and copy the data into the output sections.
274 It must build an output symbol table including any local
275 symbols from the input files and the global symbols from the
276 hash table. When producing relocatable output, it must
277 modify the input relocs and write them into the output file.
278 There may also be object format dependent work to be done.
280 The linker will also call the <<write_object_contents>> entry
281 point when the BFD is closed. The two entry points must work
282 together in order to produce the correct output file.
284 The details of how this works are inevitably dependent upon
285 the specific object file format. The a.out
286 <<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
288 @menu
289 @* Information provided by the linker::
290 @* Relocating the section contents::
291 @* Writing the symbol table::
292 @end menu
294 INODE
295 Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
296 SUBSUBSECTION
297 Information provided by the linker
299 Before the linker calls the <<_bfd_final_link>> entry point,
300 it sets up some data structures for the function to use.
302 The <<input_bfds>> field of the <<bfd_link_info>> structure
303 will point to a list of all the input files included in the
304 link. These files are linked through the <<link.next>> field
305 of the <<bfd>> structure.
307 Each section in the output file will have a list of
308 <<link_order>> structures attached to the <<map_head.link_order>>
309 field (the <<link_order>> structure is defined in
310 <<bfdlink.h>>). These structures describe how to create the
311 contents of the output section in terms of the contents of
312 various input sections, fill constants, and, eventually, other
313 types of information. They also describe relocs that must be
314 created by the BFD backend, but do not correspond to any input
315 file; this is used to support -Ur, which builds constructors
316 while generating a relocatable object file.
318 INODE
319 Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
320 SUBSUBSECTION
321 Relocating the section contents
323 The <<_bfd_final_link>> function should look through the
324 <<link_order>> structures attached to each section of the
325 output file. Each <<link_order>> structure should either be
326 handled specially, or it should be passed to the function
327 <<_bfd_default_link_order>> which will do the right thing
328 (<<_bfd_default_link_order>> is defined in <<linker.c>>).
330 For efficiency, a <<link_order>> of type
331 <<bfd_indirect_link_order>> whose associated section belongs
332 to a BFD of the same format as the output BFD must be handled
333 specially. This type of <<link_order>> describes part of an
334 output section in terms of a section belonging to one of the
335 input files. The <<_bfd_final_link>> function should read the
336 contents of the section and any associated relocs, apply the
337 relocs to the section contents, and write out the modified
338 section contents. If performing a relocatable link, the
339 relocs themselves must also be modified and written out.
341 @findex _bfd_relocate_contents
342 @findex _bfd_final_link_relocate
343 The functions <<_bfd_relocate_contents>> and
344 <<_bfd_final_link_relocate>> provide some general support for
345 performing the actual relocations, notably overflow checking.
346 Their arguments include information about the symbol the
347 relocation is against and a <<reloc_howto_type>> argument
348 which describes the relocation to perform. These functions
349 are defined in <<reloc.c>>.
351 The a.out function which handles reading, relocating, and
352 writing section contents is <<aout_link_input_section>>. The
353 actual relocation is done in <<aout_link_input_section_std>>
354 and <<aout_link_input_section_ext>>.
356 INODE
357 Writing the symbol table, , Relocating the section contents, Performing the Final Link
358 SUBSUBSECTION
359 Writing the symbol table
361 The <<_bfd_final_link>> function must gather all the symbols
362 in the input files and write them out. It must also write out
363 all the symbols in the global hash table. This must be
364 controlled by the <<strip>> and <<discard>> fields of the
365 <<bfd_link_info>> structure.
367 The local symbols of the input files will not have been
368 entered into the linker hash table. The <<_bfd_final_link>>
369 routine must consider each input file and include the symbols
370 in the output file. It may be convenient to do this when
371 looking through the <<link_order>> structures, or it may be
372 done by stepping through the <<input_bfds>> list.
374 The <<_bfd_final_link>> routine must also traverse the global
375 hash table to gather all the externally visible symbols. It
376 is possible that most of the externally visible symbols may be
377 written out when considering the symbols of each input file,
378 but it is still necessary to traverse the hash table since the
379 linker script may have defined some symbols that are not in
380 any of the input files.
382 The <<strip>> field of the <<bfd_link_info>> structure
383 controls which symbols are written out. The possible values
384 are listed in <<bfdlink.h>>. If the value is <<strip_some>>,
385 then the <<keep_hash>> field of the <<bfd_link_info>>
386 structure is a hash table of symbols to keep; each symbol
387 should be looked up in this hash table, and only symbols which
388 are present should be included in the output file.
390 If the <<strip>> field of the <<bfd_link_info>> structure
391 permits local symbols to be written out, the <<discard>> field
392 is used to further controls which local symbols are included
393 in the output file. If the value is <<discard_l>>, then all
394 local symbols which begin with a certain prefix are discarded;
395 this is controlled by the <<bfd_is_local_label_name>> entry point.
397 The a.out backend handles symbols by calling
398 <<aout_link_write_symbols>> on each input BFD and then
399 traversing the global hash table with the function
400 <<aout_link_write_other_symbol>>. It builds a string table
401 while writing out the symbols, which is written to the output
402 file at the end of <<NAME(aout,final_link)>>.
405 /* This structure is used to pass information to
406 _bfd_generic_link_write_global_symbol, which may be called via
407 _bfd_generic_link_hash_traverse. */
409 struct generic_write_global_symbol_info
411 struct bfd_link_info *info;
412 bfd *output_bfd;
413 size_t *psymalloc;
414 bool failed;
417 static bool generic_link_add_object_symbols
418 (bfd *, struct bfd_link_info *);
419 static bool generic_link_check_archive_element
420 (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
421 bool *);
422 static bool generic_link_add_symbol_list
423 (bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **);
424 static bool generic_add_output_symbol
425 (bfd *, size_t *psymalloc, asymbol *);
426 static bool default_data_link_order
427 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
428 static bool default_indirect_link_order
429 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *,
430 bool);
431 static bool _bfd_generic_link_output_symbols
432 (bfd *, bfd *, struct bfd_link_info *, size_t *);
433 static bool _bfd_generic_link_write_global_symbol
434 (struct generic_link_hash_entry *, void *);
436 /* The link hash table structure is defined in bfdlink.h. It provides
437 a base hash table which the backend specific hash tables are built
438 upon. */
440 /* Routine to create an entry in the link hash table. */
442 struct bfd_hash_entry *
443 _bfd_link_hash_newfunc (struct bfd_hash_entry *entry,
444 struct bfd_hash_table *table,
445 const char *string)
447 /* Allocate the structure if it has not already been allocated by a
448 subclass. */
449 if (entry == NULL)
451 entry = (struct bfd_hash_entry *)
452 bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry));
453 if (entry == NULL)
454 return entry;
457 /* Call the allocation method of the superclass. */
458 entry = bfd_hash_newfunc (entry, table, string);
459 if (entry)
461 struct bfd_link_hash_entry *h = (struct bfd_link_hash_entry *) entry;
463 /* Initialize the local fields. */
464 memset ((char *) &h->root + sizeof (h->root), 0,
465 sizeof (*h) - sizeof (h->root));
468 return entry;
471 /* Initialize a link hash table. The BFD argument is the one
472 responsible for creating this table. */
474 bool
475 _bfd_link_hash_table_init
476 (struct bfd_link_hash_table *table,
477 bfd *abfd ATTRIBUTE_UNUSED,
478 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
479 struct bfd_hash_table *,
480 const char *),
481 unsigned int entsize)
483 bool ret;
485 BFD_ASSERT (!abfd->is_linker_output && !abfd->link.hash);
486 table->undefs = NULL;
487 table->undefs_tail = NULL;
488 table->type = bfd_link_generic_hash_table;
490 ret = bfd_hash_table_init (&table->table, newfunc, entsize);
491 if (ret)
493 /* Arrange for destruction of this hash table on closing ABFD. */
494 table->hash_table_free = _bfd_generic_link_hash_table_free;
495 abfd->link.hash = table;
496 abfd->is_linker_output = true;
498 return ret;
501 /* Look up a symbol in a link hash table. If follow is TRUE, we
502 follow bfd_link_hash_indirect and bfd_link_hash_warning links to
503 the real symbol.
505 .{* Return TRUE if the symbol described by a linker hash entry H
506 . is going to be absolute. Linker-script defined symbols can be
507 . converted from absolute to section-relative ones late in the
508 . link. Use this macro to correctly determine whether the symbol
509 . will actually end up absolute in output. *}
510 .#define bfd_is_abs_symbol(H) \
511 . (((H)->type == bfd_link_hash_defined \
512 . || (H)->type == bfd_link_hash_defweak) \
513 . && bfd_is_abs_section ((H)->u.def.section) \
514 . && !(H)->rel_from_abs)
518 struct bfd_link_hash_entry *
519 bfd_link_hash_lookup (struct bfd_link_hash_table *table,
520 const char *string,
521 bool create,
522 bool copy,
523 bool follow)
525 struct bfd_link_hash_entry *ret;
527 if (table == NULL || string == NULL)
528 return NULL;
530 ret = ((struct bfd_link_hash_entry *)
531 bfd_hash_lookup (&table->table, string, create, copy));
533 if (follow && ret != NULL)
535 while (ret->type == bfd_link_hash_indirect
536 || ret->type == bfd_link_hash_warning)
537 ret = ret->u.i.link;
540 return ret;
543 /* Look up a symbol in the main linker hash table if the symbol might
544 be wrapped. This should only be used for references to an
545 undefined symbol, not for definitions of a symbol. */
547 struct bfd_link_hash_entry *
548 bfd_wrapped_link_hash_lookup (bfd *abfd,
549 struct bfd_link_info *info,
550 const char *string,
551 bool create,
552 bool copy,
553 bool follow)
555 size_t amt;
557 if (info->wrap_hash != NULL)
559 const char *l;
560 char prefix = '\0';
562 l = string;
563 if (*l
564 && (*l == bfd_get_symbol_leading_char (abfd)
565 || *l == info->wrap_char))
567 prefix = *l;
568 ++l;
571 #undef WRAP
572 #define WRAP "__wrap_"
574 if (bfd_hash_lookup (info->wrap_hash, l, false, false) != NULL)
576 char *n;
577 struct bfd_link_hash_entry *h;
579 /* This symbol is being wrapped. We want to replace all
580 references to SYM with references to __wrap_SYM. */
582 amt = strlen (l) + sizeof WRAP + 1;
583 n = (char *) bfd_malloc (amt);
584 if (n == NULL)
585 return NULL;
587 n[0] = prefix;
588 n[1] = '\0';
589 strcat (n, WRAP);
590 strcat (n, l);
591 h = bfd_link_hash_lookup (info->hash, n, create, true, follow);
592 if (h != NULL)
593 h->wrapper_symbol = true;
594 free (n);
595 return h;
598 #undef REAL
599 #define REAL "__real_"
601 if (*l == '_'
602 && startswith (l, REAL)
603 && bfd_hash_lookup (info->wrap_hash, l + sizeof REAL - 1,
604 false, false) != NULL)
606 char *n;
607 struct bfd_link_hash_entry *h;
609 /* This is a reference to __real_SYM, where SYM is being
610 wrapped. We want to replace all references to __real_SYM
611 with references to SYM. */
613 amt = strlen (l + sizeof REAL - 1) + 2;
614 n = (char *) bfd_malloc (amt);
615 if (n == NULL)
616 return NULL;
618 n[0] = prefix;
619 n[1] = '\0';
620 strcat (n, l + sizeof REAL - 1);
621 h = bfd_link_hash_lookup (info->hash, n, create, true, follow);
622 if (h != NULL)
623 h->ref_real = 1;
624 free (n);
625 return h;
628 #undef REAL
631 return bfd_link_hash_lookup (info->hash, string, create, copy, follow);
634 /* If H is a wrapped symbol, ie. the symbol name starts with "__wrap_"
635 and the remainder is found in wrap_hash, return the real symbol. */
637 struct bfd_link_hash_entry *
638 unwrap_hash_lookup (struct bfd_link_info *info,
639 bfd *input_bfd,
640 struct bfd_link_hash_entry *h)
642 const char *l = h->root.string;
644 if (*l
645 && (*l == bfd_get_symbol_leading_char (input_bfd)
646 || *l == info->wrap_char))
647 ++l;
649 if (startswith (l, WRAP))
651 l += sizeof WRAP - 1;
653 if (bfd_hash_lookup (info->wrap_hash, l, false, false) != NULL)
655 char save = 0;
656 if (l - (sizeof WRAP - 1) != h->root.string)
658 --l;
659 save = *l;
660 *(char *) l = *h->root.string;
662 h = bfd_link_hash_lookup (info->hash, l, false, false, false);
663 if (save)
664 *(char *) l = save;
667 return h;
669 #undef WRAP
671 /* Traverse a generic link hash table. Differs from bfd_hash_traverse
672 in the treatment of warning symbols. When warning symbols are
673 created they replace the real symbol, so you don't get to see the
674 real symbol in a bfd_hash_traverse. This traversal calls func with
675 the real symbol. */
677 void
678 bfd_link_hash_traverse
679 (struct bfd_link_hash_table *htab,
680 bool (*func) (struct bfd_link_hash_entry *, void *),
681 void *info)
683 unsigned int i;
685 htab->table.frozen = 1;
686 for (i = 0; i < htab->table.size; i++)
688 struct bfd_link_hash_entry *p;
690 p = (struct bfd_link_hash_entry *) htab->table.table[i];
691 for (; p != NULL; p = (struct bfd_link_hash_entry *) p->root.next)
692 if (!(*func) (p->type == bfd_link_hash_warning ? p->u.i.link : p, info))
693 goto out;
695 out:
696 htab->table.frozen = 0;
699 /* Add a symbol to the linker hash table undefs list. */
701 void
702 bfd_link_add_undef (struct bfd_link_hash_table *table,
703 struct bfd_link_hash_entry *h)
705 BFD_ASSERT (h->u.undef.next == NULL);
706 if (table->undefs_tail != NULL)
707 table->undefs_tail->u.undef.next = h;
708 if (table->undefs == NULL)
709 table->undefs = h;
710 table->undefs_tail = h;
713 /* The undefs list was designed so that in normal use we don't need to
714 remove entries. However, if symbols on the list are changed from
715 bfd_link_hash_undefined to either bfd_link_hash_undefweak or
716 bfd_link_hash_new for some reason, then they must be removed from the
717 list. Failure to do so might result in the linker attempting to add
718 the symbol to the list again at a later stage. */
720 void
721 bfd_link_repair_undef_list (struct bfd_link_hash_table *table)
723 struct bfd_link_hash_entry **pun;
725 pun = &table->undefs;
726 while (*pun != NULL)
728 struct bfd_link_hash_entry *h = *pun;
730 if (h->type == bfd_link_hash_new
731 || h->type == bfd_link_hash_undefweak)
733 *pun = h->u.undef.next;
734 h->u.undef.next = NULL;
735 if (h == table->undefs_tail)
737 if (pun == &table->undefs)
738 table->undefs_tail = NULL;
739 else
740 /* pun points at an u.undef.next field. Go back to
741 the start of the link_hash_entry. */
742 table->undefs_tail = (struct bfd_link_hash_entry *)
743 ((char *) pun - ((char *) &h->u.undef.next - (char *) h));
744 break;
747 else
748 pun = &h->u.undef.next;
752 /* Routine to create an entry in a generic link hash table. */
754 static struct bfd_hash_entry *
755 _bfd_generic_link_hash_newfunc (struct bfd_hash_entry *entry,
756 struct bfd_hash_table *table,
757 const char *string)
759 /* Allocate the structure if it has not already been allocated by a
760 subclass. */
761 if (entry == NULL)
763 entry = (struct bfd_hash_entry *)
764 bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry));
765 if (entry == NULL)
766 return entry;
769 /* Call the allocation method of the superclass. */
770 entry = _bfd_link_hash_newfunc (entry, table, string);
771 if (entry)
773 struct generic_link_hash_entry *ret;
775 /* Set local fields. */
776 ret = (struct generic_link_hash_entry *) entry;
777 ret->written = false;
778 ret->sym = NULL;
781 return entry;
784 /* Create a generic link hash table. */
786 struct bfd_link_hash_table *
787 _bfd_generic_link_hash_table_create (bfd *abfd)
789 struct generic_link_hash_table *ret;
790 size_t amt = sizeof (struct generic_link_hash_table);
792 ret = (struct generic_link_hash_table *) bfd_malloc (amt);
793 if (ret == NULL)
794 return NULL;
795 if (! _bfd_link_hash_table_init (&ret->root, abfd,
796 _bfd_generic_link_hash_newfunc,
797 sizeof (struct generic_link_hash_entry)))
799 free (ret);
800 return NULL;
802 return &ret->root;
805 void
806 _bfd_generic_link_hash_table_free (bfd *obfd)
808 struct generic_link_hash_table *ret;
810 BFD_ASSERT (obfd->is_linker_output && obfd->link.hash);
811 ret = (struct generic_link_hash_table *) obfd->link.hash;
812 bfd_hash_table_free (&ret->root.table);
813 free (ret);
814 obfd->link.hash = NULL;
815 obfd->is_linker_output = false;
818 /* Grab the symbols for an object file when doing a generic link. We
819 store the symbols in the outsymbols field. We need to keep them
820 around for the entire link to ensure that we only read them once.
821 If we read them multiple times, we might wind up with relocs and
822 the hash table pointing to different instances of the symbol
823 structure. */
825 bool
826 bfd_generic_link_read_symbols (bfd *abfd)
828 if (bfd_get_outsymbols (abfd) == NULL)
830 long symsize;
831 long symcount;
833 symsize = bfd_get_symtab_upper_bound (abfd);
834 if (symsize < 0)
835 return false;
836 abfd->outsymbols = bfd_alloc (abfd, symsize);
837 if (bfd_get_outsymbols (abfd) == NULL && symsize != 0)
838 return false;
839 symcount = bfd_canonicalize_symtab (abfd, bfd_get_outsymbols (abfd));
840 if (symcount < 0)
841 return false;
842 abfd->symcount = symcount;
845 return true;
848 /* Indicate that we are only retrieving symbol values from this
849 section. We want the symbols to act as though the values in the
850 file are absolute. */
852 void
853 _bfd_generic_link_just_syms (asection *sec,
854 struct bfd_link_info *info ATTRIBUTE_UNUSED)
856 sec->sec_info_type = SEC_INFO_TYPE_JUST_SYMS;
857 sec->output_section = bfd_abs_section_ptr;
858 sec->output_offset = sec->vma;
861 /* Copy the symbol type and other attributes for a linker script
862 assignment from HSRC to HDEST.
863 The default implementation does nothing. */
864 void
865 _bfd_generic_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
866 struct bfd_link_hash_entry *hdest ATTRIBUTE_UNUSED,
867 struct bfd_link_hash_entry *hsrc ATTRIBUTE_UNUSED)
871 /* Generic function to add symbols from an object file to the
872 global hash table. */
874 bool
875 _bfd_generic_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
877 bool ret;
879 switch (bfd_get_format (abfd))
881 case bfd_object:
882 ret = generic_link_add_object_symbols (abfd, info);
883 break;
884 case bfd_archive:
885 ret = (_bfd_generic_link_add_archive_symbols
886 (abfd, info, generic_link_check_archive_element));
887 break;
888 default:
889 bfd_set_error (bfd_error_wrong_format);
890 ret = false;
893 return ret;
896 /* Add symbols from an object file to the global hash table. */
898 static bool
899 generic_link_add_object_symbols (bfd *abfd,
900 struct bfd_link_info *info)
902 bfd_size_type symcount;
903 struct bfd_symbol **outsyms;
905 if (!bfd_generic_link_read_symbols (abfd))
906 return false;
907 symcount = _bfd_generic_link_get_symcount (abfd);
908 outsyms = _bfd_generic_link_get_symbols (abfd);
909 return generic_link_add_symbol_list (abfd, info, symcount, outsyms);
912 /* Generic function to add symbols from an archive file to the global
913 hash file. This function presumes that the archive symbol table
914 has already been read in (this is normally done by the
915 bfd_check_format entry point). It looks through the archive symbol
916 table for symbols that are undefined or common in the linker global
917 symbol hash table. When one is found, the CHECKFN argument is used
918 to see if an object file should be included. This allows targets
919 to customize common symbol behaviour. CHECKFN should set *PNEEDED
920 to TRUE if the object file should be included, and must also call
921 the bfd_link_info add_archive_element callback function and handle
922 adding the symbols to the global hash table. CHECKFN must notice
923 if the callback indicates a substitute BFD, and arrange to add
924 those symbols instead if it does so. CHECKFN should only return
925 FALSE if some sort of error occurs. */
927 bool
928 _bfd_generic_link_add_archive_symbols
929 (bfd *abfd,
930 struct bfd_link_info *info,
931 bool (*checkfn) (bfd *, struct bfd_link_info *,
932 struct bfd_link_hash_entry *, const char *, bool *))
934 bool loop;
935 bfd_size_type amt;
936 unsigned char *included;
938 if (! bfd_has_map (abfd))
940 /* An empty archive is a special case. */
941 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
942 return true;
943 bfd_set_error (bfd_error_no_armap);
944 return false;
947 amt = bfd_ardata (abfd)->symdef_count;
948 if (amt == 0)
949 return true;
950 amt *= sizeof (*included);
951 included = (unsigned char *) bfd_zmalloc (amt);
952 if (included == NULL)
953 return false;
957 carsym *arsyms;
958 carsym *arsym_end;
959 carsym *arsym;
960 unsigned int indx;
961 file_ptr last_ar_offset = -1;
962 bool needed = false;
963 bfd *element = NULL;
965 loop = false;
966 arsyms = bfd_ardata (abfd)->symdefs;
967 arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
968 for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
970 struct bfd_link_hash_entry *h;
971 struct bfd_link_hash_entry *undefs_tail;
973 if (included[indx])
974 continue;
975 if (needed && arsym->file_offset == last_ar_offset)
977 included[indx] = 1;
978 continue;
981 if (arsym->name == NULL)
982 goto error_return;
984 h = bfd_link_hash_lookup (info->hash, arsym->name,
985 false, false, true);
987 if (h == NULL
988 && info->pei386_auto_import
989 && startswith (arsym->name, "__imp_"))
990 h = bfd_link_hash_lookup (info->hash, arsym->name + 6,
991 false, false, true);
992 if (h == NULL)
993 continue;
995 if (h->type != bfd_link_hash_undefined
996 && h->type != bfd_link_hash_common)
998 if (h->type != bfd_link_hash_undefweak)
999 /* Symbol must be defined. Don't check it again. */
1000 included[indx] = 1;
1001 continue;
1004 if (last_ar_offset != arsym->file_offset)
1006 last_ar_offset = arsym->file_offset;
1007 element = _bfd_get_elt_at_filepos (abfd, last_ar_offset,
1008 info);
1009 if (element == NULL
1010 || !bfd_check_format (element, bfd_object))
1011 goto error_return;
1014 undefs_tail = info->hash->undefs_tail;
1016 /* CHECKFN will see if this element should be included, and
1017 go ahead and include it if appropriate. */
1018 if (! (*checkfn) (element, info, h, arsym->name, &needed))
1019 goto error_return;
1021 if (needed)
1023 unsigned int mark;
1025 /* Look backward to mark all symbols from this object file
1026 which we have already seen in this pass. */
1027 mark = indx;
1030 included[mark] = 1;
1031 if (mark == 0)
1032 break;
1033 --mark;
1035 while (arsyms[mark].file_offset == last_ar_offset);
1037 if (undefs_tail != info->hash->undefs_tail)
1038 loop = true;
1041 } while (loop);
1043 free (included);
1044 return true;
1046 error_return:
1047 free (included);
1048 return false;
1051 /* See if we should include an archive element. */
1053 static bool
1054 generic_link_check_archive_element (bfd *abfd,
1055 struct bfd_link_info *info,
1056 struct bfd_link_hash_entry *h,
1057 const char *name ATTRIBUTE_UNUSED,
1058 bool *pneeded)
1060 asymbol **pp, **ppend;
1062 *pneeded = false;
1064 if (!bfd_generic_link_read_symbols (abfd))
1065 return false;
1067 pp = _bfd_generic_link_get_symbols (abfd);
1068 ppend = pp + _bfd_generic_link_get_symcount (abfd);
1069 for (; pp < ppend; pp++)
1071 asymbol *p;
1073 p = *pp;
1075 /* We are only interested in globally visible symbols. */
1076 if (! bfd_is_com_section (p->section)
1077 && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0)
1078 continue;
1080 /* We are only interested if we know something about this
1081 symbol, and it is undefined or common. An undefined weak
1082 symbol (type bfd_link_hash_undefweak) is not considered to be
1083 a reference when pulling files out of an archive. See the
1084 SVR4 ABI, p. 4-27. */
1085 h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), false,
1086 false, true);
1087 if (h == NULL
1088 || (h->type != bfd_link_hash_undefined
1089 && h->type != bfd_link_hash_common))
1090 continue;
1092 /* P is a symbol we are looking for. */
1094 if (! bfd_is_com_section (p->section)
1095 || (h->type == bfd_link_hash_undefined
1096 && h->u.undef.abfd == NULL))
1098 /* P is not a common symbol, or an undefined reference was
1099 created from outside BFD such as from a linker -u option.
1100 This object file defines the symbol, so pull it in. */
1101 *pneeded = true;
1102 if (!(*info->callbacks
1103 ->add_archive_element) (info, abfd, bfd_asymbol_name (p),
1104 &abfd))
1105 return false;
1106 /* Potentially, the add_archive_element hook may have set a
1107 substitute BFD for us. */
1108 return bfd_link_add_symbols (abfd, info);
1111 /* P is a common symbol. */
1113 if (h->type == bfd_link_hash_undefined)
1115 bfd *symbfd;
1116 bfd_vma size;
1117 unsigned int power;
1119 /* Turn the symbol into a common symbol but do not link in
1120 the object file. This is how a.out works. Object
1121 formats that require different semantics must implement
1122 this function differently. This symbol is already on the
1123 undefs list. We add the section to a common section
1124 attached to symbfd to ensure that it is in a BFD which
1125 will be linked in. */
1126 symbfd = h->u.undef.abfd;
1127 h->type = bfd_link_hash_common;
1128 h->u.c.p = (struct bfd_link_hash_common_entry *)
1129 bfd_hash_allocate (&info->hash->table,
1130 sizeof (struct bfd_link_hash_common_entry));
1131 if (h->u.c.p == NULL)
1132 return false;
1134 size = bfd_asymbol_value (p);
1135 h->u.c.size = size;
1137 power = bfd_log2 (size);
1138 if (power > 4)
1139 power = 4;
1140 h->u.c.p->alignment_power = power;
1142 if (p->section == bfd_com_section_ptr)
1143 h->u.c.p->section = bfd_make_section_old_way (symbfd, "COMMON");
1144 else
1145 h->u.c.p->section = bfd_make_section_old_way (symbfd,
1146 p->section->name);
1147 h->u.c.p->section->flags |= SEC_ALLOC;
1149 else
1151 /* Adjust the size of the common symbol if necessary. This
1152 is how a.out works. Object formats that require
1153 different semantics must implement this function
1154 differently. */
1155 if (bfd_asymbol_value (p) > h->u.c.size)
1156 h->u.c.size = bfd_asymbol_value (p);
1160 /* This archive element is not needed. */
1161 return true;
1164 /* Add the symbols from an object file to the global hash table. ABFD
1165 is the object file. INFO is the linker information. SYMBOL_COUNT
1166 is the number of symbols. SYMBOLS is the list of symbols. */
1168 static bool
1169 generic_link_add_symbol_list (bfd *abfd,
1170 struct bfd_link_info *info,
1171 bfd_size_type symbol_count,
1172 asymbol **symbols)
1174 asymbol **pp, **ppend;
1176 pp = symbols;
1177 ppend = symbols + symbol_count;
1178 for (; pp < ppend; pp++)
1180 asymbol *p;
1182 p = *pp;
1184 if ((p->flags & (BSF_INDIRECT
1185 | BSF_WARNING
1186 | BSF_GLOBAL
1187 | BSF_CONSTRUCTOR
1188 | BSF_WEAK)) != 0
1189 || bfd_is_und_section (bfd_asymbol_section (p))
1190 || bfd_is_com_section (bfd_asymbol_section (p))
1191 || bfd_is_ind_section (bfd_asymbol_section (p)))
1193 const char *name;
1194 const char *string;
1195 struct generic_link_hash_entry *h;
1196 struct bfd_link_hash_entry *bh;
1198 string = name = bfd_asymbol_name (p);
1199 if (((p->flags & BSF_INDIRECT) != 0
1200 || bfd_is_ind_section (p->section))
1201 && pp + 1 < ppend)
1203 pp++;
1204 string = bfd_asymbol_name (*pp);
1206 else if ((p->flags & BSF_WARNING) != 0
1207 && pp + 1 < ppend)
1209 /* The name of P is actually the warning string, and the
1210 next symbol is the one to warn about. */
1211 pp++;
1212 name = bfd_asymbol_name (*pp);
1215 bh = NULL;
1216 if (! (_bfd_generic_link_add_one_symbol
1217 (info, abfd, name, p->flags, bfd_asymbol_section (p),
1218 p->value, string, false, false, &bh)))
1219 return false;
1220 h = (struct generic_link_hash_entry *) bh;
1222 /* If this is a constructor symbol, and the linker didn't do
1223 anything with it, then we want to just pass the symbol
1224 through to the output file. This will happen when
1225 linking with -r. */
1226 if ((p->flags & BSF_CONSTRUCTOR) != 0
1227 && (h == NULL || h->root.type == bfd_link_hash_new))
1229 p->udata.p = NULL;
1230 continue;
1233 /* Save the BFD symbol so that we don't lose any backend
1234 specific information that may be attached to it. We only
1235 want this one if it gives more information than the
1236 existing one; we don't want to replace a defined symbol
1237 with an undefined one. This routine may be called with a
1238 hash table other than the generic hash table, so we only
1239 do this if we are certain that the hash table is a
1240 generic one. */
1241 if (info->output_bfd->xvec == abfd->xvec)
1243 if (h->sym == NULL
1244 || (! bfd_is_und_section (bfd_asymbol_section (p))
1245 && (! bfd_is_com_section (bfd_asymbol_section (p))
1246 || bfd_is_und_section (bfd_asymbol_section (h->sym)))))
1248 h->sym = p;
1249 /* BSF_OLD_COMMON is a hack to support COFF reloc
1250 reading, and it should go away when the COFF
1251 linker is switched to the new version. */
1252 if (bfd_is_com_section (bfd_asymbol_section (p)))
1253 p->flags |= BSF_OLD_COMMON;
1257 /* Store a back pointer from the symbol to the hash
1258 table entry for the benefit of relaxation code until
1259 it gets rewritten to not use asymbol structures.
1260 Setting this is also used to check whether these
1261 symbols were set up by the generic linker. */
1262 p->udata.p = h;
1266 return true;
1269 /* We use a state table to deal with adding symbols from an object
1270 file. The first index into the state table describes the symbol
1271 from the object file. The second index into the state table is the
1272 type of the symbol in the hash table. */
1274 /* The symbol from the object file is turned into one of these row
1275 values. */
1277 enum link_row
1279 UNDEF_ROW, /* Undefined. */
1280 UNDEFW_ROW, /* Weak undefined. */
1281 DEF_ROW, /* Defined. */
1282 DEFW_ROW, /* Weak defined. */
1283 COMMON_ROW, /* Common. */
1284 INDR_ROW, /* Indirect. */
1285 WARN_ROW, /* Warning. */
1286 SET_ROW /* Member of set. */
1289 /* apparently needed for Hitachi 3050R(HI-UX/WE2)? */
1290 #undef FAIL
1292 /* The actions to take in the state table. */
1294 enum link_action
1296 FAIL, /* Abort. */
1297 UND, /* Mark symbol undefined. */
1298 WEAK, /* Mark symbol weak undefined. */
1299 DEF, /* Mark symbol defined. */
1300 DEFW, /* Mark symbol weak defined. */
1301 COM, /* Mark symbol common. */
1302 REF, /* Mark defined symbol referenced. */
1303 CREF, /* Possibly warn about common reference to defined symbol. */
1304 CDEF, /* Define existing common symbol. */
1305 NOACT, /* No action. */
1306 BIG, /* Mark symbol common using largest size. */
1307 MDEF, /* Multiple definition error. */
1308 MIND, /* Multiple indirect symbols. */
1309 IND, /* Make indirect symbol. */
1310 CIND, /* Make indirect symbol from existing common symbol. */
1311 SET, /* Add value to set. */
1312 MWARN, /* Make warning symbol. */
1313 WARN, /* Warn if referenced, else MWARN. */
1314 CYCLE, /* Repeat with symbol pointed to. */
1315 REFC, /* Mark indirect symbol referenced and then CYCLE. */
1316 WARNC /* Issue warning and then CYCLE. */
1319 /* The state table itself. The first index is a link_row and the
1320 second index is a bfd_link_hash_type. */
1322 static const enum link_action link_action[8][8] =
1324 /* current\prev new undef undefw def defw com indr warn */
1325 /* UNDEF_ROW */ {UND, NOACT, UND, REF, REF, NOACT, REFC, WARNC },
1326 /* UNDEFW_ROW */ {WEAK, NOACT, NOACT, REF, REF, NOACT, REFC, WARNC },
1327 /* DEF_ROW */ {DEF, DEF, DEF, MDEF, DEF, CDEF, MIND, CYCLE },
1328 /* DEFW_ROW */ {DEFW, DEFW, DEFW, NOACT, NOACT, NOACT, NOACT, CYCLE },
1329 /* COMMON_ROW */ {COM, COM, COM, CREF, COM, BIG, REFC, WARNC },
1330 /* INDR_ROW */ {IND, IND, IND, MDEF, IND, CIND, MIND, CYCLE },
1331 /* WARN_ROW */ {MWARN, WARN, WARN, WARN, WARN, WARN, WARN, NOACT },
1332 /* SET_ROW */ {SET, SET, SET, SET, SET, SET, CYCLE, CYCLE }
1335 /* Most of the entries in the LINK_ACTION table are straightforward,
1336 but a few are somewhat subtle.
1338 A reference to an indirect symbol (UNDEF_ROW/indr or
1339 UNDEFW_ROW/indr) is counted as a reference both to the indirect
1340 symbol and to the symbol the indirect symbol points to.
1342 A reference to a warning symbol (UNDEF_ROW/warn or UNDEFW_ROW/warn)
1343 causes the warning to be issued.
1345 A common definition of an indirect symbol (COMMON_ROW/indr) is
1346 treated as a multiple definition error. Likewise for an indirect
1347 definition of a common symbol (INDR_ROW/com).
1349 An indirect definition of a warning (INDR_ROW/warn) does not cause
1350 the warning to be issued.
1352 If a warning is created for an indirect symbol (WARN_ROW/indr) no
1353 warning is created for the symbol the indirect symbol points to.
1355 Adding an entry to a set does not count as a reference to a set,
1356 and no warning is issued (SET_ROW/warn). */
1358 /* Return the BFD in which a hash entry has been defined, if known. */
1360 static bfd *
1361 hash_entry_bfd (struct bfd_link_hash_entry *h)
1363 while (h->type == bfd_link_hash_warning)
1364 h = h->u.i.link;
1365 switch (h->type)
1367 default:
1368 return NULL;
1369 case bfd_link_hash_undefined:
1370 case bfd_link_hash_undefweak:
1371 return h->u.undef.abfd;
1372 case bfd_link_hash_defined:
1373 case bfd_link_hash_defweak:
1374 return h->u.def.section->owner;
1375 case bfd_link_hash_common:
1376 return h->u.c.p->section->owner;
1378 /*NOTREACHED*/
1381 /* Add a symbol to the global hash table.
1382 ABFD is the BFD the symbol comes from.
1383 NAME is the name of the symbol.
1384 FLAGS is the BSF_* bits associated with the symbol.
1385 SECTION is the section in which the symbol is defined; this may be
1386 bfd_und_section_ptr or bfd_com_section_ptr.
1387 VALUE is the value of the symbol, relative to the section.
1388 STRING is used for either an indirect symbol, in which case it is
1389 the name of the symbol to indirect to, or a warning symbol, in
1390 which case it is the warning string.
1391 COPY is TRUE if NAME or STRING must be copied into locally
1392 allocated memory if they need to be saved.
1393 COLLECT is TRUE if we should automatically collect gcc constructor
1394 or destructor names as collect2 does.
1395 HASHP, if not NULL, is a place to store the created hash table
1396 entry; if *HASHP is not NULL, the caller has already looked up
1397 the hash table entry, and stored it in *HASHP. */
1399 bool
1400 _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
1401 bfd *abfd,
1402 const char *name,
1403 flagword flags,
1404 asection *section,
1405 bfd_vma value,
1406 const char *string,
1407 bool copy,
1408 bool collect,
1409 struct bfd_link_hash_entry **hashp)
1411 enum link_row row;
1412 struct bfd_link_hash_entry *h;
1413 struct bfd_link_hash_entry *inh = NULL;
1414 bool cycle;
1416 BFD_ASSERT (section != NULL);
1418 if (bfd_is_ind_section (section)
1419 || (flags & BSF_INDIRECT) != 0)
1421 row = INDR_ROW;
1422 /* Create the indirect symbol here. This is for the benefit of
1423 the plugin "notice" function.
1424 STRING is the name of the symbol we want to indirect to. */
1425 inh = bfd_wrapped_link_hash_lookup (abfd, info, string, true,
1426 copy, false);
1427 if (inh == NULL)
1428 return false;
1430 else if ((flags & BSF_WARNING) != 0)
1431 row = WARN_ROW;
1432 else if ((flags & BSF_CONSTRUCTOR) != 0)
1433 row = SET_ROW;
1434 else if (bfd_is_und_section (section))
1436 if ((flags & BSF_WEAK) != 0)
1437 row = UNDEFW_ROW;
1438 else
1439 row = UNDEF_ROW;
1441 else if ((flags & BSF_WEAK) != 0)
1442 row = DEFW_ROW;
1443 else if (bfd_is_com_section (section))
1445 row = COMMON_ROW;
1446 if (!bfd_link_relocatable (info)
1447 && name != NULL
1448 && name[0] == '_'
1449 && name[1] == '_'
1450 && strcmp (name + (name[2] == '_'), "__gnu_lto_slim") == 0)
1451 _bfd_error_handler
1452 (_("%pB: plugin needed to handle lto object"), abfd);
1454 else
1455 row = DEF_ROW;
1457 if (hashp != NULL && *hashp != NULL)
1458 h = *hashp;
1459 else
1461 if (row == UNDEF_ROW || row == UNDEFW_ROW)
1462 h = bfd_wrapped_link_hash_lookup (abfd, info, name, true, copy, false);
1463 else
1464 h = bfd_link_hash_lookup (info->hash, name, true, copy, false);
1465 if (h == NULL)
1467 if (hashp != NULL)
1468 *hashp = NULL;
1469 return false;
1473 if (info->notice_all
1474 || (info->notice_hash != NULL
1475 && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL))
1477 if (! (*info->callbacks->notice) (info, h, inh,
1478 abfd, section, value, flags))
1479 return false;
1482 if (hashp != NULL)
1483 *hashp = h;
1487 enum link_action action;
1488 int prev;
1490 prev = h->type;
1491 /* Treat symbols defined by early linker script pass as undefined. */
1492 if (h->ldscript_def)
1493 prev = bfd_link_hash_undefined;
1494 cycle = false;
1495 action = link_action[(int) row][prev];
1496 switch (action)
1498 case FAIL:
1499 abort ();
1501 case NOACT:
1502 /* Do nothing. */
1503 break;
1505 case UND:
1506 /* Make a new undefined symbol. */
1507 h->type = bfd_link_hash_undefined;
1508 h->u.undef.abfd = abfd;
1509 bfd_link_add_undef (info->hash, h);
1510 break;
1512 case WEAK:
1513 /* Make a new weak undefined symbol. */
1514 h->type = bfd_link_hash_undefweak;
1515 h->u.undef.abfd = abfd;
1516 break;
1518 case CDEF:
1519 /* We have found a definition for a symbol which was
1520 previously common. */
1521 BFD_ASSERT (h->type == bfd_link_hash_common);
1522 (*info->callbacks->multiple_common) (info, h, abfd,
1523 bfd_link_hash_defined, 0);
1524 /* Fall through. */
1525 case DEF:
1526 case DEFW:
1528 enum bfd_link_hash_type oldtype;
1530 /* Define a symbol. */
1531 oldtype = h->type;
1532 if (action == DEFW)
1533 h->type = bfd_link_hash_defweak;
1534 else
1535 h->type = bfd_link_hash_defined;
1536 h->u.def.section = section;
1537 h->u.def.value = value;
1538 h->linker_def = 0;
1539 h->ldscript_def = 0;
1541 /* If we have been asked to, we act like collect2 and
1542 identify all functions that might be global
1543 constructors and destructors and pass them up in a
1544 callback. We only do this for certain object file
1545 types, since many object file types can handle this
1546 automatically. */
1547 if (collect && name[0] == '_')
1549 const char *s;
1551 /* A constructor or destructor name starts like this:
1552 _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
1553 the second are the same character (we accept any
1554 character there, in case a new object file format
1555 comes along with even worse naming restrictions). */
1557 #define CONS_PREFIX "GLOBAL_"
1558 #define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
1560 s = name + 1;
1561 while (*s == '_')
1562 ++s;
1563 if (s[0] == 'G' && startswith (s, CONS_PREFIX))
1565 char c;
1567 c = s[CONS_PREFIX_LEN + 1];
1568 if ((c == 'I' || c == 'D')
1569 && s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
1571 /* If this is a definition of a symbol which
1572 was previously weakly defined, we are in
1573 trouble. We have already added a
1574 constructor entry for the weak defined
1575 symbol, and now we are trying to add one
1576 for the new symbol. Fortunately, this case
1577 should never arise in practice. */
1578 if (oldtype == bfd_link_hash_defweak)
1579 abort ();
1581 (*info->callbacks->constructor) (info, c == 'I',
1582 h->root.string, abfd,
1583 section, value);
1589 break;
1591 case COM:
1592 /* We have found a common definition for a symbol. */
1593 if (h->type == bfd_link_hash_new)
1594 bfd_link_add_undef (info->hash, h);
1595 h->type = bfd_link_hash_common;
1596 h->u.c.p = (struct bfd_link_hash_common_entry *)
1597 bfd_hash_allocate (&info->hash->table,
1598 sizeof (struct bfd_link_hash_common_entry));
1599 if (h->u.c.p == NULL)
1600 return false;
1602 h->u.c.size = value;
1604 /* Select a default alignment based on the size. This may
1605 be overridden by the caller. */
1607 unsigned int power;
1609 power = bfd_log2 (value);
1610 if (power > 4)
1611 power = 4;
1612 h->u.c.p->alignment_power = power;
1615 /* The section of a common symbol is only used if the common
1616 symbol is actually allocated. It basically provides a
1617 hook for the linker script to decide which output section
1618 the common symbols should be put in. In most cases, the
1619 section of a common symbol will be bfd_com_section_ptr,
1620 the code here will choose a common symbol section named
1621 "COMMON", and the linker script will contain *(COMMON) in
1622 the appropriate place. A few targets use separate common
1623 sections for small symbols, and they require special
1624 handling. */
1625 if (section == bfd_com_section_ptr)
1627 h->u.c.p->section = bfd_make_section_old_way (abfd, "COMMON");
1628 h->u.c.p->section->flags |= SEC_ALLOC;
1630 else if (section->owner != abfd)
1632 h->u.c.p->section = bfd_make_section_old_way (abfd,
1633 section->name);
1634 h->u.c.p->section->flags |= SEC_ALLOC;
1636 else
1637 h->u.c.p->section = section;
1638 h->linker_def = 0;
1639 h->ldscript_def = 0;
1640 break;
1642 case REF:
1643 /* A reference to a defined symbol. */
1644 if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
1645 h->u.undef.next = h;
1646 break;
1648 case BIG:
1649 /* We have found a common definition for a symbol which
1650 already had a common definition. Use the maximum of the
1651 two sizes, and use the section required by the larger symbol. */
1652 BFD_ASSERT (h->type == bfd_link_hash_common);
1653 (*info->callbacks->multiple_common) (info, h, abfd,
1654 bfd_link_hash_common, value);
1655 if (value > h->u.c.size)
1657 unsigned int power;
1659 h->u.c.size = value;
1661 /* Select a default alignment based on the size. This may
1662 be overridden by the caller. */
1663 power = bfd_log2 (value);
1664 if (power > 4)
1665 power = 4;
1666 h->u.c.p->alignment_power = power;
1668 /* Some systems have special treatment for small commons,
1669 hence we want to select the section used by the larger
1670 symbol. This makes sure the symbol does not go in a
1671 small common section if it is now too large. */
1672 if (section == bfd_com_section_ptr)
1674 h->u.c.p->section
1675 = bfd_make_section_old_way (abfd, "COMMON");
1676 h->u.c.p->section->flags |= SEC_ALLOC;
1678 else if (section->owner != abfd)
1680 h->u.c.p->section
1681 = bfd_make_section_old_way (abfd, section->name);
1682 h->u.c.p->section->flags |= SEC_ALLOC;
1684 else
1685 h->u.c.p->section = section;
1687 break;
1689 case CREF:
1690 /* We have found a common definition for a symbol which
1691 was already defined. */
1692 (*info->callbacks->multiple_common) (info, h, abfd,
1693 bfd_link_hash_common, value);
1694 break;
1696 case MIND:
1697 /* Multiple indirect symbols. This is OK if they both point
1698 to the same symbol. */
1699 if (h->u.i.link == inh)
1700 break;
1701 if (h->u.i.link->type == bfd_link_hash_defweak)
1703 /* It is also OK to redefine a symbol that indirects to
1704 a weak definition. So for sym@ver -> sym@@ver where
1705 sym@@ver is weak and we have a new strong sym@ver,
1706 redefine sym@@ver. Of course if there exists
1707 sym -> sym@@ver then this also redefines sym. */
1708 h = h->u.i.link;
1709 cycle = true;
1710 break;
1712 /* Fall through. */
1713 case MDEF:
1714 /* Handle a multiple definition. */
1715 (*info->callbacks->multiple_definition) (info, h,
1716 abfd, section, value);
1717 break;
1719 case CIND:
1720 /* Create an indirect symbol from an existing common symbol. */
1721 BFD_ASSERT (h->type == bfd_link_hash_common);
1722 (*info->callbacks->multiple_common) (info, h, abfd,
1723 bfd_link_hash_indirect, 0);
1724 /* Fall through. */
1725 case IND:
1726 if (inh->type == bfd_link_hash_indirect
1727 && inh->u.i.link == h)
1729 _bfd_error_handler
1730 /* xgettext:c-format */
1731 (_("%pB: indirect symbol `%s' to `%s' is a loop"),
1732 abfd, name, string);
1733 bfd_set_error (bfd_error_invalid_operation);
1734 return false;
1736 if (inh->type == bfd_link_hash_new)
1738 inh->type = bfd_link_hash_undefined;
1739 inh->u.undef.abfd = abfd;
1740 bfd_link_add_undef (info->hash, inh);
1743 /* If the indirect symbol has been referenced, we need to
1744 push the reference down to the symbol we are referencing. */
1745 if (h->type != bfd_link_hash_new)
1747 /* ??? If inh->type == bfd_link_hash_undefweak this
1748 converts inh to bfd_link_hash_undefined. */
1749 row = UNDEF_ROW;
1750 cycle = true;
1753 h->type = bfd_link_hash_indirect;
1754 h->u.i.link = inh;
1755 /* Not setting h = h->u.i.link here means that when cycle is
1756 set above we'll always go to REFC, and then cycle again
1757 to the indirected symbol. This means that any successful
1758 change of an existing symbol to indirect counts as a
1759 reference. ??? That may not be correct when the existing
1760 symbol was defweak. */
1761 break;
1763 case SET:
1764 /* Add an entry to a set. */
1765 (*info->callbacks->add_to_set) (info, h, BFD_RELOC_CTOR,
1766 abfd, section, value);
1767 break;
1769 case WARNC:
1770 /* Issue a warning and cycle, except when the reference is
1771 in LTO IR. */
1772 if (h->u.i.warning != NULL
1773 && (abfd->flags & BFD_PLUGIN) == 0)
1775 (*info->callbacks->warning) (info, h->u.i.warning,
1776 h->root.string, abfd, NULL, 0);
1777 /* Only issue a warning once. */
1778 h->u.i.warning = NULL;
1780 /* Fall through. */
1781 case CYCLE:
1782 /* Try again with the referenced symbol. */
1783 h = h->u.i.link;
1784 cycle = true;
1785 break;
1787 case REFC:
1788 /* A reference to an indirect symbol. */
1789 if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
1790 h->u.undef.next = h;
1791 h = h->u.i.link;
1792 cycle = true;
1793 break;
1795 case WARN:
1796 /* Warn if this symbol has been referenced already from non-IR,
1797 otherwise add a warning. */
1798 if ((!info->lto_plugin_active
1799 && (h->u.undef.next != NULL || info->hash->undefs_tail == h))
1800 || h->non_ir_ref_regular
1801 || h->non_ir_ref_dynamic)
1803 (*info->callbacks->warning) (info, string, h->root.string,
1804 hash_entry_bfd (h), NULL, 0);
1805 /* PR 31067: If garbage collection is enabled then the
1806 referenced symbol may actually be discarded later on.
1807 This could be very confusing to the user. So give them
1808 a hint as to what might be happening. */
1809 if (info->gc_sections)
1810 (*info->callbacks->info)
1811 (_("%P: %pB: note: the message above does not take linker garbage collection into account\n"),
1812 hash_entry_bfd (h));
1813 break;
1815 /* Fall through. */
1816 case MWARN:
1817 /* Make a warning symbol. */
1819 struct bfd_link_hash_entry *sub;
1821 /* STRING is the warning to give. */
1822 sub = ((struct bfd_link_hash_entry *)
1823 ((*info->hash->table.newfunc)
1824 (NULL, &info->hash->table, h->root.string)));
1825 if (sub == NULL)
1826 return false;
1827 *sub = *h;
1828 sub->type = bfd_link_hash_warning;
1829 sub->u.i.link = h;
1830 if (! copy)
1831 sub->u.i.warning = string;
1832 else
1834 char *w;
1835 size_t len = strlen (string) + 1;
1837 w = (char *) bfd_hash_allocate (&info->hash->table, len);
1838 if (w == NULL)
1839 return false;
1840 memcpy (w, string, len);
1841 sub->u.i.warning = w;
1844 bfd_hash_replace (&info->hash->table,
1845 (struct bfd_hash_entry *) h,
1846 (struct bfd_hash_entry *) sub);
1847 if (hashp != NULL)
1848 *hashp = sub;
1850 break;
1853 while (cycle);
1855 return true;
1858 /* Generic final link routine. */
1860 bool
1861 _bfd_generic_final_link (bfd *abfd, struct bfd_link_info *info)
1863 bfd *sub;
1864 asection *o;
1865 struct bfd_link_order *p;
1866 size_t outsymalloc;
1867 struct generic_write_global_symbol_info wginfo;
1869 abfd->outsymbols = NULL;
1870 abfd->symcount = 0;
1871 outsymalloc = 0;
1873 /* Mark all sections which will be included in the output file. */
1874 for (o = abfd->sections; o != NULL; o = o->next)
1875 for (p = o->map_head.link_order; p != NULL; p = p->next)
1876 if (p->type == bfd_indirect_link_order)
1877 p->u.indirect.section->linker_mark = true;
1879 /* Build the output symbol table. */
1880 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
1881 if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
1882 return false;
1884 /* Accumulate the global symbols. */
1885 wginfo.info = info;
1886 wginfo.output_bfd = abfd;
1887 wginfo.psymalloc = &outsymalloc;
1888 wginfo.failed = false;
1889 _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
1890 _bfd_generic_link_write_global_symbol,
1891 &wginfo);
1892 if (wginfo.failed)
1893 return false;
1895 /* Make sure we have a trailing NULL pointer on OUTSYMBOLS. We
1896 shouldn't really need one, since we have SYMCOUNT, but some old
1897 code still expects one. */
1898 if (! generic_add_output_symbol (abfd, &outsymalloc, NULL))
1899 return false;
1901 if (bfd_link_relocatable (info))
1903 /* Allocate space for the output relocs for each section. */
1904 for (o = abfd->sections; o != NULL; o = o->next)
1906 o->reloc_count = 0;
1907 for (p = o->map_head.link_order; p != NULL; p = p->next)
1909 if (p->type == bfd_section_reloc_link_order
1910 || p->type == bfd_symbol_reloc_link_order)
1911 ++o->reloc_count;
1912 else if (p->type == bfd_indirect_link_order)
1914 asection *input_section;
1915 bfd *input_bfd;
1916 long relsize;
1917 arelent **relocs;
1918 asymbol **symbols;
1919 long reloc_count;
1921 input_section = p->u.indirect.section;
1922 input_bfd = input_section->owner;
1923 relsize = bfd_get_reloc_upper_bound (input_bfd,
1924 input_section);
1925 if (relsize < 0)
1926 return false;
1927 relocs = (arelent **) bfd_malloc (relsize);
1928 if (!relocs && relsize != 0)
1929 return false;
1930 symbols = _bfd_generic_link_get_symbols (input_bfd);
1931 reloc_count = bfd_canonicalize_reloc (input_bfd,
1932 input_section,
1933 relocs,
1934 symbols);
1935 free (relocs);
1936 if (reloc_count < 0)
1937 return false;
1938 BFD_ASSERT ((unsigned long) reloc_count
1939 == input_section->reloc_count);
1940 o->reloc_count += reloc_count;
1943 if (o->reloc_count > 0)
1945 bfd_size_type amt;
1947 amt = o->reloc_count;
1948 amt *= sizeof (arelent *);
1949 o->orelocation = (struct reloc_cache_entry **) bfd_alloc (abfd, amt);
1950 if (!o->orelocation)
1951 return false;
1952 o->flags |= SEC_RELOC;
1953 /* Reset the count so that it can be used as an index
1954 when putting in the output relocs. */
1955 o->reloc_count = 0;
1960 /* Handle all the link order information for the sections. */
1961 for (o = abfd->sections; o != NULL; o = o->next)
1963 for (p = o->map_head.link_order; p != NULL; p = p->next)
1965 switch (p->type)
1967 case bfd_section_reloc_link_order:
1968 case bfd_symbol_reloc_link_order:
1969 if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
1970 return false;
1971 break;
1972 case bfd_indirect_link_order:
1973 if (! default_indirect_link_order (abfd, info, o, p, true))
1974 return false;
1975 break;
1976 default:
1977 if (! _bfd_default_link_order (abfd, info, o, p))
1978 return false;
1979 break;
1984 return true;
1987 /* Add an output symbol to the output BFD. */
1989 static bool
1990 generic_add_output_symbol (bfd *output_bfd, size_t *psymalloc, asymbol *sym)
1992 if (!(bfd_applicable_file_flags (output_bfd) & HAS_SYMS))
1993 return true;
1995 if (bfd_get_symcount (output_bfd) >= *psymalloc)
1997 asymbol **newsyms;
1998 bfd_size_type amt;
2000 if (*psymalloc == 0)
2001 *psymalloc = 124;
2002 else
2003 *psymalloc *= 2;
2004 amt = *psymalloc;
2005 amt *= sizeof (asymbol *);
2006 newsyms = (asymbol **) bfd_realloc (bfd_get_outsymbols (output_bfd), amt);
2007 if (newsyms == NULL)
2008 return false;
2009 output_bfd->outsymbols = newsyms;
2012 output_bfd->outsymbols[output_bfd->symcount] = sym;
2013 if (sym != NULL)
2014 ++output_bfd->symcount;
2016 return true;
2019 /* Handle the symbols for an input BFD. */
2021 static bool
2022 _bfd_generic_link_output_symbols (bfd *output_bfd,
2023 bfd *input_bfd,
2024 struct bfd_link_info *info,
2025 size_t *psymalloc)
2027 asymbol **sym_ptr;
2028 asymbol **sym_end;
2030 if (!bfd_generic_link_read_symbols (input_bfd))
2031 return false;
2033 /* Create a filename symbol if we are supposed to. */
2034 if (info->create_object_symbols_section != NULL)
2036 asection *sec;
2038 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
2040 if (sec->output_section == info->create_object_symbols_section)
2042 asymbol *newsym;
2044 newsym = bfd_make_empty_symbol (input_bfd);
2045 if (!newsym)
2046 return false;
2047 newsym->name = bfd_get_filename (input_bfd);
2048 newsym->value = 0;
2049 newsym->flags = BSF_LOCAL | BSF_FILE;
2050 newsym->section = sec;
2052 if (! generic_add_output_symbol (output_bfd, psymalloc,
2053 newsym))
2054 return false;
2056 break;
2061 /* Adjust the values of the globally visible symbols, and write out
2062 local symbols. */
2063 sym_ptr = _bfd_generic_link_get_symbols (input_bfd);
2064 sym_end = sym_ptr + _bfd_generic_link_get_symcount (input_bfd);
2065 for (; sym_ptr < sym_end; sym_ptr++)
2067 asymbol *sym;
2068 struct generic_link_hash_entry *h;
2069 bool output;
2071 h = NULL;
2072 sym = *sym_ptr;
2073 if ((sym->flags & (BSF_INDIRECT
2074 | BSF_WARNING
2075 | BSF_GLOBAL
2076 | BSF_CONSTRUCTOR
2077 | BSF_WEAK)) != 0
2078 || bfd_is_und_section (bfd_asymbol_section (sym))
2079 || bfd_is_com_section (bfd_asymbol_section (sym))
2080 || bfd_is_ind_section (bfd_asymbol_section (sym)))
2082 if (sym->udata.p != NULL)
2083 h = (struct generic_link_hash_entry *) sym->udata.p;
2084 else if ((sym->flags & BSF_CONSTRUCTOR) != 0)
2086 /* This case normally means that the main linker code
2087 deliberately ignored this constructor symbol. We
2088 should just pass it through. This will screw up if
2089 the constructor symbol is from a different,
2090 non-generic, object file format, but the case will
2091 only arise when linking with -r, which will probably
2092 fail anyhow, since there will be no way to represent
2093 the relocs in the output format being used. */
2094 h = NULL;
2096 else if (bfd_is_und_section (bfd_asymbol_section (sym)))
2097 h = ((struct generic_link_hash_entry *)
2098 bfd_wrapped_link_hash_lookup (output_bfd, info,
2099 bfd_asymbol_name (sym),
2100 false, false, true));
2101 else
2102 h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
2103 bfd_asymbol_name (sym),
2104 false, false, true);
2106 if (h != NULL)
2108 /* Force all references to this symbol to point to
2109 the same area in memory. It is possible that
2110 this routine will be called with a hash table
2111 other than a generic hash table, so we double
2112 check that. */
2113 if (info->output_bfd->xvec == input_bfd->xvec)
2115 if (h->sym != NULL)
2116 *sym_ptr = sym = h->sym;
2119 switch (h->root.type)
2121 default:
2122 case bfd_link_hash_new:
2123 abort ();
2124 case bfd_link_hash_undefined:
2125 break;
2126 case bfd_link_hash_undefweak:
2127 sym->flags |= BSF_WEAK;
2128 break;
2129 case bfd_link_hash_indirect:
2130 h = (struct generic_link_hash_entry *) h->root.u.i.link;
2131 /* fall through */
2132 case bfd_link_hash_defined:
2133 sym->flags |= BSF_GLOBAL;
2134 sym->flags &=~ (BSF_WEAK | BSF_CONSTRUCTOR);
2135 sym->value = h->root.u.def.value;
2136 sym->section = h->root.u.def.section;
2137 break;
2138 case bfd_link_hash_defweak:
2139 sym->flags |= BSF_WEAK;
2140 sym->flags &=~ BSF_CONSTRUCTOR;
2141 sym->value = h->root.u.def.value;
2142 sym->section = h->root.u.def.section;
2143 break;
2144 case bfd_link_hash_common:
2145 sym->value = h->root.u.c.size;
2146 sym->flags |= BSF_GLOBAL;
2147 if (! bfd_is_com_section (sym->section))
2149 BFD_ASSERT (bfd_is_und_section (sym->section));
2150 sym->section = bfd_com_section_ptr;
2152 /* We do not set the section of the symbol to
2153 h->root.u.c.p->section. That value was saved so
2154 that we would know where to allocate the symbol
2155 if it was defined. In this case the type is
2156 still bfd_link_hash_common, so we did not define
2157 it, so we do not want to use that section. */
2158 break;
2163 if ((sym->flags & BSF_KEEP) == 0
2164 && (info->strip == strip_all
2165 || (info->strip == strip_some
2166 && bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym),
2167 false, false) == NULL)))
2168 output = false;
2169 else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0)
2171 /* If this symbol is marked as occurring now, rather
2172 than at the end, output it now. This is used for
2173 COFF C_EXT FCN symbols. FIXME: There must be a
2174 better way. */
2175 if (bfd_asymbol_bfd (sym) == input_bfd
2176 && (sym->flags & BSF_NOT_AT_END) != 0)
2177 output = true;
2178 else
2179 output = false;
2181 else if ((sym->flags & BSF_KEEP) != 0)
2182 output = true;
2183 else if (bfd_is_ind_section (sym->section))
2184 output = false;
2185 else if ((sym->flags & BSF_DEBUGGING) != 0)
2187 if (info->strip == strip_none)
2188 output = true;
2189 else
2190 output = false;
2192 else if (bfd_is_und_section (sym->section)
2193 || bfd_is_com_section (sym->section))
2194 output = false;
2195 else if ((sym->flags & BSF_LOCAL) != 0)
2197 if ((sym->flags & BSF_WARNING) != 0)
2198 output = false;
2199 else
2201 switch (info->discard)
2203 default:
2204 case discard_all:
2205 output = false;
2206 break;
2207 case discard_sec_merge:
2208 output = true;
2209 if (bfd_link_relocatable (info)
2210 || ! (sym->section->flags & SEC_MERGE))
2211 break;
2212 /* FALLTHROUGH */
2213 case discard_l:
2214 if (bfd_is_local_label (input_bfd, sym))
2215 output = false;
2216 else
2217 output = true;
2218 break;
2219 case discard_none:
2220 output = true;
2221 break;
2225 else if ((sym->flags & BSF_CONSTRUCTOR))
2227 if (info->strip != strip_all)
2228 output = true;
2229 else
2230 output = false;
2232 else if (sym->flags == 0
2233 && (sym->section->owner->flags & BFD_PLUGIN) != 0)
2234 /* LTO doesn't set symbol information. We get here with the
2235 generic linker for a symbol that was "common" but no longer
2236 needs to be global. */
2237 output = false;
2238 else
2239 abort ();
2241 /* If this symbol is in a section which is not being included
2242 in the output file, then we don't want to output the
2243 symbol. */
2244 if (!bfd_is_abs_section (sym->section)
2245 && bfd_section_removed_from_list (output_bfd,
2246 sym->section->output_section))
2247 output = false;
2249 if (output)
2251 if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
2252 return false;
2253 if (h != NULL)
2254 h->written = true;
2258 return true;
2261 /* Set the section and value of a generic BFD symbol based on a linker
2262 hash table entry. */
2264 static void
2265 set_symbol_from_hash (asymbol *sym, struct bfd_link_hash_entry *h)
2267 switch (h->type)
2269 default:
2270 abort ();
2271 break;
2272 case bfd_link_hash_new:
2273 /* This can happen when a constructor symbol is seen but we are
2274 not building constructors. */
2275 if (sym->section != NULL)
2277 BFD_ASSERT ((sym->flags & BSF_CONSTRUCTOR) != 0);
2279 else
2281 sym->flags |= BSF_CONSTRUCTOR;
2282 sym->section = bfd_abs_section_ptr;
2283 sym->value = 0;
2285 break;
2286 case bfd_link_hash_undefined:
2287 sym->section = bfd_und_section_ptr;
2288 sym->value = 0;
2289 break;
2290 case bfd_link_hash_undefweak:
2291 sym->section = bfd_und_section_ptr;
2292 sym->value = 0;
2293 sym->flags |= BSF_WEAK;
2294 break;
2295 case bfd_link_hash_defined:
2296 sym->section = h->u.def.section;
2297 sym->value = h->u.def.value;
2298 break;
2299 case bfd_link_hash_defweak:
2300 sym->flags |= BSF_WEAK;
2301 sym->section = h->u.def.section;
2302 sym->value = h->u.def.value;
2303 break;
2304 case bfd_link_hash_common:
2305 sym->value = h->u.c.size;
2306 if (sym->section == NULL)
2307 sym->section = bfd_com_section_ptr;
2308 else if (! bfd_is_com_section (sym->section))
2310 BFD_ASSERT (bfd_is_und_section (sym->section));
2311 sym->section = bfd_com_section_ptr;
2313 /* Do not set the section; see _bfd_generic_link_output_symbols. */
2314 break;
2315 case bfd_link_hash_indirect:
2316 case bfd_link_hash_warning:
2317 /* FIXME: What should we do here? */
2318 break;
2322 /* Write out a global symbol, if it hasn't already been written out.
2323 This is called for each symbol in the hash table. */
2325 static bool
2326 _bfd_generic_link_write_global_symbol (struct generic_link_hash_entry *h,
2327 void *data)
2329 struct generic_write_global_symbol_info *wginfo = data;
2330 asymbol *sym;
2332 if (h->written)
2333 return true;
2335 h->written = true;
2337 if (wginfo->info->strip == strip_all
2338 || (wginfo->info->strip == strip_some
2339 && bfd_hash_lookup (wginfo->info->keep_hash, h->root.root.string,
2340 false, false) == NULL))
2341 return true;
2343 if (h->sym != NULL)
2344 sym = h->sym;
2345 else
2347 sym = bfd_make_empty_symbol (wginfo->output_bfd);
2348 if (!sym)
2350 wginfo->failed = true;
2351 return false;
2353 sym->name = h->root.root.string;
2354 sym->flags = 0;
2357 set_symbol_from_hash (sym, &h->root);
2359 sym->flags |= BSF_GLOBAL;
2361 if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
2362 sym))
2364 wginfo->failed = true;
2365 return false;
2368 return true;
2371 /* Create a relocation. */
2373 bool
2374 _bfd_generic_reloc_link_order (bfd *abfd,
2375 struct bfd_link_info *info,
2376 asection *sec,
2377 struct bfd_link_order *link_order)
2379 arelent *r;
2381 if (! bfd_link_relocatable (info))
2382 abort ();
2383 if (sec->orelocation == NULL)
2384 abort ();
2386 r = (arelent *) bfd_alloc (abfd, sizeof (arelent));
2387 if (r == NULL)
2388 return false;
2390 r->address = link_order->offset;
2391 r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
2392 if (r->howto == 0)
2394 bfd_set_error (bfd_error_bad_value);
2395 return false;
2398 /* Get the symbol to use for the relocation. */
2399 if (link_order->type == bfd_section_reloc_link_order)
2400 r->sym_ptr_ptr = &link_order->u.reloc.p->u.section->symbol;
2401 else
2403 struct generic_link_hash_entry *h;
2405 h = ((struct generic_link_hash_entry *)
2406 bfd_wrapped_link_hash_lookup (abfd, info,
2407 link_order->u.reloc.p->u.name,
2408 false, false, true));
2409 if (h == NULL
2410 || ! h->written)
2412 (*info->callbacks->unattached_reloc)
2413 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
2414 bfd_set_error (bfd_error_bad_value);
2415 return false;
2417 r->sym_ptr_ptr = &h->sym;
2420 /* If this is an inplace reloc, write the addend to the object file.
2421 Otherwise, store it in the reloc addend. */
2422 if (! r->howto->partial_inplace)
2423 r->addend = link_order->u.reloc.p->addend;
2424 else
2426 bfd_size_type size;
2427 bfd_reloc_status_type rstat;
2428 bfd_byte *buf;
2429 bool ok;
2430 file_ptr loc;
2432 size = bfd_get_reloc_size (r->howto);
2433 buf = (bfd_byte *) bfd_zmalloc (size);
2434 if (buf == NULL && size != 0)
2435 return false;
2436 rstat = _bfd_relocate_contents (r->howto, abfd,
2437 (bfd_vma) link_order->u.reloc.p->addend,
2438 buf);
2439 switch (rstat)
2441 case bfd_reloc_ok:
2442 break;
2443 default:
2444 case bfd_reloc_outofrange:
2445 abort ();
2446 case bfd_reloc_overflow:
2447 (*info->callbacks->reloc_overflow)
2448 (info, NULL,
2449 (link_order->type == bfd_section_reloc_link_order
2450 ? bfd_section_name (link_order->u.reloc.p->u.section)
2451 : link_order->u.reloc.p->u.name),
2452 r->howto->name, link_order->u.reloc.p->addend,
2453 NULL, NULL, 0);
2454 break;
2456 loc = link_order->offset * bfd_octets_per_byte (abfd, sec);
2457 ok = bfd_set_section_contents (abfd, sec, buf, loc, size);
2458 free (buf);
2459 if (! ok)
2460 return false;
2462 r->addend = 0;
2465 sec->orelocation[sec->reloc_count] = r;
2466 ++sec->reloc_count;
2468 return true;
2471 /* Allocate a new link_order for a section. */
2473 struct bfd_link_order *
2474 bfd_new_link_order (bfd *abfd, asection *section)
2476 size_t amt = sizeof (struct bfd_link_order);
2477 struct bfd_link_order *new_lo;
2479 new_lo = (struct bfd_link_order *) bfd_zalloc (abfd, amt);
2480 if (!new_lo)
2481 return NULL;
2483 new_lo->type = bfd_undefined_link_order;
2485 if (section->map_tail.link_order != NULL)
2486 section->map_tail.link_order->next = new_lo;
2487 else
2488 section->map_head.link_order = new_lo;
2489 section->map_tail.link_order = new_lo;
2491 return new_lo;
2494 /* Default link order processing routine. Note that we can not handle
2495 the reloc_link_order types here, since they depend upon the details
2496 of how the particular backends generates relocs. */
2498 bool
2499 _bfd_default_link_order (bfd *abfd,
2500 struct bfd_link_info *info,
2501 asection *sec,
2502 struct bfd_link_order *link_order)
2504 switch (link_order->type)
2506 case bfd_undefined_link_order:
2507 case bfd_section_reloc_link_order:
2508 case bfd_symbol_reloc_link_order:
2509 default:
2510 abort ();
2511 case bfd_indirect_link_order:
2512 return default_indirect_link_order (abfd, info, sec, link_order,
2513 false);
2514 case bfd_data_link_order:
2515 return default_data_link_order (abfd, info, sec, link_order);
2519 /* Default routine to handle a bfd_data_link_order. */
2521 static bool
2522 default_data_link_order (bfd *abfd,
2523 struct bfd_link_info *info,
2524 asection *sec,
2525 struct bfd_link_order *link_order)
2527 bfd_size_type size;
2528 size_t fill_size;
2529 bfd_byte *fill;
2530 file_ptr loc;
2531 bool result;
2533 BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
2535 size = link_order->size;
2536 if (size == 0)
2537 return true;
2539 fill = link_order->u.data.contents;
2540 fill_size = link_order->u.data.size;
2541 if (fill_size == 0)
2543 fill = abfd->arch_info->fill (size, info->big_endian,
2544 (sec->flags & SEC_CODE) != 0);
2545 if (fill == NULL)
2546 return false;
2548 else if (fill_size < size)
2550 bfd_byte *p;
2551 fill = (bfd_byte *) bfd_malloc (size);
2552 if (fill == NULL)
2553 return false;
2554 p = fill;
2555 if (fill_size == 1)
2556 memset (p, (int) link_order->u.data.contents[0], (size_t) size);
2557 else
2561 memcpy (p, link_order->u.data.contents, fill_size);
2562 p += fill_size;
2563 size -= fill_size;
2565 while (size >= fill_size);
2566 if (size != 0)
2567 memcpy (p, link_order->u.data.contents, (size_t) size);
2568 size = link_order->size;
2572 loc = link_order->offset * bfd_octets_per_byte (abfd, sec);
2573 result = bfd_set_section_contents (abfd, sec, fill, loc, size);
2575 if (fill != link_order->u.data.contents)
2576 free (fill);
2577 return result;
2580 /* Default routine to handle a bfd_indirect_link_order. */
2582 static bool
2583 default_indirect_link_order (bfd *output_bfd,
2584 struct bfd_link_info *info,
2585 asection *output_section,
2586 struct bfd_link_order *link_order,
2587 bool generic_linker)
2589 asection *input_section;
2590 bfd *input_bfd;
2591 bfd_byte *alloced = NULL;
2592 bfd_byte *new_contents;
2593 file_ptr loc;
2595 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
2597 input_section = link_order->u.indirect.section;
2598 input_bfd = input_section->owner;
2599 if (input_section->size == 0)
2600 return true;
2602 BFD_ASSERT (input_section->output_section == output_section);
2603 BFD_ASSERT (input_section->output_offset == link_order->offset);
2604 BFD_ASSERT (input_section->size == link_order->size);
2606 if (bfd_link_relocatable (info)
2607 && input_section->reloc_count > 0
2608 && output_section->orelocation == NULL)
2610 /* Space has not been allocated for the output relocations.
2611 This can happen when we are called by a specific backend
2612 because somebody is attempting to link together different
2613 types of object files. Handling this case correctly is
2614 difficult, and sometimes impossible. */
2615 _bfd_error_handler
2616 /* xgettext:c-format */
2617 (_("attempt to do relocatable link with %s input and %s output"),
2618 bfd_get_target (input_bfd), bfd_get_target (output_bfd));
2619 bfd_set_error (bfd_error_wrong_format);
2620 return false;
2623 if (! generic_linker)
2625 asymbol **sympp;
2626 asymbol **symppend;
2628 /* Get the canonical symbols. The generic linker will always
2629 have retrieved them by this point, but we are being called by
2630 a specific linker, presumably because we are linking
2631 different types of object files together. */
2632 if (!bfd_generic_link_read_symbols (input_bfd))
2633 return false;
2635 /* Since we have been called by a specific linker, rather than
2636 the generic linker, the values of the symbols will not be
2637 right. They will be the values as seen in the input file,
2638 not the values of the final link. We need to fix them up
2639 before we can relocate the section. */
2640 sympp = _bfd_generic_link_get_symbols (input_bfd);
2641 symppend = sympp + _bfd_generic_link_get_symcount (input_bfd);
2642 for (; sympp < symppend; sympp++)
2644 asymbol *sym;
2645 struct bfd_link_hash_entry *h;
2647 sym = *sympp;
2649 if ((sym->flags & (BSF_INDIRECT
2650 | BSF_WARNING
2651 | BSF_GLOBAL
2652 | BSF_CONSTRUCTOR
2653 | BSF_WEAK)) != 0
2654 || bfd_is_und_section (bfd_asymbol_section (sym))
2655 || bfd_is_com_section (bfd_asymbol_section (sym))
2656 || bfd_is_ind_section (bfd_asymbol_section (sym)))
2658 /* sym->udata may have been set by
2659 generic_link_add_symbol_list. */
2660 if (sym->udata.p != NULL)
2661 h = (struct bfd_link_hash_entry *) sym->udata.p;
2662 else if (bfd_is_und_section (bfd_asymbol_section (sym)))
2663 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
2664 bfd_asymbol_name (sym),
2665 false, false, true);
2666 else
2667 h = bfd_link_hash_lookup (info->hash,
2668 bfd_asymbol_name (sym),
2669 false, false, true);
2670 if (h != NULL)
2671 set_symbol_from_hash (sym, h);
2676 if ((output_section->flags & (SEC_GROUP | SEC_LINKER_CREATED)) == SEC_GROUP
2677 && input_section->size != 0)
2679 /* Group section contents are set by bfd_elf_set_group_contents. */
2680 if (!output_bfd->output_has_begun)
2682 /* FIXME: This hack ensures bfd_elf_set_group_contents is called. */
2683 if (!bfd_set_section_contents (output_bfd, output_section, "", 0, 1))
2684 goto error_return;
2686 new_contents = output_section->contents;
2687 BFD_ASSERT (new_contents != NULL);
2688 BFD_ASSERT (input_section->output_offset == 0);
2690 else
2692 /* Get and relocate the section contents. */
2693 new_contents = (bfd_get_relocated_section_contents
2694 (output_bfd, info, link_order, NULL,
2695 bfd_link_relocatable (info),
2696 _bfd_generic_link_get_symbols (input_bfd)));
2697 alloced = new_contents;
2698 if (!new_contents)
2699 goto error_return;
2702 /* Output the section contents. */
2703 loc = (input_section->output_offset
2704 * bfd_octets_per_byte (output_bfd, output_section));
2705 if (! bfd_set_section_contents (output_bfd, output_section,
2706 new_contents, loc, input_section->size))
2707 goto error_return;
2709 free (alloced);
2710 return true;
2712 error_return:
2713 free (alloced);
2714 return false;
2717 /* A little routine to count the number of relocs in a link_order
2718 list. */
2720 unsigned int
2721 _bfd_count_link_order_relocs (struct bfd_link_order *link_order)
2723 register unsigned int c;
2724 register struct bfd_link_order *l;
2726 c = 0;
2727 for (l = link_order; l != NULL; l = l->next)
2729 if (l->type == bfd_section_reloc_link_order
2730 || l->type == bfd_symbol_reloc_link_order)
2731 ++c;
2734 return c;
2738 FUNCTION
2739 bfd_link_split_section
2741 SYNOPSIS
2742 bool bfd_link_split_section (bfd *abfd, asection *sec);
2744 DESCRIPTION
2745 Return nonzero if @var{sec} should be split during a
2746 reloceatable or final link.
2748 .#define bfd_link_split_section(abfd, sec) \
2749 . BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
2754 bool
2755 _bfd_generic_link_split_section (bfd *abfd ATTRIBUTE_UNUSED,
2756 asection *sec ATTRIBUTE_UNUSED)
2758 return false;
2762 FUNCTION
2763 bfd_section_already_linked
2765 SYNOPSIS
2766 bool bfd_section_already_linked (bfd *abfd,
2767 asection *sec,
2768 struct bfd_link_info *info);
2770 DESCRIPTION
2771 Check if @var{data} has been already linked during a reloceatable
2772 or final link. Return TRUE if it has.
2774 .#define bfd_section_already_linked(abfd, sec, info) \
2775 . BFD_SEND (abfd, _section_already_linked, (abfd, sec, info))
2780 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
2781 once into the output. This routine checks each section, and
2782 arrange to discard it if a section of the same name has already
2783 been linked. This code assumes that all relevant sections have the
2784 SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
2785 section name. bfd_section_already_linked is called via
2786 bfd_map_over_sections. */
2788 /* The hash table. */
2790 static struct bfd_hash_table _bfd_section_already_linked_table;
2792 /* Support routines for the hash table used by section_already_linked,
2793 initialize the table, traverse, lookup, fill in an entry and remove
2794 the table. */
2796 void
2797 bfd_section_already_linked_table_traverse
2798 (bool (*func) (struct bfd_section_already_linked_hash_entry *, void *),
2799 void *info)
2801 bfd_hash_traverse (&_bfd_section_already_linked_table,
2802 (bool (*) (struct bfd_hash_entry *, void *)) func,
2803 info);
2806 struct bfd_section_already_linked_hash_entry *
2807 bfd_section_already_linked_table_lookup (const char *name)
2809 return ((struct bfd_section_already_linked_hash_entry *)
2810 bfd_hash_lookup (&_bfd_section_already_linked_table, name,
2811 true, false));
2814 bool
2815 bfd_section_already_linked_table_insert
2816 (struct bfd_section_already_linked_hash_entry *already_linked_list,
2817 asection *sec)
2819 struct bfd_section_already_linked *l;
2821 /* Allocate the memory from the same obstack as the hash table is
2822 kept in. */
2823 l = (struct bfd_section_already_linked *)
2824 bfd_hash_allocate (&_bfd_section_already_linked_table, sizeof *l);
2825 if (l == NULL)
2826 return false;
2827 l->sec = sec;
2828 l->next = already_linked_list->entry;
2829 already_linked_list->entry = l;
2830 return true;
2833 static struct bfd_hash_entry *
2834 already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
2835 struct bfd_hash_table *table,
2836 const char *string ATTRIBUTE_UNUSED)
2838 struct bfd_section_already_linked_hash_entry *ret =
2839 (struct bfd_section_already_linked_hash_entry *)
2840 bfd_hash_allocate (table, sizeof *ret);
2842 if (ret == NULL)
2843 return NULL;
2845 ret->entry = NULL;
2847 return &ret->root;
2850 bool
2851 bfd_section_already_linked_table_init (void)
2853 return bfd_hash_table_init_n (&_bfd_section_already_linked_table,
2854 already_linked_newfunc,
2855 sizeof (struct bfd_section_already_linked_hash_entry),
2856 42);
2859 void
2860 bfd_section_already_linked_table_free (void)
2862 bfd_hash_table_free (&_bfd_section_already_linked_table);
2865 /* Report warnings as appropriate for duplicate section SEC.
2866 Return FALSE if we decide to keep SEC after all. */
2868 bool
2869 _bfd_handle_already_linked (asection *sec,
2870 struct bfd_section_already_linked *l,
2871 struct bfd_link_info *info)
2873 switch (sec->flags & SEC_LINK_DUPLICATES)
2875 default:
2876 abort ();
2878 case SEC_LINK_DUPLICATES_DISCARD:
2879 /* If we found an LTO IR match for this comdat group on
2880 the first pass, replace it with the LTO output on the
2881 second pass. We can't simply choose real object
2882 files over IR because the first pass may contain a
2883 mix of LTO and normal objects and we must keep the
2884 first match, be it IR or real. */
2885 if (sec->owner->lto_output
2886 && (l->sec->owner->flags & BFD_PLUGIN) != 0)
2888 l->sec = sec;
2889 return false;
2891 break;
2893 case SEC_LINK_DUPLICATES_ONE_ONLY:
2894 info->callbacks->einfo
2895 /* xgettext:c-format */
2896 (_("%pB: ignoring duplicate section `%pA'\n"),
2897 sec->owner, sec);
2898 break;
2900 case SEC_LINK_DUPLICATES_SAME_SIZE:
2901 if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
2903 else if (sec->size != l->sec->size)
2904 info->callbacks->einfo
2905 /* xgettext:c-format */
2906 (_("%pB: duplicate section `%pA' has different size\n"),
2907 sec->owner, sec);
2908 break;
2910 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
2911 if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
2913 else if (sec->size != l->sec->size)
2914 info->callbacks->einfo
2915 /* xgettext:c-format */
2916 (_("%pB: duplicate section `%pA' has different size\n"),
2917 sec->owner, sec);
2918 else if (sec->size != 0)
2920 bfd_byte *sec_contents, *l_sec_contents;
2922 if ((sec->flags & SEC_HAS_CONTENTS) == 0
2923 && (l->sec->flags & SEC_HAS_CONTENTS) == 0)
2925 else if ((sec->flags & SEC_HAS_CONTENTS) == 0
2926 || !bfd_malloc_and_get_section (sec->owner, sec,
2927 &sec_contents))
2928 info->callbacks->einfo
2929 /* xgettext:c-format */
2930 (_("%pB: could not read contents of section `%pA'\n"),
2931 sec->owner, sec);
2932 else if ((l->sec->flags & SEC_HAS_CONTENTS) == 0
2933 || !bfd_malloc_and_get_section (l->sec->owner, l->sec,
2934 &l_sec_contents))
2936 info->callbacks->einfo
2937 /* xgettext:c-format */
2938 (_("%pB: could not read contents of section `%pA'\n"),
2939 l->sec->owner, l->sec);
2940 free (sec_contents);
2942 else
2944 if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
2945 info->callbacks->einfo
2946 /* xgettext:c-format */
2947 (_("%pB: duplicate section `%pA' has different contents\n"),
2948 sec->owner, sec);
2949 free (l_sec_contents);
2950 free (sec_contents);
2953 break;
2956 /* Set the output_section field so that lang_add_section
2957 does not create a lang_input_section structure for this
2958 section. Since there might be a symbol in the section
2959 being discarded, we must retain a pointer to the section
2960 which we are really going to use. */
2961 sec->output_section = bfd_abs_section_ptr;
2962 sec->kept_section = l->sec;
2963 return true;
2966 /* This is used on non-ELF inputs. */
2968 bool
2969 _bfd_generic_section_already_linked (bfd *abfd ATTRIBUTE_UNUSED,
2970 asection *sec,
2971 struct bfd_link_info *info)
2973 const char *name;
2974 struct bfd_section_already_linked *l;
2975 struct bfd_section_already_linked_hash_entry *already_linked_list;
2977 if ((sec->flags & SEC_LINK_ONCE) == 0)
2978 return false;
2980 /* The generic linker doesn't handle section groups. */
2981 if ((sec->flags & SEC_GROUP) != 0)
2982 return false;
2984 /* FIXME: When doing a relocatable link, we may have trouble
2985 copying relocations in other sections that refer to local symbols
2986 in the section being discarded. Those relocations will have to
2987 be converted somehow; as of this writing I'm not sure that any of
2988 the backends handle that correctly.
2990 It is tempting to instead not discard link once sections when
2991 doing a relocatable link (technically, they should be discarded
2992 whenever we are building constructors). However, that fails,
2993 because the linker winds up combining all the link once sections
2994 into a single large link once section, which defeats the purpose
2995 of having link once sections in the first place. */
2997 name = bfd_section_name (sec);
2999 already_linked_list = bfd_section_already_linked_table_lookup (name);
3001 l = already_linked_list->entry;
3002 if (l != NULL)
3004 /* The section has already been linked. See if we should
3005 issue a warning. */
3006 return _bfd_handle_already_linked (sec, l, info);
3009 /* This is the first section with this name. Record it. */
3010 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
3011 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
3012 return false;
3015 /* Choose a neighbouring section to S in OBFD that will be output, or
3016 the absolute section if ADDR is out of bounds of the neighbours. */
3018 asection *
3019 _bfd_nearby_section (bfd *obfd, asection *s, bfd_vma addr)
3021 asection *next, *prev, *best;
3023 /* Find preceding kept section. */
3024 for (prev = s->prev; prev != NULL; prev = prev->prev)
3025 if ((prev->flags & SEC_EXCLUDE) == 0
3026 && !bfd_section_removed_from_list (obfd, prev))
3027 break;
3029 /* Find following kept section. Start at prev->next because
3030 other sections may have been added after S was removed. */
3031 if (s->prev != NULL)
3032 next = s->prev->next;
3033 else
3034 next = s->owner->sections;
3035 for (; next != NULL; next = next->next)
3036 if ((next->flags & SEC_EXCLUDE) == 0
3037 && !bfd_section_removed_from_list (obfd, next))
3038 break;
3040 /* Choose better of two sections, based on flags. The idea
3041 is to choose a section that will be in the same segment
3042 as S would have been if it was kept. */
3043 best = next;
3044 if (prev == NULL)
3046 if (next == NULL)
3047 best = bfd_abs_section_ptr;
3049 else if (next == NULL)
3050 best = prev;
3051 else if (((prev->flags ^ next->flags)
3052 & (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_LOAD)) != 0)
3054 if (((next->flags ^ s->flags)
3055 & (SEC_ALLOC | SEC_THREAD_LOCAL)) != 0
3056 /* We prefer to choose a loaded section. Section S
3057 doesn't have SEC_LOAD set (it being excluded, that
3058 part of the flag processing didn't happen) so we
3059 can't compare that flag to those of NEXT and PREV. */
3060 || ((prev->flags & SEC_LOAD) != 0
3061 && (next->flags & SEC_LOAD) == 0))
3062 best = prev;
3064 else if (((prev->flags ^ next->flags) & SEC_READONLY) != 0)
3066 if (((next->flags ^ s->flags) & SEC_READONLY) != 0)
3067 best = prev;
3069 else if (((prev->flags ^ next->flags) & SEC_CODE) != 0)
3071 if (((next->flags ^ s->flags) & SEC_CODE) != 0)
3072 best = prev;
3074 else
3076 /* Flags we care about are the same. Prefer the following
3077 section if that will result in a positive valued sym. */
3078 if (addr < next->vma)
3079 best = prev;
3082 return best;
3085 /* Convert symbols in excluded output sections to use a kept section. */
3087 static bool
3088 fix_syms (struct bfd_link_hash_entry *h, void *data)
3090 bfd *obfd = (bfd *) data;
3092 if (h->type == bfd_link_hash_defined
3093 || h->type == bfd_link_hash_defweak)
3095 asection *s = h->u.def.section;
3096 if (s != NULL
3097 && s->output_section != NULL
3098 && (s->output_section->flags & SEC_EXCLUDE) != 0
3099 && bfd_section_removed_from_list (obfd, s->output_section))
3101 asection *op;
3103 h->u.def.value += s->output_offset + s->output_section->vma;
3104 op = _bfd_nearby_section (obfd, s->output_section, h->u.def.value);
3105 h->u.def.value -= op->vma;
3106 h->u.def.section = op;
3110 return true;
3113 void
3114 _bfd_fix_excluded_sec_syms (bfd *obfd, struct bfd_link_info *info)
3116 bfd_link_hash_traverse (info->hash, fix_syms, obfd);
3120 FUNCTION
3121 bfd_generic_define_common_symbol
3123 SYNOPSIS
3124 bool bfd_generic_define_common_symbol
3125 (bfd *output_bfd, struct bfd_link_info *info,
3126 struct bfd_link_hash_entry *h);
3128 DESCRIPTION
3129 Convert common symbol @var{h} into a defined symbol.
3130 Return TRUE on success and FALSE on failure.
3132 .#define bfd_define_common_symbol(output_bfd, info, h) \
3133 . BFD_SEND (output_bfd, _bfd_define_common_symbol, (output_bfd, info, h))
3137 bool
3138 bfd_generic_define_common_symbol (bfd *output_bfd,
3139 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3140 struct bfd_link_hash_entry *h)
3142 unsigned int power_of_two;
3143 bfd_vma alignment, size;
3144 asection *section;
3146 BFD_ASSERT (h != NULL && h->type == bfd_link_hash_common);
3148 size = h->u.c.size;
3149 power_of_two = h->u.c.p->alignment_power;
3150 section = h->u.c.p->section;
3152 /* Increase the size of the section to align the common symbol.
3153 The alignment must be a power of two. But if the section does
3154 not have any alignment requirement then do not increase the
3155 alignment unnecessarily. */
3156 if (power_of_two)
3157 alignment = bfd_octets_per_byte (output_bfd, section) << power_of_two;
3158 else
3159 alignment = 1;
3160 BFD_ASSERT (alignment != 0 && (alignment & -alignment) == alignment);
3161 section->size += alignment - 1;
3162 section->size &= -alignment;
3164 /* Adjust the section's overall alignment if necessary. */
3165 if (power_of_two > section->alignment_power)
3166 section->alignment_power = power_of_two;
3168 /* Change the symbol from common to defined. */
3169 h->type = bfd_link_hash_defined;
3170 h->u.def.section = section;
3171 h->u.def.value = section->size;
3173 /* Increase the size of the section. */
3174 section->size += size;
3176 /* Make sure the section is allocated in memory, and make sure that
3177 it is no longer a common section. */
3178 section->flags |= SEC_ALLOC;
3179 section->flags &= ~(SEC_IS_COMMON | SEC_HAS_CONTENTS);
3180 return true;
3184 FUNCTION
3185 _bfd_generic_link_hide_symbol
3187 SYNOPSIS
3188 void _bfd_generic_link_hide_symbol
3189 (bfd *output_bfd, struct bfd_link_info *info,
3190 struct bfd_link_hash_entry *h);
3192 DESCRIPTION
3193 Hide symbol @var{h}.
3194 This is an internal function. It should not be called from
3195 outside the BFD library.
3197 .#define bfd_link_hide_symbol(output_bfd, info, h) \
3198 . BFD_SEND (output_bfd, _bfd_link_hide_symbol, (output_bfd, info, h))
3202 void
3203 _bfd_generic_link_hide_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
3204 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3205 struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED)
3210 FUNCTION
3211 bfd_generic_define_start_stop
3213 SYNOPSIS
3214 struct bfd_link_hash_entry *bfd_generic_define_start_stop
3215 (struct bfd_link_info *info,
3216 const char *symbol, asection *sec);
3218 DESCRIPTION
3219 Define a __start, __stop, .startof. or .sizeof. symbol.
3220 Return the symbol or NULL if no such undefined symbol exists.
3222 .#define bfd_define_start_stop(output_bfd, info, symbol, sec) \
3223 . BFD_SEND (output_bfd, _bfd_define_start_stop, (info, symbol, sec))
3227 struct bfd_link_hash_entry *
3228 bfd_generic_define_start_stop (struct bfd_link_info *info,
3229 const char *symbol, asection *sec)
3231 struct bfd_link_hash_entry *h;
3233 h = bfd_link_hash_lookup (info->hash, symbol, false, false, true);
3234 if (h != NULL
3235 && !h->ldscript_def
3236 && (h->type == bfd_link_hash_undefined
3237 || h->type == bfd_link_hash_undefweak))
3239 h->type = bfd_link_hash_defined;
3240 h->u.def.section = sec;
3241 h->u.def.value = 0;
3242 return h;
3244 return NULL;
3248 FUNCTION
3249 bfd_find_version_for_sym
3251 SYNOPSIS
3252 struct bfd_elf_version_tree * bfd_find_version_for_sym
3253 (struct bfd_elf_version_tree *verdefs,
3254 const char *sym_name, bool *hide);
3256 DESCRIPTION
3257 Search an elf version script tree for symbol versioning
3258 info and export / don't-export status for a given symbol.
3259 Return non-NULL on success and NULL on failure; also sets
3260 the output @samp{hide} boolean parameter.
3264 struct bfd_elf_version_tree *
3265 bfd_find_version_for_sym (struct bfd_elf_version_tree *verdefs,
3266 const char *sym_name,
3267 bool *hide)
3269 struct bfd_elf_version_tree *t;
3270 struct bfd_elf_version_tree *local_ver, *global_ver, *exist_ver;
3271 struct bfd_elf_version_tree *star_local_ver, *star_global_ver;
3273 local_ver = NULL;
3274 global_ver = NULL;
3275 star_local_ver = NULL;
3276 star_global_ver = NULL;
3277 exist_ver = NULL;
3278 for (t = verdefs; t != NULL; t = t->next)
3280 if (t->globals.list != NULL)
3282 struct bfd_elf_version_expr *d = NULL;
3284 while ((d = (*t->match) (&t->globals, d, sym_name)) != NULL)
3286 if (d->literal || strcmp (d->pattern, "*") != 0)
3287 global_ver = t;
3288 else
3289 star_global_ver = t;
3290 if (d->symver)
3291 exist_ver = t;
3292 d->script = 1;
3293 /* If the match is a wildcard pattern, keep looking for
3294 a more explicit, perhaps even local, match. */
3295 if (d->literal)
3296 break;
3299 if (d != NULL)
3300 break;
3303 if (t->locals.list != NULL)
3305 struct bfd_elf_version_expr *d = NULL;
3307 while ((d = (*t->match) (&t->locals, d, sym_name)) != NULL)
3309 if (d->literal || strcmp (d->pattern, "*") != 0)
3310 local_ver = t;
3311 else
3312 star_local_ver = t;
3313 /* If the match is a wildcard pattern, keep looking for
3314 a more explicit, perhaps even global, match. */
3315 if (d->literal)
3317 /* An exact match overrides a global wildcard. */
3318 global_ver = NULL;
3319 star_global_ver = NULL;
3320 break;
3324 if (d != NULL)
3325 break;
3329 if (global_ver == NULL && local_ver == NULL)
3330 global_ver = star_global_ver;
3332 if (global_ver != NULL)
3334 /* If we already have a versioned symbol that matches the
3335 node for this symbol, then we don't want to create a
3336 duplicate from the unversioned symbol. Instead hide the
3337 unversioned symbol. */
3338 *hide = exist_ver == global_ver;
3339 return global_ver;
3342 if (local_ver == NULL)
3343 local_ver = star_local_ver;
3345 if (local_ver != NULL)
3347 *hide = true;
3348 return local_ver;
3351 return NULL;
3355 FUNCTION
3356 bfd_hide_sym_by_version
3358 SYNOPSIS
3359 bool bfd_hide_sym_by_version
3360 (struct bfd_elf_version_tree *verdefs, const char *sym_name);
3362 DESCRIPTION
3363 Search an elf version script tree for symbol versioning
3364 info for a given symbol. Return TRUE if the symbol is hidden.
3368 bool
3369 bfd_hide_sym_by_version (struct bfd_elf_version_tree *verdefs,
3370 const char *sym_name)
3372 bool hidden = false;
3373 bfd_find_version_for_sym (verdefs, sym_name, &hidden);
3374 return hidden;
3378 FUNCTION
3379 bfd_link_check_relocs
3381 SYNOPSIS
3382 bool bfd_link_check_relocs
3383 (bfd *abfd, struct bfd_link_info *info);
3385 DESCRIPTION
3386 Checks the relocs in ABFD for validity.
3387 Does not execute the relocs.
3388 Return TRUE if everything is OK, FALSE otherwise.
3389 This is the external entry point to this code.
3392 bool
3393 bfd_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3395 return BFD_SEND (abfd, _bfd_link_check_relocs, (abfd, info));
3399 FUNCTION
3400 _bfd_generic_link_check_relocs
3402 SYNOPSIS
3403 bool _bfd_generic_link_check_relocs
3404 (bfd *abfd, struct bfd_link_info *info);
3406 DESCRIPTION
3407 Stub function for targets that do not implement reloc checking.
3408 Return TRUE.
3409 This is an internal function. It should not be called from
3410 outside the BFD library.
3413 bool
3414 _bfd_generic_link_check_relocs (bfd *abfd ATTRIBUTE_UNUSED,
3415 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3417 return true;
3421 FUNCTION
3422 bfd_merge_private_bfd_data
3424 SYNOPSIS
3425 bool bfd_merge_private_bfd_data
3426 (bfd *ibfd, struct bfd_link_info *info);
3428 DESCRIPTION
3429 Merge private BFD information from the BFD @var{ibfd} to the
3430 the output file BFD when linking. Return <<TRUE>> on success,
3431 <<FALSE>> on error. Possible error returns are:
3433 o <<bfd_error_no_memory>> -
3434 Not enough memory exists to create private data for @var{obfd}.
3436 .#define bfd_merge_private_bfd_data(ibfd, info) \
3437 . BFD_SEND ((info)->output_bfd, _bfd_merge_private_bfd_data, \
3438 . (ibfd, info))
3443 INTERNAL_FUNCTION
3444 _bfd_generic_verify_endian_match
3446 SYNOPSIS
3447 bool _bfd_generic_verify_endian_match
3448 (bfd *ibfd, struct bfd_link_info *info);
3450 DESCRIPTION
3451 Can be used from / for bfd_merge_private_bfd_data to check that
3452 endianness matches between input and output file. Returns
3453 TRUE for a match, otherwise returns FALSE and emits an error.
3456 bool
3457 _bfd_generic_verify_endian_match (bfd *ibfd, struct bfd_link_info *info)
3459 bfd *obfd = info->output_bfd;
3461 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
3462 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
3463 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
3465 if (bfd_big_endian (ibfd))
3466 _bfd_error_handler (_("%pB: compiled for a big endian system "
3467 "and target is little endian"), ibfd);
3468 else
3469 _bfd_error_handler (_("%pB: compiled for a little endian system "
3470 "and target is big endian"), ibfd);
3471 bfd_set_error (bfd_error_wrong_format);
3472 return false;
3475 return true;
3479 _bfd_nolink_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
3480 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3482 return 0;
3485 bool
3486 _bfd_nolink_bfd_relax_section (bfd *abfd,
3487 asection *section ATTRIBUTE_UNUSED,
3488 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
3489 bool *again ATTRIBUTE_UNUSED)
3491 return _bfd_bool_bfd_false_error (abfd);
3494 bfd_byte *
3495 _bfd_nolink_bfd_get_relocated_section_contents
3496 (bfd *abfd,
3497 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
3498 struct bfd_link_order *link_order ATTRIBUTE_UNUSED,
3499 bfd_byte *data ATTRIBUTE_UNUSED,
3500 bool relocatable ATTRIBUTE_UNUSED,
3501 asymbol **symbols ATTRIBUTE_UNUSED)
3503 return (bfd_byte *) _bfd_ptr_bfd_null_error (abfd);
3506 bool
3507 _bfd_nolink_bfd_lookup_section_flags
3508 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3509 struct flag_info *flaginfo ATTRIBUTE_UNUSED,
3510 asection *section)
3512 return _bfd_bool_bfd_false_error (section->owner);
3515 bool
3516 _bfd_nolink_bfd_is_group_section (bfd *abfd,
3517 const asection *sec ATTRIBUTE_UNUSED)
3519 return _bfd_bool_bfd_false_error (abfd);
3522 const char *
3523 _bfd_nolink_bfd_group_name (bfd *abfd,
3524 const asection *sec ATTRIBUTE_UNUSED)
3526 return _bfd_ptr_bfd_null_error (abfd);
3529 bool
3530 _bfd_nolink_bfd_discard_group (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
3532 return _bfd_bool_bfd_false_error (abfd);
3535 struct bfd_link_hash_table *
3536 _bfd_nolink_bfd_link_hash_table_create (bfd *abfd)
3538 return (struct bfd_link_hash_table *) _bfd_ptr_bfd_null_error (abfd);
3541 void
3542 _bfd_nolink_bfd_link_just_syms (asection *sec ATTRIBUTE_UNUSED,
3543 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3547 void
3548 _bfd_nolink_bfd_copy_link_hash_symbol_type
3549 (bfd *abfd ATTRIBUTE_UNUSED,
3550 struct bfd_link_hash_entry *from ATTRIBUTE_UNUSED,
3551 struct bfd_link_hash_entry *to ATTRIBUTE_UNUSED)
3555 bool
3556 _bfd_nolink_bfd_link_split_section (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
3558 return _bfd_bool_bfd_false_error (abfd);
3561 bool
3562 _bfd_nolink_section_already_linked (bfd *abfd,
3563 asection *sec ATTRIBUTE_UNUSED,
3564 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3566 return _bfd_bool_bfd_false_error (abfd);
3569 bool
3570 _bfd_nolink_bfd_define_common_symbol
3571 (bfd *abfd,
3572 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3573 struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED)
3575 return _bfd_bool_bfd_false_error (abfd);
3578 struct bfd_link_hash_entry *
3579 _bfd_nolink_bfd_define_start_stop (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3580 const char *name ATTRIBUTE_UNUSED,
3581 asection *sec)
3583 return (struct bfd_link_hash_entry *) _bfd_ptr_bfd_null_error (sec->owner);