1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2021 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
43 struct elf_info_failed
45 struct bfd_link_info
*info
;
49 /* This structure is used to pass information to
50 _bfd_elf_link_find_version_dependencies. */
52 struct elf_find_verdep_info
54 /* General link information. */
55 struct bfd_link_info
*info
;
56 /* The number of dependencies. */
58 /* Whether we had a failure. */
62 static bool _bfd_elf_fix_symbol_flags
63 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
66 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
67 unsigned long r_symndx
,
70 if (r_symndx
>= cookie
->locsymcount
71 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
73 struct elf_link_hash_entry
*h
;
75 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
77 while (h
->root
.type
== bfd_link_hash_indirect
78 || h
->root
.type
== bfd_link_hash_warning
)
79 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
81 if ((h
->root
.type
== bfd_link_hash_defined
82 || h
->root
.type
== bfd_link_hash_defweak
)
83 && discarded_section (h
->root
.u
.def
.section
))
84 return h
->root
.u
.def
.section
;
90 /* It's not a relocation against a global symbol,
91 but it could be a relocation against a local
92 symbol for a discarded section. */
94 Elf_Internal_Sym
*isym
;
96 /* Need to: get the symbol; get the section. */
97 isym
= &cookie
->locsyms
[r_symndx
];
98 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
100 && discard
? discarded_section (isec
) : 1)
106 /* Define a symbol in a dynamic linkage section. */
108 struct elf_link_hash_entry
*
109 _bfd_elf_define_linkage_sym (bfd
*abfd
,
110 struct bfd_link_info
*info
,
114 struct elf_link_hash_entry
*h
;
115 struct bfd_link_hash_entry
*bh
;
116 const struct elf_backend_data
*bed
;
118 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, false, false, false);
121 /* Zap symbol defined in an as-needed lib that wasn't linked.
122 This is a symptom of a larger problem: Absolute symbols
123 defined in shared libraries can't be overridden, because we
124 lose the link to the bfd which is via the symbol section. */
125 h
->root
.type
= bfd_link_hash_new
;
131 bed
= get_elf_backend_data (abfd
);
132 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
133 sec
, 0, NULL
, false, bed
->collect
,
136 h
= (struct elf_link_hash_entry
*) bh
;
137 BFD_ASSERT (h
!= NULL
);
140 h
->root
.linker_def
= 1;
141 h
->type
= STT_OBJECT
;
142 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
143 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
145 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
150 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
154 struct elf_link_hash_entry
*h
;
155 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
156 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
158 /* This function may be called more than once. */
159 if (htab
->sgot
!= NULL
)
162 flags
= bed
->dynamic_sec_flags
;
164 s
= bfd_make_section_anyway_with_flags (abfd
,
165 (bed
->rela_plts_and_copies_p
166 ? ".rela.got" : ".rel.got"),
167 (bed
->dynamic_sec_flags
170 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
174 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
176 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
180 if (bed
->want_got_plt
)
182 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
184 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
189 /* The first bit of the global offset table is the header. */
190 s
->size
+= bed
->got_header_size
;
192 if (bed
->want_got_sym
)
194 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
195 (or .got.plt) section. We don't do this in the linker script
196 because we don't want to define the symbol if we are not creating
197 a global offset table. */
198 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
199 "_GLOBAL_OFFSET_TABLE_");
200 elf_hash_table (info
)->hgot
= h
;
208 /* Create a strtab to hold the dynamic symbol names. */
210 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
212 struct elf_link_hash_table
*hash_table
;
214 hash_table
= elf_hash_table (info
);
215 if (hash_table
->dynobj
== NULL
)
217 /* We may not set dynobj, an input file holding linker created
218 dynamic sections to abfd, which may be a dynamic object with
219 its own dynamic sections. We need to find a normal input file
220 to hold linker created sections if possible. */
221 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
225 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
227 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0
228 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
229 && elf_object_id (ibfd
) == elf_hash_table_id (hash_table
)
230 && !((s
= ibfd
->sections
) != NULL
231 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
))
237 hash_table
->dynobj
= abfd
;
240 if (hash_table
->dynstr
== NULL
)
242 hash_table
->dynstr
= _bfd_elf_strtab_init ();
243 if (hash_table
->dynstr
== NULL
)
249 /* Create some sections which will be filled in with dynamic linking
250 information. ABFD is an input file which requires dynamic sections
251 to be created. The dynamic sections take up virtual memory space
252 when the final executable is run, so we need to create them before
253 addresses are assigned to the output sections. We work out the
254 actual contents and size of these sections later. */
257 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
261 const struct elf_backend_data
*bed
;
262 struct elf_link_hash_entry
*h
;
264 if (! is_elf_hash_table (info
->hash
))
267 if (elf_hash_table (info
)->dynamic_sections_created
)
270 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
273 abfd
= elf_hash_table (info
)->dynobj
;
274 bed
= get_elf_backend_data (abfd
);
276 flags
= bed
->dynamic_sec_flags
;
278 /* A dynamically linked executable has a .interp section, but a
279 shared library does not. */
280 if (bfd_link_executable (info
) && !info
->nointerp
)
282 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
283 flags
| SEC_READONLY
);
288 /* Create sections to hold version informations. These are removed
289 if they are not needed. */
290 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
291 flags
| SEC_READONLY
);
293 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
296 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
297 flags
| SEC_READONLY
);
299 || !bfd_set_section_alignment (s
, 1))
302 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
303 flags
| SEC_READONLY
);
305 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
308 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
309 flags
| SEC_READONLY
);
311 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
313 elf_hash_table (info
)->dynsym
= s
;
315 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
316 flags
| SEC_READONLY
);
320 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
322 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
325 /* The special symbol _DYNAMIC is always set to the start of the
326 .dynamic section. We could set _DYNAMIC in a linker script, but we
327 only want to define it if we are, in fact, creating a .dynamic
328 section. We don't want to define it if there is no .dynamic
329 section, since on some ELF platforms the start up code examines it
330 to decide how to initialize the process. */
331 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
332 elf_hash_table (info
)->hdynamic
= h
;
338 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
339 flags
| SEC_READONLY
);
341 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
343 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
346 if (info
->emit_gnu_hash
&& bed
->record_xhash_symbol
== NULL
)
348 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
349 flags
| SEC_READONLY
);
351 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
353 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
354 4 32-bit words followed by variable count of 64-bit words, then
355 variable count of 32-bit words. */
356 if (bed
->s
->arch_size
== 64)
357 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
359 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
362 /* Let the backend create the rest of the sections. This lets the
363 backend set the right flags. The backend will normally create
364 the .got and .plt sections. */
365 if (bed
->elf_backend_create_dynamic_sections
== NULL
366 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
369 elf_hash_table (info
)->dynamic_sections_created
= true;
374 /* Create dynamic sections when linking against a dynamic object. */
377 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
379 flagword flags
, pltflags
;
380 struct elf_link_hash_entry
*h
;
382 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
383 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
385 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
386 .rel[a].bss sections. */
387 flags
= bed
->dynamic_sec_flags
;
390 if (bed
->plt_not_loaded
)
391 /* We do not clear SEC_ALLOC here because we still want the OS to
392 allocate space for the section; it's just that there's nothing
393 to read in from the object file. */
394 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
396 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
397 if (bed
->plt_readonly
)
398 pltflags
|= SEC_READONLY
;
400 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
402 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
406 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
408 if (bed
->want_plt_sym
)
410 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
411 "_PROCEDURE_LINKAGE_TABLE_");
412 elf_hash_table (info
)->hplt
= h
;
417 s
= bfd_make_section_anyway_with_flags (abfd
,
418 (bed
->rela_plts_and_copies_p
419 ? ".rela.plt" : ".rel.plt"),
420 flags
| SEC_READONLY
);
422 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
426 if (! _bfd_elf_create_got_section (abfd
, info
))
429 if (bed
->want_dynbss
)
431 /* The .dynbss section is a place to put symbols which are defined
432 by dynamic objects, are referenced by regular objects, and are
433 not functions. We must allocate space for them in the process
434 image and use a R_*_COPY reloc to tell the dynamic linker to
435 initialize them at run time. The linker script puts the .dynbss
436 section into the .bss section of the final image. */
437 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
438 SEC_ALLOC
| SEC_LINKER_CREATED
);
443 if (bed
->want_dynrelro
)
445 /* Similarly, but for symbols that were originally in read-only
446 sections. This section doesn't really need to have contents,
447 but make it like other .data.rel.ro sections. */
448 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
455 /* The .rel[a].bss section holds copy relocs. This section is not
456 normally needed. We need to create it here, though, so that the
457 linker will map it to an output section. We can't just create it
458 only if we need it, because we will not know whether we need it
459 until we have seen all the input files, and the first time the
460 main linker code calls BFD after examining all the input files
461 (size_dynamic_sections) the input sections have already been
462 mapped to the output sections. If the section turns out not to
463 be needed, we can discard it later. We will never need this
464 section when generating a shared object, since they do not use
466 if (bfd_link_executable (info
))
468 s
= bfd_make_section_anyway_with_flags (abfd
,
469 (bed
->rela_plts_and_copies_p
470 ? ".rela.bss" : ".rel.bss"),
471 flags
| SEC_READONLY
);
473 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
477 if (bed
->want_dynrelro
)
479 s
= (bfd_make_section_anyway_with_flags
480 (abfd
, (bed
->rela_plts_and_copies_p
481 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
482 flags
| SEC_READONLY
));
484 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
486 htab
->sreldynrelro
= s
;
494 /* Record a new dynamic symbol. We record the dynamic symbols as we
495 read the input files, since we need to have a list of all of them
496 before we can determine the final sizes of the output sections.
497 Note that we may actually call this function even though we are not
498 going to output any dynamic symbols; in some cases we know that a
499 symbol should be in the dynamic symbol table, but only if there is
503 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
504 struct elf_link_hash_entry
*h
)
506 if (h
->dynindx
== -1)
508 struct elf_strtab_hash
*dynstr
;
513 if (h
->root
.type
== bfd_link_hash_defined
514 || h
->root
.type
== bfd_link_hash_defweak
)
516 /* An IR symbol should not be made dynamic. */
517 if (h
->root
.u
.def
.section
!= NULL
518 && h
->root
.u
.def
.section
->owner
!= NULL
519 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
523 /* XXX: The ABI draft says the linker must turn hidden and
524 internal symbols into STB_LOCAL symbols when producing the
525 DSO. However, if ld.so honors st_other in the dynamic table,
526 this would not be necessary. */
527 switch (ELF_ST_VISIBILITY (h
->other
))
531 if (h
->root
.type
!= bfd_link_hash_undefined
532 && h
->root
.type
!= bfd_link_hash_undefweak
)
535 if (!elf_hash_table (info
)->is_relocatable_executable
536 || ((h
->root
.type
== bfd_link_hash_defined
537 || h
->root
.type
== bfd_link_hash_defweak
)
538 && h
->root
.u
.def
.section
->owner
!= NULL
539 && h
->root
.u
.def
.section
->owner
->no_export
)
540 || (h
->root
.type
== bfd_link_hash_common
541 && h
->root
.u
.c
.p
->section
->owner
!= NULL
542 && h
->root
.u
.c
.p
->section
->owner
->no_export
))
550 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
551 ++elf_hash_table (info
)->dynsymcount
;
553 dynstr
= elf_hash_table (info
)->dynstr
;
556 /* Create a strtab to hold the dynamic symbol names. */
557 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
562 /* We don't put any version information in the dynamic string
564 name
= h
->root
.root
.string
;
565 p
= strchr (name
, ELF_VER_CHR
);
567 /* We know that the p points into writable memory. In fact,
568 there are only a few symbols that have read-only names, being
569 those like _GLOBAL_OFFSET_TABLE_ that are created specially
570 by the backends. Most symbols will have names pointing into
571 an ELF string table read from a file, or to objalloc memory. */
574 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
579 if (indx
== (size_t) -1)
581 h
->dynstr_index
= indx
;
587 /* Mark a symbol dynamic. */
590 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
591 struct elf_link_hash_entry
*h
,
592 Elf_Internal_Sym
*sym
)
594 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
596 /* It may be called more than once on the same H. */
597 if(h
->dynamic
|| bfd_link_relocatable (info
))
600 if ((info
->dynamic_data
601 && (h
->type
== STT_OBJECT
602 || h
->type
== STT_COMMON
604 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
605 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
608 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
611 /* NB: If a symbol is made dynamic by --dynamic-list, it has
613 h
->root
.non_ir_ref_dynamic
= 1;
617 /* Record an assignment to a symbol made by a linker script. We need
618 this in case some dynamic object refers to this symbol. */
621 bfd_elf_record_link_assignment (bfd
*output_bfd
,
622 struct bfd_link_info
*info
,
627 struct elf_link_hash_entry
*h
, *hv
;
628 struct elf_link_hash_table
*htab
;
629 const struct elf_backend_data
*bed
;
631 if (!is_elf_hash_table (info
->hash
))
634 htab
= elf_hash_table (info
);
635 h
= elf_link_hash_lookup (htab
, name
, !provide
, true, false);
639 if (h
->root
.type
== bfd_link_hash_warning
)
640 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
642 if (h
->versioned
== unknown
)
644 /* Set versioned if symbol version is unknown. */
645 char *version
= strrchr (name
, ELF_VER_CHR
);
648 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
649 h
->versioned
= versioned_hidden
;
651 h
->versioned
= versioned
;
655 /* Symbols defined in a linker script but not referenced anywhere
656 else will have non_elf set. */
659 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
663 switch (h
->root
.type
)
665 case bfd_link_hash_defined
:
666 case bfd_link_hash_defweak
:
667 case bfd_link_hash_common
:
669 case bfd_link_hash_undefweak
:
670 case bfd_link_hash_undefined
:
671 /* Since we're defining the symbol, don't let it seem to have not
672 been defined. record_dynamic_symbol and size_dynamic_sections
673 may depend on this. */
674 h
->root
.type
= bfd_link_hash_new
;
675 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
676 bfd_link_repair_undef_list (&htab
->root
);
678 case bfd_link_hash_new
:
680 case bfd_link_hash_indirect
:
681 /* We had a versioned symbol in a dynamic library. We make the
682 the versioned symbol point to this one. */
683 bed
= get_elf_backend_data (output_bfd
);
685 while (hv
->root
.type
== bfd_link_hash_indirect
686 || hv
->root
.type
== bfd_link_hash_warning
)
687 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
688 /* We don't need to update h->root.u since linker will set them
690 h
->root
.type
= bfd_link_hash_undefined
;
691 hv
->root
.type
= bfd_link_hash_indirect
;
692 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
693 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
700 /* If this symbol is being provided by the linker script, and it is
701 currently defined by a dynamic object, but not by a regular
702 object, then mark it as undefined so that the generic linker will
703 force the correct value. */
707 h
->root
.type
= bfd_link_hash_undefined
;
709 /* If this symbol is currently defined by a dynamic object, but not
710 by a regular object, then clear out any version information because
711 the symbol will not be associated with the dynamic object any
713 if (h
->def_dynamic
&& !h
->def_regular
)
714 h
->verinfo
.verdef
= NULL
;
716 /* Make sure this symbol is not garbage collected. */
723 bed
= get_elf_backend_data (output_bfd
);
724 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
725 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
726 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
729 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
731 if (!bfd_link_relocatable (info
)
733 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
734 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
739 || bfd_link_dll (info
)
740 || elf_hash_table (info
)->is_relocatable_executable
)
744 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
747 /* If this is a weak defined symbol, and we know a corresponding
748 real symbol from the same dynamic object, make sure the real
749 symbol is also made into a dynamic symbol. */
752 struct elf_link_hash_entry
*def
= weakdef (h
);
754 if (def
->dynindx
== -1
755 && !bfd_elf_link_record_dynamic_symbol (info
, def
))
763 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
764 success, and 2 on a failure caused by attempting to record a symbol
765 in a discarded section, eg. a discarded link-once section symbol. */
768 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
773 struct elf_link_local_dynamic_entry
*entry
;
774 struct elf_link_hash_table
*eht
;
775 struct elf_strtab_hash
*dynstr
;
778 Elf_External_Sym_Shndx eshndx
;
779 char esym
[sizeof (Elf64_External_Sym
)];
781 if (! is_elf_hash_table (info
->hash
))
784 /* See if the entry exists already. */
785 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
786 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
789 amt
= sizeof (*entry
);
790 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
794 /* Go find the symbol, so that we can find it's name. */
795 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
796 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
798 bfd_release (input_bfd
, entry
);
802 if (entry
->isym
.st_shndx
!= SHN_UNDEF
803 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
807 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
808 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
810 /* We can still bfd_release here as nothing has done another
811 bfd_alloc. We can't do this later in this function. */
812 bfd_release (input_bfd
, entry
);
817 name
= (bfd_elf_string_from_elf_section
818 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
819 entry
->isym
.st_name
));
821 dynstr
= elf_hash_table (info
)->dynstr
;
824 /* Create a strtab to hold the dynamic symbol names. */
825 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
830 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, false);
831 if (dynstr_index
== (size_t) -1)
833 entry
->isym
.st_name
= dynstr_index
;
835 eht
= elf_hash_table (info
);
837 entry
->next
= eht
->dynlocal
;
838 eht
->dynlocal
= entry
;
839 entry
->input_bfd
= input_bfd
;
840 entry
->input_indx
= input_indx
;
843 /* Whatever binding the symbol had before, it's now local. */
845 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
847 /* The dynindx will be set at the end of size_dynamic_sections. */
852 /* Return the dynindex of a local dynamic symbol. */
855 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
859 struct elf_link_local_dynamic_entry
*e
;
861 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
862 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
867 /* This function is used to renumber the dynamic symbols, if some of
868 them are removed because they are marked as local. This is called
869 via elf_link_hash_traverse. */
872 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
875 size_t *count
= (size_t *) data
;
880 if (h
->dynindx
!= -1)
881 h
->dynindx
= ++(*count
);
887 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
888 STB_LOCAL binding. */
891 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
894 size_t *count
= (size_t *) data
;
896 if (!h
->forced_local
)
899 if (h
->dynindx
!= -1)
900 h
->dynindx
= ++(*count
);
905 /* Return true if the dynamic symbol for a given section should be
906 omitted when creating a shared library. */
908 _bfd_elf_omit_section_dynsym_default (bfd
*output_bfd ATTRIBUTE_UNUSED
,
909 struct bfd_link_info
*info
,
912 struct elf_link_hash_table
*htab
;
915 switch (elf_section_data (p
)->this_hdr
.sh_type
)
919 /* If sh_type is yet undecided, assume it could be
920 SHT_PROGBITS/SHT_NOBITS. */
922 htab
= elf_hash_table (info
);
923 if (htab
->text_index_section
!= NULL
)
924 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
926 return (htab
->dynobj
!= NULL
927 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
928 && ip
->output_section
== p
);
930 /* There shouldn't be section relative relocations
931 against any other section. */
938 _bfd_elf_omit_section_dynsym_all
939 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
940 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
941 asection
*p ATTRIBUTE_UNUSED
)
946 /* Assign dynsym indices. In a shared library we generate a section
947 symbol for each output section, which come first. Next come symbols
948 which have been forced to local binding. Then all of the back-end
949 allocated local dynamic syms, followed by the rest of the global
950 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
951 (This prevents the early call before elf_backend_init_index_section
952 and strip_excluded_output_sections setting dynindx for sections
953 that are stripped.) */
956 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
957 struct bfd_link_info
*info
,
958 unsigned long *section_sym_count
)
960 unsigned long dynsymcount
= 0;
961 bool do_sec
= section_sym_count
!= NULL
;
963 if (bfd_link_pic (info
)
964 || elf_hash_table (info
)->is_relocatable_executable
)
966 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
968 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
969 if ((p
->flags
& SEC_EXCLUDE
) == 0
970 && (p
->flags
& SEC_ALLOC
) != 0
971 && elf_hash_table (info
)->dynamic_relocs
972 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
976 elf_section_data (p
)->dynindx
= dynsymcount
;
979 elf_section_data (p
)->dynindx
= 0;
982 *section_sym_count
= dynsymcount
;
984 elf_link_hash_traverse (elf_hash_table (info
),
985 elf_link_renumber_local_hash_table_dynsyms
,
988 if (elf_hash_table (info
)->dynlocal
)
990 struct elf_link_local_dynamic_entry
*p
;
991 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
992 p
->dynindx
= ++dynsymcount
;
994 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
996 elf_link_hash_traverse (elf_hash_table (info
),
997 elf_link_renumber_hash_table_dynsyms
,
1000 /* There is an unused NULL entry at the head of the table which we
1001 must account for in our count even if the table is empty since it
1002 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1003 .dynamic section. */
1006 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
1010 /* Merge st_other field. */
1013 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
1014 unsigned int st_other
, asection
*sec
,
1015 bool definition
, bool dynamic
)
1017 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1019 /* If st_other has a processor-specific meaning, specific
1020 code might be needed here. */
1021 if (bed
->elf_backend_merge_symbol_attribute
)
1022 (*bed
->elf_backend_merge_symbol_attribute
) (h
, st_other
, definition
,
1027 unsigned symvis
= ELF_ST_VISIBILITY (st_other
);
1028 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
1030 /* Keep the most constraining visibility. Leave the remainder
1031 of the st_other field to elf_backend_merge_symbol_attribute. */
1032 if (symvis
- 1 < hvis
- 1)
1033 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
1036 && ELF_ST_VISIBILITY (st_other
) != STV_DEFAULT
1037 && (sec
->flags
& SEC_READONLY
) == 0)
1038 h
->protected_def
= 1;
1041 /* This function is called when we want to merge a new symbol with an
1042 existing symbol. It handles the various cases which arise when we
1043 find a definition in a dynamic object, or when there is already a
1044 definition in a dynamic object. The new symbol is described by
1045 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1046 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1047 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1048 of an old common symbol. We set OVERRIDE if the old symbol is
1049 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1050 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1051 to change. By OK to change, we mean that we shouldn't warn if the
1052 type or size does change. */
1055 _bfd_elf_merge_symbol (bfd
*abfd
,
1056 struct bfd_link_info
*info
,
1058 Elf_Internal_Sym
*sym
,
1061 struct elf_link_hash_entry
**sym_hash
,
1064 unsigned int *pold_alignment
,
1067 bool *type_change_ok
,
1068 bool *size_change_ok
,
1071 asection
*sec
, *oldsec
;
1072 struct elf_link_hash_entry
*h
;
1073 struct elf_link_hash_entry
*hi
;
1074 struct elf_link_hash_entry
*flip
;
1077 bool newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1078 bool newweak
, oldweak
, newfunc
, oldfunc
;
1079 const struct elf_backend_data
*bed
;
1081 bool default_sym
= *matched
;
1087 bind
= ELF_ST_BIND (sym
->st_info
);
1089 if (! bfd_is_und_section (sec
))
1090 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
1092 h
= ((struct elf_link_hash_entry
*)
1093 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
1098 bed
= get_elf_backend_data (abfd
);
1100 /* NEW_VERSION is the symbol version of the new symbol. */
1101 if (h
->versioned
!= unversioned
)
1103 /* Symbol version is unknown or versioned. */
1104 new_version
= strrchr (name
, ELF_VER_CHR
);
1107 if (h
->versioned
== unknown
)
1109 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1110 h
->versioned
= versioned_hidden
;
1112 h
->versioned
= versioned
;
1115 if (new_version
[0] == '\0')
1119 h
->versioned
= unversioned
;
1124 /* For merging, we only care about real symbols. But we need to make
1125 sure that indirect symbol dynamic flags are updated. */
1127 while (h
->root
.type
== bfd_link_hash_indirect
1128 || h
->root
.type
== bfd_link_hash_warning
)
1129 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1133 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1137 /* OLD_HIDDEN is true if the existing symbol is only visible
1138 to the symbol with the same symbol version. NEW_HIDDEN is
1139 true if the new symbol is only visible to the symbol with
1140 the same symbol version. */
1141 bool old_hidden
= h
->versioned
== versioned_hidden
;
1142 bool new_hidden
= hi
->versioned
== versioned_hidden
;
1143 if (!old_hidden
&& !new_hidden
)
1144 /* The new symbol matches the existing symbol if both
1149 /* OLD_VERSION is the symbol version of the existing
1153 if (h
->versioned
>= versioned
)
1154 old_version
= strrchr (h
->root
.root
.string
,
1159 /* The new symbol matches the existing symbol if they
1160 have the same symbol version. */
1161 *matched
= (old_version
== new_version
1162 || (old_version
!= NULL
1163 && new_version
!= NULL
1164 && strcmp (old_version
, new_version
) == 0));
1169 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1174 switch (h
->root
.type
)
1179 case bfd_link_hash_undefined
:
1180 case bfd_link_hash_undefweak
:
1181 oldbfd
= h
->root
.u
.undef
.abfd
;
1184 case bfd_link_hash_defined
:
1185 case bfd_link_hash_defweak
:
1186 oldbfd
= h
->root
.u
.def
.section
->owner
;
1187 oldsec
= h
->root
.u
.def
.section
;
1190 case bfd_link_hash_common
:
1191 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1192 oldsec
= h
->root
.u
.c
.p
->section
;
1194 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1197 if (poldbfd
&& *poldbfd
== NULL
)
1200 /* Differentiate strong and weak symbols. */
1201 newweak
= bind
== STB_WEAK
;
1202 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1203 || h
->root
.type
== bfd_link_hash_undefweak
);
1205 *pold_weak
= oldweak
;
1207 /* We have to check it for every instance since the first few may be
1208 references and not all compilers emit symbol type for undefined
1210 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1212 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1213 respectively, is from a dynamic object. */
1215 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1217 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1218 syms and defined syms in dynamic libraries respectively.
1219 ref_dynamic on the other hand can be set for a symbol defined in
1220 a dynamic library, and def_dynamic may not be set; When the
1221 definition in a dynamic lib is overridden by a definition in the
1222 executable use of the symbol in the dynamic lib becomes a
1223 reference to the executable symbol. */
1226 if (bfd_is_und_section (sec
))
1228 if (bind
!= STB_WEAK
)
1230 h
->ref_dynamic_nonweak
= 1;
1231 hi
->ref_dynamic_nonweak
= 1;
1236 /* Update the existing symbol only if they match. */
1239 hi
->dynamic_def
= 1;
1243 /* If we just created the symbol, mark it as being an ELF symbol.
1244 Other than that, there is nothing to do--there is no merge issue
1245 with a newly defined symbol--so we just return. */
1247 if (h
->root
.type
== bfd_link_hash_new
)
1253 /* In cases involving weak versioned symbols, we may wind up trying
1254 to merge a symbol with itself. Catch that here, to avoid the
1255 confusion that results if we try to override a symbol with
1256 itself. The additional tests catch cases like
1257 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1258 dynamic object, which we do want to handle here. */
1260 && (newweak
|| oldweak
)
1261 && ((abfd
->flags
& DYNAMIC
) == 0
1262 || !h
->def_regular
))
1267 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1268 else if (oldsec
!= NULL
)
1270 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1271 indices used by MIPS ELF. */
1272 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1275 /* Handle a case where plugin_notice won't be called and thus won't
1276 set the non_ir_ref flags on the first pass over symbols. */
1278 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
)
1279 && newdyn
!= olddyn
)
1281 h
->root
.non_ir_ref_dynamic
= true;
1282 hi
->root
.non_ir_ref_dynamic
= true;
1285 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1286 respectively, appear to be a definition rather than reference. */
1288 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1290 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1291 && h
->root
.type
!= bfd_link_hash_undefweak
1292 && h
->root
.type
!= bfd_link_hash_common
);
1294 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1295 respectively, appear to be a function. */
1297 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1298 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1300 oldfunc
= (h
->type
!= STT_NOTYPE
1301 && bed
->is_function_type (h
->type
));
1303 if (!(newfunc
&& oldfunc
)
1304 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1305 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1306 && h
->type
!= STT_NOTYPE
1307 && (newdef
|| bfd_is_com_section (sec
))
1308 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1310 /* If creating a default indirect symbol ("foo" or "foo@") from
1311 a dynamic versioned definition ("foo@@") skip doing so if
1312 there is an existing regular definition with a different
1313 type. We don't want, for example, a "time" variable in the
1314 executable overriding a "time" function in a shared library. */
1322 /* When adding a symbol from a regular object file after we have
1323 created indirect symbols, undo the indirection and any
1330 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1331 h
->forced_local
= 0;
1335 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1337 h
->root
.type
= bfd_link_hash_undefined
;
1338 h
->root
.u
.undef
.abfd
= abfd
;
1342 h
->root
.type
= bfd_link_hash_new
;
1343 h
->root
.u
.undef
.abfd
= NULL
;
1349 /* Check TLS symbols. We don't check undefined symbols introduced
1350 by "ld -u" which have no type (and oldbfd NULL), and we don't
1351 check symbols from plugins because they also have no type. */
1353 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1354 && (abfd
->flags
& BFD_PLUGIN
) == 0
1355 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1356 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1360 asection
*ntsec
, *tsec
;
1362 if (h
->type
== STT_TLS
)
1383 /* xgettext:c-format */
1384 (_("%s: TLS definition in %pB section %pA "
1385 "mismatches non-TLS definition in %pB section %pA"),
1386 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1387 else if (!tdef
&& !ntdef
)
1389 /* xgettext:c-format */
1390 (_("%s: TLS reference in %pB "
1391 "mismatches non-TLS reference in %pB"),
1392 h
->root
.root
.string
, tbfd
, ntbfd
);
1395 /* xgettext:c-format */
1396 (_("%s: TLS definition in %pB section %pA "
1397 "mismatches non-TLS reference in %pB"),
1398 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1401 /* xgettext:c-format */
1402 (_("%s: TLS reference in %pB "
1403 "mismatches non-TLS definition in %pB section %pA"),
1404 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1406 bfd_set_error (bfd_error_bad_value
);
1410 /* If the old symbol has non-default visibility, we ignore the new
1411 definition from a dynamic object. */
1413 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1414 && !bfd_is_und_section (sec
))
1417 /* Make sure this symbol is dynamic. */
1419 hi
->ref_dynamic
= 1;
1420 /* A protected symbol has external availability. Make sure it is
1421 recorded as dynamic.
1423 FIXME: Should we check type and size for protected symbol? */
1424 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1425 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1430 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1433 /* If the new symbol with non-default visibility comes from a
1434 relocatable file and the old definition comes from a dynamic
1435 object, we remove the old definition. */
1436 if (hi
->root
.type
== bfd_link_hash_indirect
)
1438 /* Handle the case where the old dynamic definition is
1439 default versioned. We need to copy the symbol info from
1440 the symbol with default version to the normal one if it
1441 was referenced before. */
1444 hi
->root
.type
= h
->root
.type
;
1445 h
->root
.type
= bfd_link_hash_indirect
;
1446 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1448 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1449 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1451 /* If the new symbol is hidden or internal, completely undo
1452 any dynamic link state. */
1453 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1454 h
->forced_local
= 0;
1461 /* FIXME: Should we check type and size for protected symbol? */
1471 /* If the old symbol was undefined before, then it will still be
1472 on the undefs list. If the new symbol is undefined or
1473 common, we can't make it bfd_link_hash_new here, because new
1474 undefined or common symbols will be added to the undefs list
1475 by _bfd_generic_link_add_one_symbol. Symbols may not be
1476 added twice to the undefs list. Also, if the new symbol is
1477 undefweak then we don't want to lose the strong undef. */
1478 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1480 h
->root
.type
= bfd_link_hash_undefined
;
1481 h
->root
.u
.undef
.abfd
= abfd
;
1485 h
->root
.type
= bfd_link_hash_new
;
1486 h
->root
.u
.undef
.abfd
= NULL
;
1489 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1491 /* If the new symbol is hidden or internal, completely undo
1492 any dynamic link state. */
1493 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1494 h
->forced_local
= 0;
1500 /* FIXME: Should we check type and size for protected symbol? */
1506 /* If a new weak symbol definition comes from a regular file and the
1507 old symbol comes from a dynamic library, we treat the new one as
1508 strong. Similarly, an old weak symbol definition from a regular
1509 file is treated as strong when the new symbol comes from a dynamic
1510 library. Further, an old weak symbol from a dynamic library is
1511 treated as strong if the new symbol is from a dynamic library.
1512 This reflects the way glibc's ld.so works.
1514 Also allow a weak symbol to override a linker script symbol
1515 defined by an early pass over the script. This is done so the
1516 linker knows the symbol is defined in an object file, for the
1517 DEFINED script function.
1519 Do this before setting *type_change_ok or *size_change_ok so that
1520 we warn properly when dynamic library symbols are overridden. */
1522 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1524 if (olddef
&& newdyn
)
1527 /* Allow changes between different types of function symbol. */
1528 if (newfunc
&& oldfunc
)
1529 *type_change_ok
= true;
1531 /* It's OK to change the type if either the existing symbol or the
1532 new symbol is weak. A type change is also OK if the old symbol
1533 is undefined and the new symbol is defined. */
1538 && h
->root
.type
== bfd_link_hash_undefined
))
1539 *type_change_ok
= true;
1541 /* It's OK to change the size if either the existing symbol or the
1542 new symbol is weak, or if the old symbol is undefined. */
1545 || h
->root
.type
== bfd_link_hash_undefined
)
1546 *size_change_ok
= true;
1548 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1549 symbol, respectively, appears to be a common symbol in a dynamic
1550 object. If a symbol appears in an uninitialized section, and is
1551 not weak, and is not a function, then it may be a common symbol
1552 which was resolved when the dynamic object was created. We want
1553 to treat such symbols specially, because they raise special
1554 considerations when setting the symbol size: if the symbol
1555 appears as a common symbol in a regular object, and the size in
1556 the regular object is larger, we must make sure that we use the
1557 larger size. This problematic case can always be avoided in C,
1558 but it must be handled correctly when using Fortran shared
1561 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1562 likewise for OLDDYNCOMMON and OLDDEF.
1564 Note that this test is just a heuristic, and that it is quite
1565 possible to have an uninitialized symbol in a shared object which
1566 is really a definition, rather than a common symbol. This could
1567 lead to some minor confusion when the symbol really is a common
1568 symbol in some regular object. However, I think it will be
1574 && (sec
->flags
& SEC_ALLOC
) != 0
1575 && (sec
->flags
& SEC_LOAD
) == 0
1578 newdyncommon
= true;
1580 newdyncommon
= false;
1584 && h
->root
.type
== bfd_link_hash_defined
1586 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1587 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1590 olddyncommon
= true;
1592 olddyncommon
= false;
1594 /* We now know everything about the old and new symbols. We ask the
1595 backend to check if we can merge them. */
1596 if (bed
->merge_symbol
!= NULL
)
1598 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1603 /* There are multiple definitions of a normal symbol. Skip the
1604 default symbol as well as definition from an IR object. */
1605 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1606 && !default_sym
&& h
->def_regular
1608 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1609 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1611 /* Handle a multiple definition. */
1612 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1613 abfd
, sec
, *pvalue
);
1618 /* If both the old and the new symbols look like common symbols in a
1619 dynamic object, set the size of the symbol to the larger of the
1624 && sym
->st_size
!= h
->size
)
1626 /* Since we think we have two common symbols, issue a multiple
1627 common warning if desired. Note that we only warn if the
1628 size is different. If the size is the same, we simply let
1629 the old symbol override the new one as normally happens with
1630 symbols defined in dynamic objects. */
1632 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1633 bfd_link_hash_common
, sym
->st_size
);
1634 if (sym
->st_size
> h
->size
)
1635 h
->size
= sym
->st_size
;
1637 *size_change_ok
= true;
1640 /* If we are looking at a dynamic object, and we have found a
1641 definition, we need to see if the symbol was already defined by
1642 some other object. If so, we want to use the existing
1643 definition, and we do not want to report a multiple symbol
1644 definition error; we do this by clobbering *PSEC to be
1645 bfd_und_section_ptr.
1647 We treat a common symbol as a definition if the symbol in the
1648 shared library is a function, since common symbols always
1649 represent variables; this can cause confusion in principle, but
1650 any such confusion would seem to indicate an erroneous program or
1651 shared library. We also permit a common symbol in a regular
1652 object to override a weak symbol in a shared object. */
1657 || (h
->root
.type
== bfd_link_hash_common
1658 && (newweak
|| newfunc
))))
1662 newdyncommon
= false;
1664 *psec
= sec
= bfd_und_section_ptr
;
1665 *size_change_ok
= true;
1667 /* If we get here when the old symbol is a common symbol, then
1668 we are explicitly letting it override a weak symbol or
1669 function in a dynamic object, and we don't want to warn about
1670 a type change. If the old symbol is a defined symbol, a type
1671 change warning may still be appropriate. */
1673 if (h
->root
.type
== bfd_link_hash_common
)
1674 *type_change_ok
= true;
1677 /* Handle the special case of an old common symbol merging with a
1678 new symbol which looks like a common symbol in a shared object.
1679 We change *PSEC and *PVALUE to make the new symbol look like a
1680 common symbol, and let _bfd_generic_link_add_one_symbol do the
1684 && h
->root
.type
== bfd_link_hash_common
)
1688 newdyncommon
= false;
1689 *pvalue
= sym
->st_size
;
1690 *psec
= sec
= bed
->common_section (oldsec
);
1691 *size_change_ok
= true;
1694 /* Skip weak definitions of symbols that are already defined. */
1695 if (newdef
&& olddef
&& newweak
)
1697 /* Don't skip new non-IR weak syms. */
1698 if (!(oldbfd
!= NULL
1699 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1700 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1706 /* Merge st_other. If the symbol already has a dynamic index,
1707 but visibility says it should not be visible, turn it into a
1709 elf_merge_st_other (abfd
, h
, sym
->st_other
, sec
, newdef
, newdyn
);
1710 if (h
->dynindx
!= -1)
1711 switch (ELF_ST_VISIBILITY (h
->other
))
1715 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1720 /* If the old symbol is from a dynamic object, and the new symbol is
1721 a definition which is not from a dynamic object, then the new
1722 symbol overrides the old symbol. Symbols from regular files
1723 always take precedence over symbols from dynamic objects, even if
1724 they are defined after the dynamic object in the link.
1726 As above, we again permit a common symbol in a regular object to
1727 override a definition in a shared object if the shared object
1728 symbol is a function or is weak. */
1733 || (bfd_is_com_section (sec
)
1734 && (oldweak
|| oldfunc
)))
1739 /* Change the hash table entry to undefined, and let
1740 _bfd_generic_link_add_one_symbol do the right thing with the
1743 h
->root
.type
= bfd_link_hash_undefined
;
1744 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1745 *size_change_ok
= true;
1748 olddyncommon
= false;
1750 /* We again permit a type change when a common symbol may be
1751 overriding a function. */
1753 if (bfd_is_com_section (sec
))
1757 /* If a common symbol overrides a function, make sure
1758 that it isn't defined dynamically nor has type
1761 h
->type
= STT_NOTYPE
;
1763 *type_change_ok
= true;
1766 if (hi
->root
.type
== bfd_link_hash_indirect
)
1769 /* This union may have been set to be non-NULL when this symbol
1770 was seen in a dynamic object. We must force the union to be
1771 NULL, so that it is correct for a regular symbol. */
1772 h
->verinfo
.vertree
= NULL
;
1775 /* Handle the special case of a new common symbol merging with an
1776 old symbol that looks like it might be a common symbol defined in
1777 a shared object. Note that we have already handled the case in
1778 which a new common symbol should simply override the definition
1779 in the shared library. */
1782 && bfd_is_com_section (sec
)
1785 /* It would be best if we could set the hash table entry to a
1786 common symbol, but we don't know what to use for the section
1787 or the alignment. */
1788 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1789 bfd_link_hash_common
, sym
->st_size
);
1791 /* If the presumed common symbol in the dynamic object is
1792 larger, pretend that the new symbol has its size. */
1794 if (h
->size
> *pvalue
)
1797 /* We need to remember the alignment required by the symbol
1798 in the dynamic object. */
1799 BFD_ASSERT (pold_alignment
);
1800 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1803 olddyncommon
= false;
1805 h
->root
.type
= bfd_link_hash_undefined
;
1806 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1808 *size_change_ok
= true;
1809 *type_change_ok
= true;
1811 if (hi
->root
.type
== bfd_link_hash_indirect
)
1814 h
->verinfo
.vertree
= NULL
;
1819 /* Handle the case where we had a versioned symbol in a dynamic
1820 library and now find a definition in a normal object. In this
1821 case, we make the versioned symbol point to the normal one. */
1822 flip
->root
.type
= h
->root
.type
;
1823 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1824 h
->root
.type
= bfd_link_hash_indirect
;
1825 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1826 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1830 flip
->ref_dynamic
= 1;
1837 /* This function is called to create an indirect symbol from the
1838 default for the symbol with the default version if needed. The
1839 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1840 set DYNSYM if the new indirect symbol is dynamic. */
1843 _bfd_elf_add_default_symbol (bfd
*abfd
,
1844 struct bfd_link_info
*info
,
1845 struct elf_link_hash_entry
*h
,
1847 Elf_Internal_Sym
*sym
,
1853 bool type_change_ok
;
1854 bool size_change_ok
;
1857 struct elf_link_hash_entry
*hi
;
1858 struct bfd_link_hash_entry
*bh
;
1859 const struct elf_backend_data
*bed
;
1864 size_t len
, shortlen
;
1868 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1871 /* If this symbol has a version, and it is the default version, we
1872 create an indirect symbol from the default name to the fully
1873 decorated name. This will cause external references which do not
1874 specify a version to be bound to this version of the symbol. */
1875 p
= strchr (name
, ELF_VER_CHR
);
1876 if (h
->versioned
== unknown
)
1880 h
->versioned
= unversioned
;
1885 if (p
[1] != ELF_VER_CHR
)
1887 h
->versioned
= versioned_hidden
;
1891 h
->versioned
= versioned
;
1896 /* PR ld/19073: We may see an unversioned definition after the
1902 bed
= get_elf_backend_data (abfd
);
1903 collect
= bed
->collect
;
1904 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1906 shortlen
= p
- name
;
1907 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1908 if (shortname
== NULL
)
1910 memcpy (shortname
, name
, shortlen
);
1911 shortname
[shortlen
] = '\0';
1913 /* We are going to create a new symbol. Merge it with any existing
1914 symbol with this name. For the purposes of the merge, act as
1915 though we were defining the symbol we just defined, although we
1916 actually going to define an indirect symbol. */
1917 type_change_ok
= false;
1918 size_change_ok
= false;
1921 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1922 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1923 &type_change_ok
, &size_change_ok
, &matched
))
1929 if (hi
->def_regular
|| ELF_COMMON_DEF_P (hi
))
1931 /* If the undecorated symbol will have a version added by a
1932 script different to H, then don't indirect to/from the
1933 undecorated symbol. This isn't ideal because we may not yet
1934 have seen symbol versions, if given by a script on the
1935 command line rather than via --version-script. */
1936 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1941 = bfd_find_version_for_sym (info
->version_info
,
1942 hi
->root
.root
.string
, &hide
);
1943 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1945 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
1949 if (hi
->verinfo
.vertree
!= NULL
1950 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1956 /* Add the default symbol if not performing a relocatable link. */
1957 if (! bfd_link_relocatable (info
))
1960 if (bh
->type
== bfd_link_hash_defined
1961 && bh
->u
.def
.section
->owner
!= NULL
1962 && (bh
->u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
1964 /* Mark the previous definition from IR object as
1965 undefined so that the generic linker will override
1967 bh
->type
= bfd_link_hash_undefined
;
1968 bh
->u
.undef
.abfd
= bh
->u
.def
.section
->owner
;
1970 if (! (_bfd_generic_link_add_one_symbol
1971 (info
, abfd
, shortname
, BSF_INDIRECT
,
1972 bfd_ind_section_ptr
,
1973 0, name
, false, collect
, &bh
)))
1975 hi
= (struct elf_link_hash_entry
*) bh
;
1980 /* In this case the symbol named SHORTNAME is overriding the
1981 indirect symbol we want to add. We were planning on making
1982 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1983 is the name without a version. NAME is the fully versioned
1984 name, and it is the default version.
1986 Overriding means that we already saw a definition for the
1987 symbol SHORTNAME in a regular object, and it is overriding
1988 the symbol defined in the dynamic object.
1990 When this happens, we actually want to change NAME, the
1991 symbol we just added, to refer to SHORTNAME. This will cause
1992 references to NAME in the shared object to become references
1993 to SHORTNAME in the regular object. This is what we expect
1994 when we override a function in a shared object: that the
1995 references in the shared object will be mapped to the
1996 definition in the regular object. */
1998 while (hi
->root
.type
== bfd_link_hash_indirect
1999 || hi
->root
.type
== bfd_link_hash_warning
)
2000 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2002 h
->root
.type
= bfd_link_hash_indirect
;
2003 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
2007 hi
->ref_dynamic
= 1;
2011 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
2016 /* Now set HI to H, so that the following code will set the
2017 other fields correctly. */
2021 /* Check if HI is a warning symbol. */
2022 if (hi
->root
.type
== bfd_link_hash_warning
)
2023 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2025 /* If there is a duplicate definition somewhere, then HI may not
2026 point to an indirect symbol. We will have reported an error to
2027 the user in that case. */
2029 if (hi
->root
.type
== bfd_link_hash_indirect
)
2031 struct elf_link_hash_entry
*ht
;
2033 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2034 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2036 /* If we first saw a reference to SHORTNAME with non-default
2037 visibility, merge that visibility to the @@VER symbol. */
2038 elf_merge_st_other (abfd
, ht
, hi
->other
, sec
, true, dynamic
);
2040 /* A reference to the SHORTNAME symbol from a dynamic library
2041 will be satisfied by the versioned symbol at runtime. In
2042 effect, we have a reference to the versioned symbol. */
2043 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2044 hi
->dynamic_def
|= ht
->dynamic_def
;
2046 /* See if the new flags lead us to realize that the symbol must
2052 if (! bfd_link_executable (info
)
2059 if (hi
->ref_regular
)
2065 /* We also need to define an indirection from the nondefault version
2069 len
= strlen (name
);
2070 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2071 if (shortname
== NULL
)
2073 memcpy (shortname
, name
, shortlen
);
2074 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2076 /* Once again, merge with any existing symbol. */
2077 type_change_ok
= false;
2078 size_change_ok
= false;
2080 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2081 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2082 &type_change_ok
, &size_change_ok
, &matched
))
2088 && h
->root
.type
== bfd_link_hash_defweak
2089 && hi
->root
.type
== bfd_link_hash_defined
)
2091 /* We are handling a weak sym@@ver and attempting to define
2092 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2093 new weak sym@ver because there is already a strong sym@ver.
2094 However, sym@ver and sym@@ver are really the same symbol.
2095 The existing strong sym@ver ought to override sym@@ver. */
2096 h
->root
.type
= bfd_link_hash_defined
;
2097 h
->root
.u
.def
.section
= hi
->root
.u
.def
.section
;
2098 h
->root
.u
.def
.value
= hi
->root
.u
.def
.value
;
2099 hi
->root
.type
= bfd_link_hash_indirect
;
2100 hi
->root
.u
.i
.link
= &h
->root
;
2107 /* Here SHORTNAME is a versioned name, so we don't expect to see
2108 the type of override we do in the case above unless it is
2109 overridden by a versioned definition. */
2110 if (hi
->root
.type
!= bfd_link_hash_defined
2111 && hi
->root
.type
!= bfd_link_hash_defweak
)
2113 /* xgettext:c-format */
2114 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2121 if (! (_bfd_generic_link_add_one_symbol
2122 (info
, abfd
, shortname
, BSF_INDIRECT
,
2123 bfd_ind_section_ptr
, 0, name
, false, collect
, &bh
)))
2125 hi
= (struct elf_link_hash_entry
*) bh
;
2128 /* If there is a duplicate definition somewhere, then HI may not
2129 point to an indirect symbol. We will have reported an error
2130 to the user in that case. */
2131 if (hi
->root
.type
== bfd_link_hash_indirect
)
2133 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2134 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2135 hi
->dynamic_def
|= h
->dynamic_def
;
2137 /* If we first saw a reference to @VER symbol with
2138 non-default visibility, merge that visibility to the
2140 elf_merge_st_other (abfd
, h
, hi
->other
, sec
, true, dynamic
);
2142 /* See if the new flags lead us to realize that the symbol
2148 if (! bfd_link_executable (info
)
2154 if (hi
->ref_regular
)
2163 /* This routine is used to export all defined symbols into the dynamic
2164 symbol table. It is called via elf_link_hash_traverse. */
2167 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2169 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2171 /* Ignore indirect symbols. These are added by the versioning code. */
2172 if (h
->root
.type
== bfd_link_hash_indirect
)
2175 /* Ignore this if we won't export it. */
2176 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2179 if (h
->dynindx
== -1
2180 && (h
->def_regular
|| h
->ref_regular
)
2181 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2182 h
->root
.root
.string
))
2184 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2194 /* Look through the symbols which are defined in other shared
2195 libraries and referenced here. Update the list of version
2196 dependencies. This will be put into the .gnu.version_r section.
2197 This function is called via elf_link_hash_traverse. */
2200 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2203 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2204 Elf_Internal_Verneed
*t
;
2205 Elf_Internal_Vernaux
*a
;
2208 /* We only care about symbols defined in shared objects with version
2213 || h
->verinfo
.verdef
== NULL
2214 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2215 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2218 /* See if we already know about this version. */
2219 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2223 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2226 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2227 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2233 /* This is a new version. Add it to tree we are building. */
2238 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2241 rinfo
->failed
= true;
2245 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2246 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2247 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2251 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2254 rinfo
->failed
= true;
2258 /* Note that we are copying a string pointer here, and testing it
2259 above. If bfd_elf_string_from_elf_section is ever changed to
2260 discard the string data when low in memory, this will have to be
2262 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2264 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2265 a
->vna_nextptr
= t
->vn_auxptr
;
2267 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2270 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2277 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2278 hidden. Set *T_P to NULL if there is no match. */
2281 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2282 struct elf_link_hash_entry
*h
,
2283 const char *version_p
,
2284 struct bfd_elf_version_tree
**t_p
,
2287 struct bfd_elf_version_tree
*t
;
2289 /* Look for the version. If we find it, it is no longer weak. */
2290 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2292 if (strcmp (t
->name
, version_p
) == 0)
2296 struct bfd_elf_version_expr
*d
;
2298 len
= version_p
- h
->root
.root
.string
;
2299 alc
= (char *) bfd_malloc (len
);
2302 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2303 alc
[len
- 1] = '\0';
2304 if (alc
[len
- 2] == ELF_VER_CHR
)
2305 alc
[len
- 2] = '\0';
2307 h
->verinfo
.vertree
= t
;
2311 if (t
->globals
.list
!= NULL
)
2312 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2314 /* See if there is anything to force this symbol to
2316 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2318 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2321 && ! info
->export_dynamic
)
2335 /* Return TRUE if the symbol H is hidden by version script. */
2338 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2339 struct elf_link_hash_entry
*h
)
2343 const struct elf_backend_data
*bed
2344 = get_elf_backend_data (info
->output_bfd
);
2346 /* Version script only hides symbols defined in regular objects. */
2347 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2350 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2351 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2353 struct bfd_elf_version_tree
*t
;
2356 if (*p
== ELF_VER_CHR
)
2360 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2364 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2369 /* If we don't have a version for this symbol, see if we can find
2371 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2374 = bfd_find_version_for_sym (info
->version_info
,
2375 h
->root
.root
.string
, &hide
);
2376 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2378 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2386 /* Figure out appropriate versions for all the symbols. We may not
2387 have the version number script until we have read all of the input
2388 files, so until that point we don't know which symbols should be
2389 local. This function is called via elf_link_hash_traverse. */
2392 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2394 struct elf_info_failed
*sinfo
;
2395 struct bfd_link_info
*info
;
2396 const struct elf_backend_data
*bed
;
2397 struct elf_info_failed eif
;
2401 sinfo
= (struct elf_info_failed
*) data
;
2404 /* Fix the symbol flags. */
2407 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2410 sinfo
->failed
= true;
2414 bed
= get_elf_backend_data (info
->output_bfd
);
2416 /* We only need version numbers for symbols defined in regular
2418 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2420 /* Hide symbols defined in discarded input sections. */
2421 if ((h
->root
.type
== bfd_link_hash_defined
2422 || h
->root
.type
== bfd_link_hash_defweak
)
2423 && discarded_section (h
->root
.u
.def
.section
))
2424 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2429 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2430 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2432 struct bfd_elf_version_tree
*t
;
2435 if (*p
== ELF_VER_CHR
)
2438 /* If there is no version string, we can just return out. */
2442 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2444 sinfo
->failed
= true;
2449 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2451 /* If we are building an application, we need to create a
2452 version node for this version. */
2453 if (t
== NULL
&& bfd_link_executable (info
))
2455 struct bfd_elf_version_tree
**pp
;
2458 /* If we aren't going to export this symbol, we don't need
2459 to worry about it. */
2460 if (h
->dynindx
== -1)
2463 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2467 sinfo
->failed
= true;
2472 t
->name_indx
= (unsigned int) -1;
2476 /* Don't count anonymous version tag. */
2477 if (sinfo
->info
->version_info
!= NULL
2478 && sinfo
->info
->version_info
->vernum
== 0)
2480 for (pp
= &sinfo
->info
->version_info
;
2484 t
->vernum
= version_index
;
2488 h
->verinfo
.vertree
= t
;
2492 /* We could not find the version for a symbol when
2493 generating a shared archive. Return an error. */
2495 /* xgettext:c-format */
2496 (_("%pB: version node not found for symbol %s"),
2497 info
->output_bfd
, h
->root
.root
.string
);
2498 bfd_set_error (bfd_error_bad_value
);
2499 sinfo
->failed
= true;
2504 /* If we don't have a version for this symbol, see if we can find
2507 && h
->verinfo
.vertree
== NULL
2508 && sinfo
->info
->version_info
!= NULL
)
2511 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2512 h
->root
.root
.string
, &hide
);
2513 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2514 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2520 /* Read and swap the relocs from the section indicated by SHDR. This
2521 may be either a REL or a RELA section. The relocations are
2522 translated into RELA relocations and stored in INTERNAL_RELOCS,
2523 which should have already been allocated to contain enough space.
2524 The EXTERNAL_RELOCS are a buffer where the external form of the
2525 relocations should be stored.
2527 Returns FALSE if something goes wrong. */
2530 elf_link_read_relocs_from_section (bfd
*abfd
,
2532 Elf_Internal_Shdr
*shdr
,
2533 void *external_relocs
,
2534 Elf_Internal_Rela
*internal_relocs
)
2536 const struct elf_backend_data
*bed
;
2537 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2538 const bfd_byte
*erela
;
2539 const bfd_byte
*erelaend
;
2540 Elf_Internal_Rela
*irela
;
2541 Elf_Internal_Shdr
*symtab_hdr
;
2544 /* Position ourselves at the start of the section. */
2545 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2548 /* Read the relocations. */
2549 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2552 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2553 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2555 bed
= get_elf_backend_data (abfd
);
2557 /* Convert the external relocations to the internal format. */
2558 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2559 swap_in
= bed
->s
->swap_reloc_in
;
2560 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2561 swap_in
= bed
->s
->swap_reloca_in
;
2564 bfd_set_error (bfd_error_wrong_format
);
2568 erela
= (const bfd_byte
*) external_relocs
;
2569 /* Setting erelaend like this and comparing with <= handles case of
2570 a fuzzed object with sh_size not a multiple of sh_entsize. */
2571 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2572 irela
= internal_relocs
;
2573 while (erela
<= erelaend
)
2577 (*swap_in
) (abfd
, erela
, irela
);
2578 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2579 if (bed
->s
->arch_size
== 64)
2583 if ((size_t) r_symndx
>= nsyms
)
2586 /* xgettext:c-format */
2587 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2588 " for offset %#" PRIx64
" in section `%pA'"),
2589 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2590 (uint64_t) irela
->r_offset
, sec
);
2591 bfd_set_error (bfd_error_bad_value
);
2595 else if (r_symndx
!= STN_UNDEF
)
2598 /* xgettext:c-format */
2599 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2600 " for offset %#" PRIx64
" in section `%pA'"
2601 " when the object file has no symbol table"),
2602 abfd
, (uint64_t) r_symndx
,
2603 (uint64_t) irela
->r_offset
, sec
);
2604 bfd_set_error (bfd_error_bad_value
);
2607 irela
+= bed
->s
->int_rels_per_ext_rel
;
2608 erela
+= shdr
->sh_entsize
;
2614 /* Read and swap the relocs for a section O. They may have been
2615 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2616 not NULL, they are used as buffers to read into. They are known to
2617 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2618 the return value is allocated using either malloc or bfd_alloc,
2619 according to the KEEP_MEMORY argument. If O has two relocation
2620 sections (both REL and RELA relocations), then the REL_HDR
2621 relocations will appear first in INTERNAL_RELOCS, followed by the
2622 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2623 update cache_size. */
2626 _bfd_elf_link_info_read_relocs (bfd
*abfd
,
2627 struct bfd_link_info
*info
,
2629 void *external_relocs
,
2630 Elf_Internal_Rela
*internal_relocs
,
2633 void *alloc1
= NULL
;
2634 Elf_Internal_Rela
*alloc2
= NULL
;
2635 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2636 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2637 Elf_Internal_Rela
*internal_rela_relocs
;
2639 if (esdo
->relocs
!= NULL
)
2640 return esdo
->relocs
;
2642 if (o
->reloc_count
== 0)
2645 if (internal_relocs
== NULL
)
2649 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2652 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2654 info
->cache_size
+= size
;
2657 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2658 if (internal_relocs
== NULL
)
2662 if (external_relocs
== NULL
)
2664 bfd_size_type size
= 0;
2667 size
+= esdo
->rel
.hdr
->sh_size
;
2669 size
+= esdo
->rela
.hdr
->sh_size
;
2671 alloc1
= bfd_malloc (size
);
2674 external_relocs
= alloc1
;
2677 internal_rela_relocs
= internal_relocs
;
2680 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2684 external_relocs
= (((bfd_byte
*) external_relocs
)
2685 + esdo
->rel
.hdr
->sh_size
);
2686 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2687 * bed
->s
->int_rels_per_ext_rel
);
2691 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2693 internal_rela_relocs
)))
2696 /* Cache the results for next time, if we can. */
2698 esdo
->relocs
= internal_relocs
;
2702 /* Don't free alloc2, since if it was allocated we are passing it
2703 back (under the name of internal_relocs). */
2705 return internal_relocs
;
2712 bfd_release (abfd
, alloc2
);
2719 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2720 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2721 struct bfd_link_info. */
2724 _bfd_elf_link_read_relocs (bfd
*abfd
,
2726 void *external_relocs
,
2727 Elf_Internal_Rela
*internal_relocs
,
2730 return _bfd_elf_link_info_read_relocs (abfd
, NULL
, o
, external_relocs
,
2731 internal_relocs
, keep_memory
);
2735 /* Compute the size of, and allocate space for, REL_HDR which is the
2736 section header for a section containing relocations for O. */
2739 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2740 struct bfd_elf_section_reloc_data
*reldata
)
2742 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2744 /* That allows us to calculate the size of the section. */
2745 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2747 /* The contents field must last into write_object_contents, so we
2748 allocate it with bfd_alloc rather than malloc. Also since we
2749 cannot be sure that the contents will actually be filled in,
2750 we zero the allocated space. */
2751 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2752 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2755 if (reldata
->hashes
== NULL
&& reldata
->count
)
2757 struct elf_link_hash_entry
**p
;
2759 p
= ((struct elf_link_hash_entry
**)
2760 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2764 reldata
->hashes
= p
;
2770 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2771 originated from the section given by INPUT_REL_HDR) to the
2775 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2776 asection
*input_section
,
2777 Elf_Internal_Shdr
*input_rel_hdr
,
2778 Elf_Internal_Rela
*internal_relocs
,
2779 struct elf_link_hash_entry
**rel_hash
2782 Elf_Internal_Rela
*irela
;
2783 Elf_Internal_Rela
*irelaend
;
2785 struct bfd_elf_section_reloc_data
*output_reldata
;
2786 asection
*output_section
;
2787 const struct elf_backend_data
*bed
;
2788 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2789 struct bfd_elf_section_data
*esdo
;
2791 output_section
= input_section
->output_section
;
2793 bed
= get_elf_backend_data (output_bfd
);
2794 esdo
= elf_section_data (output_section
);
2795 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2797 output_reldata
= &esdo
->rel
;
2798 swap_out
= bed
->s
->swap_reloc_out
;
2800 else if (esdo
->rela
.hdr
2801 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2803 output_reldata
= &esdo
->rela
;
2804 swap_out
= bed
->s
->swap_reloca_out
;
2809 /* xgettext:c-format */
2810 (_("%pB: relocation size mismatch in %pB section %pA"),
2811 output_bfd
, input_section
->owner
, input_section
);
2812 bfd_set_error (bfd_error_wrong_format
);
2816 erel
= output_reldata
->hdr
->contents
;
2817 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2818 irela
= internal_relocs
;
2819 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2820 * bed
->s
->int_rels_per_ext_rel
);
2821 while (irela
< irelaend
)
2823 (*swap_out
) (output_bfd
, irela
, erel
);
2824 irela
+= bed
->s
->int_rels_per_ext_rel
;
2825 erel
+= input_rel_hdr
->sh_entsize
;
2828 /* Bump the counter, so that we know where to add the next set of
2830 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2835 /* Make weak undefined symbols in PIE dynamic. */
2838 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2839 struct elf_link_hash_entry
*h
)
2841 if (bfd_link_pie (info
)
2843 && h
->root
.type
== bfd_link_hash_undefweak
)
2844 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2849 /* Fix up the flags for a symbol. This handles various cases which
2850 can only be fixed after all the input files are seen. This is
2851 currently called by both adjust_dynamic_symbol and
2852 assign_sym_version, which is unnecessary but perhaps more robust in
2853 the face of future changes. */
2856 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2857 struct elf_info_failed
*eif
)
2859 const struct elf_backend_data
*bed
;
2861 /* If this symbol was mentioned in a non-ELF file, try to set
2862 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2863 permit a non-ELF file to correctly refer to a symbol defined in
2864 an ELF dynamic object. */
2867 while (h
->root
.type
== bfd_link_hash_indirect
)
2868 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2870 if (h
->root
.type
!= bfd_link_hash_defined
2871 && h
->root
.type
!= bfd_link_hash_defweak
)
2874 h
->ref_regular_nonweak
= 1;
2878 if (h
->root
.u
.def
.section
->owner
!= NULL
2879 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2880 == bfd_target_elf_flavour
))
2883 h
->ref_regular_nonweak
= 1;
2889 if (h
->dynindx
== -1
2893 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2902 /* Unfortunately, NON_ELF is only correct if the symbol
2903 was first seen in a non-ELF file. Fortunately, if the symbol
2904 was first seen in an ELF file, we're probably OK unless the
2905 symbol was defined in a non-ELF file. Catch that case here.
2906 FIXME: We're still in trouble if the symbol was first seen in
2907 a dynamic object, and then later in a non-ELF regular object. */
2908 if ((h
->root
.type
== bfd_link_hash_defined
2909 || h
->root
.type
== bfd_link_hash_defweak
)
2911 && (h
->root
.u
.def
.section
->owner
!= NULL
2912 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2913 != bfd_target_elf_flavour
)
2914 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2915 && !h
->def_dynamic
)))
2919 /* Backend specific symbol fixup. */
2920 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2921 if (bed
->elf_backend_fixup_symbol
2922 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2925 /* If this is a final link, and the symbol was defined as a common
2926 symbol in a regular object file, and there was no definition in
2927 any dynamic object, then the linker will have allocated space for
2928 the symbol in a common section but the DEF_REGULAR
2929 flag will not have been set. */
2930 if (h
->root
.type
== bfd_link_hash_defined
2934 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2937 /* Symbols defined in discarded sections shouldn't be dynamic. */
2938 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
2939 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
2941 /* If a weak undefined symbol has non-default visibility, we also
2942 hide it from the dynamic linker. */
2943 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2944 && h
->root
.type
== bfd_link_hash_undefweak
)
2945 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
2947 /* A hidden versioned symbol in executable should be forced local if
2948 it is is locally defined, not referenced by shared library and not
2950 else if (bfd_link_executable (eif
->info
)
2951 && h
->versioned
== versioned_hidden
2952 && !eif
->info
->export_dynamic
2956 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
2958 /* If -Bsymbolic was used (which means to bind references to global
2959 symbols to the definition within the shared object), and this
2960 symbol was defined in a regular object, then it actually doesn't
2961 need a PLT entry. Likewise, if the symbol has non-default
2962 visibility. If the symbol has hidden or internal visibility, we
2963 will force it local. */
2964 else if (h
->needs_plt
2965 && bfd_link_pic (eif
->info
)
2966 && is_elf_hash_table (eif
->info
->hash
)
2967 && (SYMBOLIC_BIND (eif
->info
, h
)
2968 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2973 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2974 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2975 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2978 /* If this is a weak defined symbol in a dynamic object, and we know
2979 the real definition in the dynamic object, copy interesting flags
2980 over to the real definition. */
2981 if (h
->is_weakalias
)
2983 struct elf_link_hash_entry
*def
= weakdef (h
);
2985 /* If the real definition is defined by a regular object file,
2986 don't do anything special. See the longer description in
2987 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2988 bfd_link_hash_defined as it was when put on the alias list
2989 then it must have originally been a versioned symbol (for
2990 which a non-versioned indirect symbol is created) and later
2991 a definition for the non-versioned symbol is found. In that
2992 case the indirection is flipped with the versioned symbol
2993 becoming an indirect pointing at the non-versioned symbol.
2994 Thus, not an alias any more. */
2995 if (def
->def_regular
2996 || def
->root
.type
!= bfd_link_hash_defined
)
2999 while ((h
= h
->u
.alias
) != def
)
3000 h
->is_weakalias
= 0;
3004 while (h
->root
.type
== bfd_link_hash_indirect
)
3005 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3006 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3007 || h
->root
.type
== bfd_link_hash_defweak
);
3008 BFD_ASSERT (def
->def_dynamic
);
3009 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
3016 /* Make the backend pick a good value for a dynamic symbol. This is
3017 called via elf_link_hash_traverse, and also calls itself
3021 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
3023 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3024 struct elf_link_hash_table
*htab
;
3025 const struct elf_backend_data
*bed
;
3027 if (! is_elf_hash_table (eif
->info
->hash
))
3030 /* Ignore indirect symbols. These are added by the versioning code. */
3031 if (h
->root
.type
== bfd_link_hash_indirect
)
3034 /* Fix the symbol flags. */
3035 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
3038 htab
= elf_hash_table (eif
->info
);
3039 bed
= get_elf_backend_data (htab
->dynobj
);
3041 if (h
->root
.type
== bfd_link_hash_undefweak
)
3043 if (eif
->info
->dynamic_undefined_weak
== 0)
3044 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3045 else if (eif
->info
->dynamic_undefined_weak
> 0
3047 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3048 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
3049 h
->root
.root
.string
))
3051 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3059 /* If this symbol does not require a PLT entry, and it is not
3060 defined by a dynamic object, or is not referenced by a regular
3061 object, ignore it. We do have to handle a weak defined symbol,
3062 even if no regular object refers to it, if we decided to add it
3063 to the dynamic symbol table. FIXME: Do we normally need to worry
3064 about symbols which are defined by one dynamic object and
3065 referenced by another one? */
3067 && h
->type
!= STT_GNU_IFUNC
3071 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3073 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3077 /* If we've already adjusted this symbol, don't do it again. This
3078 can happen via a recursive call. */
3079 if (h
->dynamic_adjusted
)
3082 /* Don't look at this symbol again. Note that we must set this
3083 after checking the above conditions, because we may look at a
3084 symbol once, decide not to do anything, and then get called
3085 recursively later after REF_REGULAR is set below. */
3086 h
->dynamic_adjusted
= 1;
3088 /* If this is a weak definition, and we know a real definition, and
3089 the real symbol is not itself defined by a regular object file,
3090 then get a good value for the real definition. We handle the
3091 real symbol first, for the convenience of the backend routine.
3093 Note that there is a confusing case here. If the real definition
3094 is defined by a regular object file, we don't get the real symbol
3095 from the dynamic object, but we do get the weak symbol. If the
3096 processor backend uses a COPY reloc, then if some routine in the
3097 dynamic object changes the real symbol, we will not see that
3098 change in the corresponding weak symbol. This is the way other
3099 ELF linkers work as well, and seems to be a result of the shared
3102 I will clarify this issue. Most SVR4 shared libraries define the
3103 variable _timezone and define timezone as a weak synonym. The
3104 tzset call changes _timezone. If you write
3105 extern int timezone;
3107 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3108 you might expect that, since timezone is a synonym for _timezone,
3109 the same number will print both times. However, if the processor
3110 backend uses a COPY reloc, then actually timezone will be copied
3111 into your process image, and, since you define _timezone
3112 yourself, _timezone will not. Thus timezone and _timezone will
3113 wind up at different memory locations. The tzset call will set
3114 _timezone, leaving timezone unchanged. */
3116 if (h
->is_weakalias
)
3118 struct elf_link_hash_entry
*def
= weakdef (h
);
3120 /* If we get to this point, there is an implicit reference to
3121 the alias by a regular object file via the weak symbol H. */
3122 def
->ref_regular
= 1;
3124 /* Ensure that the backend adjust_dynamic_symbol function sees
3125 the strong alias before H by recursively calling ourselves. */
3126 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3130 /* If a symbol has no type and no size and does not require a PLT
3131 entry, then we are probably about to do the wrong thing here: we
3132 are probably going to create a COPY reloc for an empty object.
3133 This case can arise when a shared object is built with assembly
3134 code, and the assembly code fails to set the symbol type. */
3136 && h
->type
== STT_NOTYPE
3139 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3140 h
->root
.root
.string
);
3142 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3151 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3155 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3156 struct elf_link_hash_entry
*h
,
3159 unsigned int power_of_two
;
3161 asection
*sec
= h
->root
.u
.def
.section
;
3163 /* The section alignment of the definition is the maximum alignment
3164 requirement of symbols defined in the section. Since we don't
3165 know the symbol alignment requirement, we start with the
3166 maximum alignment and check low bits of the symbol address
3167 for the minimum alignment. */
3168 power_of_two
= bfd_section_alignment (sec
);
3169 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3170 while ((h
->root
.u
.def
.value
& mask
) != 0)
3176 if (power_of_two
> bfd_section_alignment (dynbss
))
3178 /* Adjust the section alignment if needed. */
3179 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3183 /* We make sure that the symbol will be aligned properly. */
3184 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3186 /* Define the symbol as being at this point in DYNBSS. */
3187 h
->root
.u
.def
.section
= dynbss
;
3188 h
->root
.u
.def
.value
= dynbss
->size
;
3190 /* Increment the size of DYNBSS to make room for the symbol. */
3191 dynbss
->size
+= h
->size
;
3193 /* No error if extern_protected_data is true. */
3194 if (h
->protected_def
3195 && (!info
->extern_protected_data
3196 || (info
->extern_protected_data
< 0
3197 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3198 info
->callbacks
->einfo
3199 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3200 h
->root
.root
.string
);
3205 /* Adjust all external symbols pointing into SEC_MERGE sections
3206 to reflect the object merging within the sections. */
3209 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3213 if ((h
->root
.type
== bfd_link_hash_defined
3214 || h
->root
.type
== bfd_link_hash_defweak
)
3215 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3216 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3218 bfd
*output_bfd
= (bfd
*) data
;
3220 h
->root
.u
.def
.value
=
3221 _bfd_merged_section_offset (output_bfd
,
3222 &h
->root
.u
.def
.section
,
3223 elf_section_data (sec
)->sec_info
,
3224 h
->root
.u
.def
.value
);
3230 /* Returns false if the symbol referred to by H should be considered
3231 to resolve local to the current module, and true if it should be
3232 considered to bind dynamically. */
3235 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3236 struct bfd_link_info
*info
,
3237 bool not_local_protected
)
3239 bool binding_stays_local_p
;
3240 const struct elf_backend_data
*bed
;
3241 struct elf_link_hash_table
*hash_table
;
3246 while (h
->root
.type
== bfd_link_hash_indirect
3247 || h
->root
.type
== bfd_link_hash_warning
)
3248 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3250 /* If it was forced local, then clearly it's not dynamic. */
3251 if (h
->dynindx
== -1)
3253 if (h
->forced_local
)
3256 /* Identify the cases where name binding rules say that a
3257 visible symbol resolves locally. */
3258 binding_stays_local_p
= (bfd_link_executable (info
)
3259 || SYMBOLIC_BIND (info
, h
));
3261 switch (ELF_ST_VISIBILITY (h
->other
))
3268 hash_table
= elf_hash_table (info
);
3269 if (!is_elf_hash_table (&hash_table
->root
))
3272 bed
= get_elf_backend_data (hash_table
->dynobj
);
3274 /* Proper resolution for function pointer equality may require
3275 that these symbols perhaps be resolved dynamically, even though
3276 we should be resolving them to the current module. */
3277 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3278 binding_stays_local_p
= true;
3285 /* If it isn't defined locally, then clearly it's dynamic. */
3286 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3289 /* Otherwise, the symbol is dynamic if binding rules don't tell
3290 us that it remains local. */
3291 return !binding_stays_local_p
;
3294 /* Return true if the symbol referred to by H should be considered
3295 to resolve local to the current module, and false otherwise. Differs
3296 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3297 undefined symbols. The two functions are virtually identical except
3298 for the place where dynindx == -1 is tested. If that test is true,
3299 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3300 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3302 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3303 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3304 treatment of undefined weak symbols. For those that do not make
3305 undefined weak symbols dynamic, both functions may return false. */
3308 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3309 struct bfd_link_info
*info
,
3310 bool local_protected
)
3312 const struct elf_backend_data
*bed
;
3313 struct elf_link_hash_table
*hash_table
;
3315 /* If it's a local sym, of course we resolve locally. */
3319 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3320 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3321 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3324 /* Forced local symbols resolve locally. */
3325 if (h
->forced_local
)
3328 /* Common symbols that become definitions don't get the DEF_REGULAR
3329 flag set, so test it first, and don't bail out. */
3330 if (ELF_COMMON_DEF_P (h
))
3332 /* If we don't have a definition in a regular file, then we can't
3333 resolve locally. The sym is either undefined or dynamic. */
3334 else if (!h
->def_regular
)
3337 /* Non-dynamic symbols resolve locally. */
3338 if (h
->dynindx
== -1)
3341 /* At this point, we know the symbol is defined and dynamic. In an
3342 executable it must resolve locally, likewise when building symbolic
3343 shared libraries. */
3344 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3347 /* Now deal with defined dynamic symbols in shared libraries. Ones
3348 with default visibility might not resolve locally. */
3349 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3352 hash_table
= elf_hash_table (info
);
3353 if (!is_elf_hash_table (&hash_table
->root
))
3356 bed
= get_elf_backend_data (hash_table
->dynobj
);
3358 /* If extern_protected_data is false, STV_PROTECTED non-function
3359 symbols are local. */
3360 if ((!info
->extern_protected_data
3361 || (info
->extern_protected_data
< 0
3362 && !bed
->extern_protected_data
))
3363 && !bed
->is_function_type (h
->type
))
3366 /* Function pointer equality tests may require that STV_PROTECTED
3367 symbols be treated as dynamic symbols. If the address of a
3368 function not defined in an executable is set to that function's
3369 plt entry in the executable, then the address of the function in
3370 a shared library must also be the plt entry in the executable. */
3371 return local_protected
;
3374 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3375 aligned. Returns the first TLS output section. */
3377 struct bfd_section
*
3378 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3380 struct bfd_section
*sec
, *tls
;
3381 unsigned int align
= 0;
3383 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3384 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3388 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3389 if (sec
->alignment_power
> align
)
3390 align
= sec
->alignment_power
;
3392 elf_hash_table (info
)->tls_sec
= tls
;
3394 /* Ensure the alignment of the first section (usually .tdata) is the largest
3395 alignment, so that the tls segment starts aligned. */
3397 tls
->alignment_power
= align
;
3402 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3404 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3405 Elf_Internal_Sym
*sym
)
3407 const struct elf_backend_data
*bed
;
3409 /* Local symbols do not count, but target specific ones might. */
3410 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3411 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3414 bed
= get_elf_backend_data (abfd
);
3415 /* Function symbols do not count. */
3416 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3419 /* If the section is undefined, then so is the symbol. */
3420 if (sym
->st_shndx
== SHN_UNDEF
)
3423 /* If the symbol is defined in the common section, then
3424 it is a common definition and so does not count. */
3425 if (bed
->common_definition (sym
))
3428 /* If the symbol is in a target specific section then we
3429 must rely upon the backend to tell us what it is. */
3430 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3431 /* FIXME - this function is not coded yet:
3433 return _bfd_is_global_symbol_definition (abfd, sym);
3435 Instead for now assume that the definition is not global,
3436 Even if this is wrong, at least the linker will behave
3437 in the same way that it used to do. */
3443 /* Search the symbol table of the archive element of the archive ABFD
3444 whose archive map contains a mention of SYMDEF, and determine if
3445 the symbol is defined in this element. */
3447 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3449 Elf_Internal_Shdr
* hdr
;
3453 Elf_Internal_Sym
*isymbuf
;
3454 Elf_Internal_Sym
*isym
;
3455 Elf_Internal_Sym
*isymend
;
3458 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3462 if (! bfd_check_format (abfd
, bfd_object
))
3465 /* Select the appropriate symbol table. If we don't know if the
3466 object file is an IR object, give linker LTO plugin a chance to
3467 get the correct symbol table. */
3468 if (abfd
->plugin_format
== bfd_plugin_yes
3469 #if BFD_SUPPORTS_PLUGINS
3470 || (abfd
->plugin_format
== bfd_plugin_unknown
3471 && bfd_link_plugin_object_p (abfd
))
3475 /* Use the IR symbol table if the object has been claimed by
3477 abfd
= abfd
->plugin_dummy_bfd
;
3478 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3480 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3481 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3483 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3485 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3487 /* The sh_info field of the symtab header tells us where the
3488 external symbols start. We don't care about the local symbols. */
3489 if (elf_bad_symtab (abfd
))
3491 extsymcount
= symcount
;
3496 extsymcount
= symcount
- hdr
->sh_info
;
3497 extsymoff
= hdr
->sh_info
;
3500 if (extsymcount
== 0)
3503 /* Read in the symbol table. */
3504 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3506 if (isymbuf
== NULL
)
3509 /* Scan the symbol table looking for SYMDEF. */
3511 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3515 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3520 if (strcmp (name
, symdef
->name
) == 0)
3522 result
= is_global_data_symbol_definition (abfd
, isym
);
3532 /* Add an entry to the .dynamic table. */
3535 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3539 struct elf_link_hash_table
*hash_table
;
3540 const struct elf_backend_data
*bed
;
3542 bfd_size_type newsize
;
3543 bfd_byte
*newcontents
;
3544 Elf_Internal_Dyn dyn
;
3546 hash_table
= elf_hash_table (info
);
3547 if (! is_elf_hash_table (&hash_table
->root
))
3550 if (tag
== DT_RELA
|| tag
== DT_REL
)
3551 hash_table
->dynamic_relocs
= true;
3553 bed
= get_elf_backend_data (hash_table
->dynobj
);
3554 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3555 BFD_ASSERT (s
!= NULL
);
3557 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3558 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3559 if (newcontents
== NULL
)
3563 dyn
.d_un
.d_val
= val
;
3564 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3567 s
->contents
= newcontents
;
3572 /* Strip zero-sized dynamic sections. */
3575 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3577 struct elf_link_hash_table
*hash_table
;
3578 const struct elf_backend_data
*bed
;
3579 asection
*s
, *sdynamic
, **pp
;
3580 asection
*rela_dyn
, *rel_dyn
;
3581 Elf_Internal_Dyn dyn
;
3582 bfd_byte
*extdyn
, *next
;
3583 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3584 bool strip_zero_sized
;
3585 bool strip_zero_sized_plt
;
3587 if (bfd_link_relocatable (info
))
3590 hash_table
= elf_hash_table (info
);
3591 if (!is_elf_hash_table (&hash_table
->root
))
3594 if (!hash_table
->dynobj
)
3597 sdynamic
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3601 bed
= get_elf_backend_data (hash_table
->dynobj
);
3602 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3604 strip_zero_sized
= false;
3605 strip_zero_sized_plt
= false;
3607 /* Strip zero-sized dynamic sections. */
3608 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3609 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3610 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3614 || s
== hash_table
->srelplt
->output_section
3615 || s
== hash_table
->splt
->output_section
))
3618 info
->output_bfd
->section_count
--;
3619 strip_zero_sized
= true;
3624 else if (s
== hash_table
->splt
->output_section
)
3626 s
= hash_table
->splt
;
3627 strip_zero_sized_plt
= true;
3630 s
= hash_table
->srelplt
;
3631 s
->flags
|= SEC_EXCLUDE
;
3632 s
->output_section
= bfd_abs_section_ptr
;
3637 if (strip_zero_sized_plt
)
3638 for (extdyn
= sdynamic
->contents
;
3639 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3642 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3643 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3651 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3652 the procedure linkage table (the .plt section) has been
3654 memmove (extdyn
, next
,
3655 sdynamic
->size
- (next
- sdynamic
->contents
));
3660 if (strip_zero_sized
)
3662 /* Regenerate program headers. */
3663 elf_seg_map (info
->output_bfd
) = NULL
;
3664 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
);
3670 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3671 1 if a DT_NEEDED tag already exists, and 0 on success. */
3674 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3676 struct elf_link_hash_table
*hash_table
;
3680 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3683 hash_table
= elf_hash_table (info
);
3684 soname
= elf_dt_name (abfd
);
3685 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, false);
3686 if (strindex
== (size_t) -1)
3689 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3692 const struct elf_backend_data
*bed
;
3695 bed
= get_elf_backend_data (hash_table
->dynobj
);
3696 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3698 for (extdyn
= sdyn
->contents
;
3699 extdyn
< sdyn
->contents
+ sdyn
->size
;
3700 extdyn
+= bed
->s
->sizeof_dyn
)
3702 Elf_Internal_Dyn dyn
;
3704 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3705 if (dyn
.d_tag
== DT_NEEDED
3706 && dyn
.d_un
.d_val
== strindex
)
3708 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3714 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3717 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3723 /* Return true if SONAME is on the needed list between NEEDED and STOP
3724 (or the end of list if STOP is NULL), and needed by a library that
3728 on_needed_list (const char *soname
,
3729 struct bfd_link_needed_list
*needed
,
3730 struct bfd_link_needed_list
*stop
)
3732 struct bfd_link_needed_list
*look
;
3733 for (look
= needed
; look
!= stop
; look
= look
->next
)
3734 if (strcmp (soname
, look
->name
) == 0
3735 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3736 /* If needed by a library that itself is not directly
3737 needed, recursively check whether that library is
3738 indirectly needed. Since we add DT_NEEDED entries to
3739 the end of the list, library dependencies appear after
3740 the library. Therefore search prior to the current
3741 LOOK, preventing possible infinite recursion. */
3742 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3748 /* Sort symbol by value, section, size, and type. */
3750 elf_sort_symbol (const void *arg1
, const void *arg2
)
3752 const struct elf_link_hash_entry
*h1
;
3753 const struct elf_link_hash_entry
*h2
;
3754 bfd_signed_vma vdiff
;
3759 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3760 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3761 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3763 return vdiff
> 0 ? 1 : -1;
3765 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3769 /* Sort so that sized symbols are selected over zero size symbols. */
3770 vdiff
= h1
->size
- h2
->size
;
3772 return vdiff
> 0 ? 1 : -1;
3774 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3775 if (h1
->type
!= h2
->type
)
3776 return h1
->type
- h2
->type
;
3778 /* If symbols are properly sized and typed, and multiple strong
3779 aliases are not defined in a shared library by the user we
3780 shouldn't get here. Unfortunately linker script symbols like
3781 __bss_start sometimes match a user symbol defined at the start of
3782 .bss without proper size and type. We'd like to preference the
3783 user symbol over reserved system symbols. Sort on leading
3785 n1
= h1
->root
.root
.string
;
3786 n2
= h2
->root
.root
.string
;
3799 /* Final sort on name selects user symbols like '_u' over reserved
3800 system symbols like '_Z' and also will avoid qsort instability. */
3804 /* This function is used to adjust offsets into .dynstr for
3805 dynamic symbols. This is called via elf_link_hash_traverse. */
3808 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3810 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3812 if (h
->dynindx
!= -1)
3813 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3817 /* Assign string offsets in .dynstr, update all structures referencing
3821 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3823 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3824 struct elf_link_local_dynamic_entry
*entry
;
3825 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3826 bfd
*dynobj
= hash_table
->dynobj
;
3829 const struct elf_backend_data
*bed
;
3832 _bfd_elf_strtab_finalize (dynstr
);
3833 size
= _bfd_elf_strtab_size (dynstr
);
3835 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3837 if (info
->callbacks
->examine_strtab
)
3838 info
->callbacks
->examine_strtab (dynstr
);
3840 bed
= get_elf_backend_data (dynobj
);
3841 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3842 BFD_ASSERT (sdyn
!= NULL
);
3844 /* Update all .dynamic entries referencing .dynstr strings. */
3845 for (extdyn
= sdyn
->contents
;
3846 extdyn
< sdyn
->contents
+ sdyn
->size
;
3847 extdyn
+= bed
->s
->sizeof_dyn
)
3849 Elf_Internal_Dyn dyn
;
3851 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3855 dyn
.d_un
.d_val
= size
;
3865 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3870 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3873 /* Now update local dynamic symbols. */
3874 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3875 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3876 entry
->isym
.st_name
);
3878 /* And the rest of dynamic symbols. */
3879 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3881 /* Adjust version definitions. */
3882 if (elf_tdata (output_bfd
)->cverdefs
)
3887 Elf_Internal_Verdef def
;
3888 Elf_Internal_Verdaux defaux
;
3890 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3894 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3896 p
+= sizeof (Elf_External_Verdef
);
3897 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3899 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3901 _bfd_elf_swap_verdaux_in (output_bfd
,
3902 (Elf_External_Verdaux
*) p
, &defaux
);
3903 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3905 _bfd_elf_swap_verdaux_out (output_bfd
,
3906 &defaux
, (Elf_External_Verdaux
*) p
);
3907 p
+= sizeof (Elf_External_Verdaux
);
3910 while (def
.vd_next
);
3913 /* Adjust version references. */
3914 if (elf_tdata (output_bfd
)->verref
)
3919 Elf_Internal_Verneed need
;
3920 Elf_Internal_Vernaux needaux
;
3922 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3926 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3928 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3929 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3930 (Elf_External_Verneed
*) p
);
3931 p
+= sizeof (Elf_External_Verneed
);
3932 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3934 _bfd_elf_swap_vernaux_in (output_bfd
,
3935 (Elf_External_Vernaux
*) p
, &needaux
);
3936 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3938 _bfd_elf_swap_vernaux_out (output_bfd
,
3940 (Elf_External_Vernaux
*) p
);
3941 p
+= sizeof (Elf_External_Vernaux
);
3944 while (need
.vn_next
);
3950 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3951 The default is to only match when the INPUT and OUTPUT are exactly
3955 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3956 const bfd_target
*output
)
3958 return input
== output
;
3961 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3962 This version is used when different targets for the same architecture
3963 are virtually identical. */
3966 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3967 const bfd_target
*output
)
3969 const struct elf_backend_data
*obed
, *ibed
;
3971 if (input
== output
)
3974 ibed
= xvec_get_elf_backend_data (input
);
3975 obed
= xvec_get_elf_backend_data (output
);
3977 if (ibed
->arch
!= obed
->arch
)
3980 /* If both backends are using this function, deem them compatible. */
3981 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3984 /* Make a special call to the linker "notice" function to tell it that
3985 we are about to handle an as-needed lib, or have finished
3986 processing the lib. */
3989 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3990 struct bfd_link_info
*info
,
3991 enum notice_asneeded_action act
)
3993 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3996 /* Check relocations an ELF object file. */
3999 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
4001 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4002 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4004 /* If this object is the same format as the output object, and it is
4005 not a shared library, then let the backend look through the
4008 This is required to build global offset table entries and to
4009 arrange for dynamic relocs. It is not required for the
4010 particular common case of linking non PIC code, even when linking
4011 against shared libraries, but unfortunately there is no way of
4012 knowing whether an object file has been compiled PIC or not.
4013 Looking through the relocs is not particularly time consuming.
4014 The problem is that we must either (1) keep the relocs in memory,
4015 which causes the linker to require additional runtime memory or
4016 (2) read the relocs twice from the input file, which wastes time.
4017 This would be a good case for using mmap.
4019 I have no idea how to handle linking PIC code into a file of a
4020 different format. It probably can't be done. */
4021 if ((abfd
->flags
& DYNAMIC
) == 0
4022 && is_elf_hash_table (&htab
->root
)
4023 && bed
->check_relocs
!= NULL
4024 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4025 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4029 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4031 Elf_Internal_Rela
*internal_relocs
;
4034 /* Don't check relocations in excluded sections. Don't do
4035 anything special with non-loaded, non-alloced sections.
4036 In particular, any relocs in such sections should not
4037 affect GOT and PLT reference counting (ie. we don't
4038 allow them to create GOT or PLT entries), there's no
4039 possibility or desire to optimize TLS relocs, and
4040 there's not much point in propagating relocs to shared
4041 libs that the dynamic linker won't relocate. */
4042 if ((o
->flags
& SEC_ALLOC
) == 0
4043 || (o
->flags
& SEC_RELOC
) == 0
4044 || (o
->flags
& SEC_EXCLUDE
) != 0
4045 || o
->reloc_count
== 0
4046 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4047 && (o
->flags
& SEC_DEBUGGING
) != 0)
4048 || bfd_is_abs_section (o
->output_section
))
4051 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
4054 _bfd_link_keep_memory (info
));
4055 if (internal_relocs
== NULL
)
4058 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4060 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4061 free (internal_relocs
);
4071 /* Add symbols from an ELF object file to the linker hash table. */
4074 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4076 Elf_Internal_Ehdr
*ehdr
;
4077 Elf_Internal_Shdr
*hdr
;
4081 struct elf_link_hash_entry
**sym_hash
;
4083 Elf_External_Versym
*extversym
= NULL
;
4084 Elf_External_Versym
*extversym_end
= NULL
;
4085 Elf_External_Versym
*ever
;
4086 struct elf_link_hash_entry
*weaks
;
4087 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4088 size_t nondeflt_vers_cnt
= 0;
4089 Elf_Internal_Sym
*isymbuf
= NULL
;
4090 Elf_Internal_Sym
*isym
;
4091 Elf_Internal_Sym
*isymend
;
4092 const struct elf_backend_data
*bed
;
4094 struct elf_link_hash_table
*htab
;
4095 void *alloc_mark
= NULL
;
4096 struct bfd_hash_entry
**old_table
= NULL
;
4097 unsigned int old_size
= 0;
4098 unsigned int old_count
= 0;
4099 void *old_tab
= NULL
;
4101 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4102 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4103 void *old_strtab
= NULL
;
4108 htab
= elf_hash_table (info
);
4109 bed
= get_elf_backend_data (abfd
);
4111 if ((abfd
->flags
& DYNAMIC
) == 0)
4117 /* You can't use -r against a dynamic object. Also, there's no
4118 hope of using a dynamic object which does not exactly match
4119 the format of the output file. */
4120 if (bfd_link_relocatable (info
)
4121 || !is_elf_hash_table (&htab
->root
)
4122 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4124 if (bfd_link_relocatable (info
))
4125 bfd_set_error (bfd_error_invalid_operation
);
4127 bfd_set_error (bfd_error_wrong_format
);
4132 ehdr
= elf_elfheader (abfd
);
4133 if (info
->warn_alternate_em
4134 && bed
->elf_machine_code
!= ehdr
->e_machine
4135 && ((bed
->elf_machine_alt1
!= 0
4136 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4137 || (bed
->elf_machine_alt2
!= 0
4138 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4140 /* xgettext:c-format */
4141 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4142 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4144 /* As a GNU extension, any input sections which are named
4145 .gnu.warning.SYMBOL are treated as warning symbols for the given
4146 symbol. This differs from .gnu.warning sections, which generate
4147 warnings when they are included in an output file. */
4148 /* PR 12761: Also generate this warning when building shared libraries. */
4149 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4153 name
= bfd_section_name (s
);
4154 if (startswith (name
, ".gnu.warning."))
4159 name
+= sizeof ".gnu.warning." - 1;
4161 /* If this is a shared object, then look up the symbol
4162 in the hash table. If it is there, and it is already
4163 been defined, then we will not be using the entry
4164 from this shared object, so we don't need to warn.
4165 FIXME: If we see the definition in a regular object
4166 later on, we will warn, but we shouldn't. The only
4167 fix is to keep track of what warnings we are supposed
4168 to emit, and then handle them all at the end of the
4172 struct elf_link_hash_entry
*h
;
4174 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
4176 /* FIXME: What about bfd_link_hash_common? */
4178 && (h
->root
.type
== bfd_link_hash_defined
4179 || h
->root
.type
== bfd_link_hash_defweak
))
4184 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4188 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4193 if (! (_bfd_generic_link_add_one_symbol
4194 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4195 false, bed
->collect
, NULL
)))
4198 if (bfd_link_executable (info
))
4200 /* Clobber the section size so that the warning does
4201 not get copied into the output file. */
4204 /* Also set SEC_EXCLUDE, so that symbols defined in
4205 the warning section don't get copied to the output. */
4206 s
->flags
|= SEC_EXCLUDE
;
4211 just_syms
= ((s
= abfd
->sections
) != NULL
4212 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4217 /* If we are creating a shared library, create all the dynamic
4218 sections immediately. We need to attach them to something,
4219 so we attach them to this BFD, provided it is the right
4220 format and is not from ld --just-symbols. Always create the
4221 dynamic sections for -E/--dynamic-list. FIXME: If there
4222 are no input BFD's of the same format as the output, we can't
4223 make a shared library. */
4225 && (bfd_link_pic (info
)
4226 || (!bfd_link_relocatable (info
)
4228 && (info
->export_dynamic
|| info
->dynamic
)))
4229 && is_elf_hash_table (&htab
->root
)
4230 && info
->output_bfd
->xvec
== abfd
->xvec
4231 && !htab
->dynamic_sections_created
)
4233 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4237 else if (!is_elf_hash_table (&htab
->root
))
4241 const char *soname
= NULL
;
4243 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4244 const Elf_Internal_Phdr
*phdr
;
4245 struct elf_link_loaded_list
*loaded_lib
;
4247 /* ld --just-symbols and dynamic objects don't mix very well.
4248 ld shouldn't allow it. */
4252 /* If this dynamic lib was specified on the command line with
4253 --as-needed in effect, then we don't want to add a DT_NEEDED
4254 tag unless the lib is actually used. Similary for libs brought
4255 in by another lib's DT_NEEDED. When --no-add-needed is used
4256 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4257 any dynamic library in DT_NEEDED tags in the dynamic lib at
4259 add_needed
= (elf_dyn_lib_class (abfd
)
4260 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4261 | DYN_NO_NEEDED
)) == 0;
4263 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4268 unsigned int elfsec
;
4269 unsigned long shlink
;
4271 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4278 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4279 if (elfsec
== SHN_BAD
)
4280 goto error_free_dyn
;
4281 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4283 for (extdyn
= dynbuf
;
4284 extdyn
<= dynbuf
+ s
->size
- bed
->s
->sizeof_dyn
;
4285 extdyn
+= bed
->s
->sizeof_dyn
)
4287 Elf_Internal_Dyn dyn
;
4289 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4290 if (dyn
.d_tag
== DT_SONAME
)
4292 unsigned int tagv
= dyn
.d_un
.d_val
;
4293 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4295 goto error_free_dyn
;
4297 if (dyn
.d_tag
== DT_NEEDED
)
4299 struct bfd_link_needed_list
*n
, **pn
;
4301 unsigned int tagv
= dyn
.d_un
.d_val
;
4302 size_t amt
= sizeof (struct bfd_link_needed_list
);
4304 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4305 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4306 if (n
== NULL
|| fnm
== NULL
)
4307 goto error_free_dyn
;
4308 amt
= strlen (fnm
) + 1;
4309 anm
= (char *) bfd_alloc (abfd
, amt
);
4311 goto error_free_dyn
;
4312 memcpy (anm
, fnm
, amt
);
4316 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4320 if (dyn
.d_tag
== DT_RUNPATH
)
4322 struct bfd_link_needed_list
*n
, **pn
;
4324 unsigned int tagv
= dyn
.d_un
.d_val
;
4325 size_t amt
= sizeof (struct bfd_link_needed_list
);
4327 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4328 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4329 if (n
== NULL
|| fnm
== NULL
)
4330 goto error_free_dyn
;
4331 amt
= strlen (fnm
) + 1;
4332 anm
= (char *) bfd_alloc (abfd
, amt
);
4334 goto error_free_dyn
;
4335 memcpy (anm
, fnm
, amt
);
4339 for (pn
= & runpath
;
4345 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4346 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4348 struct bfd_link_needed_list
*n
, **pn
;
4350 unsigned int tagv
= dyn
.d_un
.d_val
;
4351 size_t amt
= sizeof (struct bfd_link_needed_list
);
4353 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4354 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4355 if (n
== NULL
|| fnm
== NULL
)
4356 goto error_free_dyn
;
4357 amt
= strlen (fnm
) + 1;
4358 anm
= (char *) bfd_alloc (abfd
, amt
);
4360 goto error_free_dyn
;
4361 memcpy (anm
, fnm
, amt
);
4371 if (dyn
.d_tag
== DT_AUDIT
)
4373 unsigned int tagv
= dyn
.d_un
.d_val
;
4374 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4376 if (dyn
.d_tag
== DT_FLAGS_1
)
4377 elf_tdata (abfd
)->is_pie
= (dyn
.d_un
.d_val
& DF_1_PIE
) != 0;
4383 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4384 frees all more recently bfd_alloc'd blocks as well. */
4390 struct bfd_link_needed_list
**pn
;
4391 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4396 /* If we have a PT_GNU_RELRO program header, mark as read-only
4397 all sections contained fully therein. This makes relro
4398 shared library sections appear as they will at run-time. */
4399 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4400 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4401 if (phdr
->p_type
== PT_GNU_RELRO
)
4403 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4405 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4407 if ((s
->flags
& SEC_ALLOC
) != 0
4408 && s
->vma
* opb
>= phdr
->p_vaddr
4409 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4410 s
->flags
|= SEC_READONLY
;
4415 /* We do not want to include any of the sections in a dynamic
4416 object in the output file. We hack by simply clobbering the
4417 list of sections in the BFD. This could be handled more
4418 cleanly by, say, a new section flag; the existing
4419 SEC_NEVER_LOAD flag is not the one we want, because that one
4420 still implies that the section takes up space in the output
4422 bfd_section_list_clear (abfd
);
4424 /* Find the name to use in a DT_NEEDED entry that refers to this
4425 object. If the object has a DT_SONAME entry, we use it.
4426 Otherwise, if the generic linker stuck something in
4427 elf_dt_name, we use that. Otherwise, we just use the file
4429 if (soname
== NULL
|| *soname
== '\0')
4431 soname
= elf_dt_name (abfd
);
4432 if (soname
== NULL
|| *soname
== '\0')
4433 soname
= bfd_get_filename (abfd
);
4436 /* Save the SONAME because sometimes the linker emulation code
4437 will need to know it. */
4438 elf_dt_name (abfd
) = soname
;
4440 /* If we have already included this dynamic object in the
4441 link, just ignore it. There is no reason to include a
4442 particular dynamic object more than once. */
4443 for (loaded_lib
= htab
->dyn_loaded
;
4445 loaded_lib
= loaded_lib
->next
)
4447 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4451 /* Create dynamic sections for backends that require that be done
4452 before setup_gnu_properties. */
4454 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4457 /* Save the DT_AUDIT entry for the linker emulation code. */
4458 elf_dt_audit (abfd
) = audit
;
4461 /* If this is a dynamic object, we always link against the .dynsym
4462 symbol table, not the .symtab symbol table. The dynamic linker
4463 will only see the .dynsym symbol table, so there is no reason to
4464 look at .symtab for a dynamic object. */
4466 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4467 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4469 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4471 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4473 /* The sh_info field of the symtab header tells us where the
4474 external symbols start. We don't care about the local symbols at
4476 if (elf_bad_symtab (abfd
))
4478 extsymcount
= symcount
;
4483 extsymcount
= symcount
- hdr
->sh_info
;
4484 extsymoff
= hdr
->sh_info
;
4487 sym_hash
= elf_sym_hashes (abfd
);
4488 if (extsymcount
!= 0)
4490 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4492 if (isymbuf
== NULL
)
4495 if (sym_hash
== NULL
)
4497 /* We store a pointer to the hash table entry for each
4499 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4500 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4501 if (sym_hash
== NULL
)
4502 goto error_free_sym
;
4503 elf_sym_hashes (abfd
) = sym_hash
;
4509 /* Read in any version definitions. */
4510 if (!_bfd_elf_slurp_version_tables (abfd
,
4511 info
->default_imported_symver
))
4512 goto error_free_sym
;
4514 /* Read in the symbol versions, but don't bother to convert them
4515 to internal format. */
4516 if (elf_dynversym (abfd
) != 0)
4518 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4519 bfd_size_type amt
= versymhdr
->sh_size
;
4521 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4522 goto error_free_sym
;
4523 extversym
= (Elf_External_Versym
*)
4524 _bfd_malloc_and_read (abfd
, amt
, amt
);
4525 if (extversym
== NULL
)
4526 goto error_free_sym
;
4527 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4531 /* If we are loading an as-needed shared lib, save the symbol table
4532 state before we start adding symbols. If the lib turns out
4533 to be unneeded, restore the state. */
4534 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4539 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4541 struct bfd_hash_entry
*p
;
4542 struct elf_link_hash_entry
*h
;
4544 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4546 h
= (struct elf_link_hash_entry
*) p
;
4547 entsize
+= htab
->root
.table
.entsize
;
4548 if (h
->root
.type
== bfd_link_hash_warning
)
4550 entsize
+= htab
->root
.table
.entsize
;
4551 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4553 if (h
->root
.type
== bfd_link_hash_common
)
4554 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4558 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4559 old_tab
= bfd_malloc (tabsize
+ entsize
);
4560 if (old_tab
== NULL
)
4561 goto error_free_vers
;
4563 /* Remember the current objalloc pointer, so that all mem for
4564 symbols added can later be reclaimed. */
4565 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4566 if (alloc_mark
== NULL
)
4567 goto error_free_vers
;
4569 /* Make a special call to the linker "notice" function to
4570 tell it that we are about to handle an as-needed lib. */
4571 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4572 goto error_free_vers
;
4574 /* Clone the symbol table. Remember some pointers into the
4575 symbol table, and dynamic symbol count. */
4576 old_ent
= (char *) old_tab
+ tabsize
;
4577 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4578 old_undefs
= htab
->root
.undefs
;
4579 old_undefs_tail
= htab
->root
.undefs_tail
;
4580 old_table
= htab
->root
.table
.table
;
4581 old_size
= htab
->root
.table
.size
;
4582 old_count
= htab
->root
.table
.count
;
4584 if (htab
->dynstr
!= NULL
)
4586 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4587 if (old_strtab
== NULL
)
4588 goto error_free_vers
;
4591 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4593 struct bfd_hash_entry
*p
;
4594 struct elf_link_hash_entry
*h
;
4596 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4598 h
= (struct elf_link_hash_entry
*) p
;
4599 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4600 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4601 if (h
->root
.type
== bfd_link_hash_warning
)
4603 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4604 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4605 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4607 if (h
->root
.type
== bfd_link_hash_common
)
4609 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4610 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4617 if (extversym
== NULL
)
4619 else if (extversym
+ extsymoff
< extversym_end
)
4620 ever
= extversym
+ extsymoff
;
4623 /* xgettext:c-format */
4624 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4625 abfd
, (long) extsymoff
,
4626 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4627 bfd_set_error (bfd_error_bad_value
);
4628 goto error_free_vers
;
4631 if (!bfd_link_relocatable (info
)
4632 && abfd
->lto_slim_object
)
4635 (_("%pB: plugin needed to handle lto object"), abfd
);
4638 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4640 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4644 asection
*sec
, *new_sec
;
4647 struct elf_link_hash_entry
*h
;
4648 struct elf_link_hash_entry
*hi
;
4650 bool size_change_ok
;
4651 bool type_change_ok
;
4657 unsigned int old_alignment
;
4658 unsigned int shindex
;
4664 flags
= BSF_NO_FLAGS
;
4666 value
= isym
->st_value
;
4667 common
= bed
->common_definition (isym
);
4668 if (common
&& info
->inhibit_common_definition
)
4670 /* Treat common symbol as undefined for --no-define-common. */
4671 isym
->st_shndx
= SHN_UNDEF
;
4676 bind
= ELF_ST_BIND (isym
->st_info
);
4680 /* This should be impossible, since ELF requires that all
4681 global symbols follow all local symbols, and that sh_info
4682 point to the first global symbol. Unfortunately, Irix 5
4684 if (elf_bad_symtab (abfd
))
4687 /* If we aren't prepared to handle locals within the globals
4688 then we'll likely segfault on a NULL symbol hash if the
4689 symbol is ever referenced in relocations. */
4690 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4691 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4692 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4693 " (>= sh_info of %lu)"),
4694 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4697 /* Dynamic object relocations are not processed by ld, so
4698 ld won't run into the problem mentioned above. */
4701 bfd_set_error (bfd_error_bad_value
);
4702 goto error_free_vers
;
4705 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4713 case STB_GNU_UNIQUE
:
4714 flags
= BSF_GNU_UNIQUE
;
4718 /* Leave it up to the processor backend. */
4722 if (isym
->st_shndx
== SHN_UNDEF
)
4723 sec
= bfd_und_section_ptr
;
4724 else if (isym
->st_shndx
== SHN_ABS
)
4725 sec
= bfd_abs_section_ptr
;
4726 else if (isym
->st_shndx
== SHN_COMMON
)
4728 sec
= bfd_com_section_ptr
;
4729 /* What ELF calls the size we call the value. What ELF
4730 calls the value we call the alignment. */
4731 value
= isym
->st_size
;
4735 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4737 sec
= bfd_abs_section_ptr
;
4738 else if (discarded_section (sec
))
4740 /* Symbols from discarded section are undefined. We keep
4742 sec
= bfd_und_section_ptr
;
4744 isym
->st_shndx
= SHN_UNDEF
;
4746 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4750 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4753 goto error_free_vers
;
4755 if (isym
->st_shndx
== SHN_COMMON
4756 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4758 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4762 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4764 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4766 goto error_free_vers
;
4770 else if (isym
->st_shndx
== SHN_COMMON
4771 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4772 && !bfd_link_relocatable (info
))
4774 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4778 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4779 | SEC_LINKER_CREATED
);
4780 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4782 goto error_free_vers
;
4786 else if (bed
->elf_add_symbol_hook
)
4788 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4790 goto error_free_vers
;
4792 /* The hook function sets the name to NULL if this symbol
4793 should be skipped for some reason. */
4798 /* Sanity check that all possibilities were handled. */
4802 /* Silently discard TLS symbols from --just-syms. There's
4803 no way to combine a static TLS block with a new TLS block
4804 for this executable. */
4805 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4806 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4809 if (bfd_is_und_section (sec
)
4810 || bfd_is_com_section (sec
))
4815 size_change_ok
= false;
4816 type_change_ok
= bed
->type_change_ok
;
4823 if (is_elf_hash_table (&htab
->root
))
4825 Elf_Internal_Versym iver
;
4826 unsigned int vernum
= 0;
4831 if (info
->default_imported_symver
)
4832 /* Use the default symbol version created earlier. */
4833 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4837 else if (ever
>= extversym_end
)
4839 /* xgettext:c-format */
4840 _bfd_error_handler (_("%pB: not enough version information"),
4842 bfd_set_error (bfd_error_bad_value
);
4843 goto error_free_vers
;
4846 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4848 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4850 /* If this is a hidden symbol, or if it is not version
4851 1, we append the version name to the symbol name.
4852 However, we do not modify a non-hidden absolute symbol
4853 if it is not a function, because it might be the version
4854 symbol itself. FIXME: What if it isn't? */
4855 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4857 && (!bfd_is_abs_section (sec
)
4858 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4861 size_t namelen
, verlen
, newlen
;
4864 if (isym
->st_shndx
!= SHN_UNDEF
)
4866 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4868 else if (vernum
> 1)
4870 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4877 /* xgettext:c-format */
4878 (_("%pB: %s: invalid version %u (max %d)"),
4880 elf_tdata (abfd
)->cverdefs
);
4881 bfd_set_error (bfd_error_bad_value
);
4882 goto error_free_vers
;
4887 /* We cannot simply test for the number of
4888 entries in the VERNEED section since the
4889 numbers for the needed versions do not start
4891 Elf_Internal_Verneed
*t
;
4894 for (t
= elf_tdata (abfd
)->verref
;
4898 Elf_Internal_Vernaux
*a
;
4900 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4902 if (a
->vna_other
== vernum
)
4904 verstr
= a
->vna_nodename
;
4914 /* xgettext:c-format */
4915 (_("%pB: %s: invalid needed version %d"),
4916 abfd
, name
, vernum
);
4917 bfd_set_error (bfd_error_bad_value
);
4918 goto error_free_vers
;
4922 namelen
= strlen (name
);
4923 verlen
= strlen (verstr
);
4924 newlen
= namelen
+ verlen
+ 2;
4925 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4926 && isym
->st_shndx
!= SHN_UNDEF
)
4929 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4930 if (newname
== NULL
)
4931 goto error_free_vers
;
4932 memcpy (newname
, name
, namelen
);
4933 p
= newname
+ namelen
;
4935 /* If this is a defined non-hidden version symbol,
4936 we add another @ to the name. This indicates the
4937 default version of the symbol. */
4938 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4939 && isym
->st_shndx
!= SHN_UNDEF
)
4941 memcpy (p
, verstr
, verlen
+ 1);
4946 /* If this symbol has default visibility and the user has
4947 requested we not re-export it, then mark it as hidden. */
4948 if (!bfd_is_und_section (sec
)
4951 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4952 isym
->st_other
= (STV_HIDDEN
4953 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4955 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4956 sym_hash
, &old_bfd
, &old_weak
,
4957 &old_alignment
, &skip
, &override
,
4958 &type_change_ok
, &size_change_ok
,
4960 goto error_free_vers
;
4965 /* Override a definition only if the new symbol matches the
4967 if (override
&& matched
)
4971 while (h
->root
.type
== bfd_link_hash_indirect
4972 || h
->root
.type
== bfd_link_hash_warning
)
4973 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4975 if (elf_tdata (abfd
)->verdef
!= NULL
4978 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4981 if (! (_bfd_generic_link_add_one_symbol
4982 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
4983 NULL
, false, bed
->collect
,
4984 (struct bfd_link_hash_entry
**) sym_hash
)))
4985 goto error_free_vers
;
4988 /* We need to make sure that indirect symbol dynamic flags are
4991 while (h
->root
.type
== bfd_link_hash_indirect
4992 || h
->root
.type
== bfd_link_hash_warning
)
4993 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4997 /* Setting the index to -3 tells elf_link_output_extsym that
4998 this symbol is defined in a discarded section. */
4999 if (discarded
&& is_elf_hash_table (&htab
->root
))
5002 new_weak
= (flags
& BSF_WEAK
) != 0;
5006 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
5007 && is_elf_hash_table (&htab
->root
)
5008 && h
->u
.alias
== NULL
)
5010 /* Keep a list of all weak defined non function symbols from
5011 a dynamic object, using the alias field. Later in this
5012 function we will set the alias field to the correct
5013 value. We only put non-function symbols from dynamic
5014 objects on this list, because that happens to be the only
5015 time we need to know the normal symbol corresponding to a
5016 weak symbol, and the information is time consuming to
5017 figure out. If the alias field is not already NULL,
5018 then this symbol was already defined by some previous
5019 dynamic object, and we will be using that previous
5020 definition anyhow. */
5026 /* Set the alignment of a common symbol. */
5027 if ((common
|| bfd_is_com_section (sec
))
5028 && h
->root
.type
== bfd_link_hash_common
)
5033 align
= bfd_log2 (isym
->st_value
);
5036 /* The new symbol is a common symbol in a shared object.
5037 We need to get the alignment from the section. */
5038 align
= new_sec
->alignment_power
;
5040 if (align
> old_alignment
)
5041 h
->root
.u
.c
.p
->alignment_power
= align
;
5043 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5046 if (is_elf_hash_table (&htab
->root
))
5048 /* Set a flag in the hash table entry indicating the type of
5049 reference or definition we just found. A dynamic symbol
5050 is one which is referenced or defined by both a regular
5051 object and a shared object. */
5052 bool dynsym
= false;
5054 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5055 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5057 /* Except for this flag to track nonweak references. */
5059 && bind
!= STB_WEAK
)
5060 h
->ref_ir_nonweak
= 1;
5067 if (bind
!= STB_WEAK
)
5068 h
->ref_regular_nonweak
= 1;
5085 hi
->ref_dynamic
= 1;
5090 hi
->def_dynamic
= 1;
5094 /* If an indirect symbol has been forced local, don't
5095 make the real symbol dynamic. */
5096 if (h
!= hi
&& hi
->forced_local
)
5100 if (bfd_link_dll (info
)
5110 && weakdef (h
)->dynindx
!= -1))
5114 /* Check to see if we need to add an indirect symbol for
5115 the default name. */
5117 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5119 && hi
->versioned
== versioned_hidden
))
5120 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5121 sec
, value
, &old_bfd
, &dynsym
))
5122 goto error_free_vers
;
5124 /* Check the alignment when a common symbol is involved. This
5125 can change when a common symbol is overridden by a normal
5126 definition or a common symbol is ignored due to the old
5127 normal definition. We need to make sure the maximum
5128 alignment is maintained. */
5129 if ((old_alignment
|| common
)
5130 && h
->root
.type
!= bfd_link_hash_common
)
5132 unsigned int common_align
;
5133 unsigned int normal_align
;
5134 unsigned int symbol_align
;
5138 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5139 || h
->root
.type
== bfd_link_hash_defweak
);
5141 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5142 if (h
->root
.u
.def
.section
->owner
!= NULL
5143 && (h
->root
.u
.def
.section
->owner
->flags
5144 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5146 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5147 if (normal_align
> symbol_align
)
5148 normal_align
= symbol_align
;
5151 normal_align
= symbol_align
;
5155 common_align
= old_alignment
;
5156 common_bfd
= old_bfd
;
5161 common_align
= bfd_log2 (isym
->st_value
);
5163 normal_bfd
= old_bfd
;
5166 if (normal_align
< common_align
)
5168 /* PR binutils/2735 */
5169 if (normal_bfd
== NULL
)
5171 /* xgettext:c-format */
5172 (_("warning: alignment %u of common symbol `%s' in %pB is"
5173 " greater than the alignment (%u) of its section %pA"),
5174 1 << common_align
, name
, common_bfd
,
5175 1 << normal_align
, h
->root
.u
.def
.section
);
5178 /* xgettext:c-format */
5179 (_("warning: alignment %u of symbol `%s' in %pB"
5180 " is smaller than %u in %pB"),
5181 1 << normal_align
, name
, normal_bfd
,
5182 1 << common_align
, common_bfd
);
5186 /* Remember the symbol size if it isn't undefined. */
5187 if (isym
->st_size
!= 0
5188 && isym
->st_shndx
!= SHN_UNDEF
5189 && (definition
|| h
->size
== 0))
5192 && h
->size
!= isym
->st_size
5193 && ! size_change_ok
)
5195 /* xgettext:c-format */
5196 (_("warning: size of symbol `%s' changed"
5197 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5198 name
, (uint64_t) h
->size
, old_bfd
,
5199 (uint64_t) isym
->st_size
, abfd
);
5201 h
->size
= isym
->st_size
;
5204 /* If this is a common symbol, then we always want H->SIZE
5205 to be the size of the common symbol. The code just above
5206 won't fix the size if a common symbol becomes larger. We
5207 don't warn about a size change here, because that is
5208 covered by --warn-common. Allow changes between different
5210 if (h
->root
.type
== bfd_link_hash_common
)
5211 h
->size
= h
->root
.u
.c
.size
;
5213 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5214 && ((definition
&& !new_weak
)
5215 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5216 || h
->type
== STT_NOTYPE
))
5218 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5220 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5222 if (type
== STT_GNU_IFUNC
5223 && (abfd
->flags
& DYNAMIC
) != 0)
5226 if (h
->type
!= type
)
5228 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5229 /* xgettext:c-format */
5231 (_("warning: type of symbol `%s' changed"
5232 " from %d to %d in %pB"),
5233 name
, h
->type
, type
, abfd
);
5239 /* Merge st_other field. */
5240 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5241 definition
, dynamic
);
5243 /* We don't want to make debug symbol dynamic. */
5245 && (sec
->flags
& SEC_DEBUGGING
)
5246 && !bfd_link_relocatable (info
))
5249 /* Nor should we make plugin symbols dynamic. */
5250 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5255 h
->target_internal
= isym
->st_target_internal
;
5256 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5259 if (definition
&& !dynamic
)
5261 char *p
= strchr (name
, ELF_VER_CHR
);
5262 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5264 /* Queue non-default versions so that .symver x, x@FOO
5265 aliases can be checked. */
5268 size_t amt
= ((isymend
- isym
+ 1)
5269 * sizeof (struct elf_link_hash_entry
*));
5271 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5273 goto error_free_vers
;
5275 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5279 if (dynsym
&& h
->dynindx
== -1)
5281 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5282 goto error_free_vers
;
5284 && weakdef (h
)->dynindx
== -1)
5286 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5287 goto error_free_vers
;
5290 else if (h
->dynindx
!= -1)
5291 /* If the symbol already has a dynamic index, but
5292 visibility says it should not be visible, turn it into
5294 switch (ELF_ST_VISIBILITY (h
->other
))
5298 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
5306 && h
->root
.type
!= bfd_link_hash_indirect
5308 && h
->ref_regular_nonweak
)
5310 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5311 && h
->ref_ir_nonweak
5312 && !info
->lto_all_symbols_read
)
5313 || (h
->ref_dynamic_nonweak
5314 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5315 && !on_needed_list (elf_dt_name (abfd
),
5316 htab
->needed
, NULL
))))
5318 const char *soname
= elf_dt_name (abfd
);
5320 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5321 h
->root
.root
.string
);
5323 /* A symbol from a library loaded via DT_NEEDED of some
5324 other library is referenced by a regular object.
5325 Add a DT_NEEDED entry for it. Issue an error if
5326 --no-add-needed is used and the reference was not
5329 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5332 /* xgettext:c-format */
5333 (_("%pB: undefined reference to symbol '%s'"),
5335 bfd_set_error (bfd_error_missing_dso
);
5336 goto error_free_vers
;
5339 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5340 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5342 /* Create dynamic sections for backends that require
5343 that be done before setup_gnu_properties. */
5344 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5351 if (info
->lto_plugin_active
5352 && !bfd_link_relocatable (info
)
5353 && (abfd
->flags
& BFD_PLUGIN
) == 0
5359 if (bed
->s
->arch_size
== 32)
5364 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5365 referenced in regular objects so that linker plugin will get
5366 the correct symbol resolution. */
5368 sym_hash
= elf_sym_hashes (abfd
);
5369 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5371 Elf_Internal_Rela
*internal_relocs
;
5372 Elf_Internal_Rela
*rel
, *relend
;
5374 /* Don't check relocations in excluded sections. */
5375 if ((s
->flags
& SEC_RELOC
) == 0
5376 || s
->reloc_count
== 0
5377 || (s
->flags
& SEC_EXCLUDE
) != 0
5378 || ((info
->strip
== strip_all
5379 || info
->strip
== strip_debugger
)
5380 && (s
->flags
& SEC_DEBUGGING
) != 0))
5383 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
5386 _bfd_link_keep_memory (info
));
5387 if (internal_relocs
== NULL
)
5388 goto error_free_vers
;
5390 rel
= internal_relocs
;
5391 relend
= rel
+ s
->reloc_count
;
5392 for ( ; rel
< relend
; rel
++)
5394 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5395 struct elf_link_hash_entry
*h
;
5397 /* Skip local symbols. */
5398 if (r_symndx
< extsymoff
)
5401 h
= sym_hash
[r_symndx
- extsymoff
];
5403 h
->root
.non_ir_ref_regular
= 1;
5406 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5407 free (internal_relocs
);
5416 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5420 /* Restore the symbol table. */
5421 old_ent
= (char *) old_tab
+ tabsize
;
5422 memset (elf_sym_hashes (abfd
), 0,
5423 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5424 htab
->root
.table
.table
= old_table
;
5425 htab
->root
.table
.size
= old_size
;
5426 htab
->root
.table
.count
= old_count
;
5427 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5428 htab
->root
.undefs
= old_undefs
;
5429 htab
->root
.undefs_tail
= old_undefs_tail
;
5430 if (htab
->dynstr
!= NULL
)
5431 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5434 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5436 struct bfd_hash_entry
*p
;
5437 struct elf_link_hash_entry
*h
;
5438 unsigned int non_ir_ref_dynamic
;
5440 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5442 /* Preserve non_ir_ref_dynamic so that this symbol
5443 will be exported when the dynamic lib becomes needed
5444 in the second pass. */
5445 h
= (struct elf_link_hash_entry
*) p
;
5446 if (h
->root
.type
== bfd_link_hash_warning
)
5447 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5448 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5450 h
= (struct elf_link_hash_entry
*) p
;
5451 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5452 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5453 if (h
->root
.type
== bfd_link_hash_warning
)
5455 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5456 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5457 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5459 if (h
->root
.type
== bfd_link_hash_common
)
5461 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5462 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5464 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5468 /* Make a special call to the linker "notice" function to
5469 tell it that symbols added for crefs may need to be removed. */
5470 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5471 goto error_free_vers
;
5474 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5476 free (nondeflt_vers
);
5480 if (old_tab
!= NULL
)
5482 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5483 goto error_free_vers
;
5488 /* Now that all the symbols from this input file are created, if
5489 not performing a relocatable link, handle .symver foo, foo@BAR
5490 such that any relocs against foo become foo@BAR. */
5491 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5495 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5497 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5498 char *shortname
, *p
;
5501 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5503 || (h
->root
.type
!= bfd_link_hash_defined
5504 && h
->root
.type
!= bfd_link_hash_defweak
))
5507 amt
= p
- h
->root
.root
.string
;
5508 shortname
= (char *) bfd_malloc (amt
+ 1);
5510 goto error_free_vers
;
5511 memcpy (shortname
, h
->root
.root
.string
, amt
);
5512 shortname
[amt
] = '\0';
5514 hi
= (struct elf_link_hash_entry
*)
5515 bfd_link_hash_lookup (&htab
->root
, shortname
,
5516 false, false, false);
5518 && hi
->root
.type
== h
->root
.type
5519 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5520 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5522 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
5523 hi
->root
.type
= bfd_link_hash_indirect
;
5524 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5525 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5526 sym_hash
= elf_sym_hashes (abfd
);
5528 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5529 if (sym_hash
[symidx
] == hi
)
5531 sym_hash
[symidx
] = h
;
5537 free (nondeflt_vers
);
5538 nondeflt_vers
= NULL
;
5541 /* Now set the alias field correctly for all the weak defined
5542 symbols we found. The only way to do this is to search all the
5543 symbols. Since we only need the information for non functions in
5544 dynamic objects, that's the only time we actually put anything on
5545 the list WEAKS. We need this information so that if a regular
5546 object refers to a symbol defined weakly in a dynamic object, the
5547 real symbol in the dynamic object is also put in the dynamic
5548 symbols; we also must arrange for both symbols to point to the
5549 same memory location. We could handle the general case of symbol
5550 aliasing, but a general symbol alias can only be generated in
5551 assembler code, handling it correctly would be very time
5552 consuming, and other ELF linkers don't handle general aliasing
5556 struct elf_link_hash_entry
**hpp
;
5557 struct elf_link_hash_entry
**hppend
;
5558 struct elf_link_hash_entry
**sorted_sym_hash
;
5559 struct elf_link_hash_entry
*h
;
5560 size_t sym_count
, amt
;
5562 /* Since we have to search the whole symbol list for each weak
5563 defined symbol, search time for N weak defined symbols will be
5564 O(N^2). Binary search will cut it down to O(NlogN). */
5565 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5566 sorted_sym_hash
= bfd_malloc (amt
);
5567 if (sorted_sym_hash
== NULL
)
5569 sym_hash
= sorted_sym_hash
;
5570 hpp
= elf_sym_hashes (abfd
);
5571 hppend
= hpp
+ extsymcount
;
5573 for (; hpp
< hppend
; hpp
++)
5577 && h
->root
.type
== bfd_link_hash_defined
5578 && !bed
->is_function_type (h
->type
))
5586 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5589 while (weaks
!= NULL
)
5591 struct elf_link_hash_entry
*hlook
;
5594 size_t i
, j
, idx
= 0;
5597 weaks
= hlook
->u
.alias
;
5598 hlook
->u
.alias
= NULL
;
5600 if (hlook
->root
.type
!= bfd_link_hash_defined
5601 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5604 slook
= hlook
->root
.u
.def
.section
;
5605 vlook
= hlook
->root
.u
.def
.value
;
5611 bfd_signed_vma vdiff
;
5613 h
= sorted_sym_hash
[idx
];
5614 vdiff
= vlook
- h
->root
.u
.def
.value
;
5621 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5631 /* We didn't find a value/section match. */
5635 /* With multiple aliases, or when the weak symbol is already
5636 strongly defined, we have multiple matching symbols and
5637 the binary search above may land on any of them. Step
5638 one past the matching symbol(s). */
5641 h
= sorted_sym_hash
[idx
];
5642 if (h
->root
.u
.def
.section
!= slook
5643 || h
->root
.u
.def
.value
!= vlook
)
5647 /* Now look back over the aliases. Since we sorted by size
5648 as well as value and section, we'll choose the one with
5649 the largest size. */
5652 h
= sorted_sym_hash
[idx
];
5654 /* Stop if value or section doesn't match. */
5655 if (h
->root
.u
.def
.section
!= slook
5656 || h
->root
.u
.def
.value
!= vlook
)
5658 else if (h
!= hlook
)
5660 struct elf_link_hash_entry
*t
;
5663 hlook
->is_weakalias
= 1;
5665 if (t
->u
.alias
!= NULL
)
5666 while (t
->u
.alias
!= h
)
5670 /* If the weak definition is in the list of dynamic
5671 symbols, make sure the real definition is put
5673 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5675 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5678 free (sorted_sym_hash
);
5683 /* If the real definition is in the list of dynamic
5684 symbols, make sure the weak definition is put
5685 there as well. If we don't do this, then the
5686 dynamic loader might not merge the entries for the
5687 real definition and the weak definition. */
5688 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5690 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5691 goto err_free_sym_hash
;
5698 free (sorted_sym_hash
);
5701 if (bed
->check_directives
5702 && !(*bed
->check_directives
) (abfd
, info
))
5705 /* If this is a non-traditional link, try to optimize the handling
5706 of the .stab/.stabstr sections. */
5708 && ! info
->traditional_format
5709 && is_elf_hash_table (&htab
->root
)
5710 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5714 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5715 if (stabstr
!= NULL
)
5717 bfd_size_type string_offset
= 0;
5720 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5721 if (startswith (stab
->name
, ".stab")
5722 && (!stab
->name
[5] ||
5723 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5724 && (stab
->flags
& SEC_MERGE
) == 0
5725 && !bfd_is_abs_section (stab
->output_section
))
5727 struct bfd_elf_section_data
*secdata
;
5729 secdata
= elf_section_data (stab
);
5730 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5731 stabstr
, &secdata
->sec_info
,
5734 if (secdata
->sec_info
)
5735 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5740 if (dynamic
&& add_needed
)
5742 /* Add this bfd to the loaded list. */
5743 struct elf_link_loaded_list
*n
;
5745 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5749 n
->next
= htab
->dyn_loaded
;
5750 htab
->dyn_loaded
= n
;
5752 if (dynamic
&& !add_needed
5753 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5754 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5761 free (nondeflt_vers
);
5769 /* Return the linker hash table entry of a symbol that might be
5770 satisfied by an archive symbol. Return -1 on error. */
5772 struct bfd_link_hash_entry
*
5773 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5774 struct bfd_link_info
*info
,
5777 struct bfd_link_hash_entry
*h
;
5781 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
5785 /* If this is a default version (the name contains @@), look up the
5786 symbol again with only one `@' as well as without the version.
5787 The effect is that references to the symbol with and without the
5788 version will be matched by the default symbol in the archive. */
5790 p
= strchr (name
, ELF_VER_CHR
);
5791 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5794 /* First check with only one `@'. */
5795 len
= strlen (name
);
5796 copy
= (char *) bfd_alloc (abfd
, len
);
5798 return (struct bfd_link_hash_entry
*) -1;
5800 first
= p
- name
+ 1;
5801 memcpy (copy
, name
, first
);
5802 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5804 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5807 /* We also need to check references to the symbol without the
5809 copy
[first
- 1] = '\0';
5810 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5813 bfd_release (abfd
, copy
);
5817 /* Add symbols from an ELF archive file to the linker hash table. We
5818 don't use _bfd_generic_link_add_archive_symbols because we need to
5819 handle versioned symbols.
5821 Fortunately, ELF archive handling is simpler than that done by
5822 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5823 oddities. In ELF, if we find a symbol in the archive map, and the
5824 symbol is currently undefined, we know that we must pull in that
5827 Unfortunately, we do have to make multiple passes over the symbol
5828 table until nothing further is resolved. */
5831 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5834 unsigned char *included
= NULL
;
5838 const struct elf_backend_data
*bed
;
5839 struct bfd_link_hash_entry
* (*archive_symbol_lookup
)
5840 (bfd
*, struct bfd_link_info
*, const char *);
5842 if (! bfd_has_map (abfd
))
5844 /* An empty archive is a special case. */
5845 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5847 bfd_set_error (bfd_error_no_armap
);
5851 /* Keep track of all symbols we know to be already defined, and all
5852 files we know to be already included. This is to speed up the
5853 second and subsequent passes. */
5854 c
= bfd_ardata (abfd
)->symdef_count
;
5857 amt
= c
* sizeof (*included
);
5858 included
= (unsigned char *) bfd_zmalloc (amt
);
5859 if (included
== NULL
)
5862 symdefs
= bfd_ardata (abfd
)->symdefs
;
5863 bed
= get_elf_backend_data (abfd
);
5864 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5877 symdefend
= symdef
+ c
;
5878 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5880 struct bfd_link_hash_entry
*h
;
5882 struct bfd_link_hash_entry
*undefs_tail
;
5887 if (symdef
->file_offset
== last
)
5893 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5894 if (h
== (struct bfd_link_hash_entry
*) -1)
5900 if (h
->type
== bfd_link_hash_undefined
)
5902 /* If the archive element has already been loaded then one
5903 of the symbols defined by that element might have been
5904 made undefined due to being in a discarded section. */
5905 if (is_elf_hash_table (info
->hash
)
5906 && ((struct elf_link_hash_entry
*) h
)->indx
== -3)
5909 else if (h
->type
== bfd_link_hash_common
)
5911 /* We currently have a common symbol. The archive map contains
5912 a reference to this symbol, so we may want to include it. We
5913 only want to include it however, if this archive element
5914 contains a definition of the symbol, not just another common
5917 Unfortunately some archivers (including GNU ar) will put
5918 declarations of common symbols into their archive maps, as
5919 well as real definitions, so we cannot just go by the archive
5920 map alone. Instead we must read in the element's symbol
5921 table and check that to see what kind of symbol definition
5923 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5928 if (h
->type
!= bfd_link_hash_undefweak
)
5929 /* Symbol must be defined. Don't check it again. */
5934 /* We need to include this archive member. */
5935 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5936 if (element
== NULL
)
5939 if (! bfd_check_format (element
, bfd_object
))
5942 undefs_tail
= info
->hash
->undefs_tail
;
5944 if (!(*info
->callbacks
5945 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5947 if (!bfd_link_add_symbols (element
, info
))
5950 /* If there are any new undefined symbols, we need to make
5951 another pass through the archive in order to see whether
5952 they can be defined. FIXME: This isn't perfect, because
5953 common symbols wind up on undefs_tail and because an
5954 undefined symbol which is defined later on in this pass
5955 does not require another pass. This isn't a bug, but it
5956 does make the code less efficient than it could be. */
5957 if (undefs_tail
!= info
->hash
->undefs_tail
)
5960 /* Look backward to mark all symbols from this object file
5961 which we have already seen in this pass. */
5965 included
[mark
] = true;
5970 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5972 /* We mark subsequent symbols from this object file as we go
5973 on through the loop. */
5974 last
= symdef
->file_offset
;
5987 /* Given an ELF BFD, add symbols to the global hash table as
5991 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5993 switch (bfd_get_format (abfd
))
5996 return elf_link_add_object_symbols (abfd
, info
);
5998 return elf_link_add_archive_symbols (abfd
, info
);
6000 bfd_set_error (bfd_error_wrong_format
);
6005 struct hash_codes_info
6007 unsigned long *hashcodes
;
6011 /* This function will be called though elf_link_hash_traverse to store
6012 all hash value of the exported symbols in an array. */
6015 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6017 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
6022 /* Ignore indirect symbols. These are added by the versioning code. */
6023 if (h
->dynindx
== -1)
6026 name
= h
->root
.root
.string
;
6027 if (h
->versioned
>= versioned
)
6029 char *p
= strchr (name
, ELF_VER_CHR
);
6032 alc
= (char *) bfd_malloc (p
- name
+ 1);
6038 memcpy (alc
, name
, p
- name
);
6039 alc
[p
- name
] = '\0';
6044 /* Compute the hash value. */
6045 ha
= bfd_elf_hash (name
);
6047 /* Store the found hash value in the array given as the argument. */
6048 *(inf
->hashcodes
)++ = ha
;
6050 /* And store it in the struct so that we can put it in the hash table
6052 h
->u
.elf_hash_value
= ha
;
6058 struct collect_gnu_hash_codes
6061 const struct elf_backend_data
*bed
;
6062 unsigned long int nsyms
;
6063 unsigned long int maskbits
;
6064 unsigned long int *hashcodes
;
6065 unsigned long int *hashval
;
6066 unsigned long int *indx
;
6067 unsigned long int *counts
;
6071 long int min_dynindx
;
6072 unsigned long int bucketcount
;
6073 unsigned long int symindx
;
6074 long int local_indx
;
6075 long int shift1
, shift2
;
6076 unsigned long int mask
;
6080 /* This function will be called though elf_link_hash_traverse to store
6081 all hash value of the exported symbols in an array. */
6084 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6086 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6091 /* Ignore indirect symbols. These are added by the versioning code. */
6092 if (h
->dynindx
== -1)
6095 /* Ignore also local symbols and undefined symbols. */
6096 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6099 name
= h
->root
.root
.string
;
6100 if (h
->versioned
>= versioned
)
6102 char *p
= strchr (name
, ELF_VER_CHR
);
6105 alc
= (char *) bfd_malloc (p
- name
+ 1);
6111 memcpy (alc
, name
, p
- name
);
6112 alc
[p
- name
] = '\0';
6117 /* Compute the hash value. */
6118 ha
= bfd_elf_gnu_hash (name
);
6120 /* Store the found hash value in the array for compute_bucket_count,
6121 and also for .dynsym reordering purposes. */
6122 s
->hashcodes
[s
->nsyms
] = ha
;
6123 s
->hashval
[h
->dynindx
] = ha
;
6125 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6126 s
->min_dynindx
= h
->dynindx
;
6132 /* This function will be called though elf_link_hash_traverse to do
6133 final dynamic symbol renumbering in case of .gnu.hash.
6134 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6135 to the translation table. */
6138 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6140 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6141 unsigned long int bucket
;
6142 unsigned long int val
;
6144 /* Ignore indirect symbols. */
6145 if (h
->dynindx
== -1)
6148 /* Ignore also local symbols and undefined symbols. */
6149 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6151 if (h
->dynindx
>= s
->min_dynindx
)
6153 if (s
->bed
->record_xhash_symbol
!= NULL
)
6155 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6159 h
->dynindx
= s
->local_indx
++;
6164 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6165 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6166 & ((s
->maskbits
>> s
->shift1
) - 1);
6167 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6169 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6170 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6171 if (s
->counts
[bucket
] == 1)
6172 /* Last element terminates the chain. */
6174 bfd_put_32 (s
->output_bfd
, val
,
6175 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6176 --s
->counts
[bucket
];
6177 if (s
->bed
->record_xhash_symbol
!= NULL
)
6179 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6181 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6184 h
->dynindx
= s
->indx
[bucket
]++;
6188 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6191 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6193 return !(h
->forced_local
6194 || h
->root
.type
== bfd_link_hash_undefined
6195 || h
->root
.type
== bfd_link_hash_undefweak
6196 || ((h
->root
.type
== bfd_link_hash_defined
6197 || h
->root
.type
== bfd_link_hash_defweak
)
6198 && h
->root
.u
.def
.section
->output_section
== NULL
));
6201 /* Array used to determine the number of hash table buckets to use
6202 based on the number of symbols there are. If there are fewer than
6203 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6204 fewer than 37 we use 17 buckets, and so forth. We never use more
6205 than 32771 buckets. */
6207 static const size_t elf_buckets
[] =
6209 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6213 /* Compute bucket count for hashing table. We do not use a static set
6214 of possible tables sizes anymore. Instead we determine for all
6215 possible reasonable sizes of the table the outcome (i.e., the
6216 number of collisions etc) and choose the best solution. The
6217 weighting functions are not too simple to allow the table to grow
6218 without bounds. Instead one of the weighting factors is the size.
6219 Therefore the result is always a good payoff between few collisions
6220 (= short chain lengths) and table size. */
6222 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6223 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6224 unsigned long int nsyms
,
6227 size_t best_size
= 0;
6228 unsigned long int i
;
6230 /* We have a problem here. The following code to optimize the table
6231 size requires an integer type with more the 32 bits. If
6232 BFD_HOST_U_64_BIT is set we know about such a type. */
6233 #ifdef BFD_HOST_U_64_BIT
6238 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
6239 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6240 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6241 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6242 unsigned long int *counts
;
6244 unsigned int no_improvement_count
= 0;
6246 /* Possible optimization parameters: if we have NSYMS symbols we say
6247 that the hashing table must at least have NSYMS/4 and at most
6249 minsize
= nsyms
/ 4;
6252 best_size
= maxsize
= nsyms
* 2;
6257 if ((best_size
& 31) == 0)
6261 /* Create array where we count the collisions in. We must use bfd_malloc
6262 since the size could be large. */
6264 amt
*= sizeof (unsigned long int);
6265 counts
= (unsigned long int *) bfd_malloc (amt
);
6269 /* Compute the "optimal" size for the hash table. The criteria is a
6270 minimal chain length. The minor criteria is (of course) the size
6272 for (i
= minsize
; i
< maxsize
; ++i
)
6274 /* Walk through the array of hashcodes and count the collisions. */
6275 BFD_HOST_U_64_BIT max
;
6276 unsigned long int j
;
6277 unsigned long int fact
;
6279 if (gnu_hash
&& (i
& 31) == 0)
6282 memset (counts
, '\0', i
* sizeof (unsigned long int));
6284 /* Determine how often each hash bucket is used. */
6285 for (j
= 0; j
< nsyms
; ++j
)
6286 ++counts
[hashcodes
[j
] % i
];
6288 /* For the weight function we need some information about the
6289 pagesize on the target. This is information need not be 100%
6290 accurate. Since this information is not available (so far) we
6291 define it here to a reasonable default value. If it is crucial
6292 to have a better value some day simply define this value. */
6293 # ifndef BFD_TARGET_PAGESIZE
6294 # define BFD_TARGET_PAGESIZE (4096)
6297 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6299 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6302 /* Variant 1: optimize for short chains. We add the squares
6303 of all the chain lengths (which favors many small chain
6304 over a few long chains). */
6305 for (j
= 0; j
< i
; ++j
)
6306 max
+= counts
[j
] * counts
[j
];
6308 /* This adds penalties for the overall size of the table. */
6309 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6312 /* Variant 2: Optimize a lot more for small table. Here we
6313 also add squares of the size but we also add penalties for
6314 empty slots (the +1 term). */
6315 for (j
= 0; j
< i
; ++j
)
6316 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6318 /* The overall size of the table is considered, but not as
6319 strong as in variant 1, where it is squared. */
6320 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6324 /* Compare with current best results. */
6325 if (max
< best_chlen
)
6329 no_improvement_count
= 0;
6331 /* PR 11843: Avoid futile long searches for the best bucket size
6332 when there are a large number of symbols. */
6333 else if (++no_improvement_count
== 100)
6340 #endif /* defined (BFD_HOST_U_64_BIT) */
6342 /* This is the fallback solution if no 64bit type is available or if we
6343 are not supposed to spend much time on optimizations. We select the
6344 bucket count using a fixed set of numbers. */
6345 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6347 best_size
= elf_buckets
[i
];
6348 if (nsyms
< elf_buckets
[i
+ 1])
6351 if (gnu_hash
&& best_size
< 2)
6358 /* Size any SHT_GROUP section for ld -r. */
6361 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6366 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6367 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6368 && (s
= ibfd
->sections
) != NULL
6369 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6370 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6375 /* Set a default stack segment size. The value in INFO wins. If it
6376 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6377 undefined it is initialized. */
6380 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6381 struct bfd_link_info
*info
,
6382 const char *legacy_symbol
,
6383 bfd_vma default_size
)
6385 struct elf_link_hash_entry
*h
= NULL
;
6387 /* Look for legacy symbol. */
6389 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6390 false, false, false);
6391 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6392 || h
->root
.type
== bfd_link_hash_defweak
)
6394 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6396 /* The symbol has no type if specified on the command line. */
6397 h
->type
= STT_OBJECT
;
6398 if (info
->stacksize
)
6399 /* xgettext:c-format */
6400 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6401 output_bfd
, legacy_symbol
);
6402 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6403 /* xgettext:c-format */
6404 _bfd_error_handler (_("%pB: %s not absolute"),
6405 output_bfd
, legacy_symbol
);
6407 info
->stacksize
= h
->root
.u
.def
.value
;
6410 if (!info
->stacksize
)
6411 /* If the user didn't set a size, or explicitly inhibit the
6412 size, set it now. */
6413 info
->stacksize
= default_size
;
6415 /* Provide the legacy symbol, if it is referenced. */
6416 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6417 || h
->root
.type
== bfd_link_hash_undefweak
))
6419 struct bfd_link_hash_entry
*bh
= NULL
;
6421 if (!(_bfd_generic_link_add_one_symbol
6422 (info
, output_bfd
, legacy_symbol
,
6423 BSF_GLOBAL
, bfd_abs_section_ptr
,
6424 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6425 NULL
, false, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6428 h
= (struct elf_link_hash_entry
*) bh
;
6430 h
->type
= STT_OBJECT
;
6436 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6438 struct elf_gc_sweep_symbol_info
6440 struct bfd_link_info
*info
;
6441 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6446 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6449 && (((h
->root
.type
== bfd_link_hash_defined
6450 || h
->root
.type
== bfd_link_hash_defweak
)
6451 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6452 && h
->root
.u
.def
.section
->gc_mark
))
6453 || h
->root
.type
== bfd_link_hash_undefined
6454 || h
->root
.type
== bfd_link_hash_undefweak
))
6456 struct elf_gc_sweep_symbol_info
*inf
;
6458 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6459 (*inf
->hide_symbol
) (inf
->info
, h
, true);
6462 h
->ref_regular_nonweak
= 0;
6468 /* Set up the sizes and contents of the ELF dynamic sections. This is
6469 called by the ELF linker emulation before_allocation routine. We
6470 must set the sizes of the sections before the linker sets the
6471 addresses of the various sections. */
6474 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6477 const char *filter_shlib
,
6479 const char *depaudit
,
6480 const char * const *auxiliary_filters
,
6481 struct bfd_link_info
*info
,
6482 asection
**sinterpptr
)
6485 const struct elf_backend_data
*bed
;
6489 if (!is_elf_hash_table (info
->hash
))
6492 dynobj
= elf_hash_table (info
)->dynobj
;
6494 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6496 struct bfd_elf_version_tree
*verdefs
;
6497 struct elf_info_failed asvinfo
;
6498 struct bfd_elf_version_tree
*t
;
6499 struct bfd_elf_version_expr
*d
;
6503 /* If we are supposed to export all symbols into the dynamic symbol
6504 table (this is not the normal case), then do so. */
6505 if (info
->export_dynamic
6506 || (bfd_link_executable (info
) && info
->dynamic
))
6508 struct elf_info_failed eif
;
6512 elf_link_hash_traverse (elf_hash_table (info
),
6513 _bfd_elf_export_symbol
,
6521 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6523 if (soname_indx
== (size_t) -1
6524 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6528 soname_indx
= (size_t) -1;
6530 /* Make all global versions with definition. */
6531 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6532 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6533 if (!d
->symver
&& d
->literal
)
6535 const char *verstr
, *name
;
6536 size_t namelen
, verlen
, newlen
;
6537 char *newname
, *p
, leading_char
;
6538 struct elf_link_hash_entry
*newh
;
6540 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6542 namelen
= strlen (name
) + (leading_char
!= '\0');
6544 verlen
= strlen (verstr
);
6545 newlen
= namelen
+ verlen
+ 3;
6547 newname
= (char *) bfd_malloc (newlen
);
6548 if (newname
== NULL
)
6550 newname
[0] = leading_char
;
6551 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6553 /* Check the hidden versioned definition. */
6554 p
= newname
+ namelen
;
6556 memcpy (p
, verstr
, verlen
+ 1);
6557 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6558 newname
, false, false,
6561 || (newh
->root
.type
!= bfd_link_hash_defined
6562 && newh
->root
.type
!= bfd_link_hash_defweak
))
6564 /* Check the default versioned definition. */
6566 memcpy (p
, verstr
, verlen
+ 1);
6567 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6568 newname
, false, false,
6573 /* Mark this version if there is a definition and it is
6574 not defined in a shared object. */
6576 && !newh
->def_dynamic
6577 && (newh
->root
.type
== bfd_link_hash_defined
6578 || newh
->root
.type
== bfd_link_hash_defweak
))
6582 /* Attach all the symbols to their version information. */
6583 asvinfo
.info
= info
;
6584 asvinfo
.failed
= false;
6586 elf_link_hash_traverse (elf_hash_table (info
),
6587 _bfd_elf_link_assign_sym_version
,
6592 if (!info
->allow_undefined_version
)
6594 /* Check if all global versions have a definition. */
6595 bool all_defined
= true;
6596 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6597 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6598 if (d
->literal
&& !d
->symver
&& !d
->script
)
6601 (_("%s: undefined version: %s"),
6602 d
->pattern
, t
->name
);
6603 all_defined
= false;
6608 bfd_set_error (bfd_error_bad_value
);
6613 /* Set up the version definition section. */
6614 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6615 BFD_ASSERT (s
!= NULL
);
6617 /* We may have created additional version definitions if we are
6618 just linking a regular application. */
6619 verdefs
= info
->version_info
;
6621 /* Skip anonymous version tag. */
6622 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6623 verdefs
= verdefs
->next
;
6625 if (verdefs
== NULL
&& !info
->create_default_symver
)
6626 s
->flags
|= SEC_EXCLUDE
;
6632 Elf_Internal_Verdef def
;
6633 Elf_Internal_Verdaux defaux
;
6634 struct bfd_link_hash_entry
*bh
;
6635 struct elf_link_hash_entry
*h
;
6641 /* Make space for the base version. */
6642 size
+= sizeof (Elf_External_Verdef
);
6643 size
+= sizeof (Elf_External_Verdaux
);
6646 /* Make space for the default version. */
6647 if (info
->create_default_symver
)
6649 size
+= sizeof (Elf_External_Verdef
);
6653 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6655 struct bfd_elf_version_deps
*n
;
6657 /* Don't emit base version twice. */
6661 size
+= sizeof (Elf_External_Verdef
);
6662 size
+= sizeof (Elf_External_Verdaux
);
6665 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6666 size
+= sizeof (Elf_External_Verdaux
);
6670 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6671 if (s
->contents
== NULL
&& s
->size
!= 0)
6674 /* Fill in the version definition section. */
6678 def
.vd_version
= VER_DEF_CURRENT
;
6679 def
.vd_flags
= VER_FLG_BASE
;
6682 if (info
->create_default_symver
)
6684 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6685 def
.vd_next
= sizeof (Elf_External_Verdef
);
6689 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6690 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6691 + sizeof (Elf_External_Verdaux
));
6694 if (soname_indx
!= (size_t) -1)
6696 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6698 def
.vd_hash
= bfd_elf_hash (soname
);
6699 defaux
.vda_name
= soname_indx
;
6706 name
= lbasename (bfd_get_filename (output_bfd
));
6707 def
.vd_hash
= bfd_elf_hash (name
);
6708 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6710 if (indx
== (size_t) -1)
6712 defaux
.vda_name
= indx
;
6714 defaux
.vda_next
= 0;
6716 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6717 (Elf_External_Verdef
*) p
);
6718 p
+= sizeof (Elf_External_Verdef
);
6719 if (info
->create_default_symver
)
6721 /* Add a symbol representing this version. */
6723 if (! (_bfd_generic_link_add_one_symbol
6724 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6726 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6728 h
= (struct elf_link_hash_entry
*) bh
;
6731 h
->type
= STT_OBJECT
;
6732 h
->verinfo
.vertree
= NULL
;
6734 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6737 /* Create a duplicate of the base version with the same
6738 aux block, but different flags. */
6741 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6743 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6744 + sizeof (Elf_External_Verdaux
));
6747 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6748 (Elf_External_Verdef
*) p
);
6749 p
+= sizeof (Elf_External_Verdef
);
6751 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6752 (Elf_External_Verdaux
*) p
);
6753 p
+= sizeof (Elf_External_Verdaux
);
6755 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6758 struct bfd_elf_version_deps
*n
;
6760 /* Don't emit the base version twice. */
6765 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6768 /* Add a symbol representing this version. */
6770 if (! (_bfd_generic_link_add_one_symbol
6771 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6773 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6775 h
= (struct elf_link_hash_entry
*) bh
;
6778 h
->type
= STT_OBJECT
;
6779 h
->verinfo
.vertree
= t
;
6781 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6784 def
.vd_version
= VER_DEF_CURRENT
;
6786 if (t
->globals
.list
== NULL
6787 && t
->locals
.list
== NULL
6789 def
.vd_flags
|= VER_FLG_WEAK
;
6790 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6791 def
.vd_cnt
= cdeps
+ 1;
6792 def
.vd_hash
= bfd_elf_hash (t
->name
);
6793 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6796 /* If a basever node is next, it *must* be the last node in
6797 the chain, otherwise Verdef construction breaks. */
6798 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6799 BFD_ASSERT (t
->next
->next
== NULL
);
6801 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6802 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6803 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6805 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6806 (Elf_External_Verdef
*) p
);
6807 p
+= sizeof (Elf_External_Verdef
);
6809 defaux
.vda_name
= h
->dynstr_index
;
6810 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6812 defaux
.vda_next
= 0;
6813 if (t
->deps
!= NULL
)
6814 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6815 t
->name_indx
= defaux
.vda_name
;
6817 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6818 (Elf_External_Verdaux
*) p
);
6819 p
+= sizeof (Elf_External_Verdaux
);
6821 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6823 if (n
->version_needed
== NULL
)
6825 /* This can happen if there was an error in the
6827 defaux
.vda_name
= 0;
6831 defaux
.vda_name
= n
->version_needed
->name_indx
;
6832 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6835 if (n
->next
== NULL
)
6836 defaux
.vda_next
= 0;
6838 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6840 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6841 (Elf_External_Verdaux
*) p
);
6842 p
+= sizeof (Elf_External_Verdaux
);
6846 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6850 bed
= get_elf_backend_data (output_bfd
);
6852 if (info
->gc_sections
&& bed
->can_gc_sections
)
6854 struct elf_gc_sweep_symbol_info sweep_info
;
6856 /* Remove the symbols that were in the swept sections from the
6857 dynamic symbol table. */
6858 sweep_info
.info
= info
;
6859 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6860 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6864 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6867 struct elf_find_verdep_info sinfo
;
6869 /* Work out the size of the version reference section. */
6871 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6872 BFD_ASSERT (s
!= NULL
);
6875 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6876 if (sinfo
.vers
== 0)
6878 sinfo
.failed
= false;
6880 elf_link_hash_traverse (elf_hash_table (info
),
6881 _bfd_elf_link_find_version_dependencies
,
6886 if (elf_tdata (output_bfd
)->verref
== NULL
)
6887 s
->flags
|= SEC_EXCLUDE
;
6890 Elf_Internal_Verneed
*vn
;
6895 /* Build the version dependency section. */
6898 for (vn
= elf_tdata (output_bfd
)->verref
;
6900 vn
= vn
->vn_nextref
)
6902 Elf_Internal_Vernaux
*a
;
6904 size
+= sizeof (Elf_External_Verneed
);
6906 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6907 size
+= sizeof (Elf_External_Vernaux
);
6911 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6912 if (s
->contents
== NULL
)
6916 for (vn
= elf_tdata (output_bfd
)->verref
;
6918 vn
= vn
->vn_nextref
)
6921 Elf_Internal_Vernaux
*a
;
6925 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6928 vn
->vn_version
= VER_NEED_CURRENT
;
6930 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6931 elf_dt_name (vn
->vn_bfd
) != NULL
6932 ? elf_dt_name (vn
->vn_bfd
)
6933 : lbasename (bfd_get_filename
6936 if (indx
== (size_t) -1)
6939 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6940 if (vn
->vn_nextref
== NULL
)
6943 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6944 + caux
* sizeof (Elf_External_Vernaux
));
6946 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6947 (Elf_External_Verneed
*) p
);
6948 p
+= sizeof (Elf_External_Verneed
);
6950 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6952 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6953 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6954 a
->vna_nodename
, false);
6955 if (indx
== (size_t) -1)
6958 if (a
->vna_nextptr
== NULL
)
6961 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6963 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6964 (Elf_External_Vernaux
*) p
);
6965 p
+= sizeof (Elf_External_Vernaux
);
6969 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6973 /* Any syms created from now on start with -1 in
6974 got.refcount/offset and plt.refcount/offset. */
6975 elf_hash_table (info
)->init_got_refcount
6976 = elf_hash_table (info
)->init_got_offset
;
6977 elf_hash_table (info
)->init_plt_refcount
6978 = elf_hash_table (info
)->init_plt_offset
;
6980 if (bfd_link_relocatable (info
)
6981 && !_bfd_elf_size_group_sections (info
))
6984 /* The backend may have to create some sections regardless of whether
6985 we're dynamic or not. */
6986 if (bed
->elf_backend_always_size_sections
6987 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6990 /* Determine any GNU_STACK segment requirements, after the backend
6991 has had a chance to set a default segment size. */
6992 if (info
->execstack
)
6993 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
6994 else if (info
->noexecstack
)
6995 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
6999 asection
*notesec
= NULL
;
7002 for (inputobj
= info
->input_bfds
;
7004 inputobj
= inputobj
->link
.next
)
7009 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
7011 s
= inputobj
->sections
;
7012 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
7015 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
7018 if (s
->flags
& SEC_CODE
)
7022 else if (bed
->default_execstack
)
7025 if (notesec
|| info
->stacksize
> 0)
7026 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
7027 if (notesec
&& exec
&& bfd_link_relocatable (info
)
7028 && notesec
->output_section
!= bfd_abs_section_ptr
)
7029 notesec
->output_section
->flags
|= SEC_CODE
;
7032 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7034 struct elf_info_failed eif
;
7035 struct elf_link_hash_entry
*h
;
7039 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7040 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7044 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7046 info
->flags
|= DF_SYMBOLIC
;
7054 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7056 if (indx
== (size_t) -1)
7059 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7060 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7064 if (filter_shlib
!= NULL
)
7068 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7069 filter_shlib
, true);
7070 if (indx
== (size_t) -1
7071 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7075 if (auxiliary_filters
!= NULL
)
7077 const char * const *p
;
7079 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7083 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7085 if (indx
== (size_t) -1
7086 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7095 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7097 if (indx
== (size_t) -1
7098 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7102 if (depaudit
!= NULL
)
7106 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7108 if (indx
== (size_t) -1
7109 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7116 /* Find all symbols which were defined in a dynamic object and make
7117 the backend pick a reasonable value for them. */
7118 elf_link_hash_traverse (elf_hash_table (info
),
7119 _bfd_elf_adjust_dynamic_symbol
,
7124 /* Add some entries to the .dynamic section. We fill in some of the
7125 values later, in bfd_elf_final_link, but we must add the entries
7126 now so that we know the final size of the .dynamic section. */
7128 /* If there are initialization and/or finalization functions to
7129 call then add the corresponding DT_INIT/DT_FINI entries. */
7130 h
= (info
->init_function
7131 ? elf_link_hash_lookup (elf_hash_table (info
),
7132 info
->init_function
, false,
7139 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7142 h
= (info
->fini_function
7143 ? elf_link_hash_lookup (elf_hash_table (info
),
7144 info
->fini_function
, false,
7151 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7155 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7156 if (s
!= NULL
&& s
->linker_has_input
)
7158 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7159 if (! bfd_link_executable (info
))
7164 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7165 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7166 && (o
= sub
->sections
) != NULL
7167 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7168 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7169 if (elf_section_data (o
)->this_hdr
.sh_type
7170 == SHT_PREINIT_ARRAY
)
7173 (_("%pB: .preinit_array section is not allowed in DSO"),
7178 bfd_set_error (bfd_error_nonrepresentable_section
);
7182 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7183 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7186 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7187 if (s
!= NULL
&& s
->linker_has_input
)
7189 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7190 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7193 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7194 if (s
!= NULL
&& s
->linker_has_input
)
7196 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7197 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7201 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7202 /* If .dynstr is excluded from the link, we don't want any of
7203 these tags. Strictly, we should be checking each section
7204 individually; This quick check covers for the case where
7205 someone does a /DISCARD/ : { *(*) }. */
7206 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7208 bfd_size_type strsize
;
7210 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7211 if ((info
->emit_hash
7212 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7213 || (info
->emit_gnu_hash
7214 && (bed
->record_xhash_symbol
== NULL
7215 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7216 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7217 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7218 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7219 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7221 || (info
->gnu_flags_1
7222 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_FLAGS_1
,
7223 info
->gnu_flags_1
)))
7228 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7231 /* The backend must work out the sizes of all the other dynamic
7234 && bed
->elf_backend_size_dynamic_sections
!= NULL
7235 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7238 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7240 if (elf_tdata (output_bfd
)->cverdefs
)
7242 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7244 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7245 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7249 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7251 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7254 else if (info
->flags
& DF_BIND_NOW
)
7256 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7262 if (bfd_link_executable (info
))
7263 info
->flags_1
&= ~ (DF_1_INITFIRST
7266 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7270 if (elf_tdata (output_bfd
)->cverrefs
)
7272 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7274 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7275 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7279 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7280 && elf_tdata (output_bfd
)->cverdefs
== 0)
7281 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7285 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7286 s
->flags
|= SEC_EXCLUDE
;
7292 /* Find the first non-excluded output section. We'll use its
7293 section symbol for some emitted relocs. */
7295 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7298 asection
*found
= NULL
;
7300 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7301 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7302 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7305 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7308 elf_hash_table (info
)->text_index_section
= found
;
7311 /* Find two non-excluded output sections, one for code, one for data.
7312 We'll use their section symbols for some emitted relocs. */
7314 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7317 asection
*found
= NULL
;
7319 /* Data first, since setting text_index_section changes
7320 _bfd_elf_omit_section_dynsym_default. */
7321 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7322 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7323 && !(s
->flags
& SEC_READONLY
)
7324 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7327 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7330 elf_hash_table (info
)->data_index_section
= found
;
7332 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7333 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7334 && (s
->flags
& SEC_READONLY
)
7335 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7340 elf_hash_table (info
)->text_index_section
= found
;
7343 #define GNU_HASH_SECTION_NAME(bed) \
7344 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7347 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7349 const struct elf_backend_data
*bed
;
7350 unsigned long section_sym_count
;
7351 bfd_size_type dynsymcount
= 0;
7353 if (!is_elf_hash_table (info
->hash
))
7356 bed
= get_elf_backend_data (output_bfd
);
7357 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7359 /* Assign dynsym indices. In a shared library we generate a section
7360 symbol for each output section, which come first. Next come all
7361 of the back-end allocated local dynamic syms, followed by the rest
7362 of the global symbols.
7364 This is usually not needed for static binaries, however backends
7365 can request to always do it, e.g. the MIPS backend uses dynamic
7366 symbol counts to lay out GOT, which will be produced in the
7367 presence of GOT relocations even in static binaries (holding fixed
7368 data in that case, to satisfy those relocations). */
7370 if (elf_hash_table (info
)->dynamic_sections_created
7371 || bed
->always_renumber_dynsyms
)
7372 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7373 §ion_sym_count
);
7375 if (elf_hash_table (info
)->dynamic_sections_created
)
7379 unsigned int dtagcount
;
7381 dynobj
= elf_hash_table (info
)->dynobj
;
7383 /* Work out the size of the symbol version section. */
7384 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7385 BFD_ASSERT (s
!= NULL
);
7386 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7388 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7389 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7390 if (s
->contents
== NULL
)
7393 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7397 /* Set the size of the .dynsym and .hash sections. We counted
7398 the number of dynamic symbols in elf_link_add_object_symbols.
7399 We will build the contents of .dynsym and .hash when we build
7400 the final symbol table, because until then we do not know the
7401 correct value to give the symbols. We built the .dynstr
7402 section as we went along in elf_link_add_object_symbols. */
7403 s
= elf_hash_table (info
)->dynsym
;
7404 BFD_ASSERT (s
!= NULL
);
7405 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7407 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7408 if (s
->contents
== NULL
)
7411 /* The first entry in .dynsym is a dummy symbol. Clear all the
7412 section syms, in case we don't output them all. */
7413 ++section_sym_count
;
7414 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7416 elf_hash_table (info
)->bucketcount
= 0;
7418 /* Compute the size of the hashing table. As a side effect this
7419 computes the hash values for all the names we export. */
7420 if (info
->emit_hash
)
7422 unsigned long int *hashcodes
;
7423 struct hash_codes_info hashinf
;
7425 unsigned long int nsyms
;
7427 size_t hash_entry_size
;
7429 /* Compute the hash values for all exported symbols. At the same
7430 time store the values in an array so that we could use them for
7432 amt
= dynsymcount
* sizeof (unsigned long int);
7433 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7434 if (hashcodes
== NULL
)
7436 hashinf
.hashcodes
= hashcodes
;
7437 hashinf
.error
= false;
7439 /* Put all hash values in HASHCODES. */
7440 elf_link_hash_traverse (elf_hash_table (info
),
7441 elf_collect_hash_codes
, &hashinf
);
7448 nsyms
= hashinf
.hashcodes
- hashcodes
;
7450 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7453 if (bucketcount
== 0 && nsyms
> 0)
7456 elf_hash_table (info
)->bucketcount
= bucketcount
;
7458 s
= bfd_get_linker_section (dynobj
, ".hash");
7459 BFD_ASSERT (s
!= NULL
);
7460 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7461 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7462 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7463 if (s
->contents
== NULL
)
7466 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7467 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7468 s
->contents
+ hash_entry_size
);
7471 if (info
->emit_gnu_hash
)
7474 unsigned char *contents
;
7475 struct collect_gnu_hash_codes cinfo
;
7479 memset (&cinfo
, 0, sizeof (cinfo
));
7481 /* Compute the hash values for all exported symbols. At the same
7482 time store the values in an array so that we could use them for
7484 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7485 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7486 if (cinfo
.hashcodes
== NULL
)
7489 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7490 cinfo
.min_dynindx
= -1;
7491 cinfo
.output_bfd
= output_bfd
;
7494 /* Put all hash values in HASHCODES. */
7495 elf_link_hash_traverse (elf_hash_table (info
),
7496 elf_collect_gnu_hash_codes
, &cinfo
);
7499 free (cinfo
.hashcodes
);
7504 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7506 if (bucketcount
== 0)
7508 free (cinfo
.hashcodes
);
7512 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7513 BFD_ASSERT (s
!= NULL
);
7515 if (cinfo
.nsyms
== 0)
7517 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7518 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7519 free (cinfo
.hashcodes
);
7520 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7521 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7522 if (contents
== NULL
)
7524 s
->contents
= contents
;
7525 /* 1 empty bucket. */
7526 bfd_put_32 (output_bfd
, 1, contents
);
7527 /* SYMIDX above the special symbol 0. */
7528 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7529 /* Just one word for bitmask. */
7530 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7531 /* Only hash fn bloom filter. */
7532 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7533 /* No hashes are valid - empty bitmask. */
7534 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7535 /* No hashes in the only bucket. */
7536 bfd_put_32 (output_bfd
, 0,
7537 contents
+ 16 + bed
->s
->arch_size
/ 8);
7541 unsigned long int maskwords
, maskbitslog2
, x
;
7542 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7546 while ((x
>>= 1) != 0)
7548 if (maskbitslog2
< 3)
7550 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7551 maskbitslog2
= maskbitslog2
+ 3;
7553 maskbitslog2
= maskbitslog2
+ 2;
7554 if (bed
->s
->arch_size
== 64)
7556 if (maskbitslog2
== 5)
7562 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7563 cinfo
.shift2
= maskbitslog2
;
7564 cinfo
.maskbits
= 1 << maskbitslog2
;
7565 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7566 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7567 amt
+= maskwords
* sizeof (bfd_vma
);
7568 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7569 if (cinfo
.bitmask
== NULL
)
7571 free (cinfo
.hashcodes
);
7575 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7576 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7577 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7578 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7580 /* Determine how often each hash bucket is used. */
7581 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7582 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7583 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7585 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7586 if (cinfo
.counts
[i
] != 0)
7588 cinfo
.indx
[i
] = cnt
;
7589 cnt
+= cinfo
.counts
[i
];
7591 BFD_ASSERT (cnt
== dynsymcount
);
7592 cinfo
.bucketcount
= bucketcount
;
7593 cinfo
.local_indx
= cinfo
.min_dynindx
;
7595 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7596 s
->size
+= cinfo
.maskbits
/ 8;
7597 if (bed
->record_xhash_symbol
!= NULL
)
7598 s
->size
+= cinfo
.nsyms
* 4;
7599 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7600 if (contents
== NULL
)
7602 free (cinfo
.bitmask
);
7603 free (cinfo
.hashcodes
);
7607 s
->contents
= contents
;
7608 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7609 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7610 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7611 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7612 contents
+= 16 + cinfo
.maskbits
/ 8;
7614 for (i
= 0; i
< bucketcount
; ++i
)
7616 if (cinfo
.counts
[i
] == 0)
7617 bfd_put_32 (output_bfd
, 0, contents
);
7619 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7623 cinfo
.contents
= contents
;
7625 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7626 /* Renumber dynamic symbols, if populating .gnu.hash section.
7627 If using .MIPS.xhash, populate the translation table. */
7628 elf_link_hash_traverse (elf_hash_table (info
),
7629 elf_gnu_hash_process_symidx
, &cinfo
);
7631 contents
= s
->contents
+ 16;
7632 for (i
= 0; i
< maskwords
; ++i
)
7634 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7636 contents
+= bed
->s
->arch_size
/ 8;
7639 free (cinfo
.bitmask
);
7640 free (cinfo
.hashcodes
);
7644 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7645 BFD_ASSERT (s
!= NULL
);
7647 elf_finalize_dynstr (output_bfd
, info
);
7649 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7651 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7652 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7659 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7662 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7665 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7666 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7669 /* Finish SHF_MERGE section merging. */
7672 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7677 if (!is_elf_hash_table (info
->hash
))
7680 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7681 if ((ibfd
->flags
& DYNAMIC
) == 0
7682 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7683 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7684 == get_elf_backend_data (obfd
)->s
->elfclass
))
7685 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7686 if ((sec
->flags
& SEC_MERGE
) != 0
7687 && !bfd_is_abs_section (sec
->output_section
))
7689 struct bfd_elf_section_data
*secdata
;
7691 secdata
= elf_section_data (sec
);
7692 if (! _bfd_add_merge_section (obfd
,
7693 &elf_hash_table (info
)->merge_info
,
7694 sec
, &secdata
->sec_info
))
7696 else if (secdata
->sec_info
)
7697 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7700 if (elf_hash_table (info
)->merge_info
!= NULL
)
7701 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7702 merge_sections_remove_hook
);
7706 /* Create an entry in an ELF linker hash table. */
7708 struct bfd_hash_entry
*
7709 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7710 struct bfd_hash_table
*table
,
7713 /* Allocate the structure if it has not already been allocated by a
7717 entry
= (struct bfd_hash_entry
*)
7718 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7723 /* Call the allocation method of the superclass. */
7724 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7727 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7728 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7730 /* Set local fields. */
7733 ret
->got
= htab
->init_got_refcount
;
7734 ret
->plt
= htab
->init_plt_refcount
;
7735 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7736 - offsetof (struct elf_link_hash_entry
, size
)));
7737 /* Assume that we have been called by a non-ELF symbol reader.
7738 This flag is then reset by the code which reads an ELF input
7739 file. This ensures that a symbol created by a non-ELF symbol
7740 reader will have the flag set correctly. */
7747 /* Copy data from an indirect symbol to its direct symbol, hiding the
7748 old indirect symbol. Also used for copying flags to a weakdef. */
7751 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7752 struct elf_link_hash_entry
*dir
,
7753 struct elf_link_hash_entry
*ind
)
7755 struct elf_link_hash_table
*htab
;
7757 if (ind
->dyn_relocs
!= NULL
)
7759 if (dir
->dyn_relocs
!= NULL
)
7761 struct elf_dyn_relocs
**pp
;
7762 struct elf_dyn_relocs
*p
;
7764 /* Add reloc counts against the indirect sym to the direct sym
7765 list. Merge any entries against the same section. */
7766 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
7768 struct elf_dyn_relocs
*q
;
7770 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
7771 if (q
->sec
== p
->sec
)
7773 q
->pc_count
+= p
->pc_count
;
7774 q
->count
+= p
->count
;
7781 *pp
= dir
->dyn_relocs
;
7784 dir
->dyn_relocs
= ind
->dyn_relocs
;
7785 ind
->dyn_relocs
= NULL
;
7788 /* Copy down any references that we may have already seen to the
7789 symbol which just became indirect. */
7791 if (dir
->versioned
!= versioned_hidden
)
7792 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7793 dir
->ref_regular
|= ind
->ref_regular
;
7794 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7795 dir
->non_got_ref
|= ind
->non_got_ref
;
7796 dir
->needs_plt
|= ind
->needs_plt
;
7797 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7799 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7802 /* Copy over the global and procedure linkage table refcount entries.
7803 These may have been already set up by a check_relocs routine. */
7804 htab
= elf_hash_table (info
);
7805 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7807 if (dir
->got
.refcount
< 0)
7808 dir
->got
.refcount
= 0;
7809 dir
->got
.refcount
+= ind
->got
.refcount
;
7810 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7813 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7815 if (dir
->plt
.refcount
< 0)
7816 dir
->plt
.refcount
= 0;
7817 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7818 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7821 if (ind
->dynindx
!= -1)
7823 if (dir
->dynindx
!= -1)
7824 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7825 dir
->dynindx
= ind
->dynindx
;
7826 dir
->dynstr_index
= ind
->dynstr_index
;
7828 ind
->dynstr_index
= 0;
7833 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7834 struct elf_link_hash_entry
*h
,
7837 /* STT_GNU_IFUNC symbol must go through PLT. */
7838 if (h
->type
!= STT_GNU_IFUNC
)
7840 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7845 h
->forced_local
= 1;
7846 if (h
->dynindx
!= -1)
7848 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7851 h
->dynstr_index
= 0;
7856 /* Hide a symbol. */
7859 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
7860 struct bfd_link_info
*info
,
7861 struct bfd_link_hash_entry
*h
)
7863 if (is_elf_hash_table (info
->hash
))
7865 const struct elf_backend_data
*bed
7866 = get_elf_backend_data (output_bfd
);
7867 struct elf_link_hash_entry
*eh
7868 = (struct elf_link_hash_entry
*) h
;
7869 bed
->elf_backend_hide_symbol (info
, eh
, true);
7870 eh
->def_dynamic
= 0;
7871 eh
->ref_dynamic
= 0;
7872 eh
->dynamic_def
= 0;
7876 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7880 _bfd_elf_link_hash_table_init
7881 (struct elf_link_hash_table
*table
,
7883 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7884 struct bfd_hash_table
*,
7886 unsigned int entsize
,
7887 enum elf_target_id target_id
)
7890 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7892 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7893 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7894 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7895 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7896 /* The first dynamic symbol is a dummy. */
7897 table
->dynsymcount
= 1;
7899 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7901 table
->root
.type
= bfd_link_elf_hash_table
;
7902 table
->hash_table_id
= target_id
;
7903 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
7908 /* Create an ELF linker hash table. */
7910 struct bfd_link_hash_table
*
7911 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7913 struct elf_link_hash_table
*ret
;
7914 size_t amt
= sizeof (struct elf_link_hash_table
);
7916 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7920 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7921 sizeof (struct elf_link_hash_entry
),
7927 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7932 /* Destroy an ELF linker hash table. */
7935 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7937 struct elf_link_hash_table
*htab
;
7939 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7940 if (htab
->dynstr
!= NULL
)
7941 _bfd_elf_strtab_free (htab
->dynstr
);
7942 _bfd_merge_sections_free (htab
->merge_info
);
7943 _bfd_generic_link_hash_table_free (obfd
);
7946 /* This is a hook for the ELF emulation code in the generic linker to
7947 tell the backend linker what file name to use for the DT_NEEDED
7948 entry for a dynamic object. */
7951 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7953 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7954 && bfd_get_format (abfd
) == bfd_object
)
7955 elf_dt_name (abfd
) = name
;
7959 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7962 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7963 && bfd_get_format (abfd
) == bfd_object
)
7964 lib_class
= elf_dyn_lib_class (abfd
);
7971 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7973 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7974 && bfd_get_format (abfd
) == bfd_object
)
7975 elf_dyn_lib_class (abfd
) = lib_class
;
7978 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7979 the linker ELF emulation code. */
7981 struct bfd_link_needed_list
*
7982 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7983 struct bfd_link_info
*info
)
7985 if (! is_elf_hash_table (info
->hash
))
7987 return elf_hash_table (info
)->needed
;
7990 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7991 hook for the linker ELF emulation code. */
7993 struct bfd_link_needed_list
*
7994 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7995 struct bfd_link_info
*info
)
7997 if (! is_elf_hash_table (info
->hash
))
7999 return elf_hash_table (info
)->runpath
;
8002 /* Get the name actually used for a dynamic object for a link. This
8003 is the SONAME entry if there is one. Otherwise, it is the string
8004 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8007 bfd_elf_get_dt_soname (bfd
*abfd
)
8009 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8010 && bfd_get_format (abfd
) == bfd_object
)
8011 return elf_dt_name (abfd
);
8015 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8016 the ELF linker emulation code. */
8019 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
8020 struct bfd_link_needed_list
**pneeded
)
8023 bfd_byte
*dynbuf
= NULL
;
8024 unsigned int elfsec
;
8025 unsigned long shlink
;
8026 bfd_byte
*extdyn
, *extdynend
;
8028 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
8032 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
8033 || bfd_get_format (abfd
) != bfd_object
)
8036 s
= bfd_get_section_by_name (abfd
, ".dynamic");
8037 if (s
== NULL
|| s
->size
== 0)
8040 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
8043 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8044 if (elfsec
== SHN_BAD
)
8047 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8049 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8050 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8053 extdynend
= extdyn
+ s
->size
;
8054 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
8056 Elf_Internal_Dyn dyn
;
8058 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8060 if (dyn
.d_tag
== DT_NULL
)
8063 if (dyn
.d_tag
== DT_NEEDED
)
8066 struct bfd_link_needed_list
*l
;
8067 unsigned int tagv
= dyn
.d_un
.d_val
;
8070 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8075 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8095 struct elf_symbuf_symbol
8097 unsigned long st_name
; /* Symbol name, index in string tbl */
8098 unsigned char st_info
; /* Type and binding attributes */
8099 unsigned char st_other
; /* Visibilty, and target specific */
8102 struct elf_symbuf_head
8104 struct elf_symbuf_symbol
*ssym
;
8106 unsigned int st_shndx
;
8113 Elf_Internal_Sym
*isym
;
8114 struct elf_symbuf_symbol
*ssym
;
8120 /* Sort references to symbols by ascending section number. */
8123 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8125 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8126 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8128 if (s1
->st_shndx
!= s2
->st_shndx
)
8129 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8130 /* Final sort by the address of the sym in the symbuf ensures
8133 return s1
> s2
? 1 : -1;
8138 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8140 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8141 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8142 int ret
= strcmp (s1
->name
, s2
->name
);
8145 if (s1
->u
.p
!= s2
->u
.p
)
8146 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8150 static struct elf_symbuf_head
*
8151 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8153 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8154 struct elf_symbuf_symbol
*ssym
;
8155 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8156 size_t i
, shndx_count
, total_size
, amt
;
8158 amt
= symcount
* sizeof (*indbuf
);
8159 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8163 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8164 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8165 *ind
++ = &isymbuf
[i
];
8168 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8169 elf_sort_elf_symbol
);
8172 if (indbufend
> indbuf
)
8173 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8174 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8177 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8178 + (indbufend
- indbuf
) * sizeof (*ssym
));
8179 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8180 if (ssymbuf
== NULL
)
8186 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8187 ssymbuf
->ssym
= NULL
;
8188 ssymbuf
->count
= shndx_count
;
8189 ssymbuf
->st_shndx
= 0;
8190 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8192 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8195 ssymhead
->ssym
= ssym
;
8196 ssymhead
->count
= 0;
8197 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8199 ssym
->st_name
= (*ind
)->st_name
;
8200 ssym
->st_info
= (*ind
)->st_info
;
8201 ssym
->st_other
= (*ind
)->st_other
;
8204 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8205 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
8212 /* Check if 2 sections define the same set of local and global
8216 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8217 struct bfd_link_info
*info
)
8220 const struct elf_backend_data
*bed1
, *bed2
;
8221 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8222 size_t symcount1
, symcount2
;
8223 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8224 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8225 Elf_Internal_Sym
*isym
, *isymend
;
8226 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8227 size_t count1
, count2
, sec_count1
, sec_count2
, i
;
8228 unsigned int shndx1
, shndx2
;
8230 bool ignore_section_symbol_p
;
8235 /* Both sections have to be in ELF. */
8236 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8237 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8240 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8243 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8244 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8245 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8248 bed1
= get_elf_backend_data (bfd1
);
8249 bed2
= get_elf_backend_data (bfd2
);
8250 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8251 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8252 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8253 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8255 if (symcount1
== 0 || symcount2
== 0)
8261 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8262 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8264 /* Ignore section symbols only when matching non-debugging sections
8265 or linkonce section with comdat section. */
8266 ignore_section_symbol_p
8267 = ((sec1
->flags
& SEC_DEBUGGING
) == 0
8268 || ((elf_section_flags (sec1
) & SHF_GROUP
)
8269 != (elf_section_flags (sec2
) & SHF_GROUP
)));
8271 if (ssymbuf1
== NULL
)
8273 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8275 if (isymbuf1
== NULL
)
8278 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8280 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8281 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8285 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8287 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8289 if (isymbuf2
== NULL
)
8292 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8294 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8295 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8299 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8301 /* Optimized faster version. */
8303 struct elf_symbol
*symp
;
8304 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8307 hi
= ssymbuf1
->count
;
8313 mid
= (lo
+ hi
) / 2;
8314 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8316 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8320 count1
= ssymbuf1
[mid
].count
;
8325 if (ignore_section_symbol_p
)
8327 for (i
= 0; i
< count1
; i
++)
8328 if (ELF_ST_TYPE (ssymbuf1
->ssym
[i
].st_info
) == STT_SECTION
)
8330 count1
-= sec_count1
;
8334 hi
= ssymbuf2
->count
;
8340 mid
= (lo
+ hi
) / 2;
8341 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8343 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8347 count2
= ssymbuf2
[mid
].count
;
8352 if (ignore_section_symbol_p
)
8354 for (i
= 0; i
< count2
; i
++)
8355 if (ELF_ST_TYPE (ssymbuf2
->ssym
[i
].st_info
) == STT_SECTION
)
8357 count2
-= sec_count2
;
8360 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8364 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8366 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8367 if (symtable1
== NULL
|| symtable2
== NULL
)
8371 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
+ sec_count1
;
8372 ssym
< ssymend
; ssym
++)
8374 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8376 symp
->u
.ssym
= ssym
;
8377 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8384 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
+ sec_count2
;
8385 ssym
< ssymend
; ssym
++)
8387 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8389 symp
->u
.ssym
= ssym
;
8390 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8396 /* Sort symbol by name. */
8397 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8398 elf_sym_name_compare
);
8399 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8400 elf_sym_name_compare
);
8402 for (i
= 0; i
< count1
; i
++)
8403 /* Two symbols must have the same binding, type and name. */
8404 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8405 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8406 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8413 symtable1
= (struct elf_symbol
*)
8414 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8415 symtable2
= (struct elf_symbol
*)
8416 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8417 if (symtable1
== NULL
|| symtable2
== NULL
)
8420 /* Count definitions in the section. */
8422 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8423 if (isym
->st_shndx
== shndx1
8424 && (!ignore_section_symbol_p
8425 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8426 symtable1
[count1
++].u
.isym
= isym
;
8429 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8430 if (isym
->st_shndx
== shndx2
8431 && (!ignore_section_symbol_p
8432 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8433 symtable2
[count2
++].u
.isym
= isym
;
8435 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8438 for (i
= 0; i
< count1
; i
++)
8440 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8441 symtable1
[i
].u
.isym
->st_name
);
8443 for (i
= 0; i
< count2
; i
++)
8445 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8446 symtable2
[i
].u
.isym
->st_name
);
8448 /* Sort symbol by name. */
8449 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8450 elf_sym_name_compare
);
8451 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8452 elf_sym_name_compare
);
8454 for (i
= 0; i
< count1
; i
++)
8455 /* Two symbols must have the same binding, type and name. */
8456 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8457 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8458 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8472 /* Return TRUE if 2 section types are compatible. */
8475 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8476 bfd
*bbfd
, const asection
*bsec
)
8480 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8481 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8484 return elf_section_type (asec
) == elf_section_type (bsec
);
8487 /* Final phase of ELF linker. */
8489 /* A structure we use to avoid passing large numbers of arguments. */
8491 struct elf_final_link_info
8493 /* General link information. */
8494 struct bfd_link_info
*info
;
8497 /* Symbol string table. */
8498 struct elf_strtab_hash
*symstrtab
;
8499 /* .hash section. */
8501 /* symbol version section (.gnu.version). */
8502 asection
*symver_sec
;
8503 /* Buffer large enough to hold contents of any section. */
8505 /* Buffer large enough to hold external relocs of any section. */
8506 void *external_relocs
;
8507 /* Buffer large enough to hold internal relocs of any section. */
8508 Elf_Internal_Rela
*internal_relocs
;
8509 /* Buffer large enough to hold external local symbols of any input
8511 bfd_byte
*external_syms
;
8512 /* And a buffer for symbol section indices. */
8513 Elf_External_Sym_Shndx
*locsym_shndx
;
8514 /* Buffer large enough to hold internal local symbols of any input
8516 Elf_Internal_Sym
*internal_syms
;
8517 /* Array large enough to hold a symbol index for each local symbol
8518 of any input BFD. */
8520 /* Array large enough to hold a section pointer for each local
8521 symbol of any input BFD. */
8522 asection
**sections
;
8523 /* Buffer for SHT_SYMTAB_SHNDX section. */
8524 Elf_External_Sym_Shndx
*symshndxbuf
;
8525 /* Number of STT_FILE syms seen. */
8526 size_t filesym_count
;
8527 /* Local symbol hash table. */
8528 struct bfd_hash_table local_hash_table
;
8531 struct local_hash_entry
8533 /* Base hash table entry structure. */
8534 struct bfd_hash_entry root
;
8535 /* Size of the local symbol name. */
8537 /* Number of the duplicated local symbol names. */
8541 /* Create an entry in the local symbol hash table. */
8543 static struct bfd_hash_entry
*
8544 local_hash_newfunc (struct bfd_hash_entry
*entry
,
8545 struct bfd_hash_table
*table
,
8549 /* Allocate the structure if it has not already been allocated by a
8553 entry
= bfd_hash_allocate (table
,
8554 sizeof (struct local_hash_entry
));
8559 /* Call the allocation method of the superclass. */
8560 entry
= bfd_hash_newfunc (entry
, table
, string
);
8563 ((struct local_hash_entry
*) entry
)->count
= 0;
8564 ((struct local_hash_entry
*) entry
)->size
= 0;
8570 /* This struct is used to pass information to elf_link_output_extsym. */
8572 struct elf_outext_info
8577 struct elf_final_link_info
*flinfo
;
8581 /* Support for evaluating a complex relocation.
8583 Complex relocations are generalized, self-describing relocations. The
8584 implementation of them consists of two parts: complex symbols, and the
8585 relocations themselves.
8587 The relocations use a reserved elf-wide relocation type code (R_RELC
8588 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8589 information (start bit, end bit, word width, etc) into the addend. This
8590 information is extracted from CGEN-generated operand tables within gas.
8592 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8593 internal) representing prefix-notation expressions, including but not
8594 limited to those sorts of expressions normally encoded as addends in the
8595 addend field. The symbol mangling format is:
8598 | <unary-operator> ':' <node>
8599 | <binary-operator> ':' <node> ':' <node>
8602 <literal> := 's' <digits=N> ':' <N character symbol name>
8603 | 'S' <digits=N> ':' <N character section name>
8607 <binary-operator> := as in C
8608 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8611 set_symbol_value (bfd
*bfd_with_globals
,
8612 Elf_Internal_Sym
*isymbuf
,
8617 struct elf_link_hash_entry
**sym_hashes
;
8618 struct elf_link_hash_entry
*h
;
8619 size_t extsymoff
= locsymcount
;
8621 if (symidx
< locsymcount
)
8623 Elf_Internal_Sym
*sym
;
8625 sym
= isymbuf
+ symidx
;
8626 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8628 /* It is a local symbol: move it to the
8629 "absolute" section and give it a value. */
8630 sym
->st_shndx
= SHN_ABS
;
8631 sym
->st_value
= val
;
8634 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8638 /* It is a global symbol: set its link type
8639 to "defined" and give it a value. */
8641 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8642 h
= sym_hashes
[symidx
- extsymoff
];
8643 while (h
->root
.type
== bfd_link_hash_indirect
8644 || h
->root
.type
== bfd_link_hash_warning
)
8645 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8646 h
->root
.type
= bfd_link_hash_defined
;
8647 h
->root
.u
.def
.value
= val
;
8648 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8652 resolve_symbol (const char *name
,
8654 struct elf_final_link_info
*flinfo
,
8656 Elf_Internal_Sym
*isymbuf
,
8659 Elf_Internal_Sym
*sym
;
8660 struct bfd_link_hash_entry
*global_entry
;
8661 const char *candidate
= NULL
;
8662 Elf_Internal_Shdr
*symtab_hdr
;
8665 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8667 for (i
= 0; i
< locsymcount
; ++ i
)
8671 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8674 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8675 symtab_hdr
->sh_link
,
8678 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8679 name
, candidate
, (unsigned long) sym
->st_value
);
8681 if (candidate
&& strcmp (candidate
, name
) == 0)
8683 asection
*sec
= flinfo
->sections
[i
];
8685 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8686 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8688 printf ("Found symbol with value %8.8lx\n",
8689 (unsigned long) *result
);
8695 /* Hmm, haven't found it yet. perhaps it is a global. */
8696 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8697 false, false, true);
8701 if (global_entry
->type
== bfd_link_hash_defined
8702 || global_entry
->type
== bfd_link_hash_defweak
)
8704 *result
= (global_entry
->u
.def
.value
8705 + global_entry
->u
.def
.section
->output_section
->vma
8706 + global_entry
->u
.def
.section
->output_offset
);
8708 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8709 global_entry
->root
.string
, (unsigned long) *result
);
8717 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8718 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8719 names like "foo.end" which is the end address of section "foo". */
8722 resolve_section (const char *name
,
8730 for (curr
= sections
; curr
; curr
= curr
->next
)
8731 if (strcmp (curr
->name
, name
) == 0)
8733 *result
= curr
->vma
;
8737 /* Hmm. still haven't found it. try pseudo-section names. */
8738 /* FIXME: This could be coded more efficiently... */
8739 for (curr
= sections
; curr
; curr
= curr
->next
)
8741 len
= strlen (curr
->name
);
8742 if (len
> strlen (name
))
8745 if (strncmp (curr
->name
, name
, len
) == 0)
8747 if (startswith (name
+ len
, ".end"))
8749 *result
= (curr
->vma
8750 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
8754 /* Insert more pseudo-section names here, if you like. */
8762 undefined_reference (const char *reftype
, const char *name
)
8764 /* xgettext:c-format */
8765 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8767 bfd_set_error (bfd_error_bad_value
);
8771 eval_symbol (bfd_vma
*result
,
8774 struct elf_final_link_info
*flinfo
,
8776 Elf_Internal_Sym
*isymbuf
,
8785 const char *sym
= *symp
;
8787 bool symbol_is_section
= false;
8792 if (len
< 1 || len
> sizeof (symbuf
))
8794 bfd_set_error (bfd_error_invalid_operation
);
8807 *result
= strtoul (sym
, (char **) symp
, 16);
8811 symbol_is_section
= true;
8815 symlen
= strtol (sym
, (char **) symp
, 10);
8816 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8818 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8820 bfd_set_error (bfd_error_invalid_operation
);
8824 memcpy (symbuf
, sym
, symlen
);
8825 symbuf
[symlen
] = '\0';
8826 *symp
= sym
+ symlen
;
8828 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8829 the symbol as a section, or vice-versa. so we're pretty liberal in our
8830 interpretation here; section means "try section first", not "must be a
8831 section", and likewise with symbol. */
8833 if (symbol_is_section
)
8835 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8836 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8837 isymbuf
, locsymcount
))
8839 undefined_reference ("section", symbuf
);
8845 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8846 isymbuf
, locsymcount
)
8847 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8850 undefined_reference ("symbol", symbuf
);
8857 /* All that remains are operators. */
8859 #define UNARY_OP(op) \
8860 if (startswith (sym, #op)) \
8862 sym += strlen (#op); \
8866 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8867 isymbuf, locsymcount, signed_p)) \
8870 *result = op ((bfd_signed_vma) a); \
8876 #define BINARY_OP_HEAD(op) \
8877 if (startswith (sym, #op)) \
8879 sym += strlen (#op); \
8883 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8884 isymbuf, locsymcount, signed_p)) \
8887 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8888 isymbuf, locsymcount, signed_p)) \
8890 #define BINARY_OP_TAIL(op) \
8892 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8897 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8901 BINARY_OP_HEAD (<<);
8902 if (b
>= sizeof (a
) * CHAR_BIT
)
8908 BINARY_OP_TAIL (<<);
8909 BINARY_OP_HEAD (>>);
8910 if (b
>= sizeof (a
) * CHAR_BIT
)
8912 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
8915 BINARY_OP_TAIL (>>);
8928 _bfd_error_handler (_("division by zero"));
8929 bfd_set_error (bfd_error_bad_value
);
8936 _bfd_error_handler (_("division by zero"));
8937 bfd_set_error (bfd_error_bad_value
);
8950 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8951 bfd_set_error (bfd_error_invalid_operation
);
8957 put_value (bfd_vma size
,
8958 unsigned long chunksz
,
8963 location
+= (size
- chunksz
);
8965 for (; size
; size
-= chunksz
, location
-= chunksz
)
8970 bfd_put_8 (input_bfd
, x
, location
);
8974 bfd_put_16 (input_bfd
, x
, location
);
8978 bfd_put_32 (input_bfd
, x
, location
);
8979 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8985 bfd_put_64 (input_bfd
, x
, location
);
8986 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8999 get_value (bfd_vma size
,
9000 unsigned long chunksz
,
9007 /* Sanity checks. */
9008 BFD_ASSERT (chunksz
<= sizeof (x
)
9011 && (size
% chunksz
) == 0
9012 && input_bfd
!= NULL
9013 && location
!= NULL
);
9015 if (chunksz
== sizeof (x
))
9017 BFD_ASSERT (size
== chunksz
);
9019 /* Make sure that we do not perform an undefined shift operation.
9020 We know that size == chunksz so there will only be one iteration
9021 of the loop below. */
9025 shift
= 8 * chunksz
;
9027 for (; size
; size
-= chunksz
, location
+= chunksz
)
9032 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
9035 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
9038 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
9042 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
9053 decode_complex_addend (unsigned long *start
, /* in bits */
9054 unsigned long *oplen
, /* in bits */
9055 unsigned long *len
, /* in bits */
9056 unsigned long *wordsz
, /* in bytes */
9057 unsigned long *chunksz
, /* in bytes */
9058 unsigned long *lsb0_p
,
9059 unsigned long *signed_p
,
9060 unsigned long *trunc_p
,
9061 unsigned long encoded
)
9063 * start
= encoded
& 0x3F;
9064 * len
= (encoded
>> 6) & 0x3F;
9065 * oplen
= (encoded
>> 12) & 0x3F;
9066 * wordsz
= (encoded
>> 18) & 0xF;
9067 * chunksz
= (encoded
>> 22) & 0xF;
9068 * lsb0_p
= (encoded
>> 27) & 1;
9069 * signed_p
= (encoded
>> 28) & 1;
9070 * trunc_p
= (encoded
>> 29) & 1;
9073 bfd_reloc_status_type
9074 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
9075 asection
*input_section
,
9077 Elf_Internal_Rela
*rel
,
9080 bfd_vma shift
, x
, mask
;
9081 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9082 bfd_reloc_status_type r
;
9083 bfd_size_type octets
;
9085 /* Perform this reloc, since it is complex.
9086 (this is not to say that it necessarily refers to a complex
9087 symbol; merely that it is a self-describing CGEN based reloc.
9088 i.e. the addend has the complete reloc information (bit start, end,
9089 word size, etc) encoded within it.). */
9091 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9092 &chunksz
, &lsb0_p
, &signed_p
,
9093 &trunc_p
, rel
->r_addend
);
9095 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9098 shift
= (start
+ 1) - len
;
9100 shift
= (8 * wordsz
) - (start
+ len
);
9102 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9103 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9106 printf ("Doing complex reloc: "
9107 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9108 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9109 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9110 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9111 oplen
, (unsigned long) x
, (unsigned long) mask
,
9112 (unsigned long) relocation
);
9117 /* Now do an overflow check. */
9118 r
= bfd_check_overflow ((signed_p
9119 ? complain_overflow_signed
9120 : complain_overflow_unsigned
),
9121 len
, 0, (8 * wordsz
),
9125 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9128 printf (" relocation: %8.8lx\n"
9129 " shifted mask: %8.8lx\n"
9130 " shifted/masked reloc: %8.8lx\n"
9131 " result: %8.8lx\n",
9132 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9133 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9135 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9139 /* Functions to read r_offset from external (target order) reloc
9140 entry. Faster than bfd_getl32 et al, because we let the compiler
9141 know the value is aligned. */
9144 ext32l_r_offset (const void *p
)
9151 const union aligned32
*a
9152 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9154 uint32_t aval
= ( (uint32_t) a
->c
[0]
9155 | (uint32_t) a
->c
[1] << 8
9156 | (uint32_t) a
->c
[2] << 16
9157 | (uint32_t) a
->c
[3] << 24);
9162 ext32b_r_offset (const void *p
)
9169 const union aligned32
*a
9170 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9172 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9173 | (uint32_t) a
->c
[1] << 16
9174 | (uint32_t) a
->c
[2] << 8
9175 | (uint32_t) a
->c
[3]);
9179 #ifdef BFD_HOST_64_BIT
9181 ext64l_r_offset (const void *p
)
9188 const union aligned64
*a
9189 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9191 uint64_t aval
= ( (uint64_t) a
->c
[0]
9192 | (uint64_t) a
->c
[1] << 8
9193 | (uint64_t) a
->c
[2] << 16
9194 | (uint64_t) a
->c
[3] << 24
9195 | (uint64_t) a
->c
[4] << 32
9196 | (uint64_t) a
->c
[5] << 40
9197 | (uint64_t) a
->c
[6] << 48
9198 | (uint64_t) a
->c
[7] << 56);
9203 ext64b_r_offset (const void *p
)
9210 const union aligned64
*a
9211 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9213 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9214 | (uint64_t) a
->c
[1] << 48
9215 | (uint64_t) a
->c
[2] << 40
9216 | (uint64_t) a
->c
[3] << 32
9217 | (uint64_t) a
->c
[4] << 24
9218 | (uint64_t) a
->c
[5] << 16
9219 | (uint64_t) a
->c
[6] << 8
9220 | (uint64_t) a
->c
[7]);
9225 /* When performing a relocatable link, the input relocations are
9226 preserved. But, if they reference global symbols, the indices
9227 referenced must be updated. Update all the relocations found in
9231 elf_link_adjust_relocs (bfd
*abfd
,
9233 struct bfd_elf_section_reloc_data
*reldata
,
9235 struct bfd_link_info
*info
)
9238 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9240 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9241 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9242 bfd_vma r_type_mask
;
9244 unsigned int count
= reldata
->count
;
9245 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9247 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9249 swap_in
= bed
->s
->swap_reloc_in
;
9250 swap_out
= bed
->s
->swap_reloc_out
;
9252 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9254 swap_in
= bed
->s
->swap_reloca_in
;
9255 swap_out
= bed
->s
->swap_reloca_out
;
9260 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9263 if (bed
->s
->arch_size
== 32)
9270 r_type_mask
= 0xffffffff;
9274 erela
= reldata
->hdr
->contents
;
9275 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9277 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9280 if (*rel_hash
== NULL
)
9283 if ((*rel_hash
)->indx
== -2
9284 && info
->gc_sections
9285 && ! info
->gc_keep_exported
)
9287 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9288 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9290 (*rel_hash
)->root
.root
.string
);
9291 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9293 bfd_set_error (bfd_error_invalid_operation
);
9296 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9298 (*swap_in
) (abfd
, erela
, irela
);
9299 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9300 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9301 | (irela
[j
].r_info
& r_type_mask
));
9302 (*swap_out
) (abfd
, irela
, erela
);
9305 if (bed
->elf_backend_update_relocs
)
9306 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9308 if (sort
&& count
!= 0)
9310 bfd_vma (*ext_r_off
) (const void *);
9313 bfd_byte
*base
, *end
, *p
, *loc
;
9314 bfd_byte
*buf
= NULL
;
9316 if (bed
->s
->arch_size
== 32)
9318 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9319 ext_r_off
= ext32l_r_offset
;
9320 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9321 ext_r_off
= ext32b_r_offset
;
9327 #ifdef BFD_HOST_64_BIT
9328 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9329 ext_r_off
= ext64l_r_offset
;
9330 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9331 ext_r_off
= ext64b_r_offset
;
9337 /* Must use a stable sort here. A modified insertion sort,
9338 since the relocs are mostly sorted already. */
9339 elt_size
= reldata
->hdr
->sh_entsize
;
9340 base
= reldata
->hdr
->contents
;
9341 end
= base
+ count
* elt_size
;
9342 if (elt_size
> sizeof (Elf64_External_Rela
))
9345 /* Ensure the first element is lowest. This acts as a sentinel,
9346 speeding the main loop below. */
9347 r_off
= (*ext_r_off
) (base
);
9348 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9350 bfd_vma r_off2
= (*ext_r_off
) (p
);
9359 /* Don't just swap *base and *loc as that changes the order
9360 of the original base[0] and base[1] if they happen to
9361 have the same r_offset. */
9362 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9363 memcpy (onebuf
, loc
, elt_size
);
9364 memmove (base
+ elt_size
, base
, loc
- base
);
9365 memcpy (base
, onebuf
, elt_size
);
9368 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9370 /* base to p is sorted, *p is next to insert. */
9371 r_off
= (*ext_r_off
) (p
);
9372 /* Search the sorted region for location to insert. */
9374 while (r_off
< (*ext_r_off
) (loc
))
9379 /* Chances are there is a run of relocs to insert here,
9380 from one of more input files. Files are not always
9381 linked in order due to the way elf_link_input_bfd is
9382 called. See pr17666. */
9383 size_t sortlen
= p
- loc
;
9384 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9385 size_t runlen
= elt_size
;
9386 size_t buf_size
= 96 * 1024;
9387 while (p
+ runlen
< end
9388 && (sortlen
<= buf_size
9389 || runlen
+ elt_size
<= buf_size
)
9390 && r_off2
> (*ext_r_off
) (p
+ runlen
))
9394 buf
= bfd_malloc (buf_size
);
9398 if (runlen
< sortlen
)
9400 memcpy (buf
, p
, runlen
);
9401 memmove (loc
+ runlen
, loc
, sortlen
);
9402 memcpy (loc
, buf
, runlen
);
9406 memcpy (buf
, loc
, sortlen
);
9407 memmove (loc
, p
, runlen
);
9408 memcpy (loc
+ runlen
, buf
, sortlen
);
9410 p
+= runlen
- elt_size
;
9413 /* Hashes are no longer valid. */
9414 free (reldata
->hashes
);
9415 reldata
->hashes
= NULL
;
9421 struct elf_link_sort_rela
9427 enum elf_reloc_type_class type
;
9428 /* We use this as an array of size int_rels_per_ext_rel. */
9429 Elf_Internal_Rela rela
[1];
9432 /* qsort stability here and for cmp2 is only an issue if multiple
9433 dynamic relocations are emitted at the same address. But targets
9434 that apply a series of dynamic relocations each operating on the
9435 result of the prior relocation can't use -z combreloc as
9436 implemented anyway. Such schemes tend to be broken by sorting on
9437 symbol index. That leaves dynamic NONE relocs as the only other
9438 case where ld might emit multiple relocs at the same address, and
9439 those are only emitted due to target bugs. */
9442 elf_link_sort_cmp1 (const void *A
, const void *B
)
9444 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9445 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9446 int relativea
, relativeb
;
9448 relativea
= a
->type
== reloc_class_relative
;
9449 relativeb
= b
->type
== reloc_class_relative
;
9451 if (relativea
< relativeb
)
9453 if (relativea
> relativeb
)
9455 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9457 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9459 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9461 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9467 elf_link_sort_cmp2 (const void *A
, const void *B
)
9469 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9470 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9472 if (a
->type
< b
->type
)
9474 if (a
->type
> b
->type
)
9476 if (a
->u
.offset
< b
->u
.offset
)
9478 if (a
->u
.offset
> b
->u
.offset
)
9480 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9482 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9488 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9490 asection
*dynamic_relocs
;
9493 bfd_size_type count
, size
;
9494 size_t i
, ret
, sort_elt
, ext_size
;
9495 bfd_byte
*sort
, *s_non_relative
, *p
;
9496 struct elf_link_sort_rela
*sq
;
9497 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9498 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9499 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9500 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9501 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9502 struct bfd_link_order
*lo
;
9506 /* Find a dynamic reloc section. */
9507 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9508 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9509 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9510 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9512 bool use_rela_initialised
= false;
9514 /* This is just here to stop gcc from complaining.
9515 Its initialization checking code is not perfect. */
9518 /* Both sections are present. Examine the sizes
9519 of the indirect sections to help us choose. */
9520 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9521 if (lo
->type
== bfd_indirect_link_order
)
9523 asection
*o
= lo
->u
.indirect
.section
;
9525 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9527 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9528 /* Section size is divisible by both rel and rela sizes.
9529 It is of no help to us. */
9533 /* Section size is only divisible by rela. */
9534 if (use_rela_initialised
&& !use_rela
)
9536 _bfd_error_handler (_("%pB: unable to sort relocs - "
9537 "they are in more than one size"),
9539 bfd_set_error (bfd_error_invalid_operation
);
9545 use_rela_initialised
= true;
9549 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9551 /* Section size is only divisible by rel. */
9552 if (use_rela_initialised
&& use_rela
)
9554 _bfd_error_handler (_("%pB: unable to sort relocs - "
9555 "they are in more than one size"),
9557 bfd_set_error (bfd_error_invalid_operation
);
9563 use_rela_initialised
= true;
9568 /* The section size is not divisible by either -
9569 something is wrong. */
9570 _bfd_error_handler (_("%pB: unable to sort relocs - "
9571 "they are of an unknown size"), abfd
);
9572 bfd_set_error (bfd_error_invalid_operation
);
9577 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9578 if (lo
->type
== bfd_indirect_link_order
)
9580 asection
*o
= lo
->u
.indirect
.section
;
9582 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9584 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9585 /* Section size is divisible by both rel and rela sizes.
9586 It is of no help to us. */
9590 /* Section size is only divisible by rela. */
9591 if (use_rela_initialised
&& !use_rela
)
9593 _bfd_error_handler (_("%pB: unable to sort relocs - "
9594 "they are in more than one size"),
9596 bfd_set_error (bfd_error_invalid_operation
);
9602 use_rela_initialised
= true;
9606 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9608 /* Section size is only divisible by rel. */
9609 if (use_rela_initialised
&& use_rela
)
9611 _bfd_error_handler (_("%pB: unable to sort relocs - "
9612 "they are in more than one size"),
9614 bfd_set_error (bfd_error_invalid_operation
);
9620 use_rela_initialised
= true;
9625 /* The section size is not divisible by either -
9626 something is wrong. */
9627 _bfd_error_handler (_("%pB: unable to sort relocs - "
9628 "they are of an unknown size"), abfd
);
9629 bfd_set_error (bfd_error_invalid_operation
);
9634 if (! use_rela_initialised
)
9638 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9640 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9647 dynamic_relocs
= rela_dyn
;
9648 ext_size
= bed
->s
->sizeof_rela
;
9649 swap_in
= bed
->s
->swap_reloca_in
;
9650 swap_out
= bed
->s
->swap_reloca_out
;
9654 dynamic_relocs
= rel_dyn
;
9655 ext_size
= bed
->s
->sizeof_rel
;
9656 swap_in
= bed
->s
->swap_reloc_in
;
9657 swap_out
= bed
->s
->swap_reloc_out
;
9661 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9662 if (lo
->type
== bfd_indirect_link_order
)
9663 size
+= lo
->u
.indirect
.section
->size
;
9665 if (size
!= dynamic_relocs
->size
)
9668 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9669 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9671 count
= dynamic_relocs
->size
/ ext_size
;
9674 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9678 (*info
->callbacks
->warning
)
9679 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9683 if (bed
->s
->arch_size
== 32)
9684 r_sym_mask
= ~(bfd_vma
) 0xff;
9686 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9688 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9689 if (lo
->type
== bfd_indirect_link_order
)
9691 bfd_byte
*erel
, *erelend
;
9692 asection
*o
= lo
->u
.indirect
.section
;
9694 if (o
->contents
== NULL
&& o
->size
!= 0)
9696 /* This is a reloc section that is being handled as a normal
9697 section. See bfd_section_from_shdr. We can't combine
9698 relocs in this case. */
9703 erelend
= o
->contents
+ o
->size
;
9704 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9706 while (erel
< erelend
)
9708 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9710 (*swap_in
) (abfd
, erel
, s
->rela
);
9711 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9712 s
->u
.sym_mask
= r_sym_mask
;
9718 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9720 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9722 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9723 if (s
->type
!= reloc_class_relative
)
9729 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9730 for (; i
< count
; i
++, p
+= sort_elt
)
9732 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9733 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9735 sp
->u
.offset
= sq
->rela
->r_offset
;
9738 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9740 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9741 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9743 /* We have plt relocs in .rela.dyn. */
9744 sq
= (struct elf_link_sort_rela
*) sort
;
9745 for (i
= 0; i
< count
; i
++)
9746 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9748 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9750 struct bfd_link_order
**plo
;
9751 /* Put srelplt link_order last. This is so the output_offset
9752 set in the next loop is correct for DT_JMPREL. */
9753 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9754 if ((*plo
)->type
== bfd_indirect_link_order
9755 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9761 plo
= &(*plo
)->next
;
9764 dynamic_relocs
->map_tail
.link_order
= lo
;
9769 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9770 if (lo
->type
== bfd_indirect_link_order
)
9772 bfd_byte
*erel
, *erelend
;
9773 asection
*o
= lo
->u
.indirect
.section
;
9776 erelend
= o
->contents
+ o
->size
;
9777 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9778 while (erel
< erelend
)
9780 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9781 (*swap_out
) (abfd
, s
->rela
, erel
);
9788 *psec
= dynamic_relocs
;
9792 /* Add a symbol to the output symbol string table. */
9795 elf_link_output_symstrtab (void *finf
,
9797 Elf_Internal_Sym
*elfsym
,
9798 asection
*input_sec
,
9799 struct elf_link_hash_entry
*h
)
9801 struct elf_final_link_info
*flinfo
= finf
;
9802 int (*output_symbol_hook
)
9803 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9804 struct elf_link_hash_entry
*);
9805 struct elf_link_hash_table
*hash_table
;
9806 const struct elf_backend_data
*bed
;
9807 bfd_size_type strtabsize
;
9809 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9811 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9812 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9813 if (output_symbol_hook
!= NULL
)
9815 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9820 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
9821 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
9822 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
9823 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
9827 || (input_sec
->flags
& SEC_EXCLUDE
))
9828 elfsym
->st_name
= (unsigned long) -1;
9831 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9832 to get the final offset for st_name. */
9833 char *versioned_name
= (char *) name
;
9836 if (h
->versioned
== versioned
&& h
->def_dynamic
)
9838 /* Keep only one '@' for versioned symbols defined in
9840 char *version
= strrchr (name
, ELF_VER_CHR
);
9841 char *base_end
= strchr (name
, ELF_VER_CHR
);
9842 if (version
!= base_end
)
9845 size_t len
= strlen (name
);
9846 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
9847 if (versioned_name
== NULL
)
9849 base_len
= base_end
- name
;
9850 memcpy (versioned_name
, name
, base_len
);
9851 memcpy (versioned_name
+ base_len
, version
,
9856 else if (flinfo
->info
->unique_symbol
9857 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
9859 struct local_hash_entry
*lh
;
9863 switch (ELF_ST_TYPE (elfsym
->st_info
))
9869 lh
= (struct local_hash_entry
*) bfd_hash_lookup
9870 (&flinfo
->local_hash_table
, name
, true, false);
9873 /* Always append ".COUNT" to local symbols to avoid
9874 potential conflicts with local symbol "XXX.COUNT". */
9875 sprintf (buf
, "%lx", lh
->count
);
9876 base_len
= lh
->size
;
9879 base_len
= strlen (name
);
9880 lh
->size
= base_len
;
9882 count_len
= strlen (buf
);
9883 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
9884 base_len
+ count_len
+ 2);
9885 if (versioned_name
== NULL
)
9887 memcpy (versioned_name
, name
, base_len
);
9888 versioned_name
[base_len
] = '.';
9889 memcpy (versioned_name
+ base_len
+ 1, buf
,
9896 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9897 versioned_name
, false);
9898 if (elfsym
->st_name
== (unsigned long) -1)
9902 hash_table
= elf_hash_table (flinfo
->info
);
9903 strtabsize
= hash_table
->strtabsize
;
9904 if (strtabsize
<= hash_table
->strtabcount
)
9906 strtabsize
+= strtabsize
;
9907 hash_table
->strtabsize
= strtabsize
;
9908 strtabsize
*= sizeof (*hash_table
->strtab
);
9910 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9912 if (hash_table
->strtab
== NULL
)
9915 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9916 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9917 = hash_table
->strtabcount
;
9918 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9919 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9921 flinfo
->output_bfd
->symcount
+= 1;
9922 hash_table
->strtabcount
+= 1;
9927 /* Swap symbols out to the symbol table and flush the output symbols to
9931 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9933 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9936 const struct elf_backend_data
*bed
;
9938 Elf_Internal_Shdr
*hdr
;
9942 if (!hash_table
->strtabcount
)
9945 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9947 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9949 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9950 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9954 if (flinfo
->symshndxbuf
)
9956 amt
= sizeof (Elf_External_Sym_Shndx
);
9957 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9958 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9959 if (flinfo
->symshndxbuf
== NULL
)
9966 /* Now swap out the symbols. */
9967 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9969 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9970 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9971 elfsym
->sym
.st_name
= 0;
9974 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9975 elfsym
->sym
.st_name
);
9977 /* Inform the linker of the addition of this symbol. */
9979 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
9980 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
9983 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9984 ((bfd_byte
*) symbuf
9985 + (elfsym
->dest_index
9986 * bed
->s
->sizeof_sym
)),
9987 (flinfo
->symshndxbuf
9988 + elfsym
->destshndx_index
));
9991 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9992 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9993 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9994 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9995 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9997 hdr
->sh_size
+= amt
;
10005 free (hash_table
->strtab
);
10006 hash_table
->strtab
= NULL
;
10011 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10014 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
10016 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
10017 && sym
->st_shndx
< SHN_LORESERVE
)
10019 /* The gABI doesn't support dynamic symbols in output sections
10022 /* xgettext:c-format */
10023 (_("%pB: too many sections: %d (>= %d)"),
10024 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
10025 bfd_set_error (bfd_error_nonrepresentable_section
);
10031 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10032 allowing an unsatisfied unversioned symbol in the DSO to match a
10033 versioned symbol that would normally require an explicit version.
10034 We also handle the case that a DSO references a hidden symbol
10035 which may be satisfied by a versioned symbol in another DSO. */
10038 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
10039 const struct elf_backend_data
*bed
,
10040 struct elf_link_hash_entry
*h
)
10043 struct elf_link_loaded_list
*loaded
;
10045 if (!is_elf_hash_table (info
->hash
))
10048 /* Check indirect symbol. */
10049 while (h
->root
.type
== bfd_link_hash_indirect
)
10050 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10052 switch (h
->root
.type
)
10058 case bfd_link_hash_undefined
:
10059 case bfd_link_hash_undefweak
:
10060 abfd
= h
->root
.u
.undef
.abfd
;
10062 || (abfd
->flags
& DYNAMIC
) == 0
10063 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
10067 case bfd_link_hash_defined
:
10068 case bfd_link_hash_defweak
:
10069 abfd
= h
->root
.u
.def
.section
->owner
;
10072 case bfd_link_hash_common
:
10073 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10076 BFD_ASSERT (abfd
!= NULL
);
10078 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10080 loaded
= loaded
->next
)
10083 Elf_Internal_Shdr
*hdr
;
10085 size_t extsymcount
;
10087 Elf_Internal_Shdr
*versymhdr
;
10088 Elf_Internal_Sym
*isym
;
10089 Elf_Internal_Sym
*isymend
;
10090 Elf_Internal_Sym
*isymbuf
;
10091 Elf_External_Versym
*ever
;
10092 Elf_External_Versym
*extversym
;
10094 input
= loaded
->abfd
;
10096 /* We check each DSO for a possible hidden versioned definition. */
10098 || elf_dynversym (input
) == 0)
10101 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10103 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10104 if (elf_bad_symtab (input
))
10106 extsymcount
= symcount
;
10111 extsymcount
= symcount
- hdr
->sh_info
;
10112 extsymoff
= hdr
->sh_info
;
10115 if (extsymcount
== 0)
10118 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10120 if (isymbuf
== NULL
)
10123 /* Read in any version definitions. */
10124 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10125 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10126 || (extversym
= (Elf_External_Versym
*)
10127 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10128 versymhdr
->sh_size
)) == NULL
)
10134 ever
= extversym
+ extsymoff
;
10135 isymend
= isymbuf
+ extsymcount
;
10136 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10139 Elf_Internal_Versym iver
;
10140 unsigned short version_index
;
10142 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10143 || isym
->st_shndx
== SHN_UNDEF
)
10146 name
= bfd_elf_string_from_elf_section (input
,
10149 if (strcmp (name
, h
->root
.root
.string
) != 0)
10152 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10154 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10155 && !(h
->def_regular
10156 && h
->forced_local
))
10158 /* If we have a non-hidden versioned sym, then it should
10159 have provided a definition for the undefined sym unless
10160 it is defined in a non-shared object and forced local.
10165 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10166 if (version_index
== 1 || version_index
== 2)
10168 /* This is the base or first version. We can use it. */
10182 /* Convert ELF common symbol TYPE. */
10185 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10187 /* Commom symbol can only appear in relocatable link. */
10188 if (!bfd_link_relocatable (info
))
10190 switch (info
->elf_stt_common
)
10194 case elf_stt_common
:
10197 case no_elf_stt_common
:
10204 /* Add an external symbol to the symbol table. This is called from
10205 the hash table traversal routine. When generating a shared object,
10206 we go through the symbol table twice. The first time we output
10207 anything that might have been forced to local scope in a version
10208 script. The second time we output the symbols that are still
10212 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10214 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10215 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10216 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10218 Elf_Internal_Sym sym
;
10219 asection
*input_sec
;
10220 const struct elf_backend_data
*bed
;
10225 if (h
->root
.type
== bfd_link_hash_warning
)
10227 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10228 if (h
->root
.type
== bfd_link_hash_new
)
10232 /* Decide whether to output this symbol in this pass. */
10233 if (eoinfo
->localsyms
)
10235 if (!h
->forced_local
)
10240 if (h
->forced_local
)
10244 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10246 if (h
->root
.type
== bfd_link_hash_undefined
)
10248 /* If we have an undefined symbol reference here then it must have
10249 come from a shared library that is being linked in. (Undefined
10250 references in regular files have already been handled unless
10251 they are in unreferenced sections which are removed by garbage
10253 bool ignore_undef
= false;
10255 /* Some symbols may be special in that the fact that they're
10256 undefined can be safely ignored - let backend determine that. */
10257 if (bed
->elf_backend_ignore_undef_symbol
)
10258 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10260 /* If we are reporting errors for this situation then do so now. */
10262 && h
->ref_dynamic_nonweak
10263 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10264 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10265 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10267 flinfo
->info
->callbacks
->undefined_symbol
10268 (flinfo
->info
, h
->root
.root
.string
,
10269 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10270 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10271 && !flinfo
->info
->warn_unresolved_syms
);
10274 /* Strip a global symbol defined in a discarded section. */
10279 /* We should also warn if a forced local symbol is referenced from
10280 shared libraries. */
10281 if (bfd_link_executable (flinfo
->info
)
10286 && h
->ref_dynamic_nonweak
10287 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10291 struct elf_link_hash_entry
*hi
= h
;
10293 /* Check indirect symbol. */
10294 while (hi
->root
.type
== bfd_link_hash_indirect
)
10295 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10297 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10298 /* xgettext:c-format */
10299 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10300 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10301 /* xgettext:c-format */
10302 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10304 /* xgettext:c-format */
10305 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10306 def_bfd
= flinfo
->output_bfd
;
10307 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10308 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10309 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10310 h
->root
.root
.string
, def_bfd
);
10311 bfd_set_error (bfd_error_bad_value
);
10312 eoinfo
->failed
= true;
10316 /* We don't want to output symbols that have never been mentioned by
10317 a regular file, or that we have been told to strip. However, if
10318 h->indx is set to -2, the symbol is used by a reloc and we must
10323 else if ((h
->def_dynamic
10325 || h
->root
.type
== bfd_link_hash_new
)
10327 && !h
->ref_regular
)
10329 else if (flinfo
->info
->strip
== strip_all
)
10331 else if (flinfo
->info
->strip
== strip_some
10332 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10333 h
->root
.root
.string
, false, false) == NULL
)
10335 else if ((h
->root
.type
== bfd_link_hash_defined
10336 || h
->root
.type
== bfd_link_hash_defweak
)
10337 && ((flinfo
->info
->strip_discarded
10338 && discarded_section (h
->root
.u
.def
.section
))
10339 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10340 && h
->root
.u
.def
.section
->owner
!= NULL
10341 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10343 else if ((h
->root
.type
== bfd_link_hash_undefined
10344 || h
->root
.type
== bfd_link_hash_undefweak
)
10345 && h
->root
.u
.undef
.abfd
!= NULL
10346 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10351 /* If we're stripping it, and it's not a dynamic symbol, there's
10352 nothing else to do. However, if it is a forced local symbol or
10353 an ifunc symbol we need to give the backend finish_dynamic_symbol
10354 function a chance to make it dynamic. */
10356 && h
->dynindx
== -1
10357 && type
!= STT_GNU_IFUNC
10358 && !h
->forced_local
)
10362 sym
.st_size
= h
->size
;
10363 sym
.st_other
= h
->other
;
10364 switch (h
->root
.type
)
10367 case bfd_link_hash_new
:
10368 case bfd_link_hash_warning
:
10372 case bfd_link_hash_undefined
:
10373 case bfd_link_hash_undefweak
:
10374 input_sec
= bfd_und_section_ptr
;
10375 sym
.st_shndx
= SHN_UNDEF
;
10378 case bfd_link_hash_defined
:
10379 case bfd_link_hash_defweak
:
10381 input_sec
= h
->root
.u
.def
.section
;
10382 if (input_sec
->output_section
!= NULL
)
10385 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10386 input_sec
->output_section
);
10387 if (sym
.st_shndx
== SHN_BAD
)
10390 /* xgettext:c-format */
10391 (_("%pB: could not find output section %pA for input section %pA"),
10392 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10393 bfd_set_error (bfd_error_nonrepresentable_section
);
10394 eoinfo
->failed
= true;
10398 /* ELF symbols in relocatable files are section relative,
10399 but in nonrelocatable files they are virtual
10401 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10402 if (!bfd_link_relocatable (flinfo
->info
))
10404 sym
.st_value
+= input_sec
->output_section
->vma
;
10405 if (h
->type
== STT_TLS
)
10407 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10408 if (tls_sec
!= NULL
)
10409 sym
.st_value
-= tls_sec
->vma
;
10415 BFD_ASSERT (input_sec
->owner
== NULL
10416 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10417 sym
.st_shndx
= SHN_UNDEF
;
10418 input_sec
= bfd_und_section_ptr
;
10423 case bfd_link_hash_common
:
10424 input_sec
= h
->root
.u
.c
.p
->section
;
10425 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10426 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10429 case bfd_link_hash_indirect
:
10430 /* These symbols are created by symbol versioning. They point
10431 to the decorated version of the name. For example, if the
10432 symbol foo@@GNU_1.2 is the default, which should be used when
10433 foo is used with no version, then we add an indirect symbol
10434 foo which points to foo@@GNU_1.2. We ignore these symbols,
10435 since the indirected symbol is already in the hash table. */
10439 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10440 switch (h
->root
.type
)
10442 case bfd_link_hash_common
:
10443 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10445 case bfd_link_hash_defined
:
10446 case bfd_link_hash_defweak
:
10447 if (bed
->common_definition (&sym
))
10448 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10452 case bfd_link_hash_undefined
:
10453 case bfd_link_hash_undefweak
:
10459 if (h
->forced_local
)
10461 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10462 /* Turn off visibility on local symbol. */
10463 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10465 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10466 else if (h
->unique_global
&& h
->def_regular
)
10467 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10468 else if (h
->root
.type
== bfd_link_hash_undefweak
10469 || h
->root
.type
== bfd_link_hash_defweak
)
10470 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10472 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10473 sym
.st_target_internal
= h
->target_internal
;
10475 /* Give the processor backend a chance to tweak the symbol value,
10476 and also to finish up anything that needs to be done for this
10477 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10478 forced local syms when non-shared is due to a historical quirk.
10479 STT_GNU_IFUNC symbol must go through PLT. */
10480 if ((h
->type
== STT_GNU_IFUNC
10482 && !bfd_link_relocatable (flinfo
->info
))
10483 || ((h
->dynindx
!= -1
10484 || h
->forced_local
)
10485 && ((bfd_link_pic (flinfo
->info
)
10486 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10487 || h
->root
.type
!= bfd_link_hash_undefweak
))
10488 || !h
->forced_local
)
10489 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10491 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10492 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10494 eoinfo
->failed
= true;
10499 /* If we are marking the symbol as undefined, and there are no
10500 non-weak references to this symbol from a regular object, then
10501 mark the symbol as weak undefined; if there are non-weak
10502 references, mark the symbol as strong. We can't do this earlier,
10503 because it might not be marked as undefined until the
10504 finish_dynamic_symbol routine gets through with it. */
10505 if (sym
.st_shndx
== SHN_UNDEF
10507 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10508 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10511 type
= ELF_ST_TYPE (sym
.st_info
);
10513 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10514 if (type
== STT_GNU_IFUNC
)
10517 if (h
->ref_regular_nonweak
)
10518 bindtype
= STB_GLOBAL
;
10520 bindtype
= STB_WEAK
;
10521 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10524 /* If this is a symbol defined in a dynamic library, don't use the
10525 symbol size from the dynamic library. Relinking an executable
10526 against a new library may introduce gratuitous changes in the
10527 executable's symbols if we keep the size. */
10528 if (sym
.st_shndx
== SHN_UNDEF
10533 /* If a non-weak symbol with non-default visibility is not defined
10534 locally, it is a fatal error. */
10535 if (!bfd_link_relocatable (flinfo
->info
)
10536 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10537 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10538 && h
->root
.type
== bfd_link_hash_undefined
10539 && !h
->def_regular
)
10543 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10544 /* xgettext:c-format */
10545 msg
= _("%pB: protected symbol `%s' isn't defined");
10546 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10547 /* xgettext:c-format */
10548 msg
= _("%pB: internal symbol `%s' isn't defined");
10550 /* xgettext:c-format */
10551 msg
= _("%pB: hidden symbol `%s' isn't defined");
10552 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10553 bfd_set_error (bfd_error_bad_value
);
10554 eoinfo
->failed
= true;
10558 /* If this symbol should be put in the .dynsym section, then put it
10559 there now. We already know the symbol index. We also fill in
10560 the entry in the .hash section. */
10561 if (h
->dynindx
!= -1
10562 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10563 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10564 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10568 /* Since there is no version information in the dynamic string,
10569 if there is no version info in symbol version section, we will
10570 have a run-time problem if not linking executable, referenced
10571 by shared library, or not bound locally. */
10572 if (h
->verinfo
.verdef
== NULL
10573 && (!bfd_link_executable (flinfo
->info
)
10575 || !h
->def_regular
))
10577 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10579 if (p
&& p
[1] != '\0')
10582 /* xgettext:c-format */
10583 (_("%pB: no symbol version section for versioned symbol `%s'"),
10584 flinfo
->output_bfd
, h
->root
.root
.string
);
10585 eoinfo
->failed
= true;
10590 sym
.st_name
= h
->dynstr_index
;
10591 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10592 + h
->dynindx
* bed
->s
->sizeof_sym
);
10593 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10595 eoinfo
->failed
= true;
10599 /* Inform the linker of the addition of this symbol. */
10601 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
10602 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
10604 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10606 if (flinfo
->hash_sec
!= NULL
)
10608 size_t hash_entry_size
;
10609 bfd_byte
*bucketpos
;
10611 size_t bucketcount
;
10614 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10615 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10618 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10619 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10620 + (bucket
+ 2) * hash_entry_size
);
10621 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10622 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10624 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10625 ((bfd_byte
*) flinfo
->hash_sec
->contents
10626 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10629 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10631 Elf_Internal_Versym iversym
;
10632 Elf_External_Versym
*eversym
;
10634 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10636 if (h
->verinfo
.verdef
== NULL
10637 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10638 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10639 iversym
.vs_vers
= 1;
10641 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10645 if (h
->verinfo
.vertree
== NULL
)
10646 iversym
.vs_vers
= 1;
10648 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10649 if (flinfo
->info
->create_default_symver
)
10653 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10654 defined locally. */
10655 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10656 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10658 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10659 eversym
+= h
->dynindx
;
10660 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10664 /* If the symbol is undefined, and we didn't output it to .dynsym,
10665 strip it from .symtab too. Obviously we can't do this for
10666 relocatable output or when needed for --emit-relocs. */
10667 else if (input_sec
== bfd_und_section_ptr
10669 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10670 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10671 && !bfd_link_relocatable (flinfo
->info
))
10674 /* Also strip others that we couldn't earlier due to dynamic symbol
10678 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10681 /* Output a FILE symbol so that following locals are not associated
10682 with the wrong input file. We need one for forced local symbols
10683 if we've seen more than one FILE symbol or when we have exactly
10684 one FILE symbol but global symbols are present in a file other
10685 than the one with the FILE symbol. We also need one if linker
10686 defined symbols are present. In practice these conditions are
10687 always met, so just emit the FILE symbol unconditionally. */
10688 if (eoinfo
->localsyms
10689 && !eoinfo
->file_sym_done
10690 && eoinfo
->flinfo
->filesym_count
!= 0)
10692 Elf_Internal_Sym fsym
;
10694 memset (&fsym
, 0, sizeof (fsym
));
10695 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10696 fsym
.st_shndx
= SHN_ABS
;
10697 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10698 bfd_und_section_ptr
, NULL
))
10701 eoinfo
->file_sym_done
= true;
10704 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10705 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10709 eoinfo
->failed
= true;
10714 else if (h
->indx
== -2)
10720 /* Return TRUE if special handling is done for relocs in SEC against
10721 symbols defined in discarded sections. */
10724 elf_section_ignore_discarded_relocs (asection
*sec
)
10726 const struct elf_backend_data
*bed
;
10728 switch (sec
->sec_info_type
)
10730 case SEC_INFO_TYPE_STABS
:
10731 case SEC_INFO_TYPE_EH_FRAME
:
10732 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10738 bed
= get_elf_backend_data (sec
->owner
);
10739 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10740 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10746 /* Return a mask saying how ld should treat relocations in SEC against
10747 symbols defined in discarded sections. If this function returns
10748 COMPLAIN set, ld will issue a warning message. If this function
10749 returns PRETEND set, and the discarded section was link-once and the
10750 same size as the kept link-once section, ld will pretend that the
10751 symbol was actually defined in the kept section. Otherwise ld will
10752 zero the reloc (at least that is the intent, but some cooperation by
10753 the target dependent code is needed, particularly for REL targets). */
10756 _bfd_elf_default_action_discarded (asection
*sec
)
10758 if (sec
->flags
& SEC_DEBUGGING
)
10761 if (strcmp (".eh_frame", sec
->name
) == 0)
10764 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10767 return COMPLAIN
| PRETEND
;
10770 /* Find a match between a section and a member of a section group. */
10773 match_group_member (asection
*sec
, asection
*group
,
10774 struct bfd_link_info
*info
)
10776 asection
*first
= elf_next_in_group (group
);
10777 asection
*s
= first
;
10781 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10784 s
= elf_next_in_group (s
);
10792 /* Check if the kept section of a discarded section SEC can be used
10793 to replace it. Return the replacement if it is OK. Otherwise return
10797 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10801 kept
= sec
->kept_section
;
10804 if ((kept
->flags
& SEC_GROUP
) != 0)
10805 kept
= match_group_member (sec
, kept
, info
);
10808 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10809 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
10813 /* Get the real kept section. */
10815 for (next
= kept
->kept_section
;
10817 next
= next
->kept_section
)
10821 sec
->kept_section
= kept
;
10826 /* Link an input file into the linker output file. This function
10827 handles all the sections and relocations of the input file at once.
10828 This is so that we only have to read the local symbols once, and
10829 don't have to keep them in memory. */
10832 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10834 int (*relocate_section
)
10835 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10836 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10838 Elf_Internal_Shdr
*symtab_hdr
;
10839 size_t locsymcount
;
10841 Elf_Internal_Sym
*isymbuf
;
10842 Elf_Internal_Sym
*isym
;
10843 Elf_Internal_Sym
*isymend
;
10845 asection
**ppsection
;
10847 const struct elf_backend_data
*bed
;
10848 struct elf_link_hash_entry
**sym_hashes
;
10849 bfd_size_type address_size
;
10850 bfd_vma r_type_mask
;
10852 bool have_file_sym
= false;
10854 output_bfd
= flinfo
->output_bfd
;
10855 bed
= get_elf_backend_data (output_bfd
);
10856 relocate_section
= bed
->elf_backend_relocate_section
;
10858 /* If this is a dynamic object, we don't want to do anything here:
10859 we don't want the local symbols, and we don't want the section
10861 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10864 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10865 if (elf_bad_symtab (input_bfd
))
10867 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10872 locsymcount
= symtab_hdr
->sh_info
;
10873 extsymoff
= symtab_hdr
->sh_info
;
10876 /* Enable GNU OSABI features in the output BFD that are used in the input
10878 if (bed
->elf_osabi
== ELFOSABI_NONE
10879 || bed
->elf_osabi
== ELFOSABI_GNU
10880 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
10881 elf_tdata (output_bfd
)->has_gnu_osabi
10882 |= (elf_tdata (input_bfd
)->has_gnu_osabi
10883 & (bfd_link_relocatable (flinfo
->info
)
10884 ? -1 : ~elf_gnu_osabi_retain
));
10886 /* Read the local symbols. */
10887 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10888 if (isymbuf
== NULL
&& locsymcount
!= 0)
10890 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10891 flinfo
->internal_syms
,
10892 flinfo
->external_syms
,
10893 flinfo
->locsym_shndx
);
10894 if (isymbuf
== NULL
)
10898 /* Find local symbol sections and adjust values of symbols in
10899 SEC_MERGE sections. Write out those local symbols we know are
10900 going into the output file. */
10901 isymend
= isymbuf
+ locsymcount
;
10902 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10904 isym
++, pindex
++, ppsection
++)
10908 Elf_Internal_Sym osym
;
10914 if (elf_bad_symtab (input_bfd
))
10916 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10923 if (isym
->st_shndx
== SHN_UNDEF
)
10924 isec
= bfd_und_section_ptr
;
10925 else if (isym
->st_shndx
== SHN_ABS
)
10926 isec
= bfd_abs_section_ptr
;
10927 else if (isym
->st_shndx
== SHN_COMMON
)
10928 isec
= bfd_com_section_ptr
;
10931 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10934 /* Don't attempt to output symbols with st_shnx in the
10935 reserved range other than SHN_ABS and SHN_COMMON. */
10936 isec
= bfd_und_section_ptr
;
10938 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10939 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10941 _bfd_merged_section_offset (output_bfd
, &isec
,
10942 elf_section_data (isec
)->sec_info
,
10948 /* Don't output the first, undefined, symbol. In fact, don't
10949 output any undefined local symbol. */
10950 if (isec
== bfd_und_section_ptr
)
10953 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10955 /* We never output section symbols. Instead, we use the
10956 section symbol of the corresponding section in the output
10961 /* If we are stripping all symbols, we don't want to output this
10963 if (flinfo
->info
->strip
== strip_all
)
10966 /* If we are discarding all local symbols, we don't want to
10967 output this one. If we are generating a relocatable output
10968 file, then some of the local symbols may be required by
10969 relocs; we output them below as we discover that they are
10971 if (flinfo
->info
->discard
== discard_all
)
10974 /* If this symbol is defined in a section which we are
10975 discarding, we don't need to keep it. */
10976 if (isym
->st_shndx
!= SHN_UNDEF
10977 && isym
->st_shndx
< SHN_LORESERVE
10978 && isec
->output_section
== NULL
10979 && flinfo
->info
->non_contiguous_regions
10980 && flinfo
->info
->non_contiguous_regions_warnings
)
10982 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10983 "discards section `%s' from '%s'\n"),
10984 isec
->name
, bfd_get_filename (isec
->owner
));
10988 if (isym
->st_shndx
!= SHN_UNDEF
10989 && isym
->st_shndx
< SHN_LORESERVE
10990 && bfd_section_removed_from_list (output_bfd
,
10991 isec
->output_section
))
10994 /* Get the name of the symbol. */
10995 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
11000 /* See if we are discarding symbols with this name. */
11001 if ((flinfo
->info
->strip
== strip_some
11002 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, false, false)
11004 || (((flinfo
->info
->discard
== discard_sec_merge
11005 && (isec
->flags
& SEC_MERGE
)
11006 && !bfd_link_relocatable (flinfo
->info
))
11007 || flinfo
->info
->discard
== discard_l
)
11008 && bfd_is_local_label_name (input_bfd
, name
)))
11011 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
11013 if (input_bfd
->lto_output
)
11014 /* -flto puts a temp file name here. This means builds
11015 are not reproducible. Discard the symbol. */
11017 have_file_sym
= true;
11018 flinfo
->filesym_count
+= 1;
11020 if (!have_file_sym
)
11022 /* In the absence of debug info, bfd_find_nearest_line uses
11023 FILE symbols to determine the source file for local
11024 function symbols. Provide a FILE symbol here if input
11025 files lack such, so that their symbols won't be
11026 associated with a previous input file. It's not the
11027 source file, but the best we can do. */
11028 const char *filename
;
11029 have_file_sym
= true;
11030 flinfo
->filesym_count
+= 1;
11031 memset (&osym
, 0, sizeof (osym
));
11032 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11033 osym
.st_shndx
= SHN_ABS
;
11034 if (input_bfd
->lto_output
)
11037 filename
= lbasename (bfd_get_filename (input_bfd
));
11038 if (!elf_link_output_symstrtab (flinfo
, filename
, &osym
,
11039 bfd_abs_section_ptr
, NULL
))
11045 /* Adjust the section index for the output file. */
11046 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11047 isec
->output_section
);
11048 if (osym
.st_shndx
== SHN_BAD
)
11051 /* ELF symbols in relocatable files are section relative, but
11052 in executable files they are virtual addresses. Note that
11053 this code assumes that all ELF sections have an associated
11054 BFD section with a reasonable value for output_offset; below
11055 we assume that they also have a reasonable value for
11056 output_section. Any special sections must be set up to meet
11057 these requirements. */
11058 osym
.st_value
+= isec
->output_offset
;
11059 if (!bfd_link_relocatable (flinfo
->info
))
11061 osym
.st_value
+= isec
->output_section
->vma
;
11062 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
11064 /* STT_TLS symbols are relative to PT_TLS segment base. */
11065 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
11066 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
11068 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
11073 indx
= bfd_get_symcount (output_bfd
);
11074 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11081 if (bed
->s
->arch_size
== 32)
11083 r_type_mask
= 0xff;
11089 r_type_mask
= 0xffffffff;
11094 /* Relocate the contents of each section. */
11095 sym_hashes
= elf_sym_hashes (input_bfd
);
11096 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11098 bfd_byte
*contents
;
11100 if (! o
->linker_mark
)
11102 /* This section was omitted from the link. */
11106 if (!flinfo
->info
->resolve_section_groups
11107 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11109 /* Deal with the group signature symbol. */
11110 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11111 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11112 asection
*osec
= o
->output_section
;
11114 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11115 if (symndx
>= locsymcount
11116 || (elf_bad_symtab (input_bfd
)
11117 && flinfo
->sections
[symndx
] == NULL
))
11119 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
11120 while (h
->root
.type
== bfd_link_hash_indirect
11121 || h
->root
.type
== bfd_link_hash_warning
)
11122 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11123 /* Arrange for symbol to be output. */
11125 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11127 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11129 /* We'll use the output section target_index. */
11130 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11131 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11135 if (flinfo
->indices
[symndx
] == -1)
11137 /* Otherwise output the local symbol now. */
11138 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11139 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11144 name
= bfd_elf_string_from_elf_section (input_bfd
,
11145 symtab_hdr
->sh_link
,
11150 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11152 if (sym
.st_shndx
== SHN_BAD
)
11155 sym
.st_value
+= o
->output_offset
;
11157 indx
= bfd_get_symcount (output_bfd
);
11158 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11163 flinfo
->indices
[symndx
] = indx
;
11167 elf_section_data (osec
)->this_hdr
.sh_info
11168 = flinfo
->indices
[symndx
];
11172 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11173 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11176 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11178 /* Section was created by _bfd_elf_link_create_dynamic_sections
11183 /* Get the contents of the section. They have been cached by a
11184 relaxation routine. Note that o is a section in an input
11185 file, so the contents field will not have been set by any of
11186 the routines which work on output files. */
11187 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11189 contents
= elf_section_data (o
)->this_hdr
.contents
;
11190 if (bed
->caches_rawsize
11192 && o
->rawsize
< o
->size
)
11194 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11195 contents
= flinfo
->contents
;
11200 contents
= flinfo
->contents
;
11201 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
11205 if ((o
->flags
& SEC_RELOC
) != 0)
11207 Elf_Internal_Rela
*internal_relocs
;
11208 Elf_Internal_Rela
*rel
, *relend
;
11209 int action_discarded
;
11212 /* Get the swapped relocs. */
11214 = _bfd_elf_link_info_read_relocs (input_bfd
, flinfo
->info
, o
,
11215 flinfo
->external_relocs
,
11216 flinfo
->internal_relocs
,
11218 if (internal_relocs
== NULL
11219 && o
->reloc_count
> 0)
11222 /* We need to reverse-copy input .ctors/.dtors sections if
11223 they are placed in .init_array/.finit_array for output. */
11224 if (o
->size
> address_size
11225 && ((startswith (o
->name
, ".ctors")
11226 && strcmp (o
->output_section
->name
,
11227 ".init_array") == 0)
11228 || (startswith (o
->name
, ".dtors")
11229 && strcmp (o
->output_section
->name
,
11230 ".fini_array") == 0))
11231 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
11233 if (o
->size
* bed
->s
->int_rels_per_ext_rel
11234 != o
->reloc_count
* address_size
)
11237 /* xgettext:c-format */
11238 (_("error: %pB: size of section %pA is not "
11239 "multiple of address size"),
11241 bfd_set_error (bfd_error_bad_value
);
11244 o
->flags
|= SEC_ELF_REVERSE_COPY
;
11247 action_discarded
= -1;
11248 if (!elf_section_ignore_discarded_relocs (o
))
11249 action_discarded
= (*bed
->action_discarded
) (o
);
11251 /* Run through the relocs evaluating complex reloc symbols and
11252 looking for relocs against symbols from discarded sections
11253 or section symbols from removed link-once sections.
11254 Complain about relocs against discarded sections. Zero
11255 relocs against removed link-once sections. */
11257 rel
= internal_relocs
;
11258 relend
= rel
+ o
->reloc_count
;
11259 for ( ; rel
< relend
; rel
++)
11261 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11262 unsigned int s_type
;
11263 asection
**ps
, *sec
;
11264 struct elf_link_hash_entry
*h
= NULL
;
11265 const char *sym_name
;
11267 if (r_symndx
== STN_UNDEF
)
11270 if (r_symndx
>= locsymcount
11271 || (elf_bad_symtab (input_bfd
)
11272 && flinfo
->sections
[r_symndx
] == NULL
))
11274 h
= sym_hashes
[r_symndx
- extsymoff
];
11276 /* Badly formatted input files can contain relocs that
11277 reference non-existant symbols. Check here so that
11278 we do not seg fault. */
11282 /* xgettext:c-format */
11283 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11284 "that references a non-existent global symbol"),
11285 input_bfd
, (uint64_t) rel
->r_info
, o
);
11286 bfd_set_error (bfd_error_bad_value
);
11290 while (h
->root
.type
== bfd_link_hash_indirect
11291 || h
->root
.type
== bfd_link_hash_warning
)
11292 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11296 /* If a plugin symbol is referenced from a non-IR file,
11297 mark the symbol as undefined. Note that the
11298 linker may attach linker created dynamic sections
11299 to the plugin bfd. Symbols defined in linker
11300 created sections are not plugin symbols. */
11301 if ((h
->root
.non_ir_ref_regular
11302 || h
->root
.non_ir_ref_dynamic
)
11303 && (h
->root
.type
== bfd_link_hash_defined
11304 || h
->root
.type
== bfd_link_hash_defweak
)
11305 && (h
->root
.u
.def
.section
->flags
11306 & SEC_LINKER_CREATED
) == 0
11307 && h
->root
.u
.def
.section
->owner
!= NULL
11308 && (h
->root
.u
.def
.section
->owner
->flags
11309 & BFD_PLUGIN
) != 0)
11311 h
->root
.type
= bfd_link_hash_undefined
;
11312 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11316 if (h
->root
.type
== bfd_link_hash_defined
11317 || h
->root
.type
== bfd_link_hash_defweak
)
11318 ps
= &h
->root
.u
.def
.section
;
11320 sym_name
= h
->root
.root
.string
;
11324 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11326 s_type
= ELF_ST_TYPE (sym
->st_info
);
11327 ps
= &flinfo
->sections
[r_symndx
];
11328 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11332 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11333 && !bfd_link_relocatable (flinfo
->info
))
11336 bfd_vma dot
= (rel
->r_offset
11337 + o
->output_offset
+ o
->output_section
->vma
);
11339 printf ("Encountered a complex symbol!");
11340 printf (" (input_bfd %s, section %s, reloc %ld\n",
11341 bfd_get_filename (input_bfd
), o
->name
,
11342 (long) (rel
- internal_relocs
));
11343 printf (" symbol: idx %8.8lx, name %s\n",
11344 r_symndx
, sym_name
);
11345 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11346 (unsigned long) rel
->r_info
,
11347 (unsigned long) rel
->r_offset
);
11349 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11350 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11353 /* Symbol evaluated OK. Update to absolute value. */
11354 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11359 if (action_discarded
!= -1 && ps
!= NULL
)
11361 /* Complain if the definition comes from a
11362 discarded section. */
11363 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11365 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11366 if (action_discarded
& COMPLAIN
)
11367 (*flinfo
->info
->callbacks
->einfo
)
11368 /* xgettext:c-format */
11369 (_("%X`%s' referenced in section `%pA' of %pB: "
11370 "defined in discarded section `%pA' of %pB\n"),
11371 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11373 /* Try to do the best we can to support buggy old
11374 versions of gcc. Pretend that the symbol is
11375 really defined in the kept linkonce section.
11376 FIXME: This is quite broken. Modifying the
11377 symbol here means we will be changing all later
11378 uses of the symbol, not just in this section. */
11379 if (action_discarded
& PRETEND
)
11383 kept
= _bfd_elf_check_kept_section (sec
,
11395 /* Relocate the section by invoking a back end routine.
11397 The back end routine is responsible for adjusting the
11398 section contents as necessary, and (if using Rela relocs
11399 and generating a relocatable output file) adjusting the
11400 reloc addend as necessary.
11402 The back end routine does not have to worry about setting
11403 the reloc address or the reloc symbol index.
11405 The back end routine is given a pointer to the swapped in
11406 internal symbols, and can access the hash table entries
11407 for the external symbols via elf_sym_hashes (input_bfd).
11409 When generating relocatable output, the back end routine
11410 must handle STB_LOCAL/STT_SECTION symbols specially. The
11411 output symbol is going to be a section symbol
11412 corresponding to the output section, which will require
11413 the addend to be adjusted. */
11415 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11416 input_bfd
, o
, contents
,
11424 || bfd_link_relocatable (flinfo
->info
)
11425 || flinfo
->info
->emitrelocations
)
11427 Elf_Internal_Rela
*irela
;
11428 Elf_Internal_Rela
*irelaend
, *irelamid
;
11429 bfd_vma last_offset
;
11430 struct elf_link_hash_entry
**rel_hash
;
11431 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11432 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11433 unsigned int next_erel
;
11435 struct bfd_elf_section_data
*esdi
, *esdo
;
11437 esdi
= elf_section_data (o
);
11438 esdo
= elf_section_data (o
->output_section
);
11439 rela_normal
= false;
11441 /* Adjust the reloc addresses and symbol indices. */
11443 irela
= internal_relocs
;
11444 irelaend
= irela
+ o
->reloc_count
;
11445 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
11446 /* We start processing the REL relocs, if any. When we reach
11447 IRELAMID in the loop, we switch to the RELA relocs. */
11449 if (esdi
->rel
.hdr
!= NULL
)
11450 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11451 * bed
->s
->int_rels_per_ext_rel
);
11452 rel_hash_list
= rel_hash
;
11453 rela_hash_list
= NULL
;
11454 last_offset
= o
->output_offset
;
11455 if (!bfd_link_relocatable (flinfo
->info
))
11456 last_offset
+= o
->output_section
->vma
;
11457 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11459 unsigned long r_symndx
;
11461 Elf_Internal_Sym sym
;
11463 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11469 if (irela
== irelamid
)
11471 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
11472 rela_hash_list
= rel_hash
;
11473 rela_normal
= bed
->rela_normal
;
11476 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11479 if (irela
->r_offset
>= (bfd_vma
) -2)
11481 /* This is a reloc for a deleted entry or somesuch.
11482 Turn it into an R_*_NONE reloc, at the same
11483 offset as the last reloc. elf_eh_frame.c and
11484 bfd_elf_discard_info rely on reloc offsets
11486 irela
->r_offset
= last_offset
;
11488 irela
->r_addend
= 0;
11492 irela
->r_offset
+= o
->output_offset
;
11494 /* Relocs in an executable have to be virtual addresses. */
11495 if (!bfd_link_relocatable (flinfo
->info
))
11496 irela
->r_offset
+= o
->output_section
->vma
;
11498 last_offset
= irela
->r_offset
;
11500 r_symndx
= irela
->r_info
>> r_sym_shift
;
11501 if (r_symndx
== STN_UNDEF
)
11504 if (r_symndx
>= locsymcount
11505 || (elf_bad_symtab (input_bfd
)
11506 && flinfo
->sections
[r_symndx
] == NULL
))
11508 struct elf_link_hash_entry
*rh
;
11509 unsigned long indx
;
11511 /* This is a reloc against a global symbol. We
11512 have not yet output all the local symbols, so
11513 we do not know the symbol index of any global
11514 symbol. We set the rel_hash entry for this
11515 reloc to point to the global hash table entry
11516 for this symbol. The symbol index is then
11517 set at the end of bfd_elf_final_link. */
11518 indx
= r_symndx
- extsymoff
;
11519 rh
= elf_sym_hashes (input_bfd
)[indx
];
11520 while (rh
->root
.type
== bfd_link_hash_indirect
11521 || rh
->root
.type
== bfd_link_hash_warning
)
11522 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11524 /* Setting the index to -2 tells
11525 elf_link_output_extsym that this symbol is
11526 used by a reloc. */
11527 BFD_ASSERT (rh
->indx
< 0);
11534 /* This is a reloc against a local symbol. */
11537 sym
= isymbuf
[r_symndx
];
11538 sec
= flinfo
->sections
[r_symndx
];
11539 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11541 /* I suppose the backend ought to fill in the
11542 section of any STT_SECTION symbol against a
11543 processor specific section. */
11544 r_symndx
= STN_UNDEF
;
11545 if (bfd_is_abs_section (sec
))
11547 else if (sec
== NULL
|| sec
->owner
== NULL
)
11549 bfd_set_error (bfd_error_bad_value
);
11554 asection
*osec
= sec
->output_section
;
11556 /* If we have discarded a section, the output
11557 section will be the absolute section. In
11558 case of discarded SEC_MERGE sections, use
11559 the kept section. relocate_section should
11560 have already handled discarded linkonce
11562 if (bfd_is_abs_section (osec
)
11563 && sec
->kept_section
!= NULL
11564 && sec
->kept_section
->output_section
!= NULL
)
11566 osec
= sec
->kept_section
->output_section
;
11567 irela
->r_addend
-= osec
->vma
;
11570 if (!bfd_is_abs_section (osec
))
11572 r_symndx
= osec
->target_index
;
11573 if (r_symndx
== STN_UNDEF
)
11575 irela
->r_addend
+= osec
->vma
;
11576 osec
= _bfd_nearby_section (output_bfd
, osec
,
11578 irela
->r_addend
-= osec
->vma
;
11579 r_symndx
= osec
->target_index
;
11584 /* Adjust the addend according to where the
11585 section winds up in the output section. */
11587 irela
->r_addend
+= sec
->output_offset
;
11591 if (flinfo
->indices
[r_symndx
] == -1)
11593 unsigned long shlink
;
11598 if (flinfo
->info
->strip
== strip_all
)
11600 /* You can't do ld -r -s. */
11601 bfd_set_error (bfd_error_invalid_operation
);
11605 /* This symbol was skipped earlier, but
11606 since it is needed by a reloc, we
11607 must output it now. */
11608 shlink
= symtab_hdr
->sh_link
;
11609 name
= (bfd_elf_string_from_elf_section
11610 (input_bfd
, shlink
, sym
.st_name
));
11614 osec
= sec
->output_section
;
11616 _bfd_elf_section_from_bfd_section (output_bfd
,
11618 if (sym
.st_shndx
== SHN_BAD
)
11621 sym
.st_value
+= sec
->output_offset
;
11622 if (!bfd_link_relocatable (flinfo
->info
))
11624 sym
.st_value
+= osec
->vma
;
11625 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11627 struct elf_link_hash_table
*htab
11628 = elf_hash_table (flinfo
->info
);
11630 /* STT_TLS symbols are relative to PT_TLS
11632 if (htab
->tls_sec
!= NULL
)
11633 sym
.st_value
-= htab
->tls_sec
->vma
;
11636 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11641 indx
= bfd_get_symcount (output_bfd
);
11642 ret
= elf_link_output_symstrtab (flinfo
, name
,
11648 flinfo
->indices
[r_symndx
] = indx
;
11653 r_symndx
= flinfo
->indices
[r_symndx
];
11656 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11657 | (irela
->r_info
& r_type_mask
));
11660 /* Swap out the relocs. */
11661 input_rel_hdr
= esdi
->rel
.hdr
;
11662 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11664 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11669 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11670 * bed
->s
->int_rels_per_ext_rel
);
11671 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11674 input_rela_hdr
= esdi
->rela
.hdr
;
11675 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11677 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11686 /* Write out the modified section contents. */
11687 if (bed
->elf_backend_write_section
11688 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11691 /* Section written out. */
11693 else switch (o
->sec_info_type
)
11695 case SEC_INFO_TYPE_STABS
:
11696 if (! (_bfd_write_section_stabs
11698 &elf_hash_table (flinfo
->info
)->stab_info
,
11699 o
, &elf_section_data (o
)->sec_info
, contents
)))
11702 case SEC_INFO_TYPE_MERGE
:
11703 if (! _bfd_write_merged_section (output_bfd
, o
,
11704 elf_section_data (o
)->sec_info
))
11707 case SEC_INFO_TYPE_EH_FRAME
:
11709 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11714 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11716 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11724 if (! (o
->flags
& SEC_EXCLUDE
))
11726 file_ptr offset
= (file_ptr
) o
->output_offset
;
11727 bfd_size_type todo
= o
->size
;
11729 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11731 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
11733 /* Reverse-copy input section to output. */
11736 todo
-= address_size
;
11737 if (! bfd_set_section_contents (output_bfd
,
11745 offset
+= address_size
;
11749 else if (! bfd_set_section_contents (output_bfd
,
11763 /* Generate a reloc when linking an ELF file. This is a reloc
11764 requested by the linker, and does not come from any input file. This
11765 is used to build constructor and destructor tables when linking
11769 elf_reloc_link_order (bfd
*output_bfd
,
11770 struct bfd_link_info
*info
,
11771 asection
*output_section
,
11772 struct bfd_link_order
*link_order
)
11774 reloc_howto_type
*howto
;
11778 struct bfd_elf_section_reloc_data
*reldata
;
11779 struct elf_link_hash_entry
**rel_hash_ptr
;
11780 Elf_Internal_Shdr
*rel_hdr
;
11781 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11782 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11785 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11787 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11790 bfd_set_error (bfd_error_bad_value
);
11794 addend
= link_order
->u
.reloc
.p
->addend
;
11797 reldata
= &esdo
->rel
;
11798 else if (esdo
->rela
.hdr
)
11799 reldata
= &esdo
->rela
;
11806 /* Figure out the symbol index. */
11807 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11808 if (link_order
->type
== bfd_section_reloc_link_order
)
11810 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11811 BFD_ASSERT (indx
!= 0);
11812 *rel_hash_ptr
= NULL
;
11816 struct elf_link_hash_entry
*h
;
11818 /* Treat a reloc against a defined symbol as though it were
11819 actually against the section. */
11820 h
= ((struct elf_link_hash_entry
*)
11821 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11822 link_order
->u
.reloc
.p
->u
.name
,
11823 false, false, true));
11825 && (h
->root
.type
== bfd_link_hash_defined
11826 || h
->root
.type
== bfd_link_hash_defweak
))
11830 section
= h
->root
.u
.def
.section
;
11831 indx
= section
->output_section
->target_index
;
11832 *rel_hash_ptr
= NULL
;
11833 /* It seems that we ought to add the symbol value to the
11834 addend here, but in practice it has already been added
11835 because it was passed to constructor_callback. */
11836 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11838 else if (h
!= NULL
)
11840 /* Setting the index to -2 tells elf_link_output_extsym that
11841 this symbol is used by a reloc. */
11848 (*info
->callbacks
->unattached_reloc
)
11849 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11854 /* If this is an inplace reloc, we must write the addend into the
11856 if (howto
->partial_inplace
&& addend
!= 0)
11858 bfd_size_type size
;
11859 bfd_reloc_status_type rstat
;
11862 const char *sym_name
;
11863 bfd_size_type octets
;
11865 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11866 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11867 if (buf
== NULL
&& size
!= 0)
11869 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11876 case bfd_reloc_outofrange
:
11879 case bfd_reloc_overflow
:
11880 if (link_order
->type
== bfd_section_reloc_link_order
)
11881 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
11883 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11884 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11885 howto
->name
, addend
, NULL
, NULL
,
11890 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
11892 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11899 /* The address of a reloc is relative to the section in a
11900 relocatable file, and is a virtual address in an executable
11902 offset
= link_order
->offset
;
11903 if (! bfd_link_relocatable (info
))
11904 offset
+= output_section
->vma
;
11906 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11908 irel
[i
].r_offset
= offset
;
11909 irel
[i
].r_info
= 0;
11910 irel
[i
].r_addend
= 0;
11912 if (bed
->s
->arch_size
== 32)
11913 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11915 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11917 rel_hdr
= reldata
->hdr
;
11918 erel
= rel_hdr
->contents
;
11919 if (rel_hdr
->sh_type
== SHT_REL
)
11921 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11922 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11926 irel
[0].r_addend
= addend
;
11927 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11928 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11936 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11937 Returns TRUE upon success, FALSE otherwise. */
11940 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11944 const struct elf_backend_data
*bed
;
11946 enum bfd_architecture arch
;
11948 asymbol
**sympp
= NULL
;
11952 elf_symbol_type
*osymbuf
;
11955 implib_bfd
= info
->out_implib_bfd
;
11956 bed
= get_elf_backend_data (abfd
);
11958 if (!bfd_set_format (implib_bfd
, bfd_object
))
11961 /* Use flag from executable but make it a relocatable object. */
11962 flags
= bfd_get_file_flags (abfd
);
11963 flags
&= ~HAS_RELOC
;
11964 if (!bfd_set_start_address (implib_bfd
, 0)
11965 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
11968 /* Copy architecture of output file to import library file. */
11969 arch
= bfd_get_arch (abfd
);
11970 mach
= bfd_get_mach (abfd
);
11971 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11972 && (abfd
->target_defaulted
11973 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11976 /* Get symbol table size. */
11977 symsize
= bfd_get_symtab_upper_bound (abfd
);
11981 /* Read in the symbol table. */
11982 sympp
= (asymbol
**) bfd_malloc (symsize
);
11986 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11990 /* Allow the BFD backend to copy any private header data it
11991 understands from the output BFD to the import library BFD. */
11992 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11995 /* Filter symbols to appear in the import library. */
11996 if (bed
->elf_backend_filter_implib_symbols
)
11997 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
12000 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
12003 bfd_set_error (bfd_error_no_symbols
);
12004 _bfd_error_handler (_("%pB: no symbol found for import library"),
12010 /* Make symbols absolute. */
12011 amt
= symcount
* sizeof (*osymbuf
);
12012 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
12013 if (osymbuf
== NULL
)
12016 for (src_count
= 0; src_count
< symcount
; src_count
++)
12018 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
12019 sizeof (*osymbuf
));
12020 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
12021 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
12022 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
12023 osymbuf
[src_count
].internal_elf_sym
.st_value
=
12024 osymbuf
[src_count
].symbol
.value
;
12025 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
12028 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
12030 /* Allow the BFD backend to copy any private data it understands
12031 from the output BFD to the import library BFD. This is done last
12032 to permit the routine to look at the filtered symbol table. */
12033 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
12036 if (!bfd_close (implib_bfd
))
12047 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
12051 if (flinfo
->symstrtab
!= NULL
)
12052 _bfd_elf_strtab_free (flinfo
->symstrtab
);
12053 free (flinfo
->contents
);
12054 free (flinfo
->external_relocs
);
12055 free (flinfo
->internal_relocs
);
12056 free (flinfo
->external_syms
);
12057 free (flinfo
->locsym_shndx
);
12058 free (flinfo
->internal_syms
);
12059 free (flinfo
->indices
);
12060 free (flinfo
->sections
);
12061 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12062 free (flinfo
->symshndxbuf
);
12063 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12065 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12066 free (esdo
->rel
.hashes
);
12067 free (esdo
->rela
.hashes
);
12071 /* Do the final step of an ELF link. */
12074 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12079 struct elf_final_link_info flinfo
;
12081 struct bfd_link_order
*p
;
12083 bfd_size_type max_contents_size
;
12084 bfd_size_type max_external_reloc_size
;
12085 bfd_size_type max_internal_reloc_count
;
12086 bfd_size_type max_sym_count
;
12087 bfd_size_type max_sym_shndx_count
;
12088 Elf_Internal_Sym elfsym
;
12090 Elf_Internal_Shdr
*symtab_hdr
;
12091 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12092 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12093 struct elf_outext_info eoinfo
;
12095 size_t relativecount
= 0;
12096 asection
*reldyn
= 0;
12098 asection
*attr_section
= NULL
;
12099 bfd_vma attr_size
= 0;
12100 const char *std_attrs_section
;
12101 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12102 bool sections_removed
;
12105 if (!is_elf_hash_table (&htab
->root
))
12108 if (bfd_link_pic (info
))
12109 abfd
->flags
|= DYNAMIC
;
12111 dynamic
= htab
->dynamic_sections_created
;
12112 dynobj
= htab
->dynobj
;
12114 emit_relocs
= (bfd_link_relocatable (info
)
12115 || info
->emitrelocations
);
12117 memset (&flinfo
, 0, sizeof (flinfo
));
12118 flinfo
.info
= info
;
12119 flinfo
.output_bfd
= abfd
;
12120 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12121 if (flinfo
.symstrtab
== NULL
)
12126 flinfo
.hash_sec
= NULL
;
12127 flinfo
.symver_sec
= NULL
;
12131 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12132 /* Note that dynsym_sec can be NULL (on VMS). */
12133 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12134 /* Note that it is OK if symver_sec is NULL. */
12137 if (info
->unique_symbol
12138 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12139 local_hash_newfunc
,
12140 sizeof (struct local_hash_entry
)))
12143 /* The object attributes have been merged. Remove the input
12144 sections from the link, and set the contents of the output
12146 sections_removed
= false;
12147 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12148 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12150 bool remove_section
= false;
12152 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12153 || strcmp (o
->name
, ".gnu.attributes") == 0)
12155 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12157 asection
*input_section
;
12159 if (p
->type
!= bfd_indirect_link_order
)
12161 input_section
= p
->u
.indirect
.section
;
12162 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12163 elf_link_input_bfd ignores this section. */
12164 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12167 attr_size
= bfd_elf_obj_attr_size (abfd
);
12168 bfd_set_section_size (o
, attr_size
);
12169 /* Skip this section later on. */
12170 o
->map_head
.link_order
= NULL
;
12174 remove_section
= true;
12176 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12178 /* Remove empty group section from linker output. */
12179 remove_section
= true;
12181 if (remove_section
)
12183 o
->flags
|= SEC_EXCLUDE
;
12184 bfd_section_list_remove (abfd
, o
);
12185 abfd
->section_count
--;
12186 sections_removed
= true;
12189 if (sections_removed
)
12190 _bfd_fix_excluded_sec_syms (abfd
, info
);
12192 /* Count up the number of relocations we will output for each output
12193 section, so that we know the sizes of the reloc sections. We
12194 also figure out some maximum sizes. */
12195 max_contents_size
= 0;
12196 max_external_reloc_size
= 0;
12197 max_internal_reloc_count
= 0;
12199 max_sym_shndx_count
= 0;
12201 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12203 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12204 o
->reloc_count
= 0;
12206 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12208 unsigned int reloc_count
= 0;
12209 unsigned int additional_reloc_count
= 0;
12210 struct bfd_elf_section_data
*esdi
= NULL
;
12212 if (p
->type
== bfd_section_reloc_link_order
12213 || p
->type
== bfd_symbol_reloc_link_order
)
12215 else if (p
->type
== bfd_indirect_link_order
)
12219 sec
= p
->u
.indirect
.section
;
12221 /* Mark all sections which are to be included in the
12222 link. This will normally be every section. We need
12223 to do this so that we can identify any sections which
12224 the linker has decided to not include. */
12225 sec
->linker_mark
= true;
12227 if (sec
->flags
& SEC_MERGE
)
12230 if (sec
->rawsize
> max_contents_size
)
12231 max_contents_size
= sec
->rawsize
;
12232 if (sec
->size
> max_contents_size
)
12233 max_contents_size
= sec
->size
;
12235 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12236 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12240 /* We are interested in just local symbols, not all
12242 if (elf_bad_symtab (sec
->owner
))
12243 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12244 / bed
->s
->sizeof_sym
);
12246 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12248 if (sym_count
> max_sym_count
)
12249 max_sym_count
= sym_count
;
12251 if (sym_count
> max_sym_shndx_count
12252 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12253 max_sym_shndx_count
= sym_count
;
12255 if (esdo
->this_hdr
.sh_type
== SHT_REL
12256 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
12257 /* Some backends use reloc_count in relocation sections
12258 to count particular types of relocs. Of course,
12259 reloc sections themselves can't have relocations. */
12261 else if (emit_relocs
)
12263 reloc_count
= sec
->reloc_count
;
12264 if (bed
->elf_backend_count_additional_relocs
)
12267 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12268 additional_reloc_count
+= c
;
12271 else if (bed
->elf_backend_count_relocs
)
12272 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12274 esdi
= elf_section_data (sec
);
12276 if ((sec
->flags
& SEC_RELOC
) != 0)
12278 size_t ext_size
= 0;
12280 if (esdi
->rel
.hdr
!= NULL
)
12281 ext_size
= esdi
->rel
.hdr
->sh_size
;
12282 if (esdi
->rela
.hdr
!= NULL
)
12283 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12285 if (ext_size
> max_external_reloc_size
)
12286 max_external_reloc_size
= ext_size
;
12287 if (sec
->reloc_count
> max_internal_reloc_count
)
12288 max_internal_reloc_count
= sec
->reloc_count
;
12293 if (reloc_count
== 0)
12296 reloc_count
+= additional_reloc_count
;
12297 o
->reloc_count
+= reloc_count
;
12299 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12303 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12304 esdo
->rel
.count
+= additional_reloc_count
;
12306 if (esdi
->rela
.hdr
)
12308 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12309 esdo
->rela
.count
+= additional_reloc_count
;
12315 esdo
->rela
.count
+= reloc_count
;
12317 esdo
->rel
.count
+= reloc_count
;
12321 if (o
->reloc_count
> 0)
12322 o
->flags
|= SEC_RELOC
;
12325 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12326 set it (this is probably a bug) and if it is set
12327 assign_section_numbers will create a reloc section. */
12328 o
->flags
&=~ SEC_RELOC
;
12331 /* If the SEC_ALLOC flag is not set, force the section VMA to
12332 zero. This is done in elf_fake_sections as well, but forcing
12333 the VMA to 0 here will ensure that relocs against these
12334 sections are handled correctly. */
12335 if ((o
->flags
& SEC_ALLOC
) == 0
12336 && ! o
->user_set_vma
)
12340 if (! bfd_link_relocatable (info
) && merged
)
12341 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12343 /* Figure out the file positions for everything but the symbol table
12344 and the relocs. We set symcount to force assign_section_numbers
12345 to create a symbol table. */
12346 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12347 BFD_ASSERT (! abfd
->output_has_begun
);
12348 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12351 /* Set sizes, and assign file positions for reloc sections. */
12352 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12354 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12355 if ((o
->flags
& SEC_RELOC
) != 0)
12358 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12362 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12366 /* _bfd_elf_compute_section_file_positions makes temporary use
12367 of target_index. Reset it. */
12368 o
->target_index
= 0;
12370 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12371 to count upwards while actually outputting the relocations. */
12372 esdo
->rel
.count
= 0;
12373 esdo
->rela
.count
= 0;
12375 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12376 && !bfd_section_is_ctf (o
))
12378 /* Cache the section contents so that they can be compressed
12379 later. Use bfd_malloc since it will be freed by
12380 bfd_compress_section_contents. */
12381 unsigned char *contents
= esdo
->this_hdr
.contents
;
12382 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
12385 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12386 if (contents
== NULL
)
12388 esdo
->this_hdr
.contents
= contents
;
12392 /* We have now assigned file positions for all the sections except .symtab,
12393 .strtab, and non-loaded reloc and compressed debugging sections. We start
12394 the .symtab section at the current file position, and write directly to it.
12395 We build the .strtab section in memory. */
12396 abfd
->symcount
= 0;
12397 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12398 /* sh_name is set in prep_headers. */
12399 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12400 /* sh_flags, sh_addr and sh_size all start off zero. */
12401 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12402 /* sh_link is set in assign_section_numbers. */
12403 /* sh_info is set below. */
12404 /* sh_offset is set just below. */
12405 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12407 if (max_sym_count
< 20)
12408 max_sym_count
= 20;
12409 htab
->strtabsize
= max_sym_count
;
12410 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12411 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12412 if (htab
->strtab
== NULL
)
12414 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12416 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12417 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12419 if (info
->strip
!= strip_all
|| emit_relocs
)
12421 file_ptr off
= elf_next_file_pos (abfd
);
12423 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
12425 /* Note that at this point elf_next_file_pos (abfd) is
12426 incorrect. We do not yet know the size of the .symtab section.
12427 We correct next_file_pos below, after we do know the size. */
12429 /* Start writing out the symbol table. The first symbol is always a
12431 elfsym
.st_value
= 0;
12432 elfsym
.st_size
= 0;
12433 elfsym
.st_info
= 0;
12434 elfsym
.st_other
= 0;
12435 elfsym
.st_shndx
= SHN_UNDEF
;
12436 elfsym
.st_target_internal
= 0;
12437 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12438 bfd_und_section_ptr
, NULL
) != 1)
12441 /* Output a symbol for each section if asked or they are used for
12442 relocs. These symbols usually have no names. We store the
12443 index of each one in the index field of the section, so that
12444 we can find it again when outputting relocs. */
12446 if (bfd_keep_unused_section_symbols (abfd
) || emit_relocs
)
12448 bool name_local_sections
12449 = (bed
->elf_backend_name_local_section_symbols
12450 && bed
->elf_backend_name_local_section_symbols (abfd
));
12451 const char *name
= NULL
;
12453 elfsym
.st_size
= 0;
12454 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12455 elfsym
.st_other
= 0;
12456 elfsym
.st_value
= 0;
12457 elfsym
.st_target_internal
= 0;
12458 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12460 o
= bfd_section_from_elf_index (abfd
, i
);
12463 o
->target_index
= bfd_get_symcount (abfd
);
12464 elfsym
.st_shndx
= i
;
12465 if (!bfd_link_relocatable (info
))
12466 elfsym
.st_value
= o
->vma
;
12467 if (name_local_sections
)
12469 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12477 /* On some targets like Irix 5 the symbol split between local and global
12478 ones recorded in the sh_info field needs to be done between section
12479 and all other symbols. */
12480 if (bed
->elf_backend_elfsym_local_is_section
12481 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12482 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12484 /* Allocate some memory to hold information read in from the input
12486 if (max_contents_size
!= 0)
12488 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12489 if (flinfo
.contents
== NULL
)
12493 if (max_external_reloc_size
!= 0)
12495 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12496 if (flinfo
.external_relocs
== NULL
)
12500 if (max_internal_reloc_count
!= 0)
12502 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12503 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12504 if (flinfo
.internal_relocs
== NULL
)
12508 if (max_sym_count
!= 0)
12510 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12511 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12512 if (flinfo
.external_syms
== NULL
)
12515 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12516 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12517 if (flinfo
.internal_syms
== NULL
)
12520 amt
= max_sym_count
* sizeof (long);
12521 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12522 if (flinfo
.indices
== NULL
)
12525 amt
= max_sym_count
* sizeof (asection
*);
12526 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12527 if (flinfo
.sections
== NULL
)
12531 if (max_sym_shndx_count
!= 0)
12533 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12534 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12535 if (flinfo
.locsym_shndx
== NULL
)
12541 bfd_vma base
, end
= 0; /* Both bytes. */
12544 for (sec
= htab
->tls_sec
;
12545 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12548 bfd_size_type size
= sec
->size
;
12549 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12552 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12554 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12557 size
= ord
->offset
* opb
+ ord
->size
;
12559 end
= sec
->vma
+ size
/ opb
;
12561 base
= htab
->tls_sec
->vma
;
12562 /* Only align end of TLS section if static TLS doesn't have special
12563 alignment requirements. */
12564 if (bed
->static_tls_alignment
== 1)
12565 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12566 htab
->tls_size
= end
- base
;
12569 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12572 /* Since ELF permits relocations to be against local symbols, we
12573 must have the local symbols available when we do the relocations.
12574 Since we would rather only read the local symbols once, and we
12575 would rather not keep them in memory, we handle all the
12576 relocations for a single input file at the same time.
12578 Unfortunately, there is no way to know the total number of local
12579 symbols until we have seen all of them, and the local symbol
12580 indices precede the global symbol indices. This means that when
12581 we are generating relocatable output, and we see a reloc against
12582 a global symbol, we can not know the symbol index until we have
12583 finished examining all the local symbols to see which ones we are
12584 going to output. To deal with this, we keep the relocations in
12585 memory, and don't output them until the end of the link. This is
12586 an unfortunate waste of memory, but I don't see a good way around
12587 it. Fortunately, it only happens when performing a relocatable
12588 link, which is not the common case. FIXME: If keep_memory is set
12589 we could write the relocs out and then read them again; I don't
12590 know how bad the memory loss will be. */
12592 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12593 sub
->output_has_begun
= false;
12594 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12596 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12598 if (p
->type
== bfd_indirect_link_order
12599 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12600 == bfd_target_elf_flavour
)
12601 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12603 if (! sub
->output_has_begun
)
12605 if (! elf_link_input_bfd (&flinfo
, sub
))
12607 sub
->output_has_begun
= true;
12610 else if (p
->type
== bfd_section_reloc_link_order
12611 || p
->type
== bfd_symbol_reloc_link_order
)
12613 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12618 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12620 if (p
->type
== bfd_indirect_link_order
12621 && (bfd_get_flavour (sub
)
12622 == bfd_target_elf_flavour
)
12623 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12624 != bed
->s
->elfclass
))
12626 const char *iclass
, *oclass
;
12628 switch (bed
->s
->elfclass
)
12630 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12631 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12632 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12636 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12638 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12639 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12640 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12644 bfd_set_error (bfd_error_wrong_format
);
12646 /* xgettext:c-format */
12647 (_("%pB: file class %s incompatible with %s"),
12648 sub
, iclass
, oclass
);
12657 /* Free symbol buffer if needed. */
12658 if (!info
->reduce_memory_overheads
)
12660 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12661 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
12663 free (elf_tdata (sub
)->symbuf
);
12664 elf_tdata (sub
)->symbuf
= NULL
;
12670 /* Output any global symbols that got converted to local in a
12671 version script or due to symbol visibility. We do this in a
12672 separate step since ELF requires all local symbols to appear
12673 prior to any global symbols. FIXME: We should only do this if
12674 some global symbols were, in fact, converted to become local.
12675 FIXME: Will this work correctly with the Irix 5 linker? */
12676 eoinfo
.failed
= false;
12677 eoinfo
.flinfo
= &flinfo
;
12678 eoinfo
.localsyms
= true;
12679 eoinfo
.file_sym_done
= false;
12680 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12684 goto return_local_hash_table
;
12687 /* If backend needs to output some local symbols not present in the hash
12688 table, do it now. */
12689 if (bed
->elf_backend_output_arch_local_syms
12690 && (info
->strip
!= strip_all
|| emit_relocs
))
12692 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12693 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
12696 goto return_local_hash_table
;
12700 /* That wrote out all the local symbols. Finish up the symbol table
12701 with the global symbols. Even if we want to strip everything we
12702 can, we still need to deal with those global symbols that got
12703 converted to local in a version script. */
12705 /* The sh_info field records the index of the first non local symbol. */
12706 if (!symtab_hdr
->sh_info
)
12707 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12710 && htab
->dynsym
!= NULL
12711 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12713 Elf_Internal_Sym sym
;
12714 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12716 o
= htab
->dynsym
->output_section
;
12717 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12719 /* Write out the section symbols for the output sections. */
12720 if (bfd_link_pic (info
)
12721 || htab
->is_relocatable_executable
)
12727 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12729 sym
.st_target_internal
= 0;
12731 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12737 dynindx
= elf_section_data (s
)->dynindx
;
12740 indx
= elf_section_data (s
)->this_idx
;
12741 BFD_ASSERT (indx
> 0);
12742 sym
.st_shndx
= indx
;
12743 if (! check_dynsym (abfd
, &sym
))
12746 goto return_local_hash_table
;
12748 sym
.st_value
= s
->vma
;
12749 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12751 /* Inform the linker of the addition of this symbol. */
12753 if (info
->callbacks
->ctf_new_dynsym
)
12754 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
12756 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12760 /* Write out the local dynsyms. */
12761 if (htab
->dynlocal
)
12763 struct elf_link_local_dynamic_entry
*e
;
12764 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12769 /* Copy the internal symbol and turn off visibility.
12770 Note that we saved a word of storage and overwrote
12771 the original st_name with the dynstr_index. */
12773 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12774 sym
.st_shndx
= SHN_UNDEF
;
12776 s
= bfd_section_from_elf_index (e
->input_bfd
,
12779 && s
->output_section
!= NULL
12780 && elf_section_data (s
->output_section
) != NULL
)
12783 elf_section_data (s
->output_section
)->this_idx
;
12784 if (! check_dynsym (abfd
, &sym
))
12787 goto return_local_hash_table
;
12789 sym
.st_value
= (s
->output_section
->vma
12791 + e
->isym
.st_value
);
12794 /* Inform the linker of the addition of this symbol. */
12796 if (info
->callbacks
->ctf_new_dynsym
)
12797 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
12799 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12800 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12805 /* We get the global symbols from the hash table. */
12806 eoinfo
.failed
= false;
12807 eoinfo
.localsyms
= false;
12808 eoinfo
.flinfo
= &flinfo
;
12809 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12813 goto return_local_hash_table
;
12816 /* If backend needs to output some symbols not present in the hash
12817 table, do it now. */
12818 if (bed
->elf_backend_output_arch_syms
12819 && (info
->strip
!= strip_all
|| emit_relocs
))
12821 if (! ((*bed
->elf_backend_output_arch_syms
)
12822 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
12825 goto return_local_hash_table
;
12829 /* Finalize the .strtab section. */
12830 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12832 /* Swap out the .strtab section. */
12833 if (!elf_link_swap_symbols_out (&flinfo
))
12836 goto return_local_hash_table
;
12839 /* Now we know the size of the symtab section. */
12840 if (bfd_get_symcount (abfd
) > 0)
12842 /* Finish up and write out the symbol string table (.strtab)
12844 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12845 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12847 if (elf_symtab_shndx_list (abfd
))
12849 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12851 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12853 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12854 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12855 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12856 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12857 symtab_shndx_hdr
->sh_size
= amt
;
12859 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12862 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12863 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12866 goto return_local_hash_table
;
12871 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12872 /* sh_name was set in prep_headers. */
12873 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12874 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12875 symstrtab_hdr
->sh_addr
= 0;
12876 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12877 symstrtab_hdr
->sh_entsize
= 0;
12878 symstrtab_hdr
->sh_link
= 0;
12879 symstrtab_hdr
->sh_info
= 0;
12880 /* sh_offset is set just below. */
12881 symstrtab_hdr
->sh_addralign
= 1;
12883 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12885 elf_next_file_pos (abfd
) = off
;
12887 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12888 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12891 goto return_local_hash_table
;
12895 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12897 _bfd_error_handler (_("%pB: failed to generate import library"),
12898 info
->out_implib_bfd
);
12900 goto return_local_hash_table
;
12903 /* Adjust the relocs to have the correct symbol indices. */
12904 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12906 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12909 if ((o
->flags
& SEC_RELOC
) == 0)
12912 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12913 if (esdo
->rel
.hdr
!= NULL
12914 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
12917 goto return_local_hash_table
;
12919 if (esdo
->rela
.hdr
!= NULL
12920 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
12923 goto return_local_hash_table
;
12926 /* Set the reloc_count field to 0 to prevent write_relocs from
12927 trying to swap the relocs out itself. */
12928 o
->reloc_count
= 0;
12931 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12932 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12934 /* If we are linking against a dynamic object, or generating a
12935 shared library, finish up the dynamic linking information. */
12938 bfd_byte
*dyncon
, *dynconend
;
12940 /* Fix up .dynamic entries. */
12941 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12942 BFD_ASSERT (o
!= NULL
);
12944 dyncon
= o
->contents
;
12945 dynconend
= o
->contents
+ o
->size
;
12946 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12948 Elf_Internal_Dyn dyn
;
12951 bfd_size_type sh_size
;
12954 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12961 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12963 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12965 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12966 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12969 dyn
.d_un
.d_val
= relativecount
;
12976 name
= info
->init_function
;
12979 name
= info
->fini_function
;
12982 struct elf_link_hash_entry
*h
;
12984 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
12986 && (h
->root
.type
== bfd_link_hash_defined
12987 || h
->root
.type
== bfd_link_hash_defweak
))
12989 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12990 o
= h
->root
.u
.def
.section
;
12991 if (o
->output_section
!= NULL
)
12992 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12993 + o
->output_offset
);
12996 /* The symbol is imported from another shared
12997 library and does not apply to this one. */
12998 dyn
.d_un
.d_ptr
= 0;
13005 case DT_PREINIT_ARRAYSZ
:
13006 name
= ".preinit_array";
13008 case DT_INIT_ARRAYSZ
:
13009 name
= ".init_array";
13011 case DT_FINI_ARRAYSZ
:
13012 name
= ".fini_array";
13014 o
= bfd_get_section_by_name (abfd
, name
);
13018 (_("could not find section %s"), name
);
13023 (_("warning: %s section has zero size"), name
);
13024 dyn
.d_un
.d_val
= o
->size
;
13027 case DT_PREINIT_ARRAY
:
13028 name
= ".preinit_array";
13030 case DT_INIT_ARRAY
:
13031 name
= ".init_array";
13033 case DT_FINI_ARRAY
:
13034 name
= ".fini_array";
13036 o
= bfd_get_section_by_name (abfd
, name
);
13043 name
= ".gnu.hash";
13052 name
= ".gnu.version_d";
13055 name
= ".gnu.version_r";
13058 name
= ".gnu.version";
13060 o
= bfd_get_linker_section (dynobj
, name
);
13062 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13065 (_("could not find section %s"), name
);
13068 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13071 (_("warning: section '%s' is being made into a note"), name
);
13072 bfd_set_error (bfd_error_nonrepresentable_section
);
13075 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13082 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13088 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13090 Elf_Internal_Shdr
*hdr
;
13092 hdr
= elf_elfsections (abfd
)[i
];
13093 if (hdr
->sh_type
== type
13094 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13096 sh_size
+= hdr
->sh_size
;
13098 || sh_addr
> hdr
->sh_addr
)
13099 sh_addr
= hdr
->sh_addr
;
13103 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13105 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13107 /* Don't count procedure linkage table relocs in the
13108 overall reloc count. */
13109 sh_size
-= htab
->srelplt
->size
;
13111 /* If the size is zero, make the address zero too.
13112 This is to avoid a glibc bug. If the backend
13113 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13114 zero, then we'll put DT_RELA at the end of
13115 DT_JMPREL. glibc will interpret the end of
13116 DT_RELA matching the end of DT_JMPREL as the
13117 case where DT_RELA includes DT_JMPREL, and for
13118 LD_BIND_NOW will decide that processing DT_RELA
13119 will process the PLT relocs too. Net result:
13120 No PLT relocs applied. */
13123 /* If .rela.plt is the first .rela section, exclude
13124 it from DT_RELA. */
13125 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13126 + htab
->srelplt
->output_offset
) * opb
)
13127 sh_addr
+= htab
->srelplt
->size
;
13130 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13131 dyn
.d_un
.d_val
= sh_size
;
13133 dyn
.d_un
.d_ptr
= sh_addr
;
13136 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13140 /* If we have created any dynamic sections, then output them. */
13141 if (dynobj
!= NULL
)
13143 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13146 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13147 if (bfd_link_textrel_check (info
)
13148 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
13150 bfd_byte
*dyncon
, *dynconend
;
13152 dyncon
= o
->contents
;
13153 dynconend
= o
->contents
+ o
->size
;
13154 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13156 Elf_Internal_Dyn dyn
;
13158 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13160 if (dyn
.d_tag
== DT_TEXTREL
)
13162 if (info
->textrel_check
== textrel_check_error
)
13163 info
->callbacks
->einfo
13164 (_("%P%X: read-only segment has dynamic relocations\n"));
13165 else if (bfd_link_dll (info
))
13166 info
->callbacks
->einfo
13167 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13168 else if (bfd_link_pde (info
))
13169 info
->callbacks
->einfo
13170 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13172 info
->callbacks
->einfo
13173 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13179 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13181 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13183 || o
->output_section
== bfd_abs_section_ptr
)
13185 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13187 /* At this point, we are only interested in sections
13188 created by _bfd_elf_link_create_dynamic_sections. */
13191 if (htab
->stab_info
.stabstr
== o
)
13193 if (htab
->eh_info
.hdr_sec
== o
)
13195 if (strcmp (o
->name
, ".dynstr") != 0)
13197 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13198 * bfd_octets_per_byte (abfd
, o
));
13199 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13200 o
->contents
, octets
, o
->size
))
13205 /* The contents of the .dynstr section are actually in a
13209 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13210 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13211 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13217 if (!info
->resolve_section_groups
)
13219 bool failed
= false;
13221 BFD_ASSERT (bfd_link_relocatable (info
));
13222 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13227 /* If we have optimized stabs strings, output them. */
13228 if (htab
->stab_info
.stabstr
!= NULL
)
13230 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13234 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13237 if (info
->callbacks
->emit_ctf
)
13238 info
->callbacks
->emit_ctf ();
13240 elf_final_link_free (abfd
, &flinfo
);
13244 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13245 if (contents
== NULL
)
13247 /* Bail out and fail. */
13249 goto return_local_hash_table
;
13251 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13252 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13256 return_local_hash_table
:
13257 if (info
->unique_symbol
)
13258 bfd_hash_table_free (&flinfo
.local_hash_table
);
13262 elf_final_link_free (abfd
, &flinfo
);
13264 goto return_local_hash_table
;
13267 /* Initialize COOKIE for input bfd ABFD. */
13270 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13271 struct bfd_link_info
*info
, bfd
*abfd
)
13273 Elf_Internal_Shdr
*symtab_hdr
;
13274 const struct elf_backend_data
*bed
;
13276 bed
= get_elf_backend_data (abfd
);
13277 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13279 cookie
->abfd
= abfd
;
13280 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13281 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13282 if (cookie
->bad_symtab
)
13284 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13285 cookie
->extsymoff
= 0;
13289 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13290 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13293 if (bed
->s
->arch_size
== 32)
13294 cookie
->r_sym_shift
= 8;
13296 cookie
->r_sym_shift
= 32;
13298 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13299 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13301 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13302 cookie
->locsymcount
, 0,
13304 if (cookie
->locsyms
== NULL
)
13306 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13309 if (_bfd_link_keep_memory (info
) )
13311 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13312 info
->cache_size
+= (cookie
->locsymcount
13313 * sizeof (Elf_External_Sym_Shndx
));
13319 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13322 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13324 Elf_Internal_Shdr
*symtab_hdr
;
13326 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13327 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13328 free (cookie
->locsyms
);
13331 /* Initialize the relocation information in COOKIE for input section SEC
13332 of input bfd ABFD. */
13335 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13336 struct bfd_link_info
*info
, bfd
*abfd
,
13339 if (sec
->reloc_count
== 0)
13341 cookie
->rels
= NULL
;
13342 cookie
->relend
= NULL
;
13346 cookie
->rels
= _bfd_elf_link_info_read_relocs (abfd
, info
, sec
,
13348 _bfd_link_keep_memory (info
));
13349 if (cookie
->rels
== NULL
)
13351 cookie
->rel
= cookie
->rels
;
13352 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13354 cookie
->rel
= cookie
->rels
;
13358 /* Free the memory allocated by init_reloc_cookie_rels,
13362 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13365 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13366 free (cookie
->rels
);
13369 /* Initialize the whole of COOKIE for input section SEC. */
13372 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13373 struct bfd_link_info
*info
,
13376 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13378 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13383 fini_reloc_cookie (cookie
, sec
->owner
);
13388 /* Free the memory allocated by init_reloc_cookie_for_section,
13392 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13395 fini_reloc_cookie_rels (cookie
, sec
);
13396 fini_reloc_cookie (cookie
, sec
->owner
);
13399 /* Garbage collect unused sections. */
13401 /* Default gc_mark_hook. */
13404 _bfd_elf_gc_mark_hook (asection
*sec
,
13405 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13406 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13407 struct elf_link_hash_entry
*h
,
13408 Elf_Internal_Sym
*sym
)
13412 switch (h
->root
.type
)
13414 case bfd_link_hash_defined
:
13415 case bfd_link_hash_defweak
:
13416 return h
->root
.u
.def
.section
;
13418 case bfd_link_hash_common
:
13419 return h
->root
.u
.c
.p
->section
;
13426 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13431 /* Return the debug definition section. */
13434 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13435 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13436 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13437 struct elf_link_hash_entry
*h
,
13438 Elf_Internal_Sym
*sym
)
13442 /* Return the global debug definition section. */
13443 if ((h
->root
.type
== bfd_link_hash_defined
13444 || h
->root
.type
== bfd_link_hash_defweak
)
13445 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13446 return h
->root
.u
.def
.section
;
13450 /* Return the local debug definition section. */
13451 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13453 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
13460 /* COOKIE->rel describes a relocation against section SEC, which is
13461 a section we've decided to keep. Return the section that contains
13462 the relocation symbol, or NULL if no section contains it. */
13465 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13466 elf_gc_mark_hook_fn gc_mark_hook
,
13467 struct elf_reloc_cookie
*cookie
,
13470 unsigned long r_symndx
;
13471 struct elf_link_hash_entry
*h
, *hw
;
13473 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13474 if (r_symndx
== STN_UNDEF
)
13477 if (r_symndx
>= cookie
->locsymcount
13478 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13482 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13485 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13489 while (h
->root
.type
== bfd_link_hash_indirect
13490 || h
->root
.type
== bfd_link_hash_warning
)
13491 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13493 was_marked
= h
->mark
;
13495 /* Keep all aliases of the symbol too. If an object symbol
13496 needs to be copied into .dynbss then all of its aliases
13497 should be present as dynamic symbols, not just the one used
13498 on the copy relocation. */
13500 while (hw
->is_weakalias
)
13506 if (!was_marked
&& h
->start_stop
&& !h
->root
.ldscript_def
)
13508 if (info
->start_stop_gc
)
13511 /* To work around a glibc bug, mark XXX input sections
13512 when there is a reference to __start_XXX or __stop_XXX
13514 else if (start_stop
!= NULL
)
13516 asection
*s
= h
->u2
.start_stop_section
;
13517 *start_stop
= true;
13522 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13525 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13526 &cookie
->locsyms
[r_symndx
]);
13529 /* COOKIE->rel describes a relocation against section SEC, which is
13530 a section we've decided to keep. Mark the section that contains
13531 the relocation symbol. */
13534 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13536 elf_gc_mark_hook_fn gc_mark_hook
,
13537 struct elf_reloc_cookie
*cookie
)
13540 bool start_stop
= false;
13542 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13543 while (rsec
!= NULL
)
13545 if (!rsec
->gc_mark
)
13547 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13548 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13550 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13555 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13560 /* The mark phase of garbage collection. For a given section, mark
13561 it and any sections in this section's group, and all the sections
13562 which define symbols to which it refers. */
13565 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13567 elf_gc_mark_hook_fn gc_mark_hook
)
13570 asection
*group_sec
, *eh_frame
;
13574 /* Mark all the sections in the group. */
13575 group_sec
= elf_section_data (sec
)->next_in_group
;
13576 if (group_sec
&& !group_sec
->gc_mark
)
13577 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13580 /* Look through the section relocs. */
13582 eh_frame
= elf_eh_frame_section (sec
->owner
);
13583 if ((sec
->flags
& SEC_RELOC
) != 0
13584 && sec
->reloc_count
> 0
13585 && sec
!= eh_frame
)
13587 struct elf_reloc_cookie cookie
;
13589 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13593 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13594 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13599 fini_reloc_cookie_for_section (&cookie
, sec
);
13603 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13605 struct elf_reloc_cookie cookie
;
13607 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13611 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13612 gc_mark_hook
, &cookie
))
13614 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13618 eh_frame
= elf_section_eh_frame_entry (sec
);
13619 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13620 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13626 /* Scan and mark sections in a special or debug section group. */
13629 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13631 /* Point to first section of section group. */
13633 /* Used to iterate the section group. */
13636 bool is_special_grp
= true;
13637 bool is_debug_grp
= true;
13639 /* First scan to see if group contains any section other than debug
13640 and special section. */
13641 ssec
= msec
= elf_next_in_group (grp
);
13644 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13645 is_debug_grp
= false;
13647 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13648 is_special_grp
= false;
13650 msec
= elf_next_in_group (msec
);
13652 while (msec
!= ssec
);
13654 /* If this is a pure debug section group or pure special section group,
13655 keep all sections in this group. */
13656 if (is_debug_grp
|| is_special_grp
)
13661 msec
= elf_next_in_group (msec
);
13663 while (msec
!= ssec
);
13667 /* Keep debug and special sections. */
13670 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13671 elf_gc_mark_hook_fn mark_hook
)
13675 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13679 bool debug_frag_seen
;
13680 bool has_kept_debug_info
;
13682 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13684 isec
= ibfd
->sections
;
13685 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13688 /* Ensure all linker created sections are kept,
13689 see if any other section is already marked,
13690 and note if we have any fragmented debug sections. */
13691 debug_frag_seen
= some_kept
= has_kept_debug_info
= false;
13692 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13694 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13696 else if (isec
->gc_mark
13697 && (isec
->flags
& SEC_ALLOC
) != 0
13698 && elf_section_type (isec
) != SHT_NOTE
)
13702 /* Since all sections, except for backend specific ones,
13703 have been garbage collected, call mark_hook on this
13704 section if any of its linked-to sections is marked. */
13705 asection
*linked_to_sec
;
13706 for (linked_to_sec
= elf_linked_to_section (isec
);
13707 linked_to_sec
!= NULL
&& !linked_to_sec
->linker_mark
;
13708 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13710 if (linked_to_sec
->gc_mark
)
13712 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
13716 linked_to_sec
->linker_mark
= 1;
13718 for (linked_to_sec
= elf_linked_to_section (isec
);
13719 linked_to_sec
!= NULL
&& linked_to_sec
->linker_mark
;
13720 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13721 linked_to_sec
->linker_mark
= 0;
13724 if (!debug_frag_seen
13725 && (isec
->flags
& SEC_DEBUGGING
)
13726 && startswith (isec
->name
, ".debug_line."))
13727 debug_frag_seen
= true;
13728 else if (strcmp (bfd_section_name (isec
),
13729 "__patchable_function_entries") == 0
13730 && elf_linked_to_section (isec
) == NULL
)
13731 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
13732 "need linked-to section "
13733 "for --gc-sections\n"),
13734 isec
->owner
, isec
);
13737 /* If no non-note alloc section in this file will be kept, then
13738 we can toss out the debug and special sections. */
13742 /* Keep debug and special sections like .comment when they are
13743 not part of a group. Also keep section groups that contain
13744 just debug sections or special sections. NB: Sections with
13745 linked-to section has been handled above. */
13746 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13748 if ((isec
->flags
& SEC_GROUP
) != 0)
13749 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13750 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13751 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13752 && elf_next_in_group (isec
) == NULL
13753 && elf_linked_to_section (isec
) == NULL
)
13755 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13756 has_kept_debug_info
= true;
13759 /* Look for CODE sections which are going to be discarded,
13760 and find and discard any fragmented debug sections which
13761 are associated with that code section. */
13762 if (debug_frag_seen
)
13763 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13764 if ((isec
->flags
& SEC_CODE
) != 0
13765 && isec
->gc_mark
== 0)
13770 ilen
= strlen (isec
->name
);
13772 /* Association is determined by the name of the debug
13773 section containing the name of the code section as
13774 a suffix. For example .debug_line.text.foo is a
13775 debug section associated with .text.foo. */
13776 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13780 if (dsec
->gc_mark
== 0
13781 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13784 dlen
= strlen (dsec
->name
);
13787 && strncmp (dsec
->name
+ (dlen
- ilen
),
13788 isec
->name
, ilen
) == 0)
13793 /* Mark debug sections referenced by kept debug sections. */
13794 if (has_kept_debug_info
)
13795 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13797 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13798 if (!_bfd_elf_gc_mark (info
, isec
,
13799 elf_gc_mark_debug_section
))
13806 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13809 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13811 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13815 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13816 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13817 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13820 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13823 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13825 /* When any section in a section group is kept, we keep all
13826 sections in the section group. If the first member of
13827 the section group is excluded, we will also exclude the
13829 if (o
->flags
& SEC_GROUP
)
13831 asection
*first
= elf_next_in_group (o
);
13832 o
->gc_mark
= first
->gc_mark
;
13838 /* Skip sweeping sections already excluded. */
13839 if (o
->flags
& SEC_EXCLUDE
)
13842 /* Since this is early in the link process, it is simple
13843 to remove a section from the output. */
13844 o
->flags
|= SEC_EXCLUDE
;
13846 if (info
->print_gc_sections
&& o
->size
!= 0)
13847 /* xgettext:c-format */
13848 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13856 /* Propagate collected vtable information. This is called through
13857 elf_link_hash_traverse. */
13860 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13862 /* Those that are not vtables. */
13864 || h
->u2
.vtable
== NULL
13865 || h
->u2
.vtable
->parent
== NULL
)
13868 /* Those vtables that do not have parents, we cannot merge. */
13869 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13872 /* If we've already been done, exit. */
13873 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
13876 /* Make sure the parent's table is up to date. */
13877 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
13879 if (h
->u2
.vtable
->used
== NULL
)
13881 /* None of this table's entries were referenced. Re-use the
13883 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13884 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
13891 /* Or the parent's entries into ours. */
13892 cu
= h
->u2
.vtable
->used
;
13894 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13897 const struct elf_backend_data
*bed
;
13898 unsigned int log_file_align
;
13900 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13901 log_file_align
= bed
->s
->log_file_align
;
13902 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
13916 struct link_info_ok
13918 struct bfd_link_info
*info
;
13923 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
,
13927 bfd_vma hstart
, hend
;
13928 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13929 const struct elf_backend_data
*bed
;
13930 unsigned int log_file_align
;
13931 struct link_info_ok
*info
= (struct link_info_ok
*) ptr
;
13933 /* Take care of both those symbols that do not describe vtables as
13934 well as those that are not loaded. */
13936 || h
->u2
.vtable
== NULL
13937 || h
->u2
.vtable
->parent
== NULL
)
13940 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13941 || h
->root
.type
== bfd_link_hash_defweak
);
13943 sec
= h
->root
.u
.def
.section
;
13944 hstart
= h
->root
.u
.def
.value
;
13945 hend
= hstart
+ h
->size
;
13947 relstart
= _bfd_elf_link_info_read_relocs (sec
->owner
, info
->info
,
13948 sec
, NULL
, NULL
, true);
13950 return info
->ok
= false;
13951 bed
= get_elf_backend_data (sec
->owner
);
13952 log_file_align
= bed
->s
->log_file_align
;
13954 relend
= relstart
+ sec
->reloc_count
;
13956 for (rel
= relstart
; rel
< relend
; ++rel
)
13957 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13959 /* If the entry is in use, do nothing. */
13960 if (h
->u2
.vtable
->used
13961 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
13963 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13964 if (h
->u2
.vtable
->used
[entry
])
13967 /* Otherwise, kill it. */
13968 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13974 /* Mark sections containing dynamically referenced symbols. When
13975 building shared libraries, we must assume that any visible symbol is
13979 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13981 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13982 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13984 if ((h
->root
.type
== bfd_link_hash_defined
13985 || h
->root
.type
== bfd_link_hash_defweak
)
13987 || h
->root
.ldscript_def
13988 || !info
->start_stop_gc
)
13989 && ((h
->ref_dynamic
&& !h
->forced_local
)
13990 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13991 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13992 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13993 && (!bfd_link_executable (info
)
13994 || info
->gc_keep_exported
13995 || info
->export_dynamic
13998 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13999 && (h
->versioned
>= versioned
14000 || !bfd_hide_sym_by_version (info
->version_info
,
14001 h
->root
.root
.string
)))))
14002 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14007 /* Keep all sections containing symbols undefined on the command-line,
14008 and the section containing the entry symbol. */
14011 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
14013 struct bfd_sym_chain
*sym
;
14015 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
14017 struct elf_link_hash_entry
*h
;
14019 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
14020 false, false, false);
14023 && (h
->root
.type
== bfd_link_hash_defined
14024 || h
->root
.type
== bfd_link_hash_defweak
)
14025 && !bfd_is_const_section (h
->root
.u
.def
.section
))
14026 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14031 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
14032 struct bfd_link_info
*info
)
14034 bfd
*ibfd
= info
->input_bfds
;
14036 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14039 struct elf_reloc_cookie cookie
;
14041 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14043 sec
= ibfd
->sections
;
14044 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14047 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
14050 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
14052 if (startswith (bfd_section_name (sec
), ".eh_frame_entry")
14053 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
14055 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
14056 fini_reloc_cookie_rels (&cookie
, sec
);
14063 /* Do mark and sweep of unused sections. */
14066 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14070 elf_gc_mark_hook_fn gc_mark_hook
;
14071 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14072 struct elf_link_hash_table
*htab
;
14073 struct link_info_ok info_ok
;
14075 if (!bed
->can_gc_sections
14076 || !is_elf_hash_table (info
->hash
))
14078 _bfd_error_handler(_("warning: gc-sections option ignored"));
14082 bed
->gc_keep (info
);
14083 htab
= elf_hash_table (info
);
14085 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14086 at the .eh_frame section if we can mark the FDEs individually. */
14087 for (sub
= info
->input_bfds
;
14088 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14089 sub
= sub
->link
.next
)
14092 struct elf_reloc_cookie cookie
;
14094 sec
= sub
->sections
;
14095 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14097 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14098 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
14100 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14101 if (elf_section_data (sec
)->sec_info
14102 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14103 elf_eh_frame_section (sub
) = sec
;
14104 fini_reloc_cookie_for_section (&cookie
, sec
);
14105 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14109 /* Apply transitive closure to the vtable entry usage info. */
14110 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14114 /* Kill the vtable relocations that were not used. */
14115 info_ok
.info
= info
;
14117 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &info_ok
);
14121 /* Mark dynamically referenced symbols. */
14122 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14123 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14125 /* Grovel through relocs to find out who stays ... */
14126 gc_mark_hook
= bed
->gc_mark_hook
;
14127 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14131 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14132 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14133 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14137 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14140 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14141 Also treat note sections as a root, if the section is not part
14142 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14143 well as FINI_ARRAY sections for ld -r. */
14144 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14146 && (o
->flags
& SEC_EXCLUDE
) == 0
14147 && ((o
->flags
& SEC_KEEP
) != 0
14148 || (bfd_link_relocatable (info
)
14149 && ((elf_section_data (o
)->this_hdr
.sh_type
14150 == SHT_PREINIT_ARRAY
)
14151 || (elf_section_data (o
)->this_hdr
.sh_type
14153 || (elf_section_data (o
)->this_hdr
.sh_type
14154 == SHT_FINI_ARRAY
)))
14155 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14156 && elf_next_in_group (o
) == NULL
14157 && elf_linked_to_section (o
) == NULL
)
14158 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14159 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14161 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14166 /* Allow the backend to mark additional target specific sections. */
14167 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14169 /* ... and mark SEC_EXCLUDE for those that go. */
14170 return elf_gc_sweep (abfd
, info
);
14173 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14176 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14178 struct elf_link_hash_entry
*h
,
14181 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14182 struct elf_link_hash_entry
**search
, *child
;
14183 size_t extsymcount
;
14184 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14186 /* The sh_info field of the symtab header tells us where the
14187 external symbols start. We don't care about the local symbols at
14189 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14190 if (!elf_bad_symtab (abfd
))
14191 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14193 sym_hashes
= elf_sym_hashes (abfd
);
14194 sym_hashes_end
= sym_hashes
+ extsymcount
;
14196 /* Hunt down the child symbol, which is in this section at the same
14197 offset as the relocation. */
14198 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14200 if ((child
= *search
) != NULL
14201 && (child
->root
.type
== bfd_link_hash_defined
14202 || child
->root
.type
== bfd_link_hash_defweak
)
14203 && child
->root
.u
.def
.section
== sec
14204 && child
->root
.u
.def
.value
== offset
)
14208 /* xgettext:c-format */
14209 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14210 abfd
, sec
, (uint64_t) offset
);
14211 bfd_set_error (bfd_error_invalid_operation
);
14215 if (!child
->u2
.vtable
)
14217 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14218 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14219 if (!child
->u2
.vtable
)
14224 /* This *should* only be the absolute section. It could potentially
14225 be that someone has defined a non-global vtable though, which
14226 would be bad. It isn't worth paging in the local symbols to be
14227 sure though; that case should simply be handled by the assembler. */
14229 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14232 child
->u2
.vtable
->parent
= h
;
14237 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14240 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14241 struct elf_link_hash_entry
*h
,
14244 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14245 unsigned int log_file_align
= bed
->s
->log_file_align
;
14249 /* xgettext:c-format */
14250 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14252 bfd_set_error (bfd_error_bad_value
);
14258 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14259 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14264 if (addend
>= h
->u2
.vtable
->size
)
14266 size_t size
, bytes
, file_align
;
14267 bool *ptr
= h
->u2
.vtable
->used
;
14269 /* While the symbol is undefined, we have to be prepared to handle
14271 file_align
= 1 << log_file_align
;
14272 if (h
->root
.type
== bfd_link_hash_undefined
)
14273 size
= addend
+ file_align
;
14277 if (addend
>= size
)
14279 /* Oops! We've got a reference past the defined end of
14280 the table. This is probably a bug -- shall we warn? */
14281 size
= addend
+ file_align
;
14284 size
= (size
+ file_align
- 1) & -file_align
;
14286 /* Allocate one extra entry for use as a "done" flag for the
14287 consolidation pass. */
14288 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bool);
14292 ptr
= (bool *) bfd_realloc (ptr
- 1, bytes
);
14298 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14300 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14304 ptr
= (bool *) bfd_zmalloc (bytes
);
14309 /* And arrange for that done flag to be at index -1. */
14310 h
->u2
.vtable
->used
= ptr
+ 1;
14311 h
->u2
.vtable
->size
= size
;
14314 h
->u2
.vtable
->used
[addend
>> log_file_align
] = true;
14319 /* Map an ELF section header flag to its corresponding string. */
14323 flagword flag_value
;
14324 } elf_flags_to_name_table
;
14326 static const elf_flags_to_name_table elf_flags_to_names
[] =
14328 { "SHF_WRITE", SHF_WRITE
},
14329 { "SHF_ALLOC", SHF_ALLOC
},
14330 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14331 { "SHF_MERGE", SHF_MERGE
},
14332 { "SHF_STRINGS", SHF_STRINGS
},
14333 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14334 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14335 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14336 { "SHF_GROUP", SHF_GROUP
},
14337 { "SHF_TLS", SHF_TLS
},
14338 { "SHF_MASKOS", SHF_MASKOS
},
14339 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14342 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14344 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14345 struct flag_info
*flaginfo
,
14348 const bfd_vma sh_flags
= elf_section_flags (section
);
14350 if (!flaginfo
->flags_initialized
)
14352 bfd
*obfd
= info
->output_bfd
;
14353 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14354 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14356 int without_hex
= 0;
14358 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14361 flagword (*lookup
) (char *);
14363 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14364 if (lookup
!= NULL
)
14366 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14370 if (tf
->with
== with_flags
)
14371 with_hex
|= hexval
;
14372 else if (tf
->with
== without_flags
)
14373 without_hex
|= hexval
;
14378 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14380 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14382 if (tf
->with
== with_flags
)
14383 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14384 else if (tf
->with
== without_flags
)
14385 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14392 info
->callbacks
->einfo
14393 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14397 flaginfo
->flags_initialized
= true;
14398 flaginfo
->only_with_flags
|= with_hex
;
14399 flaginfo
->not_with_flags
|= without_hex
;
14402 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14405 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14411 struct alloc_got_off_arg
{
14413 struct bfd_link_info
*info
;
14416 /* We need a special top-level link routine to convert got reference counts
14417 to real got offsets. */
14420 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14422 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14423 bfd
*obfd
= gofarg
->info
->output_bfd
;
14424 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14426 if (h
->got
.refcount
> 0)
14428 h
->got
.offset
= gofarg
->gotoff
;
14429 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14432 h
->got
.offset
= (bfd_vma
) -1;
14437 /* And an accompanying bit to work out final got entry offsets once
14438 we're done. Should be called from final_link. */
14441 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14442 struct bfd_link_info
*info
)
14445 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14447 struct alloc_got_off_arg gofarg
;
14449 BFD_ASSERT (abfd
== info
->output_bfd
);
14451 if (! is_elf_hash_table (info
->hash
))
14454 /* The GOT offset is relative to the .got section, but the GOT header is
14455 put into the .got.plt section, if the backend uses it. */
14456 if (bed
->want_got_plt
)
14459 gotoff
= bed
->got_header_size
;
14461 /* Do the local .got entries first. */
14462 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14464 bfd_signed_vma
*local_got
;
14465 size_t j
, locsymcount
;
14466 Elf_Internal_Shdr
*symtab_hdr
;
14468 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14471 local_got
= elf_local_got_refcounts (i
);
14475 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14476 if (elf_bad_symtab (i
))
14477 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14479 locsymcount
= symtab_hdr
->sh_info
;
14481 for (j
= 0; j
< locsymcount
; ++j
)
14483 if (local_got
[j
] > 0)
14485 local_got
[j
] = gotoff
;
14486 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14489 local_got
[j
] = (bfd_vma
) -1;
14493 /* Then the global .got entries. .plt refcounts are handled by
14494 adjust_dynamic_symbol */
14495 gofarg
.gotoff
= gotoff
;
14496 gofarg
.info
= info
;
14497 elf_link_hash_traverse (elf_hash_table (info
),
14498 elf_gc_allocate_got_offsets
,
14503 /* Many folk need no more in the way of final link than this, once
14504 got entry reference counting is enabled. */
14507 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14509 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14512 /* Invoke the regular ELF backend linker to do all the work. */
14513 return bfd_elf_final_link (abfd
, info
);
14517 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14519 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14521 if (rcookie
->bad_symtab
)
14522 rcookie
->rel
= rcookie
->rels
;
14524 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14526 unsigned long r_symndx
;
14528 if (! rcookie
->bad_symtab
)
14529 if (rcookie
->rel
->r_offset
> offset
)
14531 if (rcookie
->rel
->r_offset
!= offset
)
14534 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14535 if (r_symndx
== STN_UNDEF
)
14538 if (r_symndx
>= rcookie
->locsymcount
14539 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14541 struct elf_link_hash_entry
*h
;
14543 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14545 while (h
->root
.type
== bfd_link_hash_indirect
14546 || h
->root
.type
== bfd_link_hash_warning
)
14547 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14549 if ((h
->root
.type
== bfd_link_hash_defined
14550 || h
->root
.type
== bfd_link_hash_defweak
)
14551 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14552 || h
->root
.u
.def
.section
->kept_section
!= NULL
14553 || discarded_section (h
->root
.u
.def
.section
)))
14558 /* It's not a relocation against a global symbol,
14559 but it could be a relocation against a local
14560 symbol for a discarded section. */
14562 Elf_Internal_Sym
*isym
;
14564 /* Need to: get the symbol; get the section. */
14565 isym
= &rcookie
->locsyms
[r_symndx
];
14566 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14568 && (isec
->kept_section
!= NULL
14569 || discarded_section (isec
)))
14577 /* Discard unneeded references to discarded sections.
14578 Returns -1 on error, 1 if any section's size was changed, 0 if
14579 nothing changed. This function assumes that the relocations are in
14580 sorted order, which is true for all known assemblers. */
14583 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14585 struct elf_reloc_cookie cookie
;
14590 if (info
->traditional_format
14591 || !is_elf_hash_table (info
->hash
))
14594 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14599 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14602 || i
->reloc_count
== 0
14603 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14607 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14610 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14613 if (_bfd_discard_section_stabs (abfd
, i
,
14614 elf_section_data (i
)->sec_info
,
14615 bfd_elf_reloc_symbol_deleted_p
,
14619 fini_reloc_cookie_for_section (&cookie
, i
);
14624 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14625 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14629 int eh_changed
= 0;
14630 unsigned int eh_alignment
; /* Octets. */
14632 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14638 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14641 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14644 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14645 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14646 bfd_elf_reloc_symbol_deleted_p
,
14650 if (i
->size
!= i
->rawsize
)
14654 fini_reloc_cookie_for_section (&cookie
, i
);
14657 eh_alignment
= ((1 << o
->alignment_power
)
14658 * bfd_octets_per_byte (output_bfd
, o
));
14659 /* Skip over zero terminator, and prevent empty sections from
14660 adding alignment padding at the end. */
14661 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14663 i
->flags
|= SEC_EXCLUDE
;
14664 else if (i
->size
> 4)
14666 /* The last non-empty eh_frame section doesn't need padding. */
14669 /* Any prior sections must pad the last FDE out to the output
14670 section alignment. Otherwise we might have zero padding
14671 between sections, which would be seen as a terminator. */
14672 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14674 /* All but the last zero terminator should have been removed. */
14679 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14680 if (i
->size
!= size
)
14688 elf_link_hash_traverse (elf_hash_table (info
),
14689 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14692 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14694 const struct elf_backend_data
*bed
;
14697 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14699 s
= abfd
->sections
;
14700 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14703 bed
= get_elf_backend_data (abfd
);
14705 if (bed
->elf_backend_discard_info
!= NULL
)
14707 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14710 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14713 fini_reloc_cookie (&cookie
, abfd
);
14717 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14718 _bfd_elf_end_eh_frame_parsing (info
);
14720 if (info
->eh_frame_hdr_type
14721 && !bfd_link_relocatable (info
)
14722 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
14729 _bfd_elf_section_already_linked (bfd
*abfd
,
14731 struct bfd_link_info
*info
)
14734 const char *name
, *key
;
14735 struct bfd_section_already_linked
*l
;
14736 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14738 if (sec
->output_section
== bfd_abs_section_ptr
)
14741 flags
= sec
->flags
;
14743 /* Return if it isn't a linkonce section. A comdat group section
14744 also has SEC_LINK_ONCE set. */
14745 if ((flags
& SEC_LINK_ONCE
) == 0)
14748 /* Don't put group member sections on our list of already linked
14749 sections. They are handled as a group via their group section. */
14750 if (elf_sec_group (sec
) != NULL
)
14753 /* For a SHT_GROUP section, use the group signature as the key. */
14755 if ((flags
& SEC_GROUP
) != 0
14756 && elf_next_in_group (sec
) != NULL
14757 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14758 key
= elf_group_name (elf_next_in_group (sec
));
14761 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14762 if (startswith (name
, ".gnu.linkonce.")
14763 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14766 /* Must be a user linkonce section that doesn't follow gcc's
14767 naming convention. In this case we won't be matching
14768 single member groups. */
14772 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14774 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14776 /* We may have 2 different types of sections on the list: group
14777 sections with a signature of <key> (<key> is some string),
14778 and linkonce sections named .gnu.linkonce.<type>.<key>.
14779 Match like sections. LTO plugin sections are an exception.
14780 They are always named .gnu.linkonce.t.<key> and match either
14781 type of section. */
14782 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14783 && ((flags
& SEC_GROUP
) != 0
14784 || strcmp (name
, l
->sec
->name
) == 0))
14785 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
14786 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14788 /* The section has already been linked. See if we should
14789 issue a warning. */
14790 if (!_bfd_handle_already_linked (sec
, l
, info
))
14793 if (flags
& SEC_GROUP
)
14795 asection
*first
= elf_next_in_group (sec
);
14796 asection
*s
= first
;
14800 s
->output_section
= bfd_abs_section_ptr
;
14801 /* Record which group discards it. */
14802 s
->kept_section
= l
->sec
;
14803 s
= elf_next_in_group (s
);
14804 /* These lists are circular. */
14814 /* A single member comdat group section may be discarded by a
14815 linkonce section and vice versa. */
14816 if ((flags
& SEC_GROUP
) != 0)
14818 asection
*first
= elf_next_in_group (sec
);
14820 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14821 /* Check this single member group against linkonce sections. */
14822 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14823 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14824 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14826 first
->output_section
= bfd_abs_section_ptr
;
14827 first
->kept_section
= l
->sec
;
14828 sec
->output_section
= bfd_abs_section_ptr
;
14833 /* Check this linkonce section against single member groups. */
14834 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14835 if (l
->sec
->flags
& SEC_GROUP
)
14837 asection
*first
= elf_next_in_group (l
->sec
);
14840 && elf_next_in_group (first
) == first
14841 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
14843 sec
->output_section
= bfd_abs_section_ptr
;
14844 sec
->kept_section
= first
;
14849 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14850 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14851 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14852 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14853 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14854 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14855 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14856 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14857 The reverse order cannot happen as there is never a bfd with only the
14858 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14859 matter as here were are looking only for cross-bfd sections. */
14861 if ((flags
& SEC_GROUP
) == 0 && startswith (name
, ".gnu.linkonce.r."))
14862 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14863 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14864 && startswith (l
->sec
->name
, ".gnu.linkonce.t."))
14866 if (abfd
!= l
->sec
->owner
)
14867 sec
->output_section
= bfd_abs_section_ptr
;
14871 /* This is the first section with this name. Record it. */
14872 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
14873 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
14874 return sec
->output_section
== bfd_abs_section_ptr
;
14878 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
14880 return sym
->st_shndx
== SHN_COMMON
;
14884 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14890 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14892 return bfd_com_section_ptr
;
14896 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14897 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14898 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14899 bfd
*ibfd ATTRIBUTE_UNUSED
,
14900 unsigned long symndx ATTRIBUTE_UNUSED
)
14902 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14903 return bed
->s
->arch_size
/ 8;
14906 /* Routines to support the creation of dynamic relocs. */
14908 /* Returns the name of the dynamic reloc section associated with SEC. */
14910 static const char *
14911 get_dynamic_reloc_section_name (bfd
* abfd
,
14916 const char *old_name
= bfd_section_name (sec
);
14917 const char *prefix
= is_rela
? ".rela" : ".rel";
14919 if (old_name
== NULL
)
14922 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14923 sprintf (name
, "%s%s", prefix
, old_name
);
14928 /* Returns the dynamic reloc section associated with SEC.
14929 If necessary compute the name of the dynamic reloc section based
14930 on SEC's name (looked up in ABFD's string table) and the setting
14934 _bfd_elf_get_dynamic_reloc_section (bfd
*abfd
,
14938 asection
*reloc_sec
= elf_section_data (sec
)->sreloc
;
14940 if (reloc_sec
== NULL
)
14942 const char *name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14946 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14948 if (reloc_sec
!= NULL
)
14949 elf_section_data (sec
)->sreloc
= reloc_sec
;
14956 /* Returns the dynamic reloc section associated with SEC. If the
14957 section does not exist it is created and attached to the DYNOBJ
14958 bfd and stored in the SRELOC field of SEC's elf_section_data
14961 ALIGNMENT is the alignment for the newly created section and
14962 IS_RELA defines whether the name should be .rela.<SEC's name>
14963 or .rel.<SEC's name>. The section name is looked up in the
14964 string table associated with ABFD. */
14967 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
14969 unsigned int alignment
,
14973 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14975 if (reloc_sec
== NULL
)
14977 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14982 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
14984 if (reloc_sec
== NULL
)
14986 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
14987 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
14988 if ((sec
->flags
& SEC_ALLOC
) != 0)
14989 flags
|= SEC_ALLOC
| SEC_LOAD
;
14991 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
14992 if (reloc_sec
!= NULL
)
14994 /* _bfd_elf_get_sec_type_attr chooses a section type by
14995 name. Override as it may be wrong, eg. for a user
14996 section named "auto" we'll get ".relauto" which is
14997 seen to be a .rela section. */
14998 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
14999 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
15004 elf_section_data (sec
)->sreloc
= reloc_sec
;
15010 /* Copy the ELF symbol type and other attributes for a linker script
15011 assignment from HSRC to HDEST. Generally this should be treated as
15012 if we found a strong non-dynamic definition for HDEST (except that
15013 ld ignores multiple definition errors). */
15015 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
15016 struct bfd_link_hash_entry
*hdest
,
15017 struct bfd_link_hash_entry
*hsrc
)
15019 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
15020 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
15021 Elf_Internal_Sym isym
;
15023 ehdest
->type
= ehsrc
->type
;
15024 ehdest
->target_internal
= ehsrc
->target_internal
;
15026 isym
.st_other
= ehsrc
->other
;
15027 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, true, false);
15030 /* Append a RELA relocation REL to section S in BFD. */
15033 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15035 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15036 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
15037 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
15038 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
15041 /* Append a REL relocation REL to section S in BFD. */
15044 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15046 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15047 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
15048 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
15049 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
15052 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15054 struct bfd_link_hash_entry
*
15055 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
15056 const char *symbol
, asection
*sec
)
15058 struct elf_link_hash_entry
*h
;
15060 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
15061 false, false, true);
15062 /* NB: Common symbols will be turned into definition later. */
15064 && !h
->root
.ldscript_def
15065 && (h
->root
.type
== bfd_link_hash_undefined
15066 || h
->root
.type
== bfd_link_hash_undefweak
15067 || ((h
->ref_regular
|| h
->def_dynamic
)
15069 && h
->root
.type
!= bfd_link_hash_common
)))
15071 bool was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15072 h
->verinfo
.verdef
= NULL
;
15073 h
->root
.type
= bfd_link_hash_defined
;
15074 h
->root
.u
.def
.section
= sec
;
15075 h
->root
.u
.def
.value
= 0;
15076 h
->def_regular
= 1;
15077 h
->def_dynamic
= 0;
15079 h
->u2
.start_stop_section
= sec
;
15080 if (symbol
[0] == '.')
15082 /* .startof. and .sizeof. symbols are local. */
15083 const struct elf_backend_data
*bed
;
15084 bed
= get_elf_backend_data (info
->output_bfd
);
15085 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
15089 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15090 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15091 | info
->start_stop_visibility
);
15093 bfd_elf_link_record_dynamic_symbol (info
, h
);
15100 /* Find dynamic relocs for H that apply to read-only sections. */
15103 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15105 struct elf_dyn_relocs
*p
;
15107 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15109 asection
*s
= p
->sec
->output_section
;
15111 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15117 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15118 read-only sections. */
15121 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15125 if (h
->root
.type
== bfd_link_hash_indirect
)
15128 sec
= _bfd_elf_readonly_dynrelocs (h
);
15131 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15133 info
->flags
|= DF_TEXTREL
;
15134 /* xgettext:c-format */
15135 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15136 "in read-only section `%pA'\n"),
15137 sec
->owner
, h
->root
.root
.string
, sec
);
15139 if (bfd_link_textrel_check (info
))
15140 /* xgettext:c-format */
15141 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15142 "in read-only section `%pA'\n"),
15143 sec
->owner
, h
->root
.root
.string
, sec
);
15145 /* Not an error, just cut short the traversal. */
15151 /* Add dynamic tags. */
15154 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15155 bool need_dynamic_reloc
)
15157 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15159 if (htab
->dynamic_sections_created
)
15161 /* Add some entries to the .dynamic section. We fill in the
15162 values later, in finish_dynamic_sections, but we must add
15163 the entries now so that we get the correct size for the
15164 .dynamic section. The DT_DEBUG entry is filled in by the
15165 dynamic linker and used by the debugger. */
15166 #define add_dynamic_entry(TAG, VAL) \
15167 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15169 const struct elf_backend_data
*bed
15170 = get_elf_backend_data (output_bfd
);
15172 if (bfd_link_executable (info
))
15174 if (!add_dynamic_entry (DT_DEBUG
, 0))
15178 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15180 /* DT_PLTGOT is used by prelink even if there is no PLT
15182 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15186 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15188 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15189 || !add_dynamic_entry (DT_PLTREL
,
15190 (bed
->rela_plts_and_copies_p
15191 ? DT_RELA
: DT_REL
))
15192 || !add_dynamic_entry (DT_JMPREL
, 0))
15196 if (htab
->tlsdesc_plt
15197 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15198 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15201 if (need_dynamic_reloc
)
15203 if (bed
->rela_plts_and_copies_p
)
15205 if (!add_dynamic_entry (DT_RELA
, 0)
15206 || !add_dynamic_entry (DT_RELASZ
, 0)
15207 || !add_dynamic_entry (DT_RELAENT
,
15208 bed
->s
->sizeof_rela
))
15213 if (!add_dynamic_entry (DT_REL
, 0)
15214 || !add_dynamic_entry (DT_RELSZ
, 0)
15215 || !add_dynamic_entry (DT_RELENT
,
15216 bed
->s
->sizeof_rel
))
15220 /* If any dynamic relocs apply to a read-only section,
15221 then we need a DT_TEXTREL entry. */
15222 if ((info
->flags
& DF_TEXTREL
) == 0)
15223 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15226 if ((info
->flags
& DF_TEXTREL
) != 0)
15228 if (htab
->ifunc_resolvers
)
15229 info
->callbacks
->einfo
15230 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15231 "may result in a segfault at runtime; recompile with %s\n"),
15232 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15234 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15239 #undef add_dynamic_entry