1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2022 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;
1276 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
))
1278 if (newdyn
!= olddyn
)
1280 /* Handle a case where plugin_notice won't be called and thus
1281 won't set the non_ir_ref flags on the first pass over
1283 h
->root
.non_ir_ref_dynamic
= true;
1284 hi
->root
.non_ir_ref_dynamic
= true;
1287 if ((oldbfd
->flags
& BFD_PLUGIN
) != 0
1288 && hi
->root
.type
== bfd_link_hash_indirect
)
1290 /* Change indirect symbol from IR to undefined. */
1291 hi
->root
.type
= bfd_link_hash_undefined
;
1292 hi
->root
.u
.undef
.abfd
= oldbfd
;
1296 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1297 respectively, appear to be a definition rather than reference. */
1299 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1301 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1302 && h
->root
.type
!= bfd_link_hash_undefweak
1303 && h
->root
.type
!= bfd_link_hash_common
);
1305 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1306 respectively, appear to be a function. */
1308 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1309 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1311 oldfunc
= (h
->type
!= STT_NOTYPE
1312 && bed
->is_function_type (h
->type
));
1314 if (!(newfunc
&& oldfunc
)
1315 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1316 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1317 && h
->type
!= STT_NOTYPE
1318 && (newdef
|| bfd_is_com_section (sec
))
1319 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1321 /* If creating a default indirect symbol ("foo" or "foo@") from
1322 a dynamic versioned definition ("foo@@") skip doing so if
1323 there is an existing regular definition with a different
1324 type. We don't want, for example, a "time" variable in the
1325 executable overriding a "time" function in a shared library. */
1333 /* When adding a symbol from a regular object file after we have
1334 created indirect symbols, undo the indirection and any
1341 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1342 h
->forced_local
= 0;
1346 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1348 h
->root
.type
= bfd_link_hash_undefined
;
1349 h
->root
.u
.undef
.abfd
= abfd
;
1353 h
->root
.type
= bfd_link_hash_new
;
1354 h
->root
.u
.undef
.abfd
= NULL
;
1360 /* Check TLS symbols. We don't check undefined symbols introduced
1361 by "ld -u" which have no type (and oldbfd NULL), and we don't
1362 check symbols from plugins because they also have no type. */
1364 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1365 && (abfd
->flags
& BFD_PLUGIN
) == 0
1366 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1367 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1371 asection
*ntsec
, *tsec
;
1373 if (h
->type
== STT_TLS
)
1394 /* xgettext:c-format */
1395 (_("%s: TLS definition in %pB section %pA "
1396 "mismatches non-TLS definition in %pB section %pA"),
1397 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1398 else if (!tdef
&& !ntdef
)
1400 /* xgettext:c-format */
1401 (_("%s: TLS reference in %pB "
1402 "mismatches non-TLS reference in %pB"),
1403 h
->root
.root
.string
, tbfd
, ntbfd
);
1406 /* xgettext:c-format */
1407 (_("%s: TLS definition in %pB section %pA "
1408 "mismatches non-TLS reference in %pB"),
1409 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1412 /* xgettext:c-format */
1413 (_("%s: TLS reference in %pB "
1414 "mismatches non-TLS definition in %pB section %pA"),
1415 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1417 bfd_set_error (bfd_error_bad_value
);
1421 /* If the old symbol has non-default visibility, we ignore the new
1422 definition from a dynamic object. */
1424 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1425 && !bfd_is_und_section (sec
))
1428 /* Make sure this symbol is dynamic. */
1430 hi
->ref_dynamic
= 1;
1431 /* A protected symbol has external availability. Make sure it is
1432 recorded as dynamic.
1434 FIXME: Should we check type and size for protected symbol? */
1435 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1436 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1441 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1444 /* If the new symbol with non-default visibility comes from a
1445 relocatable file and the old definition comes from a dynamic
1446 object, we remove the old definition. */
1447 if (hi
->root
.type
== bfd_link_hash_indirect
)
1449 /* Handle the case where the old dynamic definition is
1450 default versioned. We need to copy the symbol info from
1451 the symbol with default version to the normal one if it
1452 was referenced before. */
1455 hi
->root
.type
= h
->root
.type
;
1456 h
->root
.type
= bfd_link_hash_indirect
;
1457 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1459 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1460 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1462 /* If the new symbol is hidden or internal, completely undo
1463 any dynamic link state. */
1464 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1465 h
->forced_local
= 0;
1472 /* FIXME: Should we check type and size for protected symbol? */
1482 /* If the old symbol was undefined before, then it will still be
1483 on the undefs list. If the new symbol is undefined or
1484 common, we can't make it bfd_link_hash_new here, because new
1485 undefined or common symbols will be added to the undefs list
1486 by _bfd_generic_link_add_one_symbol. Symbols may not be
1487 added twice to the undefs list. Also, if the new symbol is
1488 undefweak then we don't want to lose the strong undef. */
1489 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1491 h
->root
.type
= bfd_link_hash_undefined
;
1492 h
->root
.u
.undef
.abfd
= abfd
;
1496 h
->root
.type
= bfd_link_hash_new
;
1497 h
->root
.u
.undef
.abfd
= NULL
;
1500 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1502 /* If the new symbol is hidden or internal, completely undo
1503 any dynamic link state. */
1504 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1505 h
->forced_local
= 0;
1511 /* FIXME: Should we check type and size for protected symbol? */
1517 /* If a new weak symbol definition comes from a regular file and the
1518 old symbol comes from a dynamic library, we treat the new one as
1519 strong. Similarly, an old weak symbol definition from a regular
1520 file is treated as strong when the new symbol comes from a dynamic
1521 library. Further, an old weak symbol from a dynamic library is
1522 treated as strong if the new symbol is from a dynamic library.
1523 This reflects the way glibc's ld.so works.
1525 Also allow a weak symbol to override a linker script symbol
1526 defined by an early pass over the script. This is done so the
1527 linker knows the symbol is defined in an object file, for the
1528 DEFINED script function.
1530 Do this before setting *type_change_ok or *size_change_ok so that
1531 we warn properly when dynamic library symbols are overridden. */
1533 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1535 if (olddef
&& newdyn
)
1538 /* Allow changes between different types of function symbol. */
1539 if (newfunc
&& oldfunc
)
1540 *type_change_ok
= true;
1542 /* It's OK to change the type if either the existing symbol or the
1543 new symbol is weak. A type change is also OK if the old symbol
1544 is undefined and the new symbol is defined. */
1549 && h
->root
.type
== bfd_link_hash_undefined
))
1550 *type_change_ok
= true;
1552 /* It's OK to change the size if either the existing symbol or the
1553 new symbol is weak, or if the old symbol is undefined. */
1556 || h
->root
.type
== bfd_link_hash_undefined
)
1557 *size_change_ok
= true;
1559 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1560 symbol, respectively, appears to be a common symbol in a dynamic
1561 object. If a symbol appears in an uninitialized section, and is
1562 not weak, and is not a function, then it may be a common symbol
1563 which was resolved when the dynamic object was created. We want
1564 to treat such symbols specially, because they raise special
1565 considerations when setting the symbol size: if the symbol
1566 appears as a common symbol in a regular object, and the size in
1567 the regular object is larger, we must make sure that we use the
1568 larger size. This problematic case can always be avoided in C,
1569 but it must be handled correctly when using Fortran shared
1572 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1573 likewise for OLDDYNCOMMON and OLDDEF.
1575 Note that this test is just a heuristic, and that it is quite
1576 possible to have an uninitialized symbol in a shared object which
1577 is really a definition, rather than a common symbol. This could
1578 lead to some minor confusion when the symbol really is a common
1579 symbol in some regular object. However, I think it will be
1585 && (sec
->flags
& SEC_ALLOC
) != 0
1586 && (sec
->flags
& SEC_LOAD
) == 0
1589 newdyncommon
= true;
1591 newdyncommon
= false;
1595 && h
->root
.type
== bfd_link_hash_defined
1597 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1598 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1601 olddyncommon
= true;
1603 olddyncommon
= false;
1605 /* We now know everything about the old and new symbols. We ask the
1606 backend to check if we can merge them. */
1607 if (bed
->merge_symbol
!= NULL
)
1609 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1614 /* There are multiple definitions of a normal symbol. Skip the
1615 default symbol as well as definition from an IR object. */
1616 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1617 && !default_sym
&& h
->def_regular
1619 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1620 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1622 /* Handle a multiple definition. */
1623 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1624 abfd
, sec
, *pvalue
);
1629 /* If both the old and the new symbols look like common symbols in a
1630 dynamic object, set the size of the symbol to the larger of the
1635 && sym
->st_size
!= h
->size
)
1637 /* Since we think we have two common symbols, issue a multiple
1638 common warning if desired. Note that we only warn if the
1639 size is different. If the size is the same, we simply let
1640 the old symbol override the new one as normally happens with
1641 symbols defined in dynamic objects. */
1643 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1644 bfd_link_hash_common
, sym
->st_size
);
1645 if (sym
->st_size
> h
->size
)
1646 h
->size
= sym
->st_size
;
1648 *size_change_ok
= true;
1651 /* If we are looking at a dynamic object, and we have found a
1652 definition, we need to see if the symbol was already defined by
1653 some other object. If so, we want to use the existing
1654 definition, and we do not want to report a multiple symbol
1655 definition error; we do this by clobbering *PSEC to be
1656 bfd_und_section_ptr.
1658 We treat a common symbol as a definition if the symbol in the
1659 shared library is a function, since common symbols always
1660 represent variables; this can cause confusion in principle, but
1661 any such confusion would seem to indicate an erroneous program or
1662 shared library. We also permit a common symbol in a regular
1663 object to override a weak symbol in a shared object. */
1668 || (h
->root
.type
== bfd_link_hash_common
1669 && (newweak
|| newfunc
))))
1673 newdyncommon
= false;
1675 *psec
= sec
= bfd_und_section_ptr
;
1676 *size_change_ok
= true;
1678 /* If we get here when the old symbol is a common symbol, then
1679 we are explicitly letting it override a weak symbol or
1680 function in a dynamic object, and we don't want to warn about
1681 a type change. If the old symbol is a defined symbol, a type
1682 change warning may still be appropriate. */
1684 if (h
->root
.type
== bfd_link_hash_common
)
1685 *type_change_ok
= true;
1688 /* Handle the special case of an old common symbol merging with a
1689 new symbol which looks like a common symbol in a shared object.
1690 We change *PSEC and *PVALUE to make the new symbol look like a
1691 common symbol, and let _bfd_generic_link_add_one_symbol do the
1695 && h
->root
.type
== bfd_link_hash_common
)
1699 newdyncommon
= false;
1700 *pvalue
= sym
->st_size
;
1701 *psec
= sec
= bed
->common_section (oldsec
);
1702 *size_change_ok
= true;
1705 /* Skip weak definitions of symbols that are already defined. */
1706 if (newdef
&& olddef
&& newweak
)
1708 /* Don't skip new non-IR weak syms. */
1709 if (!(oldbfd
!= NULL
1710 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1711 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1717 /* Merge st_other. If the symbol already has a dynamic index,
1718 but visibility says it should not be visible, turn it into a
1720 elf_merge_st_other (abfd
, h
, sym
->st_other
, sec
, newdef
, newdyn
);
1721 if (h
->dynindx
!= -1)
1722 switch (ELF_ST_VISIBILITY (h
->other
))
1726 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1731 /* If the old symbol is from a dynamic object, and the new symbol is
1732 a definition which is not from a dynamic object, then the new
1733 symbol overrides the old symbol. Symbols from regular files
1734 always take precedence over symbols from dynamic objects, even if
1735 they are defined after the dynamic object in the link.
1737 As above, we again permit a common symbol in a regular object to
1738 override a definition in a shared object if the shared object
1739 symbol is a function or is weak. */
1744 || (bfd_is_com_section (sec
)
1745 && (oldweak
|| oldfunc
)))
1750 /* Change the hash table entry to undefined, and let
1751 _bfd_generic_link_add_one_symbol do the right thing with the
1754 h
->root
.type
= bfd_link_hash_undefined
;
1755 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1756 *size_change_ok
= true;
1759 olddyncommon
= false;
1761 /* We again permit a type change when a common symbol may be
1762 overriding a function. */
1764 if (bfd_is_com_section (sec
))
1768 /* If a common symbol overrides a function, make sure
1769 that it isn't defined dynamically nor has type
1772 h
->type
= STT_NOTYPE
;
1774 *type_change_ok
= true;
1777 if (hi
->root
.type
== bfd_link_hash_indirect
)
1780 /* This union may have been set to be non-NULL when this symbol
1781 was seen in a dynamic object. We must force the union to be
1782 NULL, so that it is correct for a regular symbol. */
1783 h
->verinfo
.vertree
= NULL
;
1786 /* Handle the special case of a new common symbol merging with an
1787 old symbol that looks like it might be a common symbol defined in
1788 a shared object. Note that we have already handled the case in
1789 which a new common symbol should simply override the definition
1790 in the shared library. */
1793 && bfd_is_com_section (sec
)
1796 /* It would be best if we could set the hash table entry to a
1797 common symbol, but we don't know what to use for the section
1798 or the alignment. */
1799 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1800 bfd_link_hash_common
, sym
->st_size
);
1802 /* If the presumed common symbol in the dynamic object is
1803 larger, pretend that the new symbol has its size. */
1805 if (h
->size
> *pvalue
)
1808 /* We need to remember the alignment required by the symbol
1809 in the dynamic object. */
1810 BFD_ASSERT (pold_alignment
);
1811 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1814 olddyncommon
= false;
1816 h
->root
.type
= bfd_link_hash_undefined
;
1817 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1819 *size_change_ok
= true;
1820 *type_change_ok
= true;
1822 if (hi
->root
.type
== bfd_link_hash_indirect
)
1825 h
->verinfo
.vertree
= NULL
;
1830 /* Handle the case where we had a versioned symbol in a dynamic
1831 library and now find a definition in a normal object. In this
1832 case, we make the versioned symbol point to the normal one. */
1833 flip
->root
.type
= h
->root
.type
;
1834 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1835 h
->root
.type
= bfd_link_hash_indirect
;
1836 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1837 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1841 flip
->ref_dynamic
= 1;
1848 /* This function is called to create an indirect symbol from the
1849 default for the symbol with the default version if needed. The
1850 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1851 set DYNSYM if the new indirect symbol is dynamic. */
1854 _bfd_elf_add_default_symbol (bfd
*abfd
,
1855 struct bfd_link_info
*info
,
1856 struct elf_link_hash_entry
*h
,
1858 Elf_Internal_Sym
*sym
,
1864 bool type_change_ok
;
1865 bool size_change_ok
;
1868 struct elf_link_hash_entry
*hi
;
1869 struct bfd_link_hash_entry
*bh
;
1870 const struct elf_backend_data
*bed
;
1875 size_t len
, shortlen
;
1879 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1882 /* If this symbol has a version, and it is the default version, we
1883 create an indirect symbol from the default name to the fully
1884 decorated name. This will cause external references which do not
1885 specify a version to be bound to this version of the symbol. */
1886 p
= strchr (name
, ELF_VER_CHR
);
1887 if (h
->versioned
== unknown
)
1891 h
->versioned
= unversioned
;
1896 if (p
[1] != ELF_VER_CHR
)
1898 h
->versioned
= versioned_hidden
;
1902 h
->versioned
= versioned
;
1907 /* PR ld/19073: We may see an unversioned definition after the
1913 bed
= get_elf_backend_data (abfd
);
1914 collect
= bed
->collect
;
1915 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1917 shortlen
= p
- name
;
1918 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1919 if (shortname
== NULL
)
1921 memcpy (shortname
, name
, shortlen
);
1922 shortname
[shortlen
] = '\0';
1924 /* We are going to create a new symbol. Merge it with any existing
1925 symbol with this name. For the purposes of the merge, act as
1926 though we were defining the symbol we just defined, although we
1927 actually going to define an indirect symbol. */
1928 type_change_ok
= false;
1929 size_change_ok
= false;
1932 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1933 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1934 &type_change_ok
, &size_change_ok
, &matched
))
1940 if (hi
->def_regular
|| ELF_COMMON_DEF_P (hi
))
1942 /* If the undecorated symbol will have a version added by a
1943 script different to H, then don't indirect to/from the
1944 undecorated symbol. This isn't ideal because we may not yet
1945 have seen symbol versions, if given by a script on the
1946 command line rather than via --version-script. */
1947 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1952 = bfd_find_version_for_sym (info
->version_info
,
1953 hi
->root
.root
.string
, &hide
);
1954 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1956 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
1960 if (hi
->verinfo
.vertree
!= NULL
1961 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1967 /* Add the default symbol if not performing a relocatable link. */
1968 if (! bfd_link_relocatable (info
))
1971 if (bh
->type
== bfd_link_hash_defined
1972 && bh
->u
.def
.section
->owner
!= NULL
1973 && (bh
->u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
1975 /* Mark the previous definition from IR object as
1976 undefined so that the generic linker will override
1978 bh
->type
= bfd_link_hash_undefined
;
1979 bh
->u
.undef
.abfd
= bh
->u
.def
.section
->owner
;
1981 if (! (_bfd_generic_link_add_one_symbol
1982 (info
, abfd
, shortname
, BSF_INDIRECT
,
1983 bfd_ind_section_ptr
,
1984 0, name
, false, collect
, &bh
)))
1986 hi
= (struct elf_link_hash_entry
*) bh
;
1991 /* In this case the symbol named SHORTNAME is overriding the
1992 indirect symbol we want to add. We were planning on making
1993 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1994 is the name without a version. NAME is the fully versioned
1995 name, and it is the default version.
1997 Overriding means that we already saw a definition for the
1998 symbol SHORTNAME in a regular object, and it is overriding
1999 the symbol defined in the dynamic object.
2001 When this happens, we actually want to change NAME, the
2002 symbol we just added, to refer to SHORTNAME. This will cause
2003 references to NAME in the shared object to become references
2004 to SHORTNAME in the regular object. This is what we expect
2005 when we override a function in a shared object: that the
2006 references in the shared object will be mapped to the
2007 definition in the regular object. */
2009 while (hi
->root
.type
== bfd_link_hash_indirect
2010 || hi
->root
.type
== bfd_link_hash_warning
)
2011 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2013 h
->root
.type
= bfd_link_hash_indirect
;
2014 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
2018 hi
->ref_dynamic
= 1;
2022 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
2027 /* Now set HI to H, so that the following code will set the
2028 other fields correctly. */
2032 /* Check if HI is a warning symbol. */
2033 if (hi
->root
.type
== bfd_link_hash_warning
)
2034 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2036 /* If there is a duplicate definition somewhere, then HI may not
2037 point to an indirect symbol. We will have reported an error to
2038 the user in that case. */
2040 if (hi
->root
.type
== bfd_link_hash_indirect
)
2042 struct elf_link_hash_entry
*ht
;
2044 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2045 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2047 /* If we first saw a reference to SHORTNAME with non-default
2048 visibility, merge that visibility to the @@VER symbol. */
2049 elf_merge_st_other (abfd
, ht
, hi
->other
, sec
, true, dynamic
);
2051 /* A reference to the SHORTNAME symbol from a dynamic library
2052 will be satisfied by the versioned symbol at runtime. In
2053 effect, we have a reference to the versioned symbol. */
2054 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2055 hi
->dynamic_def
|= ht
->dynamic_def
;
2057 /* See if the new flags lead us to realize that the symbol must
2063 if (! bfd_link_executable (info
)
2070 if (hi
->ref_regular
)
2076 /* We also need to define an indirection from the nondefault version
2080 len
= strlen (name
);
2081 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2082 if (shortname
== NULL
)
2084 memcpy (shortname
, name
, shortlen
);
2085 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2087 /* Once again, merge with any existing symbol. */
2088 type_change_ok
= false;
2089 size_change_ok
= false;
2091 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2092 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2093 &type_change_ok
, &size_change_ok
, &matched
))
2099 && h
->root
.type
== bfd_link_hash_defweak
2100 && hi
->root
.type
== bfd_link_hash_defined
)
2102 /* We are handling a weak sym@@ver and attempting to define
2103 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2104 new weak sym@ver because there is already a strong sym@ver.
2105 However, sym@ver and sym@@ver are really the same symbol.
2106 The existing strong sym@ver ought to override sym@@ver. */
2107 h
->root
.type
= bfd_link_hash_defined
;
2108 h
->root
.u
.def
.section
= hi
->root
.u
.def
.section
;
2109 h
->root
.u
.def
.value
= hi
->root
.u
.def
.value
;
2110 hi
->root
.type
= bfd_link_hash_indirect
;
2111 hi
->root
.u
.i
.link
= &h
->root
;
2118 /* Here SHORTNAME is a versioned name, so we don't expect to see
2119 the type of override we do in the case above unless it is
2120 overridden by a versioned definition. */
2121 if (hi
->root
.type
!= bfd_link_hash_defined
2122 && hi
->root
.type
!= bfd_link_hash_defweak
)
2124 /* xgettext:c-format */
2125 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2132 if (! (_bfd_generic_link_add_one_symbol
2133 (info
, abfd
, shortname
, BSF_INDIRECT
,
2134 bfd_ind_section_ptr
, 0, name
, false, collect
, &bh
)))
2136 hi
= (struct elf_link_hash_entry
*) bh
;
2139 /* If there is a duplicate definition somewhere, then HI may not
2140 point to an indirect symbol. We will have reported an error
2141 to the user in that case. */
2142 if (hi
->root
.type
== bfd_link_hash_indirect
)
2144 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2145 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2146 hi
->dynamic_def
|= h
->dynamic_def
;
2148 /* If we first saw a reference to @VER symbol with
2149 non-default visibility, merge that visibility to the
2151 elf_merge_st_other (abfd
, h
, hi
->other
, sec
, true, dynamic
);
2153 /* See if the new flags lead us to realize that the symbol
2159 if (! bfd_link_executable (info
)
2165 if (hi
->ref_regular
)
2174 /* This routine is used to export all defined symbols into the dynamic
2175 symbol table. It is called via elf_link_hash_traverse. */
2178 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2180 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2182 /* Ignore indirect symbols. These are added by the versioning code. */
2183 if (h
->root
.type
== bfd_link_hash_indirect
)
2186 /* Ignore this if we won't export it. */
2187 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2190 if (h
->dynindx
== -1
2191 && (h
->def_regular
|| h
->ref_regular
)
2192 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2193 h
->root
.root
.string
))
2195 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2205 /* Look through the symbols which are defined in other shared
2206 libraries and referenced here. Update the list of version
2207 dependencies. This will be put into the .gnu.version_r section.
2208 This function is called via elf_link_hash_traverse. */
2211 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2214 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2215 Elf_Internal_Verneed
*t
;
2216 Elf_Internal_Vernaux
*a
;
2219 /* We only care about symbols defined in shared objects with version
2224 || h
->verinfo
.verdef
== NULL
2225 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2226 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2229 /* See if we already know about this version. */
2230 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2234 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2237 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2238 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2244 /* This is a new version. Add it to tree we are building. */
2249 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2252 rinfo
->failed
= true;
2256 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2257 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2258 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2262 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2265 rinfo
->failed
= true;
2269 /* Note that we are copying a string pointer here, and testing it
2270 above. If bfd_elf_string_from_elf_section is ever changed to
2271 discard the string data when low in memory, this will have to be
2273 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2275 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2276 a
->vna_nextptr
= t
->vn_auxptr
;
2278 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2281 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2288 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2289 hidden. Set *T_P to NULL if there is no match. */
2292 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2293 struct elf_link_hash_entry
*h
,
2294 const char *version_p
,
2295 struct bfd_elf_version_tree
**t_p
,
2298 struct bfd_elf_version_tree
*t
;
2300 /* Look for the version. If we find it, it is no longer weak. */
2301 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2303 if (strcmp (t
->name
, version_p
) == 0)
2307 struct bfd_elf_version_expr
*d
;
2309 len
= version_p
- h
->root
.root
.string
;
2310 alc
= (char *) bfd_malloc (len
);
2313 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2314 alc
[len
- 1] = '\0';
2315 if (alc
[len
- 2] == ELF_VER_CHR
)
2316 alc
[len
- 2] = '\0';
2318 h
->verinfo
.vertree
= t
;
2322 if (t
->globals
.list
!= NULL
)
2323 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2325 /* See if there is anything to force this symbol to
2327 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2329 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2332 && ! info
->export_dynamic
)
2346 /* Return TRUE if the symbol H is hidden by version script. */
2349 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2350 struct elf_link_hash_entry
*h
)
2354 const struct elf_backend_data
*bed
2355 = get_elf_backend_data (info
->output_bfd
);
2357 /* Version script only hides symbols defined in regular objects. */
2358 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2361 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2362 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2364 struct bfd_elf_version_tree
*t
;
2367 if (*p
== ELF_VER_CHR
)
2371 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2375 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2380 /* If we don't have a version for this symbol, see if we can find
2382 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2385 = bfd_find_version_for_sym (info
->version_info
,
2386 h
->root
.root
.string
, &hide
);
2387 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2389 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2397 /* Figure out appropriate versions for all the symbols. We may not
2398 have the version number script until we have read all of the input
2399 files, so until that point we don't know which symbols should be
2400 local. This function is called via elf_link_hash_traverse. */
2403 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2405 struct elf_info_failed
*sinfo
;
2406 struct bfd_link_info
*info
;
2407 const struct elf_backend_data
*bed
;
2408 struct elf_info_failed eif
;
2412 sinfo
= (struct elf_info_failed
*) data
;
2415 /* Fix the symbol flags. */
2418 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2421 sinfo
->failed
= true;
2425 bed
= get_elf_backend_data (info
->output_bfd
);
2427 /* We only need version numbers for symbols defined in regular
2429 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2431 /* Hide symbols defined in discarded input sections. */
2432 if ((h
->root
.type
== bfd_link_hash_defined
2433 || h
->root
.type
== bfd_link_hash_defweak
)
2434 && discarded_section (h
->root
.u
.def
.section
))
2435 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2440 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2441 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2443 struct bfd_elf_version_tree
*t
;
2446 if (*p
== ELF_VER_CHR
)
2449 /* If there is no version string, we can just return out. */
2453 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2455 sinfo
->failed
= true;
2460 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2462 /* If we are building an application, we need to create a
2463 version node for this version. */
2464 if (t
== NULL
&& bfd_link_executable (info
))
2466 struct bfd_elf_version_tree
**pp
;
2469 /* If we aren't going to export this symbol, we don't need
2470 to worry about it. */
2471 if (h
->dynindx
== -1)
2474 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2478 sinfo
->failed
= true;
2483 t
->name_indx
= (unsigned int) -1;
2487 /* Don't count anonymous version tag. */
2488 if (sinfo
->info
->version_info
!= NULL
2489 && sinfo
->info
->version_info
->vernum
== 0)
2491 for (pp
= &sinfo
->info
->version_info
;
2495 t
->vernum
= version_index
;
2499 h
->verinfo
.vertree
= t
;
2503 /* We could not find the version for a symbol when
2504 generating a shared archive. Return an error. */
2506 /* xgettext:c-format */
2507 (_("%pB: version node not found for symbol %s"),
2508 info
->output_bfd
, h
->root
.root
.string
);
2509 bfd_set_error (bfd_error_bad_value
);
2510 sinfo
->failed
= true;
2515 /* If we don't have a version for this symbol, see if we can find
2518 && h
->verinfo
.vertree
== NULL
2519 && sinfo
->info
->version_info
!= NULL
)
2522 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2523 h
->root
.root
.string
, &hide
);
2524 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2525 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2531 /* Read and swap the relocs from the section indicated by SHDR. This
2532 may be either a REL or a RELA section. The relocations are
2533 translated into RELA relocations and stored in INTERNAL_RELOCS,
2534 which should have already been allocated to contain enough space.
2535 The EXTERNAL_RELOCS are a buffer where the external form of the
2536 relocations should be stored.
2538 Returns FALSE if something goes wrong. */
2541 elf_link_read_relocs_from_section (bfd
*abfd
,
2543 Elf_Internal_Shdr
*shdr
,
2544 void *external_relocs
,
2545 Elf_Internal_Rela
*internal_relocs
)
2547 const struct elf_backend_data
*bed
;
2548 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2549 const bfd_byte
*erela
;
2550 const bfd_byte
*erelaend
;
2551 Elf_Internal_Rela
*irela
;
2552 Elf_Internal_Shdr
*symtab_hdr
;
2555 /* Position ourselves at the start of the section. */
2556 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2559 /* Read the relocations. */
2560 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2563 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2564 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2566 bed
= get_elf_backend_data (abfd
);
2568 /* Convert the external relocations to the internal format. */
2569 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2570 swap_in
= bed
->s
->swap_reloc_in
;
2571 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2572 swap_in
= bed
->s
->swap_reloca_in
;
2575 bfd_set_error (bfd_error_wrong_format
);
2579 erela
= (const bfd_byte
*) external_relocs
;
2580 /* Setting erelaend like this and comparing with <= handles case of
2581 a fuzzed object with sh_size not a multiple of sh_entsize. */
2582 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2583 irela
= internal_relocs
;
2584 while (erela
<= erelaend
)
2588 (*swap_in
) (abfd
, erela
, irela
);
2589 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2590 if (bed
->s
->arch_size
== 64)
2594 if ((size_t) r_symndx
>= nsyms
)
2597 /* xgettext:c-format */
2598 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2599 " for offset %#" PRIx64
" in section `%pA'"),
2600 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2601 (uint64_t) irela
->r_offset
, sec
);
2602 bfd_set_error (bfd_error_bad_value
);
2606 else if (r_symndx
!= STN_UNDEF
)
2609 /* xgettext:c-format */
2610 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2611 " for offset %#" PRIx64
" in section `%pA'"
2612 " when the object file has no symbol table"),
2613 abfd
, (uint64_t) r_symndx
,
2614 (uint64_t) irela
->r_offset
, sec
);
2615 bfd_set_error (bfd_error_bad_value
);
2618 irela
+= bed
->s
->int_rels_per_ext_rel
;
2619 erela
+= shdr
->sh_entsize
;
2625 /* Read and swap the relocs for a section O. They may have been
2626 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2627 not NULL, they are used as buffers to read into. They are known to
2628 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2629 the return value is allocated using either malloc or bfd_alloc,
2630 according to the KEEP_MEMORY argument. If O has two relocation
2631 sections (both REL and RELA relocations), then the REL_HDR
2632 relocations will appear first in INTERNAL_RELOCS, followed by the
2633 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2634 update cache_size. */
2637 _bfd_elf_link_info_read_relocs (bfd
*abfd
,
2638 struct bfd_link_info
*info
,
2640 void *external_relocs
,
2641 Elf_Internal_Rela
*internal_relocs
,
2644 void *alloc1
= NULL
;
2645 Elf_Internal_Rela
*alloc2
= NULL
;
2646 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2647 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2648 Elf_Internal_Rela
*internal_rela_relocs
;
2650 if (esdo
->relocs
!= NULL
)
2651 return esdo
->relocs
;
2653 if (o
->reloc_count
== 0)
2656 if (internal_relocs
== NULL
)
2660 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2663 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2665 info
->cache_size
+= size
;
2668 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2669 if (internal_relocs
== NULL
)
2673 if (external_relocs
== NULL
)
2675 bfd_size_type size
= 0;
2678 size
+= esdo
->rel
.hdr
->sh_size
;
2680 size
+= esdo
->rela
.hdr
->sh_size
;
2682 alloc1
= bfd_malloc (size
);
2685 external_relocs
= alloc1
;
2688 internal_rela_relocs
= internal_relocs
;
2691 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2695 external_relocs
= (((bfd_byte
*) external_relocs
)
2696 + esdo
->rel
.hdr
->sh_size
);
2697 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2698 * bed
->s
->int_rels_per_ext_rel
);
2702 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2704 internal_rela_relocs
)))
2707 /* Cache the results for next time, if we can. */
2709 esdo
->relocs
= internal_relocs
;
2713 /* Don't free alloc2, since if it was allocated we are passing it
2714 back (under the name of internal_relocs). */
2716 return internal_relocs
;
2723 bfd_release (abfd
, alloc2
);
2730 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2731 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2732 struct bfd_link_info. */
2735 _bfd_elf_link_read_relocs (bfd
*abfd
,
2737 void *external_relocs
,
2738 Elf_Internal_Rela
*internal_relocs
,
2741 return _bfd_elf_link_info_read_relocs (abfd
, NULL
, o
, external_relocs
,
2742 internal_relocs
, keep_memory
);
2746 /* Compute the size of, and allocate space for, REL_HDR which is the
2747 section header for a section containing relocations for O. */
2750 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2751 struct bfd_elf_section_reloc_data
*reldata
)
2753 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2755 /* That allows us to calculate the size of the section. */
2756 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2758 /* The contents field must last into write_object_contents, so we
2759 allocate it with bfd_alloc rather than malloc. Also since we
2760 cannot be sure that the contents will actually be filled in,
2761 we zero the allocated space. */
2762 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2763 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2766 if (reldata
->hashes
== NULL
&& reldata
->count
)
2768 struct elf_link_hash_entry
**p
;
2770 p
= ((struct elf_link_hash_entry
**)
2771 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2775 reldata
->hashes
= p
;
2781 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2782 originated from the section given by INPUT_REL_HDR) to the
2786 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2787 asection
*input_section
,
2788 Elf_Internal_Shdr
*input_rel_hdr
,
2789 Elf_Internal_Rela
*internal_relocs
,
2790 struct elf_link_hash_entry
**rel_hash
2793 Elf_Internal_Rela
*irela
;
2794 Elf_Internal_Rela
*irelaend
;
2796 struct bfd_elf_section_reloc_data
*output_reldata
;
2797 asection
*output_section
;
2798 const struct elf_backend_data
*bed
;
2799 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2800 struct bfd_elf_section_data
*esdo
;
2802 output_section
= input_section
->output_section
;
2804 bed
= get_elf_backend_data (output_bfd
);
2805 esdo
= elf_section_data (output_section
);
2806 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2808 output_reldata
= &esdo
->rel
;
2809 swap_out
= bed
->s
->swap_reloc_out
;
2811 else if (esdo
->rela
.hdr
2812 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2814 output_reldata
= &esdo
->rela
;
2815 swap_out
= bed
->s
->swap_reloca_out
;
2820 /* xgettext:c-format */
2821 (_("%pB: relocation size mismatch in %pB section %pA"),
2822 output_bfd
, input_section
->owner
, input_section
);
2823 bfd_set_error (bfd_error_wrong_format
);
2827 erel
= output_reldata
->hdr
->contents
;
2828 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2829 irela
= internal_relocs
;
2830 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2831 * bed
->s
->int_rels_per_ext_rel
);
2832 while (irela
< irelaend
)
2834 (*swap_out
) (output_bfd
, irela
, erel
);
2835 irela
+= bed
->s
->int_rels_per_ext_rel
;
2836 erel
+= input_rel_hdr
->sh_entsize
;
2839 /* Bump the counter, so that we know where to add the next set of
2841 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2846 /* Make weak undefined symbols in PIE dynamic. */
2849 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2850 struct elf_link_hash_entry
*h
)
2852 if (bfd_link_pie (info
)
2854 && h
->root
.type
== bfd_link_hash_undefweak
)
2855 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2860 /* Fix up the flags for a symbol. This handles various cases which
2861 can only be fixed after all the input files are seen. This is
2862 currently called by both adjust_dynamic_symbol and
2863 assign_sym_version, which is unnecessary but perhaps more robust in
2864 the face of future changes. */
2867 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2868 struct elf_info_failed
*eif
)
2870 const struct elf_backend_data
*bed
;
2872 /* If this symbol was mentioned in a non-ELF file, try to set
2873 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2874 permit a non-ELF file to correctly refer to a symbol defined in
2875 an ELF dynamic object. */
2878 while (h
->root
.type
== bfd_link_hash_indirect
)
2879 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2881 if (h
->root
.type
!= bfd_link_hash_defined
2882 && h
->root
.type
!= bfd_link_hash_defweak
)
2885 h
->ref_regular_nonweak
= 1;
2889 if (h
->root
.u
.def
.section
->owner
!= NULL
2890 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2891 == bfd_target_elf_flavour
))
2894 h
->ref_regular_nonweak
= 1;
2900 if (h
->dynindx
== -1
2904 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2913 /* Unfortunately, NON_ELF is only correct if the symbol
2914 was first seen in a non-ELF file. Fortunately, if the symbol
2915 was first seen in an ELF file, we're probably OK unless the
2916 symbol was defined in a non-ELF file. Catch that case here.
2917 FIXME: We're still in trouble if the symbol was first seen in
2918 a dynamic object, and then later in a non-ELF regular object. */
2919 if ((h
->root
.type
== bfd_link_hash_defined
2920 || h
->root
.type
== bfd_link_hash_defweak
)
2922 && (h
->root
.u
.def
.section
->owner
!= NULL
2923 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2924 != bfd_target_elf_flavour
)
2925 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2926 && !h
->def_dynamic
)))
2930 /* Backend specific symbol fixup. */
2931 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2932 if (bed
->elf_backend_fixup_symbol
2933 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2936 /* If this is a final link, and the symbol was defined as a common
2937 symbol in a regular object file, and there was no definition in
2938 any dynamic object, then the linker will have allocated space for
2939 the symbol in a common section but the DEF_REGULAR
2940 flag will not have been set. */
2941 if (h
->root
.type
== bfd_link_hash_defined
2945 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2948 /* Symbols defined in discarded sections shouldn't be dynamic. */
2949 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
2950 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
2952 /* If a weak undefined symbol has non-default visibility, we also
2953 hide it from the dynamic linker. */
2954 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2955 && h
->root
.type
== bfd_link_hash_undefweak
)
2956 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
2958 /* A hidden versioned symbol in executable should be forced local if
2959 it is is locally defined, not referenced by shared library and not
2961 else if (bfd_link_executable (eif
->info
)
2962 && h
->versioned
== versioned_hidden
2963 && !eif
->info
->export_dynamic
2967 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
2969 /* If -Bsymbolic was used (which means to bind references to global
2970 symbols to the definition within the shared object), and this
2971 symbol was defined in a regular object, then it actually doesn't
2972 need a PLT entry. Likewise, if the symbol has non-default
2973 visibility. If the symbol has hidden or internal visibility, we
2974 will force it local. */
2975 else if (h
->needs_plt
2976 && bfd_link_pic (eif
->info
)
2977 && is_elf_hash_table (eif
->info
->hash
)
2978 && (SYMBOLIC_BIND (eif
->info
, h
)
2979 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2984 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2985 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2986 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2989 /* If this is a weak defined symbol in a dynamic object, and we know
2990 the real definition in the dynamic object, copy interesting flags
2991 over to the real definition. */
2992 if (h
->is_weakalias
)
2994 struct elf_link_hash_entry
*def
= weakdef (h
);
2996 /* If the real definition is defined by a regular object file,
2997 don't do anything special. See the longer description in
2998 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2999 bfd_link_hash_defined as it was when put on the alias list
3000 then it must have originally been a versioned symbol (for
3001 which a non-versioned indirect symbol is created) and later
3002 a definition for the non-versioned symbol is found. In that
3003 case the indirection is flipped with the versioned symbol
3004 becoming an indirect pointing at the non-versioned symbol.
3005 Thus, not an alias any more. */
3006 if (def
->def_regular
3007 || def
->root
.type
!= bfd_link_hash_defined
)
3010 while ((h
= h
->u
.alias
) != def
)
3011 h
->is_weakalias
= 0;
3015 while (h
->root
.type
== bfd_link_hash_indirect
)
3016 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3017 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3018 || h
->root
.type
== bfd_link_hash_defweak
);
3019 BFD_ASSERT (def
->def_dynamic
);
3020 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
3027 /* Make the backend pick a good value for a dynamic symbol. This is
3028 called via elf_link_hash_traverse, and also calls itself
3032 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
3034 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3035 struct elf_link_hash_table
*htab
;
3036 const struct elf_backend_data
*bed
;
3038 if (! is_elf_hash_table (eif
->info
->hash
))
3041 /* Ignore indirect symbols. These are added by the versioning code. */
3042 if (h
->root
.type
== bfd_link_hash_indirect
)
3045 /* Fix the symbol flags. */
3046 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
3049 htab
= elf_hash_table (eif
->info
);
3050 bed
= get_elf_backend_data (htab
->dynobj
);
3052 if (h
->root
.type
== bfd_link_hash_undefweak
)
3054 if (eif
->info
->dynamic_undefined_weak
== 0)
3055 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3056 else if (eif
->info
->dynamic_undefined_weak
> 0
3058 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3059 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
3060 h
->root
.root
.string
))
3062 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3070 /* If this symbol does not require a PLT entry, and it is not
3071 defined by a dynamic object, or is not referenced by a regular
3072 object, ignore it. We do have to handle a weak defined symbol,
3073 even if no regular object refers to it, if we decided to add it
3074 to the dynamic symbol table. FIXME: Do we normally need to worry
3075 about symbols which are defined by one dynamic object and
3076 referenced by another one? */
3078 && h
->type
!= STT_GNU_IFUNC
3082 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3084 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3088 /* If we've already adjusted this symbol, don't do it again. This
3089 can happen via a recursive call. */
3090 if (h
->dynamic_adjusted
)
3093 /* Don't look at this symbol again. Note that we must set this
3094 after checking the above conditions, because we may look at a
3095 symbol once, decide not to do anything, and then get called
3096 recursively later after REF_REGULAR is set below. */
3097 h
->dynamic_adjusted
= 1;
3099 /* If this is a weak definition, and we know a real definition, and
3100 the real symbol is not itself defined by a regular object file,
3101 then get a good value for the real definition. We handle the
3102 real symbol first, for the convenience of the backend routine.
3104 Note that there is a confusing case here. If the real definition
3105 is defined by a regular object file, we don't get the real symbol
3106 from the dynamic object, but we do get the weak symbol. If the
3107 processor backend uses a COPY reloc, then if some routine in the
3108 dynamic object changes the real symbol, we will not see that
3109 change in the corresponding weak symbol. This is the way other
3110 ELF linkers work as well, and seems to be a result of the shared
3113 I will clarify this issue. Most SVR4 shared libraries define the
3114 variable _timezone and define timezone as a weak synonym. The
3115 tzset call changes _timezone. If you write
3116 extern int timezone;
3118 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3119 you might expect that, since timezone is a synonym for _timezone,
3120 the same number will print both times. However, if the processor
3121 backend uses a COPY reloc, then actually timezone will be copied
3122 into your process image, and, since you define _timezone
3123 yourself, _timezone will not. Thus timezone and _timezone will
3124 wind up at different memory locations. The tzset call will set
3125 _timezone, leaving timezone unchanged. */
3127 if (h
->is_weakalias
)
3129 struct elf_link_hash_entry
*def
= weakdef (h
);
3131 /* If we get to this point, there is an implicit reference to
3132 the alias by a regular object file via the weak symbol H. */
3133 def
->ref_regular
= 1;
3135 /* Ensure that the backend adjust_dynamic_symbol function sees
3136 the strong alias before H by recursively calling ourselves. */
3137 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3141 /* If a symbol has no type and no size and does not require a PLT
3142 entry, then we are probably about to do the wrong thing here: we
3143 are probably going to create a COPY reloc for an empty object.
3144 This case can arise when a shared object is built with assembly
3145 code, and the assembly code fails to set the symbol type. */
3147 && h
->type
== STT_NOTYPE
3150 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3151 h
->root
.root
.string
);
3153 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3162 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3166 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3167 struct elf_link_hash_entry
*h
,
3170 unsigned int power_of_two
;
3172 asection
*sec
= h
->root
.u
.def
.section
;
3174 /* The section alignment of the definition is the maximum alignment
3175 requirement of symbols defined in the section. Since we don't
3176 know the symbol alignment requirement, we start with the
3177 maximum alignment and check low bits of the symbol address
3178 for the minimum alignment. */
3179 power_of_two
= bfd_section_alignment (sec
);
3180 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3181 while ((h
->root
.u
.def
.value
& mask
) != 0)
3187 if (power_of_two
> bfd_section_alignment (dynbss
))
3189 /* Adjust the section alignment if needed. */
3190 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3194 /* We make sure that the symbol will be aligned properly. */
3195 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3197 /* Define the symbol as being at this point in DYNBSS. */
3198 h
->root
.u
.def
.section
= dynbss
;
3199 h
->root
.u
.def
.value
= dynbss
->size
;
3201 /* Increment the size of DYNBSS to make room for the symbol. */
3202 dynbss
->size
+= h
->size
;
3204 /* No error if extern_protected_data is true. */
3205 if (h
->protected_def
3206 && (!info
->extern_protected_data
3207 || (info
->extern_protected_data
< 0
3208 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3209 info
->callbacks
->einfo
3210 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3211 h
->root
.root
.string
);
3216 /* Adjust all external symbols pointing into SEC_MERGE sections
3217 to reflect the object merging within the sections. */
3220 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3224 if ((h
->root
.type
== bfd_link_hash_defined
3225 || h
->root
.type
== bfd_link_hash_defweak
)
3226 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3227 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3229 bfd
*output_bfd
= (bfd
*) data
;
3231 h
->root
.u
.def
.value
=
3232 _bfd_merged_section_offset (output_bfd
,
3233 &h
->root
.u
.def
.section
,
3234 elf_section_data (sec
)->sec_info
,
3235 h
->root
.u
.def
.value
);
3241 /* Returns false if the symbol referred to by H should be considered
3242 to resolve local to the current module, and true if it should be
3243 considered to bind dynamically. */
3246 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3247 struct bfd_link_info
*info
,
3248 bool not_local_protected
)
3250 bool binding_stays_local_p
;
3251 const struct elf_backend_data
*bed
;
3252 struct elf_link_hash_table
*hash_table
;
3257 while (h
->root
.type
== bfd_link_hash_indirect
3258 || h
->root
.type
== bfd_link_hash_warning
)
3259 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3261 /* If it was forced local, then clearly it's not dynamic. */
3262 if (h
->dynindx
== -1)
3264 if (h
->forced_local
)
3267 /* Identify the cases where name binding rules say that a
3268 visible symbol resolves locally. */
3269 binding_stays_local_p
= (bfd_link_executable (info
)
3270 || SYMBOLIC_BIND (info
, h
));
3272 switch (ELF_ST_VISIBILITY (h
->other
))
3279 hash_table
= elf_hash_table (info
);
3280 if (!is_elf_hash_table (&hash_table
->root
))
3283 bed
= get_elf_backend_data (hash_table
->dynobj
);
3285 /* Proper resolution for function pointer equality may require
3286 that these symbols perhaps be resolved dynamically, even though
3287 we should be resolving them to the current module. */
3288 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3289 binding_stays_local_p
= true;
3296 /* If it isn't defined locally, then clearly it's dynamic. */
3297 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3300 /* Otherwise, the symbol is dynamic if binding rules don't tell
3301 us that it remains local. */
3302 return !binding_stays_local_p
;
3305 /* Return true if the symbol referred to by H should be considered
3306 to resolve local to the current module, and false otherwise. Differs
3307 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3308 undefined symbols. The two functions are virtually identical except
3309 for the place where dynindx == -1 is tested. If that test is true,
3310 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3311 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3313 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3314 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3315 treatment of undefined weak symbols. For those that do not make
3316 undefined weak symbols dynamic, both functions may return false. */
3319 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3320 struct bfd_link_info
*info
,
3321 bool local_protected
)
3323 const struct elf_backend_data
*bed
;
3324 struct elf_link_hash_table
*hash_table
;
3326 /* If it's a local sym, of course we resolve locally. */
3330 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3331 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3332 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3335 /* Forced local symbols resolve locally. */
3336 if (h
->forced_local
)
3339 /* Common symbols that become definitions don't get the DEF_REGULAR
3340 flag set, so test it first, and don't bail out. */
3341 if (ELF_COMMON_DEF_P (h
))
3343 /* If we don't have a definition in a regular file, then we can't
3344 resolve locally. The sym is either undefined or dynamic. */
3345 else if (!h
->def_regular
)
3348 /* Non-dynamic symbols resolve locally. */
3349 if (h
->dynindx
== -1)
3352 /* At this point, we know the symbol is defined and dynamic. In an
3353 executable it must resolve locally, likewise when building symbolic
3354 shared libraries. */
3355 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3358 /* Now deal with defined dynamic symbols in shared libraries. Ones
3359 with default visibility might not resolve locally. */
3360 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3363 hash_table
= elf_hash_table (info
);
3364 if (!is_elf_hash_table (&hash_table
->root
))
3367 /* STV_PROTECTED symbols with indirect external access are local. */
3368 if (info
->indirect_extern_access
> 0)
3371 bed
= get_elf_backend_data (hash_table
->dynobj
);
3373 /* If extern_protected_data is false, STV_PROTECTED non-function
3374 symbols are local. */
3375 if ((!info
->extern_protected_data
3376 || (info
->extern_protected_data
< 0
3377 && !bed
->extern_protected_data
))
3378 && !bed
->is_function_type (h
->type
))
3381 /* Function pointer equality tests may require that STV_PROTECTED
3382 symbols be treated as dynamic symbols. If the address of a
3383 function not defined in an executable is set to that function's
3384 plt entry in the executable, then the address of the function in
3385 a shared library must also be the plt entry in the executable. */
3386 return local_protected
;
3389 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3390 aligned. Returns the first TLS output section. */
3392 struct bfd_section
*
3393 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3395 struct bfd_section
*sec
, *tls
;
3396 unsigned int align
= 0;
3398 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3399 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3403 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3404 if (sec
->alignment_power
> align
)
3405 align
= sec
->alignment_power
;
3407 elf_hash_table (info
)->tls_sec
= tls
;
3409 /* Ensure the alignment of the first section (usually .tdata) is the largest
3410 alignment, so that the tls segment starts aligned. */
3412 tls
->alignment_power
= align
;
3417 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3419 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3420 Elf_Internal_Sym
*sym
)
3422 const struct elf_backend_data
*bed
;
3424 /* Local symbols do not count, but target specific ones might. */
3425 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3426 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3429 bed
= get_elf_backend_data (abfd
);
3430 /* Function symbols do not count. */
3431 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3434 /* If the section is undefined, then so is the symbol. */
3435 if (sym
->st_shndx
== SHN_UNDEF
)
3438 /* If the symbol is defined in the common section, then
3439 it is a common definition and so does not count. */
3440 if (bed
->common_definition (sym
))
3443 /* If the symbol is in a target specific section then we
3444 must rely upon the backend to tell us what it is. */
3445 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3446 /* FIXME - this function is not coded yet:
3448 return _bfd_is_global_symbol_definition (abfd, sym);
3450 Instead for now assume that the definition is not global,
3451 Even if this is wrong, at least the linker will behave
3452 in the same way that it used to do. */
3458 /* Search the symbol table of the archive element of the archive ABFD
3459 whose archive map contains a mention of SYMDEF, and determine if
3460 the symbol is defined in this element. */
3462 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3464 Elf_Internal_Shdr
* hdr
;
3468 Elf_Internal_Sym
*isymbuf
;
3469 Elf_Internal_Sym
*isym
;
3470 Elf_Internal_Sym
*isymend
;
3473 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
, NULL
);
3477 if (! bfd_check_format (abfd
, bfd_object
))
3480 /* Select the appropriate symbol table. If we don't know if the
3481 object file is an IR object, give linker LTO plugin a chance to
3482 get the correct symbol table. */
3483 if (abfd
->plugin_format
== bfd_plugin_yes
3484 #if BFD_SUPPORTS_PLUGINS
3485 || (abfd
->plugin_format
== bfd_plugin_unknown
3486 && bfd_link_plugin_object_p (abfd
))
3490 /* Use the IR symbol table if the object has been claimed by
3492 abfd
= abfd
->plugin_dummy_bfd
;
3493 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3495 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3496 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3498 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3500 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3502 /* The sh_info field of the symtab header tells us where the
3503 external symbols start. We don't care about the local symbols. */
3504 if (elf_bad_symtab (abfd
))
3506 extsymcount
= symcount
;
3511 extsymcount
= symcount
- hdr
->sh_info
;
3512 extsymoff
= hdr
->sh_info
;
3515 if (extsymcount
== 0)
3518 /* Read in the symbol table. */
3519 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3521 if (isymbuf
== NULL
)
3524 /* Scan the symbol table looking for SYMDEF. */
3526 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3530 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3535 if (strcmp (name
, symdef
->name
) == 0)
3537 result
= is_global_data_symbol_definition (abfd
, isym
);
3547 /* Add an entry to the .dynamic table. */
3550 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3554 struct elf_link_hash_table
*hash_table
;
3555 const struct elf_backend_data
*bed
;
3557 bfd_size_type newsize
;
3558 bfd_byte
*newcontents
;
3559 Elf_Internal_Dyn dyn
;
3561 hash_table
= elf_hash_table (info
);
3562 if (! is_elf_hash_table (&hash_table
->root
))
3565 if (tag
== DT_RELA
|| tag
== DT_REL
)
3566 hash_table
->dynamic_relocs
= true;
3568 bed
= get_elf_backend_data (hash_table
->dynobj
);
3569 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3570 BFD_ASSERT (s
!= NULL
);
3572 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3573 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3574 if (newcontents
== NULL
)
3578 dyn
.d_un
.d_val
= val
;
3579 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3582 s
->contents
= newcontents
;
3587 /* Strip zero-sized dynamic sections. */
3590 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3592 struct elf_link_hash_table
*hash_table
;
3593 const struct elf_backend_data
*bed
;
3594 asection
*s
, *sdynamic
, **pp
;
3595 asection
*rela_dyn
, *rel_dyn
;
3596 Elf_Internal_Dyn dyn
;
3597 bfd_byte
*extdyn
, *next
;
3598 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3599 bool strip_zero_sized
;
3600 bool strip_zero_sized_plt
;
3602 if (bfd_link_relocatable (info
))
3605 hash_table
= elf_hash_table (info
);
3606 if (!is_elf_hash_table (&hash_table
->root
))
3609 if (!hash_table
->dynobj
)
3612 sdynamic
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3616 bed
= get_elf_backend_data (hash_table
->dynobj
);
3617 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3619 strip_zero_sized
= false;
3620 strip_zero_sized_plt
= false;
3622 /* Strip zero-sized dynamic sections. */
3623 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3624 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3625 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3629 || s
== hash_table
->srelplt
->output_section
3630 || s
== hash_table
->splt
->output_section
))
3633 info
->output_bfd
->section_count
--;
3634 strip_zero_sized
= true;
3639 else if (s
== hash_table
->splt
->output_section
)
3641 s
= hash_table
->splt
;
3642 strip_zero_sized_plt
= true;
3645 s
= hash_table
->srelplt
;
3646 s
->flags
|= SEC_EXCLUDE
;
3647 s
->output_section
= bfd_abs_section_ptr
;
3652 if (strip_zero_sized_plt
&& sdynamic
->size
!= 0)
3653 for (extdyn
= sdynamic
->contents
;
3654 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3657 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3658 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3666 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3667 the procedure linkage table (the .plt section) has been
3669 memmove (extdyn
, next
,
3670 sdynamic
->size
- (next
- sdynamic
->contents
));
3675 if (strip_zero_sized
)
3677 /* Regenerate program headers. */
3678 elf_seg_map (info
->output_bfd
) = NULL
;
3679 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
);
3685 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3686 1 if a DT_NEEDED tag already exists, and 0 on success. */
3689 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3691 struct elf_link_hash_table
*hash_table
;
3695 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3698 hash_table
= elf_hash_table (info
);
3699 soname
= elf_dt_name (abfd
);
3700 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, false);
3701 if (strindex
== (size_t) -1)
3704 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3707 const struct elf_backend_data
*bed
;
3710 bed
= get_elf_backend_data (hash_table
->dynobj
);
3711 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3712 if (sdyn
!= NULL
&& sdyn
->size
!= 0)
3713 for (extdyn
= sdyn
->contents
;
3714 extdyn
< sdyn
->contents
+ sdyn
->size
;
3715 extdyn
+= bed
->s
->sizeof_dyn
)
3717 Elf_Internal_Dyn dyn
;
3719 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3720 if (dyn
.d_tag
== DT_NEEDED
3721 && dyn
.d_un
.d_val
== strindex
)
3723 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3729 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3732 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3738 /* Return true if SONAME is on the needed list between NEEDED and STOP
3739 (or the end of list if STOP is NULL), and needed by a library that
3743 on_needed_list (const char *soname
,
3744 struct bfd_link_needed_list
*needed
,
3745 struct bfd_link_needed_list
*stop
)
3747 struct bfd_link_needed_list
*look
;
3748 for (look
= needed
; look
!= stop
; look
= look
->next
)
3749 if (strcmp (soname
, look
->name
) == 0
3750 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3751 /* If needed by a library that itself is not directly
3752 needed, recursively check whether that library is
3753 indirectly needed. Since we add DT_NEEDED entries to
3754 the end of the list, library dependencies appear after
3755 the library. Therefore search prior to the current
3756 LOOK, preventing possible infinite recursion. */
3757 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3763 /* Sort symbol by value, section, size, and type. */
3765 elf_sort_symbol (const void *arg1
, const void *arg2
)
3767 const struct elf_link_hash_entry
*h1
;
3768 const struct elf_link_hash_entry
*h2
;
3769 bfd_signed_vma vdiff
;
3774 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3775 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3776 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3778 return vdiff
> 0 ? 1 : -1;
3780 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3784 /* Sort so that sized symbols are selected over zero size symbols. */
3785 vdiff
= h1
->size
- h2
->size
;
3787 return vdiff
> 0 ? 1 : -1;
3789 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3790 if (h1
->type
!= h2
->type
)
3791 return h1
->type
- h2
->type
;
3793 /* If symbols are properly sized and typed, and multiple strong
3794 aliases are not defined in a shared library by the user we
3795 shouldn't get here. Unfortunately linker script symbols like
3796 __bss_start sometimes match a user symbol defined at the start of
3797 .bss without proper size and type. We'd like to preference the
3798 user symbol over reserved system symbols. Sort on leading
3800 n1
= h1
->root
.root
.string
;
3801 n2
= h2
->root
.root
.string
;
3814 /* Final sort on name selects user symbols like '_u' over reserved
3815 system symbols like '_Z' and also will avoid qsort instability. */
3819 /* This function is used to adjust offsets into .dynstr for
3820 dynamic symbols. This is called via elf_link_hash_traverse. */
3823 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3825 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3827 if (h
->dynindx
!= -1)
3828 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3832 /* Assign string offsets in .dynstr, update all structures referencing
3836 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3838 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3839 struct elf_link_local_dynamic_entry
*entry
;
3840 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3841 bfd
*dynobj
= hash_table
->dynobj
;
3844 const struct elf_backend_data
*bed
;
3847 _bfd_elf_strtab_finalize (dynstr
);
3848 size
= _bfd_elf_strtab_size (dynstr
);
3850 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3852 if (info
->callbacks
->examine_strtab
)
3853 info
->callbacks
->examine_strtab (dynstr
);
3855 bed
= get_elf_backend_data (dynobj
);
3856 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3857 BFD_ASSERT (sdyn
!= NULL
);
3859 /* Update all .dynamic entries referencing .dynstr strings. */
3860 for (extdyn
= sdyn
->contents
;
3861 extdyn
< PTR_ADD (sdyn
->contents
, sdyn
->size
);
3862 extdyn
+= bed
->s
->sizeof_dyn
)
3864 Elf_Internal_Dyn dyn
;
3866 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3870 dyn
.d_un
.d_val
= size
;
3880 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3885 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3888 /* Now update local dynamic symbols. */
3889 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3890 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3891 entry
->isym
.st_name
);
3893 /* And the rest of dynamic symbols. */
3894 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3896 /* Adjust version definitions. */
3897 if (elf_tdata (output_bfd
)->cverdefs
)
3902 Elf_Internal_Verdef def
;
3903 Elf_Internal_Verdaux defaux
;
3905 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3909 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3911 p
+= sizeof (Elf_External_Verdef
);
3912 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3914 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3916 _bfd_elf_swap_verdaux_in (output_bfd
,
3917 (Elf_External_Verdaux
*) p
, &defaux
);
3918 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3920 _bfd_elf_swap_verdaux_out (output_bfd
,
3921 &defaux
, (Elf_External_Verdaux
*) p
);
3922 p
+= sizeof (Elf_External_Verdaux
);
3925 while (def
.vd_next
);
3928 /* Adjust version references. */
3929 if (elf_tdata (output_bfd
)->verref
)
3934 Elf_Internal_Verneed need
;
3935 Elf_Internal_Vernaux needaux
;
3937 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3941 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3943 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3944 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3945 (Elf_External_Verneed
*) p
);
3946 p
+= sizeof (Elf_External_Verneed
);
3947 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3949 _bfd_elf_swap_vernaux_in (output_bfd
,
3950 (Elf_External_Vernaux
*) p
, &needaux
);
3951 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3953 _bfd_elf_swap_vernaux_out (output_bfd
,
3955 (Elf_External_Vernaux
*) p
);
3956 p
+= sizeof (Elf_External_Vernaux
);
3959 while (need
.vn_next
);
3965 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3966 The default is to only match when the INPUT and OUTPUT are exactly
3970 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3971 const bfd_target
*output
)
3973 return input
== output
;
3976 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3977 This version is used when different targets for the same architecture
3978 are virtually identical. */
3981 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3982 const bfd_target
*output
)
3984 const struct elf_backend_data
*obed
, *ibed
;
3986 if (input
== output
)
3989 ibed
= xvec_get_elf_backend_data (input
);
3990 obed
= xvec_get_elf_backend_data (output
);
3992 if (ibed
->arch
!= obed
->arch
)
3995 /* If both backends are using this function, deem them compatible. */
3996 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3999 /* Make a special call to the linker "notice" function to tell it that
4000 we are about to handle an as-needed lib, or have finished
4001 processing the lib. */
4004 _bfd_elf_notice_as_needed (bfd
*ibfd
,
4005 struct bfd_link_info
*info
,
4006 enum notice_asneeded_action act
)
4008 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
4011 /* Call ACTION on each relocation in an ELF object file. */
4014 _bfd_elf_link_iterate_on_relocs
4015 (bfd
*abfd
, struct bfd_link_info
*info
,
4016 bool (*action
) (bfd
*, struct bfd_link_info
*, asection
*,
4017 const Elf_Internal_Rela
*))
4019 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4020 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4022 /* If this object is the same format as the output object, and it is
4023 not a shared library, then let the backend look through the
4026 This is required to build global offset table entries and to
4027 arrange for dynamic relocs. It is not required for the
4028 particular common case of linking non PIC code, even when linking
4029 against shared libraries, but unfortunately there is no way of
4030 knowing whether an object file has been compiled PIC or not.
4031 Looking through the relocs is not particularly time consuming.
4032 The problem is that we must either (1) keep the relocs in memory,
4033 which causes the linker to require additional runtime memory or
4034 (2) read the relocs twice from the input file, which wastes time.
4035 This would be a good case for using mmap.
4037 I have no idea how to handle linking PIC code into a file of a
4038 different format. It probably can't be done. */
4039 if ((abfd
->flags
& DYNAMIC
) == 0
4040 && is_elf_hash_table (&htab
->root
)
4041 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4042 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4046 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4048 Elf_Internal_Rela
*internal_relocs
;
4051 /* Don't check relocations in excluded sections. Don't do
4052 anything special with non-loaded, non-alloced sections.
4053 In particular, any relocs in such sections should not
4054 affect GOT and PLT reference counting (ie. we don't
4055 allow them to create GOT or PLT entries), there's no
4056 possibility or desire to optimize TLS relocs, and
4057 there's not much point in propagating relocs to shared
4058 libs that the dynamic linker won't relocate. */
4059 if ((o
->flags
& SEC_ALLOC
) == 0
4060 || (o
->flags
& SEC_RELOC
) == 0
4061 || (o
->flags
& SEC_EXCLUDE
) != 0
4062 || o
->reloc_count
== 0
4063 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4064 && (o
->flags
& SEC_DEBUGGING
) != 0)
4065 || bfd_is_abs_section (o
->output_section
))
4068 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
4071 _bfd_link_keep_memory (info
));
4072 if (internal_relocs
== NULL
)
4075 ok
= action (abfd
, info
, o
, internal_relocs
);
4077 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4078 free (internal_relocs
);
4088 /* Check relocations in an ELF object file. This is called after
4089 all input files have been opened. */
4092 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
4094 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4095 if (bed
->check_relocs
!= NULL
)
4096 return _bfd_elf_link_iterate_on_relocs (abfd
, info
,
4101 /* Add symbols from an ELF object file to the linker hash table. */
4104 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4106 Elf_Internal_Ehdr
*ehdr
;
4107 Elf_Internal_Shdr
*hdr
;
4111 struct elf_link_hash_entry
**sym_hash
;
4113 Elf_External_Versym
*extversym
= NULL
;
4114 Elf_External_Versym
*extversym_end
= NULL
;
4115 Elf_External_Versym
*ever
;
4116 struct elf_link_hash_entry
*weaks
;
4117 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4118 size_t nondeflt_vers_cnt
= 0;
4119 Elf_Internal_Sym
*isymbuf
= NULL
;
4120 Elf_Internal_Sym
*isym
;
4121 Elf_Internal_Sym
*isymend
;
4122 const struct elf_backend_data
*bed
;
4124 struct elf_link_hash_table
*htab
;
4125 void *alloc_mark
= NULL
;
4126 struct bfd_hash_entry
**old_table
= NULL
;
4127 unsigned int old_size
= 0;
4128 unsigned int old_count
= 0;
4129 void *old_tab
= NULL
;
4131 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4132 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4133 void *old_strtab
= NULL
;
4138 htab
= elf_hash_table (info
);
4139 bed
= get_elf_backend_data (abfd
);
4141 if ((abfd
->flags
& DYNAMIC
) == 0)
4147 /* You can't use -r against a dynamic object. Also, there's no
4148 hope of using a dynamic object which does not exactly match
4149 the format of the output file. */
4150 if (bfd_link_relocatable (info
)
4151 || !is_elf_hash_table (&htab
->root
)
4152 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4154 if (bfd_link_relocatable (info
))
4155 bfd_set_error (bfd_error_invalid_operation
);
4157 bfd_set_error (bfd_error_wrong_format
);
4162 ehdr
= elf_elfheader (abfd
);
4163 if (info
->warn_alternate_em
4164 && bed
->elf_machine_code
!= ehdr
->e_machine
4165 && ((bed
->elf_machine_alt1
!= 0
4166 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4167 || (bed
->elf_machine_alt2
!= 0
4168 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4170 /* xgettext:c-format */
4171 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4172 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4174 /* As a GNU extension, any input sections which are named
4175 .gnu.warning.SYMBOL are treated as warning symbols for the given
4176 symbol. This differs from .gnu.warning sections, which generate
4177 warnings when they are included in an output file. */
4178 /* PR 12761: Also generate this warning when building shared libraries. */
4179 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4183 name
= bfd_section_name (s
);
4184 if (startswith (name
, ".gnu.warning."))
4189 name
+= sizeof ".gnu.warning." - 1;
4191 /* If this is a shared object, then look up the symbol
4192 in the hash table. If it is there, and it is already
4193 been defined, then we will not be using the entry
4194 from this shared object, so we don't need to warn.
4195 FIXME: If we see the definition in a regular object
4196 later on, we will warn, but we shouldn't. The only
4197 fix is to keep track of what warnings we are supposed
4198 to emit, and then handle them all at the end of the
4202 struct elf_link_hash_entry
*h
;
4204 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
4206 /* FIXME: What about bfd_link_hash_common? */
4208 && (h
->root
.type
== bfd_link_hash_defined
4209 || h
->root
.type
== bfd_link_hash_defweak
))
4214 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4218 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4223 if (! (_bfd_generic_link_add_one_symbol
4224 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4225 false, bed
->collect
, NULL
)))
4228 if (bfd_link_executable (info
))
4230 /* Clobber the section size so that the warning does
4231 not get copied into the output file. */
4234 /* Also set SEC_EXCLUDE, so that symbols defined in
4235 the warning section don't get copied to the output. */
4236 s
->flags
|= SEC_EXCLUDE
;
4241 just_syms
= ((s
= abfd
->sections
) != NULL
4242 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4247 /* If we are creating a shared library, create all the dynamic
4248 sections immediately. We need to attach them to something,
4249 so we attach them to this BFD, provided it is the right
4250 format and is not from ld --just-symbols. Always create the
4251 dynamic sections for -E/--dynamic-list. FIXME: If there
4252 are no input BFD's of the same format as the output, we can't
4253 make a shared library. */
4255 && (bfd_link_pic (info
)
4256 || (!bfd_link_relocatable (info
)
4258 && (info
->export_dynamic
|| info
->dynamic
)))
4259 && is_elf_hash_table (&htab
->root
)
4260 && info
->output_bfd
->xvec
== abfd
->xvec
4261 && !htab
->dynamic_sections_created
)
4263 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4267 else if (!is_elf_hash_table (&htab
->root
))
4271 const char *soname
= NULL
;
4273 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4274 const Elf_Internal_Phdr
*phdr
;
4275 struct elf_link_loaded_list
*loaded_lib
;
4277 /* ld --just-symbols and dynamic objects don't mix very well.
4278 ld shouldn't allow it. */
4282 /* If this dynamic lib was specified on the command line with
4283 --as-needed in effect, then we don't want to add a DT_NEEDED
4284 tag unless the lib is actually used. Similary for libs brought
4285 in by another lib's DT_NEEDED. When --no-add-needed is used
4286 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4287 any dynamic library in DT_NEEDED tags in the dynamic lib at
4289 add_needed
= (elf_dyn_lib_class (abfd
)
4290 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4291 | DYN_NO_NEEDED
)) == 0;
4293 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4294 if (s
!= NULL
&& s
->size
!= 0)
4298 unsigned int elfsec
;
4299 unsigned long shlink
;
4301 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4308 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4309 if (elfsec
== SHN_BAD
)
4310 goto error_free_dyn
;
4311 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4313 for (extdyn
= dynbuf
;
4314 extdyn
<= dynbuf
+ s
->size
- bed
->s
->sizeof_dyn
;
4315 extdyn
+= bed
->s
->sizeof_dyn
)
4317 Elf_Internal_Dyn dyn
;
4319 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4320 if (dyn
.d_tag
== DT_SONAME
)
4322 unsigned int tagv
= dyn
.d_un
.d_val
;
4323 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4325 goto error_free_dyn
;
4327 if (dyn
.d_tag
== DT_NEEDED
)
4329 struct bfd_link_needed_list
*n
, **pn
;
4331 unsigned int tagv
= dyn
.d_un
.d_val
;
4332 size_t amt
= sizeof (struct bfd_link_needed_list
);
4334 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4335 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4336 if (n
== NULL
|| fnm
== NULL
)
4337 goto error_free_dyn
;
4338 amt
= strlen (fnm
) + 1;
4339 anm
= (char *) bfd_alloc (abfd
, amt
);
4341 goto error_free_dyn
;
4342 memcpy (anm
, fnm
, amt
);
4346 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4350 if (dyn
.d_tag
== DT_RUNPATH
)
4352 struct bfd_link_needed_list
*n
, **pn
;
4354 unsigned int tagv
= dyn
.d_un
.d_val
;
4355 size_t amt
= sizeof (struct bfd_link_needed_list
);
4357 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4358 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4359 if (n
== NULL
|| fnm
== NULL
)
4360 goto error_free_dyn
;
4361 amt
= strlen (fnm
) + 1;
4362 anm
= (char *) bfd_alloc (abfd
, amt
);
4364 goto error_free_dyn
;
4365 memcpy (anm
, fnm
, amt
);
4369 for (pn
= & runpath
;
4375 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4376 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4378 struct bfd_link_needed_list
*n
, **pn
;
4380 unsigned int tagv
= dyn
.d_un
.d_val
;
4381 size_t amt
= sizeof (struct bfd_link_needed_list
);
4383 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4384 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4385 if (n
== NULL
|| fnm
== NULL
)
4386 goto error_free_dyn
;
4387 amt
= strlen (fnm
) + 1;
4388 anm
= (char *) bfd_alloc (abfd
, amt
);
4390 goto error_free_dyn
;
4391 memcpy (anm
, fnm
, amt
);
4401 if (dyn
.d_tag
== DT_AUDIT
)
4403 unsigned int tagv
= dyn
.d_un
.d_val
;
4404 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4406 if (dyn
.d_tag
== DT_FLAGS_1
)
4407 elf_tdata (abfd
)->is_pie
= (dyn
.d_un
.d_val
& DF_1_PIE
) != 0;
4413 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4414 frees all more recently bfd_alloc'd blocks as well. */
4420 struct bfd_link_needed_list
**pn
;
4421 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4426 /* If we have a PT_GNU_RELRO program header, mark as read-only
4427 all sections contained fully therein. This makes relro
4428 shared library sections appear as they will at run-time. */
4429 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4430 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4431 if (phdr
->p_type
== PT_GNU_RELRO
)
4433 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4435 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4437 if ((s
->flags
& SEC_ALLOC
) != 0
4438 && s
->vma
* opb
>= phdr
->p_vaddr
4439 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4440 s
->flags
|= SEC_READONLY
;
4445 /* We do not want to include any of the sections in a dynamic
4446 object in the output file. We hack by simply clobbering the
4447 list of sections in the BFD. This could be handled more
4448 cleanly by, say, a new section flag; the existing
4449 SEC_NEVER_LOAD flag is not the one we want, because that one
4450 still implies that the section takes up space in the output
4452 bfd_section_list_clear (abfd
);
4454 /* Find the name to use in a DT_NEEDED entry that refers to this
4455 object. If the object has a DT_SONAME entry, we use it.
4456 Otherwise, if the generic linker stuck something in
4457 elf_dt_name, we use that. Otherwise, we just use the file
4459 if (soname
== NULL
|| *soname
== '\0')
4461 soname
= elf_dt_name (abfd
);
4462 if (soname
== NULL
|| *soname
== '\0')
4463 soname
= bfd_get_filename (abfd
);
4466 /* Save the SONAME because sometimes the linker emulation code
4467 will need to know it. */
4468 elf_dt_name (abfd
) = soname
;
4470 /* If we have already included this dynamic object in the
4471 link, just ignore it. There is no reason to include a
4472 particular dynamic object more than once. */
4473 for (loaded_lib
= htab
->dyn_loaded
;
4475 loaded_lib
= loaded_lib
->next
)
4477 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4481 /* Create dynamic sections for backends that require that be done
4482 before setup_gnu_properties. */
4484 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4487 /* Save the DT_AUDIT entry for the linker emulation code. */
4488 elf_dt_audit (abfd
) = audit
;
4491 /* If this is a dynamic object, we always link against the .dynsym
4492 symbol table, not the .symtab symbol table. The dynamic linker
4493 will only see the .dynsym symbol table, so there is no reason to
4494 look at .symtab for a dynamic object. */
4496 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4497 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4499 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4501 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4503 /* The sh_info field of the symtab header tells us where the
4504 external symbols start. We don't care about the local symbols at
4506 if (elf_bad_symtab (abfd
))
4508 extsymcount
= symcount
;
4513 extsymcount
= symcount
- hdr
->sh_info
;
4514 extsymoff
= hdr
->sh_info
;
4517 sym_hash
= elf_sym_hashes (abfd
);
4518 if (extsymcount
!= 0)
4520 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4522 if (isymbuf
== NULL
)
4525 if (sym_hash
== NULL
)
4527 /* We store a pointer to the hash table entry for each
4529 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4530 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4531 if (sym_hash
== NULL
)
4532 goto error_free_sym
;
4533 elf_sym_hashes (abfd
) = sym_hash
;
4539 /* Read in any version definitions. */
4540 if (!_bfd_elf_slurp_version_tables (abfd
,
4541 info
->default_imported_symver
))
4542 goto error_free_sym
;
4544 /* Read in the symbol versions, but don't bother to convert them
4545 to internal format. */
4546 if (elf_dynversym (abfd
) != 0)
4548 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4549 bfd_size_type amt
= versymhdr
->sh_size
;
4551 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4552 goto error_free_sym
;
4553 extversym
= (Elf_External_Versym
*)
4554 _bfd_malloc_and_read (abfd
, amt
, amt
);
4555 if (extversym
== NULL
)
4556 goto error_free_sym
;
4557 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4561 /* If we are loading an as-needed shared lib, save the symbol table
4562 state before we start adding symbols. If the lib turns out
4563 to be unneeded, restore the state. */
4564 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4569 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4571 struct bfd_hash_entry
*p
;
4572 struct elf_link_hash_entry
*h
;
4574 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4576 h
= (struct elf_link_hash_entry
*) p
;
4577 entsize
+= htab
->root
.table
.entsize
;
4578 if (h
->root
.type
== bfd_link_hash_warning
)
4580 entsize
+= htab
->root
.table
.entsize
;
4581 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4583 if (h
->root
.type
== bfd_link_hash_common
)
4584 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4588 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4589 old_tab
= bfd_malloc (tabsize
+ entsize
);
4590 if (old_tab
== NULL
)
4591 goto error_free_vers
;
4593 /* Remember the current objalloc pointer, so that all mem for
4594 symbols added can later be reclaimed. */
4595 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4596 if (alloc_mark
== NULL
)
4597 goto error_free_vers
;
4599 /* Make a special call to the linker "notice" function to
4600 tell it that we are about to handle an as-needed lib. */
4601 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4602 goto error_free_vers
;
4604 /* Clone the symbol table. Remember some pointers into the
4605 symbol table, and dynamic symbol count. */
4606 old_ent
= (char *) old_tab
+ tabsize
;
4607 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4608 old_undefs
= htab
->root
.undefs
;
4609 old_undefs_tail
= htab
->root
.undefs_tail
;
4610 old_table
= htab
->root
.table
.table
;
4611 old_size
= htab
->root
.table
.size
;
4612 old_count
= htab
->root
.table
.count
;
4614 if (htab
->dynstr
!= NULL
)
4616 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4617 if (old_strtab
== NULL
)
4618 goto error_free_vers
;
4621 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4623 struct bfd_hash_entry
*p
;
4624 struct elf_link_hash_entry
*h
;
4626 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4628 h
= (struct elf_link_hash_entry
*) p
;
4629 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4630 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4631 if (h
->root
.type
== bfd_link_hash_warning
)
4633 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4634 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4635 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4637 if (h
->root
.type
== bfd_link_hash_common
)
4639 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4640 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4647 if (extversym
== NULL
)
4649 else if (extversym
+ extsymoff
< extversym_end
)
4650 ever
= extversym
+ extsymoff
;
4653 /* xgettext:c-format */
4654 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4655 abfd
, (long) extsymoff
,
4656 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4657 bfd_set_error (bfd_error_bad_value
);
4658 goto error_free_vers
;
4661 if (!bfd_link_relocatable (info
)
4662 && abfd
->lto_slim_object
)
4665 (_("%pB: plugin needed to handle lto object"), abfd
);
4668 for (isym
= isymbuf
, isymend
= PTR_ADD (isymbuf
, extsymcount
);
4670 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4674 asection
*sec
, *new_sec
;
4677 struct elf_link_hash_entry
*h
;
4678 struct elf_link_hash_entry
*hi
;
4680 bool size_change_ok
;
4681 bool type_change_ok
;
4687 unsigned int old_alignment
;
4688 unsigned int shindex
;
4694 flags
= BSF_NO_FLAGS
;
4696 value
= isym
->st_value
;
4697 common
= bed
->common_definition (isym
);
4698 if (common
&& info
->inhibit_common_definition
)
4700 /* Treat common symbol as undefined for --no-define-common. */
4701 isym
->st_shndx
= SHN_UNDEF
;
4706 bind
= ELF_ST_BIND (isym
->st_info
);
4710 /* This should be impossible, since ELF requires that all
4711 global symbols follow all local symbols, and that sh_info
4712 point to the first global symbol. Unfortunately, Irix 5
4714 if (elf_bad_symtab (abfd
))
4717 /* If we aren't prepared to handle locals within the globals
4718 then we'll likely segfault on a NULL symbol hash if the
4719 symbol is ever referenced in relocations. */
4720 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4721 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4722 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4723 " (>= sh_info of %lu)"),
4724 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4727 /* Dynamic object relocations are not processed by ld, so
4728 ld won't run into the problem mentioned above. */
4731 bfd_set_error (bfd_error_bad_value
);
4732 goto error_free_vers
;
4735 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4743 case STB_GNU_UNIQUE
:
4744 flags
= BSF_GNU_UNIQUE
;
4748 /* Leave it up to the processor backend. */
4752 if (isym
->st_shndx
== SHN_UNDEF
)
4753 sec
= bfd_und_section_ptr
;
4754 else if (isym
->st_shndx
== SHN_ABS
)
4755 sec
= bfd_abs_section_ptr
;
4756 else if (isym
->st_shndx
== SHN_COMMON
)
4758 sec
= bfd_com_section_ptr
;
4759 /* What ELF calls the size we call the value. What ELF
4760 calls the value we call the alignment. */
4761 value
= isym
->st_size
;
4765 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4767 sec
= bfd_abs_section_ptr
;
4768 else if (discarded_section (sec
))
4770 /* Symbols from discarded section are undefined. We keep
4772 sec
= bfd_und_section_ptr
;
4774 isym
->st_shndx
= SHN_UNDEF
;
4776 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4780 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4783 goto error_free_vers
;
4785 if (isym
->st_shndx
== SHN_COMMON
4786 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4788 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4792 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4794 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4796 goto error_free_vers
;
4800 else if (isym
->st_shndx
== SHN_COMMON
4801 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4802 && !bfd_link_relocatable (info
))
4804 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4808 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4809 | SEC_LINKER_CREATED
);
4810 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4812 goto error_free_vers
;
4816 else if (bed
->elf_add_symbol_hook
)
4818 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4820 goto error_free_vers
;
4822 /* The hook function sets the name to NULL if this symbol
4823 should be skipped for some reason. */
4828 /* Sanity check that all possibilities were handled. */
4832 /* Silently discard TLS symbols from --just-syms. There's
4833 no way to combine a static TLS block with a new TLS block
4834 for this executable. */
4835 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4836 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4839 if (bfd_is_und_section (sec
)
4840 || bfd_is_com_section (sec
))
4845 size_change_ok
= false;
4846 type_change_ok
= bed
->type_change_ok
;
4853 if (is_elf_hash_table (&htab
->root
))
4855 Elf_Internal_Versym iver
;
4856 unsigned int vernum
= 0;
4861 if (info
->default_imported_symver
)
4862 /* Use the default symbol version created earlier. */
4863 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4867 else if (ever
>= extversym_end
)
4869 /* xgettext:c-format */
4870 _bfd_error_handler (_("%pB: not enough version information"),
4872 bfd_set_error (bfd_error_bad_value
);
4873 goto error_free_vers
;
4876 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4878 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4880 /* If this is a hidden symbol, or if it is not version
4881 1, we append the version name to the symbol name.
4882 However, we do not modify a non-hidden absolute symbol
4883 if it is not a function, because it might be the version
4884 symbol itself. FIXME: What if it isn't? */
4885 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4887 && (!bfd_is_abs_section (sec
)
4888 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4891 size_t namelen
, verlen
, newlen
;
4894 if (isym
->st_shndx
!= SHN_UNDEF
)
4896 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4898 else if (vernum
> 1)
4900 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4907 /* xgettext:c-format */
4908 (_("%pB: %s: invalid version %u (max %d)"),
4910 elf_tdata (abfd
)->cverdefs
);
4911 bfd_set_error (bfd_error_bad_value
);
4912 goto error_free_vers
;
4917 /* We cannot simply test for the number of
4918 entries in the VERNEED section since the
4919 numbers for the needed versions do not start
4921 Elf_Internal_Verneed
*t
;
4924 for (t
= elf_tdata (abfd
)->verref
;
4928 Elf_Internal_Vernaux
*a
;
4930 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4932 if (a
->vna_other
== vernum
)
4934 verstr
= a
->vna_nodename
;
4944 /* xgettext:c-format */
4945 (_("%pB: %s: invalid needed version %d"),
4946 abfd
, name
, vernum
);
4947 bfd_set_error (bfd_error_bad_value
);
4948 goto error_free_vers
;
4952 namelen
= strlen (name
);
4953 verlen
= strlen (verstr
);
4954 newlen
= namelen
+ verlen
+ 2;
4955 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4956 && isym
->st_shndx
!= SHN_UNDEF
)
4959 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4960 if (newname
== NULL
)
4961 goto error_free_vers
;
4962 memcpy (newname
, name
, namelen
);
4963 p
= newname
+ namelen
;
4965 /* If this is a defined non-hidden version symbol,
4966 we add another @ to the name. This indicates the
4967 default version of the symbol. */
4968 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4969 && isym
->st_shndx
!= SHN_UNDEF
)
4971 memcpy (p
, verstr
, verlen
+ 1);
4976 /* If this symbol has default visibility and the user has
4977 requested we not re-export it, then mark it as hidden. */
4978 if (!bfd_is_und_section (sec
)
4981 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4982 isym
->st_other
= (STV_HIDDEN
4983 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4985 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4986 sym_hash
, &old_bfd
, &old_weak
,
4987 &old_alignment
, &skip
, &override
,
4988 &type_change_ok
, &size_change_ok
,
4990 goto error_free_vers
;
4995 /* Override a definition only if the new symbol matches the
4997 if (override
&& matched
)
5001 while (h
->root
.type
== bfd_link_hash_indirect
5002 || h
->root
.type
== bfd_link_hash_warning
)
5003 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5005 if (h
->versioned
!= unversioned
5006 && elf_tdata (abfd
)->verdef
!= NULL
5009 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
5012 if (! (_bfd_generic_link_add_one_symbol
5013 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
5014 NULL
, false, bed
->collect
,
5015 (struct bfd_link_hash_entry
**) sym_hash
)))
5016 goto error_free_vers
;
5019 /* We need to make sure that indirect symbol dynamic flags are
5022 while (h
->root
.type
== bfd_link_hash_indirect
5023 || h
->root
.type
== bfd_link_hash_warning
)
5024 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5028 /* Setting the index to -3 tells elf_link_output_extsym that
5029 this symbol is defined in a discarded section. */
5030 if (discarded
&& is_elf_hash_table (&htab
->root
))
5033 new_weak
= (flags
& BSF_WEAK
) != 0;
5037 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
5038 && is_elf_hash_table (&htab
->root
)
5039 && h
->u
.alias
== NULL
)
5041 /* Keep a list of all weak defined non function symbols from
5042 a dynamic object, using the alias field. Later in this
5043 function we will set the alias field to the correct
5044 value. We only put non-function symbols from dynamic
5045 objects on this list, because that happens to be the only
5046 time we need to know the normal symbol corresponding to a
5047 weak symbol, and the information is time consuming to
5048 figure out. If the alias field is not already NULL,
5049 then this symbol was already defined by some previous
5050 dynamic object, and we will be using that previous
5051 definition anyhow. */
5057 /* Set the alignment of a common symbol. */
5058 if ((common
|| bfd_is_com_section (sec
))
5059 && h
->root
.type
== bfd_link_hash_common
)
5064 align
= bfd_log2 (isym
->st_value
);
5067 /* The new symbol is a common symbol in a shared object.
5068 We need to get the alignment from the section. */
5069 align
= new_sec
->alignment_power
;
5071 if (align
> old_alignment
)
5072 h
->root
.u
.c
.p
->alignment_power
= align
;
5074 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5077 if (is_elf_hash_table (&htab
->root
))
5079 /* Set a flag in the hash table entry indicating the type of
5080 reference or definition we just found. A dynamic symbol
5081 is one which is referenced or defined by both a regular
5082 object and a shared object. */
5083 bool dynsym
= false;
5085 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5086 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5088 /* Except for this flag to track nonweak references. */
5090 && bind
!= STB_WEAK
)
5091 h
->ref_ir_nonweak
= 1;
5098 if (bind
!= STB_WEAK
)
5099 h
->ref_regular_nonweak
= 1;
5116 hi
->ref_dynamic
= 1;
5121 hi
->def_dynamic
= 1;
5125 /* If an indirect symbol has been forced local, don't
5126 make the real symbol dynamic. */
5127 if (h
!= hi
&& hi
->forced_local
)
5131 if (bfd_link_dll (info
)
5141 && weakdef (h
)->dynindx
!= -1))
5145 /* Check to see if we need to add an indirect symbol for
5146 the default name. */
5148 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5150 && hi
->versioned
== versioned_hidden
))
5151 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5152 sec
, value
, &old_bfd
, &dynsym
))
5153 goto error_free_vers
;
5155 /* Check the alignment when a common symbol is involved. This
5156 can change when a common symbol is overridden by a normal
5157 definition or a common symbol is ignored due to the old
5158 normal definition. We need to make sure the maximum
5159 alignment is maintained. */
5160 if ((old_alignment
|| common
)
5161 && h
->root
.type
!= bfd_link_hash_common
)
5163 unsigned int common_align
;
5164 unsigned int normal_align
;
5165 unsigned int symbol_align
;
5169 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5170 || h
->root
.type
== bfd_link_hash_defweak
);
5172 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5173 if (h
->root
.u
.def
.section
->owner
!= NULL
5174 && (h
->root
.u
.def
.section
->owner
->flags
5175 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5177 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5178 if (normal_align
> symbol_align
)
5179 normal_align
= symbol_align
;
5182 normal_align
= symbol_align
;
5186 common_align
= old_alignment
;
5187 common_bfd
= old_bfd
;
5192 common_align
= bfd_log2 (isym
->st_value
);
5194 normal_bfd
= old_bfd
;
5197 if (normal_align
< common_align
)
5199 /* PR binutils/2735 */
5200 if (normal_bfd
== NULL
)
5202 /* xgettext:c-format */
5203 (_("warning: alignment %u of common symbol `%s' in %pB is"
5204 " greater than the alignment (%u) of its section %pA"),
5205 1 << common_align
, name
, common_bfd
,
5206 1 << normal_align
, h
->root
.u
.def
.section
);
5209 /* xgettext:c-format */
5210 (_("warning: alignment %u of symbol `%s' in %pB"
5211 " is smaller than %u in %pB"),
5212 1 << normal_align
, name
, normal_bfd
,
5213 1 << common_align
, common_bfd
);
5217 /* Remember the symbol size if it isn't undefined. */
5218 if (isym
->st_size
!= 0
5219 && isym
->st_shndx
!= SHN_UNDEF
5220 && (definition
|| h
->size
== 0))
5223 && h
->size
!= isym
->st_size
5224 && ! size_change_ok
)
5226 /* xgettext:c-format */
5227 (_("warning: size of symbol `%s' changed"
5228 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5229 name
, (uint64_t) h
->size
, old_bfd
,
5230 (uint64_t) isym
->st_size
, abfd
);
5232 h
->size
= isym
->st_size
;
5235 /* If this is a common symbol, then we always want H->SIZE
5236 to be the size of the common symbol. The code just above
5237 won't fix the size if a common symbol becomes larger. We
5238 don't warn about a size change here, because that is
5239 covered by --warn-common. Allow changes between different
5241 if (h
->root
.type
== bfd_link_hash_common
)
5242 h
->size
= h
->root
.u
.c
.size
;
5244 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5245 && ((definition
&& !new_weak
)
5246 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5247 || h
->type
== STT_NOTYPE
))
5249 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5251 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5253 if (type
== STT_GNU_IFUNC
5254 && (abfd
->flags
& DYNAMIC
) != 0)
5257 if (h
->type
!= type
)
5259 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5260 /* xgettext:c-format */
5262 (_("warning: type of symbol `%s' changed"
5263 " from %d to %d in %pB"),
5264 name
, h
->type
, type
, abfd
);
5270 /* Merge st_other field. */
5271 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5272 definition
, dynamic
);
5274 /* We don't want to make debug symbol dynamic. */
5276 && (sec
->flags
& SEC_DEBUGGING
)
5277 && !bfd_link_relocatable (info
))
5280 /* Nor should we make plugin symbols dynamic. */
5281 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5286 h
->target_internal
= isym
->st_target_internal
;
5287 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5290 if (definition
&& !dynamic
)
5292 char *p
= strchr (name
, ELF_VER_CHR
);
5293 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5295 /* Queue non-default versions so that .symver x, x@FOO
5296 aliases can be checked. */
5299 size_t amt
= ((isymend
- isym
+ 1)
5300 * sizeof (struct elf_link_hash_entry
*));
5302 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5304 goto error_free_vers
;
5306 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5310 if (dynsym
&& h
->dynindx
== -1)
5312 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5313 goto error_free_vers
;
5315 && weakdef (h
)->dynindx
== -1)
5317 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5318 goto error_free_vers
;
5321 else if (h
->dynindx
!= -1)
5322 /* If the symbol already has a dynamic index, but
5323 visibility says it should not be visible, turn it into
5325 switch (ELF_ST_VISIBILITY (h
->other
))
5329 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
5337 && h
->root
.type
!= bfd_link_hash_indirect
5339 && h
->ref_regular_nonweak
)
5341 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5342 && h
->ref_ir_nonweak
5343 && !info
->lto_all_symbols_read
)
5344 || (h
->ref_dynamic_nonweak
5345 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5346 && !on_needed_list (elf_dt_name (abfd
),
5347 htab
->needed
, NULL
))))
5349 const char *soname
= elf_dt_name (abfd
);
5351 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5352 h
->root
.root
.string
);
5354 /* A symbol from a library loaded via DT_NEEDED of some
5355 other library is referenced by a regular object.
5356 Add a DT_NEEDED entry for it. Issue an error if
5357 --no-add-needed is used and the reference was not
5360 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5363 /* xgettext:c-format */
5364 (_("%pB: undefined reference to symbol '%s'"),
5366 bfd_set_error (bfd_error_missing_dso
);
5367 goto error_free_vers
;
5370 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5371 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5373 /* Create dynamic sections for backends that require
5374 that be done before setup_gnu_properties. */
5375 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5382 if (info
->lto_plugin_active
5383 && !bfd_link_relocatable (info
)
5384 && (abfd
->flags
& BFD_PLUGIN
) == 0
5390 if (bed
->s
->arch_size
== 32)
5395 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5396 referenced in regular objects so that linker plugin will get
5397 the correct symbol resolution. */
5399 sym_hash
= elf_sym_hashes (abfd
);
5400 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5402 Elf_Internal_Rela
*internal_relocs
;
5403 Elf_Internal_Rela
*rel
, *relend
;
5405 /* Don't check relocations in excluded sections. */
5406 if ((s
->flags
& SEC_RELOC
) == 0
5407 || s
->reloc_count
== 0
5408 || (s
->flags
& SEC_EXCLUDE
) != 0
5409 || ((info
->strip
== strip_all
5410 || info
->strip
== strip_debugger
)
5411 && (s
->flags
& SEC_DEBUGGING
) != 0))
5414 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
5417 _bfd_link_keep_memory (info
));
5418 if (internal_relocs
== NULL
)
5419 goto error_free_vers
;
5421 rel
= internal_relocs
;
5422 relend
= rel
+ s
->reloc_count
;
5423 for ( ; rel
< relend
; rel
++)
5425 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5426 struct elf_link_hash_entry
*h
;
5428 /* Skip local symbols. */
5429 if (r_symndx
< extsymoff
)
5432 h
= sym_hash
[r_symndx
- extsymoff
];
5434 h
->root
.non_ir_ref_regular
= 1;
5437 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5438 free (internal_relocs
);
5447 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5451 /* Restore the symbol table. */
5452 old_ent
= (char *) old_tab
+ tabsize
;
5453 memset (elf_sym_hashes (abfd
), 0,
5454 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5455 htab
->root
.table
.table
= old_table
;
5456 htab
->root
.table
.size
= old_size
;
5457 htab
->root
.table
.count
= old_count
;
5458 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5459 htab
->root
.undefs
= old_undefs
;
5460 htab
->root
.undefs_tail
= old_undefs_tail
;
5461 if (htab
->dynstr
!= NULL
)
5462 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5465 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5467 struct bfd_hash_entry
*p
;
5468 struct elf_link_hash_entry
*h
;
5469 unsigned int non_ir_ref_dynamic
;
5471 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5473 /* Preserve non_ir_ref_dynamic so that this symbol
5474 will be exported when the dynamic lib becomes needed
5475 in the second pass. */
5476 h
= (struct elf_link_hash_entry
*) p
;
5477 if (h
->root
.type
== bfd_link_hash_warning
)
5478 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5479 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5481 h
= (struct elf_link_hash_entry
*) p
;
5482 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5483 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5484 if (h
->root
.type
== bfd_link_hash_warning
)
5486 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5487 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5488 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5490 if (h
->root
.type
== bfd_link_hash_common
)
5492 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5493 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5495 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5499 /* Make a special call to the linker "notice" function to
5500 tell it that symbols added for crefs may need to be removed. */
5501 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5502 goto error_free_vers
;
5505 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5507 free (nondeflt_vers
);
5511 if (old_tab
!= NULL
)
5513 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5514 goto error_free_vers
;
5519 /* Now that all the symbols from this input file are created, if
5520 not performing a relocatable link, handle .symver foo, foo@BAR
5521 such that any relocs against foo become foo@BAR. */
5522 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5526 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5528 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5529 char *shortname
, *p
;
5532 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5534 || (h
->root
.type
!= bfd_link_hash_defined
5535 && h
->root
.type
!= bfd_link_hash_defweak
))
5538 amt
= p
- h
->root
.root
.string
;
5539 shortname
= (char *) bfd_malloc (amt
+ 1);
5541 goto error_free_vers
;
5542 memcpy (shortname
, h
->root
.root
.string
, amt
);
5543 shortname
[amt
] = '\0';
5545 hi
= (struct elf_link_hash_entry
*)
5546 bfd_link_hash_lookup (&htab
->root
, shortname
,
5547 false, false, false);
5549 && hi
->root
.type
== h
->root
.type
5550 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5551 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5553 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
5554 hi
->root
.type
= bfd_link_hash_indirect
;
5555 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5556 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5557 sym_hash
= elf_sym_hashes (abfd
);
5559 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5560 if (sym_hash
[symidx
] == hi
)
5562 sym_hash
[symidx
] = h
;
5568 free (nondeflt_vers
);
5569 nondeflt_vers
= NULL
;
5572 /* Now set the alias field correctly for all the weak defined
5573 symbols we found. The only way to do this is to search all the
5574 symbols. Since we only need the information for non functions in
5575 dynamic objects, that's the only time we actually put anything on
5576 the list WEAKS. We need this information so that if a regular
5577 object refers to a symbol defined weakly in a dynamic object, the
5578 real symbol in the dynamic object is also put in the dynamic
5579 symbols; we also must arrange for both symbols to point to the
5580 same memory location. We could handle the general case of symbol
5581 aliasing, but a general symbol alias can only be generated in
5582 assembler code, handling it correctly would be very time
5583 consuming, and other ELF linkers don't handle general aliasing
5587 struct elf_link_hash_entry
**hpp
;
5588 struct elf_link_hash_entry
**hppend
;
5589 struct elf_link_hash_entry
**sorted_sym_hash
;
5590 struct elf_link_hash_entry
*h
;
5591 size_t sym_count
, amt
;
5593 /* Since we have to search the whole symbol list for each weak
5594 defined symbol, search time for N weak defined symbols will be
5595 O(N^2). Binary search will cut it down to O(NlogN). */
5596 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5597 sorted_sym_hash
= bfd_malloc (amt
);
5598 if (sorted_sym_hash
== NULL
)
5600 sym_hash
= sorted_sym_hash
;
5601 hpp
= elf_sym_hashes (abfd
);
5602 hppend
= hpp
+ extsymcount
;
5604 for (; hpp
< hppend
; hpp
++)
5608 && h
->root
.type
== bfd_link_hash_defined
5609 && !bed
->is_function_type (h
->type
))
5617 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5620 while (weaks
!= NULL
)
5622 struct elf_link_hash_entry
*hlook
;
5625 size_t i
, j
, idx
= 0;
5628 weaks
= hlook
->u
.alias
;
5629 hlook
->u
.alias
= NULL
;
5631 if (hlook
->root
.type
!= bfd_link_hash_defined
5632 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5635 slook
= hlook
->root
.u
.def
.section
;
5636 vlook
= hlook
->root
.u
.def
.value
;
5642 bfd_signed_vma vdiff
;
5644 h
= sorted_sym_hash
[idx
];
5645 vdiff
= vlook
- h
->root
.u
.def
.value
;
5652 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5662 /* We didn't find a value/section match. */
5666 /* With multiple aliases, or when the weak symbol is already
5667 strongly defined, we have multiple matching symbols and
5668 the binary search above may land on any of them. Step
5669 one past the matching symbol(s). */
5672 h
= sorted_sym_hash
[idx
];
5673 if (h
->root
.u
.def
.section
!= slook
5674 || h
->root
.u
.def
.value
!= vlook
)
5678 /* Now look back over the aliases. Since we sorted by size
5679 as well as value and section, we'll choose the one with
5680 the largest size. */
5683 h
= sorted_sym_hash
[idx
];
5685 /* Stop if value or section doesn't match. */
5686 if (h
->root
.u
.def
.section
!= slook
5687 || h
->root
.u
.def
.value
!= vlook
)
5689 else if (h
!= hlook
)
5691 struct elf_link_hash_entry
*t
;
5694 hlook
->is_weakalias
= 1;
5696 if (t
->u
.alias
!= NULL
)
5697 while (t
->u
.alias
!= h
)
5701 /* If the weak definition is in the list of dynamic
5702 symbols, make sure the real definition is put
5704 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5706 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5709 free (sorted_sym_hash
);
5714 /* If the real definition is in the list of dynamic
5715 symbols, make sure the weak definition is put
5716 there as well. If we don't do this, then the
5717 dynamic loader might not merge the entries for the
5718 real definition and the weak definition. */
5719 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5721 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5722 goto err_free_sym_hash
;
5729 free (sorted_sym_hash
);
5732 if (bed
->check_directives
5733 && !(*bed
->check_directives
) (abfd
, info
))
5736 /* If this is a non-traditional link, try to optimize the handling
5737 of the .stab/.stabstr sections. */
5739 && ! info
->traditional_format
5740 && is_elf_hash_table (&htab
->root
)
5741 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5745 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5746 if (stabstr
!= NULL
)
5748 bfd_size_type string_offset
= 0;
5751 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5752 if (startswith (stab
->name
, ".stab")
5753 && (!stab
->name
[5] ||
5754 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5755 && (stab
->flags
& SEC_MERGE
) == 0
5756 && !bfd_is_abs_section (stab
->output_section
))
5758 struct bfd_elf_section_data
*secdata
;
5760 secdata
= elf_section_data (stab
);
5761 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5762 stabstr
, &secdata
->sec_info
,
5765 if (secdata
->sec_info
)
5766 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5771 if (dynamic
&& add_needed
)
5773 /* Add this bfd to the loaded list. */
5774 struct elf_link_loaded_list
*n
;
5776 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5780 n
->next
= htab
->dyn_loaded
;
5781 htab
->dyn_loaded
= n
;
5783 if (dynamic
&& !add_needed
5784 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5785 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5792 free (nondeflt_vers
);
5800 /* Return the linker hash table entry of a symbol that might be
5801 satisfied by an archive symbol. Return -1 on error. */
5803 struct bfd_link_hash_entry
*
5804 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5805 struct bfd_link_info
*info
,
5808 struct bfd_link_hash_entry
*h
;
5812 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
5816 /* If this is a default version (the name contains @@), look up the
5817 symbol again with only one `@' as well as without the version.
5818 The effect is that references to the symbol with and without the
5819 version will be matched by the default symbol in the archive. */
5821 p
= strchr (name
, ELF_VER_CHR
);
5822 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5825 /* First check with only one `@'. */
5826 len
= strlen (name
);
5827 copy
= (char *) bfd_alloc (abfd
, len
);
5829 return (struct bfd_link_hash_entry
*) -1;
5831 first
= p
- name
+ 1;
5832 memcpy (copy
, name
, first
);
5833 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5835 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5838 /* We also need to check references to the symbol without the
5840 copy
[first
- 1] = '\0';
5841 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5844 bfd_release (abfd
, copy
);
5848 /* Add symbols from an ELF archive file to the linker hash table. We
5849 don't use _bfd_generic_link_add_archive_symbols because we need to
5850 handle versioned symbols.
5852 Fortunately, ELF archive handling is simpler than that done by
5853 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5854 oddities. In ELF, if we find a symbol in the archive map, and the
5855 symbol is currently undefined, we know that we must pull in that
5858 Unfortunately, we do have to make multiple passes over the symbol
5859 table until nothing further is resolved. */
5862 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5865 unsigned char *included
= NULL
;
5869 const struct elf_backend_data
*bed
;
5870 struct bfd_link_hash_entry
* (*archive_symbol_lookup
)
5871 (bfd
*, struct bfd_link_info
*, const char *);
5873 if (! bfd_has_map (abfd
))
5875 /* An empty archive is a special case. */
5876 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5878 bfd_set_error (bfd_error_no_armap
);
5882 /* Keep track of all symbols we know to be already defined, and all
5883 files we know to be already included. This is to speed up the
5884 second and subsequent passes. */
5885 c
= bfd_ardata (abfd
)->symdef_count
;
5888 amt
= c
* sizeof (*included
);
5889 included
= (unsigned char *) bfd_zmalloc (amt
);
5890 if (included
== NULL
)
5893 symdefs
= bfd_ardata (abfd
)->symdefs
;
5894 bed
= get_elf_backend_data (abfd
);
5895 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5908 symdefend
= symdef
+ c
;
5909 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5911 struct bfd_link_hash_entry
*h
;
5913 struct bfd_link_hash_entry
*undefs_tail
;
5918 if (symdef
->file_offset
== last
)
5924 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5925 if (h
== (struct bfd_link_hash_entry
*) -1)
5931 if (h
->type
== bfd_link_hash_undefined
)
5933 /* If the archive element has already been loaded then one
5934 of the symbols defined by that element might have been
5935 made undefined due to being in a discarded section. */
5936 if (is_elf_hash_table (info
->hash
)
5937 && ((struct elf_link_hash_entry
*) h
)->indx
== -3)
5940 else if (h
->type
== bfd_link_hash_common
)
5942 /* We currently have a common symbol. The archive map contains
5943 a reference to this symbol, so we may want to include it. We
5944 only want to include it however, if this archive element
5945 contains a definition of the symbol, not just another common
5948 Unfortunately some archivers (including GNU ar) will put
5949 declarations of common symbols into their archive maps, as
5950 well as real definitions, so we cannot just go by the archive
5951 map alone. Instead we must read in the element's symbol
5952 table and check that to see what kind of symbol definition
5954 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5959 if (h
->type
!= bfd_link_hash_undefweak
)
5960 /* Symbol must be defined. Don't check it again. */
5965 /* We need to include this archive member. */
5966 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
,
5968 if (element
== NULL
)
5971 if (! bfd_check_format (element
, bfd_object
))
5974 undefs_tail
= info
->hash
->undefs_tail
;
5976 if (!(*info
->callbacks
5977 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5979 if (!bfd_link_add_symbols (element
, info
))
5982 /* If there are any new undefined symbols, we need to make
5983 another pass through the archive in order to see whether
5984 they can be defined. FIXME: This isn't perfect, because
5985 common symbols wind up on undefs_tail and because an
5986 undefined symbol which is defined later on in this pass
5987 does not require another pass. This isn't a bug, but it
5988 does make the code less efficient than it could be. */
5989 if (undefs_tail
!= info
->hash
->undefs_tail
)
5992 /* Look backward to mark all symbols from this object file
5993 which we have already seen in this pass. */
5997 included
[mark
] = true;
6002 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
6004 /* We mark subsequent symbols from this object file as we go
6005 on through the loop. */
6006 last
= symdef
->file_offset
;
6019 /* Given an ELF BFD, add symbols to the global hash table as
6023 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6025 switch (bfd_get_format (abfd
))
6028 return elf_link_add_object_symbols (abfd
, info
);
6030 return elf_link_add_archive_symbols (abfd
, info
);
6032 bfd_set_error (bfd_error_wrong_format
);
6037 struct hash_codes_info
6039 unsigned long *hashcodes
;
6043 /* This function will be called though elf_link_hash_traverse to store
6044 all hash value of the exported symbols in an array. */
6047 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6049 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
6054 /* Ignore indirect symbols. These are added by the versioning code. */
6055 if (h
->dynindx
== -1)
6058 name
= h
->root
.root
.string
;
6059 if (h
->versioned
>= versioned
)
6061 char *p
= strchr (name
, ELF_VER_CHR
);
6064 alc
= (char *) bfd_malloc (p
- name
+ 1);
6070 memcpy (alc
, name
, p
- name
);
6071 alc
[p
- name
] = '\0';
6076 /* Compute the hash value. */
6077 ha
= bfd_elf_hash (name
);
6079 /* Store the found hash value in the array given as the argument. */
6080 *(inf
->hashcodes
)++ = ha
;
6082 /* And store it in the struct so that we can put it in the hash table
6084 h
->u
.elf_hash_value
= ha
;
6090 struct collect_gnu_hash_codes
6093 const struct elf_backend_data
*bed
;
6094 unsigned long int nsyms
;
6095 unsigned long int maskbits
;
6096 unsigned long int *hashcodes
;
6097 unsigned long int *hashval
;
6098 unsigned long int *indx
;
6099 unsigned long int *counts
;
6103 long int min_dynindx
;
6104 unsigned long int bucketcount
;
6105 unsigned long int symindx
;
6106 long int local_indx
;
6107 long int shift1
, shift2
;
6108 unsigned long int mask
;
6112 /* This function will be called though elf_link_hash_traverse to store
6113 all hash value of the exported symbols in an array. */
6116 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6118 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6123 /* Ignore indirect symbols. These are added by the versioning code. */
6124 if (h
->dynindx
== -1)
6127 /* Ignore also local symbols and undefined symbols. */
6128 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6131 name
= h
->root
.root
.string
;
6132 if (h
->versioned
>= versioned
)
6134 char *p
= strchr (name
, ELF_VER_CHR
);
6137 alc
= (char *) bfd_malloc (p
- name
+ 1);
6143 memcpy (alc
, name
, p
- name
);
6144 alc
[p
- name
] = '\0';
6149 /* Compute the hash value. */
6150 ha
= bfd_elf_gnu_hash (name
);
6152 /* Store the found hash value in the array for compute_bucket_count,
6153 and also for .dynsym reordering purposes. */
6154 s
->hashcodes
[s
->nsyms
] = ha
;
6155 s
->hashval
[h
->dynindx
] = ha
;
6157 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6158 s
->min_dynindx
= h
->dynindx
;
6164 /* This function will be called though elf_link_hash_traverse to do
6165 final dynamic symbol renumbering in case of .gnu.hash.
6166 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6167 to the translation table. */
6170 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6172 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6173 unsigned long int bucket
;
6174 unsigned long int val
;
6176 /* Ignore indirect symbols. */
6177 if (h
->dynindx
== -1)
6180 /* Ignore also local symbols and undefined symbols. */
6181 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6183 if (h
->dynindx
>= s
->min_dynindx
)
6185 if (s
->bed
->record_xhash_symbol
!= NULL
)
6187 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6191 h
->dynindx
= s
->local_indx
++;
6196 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6197 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6198 & ((s
->maskbits
>> s
->shift1
) - 1);
6199 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6201 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6202 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6203 if (s
->counts
[bucket
] == 1)
6204 /* Last element terminates the chain. */
6206 bfd_put_32 (s
->output_bfd
, val
,
6207 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6208 --s
->counts
[bucket
];
6209 if (s
->bed
->record_xhash_symbol
!= NULL
)
6211 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6213 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6216 h
->dynindx
= s
->indx
[bucket
]++;
6220 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6223 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6225 return !(h
->forced_local
6226 || h
->root
.type
== bfd_link_hash_undefined
6227 || h
->root
.type
== bfd_link_hash_undefweak
6228 || ((h
->root
.type
== bfd_link_hash_defined
6229 || h
->root
.type
== bfd_link_hash_defweak
)
6230 && h
->root
.u
.def
.section
->output_section
== NULL
));
6233 /* Array used to determine the number of hash table buckets to use
6234 based on the number of symbols there are. If there are fewer than
6235 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6236 fewer than 37 we use 17 buckets, and so forth. We never use more
6237 than 32771 buckets. */
6239 static const size_t elf_buckets
[] =
6241 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6245 /* Compute bucket count for hashing table. We do not use a static set
6246 of possible tables sizes anymore. Instead we determine for all
6247 possible reasonable sizes of the table the outcome (i.e., the
6248 number of collisions etc) and choose the best solution. The
6249 weighting functions are not too simple to allow the table to grow
6250 without bounds. Instead one of the weighting factors is the size.
6251 Therefore the result is always a good payoff between few collisions
6252 (= short chain lengths) and table size. */
6254 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6255 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6256 unsigned long int nsyms
,
6259 size_t best_size
= 0;
6260 unsigned long int i
;
6262 /* We have a problem here. The following code to optimize the table
6263 size requires an integer type with more the 32 bits. If
6264 BFD_HOST_U_64_BIT is set we know about such a type. */
6265 #ifdef BFD_HOST_U_64_BIT
6270 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
6271 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6272 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6273 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6274 unsigned long int *counts
;
6276 unsigned int no_improvement_count
= 0;
6278 /* Possible optimization parameters: if we have NSYMS symbols we say
6279 that the hashing table must at least have NSYMS/4 and at most
6281 minsize
= nsyms
/ 4;
6284 best_size
= maxsize
= nsyms
* 2;
6289 if ((best_size
& 31) == 0)
6293 /* Create array where we count the collisions in. We must use bfd_malloc
6294 since the size could be large. */
6296 amt
*= sizeof (unsigned long int);
6297 counts
= (unsigned long int *) bfd_malloc (amt
);
6301 /* Compute the "optimal" size for the hash table. The criteria is a
6302 minimal chain length. The minor criteria is (of course) the size
6304 for (i
= minsize
; i
< maxsize
; ++i
)
6306 /* Walk through the array of hashcodes and count the collisions. */
6307 BFD_HOST_U_64_BIT max
;
6308 unsigned long int j
;
6309 unsigned long int fact
;
6311 if (gnu_hash
&& (i
& 31) == 0)
6314 memset (counts
, '\0', i
* sizeof (unsigned long int));
6316 /* Determine how often each hash bucket is used. */
6317 for (j
= 0; j
< nsyms
; ++j
)
6318 ++counts
[hashcodes
[j
] % i
];
6320 /* For the weight function we need some information about the
6321 pagesize on the target. This is information need not be 100%
6322 accurate. Since this information is not available (so far) we
6323 define it here to a reasonable default value. If it is crucial
6324 to have a better value some day simply define this value. */
6325 # ifndef BFD_TARGET_PAGESIZE
6326 # define BFD_TARGET_PAGESIZE (4096)
6329 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6331 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6334 /* Variant 1: optimize for short chains. We add the squares
6335 of all the chain lengths (which favors many small chain
6336 over a few long chains). */
6337 for (j
= 0; j
< i
; ++j
)
6338 max
+= counts
[j
] * counts
[j
];
6340 /* This adds penalties for the overall size of the table. */
6341 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6344 /* Variant 2: Optimize a lot more for small table. Here we
6345 also add squares of the size but we also add penalties for
6346 empty slots (the +1 term). */
6347 for (j
= 0; j
< i
; ++j
)
6348 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6350 /* The overall size of the table is considered, but not as
6351 strong as in variant 1, where it is squared. */
6352 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6356 /* Compare with current best results. */
6357 if (max
< best_chlen
)
6361 no_improvement_count
= 0;
6363 /* PR 11843: Avoid futile long searches for the best bucket size
6364 when there are a large number of symbols. */
6365 else if (++no_improvement_count
== 100)
6372 #endif /* defined (BFD_HOST_U_64_BIT) */
6374 /* This is the fallback solution if no 64bit type is available or if we
6375 are not supposed to spend much time on optimizations. We select the
6376 bucket count using a fixed set of numbers. */
6377 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6379 best_size
= elf_buckets
[i
];
6380 if (nsyms
< elf_buckets
[i
+ 1])
6383 if (gnu_hash
&& best_size
< 2)
6390 /* Size any SHT_GROUP section for ld -r. */
6393 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6398 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6399 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6400 && (s
= ibfd
->sections
) != NULL
6401 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6402 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6407 /* Set a default stack segment size. The value in INFO wins. If it
6408 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6409 undefined it is initialized. */
6412 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6413 struct bfd_link_info
*info
,
6414 const char *legacy_symbol
,
6415 bfd_vma default_size
)
6417 struct elf_link_hash_entry
*h
= NULL
;
6419 /* Look for legacy symbol. */
6421 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6422 false, false, false);
6423 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6424 || h
->root
.type
== bfd_link_hash_defweak
)
6426 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6428 /* The symbol has no type if specified on the command line. */
6429 h
->type
= STT_OBJECT
;
6430 if (info
->stacksize
)
6431 /* xgettext:c-format */
6432 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6433 output_bfd
, legacy_symbol
);
6434 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6435 /* xgettext:c-format */
6436 _bfd_error_handler (_("%pB: %s not absolute"),
6437 output_bfd
, legacy_symbol
);
6439 info
->stacksize
= h
->root
.u
.def
.value
;
6442 if (!info
->stacksize
)
6443 /* If the user didn't set a size, or explicitly inhibit the
6444 size, set it now. */
6445 info
->stacksize
= default_size
;
6447 /* Provide the legacy symbol, if it is referenced. */
6448 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6449 || h
->root
.type
== bfd_link_hash_undefweak
))
6451 struct bfd_link_hash_entry
*bh
= NULL
;
6453 if (!(_bfd_generic_link_add_one_symbol
6454 (info
, output_bfd
, legacy_symbol
,
6455 BSF_GLOBAL
, bfd_abs_section_ptr
,
6456 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6457 NULL
, false, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6460 h
= (struct elf_link_hash_entry
*) bh
;
6462 h
->type
= STT_OBJECT
;
6468 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6470 struct elf_gc_sweep_symbol_info
6472 struct bfd_link_info
*info
;
6473 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6478 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6481 && (((h
->root
.type
== bfd_link_hash_defined
6482 || h
->root
.type
== bfd_link_hash_defweak
)
6483 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6484 && h
->root
.u
.def
.section
->gc_mark
))
6485 || h
->root
.type
== bfd_link_hash_undefined
6486 || h
->root
.type
== bfd_link_hash_undefweak
))
6488 struct elf_gc_sweep_symbol_info
*inf
;
6490 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6491 (*inf
->hide_symbol
) (inf
->info
, h
, true);
6494 h
->ref_regular_nonweak
= 0;
6500 /* Set up the sizes and contents of the ELF dynamic sections. This is
6501 called by the ELF linker emulation before_allocation routine. We
6502 must set the sizes of the sections before the linker sets the
6503 addresses of the various sections. */
6506 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6509 const char *filter_shlib
,
6511 const char *depaudit
,
6512 const char * const *auxiliary_filters
,
6513 struct bfd_link_info
*info
,
6514 asection
**sinterpptr
)
6517 const struct elf_backend_data
*bed
;
6521 if (!is_elf_hash_table (info
->hash
))
6524 /* Any syms created from now on start with -1 in
6525 got.refcount/offset and plt.refcount/offset. */
6526 elf_hash_table (info
)->init_got_refcount
6527 = elf_hash_table (info
)->init_got_offset
;
6528 elf_hash_table (info
)->init_plt_refcount
6529 = elf_hash_table (info
)->init_plt_offset
;
6531 bed
= get_elf_backend_data (output_bfd
);
6533 /* The backend may have to create some sections regardless of whether
6534 we're dynamic or not. */
6535 if (bed
->elf_backend_always_size_sections
6536 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6539 dynobj
= elf_hash_table (info
)->dynobj
;
6541 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6543 struct bfd_elf_version_tree
*verdefs
;
6544 struct elf_info_failed asvinfo
;
6545 struct bfd_elf_version_tree
*t
;
6546 struct bfd_elf_version_expr
*d
;
6550 /* If we are supposed to export all symbols into the dynamic symbol
6551 table (this is not the normal case), then do so. */
6552 if (info
->export_dynamic
6553 || (bfd_link_executable (info
) && info
->dynamic
))
6555 struct elf_info_failed eif
;
6559 elf_link_hash_traverse (elf_hash_table (info
),
6560 _bfd_elf_export_symbol
,
6568 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6570 if (soname_indx
== (size_t) -1
6571 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6575 soname_indx
= (size_t) -1;
6577 /* Make all global versions with definition. */
6578 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6579 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6580 if (!d
->symver
&& d
->literal
)
6582 const char *verstr
, *name
;
6583 size_t namelen
, verlen
, newlen
;
6584 char *newname
, *p
, leading_char
;
6585 struct elf_link_hash_entry
*newh
;
6587 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6589 namelen
= strlen (name
) + (leading_char
!= '\0');
6591 verlen
= strlen (verstr
);
6592 newlen
= namelen
+ verlen
+ 3;
6594 newname
= (char *) bfd_malloc (newlen
);
6595 if (newname
== NULL
)
6597 newname
[0] = leading_char
;
6598 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6600 /* Check the hidden versioned definition. */
6601 p
= newname
+ namelen
;
6603 memcpy (p
, verstr
, verlen
+ 1);
6604 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6605 newname
, false, false,
6608 || (newh
->root
.type
!= bfd_link_hash_defined
6609 && newh
->root
.type
!= bfd_link_hash_defweak
))
6611 /* Check the default versioned definition. */
6613 memcpy (p
, verstr
, verlen
+ 1);
6614 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6615 newname
, false, false,
6620 /* Mark this version if there is a definition and it is
6621 not defined in a shared object. */
6623 && !newh
->def_dynamic
6624 && (newh
->root
.type
== bfd_link_hash_defined
6625 || newh
->root
.type
== bfd_link_hash_defweak
))
6629 /* Attach all the symbols to their version information. */
6630 asvinfo
.info
= info
;
6631 asvinfo
.failed
= false;
6633 elf_link_hash_traverse (elf_hash_table (info
),
6634 _bfd_elf_link_assign_sym_version
,
6639 if (!info
->allow_undefined_version
)
6641 /* Check if all global versions have a definition. */
6642 bool all_defined
= true;
6643 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6644 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6645 if (d
->literal
&& !d
->symver
&& !d
->script
)
6648 (_("%s: undefined version: %s"),
6649 d
->pattern
, t
->name
);
6650 all_defined
= false;
6655 bfd_set_error (bfd_error_bad_value
);
6660 /* Set up the version definition section. */
6661 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6662 BFD_ASSERT (s
!= NULL
);
6664 /* We may have created additional version definitions if we are
6665 just linking a regular application. */
6666 verdefs
= info
->version_info
;
6668 /* Skip anonymous version tag. */
6669 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6670 verdefs
= verdefs
->next
;
6672 if (verdefs
== NULL
&& !info
->create_default_symver
)
6673 s
->flags
|= SEC_EXCLUDE
;
6679 Elf_Internal_Verdef def
;
6680 Elf_Internal_Verdaux defaux
;
6681 struct bfd_link_hash_entry
*bh
;
6682 struct elf_link_hash_entry
*h
;
6688 /* Make space for the base version. */
6689 size
+= sizeof (Elf_External_Verdef
);
6690 size
+= sizeof (Elf_External_Verdaux
);
6693 /* Make space for the default version. */
6694 if (info
->create_default_symver
)
6696 size
+= sizeof (Elf_External_Verdef
);
6700 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6702 struct bfd_elf_version_deps
*n
;
6704 /* Don't emit base version twice. */
6708 size
+= sizeof (Elf_External_Verdef
);
6709 size
+= sizeof (Elf_External_Verdaux
);
6712 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6713 size
+= sizeof (Elf_External_Verdaux
);
6717 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6718 if (s
->contents
== NULL
&& s
->size
!= 0)
6721 /* Fill in the version definition section. */
6725 def
.vd_version
= VER_DEF_CURRENT
;
6726 def
.vd_flags
= VER_FLG_BASE
;
6729 if (info
->create_default_symver
)
6731 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6732 def
.vd_next
= sizeof (Elf_External_Verdef
);
6736 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6737 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6738 + sizeof (Elf_External_Verdaux
));
6741 if (soname_indx
!= (size_t) -1)
6743 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6745 def
.vd_hash
= bfd_elf_hash (soname
);
6746 defaux
.vda_name
= soname_indx
;
6753 name
= lbasename (bfd_get_filename (output_bfd
));
6754 def
.vd_hash
= bfd_elf_hash (name
);
6755 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6757 if (indx
== (size_t) -1)
6759 defaux
.vda_name
= indx
;
6761 defaux
.vda_next
= 0;
6763 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6764 (Elf_External_Verdef
*) p
);
6765 p
+= sizeof (Elf_External_Verdef
);
6766 if (info
->create_default_symver
)
6768 /* Add a symbol representing this version. */
6770 if (! (_bfd_generic_link_add_one_symbol
6771 (info
, dynobj
, 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
= NULL
;
6781 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6784 /* Create a duplicate of the base version with the same
6785 aux block, but different flags. */
6788 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6790 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6791 + sizeof (Elf_External_Verdaux
));
6794 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6795 (Elf_External_Verdef
*) p
);
6796 p
+= sizeof (Elf_External_Verdef
);
6798 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6799 (Elf_External_Verdaux
*) p
);
6800 p
+= sizeof (Elf_External_Verdaux
);
6802 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6805 struct bfd_elf_version_deps
*n
;
6807 /* Don't emit the base version twice. */
6812 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6815 /* Add a symbol representing this version. */
6817 if (! (_bfd_generic_link_add_one_symbol
6818 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6820 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6822 h
= (struct elf_link_hash_entry
*) bh
;
6825 h
->type
= STT_OBJECT
;
6826 h
->verinfo
.vertree
= t
;
6828 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6831 def
.vd_version
= VER_DEF_CURRENT
;
6833 if (t
->globals
.list
== NULL
6834 && t
->locals
.list
== NULL
6836 def
.vd_flags
|= VER_FLG_WEAK
;
6837 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6838 def
.vd_cnt
= cdeps
+ 1;
6839 def
.vd_hash
= bfd_elf_hash (t
->name
);
6840 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6843 /* If a basever node is next, it *must* be the last node in
6844 the chain, otherwise Verdef construction breaks. */
6845 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6846 BFD_ASSERT (t
->next
->next
== NULL
);
6848 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6849 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6850 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6852 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6853 (Elf_External_Verdef
*) p
);
6854 p
+= sizeof (Elf_External_Verdef
);
6856 defaux
.vda_name
= h
->dynstr_index
;
6857 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6859 defaux
.vda_next
= 0;
6860 if (t
->deps
!= NULL
)
6861 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6862 t
->name_indx
= defaux
.vda_name
;
6864 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6865 (Elf_External_Verdaux
*) p
);
6866 p
+= sizeof (Elf_External_Verdaux
);
6868 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6870 if (n
->version_needed
== NULL
)
6872 /* This can happen if there was an error in the
6874 defaux
.vda_name
= 0;
6878 defaux
.vda_name
= n
->version_needed
->name_indx
;
6879 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6882 if (n
->next
== NULL
)
6883 defaux
.vda_next
= 0;
6885 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6887 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6888 (Elf_External_Verdaux
*) p
);
6889 p
+= sizeof (Elf_External_Verdaux
);
6893 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6897 if (info
->gc_sections
&& bed
->can_gc_sections
)
6899 struct elf_gc_sweep_symbol_info sweep_info
;
6901 /* Remove the symbols that were in the swept sections from the
6902 dynamic symbol table. */
6903 sweep_info
.info
= info
;
6904 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6905 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6909 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6912 struct elf_find_verdep_info sinfo
;
6914 /* Work out the size of the version reference section. */
6916 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6917 BFD_ASSERT (s
!= NULL
);
6920 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6921 if (sinfo
.vers
== 0)
6923 sinfo
.failed
= false;
6925 elf_link_hash_traverse (elf_hash_table (info
),
6926 _bfd_elf_link_find_version_dependencies
,
6931 if (elf_tdata (output_bfd
)->verref
== NULL
)
6932 s
->flags
|= SEC_EXCLUDE
;
6935 Elf_Internal_Verneed
*vn
;
6940 /* Build the version dependency section. */
6943 for (vn
= elf_tdata (output_bfd
)->verref
;
6945 vn
= vn
->vn_nextref
)
6947 Elf_Internal_Vernaux
*a
;
6949 size
+= sizeof (Elf_External_Verneed
);
6951 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6952 size
+= sizeof (Elf_External_Vernaux
);
6956 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6957 if (s
->contents
== NULL
)
6961 for (vn
= elf_tdata (output_bfd
)->verref
;
6963 vn
= vn
->vn_nextref
)
6966 Elf_Internal_Vernaux
*a
;
6970 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6973 vn
->vn_version
= VER_NEED_CURRENT
;
6975 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6976 elf_dt_name (vn
->vn_bfd
) != NULL
6977 ? elf_dt_name (vn
->vn_bfd
)
6978 : lbasename (bfd_get_filename
6981 if (indx
== (size_t) -1)
6984 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6985 if (vn
->vn_nextref
== NULL
)
6988 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6989 + caux
* sizeof (Elf_External_Vernaux
));
6991 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6992 (Elf_External_Verneed
*) p
);
6993 p
+= sizeof (Elf_External_Verneed
);
6995 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6997 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6998 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6999 a
->vna_nodename
, false);
7000 if (indx
== (size_t) -1)
7003 if (a
->vna_nextptr
== NULL
)
7006 a
->vna_next
= sizeof (Elf_External_Vernaux
);
7008 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
7009 (Elf_External_Vernaux
*) p
);
7010 p
+= sizeof (Elf_External_Vernaux
);
7014 elf_tdata (output_bfd
)->cverrefs
= crefs
;
7018 if (bfd_link_relocatable (info
)
7019 && !_bfd_elf_size_group_sections (info
))
7022 /* Determine any GNU_STACK segment requirements, after the backend
7023 has had a chance to set a default segment size. */
7024 if (info
->execstack
)
7025 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
7026 else if (info
->noexecstack
)
7027 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
7031 asection
*notesec
= NULL
;
7034 for (inputobj
= info
->input_bfds
;
7036 inputobj
= inputobj
->link
.next
)
7041 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
7043 s
= inputobj
->sections
;
7044 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
7047 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
7050 if (s
->flags
& SEC_CODE
)
7054 else if (bed
->default_execstack
)
7057 if (notesec
|| info
->stacksize
> 0)
7058 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
7059 if (notesec
&& exec
&& bfd_link_relocatable (info
)
7060 && notesec
->output_section
!= bfd_abs_section_ptr
)
7061 notesec
->output_section
->flags
|= SEC_CODE
;
7064 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7066 struct elf_info_failed eif
;
7067 struct elf_link_hash_entry
*h
;
7071 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7072 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7076 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7078 info
->flags
|= DF_SYMBOLIC
;
7086 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7088 if (indx
== (size_t) -1)
7091 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7092 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7096 if (filter_shlib
!= NULL
)
7100 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7101 filter_shlib
, true);
7102 if (indx
== (size_t) -1
7103 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7107 if (auxiliary_filters
!= NULL
)
7109 const char * const *p
;
7111 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7115 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7117 if (indx
== (size_t) -1
7118 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7127 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7129 if (indx
== (size_t) -1
7130 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7134 if (depaudit
!= NULL
)
7138 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7140 if (indx
== (size_t) -1
7141 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7148 /* Find all symbols which were defined in a dynamic object and make
7149 the backend pick a reasonable value for them. */
7150 elf_link_hash_traverse (elf_hash_table (info
),
7151 _bfd_elf_adjust_dynamic_symbol
,
7156 /* Add some entries to the .dynamic section. We fill in some of the
7157 values later, in bfd_elf_final_link, but we must add the entries
7158 now so that we know the final size of the .dynamic section. */
7160 /* If there are initialization and/or finalization functions to
7161 call then add the corresponding DT_INIT/DT_FINI entries. */
7162 h
= (info
->init_function
7163 ? elf_link_hash_lookup (elf_hash_table (info
),
7164 info
->init_function
, false,
7171 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7174 h
= (info
->fini_function
7175 ? elf_link_hash_lookup (elf_hash_table (info
),
7176 info
->fini_function
, false,
7183 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7187 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7188 if (s
!= NULL
&& s
->linker_has_input
)
7190 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7191 if (! bfd_link_executable (info
))
7196 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7197 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7198 && (o
= sub
->sections
) != NULL
7199 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7200 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7201 if (elf_section_data (o
)->this_hdr
.sh_type
7202 == SHT_PREINIT_ARRAY
)
7205 (_("%pB: .preinit_array section is not allowed in DSO"),
7210 bfd_set_error (bfd_error_nonrepresentable_section
);
7214 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7215 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7218 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7219 if (s
!= NULL
&& s
->linker_has_input
)
7221 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7222 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7225 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7226 if (s
!= NULL
&& s
->linker_has_input
)
7228 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7229 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7233 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7234 /* If .dynstr is excluded from the link, we don't want any of
7235 these tags. Strictly, we should be checking each section
7236 individually; This quick check covers for the case where
7237 someone does a /DISCARD/ : { *(*) }. */
7238 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7240 bfd_size_type strsize
;
7242 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7243 if ((info
->emit_hash
7244 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7245 || (info
->emit_gnu_hash
7246 && (bed
->record_xhash_symbol
== NULL
7247 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7248 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7249 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7250 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7251 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7253 || (info
->gnu_flags_1
7254 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_FLAGS_1
,
7255 info
->gnu_flags_1
)))
7260 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7263 /* The backend must work out the sizes of all the other dynamic
7266 && bed
->elf_backend_size_dynamic_sections
!= NULL
7267 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7270 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7272 if (elf_tdata (output_bfd
)->cverdefs
)
7274 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7276 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7277 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7281 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7283 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7286 else if (info
->flags
& DF_BIND_NOW
)
7288 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7294 if (bfd_link_executable (info
))
7295 info
->flags_1
&= ~ (DF_1_INITFIRST
7298 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7302 if (elf_tdata (output_bfd
)->cverrefs
)
7304 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7306 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7307 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7311 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7312 && elf_tdata (output_bfd
)->cverdefs
== 0)
7313 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7317 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7318 s
->flags
|= SEC_EXCLUDE
;
7324 /* Find the first non-excluded output section. We'll use its
7325 section symbol for some emitted relocs. */
7327 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7330 asection
*found
= NULL
;
7332 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7333 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7334 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7337 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7340 elf_hash_table (info
)->text_index_section
= found
;
7343 /* Find two non-excluded output sections, one for code, one for data.
7344 We'll use their section symbols for some emitted relocs. */
7346 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7349 asection
*found
= NULL
;
7351 /* Data first, since setting text_index_section changes
7352 _bfd_elf_omit_section_dynsym_default. */
7353 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7354 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7355 && !(s
->flags
& SEC_READONLY
)
7356 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7359 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7362 elf_hash_table (info
)->data_index_section
= found
;
7364 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7365 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7366 && (s
->flags
& SEC_READONLY
)
7367 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7372 elf_hash_table (info
)->text_index_section
= found
;
7375 #define GNU_HASH_SECTION_NAME(bed) \
7376 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7379 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7381 const struct elf_backend_data
*bed
;
7382 unsigned long section_sym_count
;
7383 bfd_size_type dynsymcount
= 0;
7385 if (!is_elf_hash_table (info
->hash
))
7388 bed
= get_elf_backend_data (output_bfd
);
7389 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7391 /* Assign dynsym indices. In a shared library we generate a section
7392 symbol for each output section, which come first. Next come all
7393 of the back-end allocated local dynamic syms, followed by the rest
7394 of the global symbols.
7396 This is usually not needed for static binaries, however backends
7397 can request to always do it, e.g. the MIPS backend uses dynamic
7398 symbol counts to lay out GOT, which will be produced in the
7399 presence of GOT relocations even in static binaries (holding fixed
7400 data in that case, to satisfy those relocations). */
7402 if (elf_hash_table (info
)->dynamic_sections_created
7403 || bed
->always_renumber_dynsyms
)
7404 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7405 §ion_sym_count
);
7407 if (elf_hash_table (info
)->dynamic_sections_created
)
7411 unsigned int dtagcount
;
7413 dynobj
= elf_hash_table (info
)->dynobj
;
7415 /* Work out the size of the symbol version section. */
7416 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7417 BFD_ASSERT (s
!= NULL
);
7418 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7420 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7421 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7422 if (s
->contents
== NULL
)
7425 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7429 /* Set the size of the .dynsym and .hash sections. We counted
7430 the number of dynamic symbols in elf_link_add_object_symbols.
7431 We will build the contents of .dynsym and .hash when we build
7432 the final symbol table, because until then we do not know the
7433 correct value to give the symbols. We built the .dynstr
7434 section as we went along in elf_link_add_object_symbols. */
7435 s
= elf_hash_table (info
)->dynsym
;
7436 BFD_ASSERT (s
!= NULL
);
7437 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7439 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7440 if (s
->contents
== NULL
)
7443 /* The first entry in .dynsym is a dummy symbol. Clear all the
7444 section syms, in case we don't output them all. */
7445 ++section_sym_count
;
7446 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7448 elf_hash_table (info
)->bucketcount
= 0;
7450 /* Compute the size of the hashing table. As a side effect this
7451 computes the hash values for all the names we export. */
7452 if (info
->emit_hash
)
7454 unsigned long int *hashcodes
;
7455 struct hash_codes_info hashinf
;
7457 unsigned long int nsyms
;
7459 size_t hash_entry_size
;
7461 /* Compute the hash values for all exported symbols. At the same
7462 time store the values in an array so that we could use them for
7464 amt
= dynsymcount
* sizeof (unsigned long int);
7465 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7466 if (hashcodes
== NULL
)
7468 hashinf
.hashcodes
= hashcodes
;
7469 hashinf
.error
= false;
7471 /* Put all hash values in HASHCODES. */
7472 elf_link_hash_traverse (elf_hash_table (info
),
7473 elf_collect_hash_codes
, &hashinf
);
7480 nsyms
= hashinf
.hashcodes
- hashcodes
;
7482 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7485 if (bucketcount
== 0 && nsyms
> 0)
7488 elf_hash_table (info
)->bucketcount
= bucketcount
;
7490 s
= bfd_get_linker_section (dynobj
, ".hash");
7491 BFD_ASSERT (s
!= NULL
);
7492 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7493 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7494 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7495 if (s
->contents
== NULL
)
7498 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7499 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7500 s
->contents
+ hash_entry_size
);
7503 if (info
->emit_gnu_hash
)
7506 unsigned char *contents
;
7507 struct collect_gnu_hash_codes cinfo
;
7511 memset (&cinfo
, 0, sizeof (cinfo
));
7513 /* Compute the hash values for all exported symbols. At the same
7514 time store the values in an array so that we could use them for
7516 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7517 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7518 if (cinfo
.hashcodes
== NULL
)
7521 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7522 cinfo
.min_dynindx
= -1;
7523 cinfo
.output_bfd
= output_bfd
;
7526 /* Put all hash values in HASHCODES. */
7527 elf_link_hash_traverse (elf_hash_table (info
),
7528 elf_collect_gnu_hash_codes
, &cinfo
);
7531 free (cinfo
.hashcodes
);
7536 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7538 if (bucketcount
== 0)
7540 free (cinfo
.hashcodes
);
7544 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7545 BFD_ASSERT (s
!= NULL
);
7547 if (cinfo
.nsyms
== 0)
7549 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7550 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7551 free (cinfo
.hashcodes
);
7552 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7553 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7554 if (contents
== NULL
)
7556 s
->contents
= contents
;
7557 /* 1 empty bucket. */
7558 bfd_put_32 (output_bfd
, 1, contents
);
7559 /* SYMIDX above the special symbol 0. */
7560 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7561 /* Just one word for bitmask. */
7562 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7563 /* Only hash fn bloom filter. */
7564 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7565 /* No hashes are valid - empty bitmask. */
7566 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7567 /* No hashes in the only bucket. */
7568 bfd_put_32 (output_bfd
, 0,
7569 contents
+ 16 + bed
->s
->arch_size
/ 8);
7573 unsigned long int maskwords
, maskbitslog2
, x
;
7574 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7578 while ((x
>>= 1) != 0)
7580 if (maskbitslog2
< 3)
7582 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7583 maskbitslog2
= maskbitslog2
+ 3;
7585 maskbitslog2
= maskbitslog2
+ 2;
7586 if (bed
->s
->arch_size
== 64)
7588 if (maskbitslog2
== 5)
7594 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7595 cinfo
.shift2
= maskbitslog2
;
7596 cinfo
.maskbits
= 1 << maskbitslog2
;
7597 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7598 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7599 amt
+= maskwords
* sizeof (bfd_vma
);
7600 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7601 if (cinfo
.bitmask
== NULL
)
7603 free (cinfo
.hashcodes
);
7607 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7608 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7609 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7610 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7612 /* Determine how often each hash bucket is used. */
7613 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7614 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7615 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7617 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7618 if (cinfo
.counts
[i
] != 0)
7620 cinfo
.indx
[i
] = cnt
;
7621 cnt
+= cinfo
.counts
[i
];
7623 BFD_ASSERT (cnt
== dynsymcount
);
7624 cinfo
.bucketcount
= bucketcount
;
7625 cinfo
.local_indx
= cinfo
.min_dynindx
;
7627 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7628 s
->size
+= cinfo
.maskbits
/ 8;
7629 if (bed
->record_xhash_symbol
!= NULL
)
7630 s
->size
+= cinfo
.nsyms
* 4;
7631 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7632 if (contents
== NULL
)
7634 free (cinfo
.bitmask
);
7635 free (cinfo
.hashcodes
);
7639 s
->contents
= contents
;
7640 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7641 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7642 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7643 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7644 contents
+= 16 + cinfo
.maskbits
/ 8;
7646 for (i
= 0; i
< bucketcount
; ++i
)
7648 if (cinfo
.counts
[i
] == 0)
7649 bfd_put_32 (output_bfd
, 0, contents
);
7651 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7655 cinfo
.contents
= contents
;
7657 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7658 /* Renumber dynamic symbols, if populating .gnu.hash section.
7659 If using .MIPS.xhash, populate the translation table. */
7660 elf_link_hash_traverse (elf_hash_table (info
),
7661 elf_gnu_hash_process_symidx
, &cinfo
);
7663 contents
= s
->contents
+ 16;
7664 for (i
= 0; i
< maskwords
; ++i
)
7666 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7668 contents
+= bed
->s
->arch_size
/ 8;
7671 free (cinfo
.bitmask
);
7672 free (cinfo
.hashcodes
);
7676 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7677 BFD_ASSERT (s
!= NULL
);
7679 elf_finalize_dynstr (output_bfd
, info
);
7681 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7683 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7684 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7691 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7694 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7697 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7698 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7701 /* Finish SHF_MERGE section merging. */
7704 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7709 if (!is_elf_hash_table (info
->hash
))
7712 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7713 if ((ibfd
->flags
& DYNAMIC
) == 0
7714 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7715 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7716 == get_elf_backend_data (obfd
)->s
->elfclass
))
7717 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7718 if ((sec
->flags
& SEC_MERGE
) != 0
7719 && !bfd_is_abs_section (sec
->output_section
))
7721 struct bfd_elf_section_data
*secdata
;
7723 secdata
= elf_section_data (sec
);
7724 if (! _bfd_add_merge_section (obfd
,
7725 &elf_hash_table (info
)->merge_info
,
7726 sec
, &secdata
->sec_info
))
7728 else if (secdata
->sec_info
)
7729 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7732 if (elf_hash_table (info
)->merge_info
!= NULL
)
7733 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7734 merge_sections_remove_hook
);
7738 /* Create an entry in an ELF linker hash table. */
7740 struct bfd_hash_entry
*
7741 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7742 struct bfd_hash_table
*table
,
7745 /* Allocate the structure if it has not already been allocated by a
7749 entry
= (struct bfd_hash_entry
*)
7750 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7755 /* Call the allocation method of the superclass. */
7756 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7759 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7760 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7762 /* Set local fields. */
7765 ret
->got
= htab
->init_got_refcount
;
7766 ret
->plt
= htab
->init_plt_refcount
;
7767 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7768 - offsetof (struct elf_link_hash_entry
, size
)));
7769 /* Assume that we have been called by a non-ELF symbol reader.
7770 This flag is then reset by the code which reads an ELF input
7771 file. This ensures that a symbol created by a non-ELF symbol
7772 reader will have the flag set correctly. */
7779 /* Copy data from an indirect symbol to its direct symbol, hiding the
7780 old indirect symbol. Also used for copying flags to a weakdef. */
7783 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7784 struct elf_link_hash_entry
*dir
,
7785 struct elf_link_hash_entry
*ind
)
7787 struct elf_link_hash_table
*htab
;
7789 if (ind
->dyn_relocs
!= NULL
)
7791 if (dir
->dyn_relocs
!= NULL
)
7793 struct elf_dyn_relocs
**pp
;
7794 struct elf_dyn_relocs
*p
;
7796 /* Add reloc counts against the indirect sym to the direct sym
7797 list. Merge any entries against the same section. */
7798 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
7800 struct elf_dyn_relocs
*q
;
7802 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
7803 if (q
->sec
== p
->sec
)
7805 q
->pc_count
+= p
->pc_count
;
7806 q
->count
+= p
->count
;
7813 *pp
= dir
->dyn_relocs
;
7816 dir
->dyn_relocs
= ind
->dyn_relocs
;
7817 ind
->dyn_relocs
= NULL
;
7820 /* Copy down any references that we may have already seen to the
7821 symbol which just became indirect. */
7823 if (dir
->versioned
!= versioned_hidden
)
7824 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7825 dir
->ref_regular
|= ind
->ref_regular
;
7826 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7827 dir
->non_got_ref
|= ind
->non_got_ref
;
7828 dir
->needs_plt
|= ind
->needs_plt
;
7829 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7831 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7834 /* Copy over the global and procedure linkage table refcount entries.
7835 These may have been already set up by a check_relocs routine. */
7836 htab
= elf_hash_table (info
);
7837 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7839 if (dir
->got
.refcount
< 0)
7840 dir
->got
.refcount
= 0;
7841 dir
->got
.refcount
+= ind
->got
.refcount
;
7842 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7845 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7847 if (dir
->plt
.refcount
< 0)
7848 dir
->plt
.refcount
= 0;
7849 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7850 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7853 if (ind
->dynindx
!= -1)
7855 if (dir
->dynindx
!= -1)
7856 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7857 dir
->dynindx
= ind
->dynindx
;
7858 dir
->dynstr_index
= ind
->dynstr_index
;
7860 ind
->dynstr_index
= 0;
7865 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7866 struct elf_link_hash_entry
*h
,
7869 /* STT_GNU_IFUNC symbol must go through PLT. */
7870 if (h
->type
!= STT_GNU_IFUNC
)
7872 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7877 h
->forced_local
= 1;
7878 if (h
->dynindx
!= -1)
7880 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7883 h
->dynstr_index
= 0;
7888 /* Hide a symbol. */
7891 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
7892 struct bfd_link_info
*info
,
7893 struct bfd_link_hash_entry
*h
)
7895 if (is_elf_hash_table (info
->hash
))
7897 const struct elf_backend_data
*bed
7898 = get_elf_backend_data (output_bfd
);
7899 struct elf_link_hash_entry
*eh
7900 = (struct elf_link_hash_entry
*) h
;
7901 bed
->elf_backend_hide_symbol (info
, eh
, true);
7902 eh
->def_dynamic
= 0;
7903 eh
->ref_dynamic
= 0;
7904 eh
->dynamic_def
= 0;
7908 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7912 _bfd_elf_link_hash_table_init
7913 (struct elf_link_hash_table
*table
,
7915 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7916 struct bfd_hash_table
*,
7918 unsigned int entsize
,
7919 enum elf_target_id target_id
)
7922 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7924 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7925 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7926 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7927 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7928 /* The first dynamic symbol is a dummy. */
7929 table
->dynsymcount
= 1;
7931 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7933 table
->root
.type
= bfd_link_elf_hash_table
;
7934 table
->hash_table_id
= target_id
;
7935 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
7940 /* Create an ELF linker hash table. */
7942 struct bfd_link_hash_table
*
7943 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7945 struct elf_link_hash_table
*ret
;
7946 size_t amt
= sizeof (struct elf_link_hash_table
);
7948 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7952 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7953 sizeof (struct elf_link_hash_entry
),
7959 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7964 /* Destroy an ELF linker hash table. */
7967 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7969 struct elf_link_hash_table
*htab
;
7971 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7972 if (htab
->dynstr
!= NULL
)
7973 _bfd_elf_strtab_free (htab
->dynstr
);
7974 _bfd_merge_sections_free (htab
->merge_info
);
7975 _bfd_generic_link_hash_table_free (obfd
);
7978 /* This is a hook for the ELF emulation code in the generic linker to
7979 tell the backend linker what file name to use for the DT_NEEDED
7980 entry for a dynamic object. */
7983 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7985 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7986 && bfd_get_format (abfd
) == bfd_object
)
7987 elf_dt_name (abfd
) = name
;
7991 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7994 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7995 && bfd_get_format (abfd
) == bfd_object
)
7996 lib_class
= elf_dyn_lib_class (abfd
);
8003 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
8005 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8006 && bfd_get_format (abfd
) == bfd_object
)
8007 elf_dyn_lib_class (abfd
) = lib_class
;
8010 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8011 the linker ELF emulation code. */
8013 struct bfd_link_needed_list
*
8014 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8015 struct bfd_link_info
*info
)
8017 if (! is_elf_hash_table (info
->hash
))
8019 return elf_hash_table (info
)->needed
;
8022 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8023 hook for the linker ELF emulation code. */
8025 struct bfd_link_needed_list
*
8026 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8027 struct bfd_link_info
*info
)
8029 if (! is_elf_hash_table (info
->hash
))
8031 return elf_hash_table (info
)->runpath
;
8034 /* Get the name actually used for a dynamic object for a link. This
8035 is the SONAME entry if there is one. Otherwise, it is the string
8036 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8039 bfd_elf_get_dt_soname (bfd
*abfd
)
8041 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8042 && bfd_get_format (abfd
) == bfd_object
)
8043 return elf_dt_name (abfd
);
8047 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8048 the ELF linker emulation code. */
8051 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
8052 struct bfd_link_needed_list
**pneeded
)
8055 bfd_byte
*dynbuf
= NULL
;
8056 unsigned int elfsec
;
8057 unsigned long shlink
;
8058 bfd_byte
*extdyn
, *extdynend
;
8060 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
8064 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
8065 || bfd_get_format (abfd
) != bfd_object
)
8068 s
= bfd_get_section_by_name (abfd
, ".dynamic");
8069 if (s
== NULL
|| s
->size
== 0)
8072 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
8075 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8076 if (elfsec
== SHN_BAD
)
8079 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8081 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8082 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8085 extdynend
= extdyn
+ s
->size
;
8086 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
8088 Elf_Internal_Dyn dyn
;
8090 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8092 if (dyn
.d_tag
== DT_NULL
)
8095 if (dyn
.d_tag
== DT_NEEDED
)
8098 struct bfd_link_needed_list
*l
;
8099 unsigned int tagv
= dyn
.d_un
.d_val
;
8102 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8107 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8127 struct elf_symbuf_symbol
8129 unsigned long st_name
; /* Symbol name, index in string tbl */
8130 unsigned char st_info
; /* Type and binding attributes */
8131 unsigned char st_other
; /* Visibilty, and target specific */
8134 struct elf_symbuf_head
8136 struct elf_symbuf_symbol
*ssym
;
8138 unsigned int st_shndx
;
8145 Elf_Internal_Sym
*isym
;
8146 struct elf_symbuf_symbol
*ssym
;
8152 /* Sort references to symbols by ascending section number. */
8155 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8157 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8158 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8160 if (s1
->st_shndx
!= s2
->st_shndx
)
8161 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8162 /* Final sort by the address of the sym in the symbuf ensures
8165 return s1
> s2
? 1 : -1;
8170 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8172 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8173 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8174 int ret
= strcmp (s1
->name
, s2
->name
);
8177 if (s1
->u
.p
!= s2
->u
.p
)
8178 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8182 static struct elf_symbuf_head
*
8183 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8185 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8186 struct elf_symbuf_symbol
*ssym
;
8187 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8188 size_t i
, shndx_count
, total_size
, amt
;
8190 amt
= symcount
* sizeof (*indbuf
);
8191 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8195 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8196 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8197 *ind
++ = &isymbuf
[i
];
8200 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8201 elf_sort_elf_symbol
);
8204 if (indbufend
> indbuf
)
8205 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8206 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8209 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8210 + (indbufend
- indbuf
) * sizeof (*ssym
));
8211 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8212 if (ssymbuf
== NULL
)
8218 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8219 ssymbuf
->ssym
= NULL
;
8220 ssymbuf
->count
= shndx_count
;
8221 ssymbuf
->st_shndx
= 0;
8222 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8224 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8227 ssymhead
->ssym
= ssym
;
8228 ssymhead
->count
= 0;
8229 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8231 ssym
->st_name
= (*ind
)->st_name
;
8232 ssym
->st_info
= (*ind
)->st_info
;
8233 ssym
->st_other
= (*ind
)->st_other
;
8236 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8237 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
8244 /* Check if 2 sections define the same set of local and global
8248 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8249 struct bfd_link_info
*info
)
8252 const struct elf_backend_data
*bed1
, *bed2
;
8253 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8254 size_t symcount1
, symcount2
;
8255 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8256 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8257 Elf_Internal_Sym
*isym
, *isymend
;
8258 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8259 size_t count1
, count2
, sec_count1
, sec_count2
, i
;
8260 unsigned int shndx1
, shndx2
;
8262 bool ignore_section_symbol_p
;
8267 /* Both sections have to be in ELF. */
8268 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8269 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8272 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8275 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8276 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8277 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8280 bed1
= get_elf_backend_data (bfd1
);
8281 bed2
= get_elf_backend_data (bfd2
);
8282 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8283 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8284 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8285 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8287 if (symcount1
== 0 || symcount2
== 0)
8293 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8294 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8296 /* Ignore section symbols only when matching non-debugging sections
8297 or linkonce section with comdat section. */
8298 ignore_section_symbol_p
8299 = ((sec1
->flags
& SEC_DEBUGGING
) == 0
8300 || ((elf_section_flags (sec1
) & SHF_GROUP
)
8301 != (elf_section_flags (sec2
) & SHF_GROUP
)));
8303 if (ssymbuf1
== NULL
)
8305 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8307 if (isymbuf1
== NULL
)
8310 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8312 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8313 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8317 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8319 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8321 if (isymbuf2
== NULL
)
8324 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8326 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8327 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8331 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8333 /* Optimized faster version. */
8335 struct elf_symbol
*symp
;
8336 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8339 hi
= ssymbuf1
->count
;
8345 mid
= (lo
+ hi
) / 2;
8346 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8348 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8352 count1
= ssymbuf1
[mid
].count
;
8357 if (ignore_section_symbol_p
)
8359 for (i
= 0; i
< count1
; i
++)
8360 if (ELF_ST_TYPE (ssymbuf1
->ssym
[i
].st_info
) == STT_SECTION
)
8362 count1
-= sec_count1
;
8366 hi
= ssymbuf2
->count
;
8372 mid
= (lo
+ hi
) / 2;
8373 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8375 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8379 count2
= ssymbuf2
[mid
].count
;
8384 if (ignore_section_symbol_p
)
8386 for (i
= 0; i
< count2
; i
++)
8387 if (ELF_ST_TYPE (ssymbuf2
->ssym
[i
].st_info
) == STT_SECTION
)
8389 count2
-= sec_count2
;
8392 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8396 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8398 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8399 if (symtable1
== NULL
|| symtable2
== NULL
)
8403 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
+ sec_count1
;
8404 ssym
< ssymend
; ssym
++)
8406 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8408 symp
->u
.ssym
= ssym
;
8409 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8416 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
+ sec_count2
;
8417 ssym
< ssymend
; ssym
++)
8419 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8421 symp
->u
.ssym
= ssym
;
8422 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8428 /* Sort symbol by name. */
8429 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8430 elf_sym_name_compare
);
8431 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8432 elf_sym_name_compare
);
8434 for (i
= 0; i
< count1
; i
++)
8435 /* Two symbols must have the same binding, type and name. */
8436 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8437 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8438 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8445 symtable1
= (struct elf_symbol
*)
8446 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8447 symtable2
= (struct elf_symbol
*)
8448 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8449 if (symtable1
== NULL
|| symtable2
== NULL
)
8452 /* Count definitions in the section. */
8454 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8455 if (isym
->st_shndx
== shndx1
8456 && (!ignore_section_symbol_p
8457 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8458 symtable1
[count1
++].u
.isym
= isym
;
8461 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8462 if (isym
->st_shndx
== shndx2
8463 && (!ignore_section_symbol_p
8464 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8465 symtable2
[count2
++].u
.isym
= isym
;
8467 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8470 for (i
= 0; i
< count1
; i
++)
8472 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8473 symtable1
[i
].u
.isym
->st_name
);
8475 for (i
= 0; i
< count2
; i
++)
8477 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8478 symtable2
[i
].u
.isym
->st_name
);
8480 /* Sort symbol by name. */
8481 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8482 elf_sym_name_compare
);
8483 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8484 elf_sym_name_compare
);
8486 for (i
= 0; i
< count1
; i
++)
8487 /* Two symbols must have the same binding, type and name. */
8488 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8489 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8490 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8504 /* Return TRUE if 2 section types are compatible. */
8507 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8508 bfd
*bbfd
, const asection
*bsec
)
8512 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8513 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8516 return elf_section_type (asec
) == elf_section_type (bsec
);
8519 /* Final phase of ELF linker. */
8521 /* A structure we use to avoid passing large numbers of arguments. */
8523 struct elf_final_link_info
8525 /* General link information. */
8526 struct bfd_link_info
*info
;
8529 /* Symbol string table. */
8530 struct elf_strtab_hash
*symstrtab
;
8531 /* .hash section. */
8533 /* symbol version section (.gnu.version). */
8534 asection
*symver_sec
;
8535 /* Buffer large enough to hold contents of any section. */
8537 /* Buffer large enough to hold external relocs of any section. */
8538 void *external_relocs
;
8539 /* Buffer large enough to hold internal relocs of any section. */
8540 Elf_Internal_Rela
*internal_relocs
;
8541 /* Buffer large enough to hold external local symbols of any input
8543 bfd_byte
*external_syms
;
8544 /* And a buffer for symbol section indices. */
8545 Elf_External_Sym_Shndx
*locsym_shndx
;
8546 /* Buffer large enough to hold internal local symbols of any input
8548 Elf_Internal_Sym
*internal_syms
;
8549 /* Array large enough to hold a symbol index for each local symbol
8550 of any input BFD. */
8552 /* Array large enough to hold a section pointer for each local
8553 symbol of any input BFD. */
8554 asection
**sections
;
8555 /* Buffer for SHT_SYMTAB_SHNDX section. */
8556 Elf_External_Sym_Shndx
*symshndxbuf
;
8557 /* Number of STT_FILE syms seen. */
8558 size_t filesym_count
;
8559 /* Local symbol hash table. */
8560 struct bfd_hash_table local_hash_table
;
8563 struct local_hash_entry
8565 /* Base hash table entry structure. */
8566 struct bfd_hash_entry root
;
8567 /* Size of the local symbol name. */
8569 /* Number of the duplicated local symbol names. */
8573 /* Create an entry in the local symbol hash table. */
8575 static struct bfd_hash_entry
*
8576 local_hash_newfunc (struct bfd_hash_entry
*entry
,
8577 struct bfd_hash_table
*table
,
8581 /* Allocate the structure if it has not already been allocated by a
8585 entry
= bfd_hash_allocate (table
,
8586 sizeof (struct local_hash_entry
));
8591 /* Call the allocation method of the superclass. */
8592 entry
= bfd_hash_newfunc (entry
, table
, string
);
8595 ((struct local_hash_entry
*) entry
)->count
= 0;
8596 ((struct local_hash_entry
*) entry
)->size
= 0;
8602 /* This struct is used to pass information to elf_link_output_extsym. */
8604 struct elf_outext_info
8609 struct elf_final_link_info
*flinfo
;
8613 /* Support for evaluating a complex relocation.
8615 Complex relocations are generalized, self-describing relocations. The
8616 implementation of them consists of two parts: complex symbols, and the
8617 relocations themselves.
8619 The relocations use a reserved elf-wide relocation type code (R_RELC
8620 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8621 information (start bit, end bit, word width, etc) into the addend. This
8622 information is extracted from CGEN-generated operand tables within gas.
8624 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8625 internal) representing prefix-notation expressions, including but not
8626 limited to those sorts of expressions normally encoded as addends in the
8627 addend field. The symbol mangling format is:
8630 | <unary-operator> ':' <node>
8631 | <binary-operator> ':' <node> ':' <node>
8634 <literal> := 's' <digits=N> ':' <N character symbol name>
8635 | 'S' <digits=N> ':' <N character section name>
8639 <binary-operator> := as in C
8640 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8643 set_symbol_value (bfd
*bfd_with_globals
,
8644 Elf_Internal_Sym
*isymbuf
,
8649 struct elf_link_hash_entry
**sym_hashes
;
8650 struct elf_link_hash_entry
*h
;
8651 size_t extsymoff
= locsymcount
;
8653 if (symidx
< locsymcount
)
8655 Elf_Internal_Sym
*sym
;
8657 sym
= isymbuf
+ symidx
;
8658 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8660 /* It is a local symbol: move it to the
8661 "absolute" section and give it a value. */
8662 sym
->st_shndx
= SHN_ABS
;
8663 sym
->st_value
= val
;
8666 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8670 /* It is a global symbol: set its link type
8671 to "defined" and give it a value. */
8673 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8674 h
= sym_hashes
[symidx
- extsymoff
];
8675 while (h
->root
.type
== bfd_link_hash_indirect
8676 || h
->root
.type
== bfd_link_hash_warning
)
8677 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8678 h
->root
.type
= bfd_link_hash_defined
;
8679 h
->root
.u
.def
.value
= val
;
8680 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8684 resolve_symbol (const char *name
,
8686 struct elf_final_link_info
*flinfo
,
8688 Elf_Internal_Sym
*isymbuf
,
8691 Elf_Internal_Sym
*sym
;
8692 struct bfd_link_hash_entry
*global_entry
;
8693 const char *candidate
= NULL
;
8694 Elf_Internal_Shdr
*symtab_hdr
;
8697 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8699 for (i
= 0; i
< locsymcount
; ++ i
)
8703 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8706 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8707 symtab_hdr
->sh_link
,
8710 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8711 name
, candidate
, (unsigned long) sym
->st_value
);
8713 if (candidate
&& strcmp (candidate
, name
) == 0)
8715 asection
*sec
= flinfo
->sections
[i
];
8717 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8718 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8720 printf ("Found symbol with value %8.8lx\n",
8721 (unsigned long) *result
);
8727 /* Hmm, haven't found it yet. perhaps it is a global. */
8728 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8729 false, false, true);
8733 if (global_entry
->type
== bfd_link_hash_defined
8734 || global_entry
->type
== bfd_link_hash_defweak
)
8736 *result
= (global_entry
->u
.def
.value
8737 + global_entry
->u
.def
.section
->output_section
->vma
8738 + global_entry
->u
.def
.section
->output_offset
);
8740 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8741 global_entry
->root
.string
, (unsigned long) *result
);
8749 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8750 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8751 names like "foo.end" which is the end address of section "foo". */
8754 resolve_section (const char *name
,
8762 for (curr
= sections
; curr
; curr
= curr
->next
)
8763 if (strcmp (curr
->name
, name
) == 0)
8765 *result
= curr
->vma
;
8769 /* Hmm. still haven't found it. try pseudo-section names. */
8770 /* FIXME: This could be coded more efficiently... */
8771 for (curr
= sections
; curr
; curr
= curr
->next
)
8773 len
= strlen (curr
->name
);
8774 if (len
> strlen (name
))
8777 if (strncmp (curr
->name
, name
, len
) == 0)
8779 if (startswith (name
+ len
, ".end"))
8781 *result
= (curr
->vma
8782 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
8786 /* Insert more pseudo-section names here, if you like. */
8794 undefined_reference (const char *reftype
, const char *name
)
8796 /* xgettext:c-format */
8797 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8799 bfd_set_error (bfd_error_bad_value
);
8803 eval_symbol (bfd_vma
*result
,
8806 struct elf_final_link_info
*flinfo
,
8808 Elf_Internal_Sym
*isymbuf
,
8817 const char *sym
= *symp
;
8819 bool symbol_is_section
= false;
8824 if (len
< 1 || len
> sizeof (symbuf
))
8826 bfd_set_error (bfd_error_invalid_operation
);
8839 *result
= strtoul (sym
, (char **) symp
, 16);
8843 symbol_is_section
= true;
8847 symlen
= strtol (sym
, (char **) symp
, 10);
8848 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8850 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8852 bfd_set_error (bfd_error_invalid_operation
);
8856 memcpy (symbuf
, sym
, symlen
);
8857 symbuf
[symlen
] = '\0';
8858 *symp
= sym
+ symlen
;
8860 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8861 the symbol as a section, or vice-versa. so we're pretty liberal in our
8862 interpretation here; section means "try section first", not "must be a
8863 section", and likewise with symbol. */
8865 if (symbol_is_section
)
8867 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8868 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8869 isymbuf
, locsymcount
))
8871 undefined_reference ("section", symbuf
);
8877 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8878 isymbuf
, locsymcount
)
8879 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8882 undefined_reference ("symbol", symbuf
);
8889 /* All that remains are operators. */
8891 #define UNARY_OP(op) \
8892 if (startswith (sym, #op)) \
8894 sym += strlen (#op); \
8898 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8899 isymbuf, locsymcount, signed_p)) \
8902 *result = op ((bfd_signed_vma) a); \
8908 #define BINARY_OP_HEAD(op) \
8909 if (startswith (sym, #op)) \
8911 sym += strlen (#op); \
8915 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8916 isymbuf, locsymcount, signed_p)) \
8919 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8920 isymbuf, locsymcount, signed_p)) \
8922 #define BINARY_OP_TAIL(op) \
8924 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8929 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8933 BINARY_OP_HEAD (<<);
8934 if (b
>= sizeof (a
) * CHAR_BIT
)
8940 BINARY_OP_TAIL (<<);
8941 BINARY_OP_HEAD (>>);
8942 if (b
>= sizeof (a
) * CHAR_BIT
)
8944 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
8947 BINARY_OP_TAIL (>>);
8960 _bfd_error_handler (_("division by zero"));
8961 bfd_set_error (bfd_error_bad_value
);
8968 _bfd_error_handler (_("division by zero"));
8969 bfd_set_error (bfd_error_bad_value
);
8982 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8983 bfd_set_error (bfd_error_invalid_operation
);
8989 put_value (bfd_vma size
,
8990 unsigned long chunksz
,
8995 location
+= (size
- chunksz
);
8997 for (; size
; size
-= chunksz
, location
-= chunksz
)
9002 bfd_put_8 (input_bfd
, x
, location
);
9006 bfd_put_16 (input_bfd
, x
, location
);
9010 bfd_put_32 (input_bfd
, x
, location
);
9011 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9017 bfd_put_64 (input_bfd
, x
, location
);
9018 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9031 get_value (bfd_vma size
,
9032 unsigned long chunksz
,
9039 /* Sanity checks. */
9040 BFD_ASSERT (chunksz
<= sizeof (x
)
9043 && (size
% chunksz
) == 0
9044 && input_bfd
!= NULL
9045 && location
!= NULL
);
9047 if (chunksz
== sizeof (x
))
9049 BFD_ASSERT (size
== chunksz
);
9051 /* Make sure that we do not perform an undefined shift operation.
9052 We know that size == chunksz so there will only be one iteration
9053 of the loop below. */
9057 shift
= 8 * chunksz
;
9059 for (; size
; size
-= chunksz
, location
+= chunksz
)
9064 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
9067 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
9070 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
9074 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
9085 decode_complex_addend (unsigned long *start
, /* in bits */
9086 unsigned long *oplen
, /* in bits */
9087 unsigned long *len
, /* in bits */
9088 unsigned long *wordsz
, /* in bytes */
9089 unsigned long *chunksz
, /* in bytes */
9090 unsigned long *lsb0_p
,
9091 unsigned long *signed_p
,
9092 unsigned long *trunc_p
,
9093 unsigned long encoded
)
9095 * start
= encoded
& 0x3F;
9096 * len
= (encoded
>> 6) & 0x3F;
9097 * oplen
= (encoded
>> 12) & 0x3F;
9098 * wordsz
= (encoded
>> 18) & 0xF;
9099 * chunksz
= (encoded
>> 22) & 0xF;
9100 * lsb0_p
= (encoded
>> 27) & 1;
9101 * signed_p
= (encoded
>> 28) & 1;
9102 * trunc_p
= (encoded
>> 29) & 1;
9105 bfd_reloc_status_type
9106 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
9107 asection
*input_section
,
9109 Elf_Internal_Rela
*rel
,
9112 bfd_vma shift
, x
, mask
;
9113 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9114 bfd_reloc_status_type r
;
9115 bfd_size_type octets
;
9117 /* Perform this reloc, since it is complex.
9118 (this is not to say that it necessarily refers to a complex
9119 symbol; merely that it is a self-describing CGEN based reloc.
9120 i.e. the addend has the complete reloc information (bit start, end,
9121 word size, etc) encoded within it.). */
9123 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9124 &chunksz
, &lsb0_p
, &signed_p
,
9125 &trunc_p
, rel
->r_addend
);
9127 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9130 shift
= (start
+ 1) - len
;
9132 shift
= (8 * wordsz
) - (start
+ len
);
9134 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9135 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9138 printf ("Doing complex reloc: "
9139 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9140 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9141 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9142 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9143 oplen
, (unsigned long) x
, (unsigned long) mask
,
9144 (unsigned long) relocation
);
9149 /* Now do an overflow check. */
9150 r
= bfd_check_overflow ((signed_p
9151 ? complain_overflow_signed
9152 : complain_overflow_unsigned
),
9153 len
, 0, (8 * wordsz
),
9157 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9160 printf (" relocation: %8.8lx\n"
9161 " shifted mask: %8.8lx\n"
9162 " shifted/masked reloc: %8.8lx\n"
9163 " result: %8.8lx\n",
9164 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9165 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9167 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9171 /* Functions to read r_offset from external (target order) reloc
9172 entry. Faster than bfd_getl32 et al, because we let the compiler
9173 know the value is aligned. */
9176 ext32l_r_offset (const void *p
)
9183 const union aligned32
*a
9184 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9186 uint32_t aval
= ( (uint32_t) a
->c
[0]
9187 | (uint32_t) a
->c
[1] << 8
9188 | (uint32_t) a
->c
[2] << 16
9189 | (uint32_t) a
->c
[3] << 24);
9194 ext32b_r_offset (const void *p
)
9201 const union aligned32
*a
9202 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9204 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9205 | (uint32_t) a
->c
[1] << 16
9206 | (uint32_t) a
->c
[2] << 8
9207 | (uint32_t) a
->c
[3]);
9211 #ifdef BFD_HOST_64_BIT
9213 ext64l_r_offset (const void *p
)
9220 const union aligned64
*a
9221 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9223 uint64_t aval
= ( (uint64_t) a
->c
[0]
9224 | (uint64_t) a
->c
[1] << 8
9225 | (uint64_t) a
->c
[2] << 16
9226 | (uint64_t) a
->c
[3] << 24
9227 | (uint64_t) a
->c
[4] << 32
9228 | (uint64_t) a
->c
[5] << 40
9229 | (uint64_t) a
->c
[6] << 48
9230 | (uint64_t) a
->c
[7] << 56);
9235 ext64b_r_offset (const void *p
)
9242 const union aligned64
*a
9243 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9245 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9246 | (uint64_t) a
->c
[1] << 48
9247 | (uint64_t) a
->c
[2] << 40
9248 | (uint64_t) a
->c
[3] << 32
9249 | (uint64_t) a
->c
[4] << 24
9250 | (uint64_t) a
->c
[5] << 16
9251 | (uint64_t) a
->c
[6] << 8
9252 | (uint64_t) a
->c
[7]);
9257 /* When performing a relocatable link, the input relocations are
9258 preserved. But, if they reference global symbols, the indices
9259 referenced must be updated. Update all the relocations found in
9263 elf_link_adjust_relocs (bfd
*abfd
,
9265 struct bfd_elf_section_reloc_data
*reldata
,
9267 struct bfd_link_info
*info
)
9270 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9272 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9273 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9274 bfd_vma r_type_mask
;
9276 unsigned int count
= reldata
->count
;
9277 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9279 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9281 swap_in
= bed
->s
->swap_reloc_in
;
9282 swap_out
= bed
->s
->swap_reloc_out
;
9284 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9286 swap_in
= bed
->s
->swap_reloca_in
;
9287 swap_out
= bed
->s
->swap_reloca_out
;
9292 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9295 if (bed
->s
->arch_size
== 32)
9302 r_type_mask
= 0xffffffff;
9306 erela
= reldata
->hdr
->contents
;
9307 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9309 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9312 if (*rel_hash
== NULL
)
9315 if ((*rel_hash
)->indx
== -2
9316 && info
->gc_sections
9317 && ! info
->gc_keep_exported
)
9319 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9320 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9322 (*rel_hash
)->root
.root
.string
);
9323 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9325 bfd_set_error (bfd_error_invalid_operation
);
9328 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9330 (*swap_in
) (abfd
, erela
, irela
);
9331 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9332 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9333 | (irela
[j
].r_info
& r_type_mask
));
9334 (*swap_out
) (abfd
, irela
, erela
);
9337 if (bed
->elf_backend_update_relocs
)
9338 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9340 if (sort
&& count
!= 0)
9342 bfd_vma (*ext_r_off
) (const void *);
9345 bfd_byte
*base
, *end
, *p
, *loc
;
9346 bfd_byte
*buf
= NULL
;
9348 if (bed
->s
->arch_size
== 32)
9350 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9351 ext_r_off
= ext32l_r_offset
;
9352 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9353 ext_r_off
= ext32b_r_offset
;
9359 #ifdef BFD_HOST_64_BIT
9360 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9361 ext_r_off
= ext64l_r_offset
;
9362 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9363 ext_r_off
= ext64b_r_offset
;
9369 /* Must use a stable sort here. A modified insertion sort,
9370 since the relocs are mostly sorted already. */
9371 elt_size
= reldata
->hdr
->sh_entsize
;
9372 base
= reldata
->hdr
->contents
;
9373 end
= base
+ count
* elt_size
;
9374 if (elt_size
> sizeof (Elf64_External_Rela
))
9377 /* Ensure the first element is lowest. This acts as a sentinel,
9378 speeding the main loop below. */
9379 r_off
= (*ext_r_off
) (base
);
9380 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9382 bfd_vma r_off2
= (*ext_r_off
) (p
);
9391 /* Don't just swap *base and *loc as that changes the order
9392 of the original base[0] and base[1] if they happen to
9393 have the same r_offset. */
9394 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9395 memcpy (onebuf
, loc
, elt_size
);
9396 memmove (base
+ elt_size
, base
, loc
- base
);
9397 memcpy (base
, onebuf
, elt_size
);
9400 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9402 /* base to p is sorted, *p is next to insert. */
9403 r_off
= (*ext_r_off
) (p
);
9404 /* Search the sorted region for location to insert. */
9406 while (r_off
< (*ext_r_off
) (loc
))
9411 /* Chances are there is a run of relocs to insert here,
9412 from one of more input files. Files are not always
9413 linked in order due to the way elf_link_input_bfd is
9414 called. See pr17666. */
9415 size_t sortlen
= p
- loc
;
9416 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9417 size_t runlen
= elt_size
;
9418 size_t buf_size
= 96 * 1024;
9419 while (p
+ runlen
< end
9420 && (sortlen
<= buf_size
9421 || runlen
+ elt_size
<= buf_size
)
9422 && r_off2
> (*ext_r_off
) (p
+ runlen
))
9426 buf
= bfd_malloc (buf_size
);
9430 if (runlen
< sortlen
)
9432 memcpy (buf
, p
, runlen
);
9433 memmove (loc
+ runlen
, loc
, sortlen
);
9434 memcpy (loc
, buf
, runlen
);
9438 memcpy (buf
, loc
, sortlen
);
9439 memmove (loc
, p
, runlen
);
9440 memcpy (loc
+ runlen
, buf
, sortlen
);
9442 p
+= runlen
- elt_size
;
9445 /* Hashes are no longer valid. */
9446 free (reldata
->hashes
);
9447 reldata
->hashes
= NULL
;
9453 struct elf_link_sort_rela
9459 enum elf_reloc_type_class type
;
9460 /* We use this as an array of size int_rels_per_ext_rel. */
9461 Elf_Internal_Rela rela
[1];
9464 /* qsort stability here and for cmp2 is only an issue if multiple
9465 dynamic relocations are emitted at the same address. But targets
9466 that apply a series of dynamic relocations each operating on the
9467 result of the prior relocation can't use -z combreloc as
9468 implemented anyway. Such schemes tend to be broken by sorting on
9469 symbol index. That leaves dynamic NONE relocs as the only other
9470 case where ld might emit multiple relocs at the same address, and
9471 those are only emitted due to target bugs. */
9474 elf_link_sort_cmp1 (const void *A
, const void *B
)
9476 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9477 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9478 int relativea
, relativeb
;
9480 relativea
= a
->type
== reloc_class_relative
;
9481 relativeb
= b
->type
== reloc_class_relative
;
9483 if (relativea
< relativeb
)
9485 if (relativea
> relativeb
)
9487 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9489 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9491 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9493 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9499 elf_link_sort_cmp2 (const void *A
, const void *B
)
9501 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9502 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9504 if (a
->type
< b
->type
)
9506 if (a
->type
> b
->type
)
9508 if (a
->u
.offset
< b
->u
.offset
)
9510 if (a
->u
.offset
> b
->u
.offset
)
9512 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9514 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9520 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9522 asection
*dynamic_relocs
;
9525 bfd_size_type count
, size
;
9526 size_t i
, ret
, sort_elt
, ext_size
;
9527 bfd_byte
*sort
, *s_non_relative
, *p
;
9528 struct elf_link_sort_rela
*sq
;
9529 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9530 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9531 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9532 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9533 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9534 struct bfd_link_order
*lo
;
9538 /* Find a dynamic reloc section. */
9539 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9540 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9541 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9542 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9544 bool use_rela_initialised
= false;
9546 /* This is just here to stop gcc from complaining.
9547 Its initialization checking code is not perfect. */
9550 /* Both sections are present. Examine the sizes
9551 of the indirect sections to help us choose. */
9552 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9553 if (lo
->type
== bfd_indirect_link_order
)
9555 asection
*o
= lo
->u
.indirect
.section
;
9557 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9559 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9560 /* Section size is divisible by both rel and rela sizes.
9561 It is of no help to us. */
9565 /* Section size is only divisible by rela. */
9566 if (use_rela_initialised
&& !use_rela
)
9568 _bfd_error_handler (_("%pB: unable to sort relocs - "
9569 "they are in more than one size"),
9571 bfd_set_error (bfd_error_invalid_operation
);
9577 use_rela_initialised
= true;
9581 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9583 /* Section size is only divisible by rel. */
9584 if (use_rela_initialised
&& use_rela
)
9586 _bfd_error_handler (_("%pB: unable to sort relocs - "
9587 "they are in more than one size"),
9589 bfd_set_error (bfd_error_invalid_operation
);
9595 use_rela_initialised
= true;
9600 /* The section size is not divisible by either -
9601 something is wrong. */
9602 _bfd_error_handler (_("%pB: unable to sort relocs - "
9603 "they are of an unknown size"), abfd
);
9604 bfd_set_error (bfd_error_invalid_operation
);
9609 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9610 if (lo
->type
== bfd_indirect_link_order
)
9612 asection
*o
= lo
->u
.indirect
.section
;
9614 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9616 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9617 /* Section size is divisible by both rel and rela sizes.
9618 It is of no help to us. */
9622 /* Section size is only divisible by rela. */
9623 if (use_rela_initialised
&& !use_rela
)
9625 _bfd_error_handler (_("%pB: unable to sort relocs - "
9626 "they are in more than one size"),
9628 bfd_set_error (bfd_error_invalid_operation
);
9634 use_rela_initialised
= true;
9638 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9640 /* Section size is only divisible by rel. */
9641 if (use_rela_initialised
&& use_rela
)
9643 _bfd_error_handler (_("%pB: unable to sort relocs - "
9644 "they are in more than one size"),
9646 bfd_set_error (bfd_error_invalid_operation
);
9652 use_rela_initialised
= true;
9657 /* The section size is not divisible by either -
9658 something is wrong. */
9659 _bfd_error_handler (_("%pB: unable to sort relocs - "
9660 "they are of an unknown size"), abfd
);
9661 bfd_set_error (bfd_error_invalid_operation
);
9666 if (! use_rela_initialised
)
9670 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9672 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9679 dynamic_relocs
= rela_dyn
;
9680 ext_size
= bed
->s
->sizeof_rela
;
9681 swap_in
= bed
->s
->swap_reloca_in
;
9682 swap_out
= bed
->s
->swap_reloca_out
;
9686 dynamic_relocs
= rel_dyn
;
9687 ext_size
= bed
->s
->sizeof_rel
;
9688 swap_in
= bed
->s
->swap_reloc_in
;
9689 swap_out
= bed
->s
->swap_reloc_out
;
9693 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9694 if (lo
->type
== bfd_indirect_link_order
)
9695 size
+= lo
->u
.indirect
.section
->size
;
9697 if (size
!= dynamic_relocs
->size
)
9700 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9701 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9703 count
= dynamic_relocs
->size
/ ext_size
;
9706 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9710 (*info
->callbacks
->warning
)
9711 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9715 if (bed
->s
->arch_size
== 32)
9716 r_sym_mask
= ~(bfd_vma
) 0xff;
9718 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9720 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9721 if (lo
->type
== bfd_indirect_link_order
)
9723 bfd_byte
*erel
, *erelend
;
9724 asection
*o
= lo
->u
.indirect
.section
;
9726 if (o
->contents
== NULL
&& o
->size
!= 0)
9728 /* This is a reloc section that is being handled as a normal
9729 section. See bfd_section_from_shdr. We can't combine
9730 relocs in this case. */
9735 erelend
= o
->contents
+ o
->size
;
9736 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9738 while (erel
< erelend
)
9740 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9742 (*swap_in
) (abfd
, erel
, s
->rela
);
9743 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9744 s
->u
.sym_mask
= r_sym_mask
;
9750 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9752 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9754 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9755 if (s
->type
!= reloc_class_relative
)
9761 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9762 for (; i
< count
; i
++, p
+= sort_elt
)
9764 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9765 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9767 sp
->u
.offset
= sq
->rela
->r_offset
;
9770 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9772 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9773 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9775 /* We have plt relocs in .rela.dyn. */
9776 sq
= (struct elf_link_sort_rela
*) sort
;
9777 for (i
= 0; i
< count
; i
++)
9778 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9780 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9782 struct bfd_link_order
**plo
;
9783 /* Put srelplt link_order last. This is so the output_offset
9784 set in the next loop is correct for DT_JMPREL. */
9785 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9786 if ((*plo
)->type
== bfd_indirect_link_order
9787 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9793 plo
= &(*plo
)->next
;
9796 dynamic_relocs
->map_tail
.link_order
= lo
;
9801 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9802 if (lo
->type
== bfd_indirect_link_order
)
9804 bfd_byte
*erel
, *erelend
;
9805 asection
*o
= lo
->u
.indirect
.section
;
9808 erelend
= o
->contents
+ o
->size
;
9809 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9810 while (erel
< erelend
)
9812 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9813 (*swap_out
) (abfd
, s
->rela
, erel
);
9820 *psec
= dynamic_relocs
;
9824 /* Add a symbol to the output symbol string table. */
9827 elf_link_output_symstrtab (void *finf
,
9829 Elf_Internal_Sym
*elfsym
,
9830 asection
*input_sec
,
9831 struct elf_link_hash_entry
*h
)
9833 struct elf_final_link_info
*flinfo
= finf
;
9834 int (*output_symbol_hook
)
9835 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9836 struct elf_link_hash_entry
*);
9837 struct elf_link_hash_table
*hash_table
;
9838 const struct elf_backend_data
*bed
;
9839 bfd_size_type strtabsize
;
9841 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9843 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9844 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9845 if (output_symbol_hook
!= NULL
)
9847 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9852 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
9853 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
9854 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
9855 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
9859 || (input_sec
->flags
& SEC_EXCLUDE
))
9860 elfsym
->st_name
= (unsigned long) -1;
9863 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9864 to get the final offset for st_name. */
9865 char *versioned_name
= (char *) name
;
9868 if (h
->versioned
== versioned
&& h
->def_dynamic
)
9870 /* Keep only one '@' for versioned symbols defined in
9872 char *version
= strrchr (name
, ELF_VER_CHR
);
9873 char *base_end
= strchr (name
, ELF_VER_CHR
);
9874 if (version
!= base_end
)
9877 size_t len
= strlen (name
);
9878 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
9879 if (versioned_name
== NULL
)
9881 base_len
= base_end
- name
;
9882 memcpy (versioned_name
, name
, base_len
);
9883 memcpy (versioned_name
+ base_len
, version
,
9888 else if (flinfo
->info
->unique_symbol
9889 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
9891 struct local_hash_entry
*lh
;
9895 switch (ELF_ST_TYPE (elfsym
->st_info
))
9901 lh
= (struct local_hash_entry
*) bfd_hash_lookup
9902 (&flinfo
->local_hash_table
, name
, true, false);
9905 /* Always append ".COUNT" to local symbols to avoid
9906 potential conflicts with local symbol "XXX.COUNT". */
9907 sprintf (buf
, "%lx", lh
->count
);
9908 base_len
= lh
->size
;
9911 base_len
= strlen (name
);
9912 lh
->size
= base_len
;
9914 count_len
= strlen (buf
);
9915 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
9916 base_len
+ count_len
+ 2);
9917 if (versioned_name
== NULL
)
9919 memcpy (versioned_name
, name
, base_len
);
9920 versioned_name
[base_len
] = '.';
9921 memcpy (versioned_name
+ base_len
+ 1, buf
,
9928 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9929 versioned_name
, false);
9930 if (elfsym
->st_name
== (unsigned long) -1)
9934 hash_table
= elf_hash_table (flinfo
->info
);
9935 strtabsize
= hash_table
->strtabsize
;
9936 if (strtabsize
<= flinfo
->output_bfd
->symcount
)
9938 strtabsize
+= strtabsize
;
9939 hash_table
->strtabsize
= strtabsize
;
9940 strtabsize
*= sizeof (*hash_table
->strtab
);
9942 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9944 if (hash_table
->strtab
== NULL
)
9947 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].sym
= *elfsym
;
9948 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].dest_index
9949 = flinfo
->output_bfd
->symcount
;
9950 flinfo
->output_bfd
->symcount
+= 1;
9955 /* Swap symbols out to the symbol table and flush the output symbols to
9959 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9961 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9964 const struct elf_backend_data
*bed
;
9966 Elf_Internal_Shdr
*hdr
;
9970 if (flinfo
->output_bfd
->symcount
== 0)
9973 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9975 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9977 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
9978 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9982 if (flinfo
->symshndxbuf
)
9984 amt
= sizeof (Elf_External_Sym_Shndx
);
9985 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9986 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9987 if (flinfo
->symshndxbuf
== NULL
)
9994 /* Now swap out the symbols. */
9995 for (i
= 0; i
< flinfo
->output_bfd
->symcount
; i
++)
9997 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9998 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9999 elfsym
->sym
.st_name
= 0;
10001 elfsym
->sym
.st_name
10002 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
10003 elfsym
->sym
.st_name
);
10005 /* Inform the linker of the addition of this symbol. */
10007 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
10008 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
10011 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
10012 ((bfd_byte
*) symbuf
10013 + (elfsym
->dest_index
10014 * bed
->s
->sizeof_sym
)),
10015 NPTR_ADD (flinfo
->symshndxbuf
,
10016 elfsym
->dest_index
));
10019 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
10020 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
10021 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10022 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
10023 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
10025 hdr
->sh_size
+= amt
;
10033 free (hash_table
->strtab
);
10034 hash_table
->strtab
= NULL
;
10039 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10042 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
10044 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
10045 && sym
->st_shndx
< SHN_LORESERVE
)
10047 /* The gABI doesn't support dynamic symbols in output sections
10050 /* xgettext:c-format */
10051 (_("%pB: too many sections: %d (>= %d)"),
10052 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
10053 bfd_set_error (bfd_error_nonrepresentable_section
);
10059 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10060 allowing an unsatisfied unversioned symbol in the DSO to match a
10061 versioned symbol that would normally require an explicit version.
10062 We also handle the case that a DSO references a hidden symbol
10063 which may be satisfied by a versioned symbol in another DSO. */
10066 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
10067 const struct elf_backend_data
*bed
,
10068 struct elf_link_hash_entry
*h
)
10071 struct elf_link_loaded_list
*loaded
;
10073 if (!is_elf_hash_table (info
->hash
))
10076 /* Check indirect symbol. */
10077 while (h
->root
.type
== bfd_link_hash_indirect
)
10078 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10080 switch (h
->root
.type
)
10086 case bfd_link_hash_undefined
:
10087 case bfd_link_hash_undefweak
:
10088 abfd
= h
->root
.u
.undef
.abfd
;
10090 || (abfd
->flags
& DYNAMIC
) == 0
10091 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
10095 case bfd_link_hash_defined
:
10096 case bfd_link_hash_defweak
:
10097 abfd
= h
->root
.u
.def
.section
->owner
;
10100 case bfd_link_hash_common
:
10101 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10104 BFD_ASSERT (abfd
!= NULL
);
10106 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10108 loaded
= loaded
->next
)
10111 Elf_Internal_Shdr
*hdr
;
10113 size_t extsymcount
;
10115 Elf_Internal_Shdr
*versymhdr
;
10116 Elf_Internal_Sym
*isym
;
10117 Elf_Internal_Sym
*isymend
;
10118 Elf_Internal_Sym
*isymbuf
;
10119 Elf_External_Versym
*ever
;
10120 Elf_External_Versym
*extversym
;
10122 input
= loaded
->abfd
;
10124 /* We check each DSO for a possible hidden versioned definition. */
10126 || elf_dynversym (input
) == 0)
10129 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10131 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10132 if (elf_bad_symtab (input
))
10134 extsymcount
= symcount
;
10139 extsymcount
= symcount
- hdr
->sh_info
;
10140 extsymoff
= hdr
->sh_info
;
10143 if (extsymcount
== 0)
10146 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10148 if (isymbuf
== NULL
)
10151 /* Read in any version definitions. */
10152 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10153 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10154 || (extversym
= (Elf_External_Versym
*)
10155 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10156 versymhdr
->sh_size
)) == NULL
)
10162 ever
= extversym
+ extsymoff
;
10163 isymend
= isymbuf
+ extsymcount
;
10164 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10167 Elf_Internal_Versym iver
;
10168 unsigned short version_index
;
10170 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10171 || isym
->st_shndx
== SHN_UNDEF
)
10174 name
= bfd_elf_string_from_elf_section (input
,
10177 if (strcmp (name
, h
->root
.root
.string
) != 0)
10180 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10182 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10183 && !(h
->def_regular
10184 && h
->forced_local
))
10186 /* If we have a non-hidden versioned sym, then it should
10187 have provided a definition for the undefined sym unless
10188 it is defined in a non-shared object and forced local.
10193 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10194 if (version_index
== 1 || version_index
== 2)
10196 /* This is the base or first version. We can use it. */
10210 /* Convert ELF common symbol TYPE. */
10213 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10215 /* Commom symbol can only appear in relocatable link. */
10216 if (!bfd_link_relocatable (info
))
10218 switch (info
->elf_stt_common
)
10222 case elf_stt_common
:
10225 case no_elf_stt_common
:
10232 /* Add an external symbol to the symbol table. This is called from
10233 the hash table traversal routine. When generating a shared object,
10234 we go through the symbol table twice. The first time we output
10235 anything that might have been forced to local scope in a version
10236 script. The second time we output the symbols that are still
10240 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10242 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10243 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10244 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10246 Elf_Internal_Sym sym
;
10247 asection
*input_sec
;
10248 const struct elf_backend_data
*bed
;
10253 if (h
->root
.type
== bfd_link_hash_warning
)
10255 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10256 if (h
->root
.type
== bfd_link_hash_new
)
10260 /* Decide whether to output this symbol in this pass. */
10261 if (eoinfo
->localsyms
)
10263 if (!h
->forced_local
)
10268 if (h
->forced_local
)
10272 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10274 if (h
->root
.type
== bfd_link_hash_undefined
)
10276 /* If we have an undefined symbol reference here then it must have
10277 come from a shared library that is being linked in. (Undefined
10278 references in regular files have already been handled unless
10279 they are in unreferenced sections which are removed by garbage
10281 bool ignore_undef
= false;
10283 /* Some symbols may be special in that the fact that they're
10284 undefined can be safely ignored - let backend determine that. */
10285 if (bed
->elf_backend_ignore_undef_symbol
)
10286 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10288 /* If we are reporting errors for this situation then do so now. */
10290 && h
->ref_dynamic_nonweak
10291 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10292 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10293 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10295 flinfo
->info
->callbacks
->undefined_symbol
10296 (flinfo
->info
, h
->root
.root
.string
,
10297 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10298 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10299 && !flinfo
->info
->warn_unresolved_syms
);
10302 /* Strip a global symbol defined in a discarded section. */
10307 /* We should also warn if a forced local symbol is referenced from
10308 shared libraries. */
10309 if (bfd_link_executable (flinfo
->info
)
10314 && h
->ref_dynamic_nonweak
10315 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10319 struct elf_link_hash_entry
*hi
= h
;
10321 /* Check indirect symbol. */
10322 while (hi
->root
.type
== bfd_link_hash_indirect
)
10323 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10325 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10326 /* xgettext:c-format */
10327 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10328 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10329 /* xgettext:c-format */
10330 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10332 /* xgettext:c-format */
10333 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10334 def_bfd
= flinfo
->output_bfd
;
10335 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10336 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10337 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10338 h
->root
.root
.string
, def_bfd
);
10339 bfd_set_error (bfd_error_bad_value
);
10340 eoinfo
->failed
= true;
10344 /* We don't want to output symbols that have never been mentioned by
10345 a regular file, or that we have been told to strip. However, if
10346 h->indx is set to -2, the symbol is used by a reloc and we must
10351 else if ((h
->def_dynamic
10353 || h
->root
.type
== bfd_link_hash_new
)
10355 && !h
->ref_regular
)
10357 else if (flinfo
->info
->strip
== strip_all
)
10359 else if (flinfo
->info
->strip
== strip_some
10360 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10361 h
->root
.root
.string
, false, false) == NULL
)
10363 else if ((h
->root
.type
== bfd_link_hash_defined
10364 || h
->root
.type
== bfd_link_hash_defweak
)
10365 && ((flinfo
->info
->strip_discarded
10366 && discarded_section (h
->root
.u
.def
.section
))
10367 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10368 && h
->root
.u
.def
.section
->owner
!= NULL
10369 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10371 else if ((h
->root
.type
== bfd_link_hash_undefined
10372 || h
->root
.type
== bfd_link_hash_undefweak
)
10373 && h
->root
.u
.undef
.abfd
!= NULL
10374 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10379 /* If we're stripping it, and it's not a dynamic symbol, there's
10380 nothing else to do. However, if it is a forced local symbol or
10381 an ifunc symbol we need to give the backend finish_dynamic_symbol
10382 function a chance to make it dynamic. */
10384 && h
->dynindx
== -1
10385 && type
!= STT_GNU_IFUNC
10386 && !h
->forced_local
)
10390 sym
.st_size
= h
->size
;
10391 sym
.st_other
= h
->other
;
10392 switch (h
->root
.type
)
10395 case bfd_link_hash_new
:
10396 case bfd_link_hash_warning
:
10400 case bfd_link_hash_undefined
:
10401 case bfd_link_hash_undefweak
:
10402 input_sec
= bfd_und_section_ptr
;
10403 sym
.st_shndx
= SHN_UNDEF
;
10406 case bfd_link_hash_defined
:
10407 case bfd_link_hash_defweak
:
10409 input_sec
= h
->root
.u
.def
.section
;
10410 if (input_sec
->output_section
!= NULL
)
10413 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10414 input_sec
->output_section
);
10415 if (sym
.st_shndx
== SHN_BAD
)
10418 /* xgettext:c-format */
10419 (_("%pB: could not find output section %pA for input section %pA"),
10420 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10421 bfd_set_error (bfd_error_nonrepresentable_section
);
10422 eoinfo
->failed
= true;
10426 /* ELF symbols in relocatable files are section relative,
10427 but in nonrelocatable files they are virtual
10429 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10430 if (!bfd_link_relocatable (flinfo
->info
))
10432 sym
.st_value
+= input_sec
->output_section
->vma
;
10433 if (h
->type
== STT_TLS
)
10435 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10436 if (tls_sec
!= NULL
)
10437 sym
.st_value
-= tls_sec
->vma
;
10443 BFD_ASSERT (input_sec
->owner
== NULL
10444 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10445 sym
.st_shndx
= SHN_UNDEF
;
10446 input_sec
= bfd_und_section_ptr
;
10451 case bfd_link_hash_common
:
10452 input_sec
= h
->root
.u
.c
.p
->section
;
10453 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10454 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10457 case bfd_link_hash_indirect
:
10458 /* These symbols are created by symbol versioning. They point
10459 to the decorated version of the name. For example, if the
10460 symbol foo@@GNU_1.2 is the default, which should be used when
10461 foo is used with no version, then we add an indirect symbol
10462 foo which points to foo@@GNU_1.2. We ignore these symbols,
10463 since the indirected symbol is already in the hash table. */
10467 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10468 switch (h
->root
.type
)
10470 case bfd_link_hash_common
:
10471 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10473 case bfd_link_hash_defined
:
10474 case bfd_link_hash_defweak
:
10475 if (bed
->common_definition (&sym
))
10476 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10480 case bfd_link_hash_undefined
:
10481 case bfd_link_hash_undefweak
:
10487 if (h
->forced_local
)
10489 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10490 /* Turn off visibility on local symbol. */
10491 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10493 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10494 else if (h
->unique_global
&& h
->def_regular
)
10495 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10496 else if (h
->root
.type
== bfd_link_hash_undefweak
10497 || h
->root
.type
== bfd_link_hash_defweak
)
10498 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10500 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10501 sym
.st_target_internal
= h
->target_internal
;
10503 /* Give the processor backend a chance to tweak the symbol value,
10504 and also to finish up anything that needs to be done for this
10505 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10506 forced local syms when non-shared is due to a historical quirk.
10507 STT_GNU_IFUNC symbol must go through PLT. */
10508 if ((h
->type
== STT_GNU_IFUNC
10510 && !bfd_link_relocatable (flinfo
->info
))
10511 || ((h
->dynindx
!= -1
10512 || h
->forced_local
)
10513 && ((bfd_link_pic (flinfo
->info
)
10514 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10515 || h
->root
.type
!= bfd_link_hash_undefweak
))
10516 || !h
->forced_local
)
10517 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10519 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10520 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10522 eoinfo
->failed
= true;
10527 /* If we are marking the symbol as undefined, and there are no
10528 non-weak references to this symbol from a regular object, then
10529 mark the symbol as weak undefined; if there are non-weak
10530 references, mark the symbol as strong. We can't do this earlier,
10531 because it might not be marked as undefined until the
10532 finish_dynamic_symbol routine gets through with it. */
10533 if (sym
.st_shndx
== SHN_UNDEF
10535 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10536 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10539 type
= ELF_ST_TYPE (sym
.st_info
);
10541 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10542 if (type
== STT_GNU_IFUNC
)
10545 if (h
->ref_regular_nonweak
)
10546 bindtype
= STB_GLOBAL
;
10548 bindtype
= STB_WEAK
;
10549 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10552 /* If this is a symbol defined in a dynamic library, don't use the
10553 symbol size from the dynamic library. Relinking an executable
10554 against a new library may introduce gratuitous changes in the
10555 executable's symbols if we keep the size. */
10556 if (sym
.st_shndx
== SHN_UNDEF
10561 /* If a non-weak symbol with non-default visibility is not defined
10562 locally, it is a fatal error. */
10563 if (!bfd_link_relocatable (flinfo
->info
)
10564 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10565 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10566 && h
->root
.type
== bfd_link_hash_undefined
10567 && !h
->def_regular
)
10571 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10572 /* xgettext:c-format */
10573 msg
= _("%pB: protected symbol `%s' isn't defined");
10574 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10575 /* xgettext:c-format */
10576 msg
= _("%pB: internal symbol `%s' isn't defined");
10578 /* xgettext:c-format */
10579 msg
= _("%pB: hidden symbol `%s' isn't defined");
10580 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10581 bfd_set_error (bfd_error_bad_value
);
10582 eoinfo
->failed
= true;
10586 /* If this symbol should be put in the .dynsym section, then put it
10587 there now. We already know the symbol index. We also fill in
10588 the entry in the .hash section. */
10589 if (h
->dynindx
!= -1
10590 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10591 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10592 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10596 /* Since there is no version information in the dynamic string,
10597 if there is no version info in symbol version section, we will
10598 have a run-time problem if not linking executable, referenced
10599 by shared library, or not bound locally. */
10600 if (h
->verinfo
.verdef
== NULL
10601 && (!bfd_link_executable (flinfo
->info
)
10603 || !h
->def_regular
))
10605 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10607 if (p
&& p
[1] != '\0')
10610 /* xgettext:c-format */
10611 (_("%pB: no symbol version section for versioned symbol `%s'"),
10612 flinfo
->output_bfd
, h
->root
.root
.string
);
10613 eoinfo
->failed
= true;
10618 sym
.st_name
= h
->dynstr_index
;
10619 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10620 + h
->dynindx
* bed
->s
->sizeof_sym
);
10621 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10623 eoinfo
->failed
= true;
10627 /* Inform the linker of the addition of this symbol. */
10629 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
10630 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
10632 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10634 if (flinfo
->hash_sec
!= NULL
)
10636 size_t hash_entry_size
;
10637 bfd_byte
*bucketpos
;
10639 size_t bucketcount
;
10642 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10643 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10646 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10647 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10648 + (bucket
+ 2) * hash_entry_size
);
10649 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10650 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10652 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10653 ((bfd_byte
*) flinfo
->hash_sec
->contents
10654 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10657 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10659 Elf_Internal_Versym iversym
;
10660 Elf_External_Versym
*eversym
;
10662 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10664 if (h
->verinfo
.verdef
== NULL
10665 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10666 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10667 iversym
.vs_vers
= 1;
10669 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10673 if (h
->verinfo
.vertree
== NULL
)
10674 iversym
.vs_vers
= 1;
10676 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10677 if (flinfo
->info
->create_default_symver
)
10681 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10682 defined locally. */
10683 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10684 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10686 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10687 eversym
+= h
->dynindx
;
10688 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10692 /* If the symbol is undefined, and we didn't output it to .dynsym,
10693 strip it from .symtab too. Obviously we can't do this for
10694 relocatable output or when needed for --emit-relocs. */
10695 else if (input_sec
== bfd_und_section_ptr
10697 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10698 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10699 && !bfd_link_relocatable (flinfo
->info
))
10702 /* Also strip others that we couldn't earlier due to dynamic symbol
10706 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10709 /* Output a FILE symbol so that following locals are not associated
10710 with the wrong input file. We need one for forced local symbols
10711 if we've seen more than one FILE symbol or when we have exactly
10712 one FILE symbol but global symbols are present in a file other
10713 than the one with the FILE symbol. We also need one if linker
10714 defined symbols are present. In practice these conditions are
10715 always met, so just emit the FILE symbol unconditionally. */
10716 if (eoinfo
->localsyms
10717 && !eoinfo
->file_sym_done
10718 && eoinfo
->flinfo
->filesym_count
!= 0)
10720 Elf_Internal_Sym fsym
;
10722 memset (&fsym
, 0, sizeof (fsym
));
10723 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10724 fsym
.st_shndx
= SHN_ABS
;
10725 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10726 bfd_und_section_ptr
, NULL
))
10729 eoinfo
->file_sym_done
= true;
10732 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10733 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10737 eoinfo
->failed
= true;
10742 else if (h
->indx
== -2)
10748 /* Return TRUE if special handling is done for relocs in SEC against
10749 symbols defined in discarded sections. */
10752 elf_section_ignore_discarded_relocs (asection
*sec
)
10754 const struct elf_backend_data
*bed
;
10756 switch (sec
->sec_info_type
)
10758 case SEC_INFO_TYPE_STABS
:
10759 case SEC_INFO_TYPE_EH_FRAME
:
10760 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10766 bed
= get_elf_backend_data (sec
->owner
);
10767 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10768 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10774 /* Return a mask saying how ld should treat relocations in SEC against
10775 symbols defined in discarded sections. If this function returns
10776 COMPLAIN set, ld will issue a warning message. If this function
10777 returns PRETEND set, and the discarded section was link-once and the
10778 same size as the kept link-once section, ld will pretend that the
10779 symbol was actually defined in the kept section. Otherwise ld will
10780 zero the reloc (at least that is the intent, but some cooperation by
10781 the target dependent code is needed, particularly for REL targets). */
10784 _bfd_elf_default_action_discarded (asection
*sec
)
10786 if (sec
->flags
& SEC_DEBUGGING
)
10789 if (strcmp (".eh_frame", sec
->name
) == 0)
10792 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10795 return COMPLAIN
| PRETEND
;
10798 /* Find a match between a section and a member of a section group. */
10801 match_group_member (asection
*sec
, asection
*group
,
10802 struct bfd_link_info
*info
)
10804 asection
*first
= elf_next_in_group (group
);
10805 asection
*s
= first
;
10809 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10812 s
= elf_next_in_group (s
);
10820 /* Check if the kept section of a discarded section SEC can be used
10821 to replace it. Return the replacement if it is OK. Otherwise return
10825 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10829 kept
= sec
->kept_section
;
10832 if ((kept
->flags
& SEC_GROUP
) != 0)
10833 kept
= match_group_member (sec
, kept
, info
);
10836 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10837 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
10841 /* Get the real kept section. */
10843 for (next
= kept
->kept_section
;
10845 next
= next
->kept_section
)
10849 sec
->kept_section
= kept
;
10854 /* Link an input file into the linker output file. This function
10855 handles all the sections and relocations of the input file at once.
10856 This is so that we only have to read the local symbols once, and
10857 don't have to keep them in memory. */
10860 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10862 int (*relocate_section
)
10863 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10864 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10866 Elf_Internal_Shdr
*symtab_hdr
;
10867 size_t locsymcount
;
10869 Elf_Internal_Sym
*isymbuf
;
10870 Elf_Internal_Sym
*isym
;
10871 Elf_Internal_Sym
*isymend
;
10873 asection
**ppsection
;
10875 const struct elf_backend_data
*bed
;
10876 struct elf_link_hash_entry
**sym_hashes
;
10877 bfd_size_type address_size
;
10878 bfd_vma r_type_mask
;
10880 bool have_file_sym
= false;
10882 output_bfd
= flinfo
->output_bfd
;
10883 bed
= get_elf_backend_data (output_bfd
);
10884 relocate_section
= bed
->elf_backend_relocate_section
;
10886 /* If this is a dynamic object, we don't want to do anything here:
10887 we don't want the local symbols, and we don't want the section
10889 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10892 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10893 if (elf_bad_symtab (input_bfd
))
10895 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10900 locsymcount
= symtab_hdr
->sh_info
;
10901 extsymoff
= symtab_hdr
->sh_info
;
10904 /* Enable GNU OSABI features in the output BFD that are used in the input
10906 if (bed
->elf_osabi
== ELFOSABI_NONE
10907 || bed
->elf_osabi
== ELFOSABI_GNU
10908 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
10909 elf_tdata (output_bfd
)->has_gnu_osabi
10910 |= (elf_tdata (input_bfd
)->has_gnu_osabi
10911 & (bfd_link_relocatable (flinfo
->info
)
10912 ? -1 : ~elf_gnu_osabi_retain
));
10914 /* Read the local symbols. */
10915 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10916 if (isymbuf
== NULL
&& locsymcount
!= 0)
10918 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10919 flinfo
->internal_syms
,
10920 flinfo
->external_syms
,
10921 flinfo
->locsym_shndx
);
10922 if (isymbuf
== NULL
)
10926 /* Find local symbol sections and adjust values of symbols in
10927 SEC_MERGE sections. Write out those local symbols we know are
10928 going into the output file. */
10929 isymend
= PTR_ADD (isymbuf
, locsymcount
);
10930 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10932 isym
++, pindex
++, ppsection
++)
10936 Elf_Internal_Sym osym
;
10942 if (elf_bad_symtab (input_bfd
))
10944 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10951 if (isym
->st_shndx
== SHN_UNDEF
)
10952 isec
= bfd_und_section_ptr
;
10953 else if (isym
->st_shndx
== SHN_ABS
)
10954 isec
= bfd_abs_section_ptr
;
10955 else if (isym
->st_shndx
== SHN_COMMON
)
10956 isec
= bfd_com_section_ptr
;
10959 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10962 /* Don't attempt to output symbols with st_shnx in the
10963 reserved range other than SHN_ABS and SHN_COMMON. */
10964 isec
= bfd_und_section_ptr
;
10966 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10967 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10969 _bfd_merged_section_offset (output_bfd
, &isec
,
10970 elf_section_data (isec
)->sec_info
,
10976 /* Don't output the first, undefined, symbol. In fact, don't
10977 output any undefined local symbol. */
10978 if (isec
== bfd_und_section_ptr
)
10981 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10983 /* We never output section symbols. Instead, we use the
10984 section symbol of the corresponding section in the output
10989 /* If we are stripping all symbols, we don't want to output this
10991 if (flinfo
->info
->strip
== strip_all
)
10994 /* If we are discarding all local symbols, we don't want to
10995 output this one. If we are generating a relocatable output
10996 file, then some of the local symbols may be required by
10997 relocs; we output them below as we discover that they are
10999 if (flinfo
->info
->discard
== discard_all
)
11002 /* If this symbol is defined in a section which we are
11003 discarding, we don't need to keep it. */
11004 if (isym
->st_shndx
!= SHN_UNDEF
11005 && isym
->st_shndx
< SHN_LORESERVE
11006 && isec
->output_section
== NULL
11007 && flinfo
->info
->non_contiguous_regions
11008 && flinfo
->info
->non_contiguous_regions_warnings
)
11010 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
11011 "discards section `%s' from '%s'\n"),
11012 isec
->name
, bfd_get_filename (isec
->owner
));
11016 if (isym
->st_shndx
!= SHN_UNDEF
11017 && isym
->st_shndx
< SHN_LORESERVE
11018 && bfd_section_removed_from_list (output_bfd
,
11019 isec
->output_section
))
11022 /* Get the name of the symbol. */
11023 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
11028 /* See if we are discarding symbols with this name. */
11029 if ((flinfo
->info
->strip
== strip_some
11030 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, false, false)
11032 || (((flinfo
->info
->discard
== discard_sec_merge
11033 && (isec
->flags
& SEC_MERGE
)
11034 && !bfd_link_relocatable (flinfo
->info
))
11035 || flinfo
->info
->discard
== discard_l
)
11036 && bfd_is_local_label_name (input_bfd
, name
)))
11039 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
11041 if (input_bfd
->lto_output
)
11042 /* -flto puts a temp file name here. This means builds
11043 are not reproducible. Discard the symbol. */
11045 have_file_sym
= true;
11046 flinfo
->filesym_count
+= 1;
11048 if (!have_file_sym
)
11050 /* In the absence of debug info, bfd_find_nearest_line uses
11051 FILE symbols to determine the source file for local
11052 function symbols. Provide a FILE symbol here if input
11053 files lack such, so that their symbols won't be
11054 associated with a previous input file. It's not the
11055 source file, but the best we can do. */
11056 const char *filename
;
11057 have_file_sym
= true;
11058 flinfo
->filesym_count
+= 1;
11059 memset (&osym
, 0, sizeof (osym
));
11060 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11061 osym
.st_shndx
= SHN_ABS
;
11062 if (input_bfd
->lto_output
)
11065 filename
= lbasename (bfd_get_filename (input_bfd
));
11066 if (!elf_link_output_symstrtab (flinfo
, filename
, &osym
,
11067 bfd_abs_section_ptr
, NULL
))
11073 /* Adjust the section index for the output file. */
11074 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11075 isec
->output_section
);
11076 if (osym
.st_shndx
== SHN_BAD
)
11079 /* ELF symbols in relocatable files are section relative, but
11080 in executable files they are virtual addresses. Note that
11081 this code assumes that all ELF sections have an associated
11082 BFD section with a reasonable value for output_offset; below
11083 we assume that they also have a reasonable value for
11084 output_section. Any special sections must be set up to meet
11085 these requirements. */
11086 osym
.st_value
+= isec
->output_offset
;
11087 if (!bfd_link_relocatable (flinfo
->info
))
11089 osym
.st_value
+= isec
->output_section
->vma
;
11090 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
11092 /* STT_TLS symbols are relative to PT_TLS segment base. */
11093 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
11094 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
11096 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
11101 indx
= bfd_get_symcount (output_bfd
);
11102 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11109 if (bed
->s
->arch_size
== 32)
11111 r_type_mask
= 0xff;
11117 r_type_mask
= 0xffffffff;
11122 /* Relocate the contents of each section. */
11123 sym_hashes
= elf_sym_hashes (input_bfd
);
11124 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11126 bfd_byte
*contents
;
11128 if (! o
->linker_mark
)
11130 /* This section was omitted from the link. */
11134 if (!flinfo
->info
->resolve_section_groups
11135 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11137 /* Deal with the group signature symbol. */
11138 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11139 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11140 asection
*osec
= o
->output_section
;
11142 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11143 if (symndx
>= locsymcount
11144 || (elf_bad_symtab (input_bfd
)
11145 && flinfo
->sections
[symndx
] == NULL
))
11147 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
11148 while (h
->root
.type
== bfd_link_hash_indirect
11149 || h
->root
.type
== bfd_link_hash_warning
)
11150 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11151 /* Arrange for symbol to be output. */
11153 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11155 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11157 /* We'll use the output section target_index. */
11158 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11159 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11163 if (flinfo
->indices
[symndx
] == -1)
11165 /* Otherwise output the local symbol now. */
11166 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11167 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11172 name
= bfd_elf_string_from_elf_section (input_bfd
,
11173 symtab_hdr
->sh_link
,
11178 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11180 if (sym
.st_shndx
== SHN_BAD
)
11183 sym
.st_value
+= o
->output_offset
;
11185 indx
= bfd_get_symcount (output_bfd
);
11186 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11191 flinfo
->indices
[symndx
] = indx
;
11195 elf_section_data (osec
)->this_hdr
.sh_info
11196 = flinfo
->indices
[symndx
];
11200 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11201 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11204 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11206 /* Section was created by _bfd_elf_link_create_dynamic_sections
11211 /* Get the contents of the section. They have been cached by a
11212 relaxation routine. Note that o is a section in an input
11213 file, so the contents field will not have been set by any of
11214 the routines which work on output files. */
11215 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11217 contents
= elf_section_data (o
)->this_hdr
.contents
;
11218 if (bed
->caches_rawsize
11220 && o
->rawsize
< o
->size
)
11222 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11223 contents
= flinfo
->contents
;
11228 contents
= flinfo
->contents
;
11229 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
11233 if ((o
->flags
& SEC_RELOC
) != 0)
11235 Elf_Internal_Rela
*internal_relocs
;
11236 Elf_Internal_Rela
*rel
, *relend
;
11237 int action_discarded
;
11240 /* Get the swapped relocs. */
11242 = _bfd_elf_link_info_read_relocs (input_bfd
, flinfo
->info
, o
,
11243 flinfo
->external_relocs
,
11244 flinfo
->internal_relocs
,
11246 if (internal_relocs
== NULL
11247 && o
->reloc_count
> 0)
11250 /* We need to reverse-copy input .ctors/.dtors sections if
11251 they are placed in .init_array/.finit_array for output. */
11252 if (o
->size
> address_size
11253 && ((startswith (o
->name
, ".ctors")
11254 && strcmp (o
->output_section
->name
,
11255 ".init_array") == 0)
11256 || (startswith (o
->name
, ".dtors")
11257 && strcmp (o
->output_section
->name
,
11258 ".fini_array") == 0))
11259 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
11261 if (o
->size
* bed
->s
->int_rels_per_ext_rel
11262 != o
->reloc_count
* address_size
)
11265 /* xgettext:c-format */
11266 (_("error: %pB: size of section %pA is not "
11267 "multiple of address size"),
11269 bfd_set_error (bfd_error_bad_value
);
11272 o
->flags
|= SEC_ELF_REVERSE_COPY
;
11275 action_discarded
= -1;
11276 if (!elf_section_ignore_discarded_relocs (o
))
11277 action_discarded
= (*bed
->action_discarded
) (o
);
11279 /* Run through the relocs evaluating complex reloc symbols and
11280 looking for relocs against symbols from discarded sections
11281 or section symbols from removed link-once sections.
11282 Complain about relocs against discarded sections. Zero
11283 relocs against removed link-once sections. */
11285 rel
= internal_relocs
;
11286 relend
= rel
+ o
->reloc_count
;
11287 for ( ; rel
< relend
; rel
++)
11289 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11290 unsigned int s_type
;
11291 asection
**ps
, *sec
;
11292 struct elf_link_hash_entry
*h
= NULL
;
11293 const char *sym_name
;
11295 if (r_symndx
== STN_UNDEF
)
11298 if (r_symndx
>= locsymcount
11299 || (elf_bad_symtab (input_bfd
)
11300 && flinfo
->sections
[r_symndx
] == NULL
))
11302 h
= sym_hashes
[r_symndx
- extsymoff
];
11304 /* Badly formatted input files can contain relocs that
11305 reference non-existant symbols. Check here so that
11306 we do not seg fault. */
11310 /* xgettext:c-format */
11311 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11312 "that references a non-existent global symbol"),
11313 input_bfd
, (uint64_t) rel
->r_info
, o
);
11314 bfd_set_error (bfd_error_bad_value
);
11318 while (h
->root
.type
== bfd_link_hash_indirect
11319 || h
->root
.type
== bfd_link_hash_warning
)
11320 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11324 /* If a plugin symbol is referenced from a non-IR file,
11325 mark the symbol as undefined. Note that the
11326 linker may attach linker created dynamic sections
11327 to the plugin bfd. Symbols defined in linker
11328 created sections are not plugin symbols. */
11329 if ((h
->root
.non_ir_ref_regular
11330 || h
->root
.non_ir_ref_dynamic
)
11331 && (h
->root
.type
== bfd_link_hash_defined
11332 || h
->root
.type
== bfd_link_hash_defweak
)
11333 && (h
->root
.u
.def
.section
->flags
11334 & SEC_LINKER_CREATED
) == 0
11335 && h
->root
.u
.def
.section
->owner
!= NULL
11336 && (h
->root
.u
.def
.section
->owner
->flags
11337 & BFD_PLUGIN
) != 0)
11339 h
->root
.type
= bfd_link_hash_undefined
;
11340 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11344 if (h
->root
.type
== bfd_link_hash_defined
11345 || h
->root
.type
== bfd_link_hash_defweak
)
11346 ps
= &h
->root
.u
.def
.section
;
11348 sym_name
= h
->root
.root
.string
;
11352 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11354 s_type
= ELF_ST_TYPE (sym
->st_info
);
11355 ps
= &flinfo
->sections
[r_symndx
];
11356 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11360 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11361 && !bfd_link_relocatable (flinfo
->info
))
11364 bfd_vma dot
= (rel
->r_offset
11365 + o
->output_offset
+ o
->output_section
->vma
);
11367 printf ("Encountered a complex symbol!");
11368 printf (" (input_bfd %s, section %s, reloc %ld\n",
11369 bfd_get_filename (input_bfd
), o
->name
,
11370 (long) (rel
- internal_relocs
));
11371 printf (" symbol: idx %8.8lx, name %s\n",
11372 r_symndx
, sym_name
);
11373 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11374 (unsigned long) rel
->r_info
,
11375 (unsigned long) rel
->r_offset
);
11377 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11378 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11381 /* Symbol evaluated OK. Update to absolute value. */
11382 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11387 if (action_discarded
!= -1 && ps
!= NULL
)
11389 /* Complain if the definition comes from a
11390 discarded section. */
11391 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11393 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11394 if (action_discarded
& COMPLAIN
)
11395 (*flinfo
->info
->callbacks
->einfo
)
11396 /* xgettext:c-format */
11397 (_("%X`%s' referenced in section `%pA' of %pB: "
11398 "defined in discarded section `%pA' of %pB\n"),
11399 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11401 /* Try to do the best we can to support buggy old
11402 versions of gcc. Pretend that the symbol is
11403 really defined in the kept linkonce section.
11404 FIXME: This is quite broken. Modifying the
11405 symbol here means we will be changing all later
11406 uses of the symbol, not just in this section. */
11407 if (action_discarded
& PRETEND
)
11411 kept
= _bfd_elf_check_kept_section (sec
,
11423 /* Relocate the section by invoking a back end routine.
11425 The back end routine is responsible for adjusting the
11426 section contents as necessary, and (if using Rela relocs
11427 and generating a relocatable output file) adjusting the
11428 reloc addend as necessary.
11430 The back end routine does not have to worry about setting
11431 the reloc address or the reloc symbol index.
11433 The back end routine is given a pointer to the swapped in
11434 internal symbols, and can access the hash table entries
11435 for the external symbols via elf_sym_hashes (input_bfd).
11437 When generating relocatable output, the back end routine
11438 must handle STB_LOCAL/STT_SECTION symbols specially. The
11439 output symbol is going to be a section symbol
11440 corresponding to the output section, which will require
11441 the addend to be adjusted. */
11443 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11444 input_bfd
, o
, contents
,
11452 || bfd_link_relocatable (flinfo
->info
)
11453 || flinfo
->info
->emitrelocations
)
11455 Elf_Internal_Rela
*irela
;
11456 Elf_Internal_Rela
*irelaend
, *irelamid
;
11457 bfd_vma last_offset
;
11458 struct elf_link_hash_entry
**rel_hash
;
11459 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11460 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11461 unsigned int next_erel
;
11463 struct bfd_elf_section_data
*esdi
, *esdo
;
11465 esdi
= elf_section_data (o
);
11466 esdo
= elf_section_data (o
->output_section
);
11467 rela_normal
= false;
11469 /* Adjust the reloc addresses and symbol indices. */
11471 irela
= internal_relocs
;
11472 irelaend
= irela
+ o
->reloc_count
;
11473 rel_hash
= PTR_ADD (esdo
->rel
.hashes
, esdo
->rel
.count
);
11474 /* We start processing the REL relocs, if any. When we reach
11475 IRELAMID in the loop, we switch to the RELA relocs. */
11477 if (esdi
->rel
.hdr
!= NULL
)
11478 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11479 * bed
->s
->int_rels_per_ext_rel
);
11480 rel_hash_list
= rel_hash
;
11481 rela_hash_list
= NULL
;
11482 last_offset
= o
->output_offset
;
11483 if (!bfd_link_relocatable (flinfo
->info
))
11484 last_offset
+= o
->output_section
->vma
;
11485 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11487 unsigned long r_symndx
;
11489 Elf_Internal_Sym sym
;
11491 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11497 if (irela
== irelamid
)
11499 rel_hash
= PTR_ADD (esdo
->rela
.hashes
, esdo
->rela
.count
);
11500 rela_hash_list
= rel_hash
;
11501 rela_normal
= bed
->rela_normal
;
11504 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11507 if (irela
->r_offset
>= (bfd_vma
) -2)
11509 /* This is a reloc for a deleted entry or somesuch.
11510 Turn it into an R_*_NONE reloc, at the same
11511 offset as the last reloc. elf_eh_frame.c and
11512 bfd_elf_discard_info rely on reloc offsets
11514 irela
->r_offset
= last_offset
;
11516 irela
->r_addend
= 0;
11520 irela
->r_offset
+= o
->output_offset
;
11522 /* Relocs in an executable have to be virtual addresses. */
11523 if (!bfd_link_relocatable (flinfo
->info
))
11524 irela
->r_offset
+= o
->output_section
->vma
;
11526 last_offset
= irela
->r_offset
;
11528 r_symndx
= irela
->r_info
>> r_sym_shift
;
11529 if (r_symndx
== STN_UNDEF
)
11532 if (r_symndx
>= locsymcount
11533 || (elf_bad_symtab (input_bfd
)
11534 && flinfo
->sections
[r_symndx
] == NULL
))
11536 struct elf_link_hash_entry
*rh
;
11537 unsigned long indx
;
11539 /* This is a reloc against a global symbol. We
11540 have not yet output all the local symbols, so
11541 we do not know the symbol index of any global
11542 symbol. We set the rel_hash entry for this
11543 reloc to point to the global hash table entry
11544 for this symbol. The symbol index is then
11545 set at the end of bfd_elf_final_link. */
11546 indx
= r_symndx
- extsymoff
;
11547 rh
= elf_sym_hashes (input_bfd
)[indx
];
11548 while (rh
->root
.type
== bfd_link_hash_indirect
11549 || rh
->root
.type
== bfd_link_hash_warning
)
11550 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11552 /* Setting the index to -2 tells
11553 elf_link_output_extsym that this symbol is
11554 used by a reloc. */
11555 BFD_ASSERT (rh
->indx
< 0);
11562 /* This is a reloc against a local symbol. */
11565 sym
= isymbuf
[r_symndx
];
11566 sec
= flinfo
->sections
[r_symndx
];
11567 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11569 /* I suppose the backend ought to fill in the
11570 section of any STT_SECTION symbol against a
11571 processor specific section. */
11572 r_symndx
= STN_UNDEF
;
11573 if (bfd_is_abs_section (sec
))
11575 else if (sec
== NULL
|| sec
->owner
== NULL
)
11577 bfd_set_error (bfd_error_bad_value
);
11582 asection
*osec
= sec
->output_section
;
11584 /* If we have discarded a section, the output
11585 section will be the absolute section. In
11586 case of discarded SEC_MERGE sections, use
11587 the kept section. relocate_section should
11588 have already handled discarded linkonce
11590 if (bfd_is_abs_section (osec
)
11591 && sec
->kept_section
!= NULL
11592 && sec
->kept_section
->output_section
!= NULL
)
11594 osec
= sec
->kept_section
->output_section
;
11595 irela
->r_addend
-= osec
->vma
;
11598 if (!bfd_is_abs_section (osec
))
11600 r_symndx
= osec
->target_index
;
11601 if (r_symndx
== STN_UNDEF
)
11603 irela
->r_addend
+= osec
->vma
;
11604 osec
= _bfd_nearby_section (output_bfd
, osec
,
11606 irela
->r_addend
-= osec
->vma
;
11607 r_symndx
= osec
->target_index
;
11612 /* Adjust the addend according to where the
11613 section winds up in the output section. */
11615 irela
->r_addend
+= sec
->output_offset
;
11619 if (flinfo
->indices
[r_symndx
] == -1)
11621 unsigned long shlink
;
11626 if (flinfo
->info
->strip
== strip_all
)
11628 /* You can't do ld -r -s. */
11629 bfd_set_error (bfd_error_invalid_operation
);
11633 /* This symbol was skipped earlier, but
11634 since it is needed by a reloc, we
11635 must output it now. */
11636 shlink
= symtab_hdr
->sh_link
;
11637 name
= (bfd_elf_string_from_elf_section
11638 (input_bfd
, shlink
, sym
.st_name
));
11642 osec
= sec
->output_section
;
11644 _bfd_elf_section_from_bfd_section (output_bfd
,
11646 if (sym
.st_shndx
== SHN_BAD
)
11649 sym
.st_value
+= sec
->output_offset
;
11650 if (!bfd_link_relocatable (flinfo
->info
))
11652 sym
.st_value
+= osec
->vma
;
11653 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11655 struct elf_link_hash_table
*htab
11656 = elf_hash_table (flinfo
->info
);
11658 /* STT_TLS symbols are relative to PT_TLS
11660 if (htab
->tls_sec
!= NULL
)
11661 sym
.st_value
-= htab
->tls_sec
->vma
;
11664 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11669 indx
= bfd_get_symcount (output_bfd
);
11670 ret
= elf_link_output_symstrtab (flinfo
, name
,
11676 flinfo
->indices
[r_symndx
] = indx
;
11681 r_symndx
= flinfo
->indices
[r_symndx
];
11684 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11685 | (irela
->r_info
& r_type_mask
));
11688 /* Swap out the relocs. */
11689 input_rel_hdr
= esdi
->rel
.hdr
;
11690 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11692 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11697 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11698 * bed
->s
->int_rels_per_ext_rel
);
11699 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11702 input_rela_hdr
= esdi
->rela
.hdr
;
11703 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11705 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11714 /* Write out the modified section contents. */
11715 if (bed
->elf_backend_write_section
11716 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11719 /* Section written out. */
11721 else switch (o
->sec_info_type
)
11723 case SEC_INFO_TYPE_STABS
:
11724 if (! (_bfd_write_section_stabs
11726 &elf_hash_table (flinfo
->info
)->stab_info
,
11727 o
, &elf_section_data (o
)->sec_info
, contents
)))
11730 case SEC_INFO_TYPE_MERGE
:
11731 if (! _bfd_write_merged_section (output_bfd
, o
,
11732 elf_section_data (o
)->sec_info
))
11735 case SEC_INFO_TYPE_EH_FRAME
:
11737 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11742 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11744 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11752 if (! (o
->flags
& SEC_EXCLUDE
))
11754 file_ptr offset
= (file_ptr
) o
->output_offset
;
11755 bfd_size_type todo
= o
->size
;
11757 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11759 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
11761 /* Reverse-copy input section to output. */
11764 todo
-= address_size
;
11765 if (! bfd_set_section_contents (output_bfd
,
11773 offset
+= address_size
;
11777 else if (! bfd_set_section_contents (output_bfd
,
11791 /* Generate a reloc when linking an ELF file. This is a reloc
11792 requested by the linker, and does not come from any input file. This
11793 is used to build constructor and destructor tables when linking
11797 elf_reloc_link_order (bfd
*output_bfd
,
11798 struct bfd_link_info
*info
,
11799 asection
*output_section
,
11800 struct bfd_link_order
*link_order
)
11802 reloc_howto_type
*howto
;
11806 struct bfd_elf_section_reloc_data
*reldata
;
11807 struct elf_link_hash_entry
**rel_hash_ptr
;
11808 Elf_Internal_Shdr
*rel_hdr
;
11809 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11810 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11813 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11815 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11818 bfd_set_error (bfd_error_bad_value
);
11822 addend
= link_order
->u
.reloc
.p
->addend
;
11825 reldata
= &esdo
->rel
;
11826 else if (esdo
->rela
.hdr
)
11827 reldata
= &esdo
->rela
;
11834 /* Figure out the symbol index. */
11835 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11836 if (link_order
->type
== bfd_section_reloc_link_order
)
11838 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11839 BFD_ASSERT (indx
!= 0);
11840 *rel_hash_ptr
= NULL
;
11844 struct elf_link_hash_entry
*h
;
11846 /* Treat a reloc against a defined symbol as though it were
11847 actually against the section. */
11848 h
= ((struct elf_link_hash_entry
*)
11849 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11850 link_order
->u
.reloc
.p
->u
.name
,
11851 false, false, true));
11853 && (h
->root
.type
== bfd_link_hash_defined
11854 || h
->root
.type
== bfd_link_hash_defweak
))
11858 section
= h
->root
.u
.def
.section
;
11859 indx
= section
->output_section
->target_index
;
11860 *rel_hash_ptr
= NULL
;
11861 /* It seems that we ought to add the symbol value to the
11862 addend here, but in practice it has already been added
11863 because it was passed to constructor_callback. */
11864 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11866 else if (h
!= NULL
)
11868 /* Setting the index to -2 tells elf_link_output_extsym that
11869 this symbol is used by a reloc. */
11876 (*info
->callbacks
->unattached_reloc
)
11877 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11882 /* If this is an inplace reloc, we must write the addend into the
11884 if (howto
->partial_inplace
&& addend
!= 0)
11886 bfd_size_type size
;
11887 bfd_reloc_status_type rstat
;
11890 const char *sym_name
;
11891 bfd_size_type octets
;
11893 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11894 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11895 if (buf
== NULL
&& size
!= 0)
11897 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11904 case bfd_reloc_outofrange
:
11907 case bfd_reloc_overflow
:
11908 if (link_order
->type
== bfd_section_reloc_link_order
)
11909 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
11911 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11912 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11913 howto
->name
, addend
, NULL
, NULL
,
11918 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
11920 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11927 /* The address of a reloc is relative to the section in a
11928 relocatable file, and is a virtual address in an executable
11930 offset
= link_order
->offset
;
11931 if (! bfd_link_relocatable (info
))
11932 offset
+= output_section
->vma
;
11934 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11936 irel
[i
].r_offset
= offset
;
11937 irel
[i
].r_info
= 0;
11938 irel
[i
].r_addend
= 0;
11940 if (bed
->s
->arch_size
== 32)
11941 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11943 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11945 rel_hdr
= reldata
->hdr
;
11946 erel
= rel_hdr
->contents
;
11947 if (rel_hdr
->sh_type
== SHT_REL
)
11949 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11950 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11954 irel
[0].r_addend
= addend
;
11955 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11956 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11964 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11965 Returns TRUE upon success, FALSE otherwise. */
11968 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11972 const struct elf_backend_data
*bed
;
11974 enum bfd_architecture arch
;
11976 asymbol
**sympp
= NULL
;
11980 elf_symbol_type
*osymbuf
;
11983 implib_bfd
= info
->out_implib_bfd
;
11984 bed
= get_elf_backend_data (abfd
);
11986 if (!bfd_set_format (implib_bfd
, bfd_object
))
11989 /* Use flag from executable but make it a relocatable object. */
11990 flags
= bfd_get_file_flags (abfd
);
11991 flags
&= ~HAS_RELOC
;
11992 if (!bfd_set_start_address (implib_bfd
, 0)
11993 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
11996 /* Copy architecture of output file to import library file. */
11997 arch
= bfd_get_arch (abfd
);
11998 mach
= bfd_get_mach (abfd
);
11999 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
12000 && (abfd
->target_defaulted
12001 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
12004 /* Get symbol table size. */
12005 symsize
= bfd_get_symtab_upper_bound (abfd
);
12009 /* Read in the symbol table. */
12010 sympp
= (asymbol
**) bfd_malloc (symsize
);
12014 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
12018 /* Allow the BFD backend to copy any private header data it
12019 understands from the output BFD to the import library BFD. */
12020 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
12023 /* Filter symbols to appear in the import library. */
12024 if (bed
->elf_backend_filter_implib_symbols
)
12025 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
12028 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
12031 bfd_set_error (bfd_error_no_symbols
);
12032 _bfd_error_handler (_("%pB: no symbol found for import library"),
12038 /* Make symbols absolute. */
12039 amt
= symcount
* sizeof (*osymbuf
);
12040 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
12041 if (osymbuf
== NULL
)
12044 for (src_count
= 0; src_count
< symcount
; src_count
++)
12046 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
12047 sizeof (*osymbuf
));
12048 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
12049 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
12050 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
12051 osymbuf
[src_count
].internal_elf_sym
.st_value
=
12052 osymbuf
[src_count
].symbol
.value
;
12053 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
12056 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
12058 /* Allow the BFD backend to copy any private data it understands
12059 from the output BFD to the import library BFD. This is done last
12060 to permit the routine to look at the filtered symbol table. */
12061 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
12064 if (!bfd_close (implib_bfd
))
12075 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
12079 if (flinfo
->symstrtab
!= NULL
)
12080 _bfd_elf_strtab_free (flinfo
->symstrtab
);
12081 free (flinfo
->contents
);
12082 free (flinfo
->external_relocs
);
12083 free (flinfo
->internal_relocs
);
12084 free (flinfo
->external_syms
);
12085 free (flinfo
->locsym_shndx
);
12086 free (flinfo
->internal_syms
);
12087 free (flinfo
->indices
);
12088 free (flinfo
->sections
);
12089 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12090 free (flinfo
->symshndxbuf
);
12091 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12093 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12094 free (esdo
->rel
.hashes
);
12095 free (esdo
->rela
.hashes
);
12099 /* Do the final step of an ELF link. */
12102 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12107 struct elf_final_link_info flinfo
;
12109 struct bfd_link_order
*p
;
12111 bfd_size_type max_contents_size
;
12112 bfd_size_type max_external_reloc_size
;
12113 bfd_size_type max_internal_reloc_count
;
12114 bfd_size_type max_sym_count
;
12115 bfd_size_type max_sym_shndx_count
;
12116 Elf_Internal_Sym elfsym
;
12118 Elf_Internal_Shdr
*symtab_hdr
;
12119 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12120 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12121 struct elf_outext_info eoinfo
;
12123 size_t relativecount
= 0;
12124 asection
*reldyn
= 0;
12126 asection
*attr_section
= NULL
;
12127 bfd_vma attr_size
= 0;
12128 const char *std_attrs_section
;
12129 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12130 bool sections_removed
;
12133 if (!is_elf_hash_table (&htab
->root
))
12136 if (bfd_link_pic (info
))
12137 abfd
->flags
|= DYNAMIC
;
12139 dynamic
= htab
->dynamic_sections_created
;
12140 dynobj
= htab
->dynobj
;
12142 emit_relocs
= (bfd_link_relocatable (info
)
12143 || info
->emitrelocations
);
12145 memset (&flinfo
, 0, sizeof (flinfo
));
12146 flinfo
.info
= info
;
12147 flinfo
.output_bfd
= abfd
;
12148 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12149 if (flinfo
.symstrtab
== NULL
)
12154 flinfo
.hash_sec
= NULL
;
12155 flinfo
.symver_sec
= NULL
;
12159 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12160 /* Note that dynsym_sec can be NULL (on VMS). */
12161 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12162 /* Note that it is OK if symver_sec is NULL. */
12165 if (info
->unique_symbol
12166 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12167 local_hash_newfunc
,
12168 sizeof (struct local_hash_entry
)))
12171 /* The object attributes have been merged. Remove the input
12172 sections from the link, and set the contents of the output
12174 sections_removed
= false;
12175 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12176 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12178 bool remove_section
= false;
12180 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12181 || strcmp (o
->name
, ".gnu.attributes") == 0)
12183 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12185 asection
*input_section
;
12187 if (p
->type
!= bfd_indirect_link_order
)
12189 input_section
= p
->u
.indirect
.section
;
12190 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12191 elf_link_input_bfd ignores this section. */
12192 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12195 attr_size
= bfd_elf_obj_attr_size (abfd
);
12196 bfd_set_section_size (o
, attr_size
);
12197 /* Skip this section later on. */
12198 o
->map_head
.link_order
= NULL
;
12202 remove_section
= true;
12204 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12206 /* Remove empty group section from linker output. */
12207 remove_section
= true;
12209 if (remove_section
)
12211 o
->flags
|= SEC_EXCLUDE
;
12212 bfd_section_list_remove (abfd
, o
);
12213 abfd
->section_count
--;
12214 sections_removed
= true;
12217 if (sections_removed
)
12218 _bfd_fix_excluded_sec_syms (abfd
, info
);
12220 /* Count up the number of relocations we will output for each output
12221 section, so that we know the sizes of the reloc sections. We
12222 also figure out some maximum sizes. */
12223 max_contents_size
= 0;
12224 max_external_reloc_size
= 0;
12225 max_internal_reloc_count
= 0;
12227 max_sym_shndx_count
= 0;
12229 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12231 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12232 o
->reloc_count
= 0;
12234 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12236 unsigned int reloc_count
= 0;
12237 unsigned int additional_reloc_count
= 0;
12238 struct bfd_elf_section_data
*esdi
= NULL
;
12240 if (p
->type
== bfd_section_reloc_link_order
12241 || p
->type
== bfd_symbol_reloc_link_order
)
12243 else if (p
->type
== bfd_indirect_link_order
)
12247 sec
= p
->u
.indirect
.section
;
12249 /* Mark all sections which are to be included in the
12250 link. This will normally be every section. We need
12251 to do this so that we can identify any sections which
12252 the linker has decided to not include. */
12253 sec
->linker_mark
= true;
12255 if (sec
->flags
& SEC_MERGE
)
12258 if (sec
->rawsize
> max_contents_size
)
12259 max_contents_size
= sec
->rawsize
;
12260 if (sec
->size
> max_contents_size
)
12261 max_contents_size
= sec
->size
;
12263 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12264 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12268 /* We are interested in just local symbols, not all
12270 if (elf_bad_symtab (sec
->owner
))
12271 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12272 / bed
->s
->sizeof_sym
);
12274 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12276 if (sym_count
> max_sym_count
)
12277 max_sym_count
= sym_count
;
12279 if (sym_count
> max_sym_shndx_count
12280 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12281 max_sym_shndx_count
= sym_count
;
12283 if (esdo
->this_hdr
.sh_type
== SHT_REL
12284 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
12285 /* Some backends use reloc_count in relocation sections
12286 to count particular types of relocs. Of course,
12287 reloc sections themselves can't have relocations. */
12289 else if (emit_relocs
)
12291 reloc_count
= sec
->reloc_count
;
12292 if (bed
->elf_backend_count_additional_relocs
)
12295 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12296 additional_reloc_count
+= c
;
12299 else if (bed
->elf_backend_count_relocs
)
12300 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12302 esdi
= elf_section_data (sec
);
12304 if ((sec
->flags
& SEC_RELOC
) != 0)
12306 size_t ext_size
= 0;
12308 if (esdi
->rel
.hdr
!= NULL
)
12309 ext_size
= esdi
->rel
.hdr
->sh_size
;
12310 if (esdi
->rela
.hdr
!= NULL
)
12311 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12313 if (ext_size
> max_external_reloc_size
)
12314 max_external_reloc_size
= ext_size
;
12315 if (sec
->reloc_count
> max_internal_reloc_count
)
12316 max_internal_reloc_count
= sec
->reloc_count
;
12321 if (reloc_count
== 0)
12324 reloc_count
+= additional_reloc_count
;
12325 o
->reloc_count
+= reloc_count
;
12327 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12331 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12332 esdo
->rel
.count
+= additional_reloc_count
;
12334 if (esdi
->rela
.hdr
)
12336 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12337 esdo
->rela
.count
+= additional_reloc_count
;
12343 esdo
->rela
.count
+= reloc_count
;
12345 esdo
->rel
.count
+= reloc_count
;
12349 if (o
->reloc_count
> 0)
12350 o
->flags
|= SEC_RELOC
;
12353 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12354 set it (this is probably a bug) and if it is set
12355 assign_section_numbers will create a reloc section. */
12356 o
->flags
&=~ SEC_RELOC
;
12359 /* If the SEC_ALLOC flag is not set, force the section VMA to
12360 zero. This is done in elf_fake_sections as well, but forcing
12361 the VMA to 0 here will ensure that relocs against these
12362 sections are handled correctly. */
12363 if ((o
->flags
& SEC_ALLOC
) == 0
12364 && ! o
->user_set_vma
)
12368 if (! bfd_link_relocatable (info
) && merged
)
12369 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12371 /* Figure out the file positions for everything but the symbol table
12372 and the relocs. We set symcount to force assign_section_numbers
12373 to create a symbol table. */
12374 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12375 BFD_ASSERT (! abfd
->output_has_begun
);
12376 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12379 /* Set sizes, and assign file positions for reloc sections. */
12380 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12382 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12383 if ((o
->flags
& SEC_RELOC
) != 0)
12386 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12390 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12394 /* _bfd_elf_compute_section_file_positions makes temporary use
12395 of target_index. Reset it. */
12396 o
->target_index
= 0;
12398 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12399 to count upwards while actually outputting the relocations. */
12400 esdo
->rel
.count
= 0;
12401 esdo
->rela
.count
= 0;
12403 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12404 && !bfd_section_is_ctf (o
))
12406 /* Cache the section contents so that they can be compressed
12407 later. Use bfd_malloc since it will be freed by
12408 bfd_compress_section_contents. */
12409 unsigned char *contents
= esdo
->this_hdr
.contents
;
12410 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
12413 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12414 if (contents
== NULL
)
12416 esdo
->this_hdr
.contents
= contents
;
12420 /* We have now assigned file positions for all the sections except .symtab,
12421 .strtab, and non-loaded reloc and compressed debugging sections. We start
12422 the .symtab section at the current file position, and write directly to it.
12423 We build the .strtab section in memory. */
12424 abfd
->symcount
= 0;
12425 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12426 /* sh_name is set in prep_headers. */
12427 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12428 /* sh_flags, sh_addr and sh_size all start off zero. */
12429 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12430 /* sh_link is set in assign_section_numbers. */
12431 /* sh_info is set below. */
12432 /* sh_offset is set just below. */
12433 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12435 if (max_sym_count
< 20)
12436 max_sym_count
= 20;
12437 htab
->strtabsize
= max_sym_count
;
12438 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12439 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12440 if (htab
->strtab
== NULL
)
12442 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12444 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12445 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12447 if (info
->strip
!= strip_all
|| emit_relocs
)
12449 file_ptr off
= elf_next_file_pos (abfd
);
12451 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
12453 /* Note that at this point elf_next_file_pos (abfd) is
12454 incorrect. We do not yet know the size of the .symtab section.
12455 We correct next_file_pos below, after we do know the size. */
12457 /* Start writing out the symbol table. The first symbol is always a
12459 elfsym
.st_value
= 0;
12460 elfsym
.st_size
= 0;
12461 elfsym
.st_info
= 0;
12462 elfsym
.st_other
= 0;
12463 elfsym
.st_shndx
= SHN_UNDEF
;
12464 elfsym
.st_target_internal
= 0;
12465 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12466 bfd_und_section_ptr
, NULL
) != 1)
12469 /* Output a symbol for each section if asked or they are used for
12470 relocs. These symbols usually have no names. We store the
12471 index of each one in the index field of the section, so that
12472 we can find it again when outputting relocs. */
12474 if (bfd_keep_unused_section_symbols (abfd
) || emit_relocs
)
12476 bool name_local_sections
12477 = (bed
->elf_backend_name_local_section_symbols
12478 && bed
->elf_backend_name_local_section_symbols (abfd
));
12479 const char *name
= NULL
;
12481 elfsym
.st_size
= 0;
12482 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12483 elfsym
.st_other
= 0;
12484 elfsym
.st_value
= 0;
12485 elfsym
.st_target_internal
= 0;
12486 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12488 o
= bfd_section_from_elf_index (abfd
, i
);
12491 o
->target_index
= bfd_get_symcount (abfd
);
12492 elfsym
.st_shndx
= i
;
12493 if (!bfd_link_relocatable (info
))
12494 elfsym
.st_value
= o
->vma
;
12495 if (name_local_sections
)
12497 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12505 /* On some targets like Irix 5 the symbol split between local and global
12506 ones recorded in the sh_info field needs to be done between section
12507 and all other symbols. */
12508 if (bed
->elf_backend_elfsym_local_is_section
12509 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12510 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12512 /* Allocate some memory to hold information read in from the input
12514 if (max_contents_size
!= 0)
12516 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12517 if (flinfo
.contents
== NULL
)
12521 if (max_external_reloc_size
!= 0)
12523 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12524 if (flinfo
.external_relocs
== NULL
)
12528 if (max_internal_reloc_count
!= 0)
12530 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12531 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12532 if (flinfo
.internal_relocs
== NULL
)
12536 if (max_sym_count
!= 0)
12538 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12539 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12540 if (flinfo
.external_syms
== NULL
)
12543 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12544 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12545 if (flinfo
.internal_syms
== NULL
)
12548 amt
= max_sym_count
* sizeof (long);
12549 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12550 if (flinfo
.indices
== NULL
)
12553 amt
= max_sym_count
* sizeof (asection
*);
12554 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12555 if (flinfo
.sections
== NULL
)
12559 if (max_sym_shndx_count
!= 0)
12561 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12562 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12563 if (flinfo
.locsym_shndx
== NULL
)
12569 bfd_vma base
, end
= 0; /* Both bytes. */
12572 for (sec
= htab
->tls_sec
;
12573 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12576 bfd_size_type size
= sec
->size
;
12577 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12580 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12582 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12585 size
= ord
->offset
* opb
+ ord
->size
;
12587 end
= sec
->vma
+ size
/ opb
;
12589 base
= htab
->tls_sec
->vma
;
12590 /* Only align end of TLS section if static TLS doesn't have special
12591 alignment requirements. */
12592 if (bed
->static_tls_alignment
== 1)
12593 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12594 htab
->tls_size
= end
- base
;
12597 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12600 /* Since ELF permits relocations to be against local symbols, we
12601 must have the local symbols available when we do the relocations.
12602 Since we would rather only read the local symbols once, and we
12603 would rather not keep them in memory, we handle all the
12604 relocations for a single input file at the same time.
12606 Unfortunately, there is no way to know the total number of local
12607 symbols until we have seen all of them, and the local symbol
12608 indices precede the global symbol indices. This means that when
12609 we are generating relocatable output, and we see a reloc against
12610 a global symbol, we can not know the symbol index until we have
12611 finished examining all the local symbols to see which ones we are
12612 going to output. To deal with this, we keep the relocations in
12613 memory, and don't output them until the end of the link. This is
12614 an unfortunate waste of memory, but I don't see a good way around
12615 it. Fortunately, it only happens when performing a relocatable
12616 link, which is not the common case. FIXME: If keep_memory is set
12617 we could write the relocs out and then read them again; I don't
12618 know how bad the memory loss will be. */
12620 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12621 sub
->output_has_begun
= false;
12622 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12624 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12626 if (p
->type
== bfd_indirect_link_order
12627 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12628 == bfd_target_elf_flavour
)
12629 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12631 if (! sub
->output_has_begun
)
12633 if (! elf_link_input_bfd (&flinfo
, sub
))
12635 sub
->output_has_begun
= true;
12638 else if (p
->type
== bfd_section_reloc_link_order
12639 || p
->type
== bfd_symbol_reloc_link_order
)
12641 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12646 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12648 if (p
->type
== bfd_indirect_link_order
12649 && (bfd_get_flavour (sub
)
12650 == bfd_target_elf_flavour
)
12651 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12652 != bed
->s
->elfclass
))
12654 const char *iclass
, *oclass
;
12656 switch (bed
->s
->elfclass
)
12658 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12659 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12660 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12664 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12666 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12667 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12668 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12672 bfd_set_error (bfd_error_wrong_format
);
12674 /* xgettext:c-format */
12675 (_("%pB: file class %s incompatible with %s"),
12676 sub
, iclass
, oclass
);
12685 /* Free symbol buffer if needed. */
12686 if (!info
->reduce_memory_overheads
)
12688 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12689 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
12691 free (elf_tdata (sub
)->symbuf
);
12692 elf_tdata (sub
)->symbuf
= NULL
;
12698 /* Output any global symbols that got converted to local in a
12699 version script or due to symbol visibility. We do this in a
12700 separate step since ELF requires all local symbols to appear
12701 prior to any global symbols. FIXME: We should only do this if
12702 some global symbols were, in fact, converted to become local.
12703 FIXME: Will this work correctly with the Irix 5 linker? */
12704 eoinfo
.failed
= false;
12705 eoinfo
.flinfo
= &flinfo
;
12706 eoinfo
.localsyms
= true;
12707 eoinfo
.file_sym_done
= false;
12708 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12712 goto return_local_hash_table
;
12715 /* If backend needs to output some local symbols not present in the hash
12716 table, do it now. */
12717 if (bed
->elf_backend_output_arch_local_syms
12718 && (info
->strip
!= strip_all
|| emit_relocs
))
12720 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12721 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
12724 goto return_local_hash_table
;
12728 /* That wrote out all the local symbols. Finish up the symbol table
12729 with the global symbols. Even if we want to strip everything we
12730 can, we still need to deal with those global symbols that got
12731 converted to local in a version script. */
12733 /* The sh_info field records the index of the first non local symbol. */
12734 if (!symtab_hdr
->sh_info
)
12735 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12738 && htab
->dynsym
!= NULL
12739 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12741 Elf_Internal_Sym sym
;
12742 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12744 o
= htab
->dynsym
->output_section
;
12745 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12747 /* Write out the section symbols for the output sections. */
12748 if (bfd_link_pic (info
)
12749 || htab
->is_relocatable_executable
)
12755 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12757 sym
.st_target_internal
= 0;
12759 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12765 dynindx
= elf_section_data (s
)->dynindx
;
12768 indx
= elf_section_data (s
)->this_idx
;
12769 BFD_ASSERT (indx
> 0);
12770 sym
.st_shndx
= indx
;
12771 if (! check_dynsym (abfd
, &sym
))
12774 goto return_local_hash_table
;
12776 sym
.st_value
= s
->vma
;
12777 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12779 /* Inform the linker of the addition of this symbol. */
12781 if (info
->callbacks
->ctf_new_dynsym
)
12782 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
12784 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12788 /* Write out the local dynsyms. */
12789 if (htab
->dynlocal
)
12791 struct elf_link_local_dynamic_entry
*e
;
12792 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12797 /* Copy the internal symbol and turn off visibility.
12798 Note that we saved a word of storage and overwrote
12799 the original st_name with the dynstr_index. */
12801 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12802 sym
.st_shndx
= SHN_UNDEF
;
12804 s
= bfd_section_from_elf_index (e
->input_bfd
,
12807 && s
->output_section
!= NULL
12808 && elf_section_data (s
->output_section
) != NULL
)
12811 elf_section_data (s
->output_section
)->this_idx
;
12812 if (! check_dynsym (abfd
, &sym
))
12815 goto return_local_hash_table
;
12817 sym
.st_value
= (s
->output_section
->vma
12819 + e
->isym
.st_value
);
12822 /* Inform the linker of the addition of this symbol. */
12824 if (info
->callbacks
->ctf_new_dynsym
)
12825 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
12827 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12828 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12833 /* We get the global symbols from the hash table. */
12834 eoinfo
.failed
= false;
12835 eoinfo
.localsyms
= false;
12836 eoinfo
.flinfo
= &flinfo
;
12837 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12841 goto return_local_hash_table
;
12844 /* If backend needs to output some symbols not present in the hash
12845 table, do it now. */
12846 if (bed
->elf_backend_output_arch_syms
12847 && (info
->strip
!= strip_all
|| emit_relocs
))
12849 if (! ((*bed
->elf_backend_output_arch_syms
)
12850 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
12853 goto return_local_hash_table
;
12857 /* Finalize the .strtab section. */
12858 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12860 /* Swap out the .strtab section. */
12861 if (!elf_link_swap_symbols_out (&flinfo
))
12864 goto return_local_hash_table
;
12867 /* Now we know the size of the symtab section. */
12868 if (bfd_get_symcount (abfd
) > 0)
12870 /* Finish up and write out the symbol string table (.strtab)
12872 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12873 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12875 if (elf_symtab_shndx_list (abfd
))
12877 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12879 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12881 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12882 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12883 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12884 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12885 symtab_shndx_hdr
->sh_size
= amt
;
12887 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12890 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12891 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12894 goto return_local_hash_table
;
12899 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12900 /* sh_name was set in prep_headers. */
12901 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12902 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12903 symstrtab_hdr
->sh_addr
= 0;
12904 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12905 symstrtab_hdr
->sh_entsize
= 0;
12906 symstrtab_hdr
->sh_link
= 0;
12907 symstrtab_hdr
->sh_info
= 0;
12908 /* sh_offset is set just below. */
12909 symstrtab_hdr
->sh_addralign
= 1;
12911 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12913 elf_next_file_pos (abfd
) = off
;
12915 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12916 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12919 goto return_local_hash_table
;
12923 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12925 _bfd_error_handler (_("%pB: failed to generate import library"),
12926 info
->out_implib_bfd
);
12928 goto return_local_hash_table
;
12931 /* Adjust the relocs to have the correct symbol indices. */
12932 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12934 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12937 if ((o
->flags
& SEC_RELOC
) == 0)
12940 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12941 if (esdo
->rel
.hdr
!= NULL
12942 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
12945 goto return_local_hash_table
;
12947 if (esdo
->rela
.hdr
!= NULL
12948 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
12951 goto return_local_hash_table
;
12954 /* Set the reloc_count field to 0 to prevent write_relocs from
12955 trying to swap the relocs out itself. */
12956 o
->reloc_count
= 0;
12959 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12960 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12962 /* If we are linking against a dynamic object, or generating a
12963 shared library, finish up the dynamic linking information. */
12966 bfd_byte
*dyncon
, *dynconend
;
12968 /* Fix up .dynamic entries. */
12969 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12970 BFD_ASSERT (o
!= NULL
);
12972 dyncon
= o
->contents
;
12973 dynconend
= PTR_ADD (o
->contents
, o
->size
);
12974 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12976 Elf_Internal_Dyn dyn
;
12979 bfd_size_type sh_size
;
12982 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12989 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12991 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12993 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12994 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12997 dyn
.d_un
.d_val
= relativecount
;
13004 name
= info
->init_function
;
13007 name
= info
->fini_function
;
13010 struct elf_link_hash_entry
*h
;
13012 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
13014 && (h
->root
.type
== bfd_link_hash_defined
13015 || h
->root
.type
== bfd_link_hash_defweak
))
13017 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
13018 o
= h
->root
.u
.def
.section
;
13019 if (o
->output_section
!= NULL
)
13020 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
13021 + o
->output_offset
);
13024 /* The symbol is imported from another shared
13025 library and does not apply to this one. */
13026 dyn
.d_un
.d_ptr
= 0;
13033 case DT_PREINIT_ARRAYSZ
:
13034 name
= ".preinit_array";
13036 case DT_INIT_ARRAYSZ
:
13037 name
= ".init_array";
13039 case DT_FINI_ARRAYSZ
:
13040 name
= ".fini_array";
13042 o
= bfd_get_section_by_name (abfd
, name
);
13046 (_("could not find section %s"), name
);
13051 (_("warning: %s section has zero size"), name
);
13052 dyn
.d_un
.d_val
= o
->size
;
13055 case DT_PREINIT_ARRAY
:
13056 name
= ".preinit_array";
13058 case DT_INIT_ARRAY
:
13059 name
= ".init_array";
13061 case DT_FINI_ARRAY
:
13062 name
= ".fini_array";
13064 o
= bfd_get_section_by_name (abfd
, name
);
13071 name
= ".gnu.hash";
13080 name
= ".gnu.version_d";
13083 name
= ".gnu.version_r";
13086 name
= ".gnu.version";
13088 o
= bfd_get_linker_section (dynobj
, name
);
13090 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13093 (_("could not find section %s"), name
);
13096 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13099 (_("warning: section '%s' is being made into a note"), name
);
13100 bfd_set_error (bfd_error_nonrepresentable_section
);
13103 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13110 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13116 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13118 Elf_Internal_Shdr
*hdr
;
13120 hdr
= elf_elfsections (abfd
)[i
];
13121 if (hdr
->sh_type
== type
13122 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13124 sh_size
+= hdr
->sh_size
;
13126 || sh_addr
> hdr
->sh_addr
)
13127 sh_addr
= hdr
->sh_addr
;
13131 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13133 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13135 /* Don't count procedure linkage table relocs in the
13136 overall reloc count. */
13137 sh_size
-= htab
->srelplt
->size
;
13139 /* If the size is zero, make the address zero too.
13140 This is to avoid a glibc bug. If the backend
13141 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13142 zero, then we'll put DT_RELA at the end of
13143 DT_JMPREL. glibc will interpret the end of
13144 DT_RELA matching the end of DT_JMPREL as the
13145 case where DT_RELA includes DT_JMPREL, and for
13146 LD_BIND_NOW will decide that processing DT_RELA
13147 will process the PLT relocs too. Net result:
13148 No PLT relocs applied. */
13151 /* If .rela.plt is the first .rela section, exclude
13152 it from DT_RELA. */
13153 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13154 + htab
->srelplt
->output_offset
) * opb
)
13155 sh_addr
+= htab
->srelplt
->size
;
13158 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13159 dyn
.d_un
.d_val
= sh_size
;
13161 dyn
.d_un
.d_ptr
= sh_addr
;
13164 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13168 /* If we have created any dynamic sections, then output them. */
13169 if (dynobj
!= NULL
)
13171 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13174 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13175 if (bfd_link_textrel_check (info
)
13176 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
13179 bfd_byte
*dyncon
, *dynconend
;
13181 dyncon
= o
->contents
;
13182 dynconend
= o
->contents
+ o
->size
;
13183 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13185 Elf_Internal_Dyn dyn
;
13187 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13189 if (dyn
.d_tag
== DT_TEXTREL
)
13191 if (info
->textrel_check
== textrel_check_error
)
13192 info
->callbacks
->einfo
13193 (_("%P%X: read-only segment has dynamic relocations\n"));
13194 else if (bfd_link_dll (info
))
13195 info
->callbacks
->einfo
13196 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13197 else if (bfd_link_pde (info
))
13198 info
->callbacks
->einfo
13199 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13201 info
->callbacks
->einfo
13202 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13208 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13210 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13212 || o
->output_section
== bfd_abs_section_ptr
)
13214 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13216 /* At this point, we are only interested in sections
13217 created by _bfd_elf_link_create_dynamic_sections. */
13220 if (htab
->stab_info
.stabstr
== o
)
13222 if (htab
->eh_info
.hdr_sec
== o
)
13224 if (strcmp (o
->name
, ".dynstr") != 0)
13226 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13227 * bfd_octets_per_byte (abfd
, o
));
13228 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13229 o
->contents
, octets
, o
->size
))
13234 /* The contents of the .dynstr section are actually in a
13238 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13239 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13240 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13246 if (!info
->resolve_section_groups
)
13248 bool failed
= false;
13250 BFD_ASSERT (bfd_link_relocatable (info
));
13251 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13256 /* If we have optimized stabs strings, output them. */
13257 if (htab
->stab_info
.stabstr
!= NULL
)
13259 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13263 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13266 if (info
->callbacks
->emit_ctf
)
13267 info
->callbacks
->emit_ctf ();
13269 elf_final_link_free (abfd
, &flinfo
);
13273 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13274 if (contents
== NULL
)
13276 /* Bail out and fail. */
13278 goto return_local_hash_table
;
13280 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13281 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13285 return_local_hash_table
:
13286 if (info
->unique_symbol
)
13287 bfd_hash_table_free (&flinfo
.local_hash_table
);
13291 elf_final_link_free (abfd
, &flinfo
);
13293 goto return_local_hash_table
;
13296 /* Initialize COOKIE for input bfd ABFD. */
13299 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13300 struct bfd_link_info
*info
, bfd
*abfd
)
13302 Elf_Internal_Shdr
*symtab_hdr
;
13303 const struct elf_backend_data
*bed
;
13305 bed
= get_elf_backend_data (abfd
);
13306 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13308 cookie
->abfd
= abfd
;
13309 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13310 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13311 if (cookie
->bad_symtab
)
13313 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13314 cookie
->extsymoff
= 0;
13318 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13319 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13322 if (bed
->s
->arch_size
== 32)
13323 cookie
->r_sym_shift
= 8;
13325 cookie
->r_sym_shift
= 32;
13327 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13328 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13330 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13331 cookie
->locsymcount
, 0,
13333 if (cookie
->locsyms
== NULL
)
13335 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13338 if (_bfd_link_keep_memory (info
) )
13340 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13341 info
->cache_size
+= (cookie
->locsymcount
13342 * sizeof (Elf_External_Sym_Shndx
));
13348 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13351 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13353 Elf_Internal_Shdr
*symtab_hdr
;
13355 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13356 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13357 free (cookie
->locsyms
);
13360 /* Initialize the relocation information in COOKIE for input section SEC
13361 of input bfd ABFD. */
13364 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13365 struct bfd_link_info
*info
, bfd
*abfd
,
13368 if (sec
->reloc_count
== 0)
13370 cookie
->rels
= NULL
;
13371 cookie
->relend
= NULL
;
13375 cookie
->rels
= _bfd_elf_link_info_read_relocs (abfd
, info
, sec
,
13377 _bfd_link_keep_memory (info
));
13378 if (cookie
->rels
== NULL
)
13380 cookie
->rel
= cookie
->rels
;
13381 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13383 cookie
->rel
= cookie
->rels
;
13387 /* Free the memory allocated by init_reloc_cookie_rels,
13391 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13394 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13395 free (cookie
->rels
);
13398 /* Initialize the whole of COOKIE for input section SEC. */
13401 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13402 struct bfd_link_info
*info
,
13405 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13407 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13412 fini_reloc_cookie (cookie
, sec
->owner
);
13417 /* Free the memory allocated by init_reloc_cookie_for_section,
13421 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13424 fini_reloc_cookie_rels (cookie
, sec
);
13425 fini_reloc_cookie (cookie
, sec
->owner
);
13428 /* Garbage collect unused sections. */
13430 /* Default gc_mark_hook. */
13433 _bfd_elf_gc_mark_hook (asection
*sec
,
13434 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13435 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13436 struct elf_link_hash_entry
*h
,
13437 Elf_Internal_Sym
*sym
)
13441 switch (h
->root
.type
)
13443 case bfd_link_hash_defined
:
13444 case bfd_link_hash_defweak
:
13445 return h
->root
.u
.def
.section
;
13447 case bfd_link_hash_common
:
13448 return h
->root
.u
.c
.p
->section
;
13455 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13460 /* Return the debug definition section. */
13463 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13464 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13465 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13466 struct elf_link_hash_entry
*h
,
13467 Elf_Internal_Sym
*sym
)
13471 /* Return the global debug definition section. */
13472 if ((h
->root
.type
== bfd_link_hash_defined
13473 || h
->root
.type
== bfd_link_hash_defweak
)
13474 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13475 return h
->root
.u
.def
.section
;
13479 /* Return the local debug definition section. */
13480 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13482 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
13489 /* COOKIE->rel describes a relocation against section SEC, which is
13490 a section we've decided to keep. Return the section that contains
13491 the relocation symbol, or NULL if no section contains it. */
13494 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13495 elf_gc_mark_hook_fn gc_mark_hook
,
13496 struct elf_reloc_cookie
*cookie
,
13499 unsigned long r_symndx
;
13500 struct elf_link_hash_entry
*h
, *hw
;
13502 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13503 if (r_symndx
== STN_UNDEF
)
13506 if (r_symndx
>= cookie
->locsymcount
13507 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13511 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13514 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13518 while (h
->root
.type
== bfd_link_hash_indirect
13519 || h
->root
.type
== bfd_link_hash_warning
)
13520 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13522 was_marked
= h
->mark
;
13524 /* Keep all aliases of the symbol too. If an object symbol
13525 needs to be copied into .dynbss then all of its aliases
13526 should be present as dynamic symbols, not just the one used
13527 on the copy relocation. */
13529 while (hw
->is_weakalias
)
13535 if (!was_marked
&& h
->start_stop
&& !h
->root
.ldscript_def
)
13537 if (info
->start_stop_gc
)
13540 /* To work around a glibc bug, mark XXX input sections
13541 when there is a reference to __start_XXX or __stop_XXX
13543 else if (start_stop
!= NULL
)
13545 asection
*s
= h
->u2
.start_stop_section
;
13546 *start_stop
= true;
13551 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13554 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13555 &cookie
->locsyms
[r_symndx
]);
13558 /* COOKIE->rel describes a relocation against section SEC, which is
13559 a section we've decided to keep. Mark the section that contains
13560 the relocation symbol. */
13563 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13565 elf_gc_mark_hook_fn gc_mark_hook
,
13566 struct elf_reloc_cookie
*cookie
)
13569 bool start_stop
= false;
13571 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13572 while (rsec
!= NULL
)
13574 if (!rsec
->gc_mark
)
13576 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13577 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13579 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13584 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13589 /* The mark phase of garbage collection. For a given section, mark
13590 it and any sections in this section's group, and all the sections
13591 which define symbols to which it refers. */
13594 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13596 elf_gc_mark_hook_fn gc_mark_hook
)
13599 asection
*group_sec
, *eh_frame
;
13603 /* Mark all the sections in the group. */
13604 group_sec
= elf_section_data (sec
)->next_in_group
;
13605 if (group_sec
&& !group_sec
->gc_mark
)
13606 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13609 /* Look through the section relocs. */
13611 eh_frame
= elf_eh_frame_section (sec
->owner
);
13612 if ((sec
->flags
& SEC_RELOC
) != 0
13613 && sec
->reloc_count
> 0
13614 && sec
!= eh_frame
)
13616 struct elf_reloc_cookie cookie
;
13618 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13622 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13623 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13628 fini_reloc_cookie_for_section (&cookie
, sec
);
13632 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13634 struct elf_reloc_cookie cookie
;
13636 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13640 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13641 gc_mark_hook
, &cookie
))
13643 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13647 eh_frame
= elf_section_eh_frame_entry (sec
);
13648 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13649 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13655 /* Scan and mark sections in a special or debug section group. */
13658 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13660 /* Point to first section of section group. */
13662 /* Used to iterate the section group. */
13665 bool is_special_grp
= true;
13666 bool is_debug_grp
= true;
13668 /* First scan to see if group contains any section other than debug
13669 and special section. */
13670 ssec
= msec
= elf_next_in_group (grp
);
13673 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13674 is_debug_grp
= false;
13676 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13677 is_special_grp
= false;
13679 msec
= elf_next_in_group (msec
);
13681 while (msec
!= ssec
);
13683 /* If this is a pure debug section group or pure special section group,
13684 keep all sections in this group. */
13685 if (is_debug_grp
|| is_special_grp
)
13690 msec
= elf_next_in_group (msec
);
13692 while (msec
!= ssec
);
13696 /* Keep debug and special sections. */
13699 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13700 elf_gc_mark_hook_fn mark_hook
)
13704 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13708 bool debug_frag_seen
;
13709 bool has_kept_debug_info
;
13711 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13713 isec
= ibfd
->sections
;
13714 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13717 /* Ensure all linker created sections are kept,
13718 see if any other section is already marked,
13719 and note if we have any fragmented debug sections. */
13720 debug_frag_seen
= some_kept
= has_kept_debug_info
= false;
13721 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13723 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13725 else if (isec
->gc_mark
13726 && (isec
->flags
& SEC_ALLOC
) != 0
13727 && elf_section_type (isec
) != SHT_NOTE
)
13731 /* Since all sections, except for backend specific ones,
13732 have been garbage collected, call mark_hook on this
13733 section if any of its linked-to sections is marked. */
13734 asection
*linked_to_sec
;
13735 for (linked_to_sec
= elf_linked_to_section (isec
);
13736 linked_to_sec
!= NULL
&& !linked_to_sec
->linker_mark
;
13737 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13739 if (linked_to_sec
->gc_mark
)
13741 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
13745 linked_to_sec
->linker_mark
= 1;
13747 for (linked_to_sec
= elf_linked_to_section (isec
);
13748 linked_to_sec
!= NULL
&& linked_to_sec
->linker_mark
;
13749 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13750 linked_to_sec
->linker_mark
= 0;
13753 if (!debug_frag_seen
13754 && (isec
->flags
& SEC_DEBUGGING
)
13755 && startswith (isec
->name
, ".debug_line."))
13756 debug_frag_seen
= true;
13757 else if (strcmp (bfd_section_name (isec
),
13758 "__patchable_function_entries") == 0
13759 && elf_linked_to_section (isec
) == NULL
)
13760 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
13761 "need linked-to section "
13762 "for --gc-sections\n"),
13763 isec
->owner
, isec
);
13766 /* If no non-note alloc section in this file will be kept, then
13767 we can toss out the debug and special sections. */
13771 /* Keep debug and special sections like .comment when they are
13772 not part of a group. Also keep section groups that contain
13773 just debug sections or special sections. NB: Sections with
13774 linked-to section has been handled above. */
13775 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13777 if ((isec
->flags
& SEC_GROUP
) != 0)
13778 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13779 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13780 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13781 && elf_next_in_group (isec
) == NULL
13782 && elf_linked_to_section (isec
) == NULL
)
13784 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13785 has_kept_debug_info
= true;
13788 /* Look for CODE sections which are going to be discarded,
13789 and find and discard any fragmented debug sections which
13790 are associated with that code section. */
13791 if (debug_frag_seen
)
13792 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13793 if ((isec
->flags
& SEC_CODE
) != 0
13794 && isec
->gc_mark
== 0)
13799 ilen
= strlen (isec
->name
);
13801 /* Association is determined by the name of the debug
13802 section containing the name of the code section as
13803 a suffix. For example .debug_line.text.foo is a
13804 debug section associated with .text.foo. */
13805 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13809 if (dsec
->gc_mark
== 0
13810 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13813 dlen
= strlen (dsec
->name
);
13816 && strncmp (dsec
->name
+ (dlen
- ilen
),
13817 isec
->name
, ilen
) == 0)
13822 /* Mark debug sections referenced by kept debug sections. */
13823 if (has_kept_debug_info
)
13824 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13826 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13827 if (!_bfd_elf_gc_mark (info
, isec
,
13828 elf_gc_mark_debug_section
))
13835 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13838 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13840 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13844 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13845 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13846 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13849 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13852 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13854 /* When any section in a section group is kept, we keep all
13855 sections in the section group. If the first member of
13856 the section group is excluded, we will also exclude the
13858 if (o
->flags
& SEC_GROUP
)
13860 asection
*first
= elf_next_in_group (o
);
13861 o
->gc_mark
= first
->gc_mark
;
13867 /* Skip sweeping sections already excluded. */
13868 if (o
->flags
& SEC_EXCLUDE
)
13871 /* Since this is early in the link process, it is simple
13872 to remove a section from the output. */
13873 o
->flags
|= SEC_EXCLUDE
;
13875 if (info
->print_gc_sections
&& o
->size
!= 0)
13876 /* xgettext:c-format */
13877 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13885 /* Propagate collected vtable information. This is called through
13886 elf_link_hash_traverse. */
13889 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13891 /* Those that are not vtables. */
13893 || h
->u2
.vtable
== NULL
13894 || h
->u2
.vtable
->parent
== NULL
)
13897 /* Those vtables that do not have parents, we cannot merge. */
13898 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13901 /* If we've already been done, exit. */
13902 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
13905 /* Make sure the parent's table is up to date. */
13906 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
13908 if (h
->u2
.vtable
->used
== NULL
)
13910 /* None of this table's entries were referenced. Re-use the
13912 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13913 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
13920 /* Or the parent's entries into ours. */
13921 cu
= h
->u2
.vtable
->used
;
13923 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13926 const struct elf_backend_data
*bed
;
13927 unsigned int log_file_align
;
13929 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13930 log_file_align
= bed
->s
->log_file_align
;
13931 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
13945 struct link_info_ok
13947 struct bfd_link_info
*info
;
13952 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
,
13956 bfd_vma hstart
, hend
;
13957 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13958 const struct elf_backend_data
*bed
;
13959 unsigned int log_file_align
;
13960 struct link_info_ok
*info
= (struct link_info_ok
*) ptr
;
13962 /* Take care of both those symbols that do not describe vtables as
13963 well as those that are not loaded. */
13965 || h
->u2
.vtable
== NULL
13966 || h
->u2
.vtable
->parent
== NULL
)
13969 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13970 || h
->root
.type
== bfd_link_hash_defweak
);
13972 sec
= h
->root
.u
.def
.section
;
13973 hstart
= h
->root
.u
.def
.value
;
13974 hend
= hstart
+ h
->size
;
13976 relstart
= _bfd_elf_link_info_read_relocs (sec
->owner
, info
->info
,
13977 sec
, NULL
, NULL
, true);
13979 return info
->ok
= false;
13980 bed
= get_elf_backend_data (sec
->owner
);
13981 log_file_align
= bed
->s
->log_file_align
;
13983 relend
= relstart
+ sec
->reloc_count
;
13985 for (rel
= relstart
; rel
< relend
; ++rel
)
13986 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13988 /* If the entry is in use, do nothing. */
13989 if (h
->u2
.vtable
->used
13990 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
13992 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13993 if (h
->u2
.vtable
->used
[entry
])
13996 /* Otherwise, kill it. */
13997 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
14003 /* Mark sections containing dynamically referenced symbols. When
14004 building shared libraries, we must assume that any visible symbol is
14008 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
14010 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
14011 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
14013 if ((h
->root
.type
== bfd_link_hash_defined
14014 || h
->root
.type
== bfd_link_hash_defweak
)
14016 || h
->root
.ldscript_def
14017 || !info
->start_stop_gc
)
14018 && ((h
->ref_dynamic
&& !h
->forced_local
)
14019 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
14020 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
14021 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
14022 && (!bfd_link_executable (info
)
14023 || info
->gc_keep_exported
14024 || info
->export_dynamic
14027 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
14028 && (h
->versioned
>= versioned
14029 || !bfd_hide_sym_by_version (info
->version_info
,
14030 h
->root
.root
.string
)))))
14031 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14036 /* Keep all sections containing symbols undefined on the command-line,
14037 and the section containing the entry symbol. */
14040 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
14042 struct bfd_sym_chain
*sym
;
14044 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
14046 struct elf_link_hash_entry
*h
;
14048 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
14049 false, false, false);
14052 && (h
->root
.type
== bfd_link_hash_defined
14053 || h
->root
.type
== bfd_link_hash_defweak
)
14054 && !bfd_is_const_section (h
->root
.u
.def
.section
))
14055 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14060 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
14061 struct bfd_link_info
*info
)
14063 bfd
*ibfd
= info
->input_bfds
;
14065 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14068 struct elf_reloc_cookie cookie
;
14070 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14072 sec
= ibfd
->sections
;
14073 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14076 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
14079 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
14081 if (startswith (bfd_section_name (sec
), ".eh_frame_entry")
14082 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
14084 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
14085 fini_reloc_cookie_rels (&cookie
, sec
);
14092 /* Do mark and sweep of unused sections. */
14095 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14099 elf_gc_mark_hook_fn gc_mark_hook
;
14100 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14101 struct elf_link_hash_table
*htab
;
14102 struct link_info_ok info_ok
;
14104 if (!bed
->can_gc_sections
14105 || !is_elf_hash_table (info
->hash
))
14107 _bfd_error_handler(_("warning: gc-sections option ignored"));
14111 bed
->gc_keep (info
);
14112 htab
= elf_hash_table (info
);
14114 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14115 at the .eh_frame section if we can mark the FDEs individually. */
14116 for (sub
= info
->input_bfds
;
14117 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14118 sub
= sub
->link
.next
)
14121 struct elf_reloc_cookie cookie
;
14123 sec
= sub
->sections
;
14124 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14126 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14127 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
14129 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14130 if (elf_section_data (sec
)->sec_info
14131 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14132 elf_eh_frame_section (sub
) = sec
;
14133 fini_reloc_cookie_for_section (&cookie
, sec
);
14134 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14138 /* Apply transitive closure to the vtable entry usage info. */
14139 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14143 /* Kill the vtable relocations that were not used. */
14144 info_ok
.info
= info
;
14146 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &info_ok
);
14150 /* Mark dynamically referenced symbols. */
14151 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14152 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14154 /* Grovel through relocs to find out who stays ... */
14155 gc_mark_hook
= bed
->gc_mark_hook
;
14156 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14160 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14161 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14162 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14166 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14169 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14170 Also treat note sections as a root, if the section is not part
14171 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14172 well as FINI_ARRAY sections for ld -r. */
14173 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14175 && (o
->flags
& SEC_EXCLUDE
) == 0
14176 && ((o
->flags
& SEC_KEEP
) != 0
14177 || (bfd_link_relocatable (info
)
14178 && ((elf_section_data (o
)->this_hdr
.sh_type
14179 == SHT_PREINIT_ARRAY
)
14180 || (elf_section_data (o
)->this_hdr
.sh_type
14182 || (elf_section_data (o
)->this_hdr
.sh_type
14183 == SHT_FINI_ARRAY
)))
14184 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14185 && elf_next_in_group (o
) == NULL
14186 && elf_linked_to_section (o
) == NULL
)
14187 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14188 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14190 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14195 /* Allow the backend to mark additional target specific sections. */
14196 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14198 /* ... and mark SEC_EXCLUDE for those that go. */
14199 return elf_gc_sweep (abfd
, info
);
14202 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14205 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14207 struct elf_link_hash_entry
*h
,
14210 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14211 struct elf_link_hash_entry
**search
, *child
;
14212 size_t extsymcount
;
14213 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14215 /* The sh_info field of the symtab header tells us where the
14216 external symbols start. We don't care about the local symbols at
14218 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14219 if (!elf_bad_symtab (abfd
))
14220 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14222 sym_hashes
= elf_sym_hashes (abfd
);
14223 sym_hashes_end
= PTR_ADD (sym_hashes
, extsymcount
);
14225 /* Hunt down the child symbol, which is in this section at the same
14226 offset as the relocation. */
14227 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14229 if ((child
= *search
) != NULL
14230 && (child
->root
.type
== bfd_link_hash_defined
14231 || child
->root
.type
== bfd_link_hash_defweak
)
14232 && child
->root
.u
.def
.section
== sec
14233 && child
->root
.u
.def
.value
== offset
)
14237 /* xgettext:c-format */
14238 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14239 abfd
, sec
, (uint64_t) offset
);
14240 bfd_set_error (bfd_error_invalid_operation
);
14244 if (!child
->u2
.vtable
)
14246 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14247 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14248 if (!child
->u2
.vtable
)
14253 /* This *should* only be the absolute section. It could potentially
14254 be that someone has defined a non-global vtable though, which
14255 would be bad. It isn't worth paging in the local symbols to be
14256 sure though; that case should simply be handled by the assembler. */
14258 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14261 child
->u2
.vtable
->parent
= h
;
14266 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14269 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14270 struct elf_link_hash_entry
*h
,
14273 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14274 unsigned int log_file_align
= bed
->s
->log_file_align
;
14278 /* xgettext:c-format */
14279 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14281 bfd_set_error (bfd_error_bad_value
);
14287 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14288 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14293 if (addend
>= h
->u2
.vtable
->size
)
14295 size_t size
, bytes
, file_align
;
14296 bool *ptr
= h
->u2
.vtable
->used
;
14298 /* While the symbol is undefined, we have to be prepared to handle
14300 file_align
= 1 << log_file_align
;
14301 if (h
->root
.type
== bfd_link_hash_undefined
)
14302 size
= addend
+ file_align
;
14306 if (addend
>= size
)
14308 /* Oops! We've got a reference past the defined end of
14309 the table. This is probably a bug -- shall we warn? */
14310 size
= addend
+ file_align
;
14313 size
= (size
+ file_align
- 1) & -file_align
;
14315 /* Allocate one extra entry for use as a "done" flag for the
14316 consolidation pass. */
14317 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bool);
14321 ptr
= (bool *) bfd_realloc (ptr
- 1, bytes
);
14327 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14329 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14333 ptr
= (bool *) bfd_zmalloc (bytes
);
14338 /* And arrange for that done flag to be at index -1. */
14339 h
->u2
.vtable
->used
= ptr
+ 1;
14340 h
->u2
.vtable
->size
= size
;
14343 h
->u2
.vtable
->used
[addend
>> log_file_align
] = true;
14348 /* Map an ELF section header flag to its corresponding string. */
14352 flagword flag_value
;
14353 } elf_flags_to_name_table
;
14355 static const elf_flags_to_name_table elf_flags_to_names
[] =
14357 { "SHF_WRITE", SHF_WRITE
},
14358 { "SHF_ALLOC", SHF_ALLOC
},
14359 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14360 { "SHF_MERGE", SHF_MERGE
},
14361 { "SHF_STRINGS", SHF_STRINGS
},
14362 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14363 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14364 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14365 { "SHF_GROUP", SHF_GROUP
},
14366 { "SHF_TLS", SHF_TLS
},
14367 { "SHF_MASKOS", SHF_MASKOS
},
14368 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14371 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14373 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14374 struct flag_info
*flaginfo
,
14377 const bfd_vma sh_flags
= elf_section_flags (section
);
14379 if (!flaginfo
->flags_initialized
)
14381 bfd
*obfd
= info
->output_bfd
;
14382 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14383 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14385 int without_hex
= 0;
14387 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14390 flagword (*lookup
) (char *);
14392 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14393 if (lookup
!= NULL
)
14395 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14399 if (tf
->with
== with_flags
)
14400 with_hex
|= hexval
;
14401 else if (tf
->with
== without_flags
)
14402 without_hex
|= hexval
;
14407 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14409 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14411 if (tf
->with
== with_flags
)
14412 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14413 else if (tf
->with
== without_flags
)
14414 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14421 info
->callbacks
->einfo
14422 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14426 flaginfo
->flags_initialized
= true;
14427 flaginfo
->only_with_flags
|= with_hex
;
14428 flaginfo
->not_with_flags
|= without_hex
;
14431 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14434 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14440 struct alloc_got_off_arg
{
14442 struct bfd_link_info
*info
;
14445 /* We need a special top-level link routine to convert got reference counts
14446 to real got offsets. */
14449 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14451 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14452 bfd
*obfd
= gofarg
->info
->output_bfd
;
14453 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14455 if (h
->got
.refcount
> 0)
14457 h
->got
.offset
= gofarg
->gotoff
;
14458 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14461 h
->got
.offset
= (bfd_vma
) -1;
14466 /* And an accompanying bit to work out final got entry offsets once
14467 we're done. Should be called from final_link. */
14470 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14471 struct bfd_link_info
*info
)
14474 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14476 struct alloc_got_off_arg gofarg
;
14478 BFD_ASSERT (abfd
== info
->output_bfd
);
14480 if (! is_elf_hash_table (info
->hash
))
14483 /* The GOT offset is relative to the .got section, but the GOT header is
14484 put into the .got.plt section, if the backend uses it. */
14485 if (bed
->want_got_plt
)
14488 gotoff
= bed
->got_header_size
;
14490 /* Do the local .got entries first. */
14491 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14493 bfd_signed_vma
*local_got
;
14494 size_t j
, locsymcount
;
14495 Elf_Internal_Shdr
*symtab_hdr
;
14497 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14500 local_got
= elf_local_got_refcounts (i
);
14504 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14505 if (elf_bad_symtab (i
))
14506 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14508 locsymcount
= symtab_hdr
->sh_info
;
14510 for (j
= 0; j
< locsymcount
; ++j
)
14512 if (local_got
[j
] > 0)
14514 local_got
[j
] = gotoff
;
14515 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14518 local_got
[j
] = (bfd_vma
) -1;
14522 /* Then the global .got entries. .plt refcounts are handled by
14523 adjust_dynamic_symbol */
14524 gofarg
.gotoff
= gotoff
;
14525 gofarg
.info
= info
;
14526 elf_link_hash_traverse (elf_hash_table (info
),
14527 elf_gc_allocate_got_offsets
,
14532 /* Many folk need no more in the way of final link than this, once
14533 got entry reference counting is enabled. */
14536 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14538 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14541 /* Invoke the regular ELF backend linker to do all the work. */
14542 return bfd_elf_final_link (abfd
, info
);
14546 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14548 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14550 if (rcookie
->bad_symtab
)
14551 rcookie
->rel
= rcookie
->rels
;
14553 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14555 unsigned long r_symndx
;
14557 if (! rcookie
->bad_symtab
)
14558 if (rcookie
->rel
->r_offset
> offset
)
14560 if (rcookie
->rel
->r_offset
!= offset
)
14563 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14564 if (r_symndx
== STN_UNDEF
)
14567 if (r_symndx
>= rcookie
->locsymcount
14568 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14570 struct elf_link_hash_entry
*h
;
14572 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14574 while (h
->root
.type
== bfd_link_hash_indirect
14575 || h
->root
.type
== bfd_link_hash_warning
)
14576 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14578 if ((h
->root
.type
== bfd_link_hash_defined
14579 || h
->root
.type
== bfd_link_hash_defweak
)
14580 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14581 || h
->root
.u
.def
.section
->kept_section
!= NULL
14582 || discarded_section (h
->root
.u
.def
.section
)))
14587 /* It's not a relocation against a global symbol,
14588 but it could be a relocation against a local
14589 symbol for a discarded section. */
14591 Elf_Internal_Sym
*isym
;
14593 /* Need to: get the symbol; get the section. */
14594 isym
= &rcookie
->locsyms
[r_symndx
];
14595 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14597 && (isec
->kept_section
!= NULL
14598 || discarded_section (isec
)))
14606 /* Discard unneeded references to discarded sections.
14607 Returns -1 on error, 1 if any section's size was changed, 0 if
14608 nothing changed. This function assumes that the relocations are in
14609 sorted order, which is true for all known assemblers. */
14612 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14614 struct elf_reloc_cookie cookie
;
14619 if (info
->traditional_format
14620 || !is_elf_hash_table (info
->hash
))
14623 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14628 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14631 || i
->reloc_count
== 0
14632 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14636 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14639 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14642 if (_bfd_discard_section_stabs (abfd
, i
,
14643 elf_section_data (i
)->sec_info
,
14644 bfd_elf_reloc_symbol_deleted_p
,
14648 fini_reloc_cookie_for_section (&cookie
, i
);
14653 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14654 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14658 int eh_changed
= 0;
14659 unsigned int eh_alignment
; /* Octets. */
14661 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14667 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14670 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14673 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14674 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14675 bfd_elf_reloc_symbol_deleted_p
,
14679 if (i
->size
!= i
->rawsize
)
14683 fini_reloc_cookie_for_section (&cookie
, i
);
14686 eh_alignment
= ((1 << o
->alignment_power
)
14687 * bfd_octets_per_byte (output_bfd
, o
));
14688 /* Skip over zero terminator, and prevent empty sections from
14689 adding alignment padding at the end. */
14690 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14692 i
->flags
|= SEC_EXCLUDE
;
14693 else if (i
->size
> 4)
14695 /* The last non-empty eh_frame section doesn't need padding. */
14698 /* Any prior sections must pad the last FDE out to the output
14699 section alignment. Otherwise we might have zero padding
14700 between sections, which would be seen as a terminator. */
14701 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14703 /* All but the last zero terminator should have been removed. */
14708 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14709 if (i
->size
!= size
)
14717 elf_link_hash_traverse (elf_hash_table (info
),
14718 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14721 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14723 const struct elf_backend_data
*bed
;
14726 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14728 s
= abfd
->sections
;
14729 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14732 bed
= get_elf_backend_data (abfd
);
14734 if (bed
->elf_backend_discard_info
!= NULL
)
14736 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14739 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14742 fini_reloc_cookie (&cookie
, abfd
);
14746 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14747 _bfd_elf_end_eh_frame_parsing (info
);
14749 if (info
->eh_frame_hdr_type
14750 && !bfd_link_relocatable (info
)
14751 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
14758 _bfd_elf_section_already_linked (bfd
*abfd
,
14760 struct bfd_link_info
*info
)
14763 const char *name
, *key
;
14764 struct bfd_section_already_linked
*l
;
14765 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14767 if (sec
->output_section
== bfd_abs_section_ptr
)
14770 flags
= sec
->flags
;
14772 /* Return if it isn't a linkonce section. A comdat group section
14773 also has SEC_LINK_ONCE set. */
14774 if ((flags
& SEC_LINK_ONCE
) == 0)
14777 /* Don't put group member sections on our list of already linked
14778 sections. They are handled as a group via their group section. */
14779 if (elf_sec_group (sec
) != NULL
)
14782 /* For a SHT_GROUP section, use the group signature as the key. */
14784 if ((flags
& SEC_GROUP
) != 0
14785 && elf_next_in_group (sec
) != NULL
14786 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14787 key
= elf_group_name (elf_next_in_group (sec
));
14790 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14791 if (startswith (name
, ".gnu.linkonce.")
14792 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14795 /* Must be a user linkonce section that doesn't follow gcc's
14796 naming convention. In this case we won't be matching
14797 single member groups. */
14801 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14803 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14805 /* We may have 2 different types of sections on the list: group
14806 sections with a signature of <key> (<key> is some string),
14807 and linkonce sections named .gnu.linkonce.<type>.<key>.
14808 Match like sections. LTO plugin sections are an exception.
14809 They are always named .gnu.linkonce.t.<key> and match either
14810 type of section. */
14811 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14812 && ((flags
& SEC_GROUP
) != 0
14813 || strcmp (name
, l
->sec
->name
) == 0))
14814 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
14815 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14817 /* The section has already been linked. See if we should
14818 issue a warning. */
14819 if (!_bfd_handle_already_linked (sec
, l
, info
))
14822 if (flags
& SEC_GROUP
)
14824 asection
*first
= elf_next_in_group (sec
);
14825 asection
*s
= first
;
14829 s
->output_section
= bfd_abs_section_ptr
;
14830 /* Record which group discards it. */
14831 s
->kept_section
= l
->sec
;
14832 s
= elf_next_in_group (s
);
14833 /* These lists are circular. */
14843 /* A single member comdat group section may be discarded by a
14844 linkonce section and vice versa. */
14845 if ((flags
& SEC_GROUP
) != 0)
14847 asection
*first
= elf_next_in_group (sec
);
14849 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14850 /* Check this single member group against linkonce sections. */
14851 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14852 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14853 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14855 first
->output_section
= bfd_abs_section_ptr
;
14856 first
->kept_section
= l
->sec
;
14857 sec
->output_section
= bfd_abs_section_ptr
;
14862 /* Check this linkonce section against single member groups. */
14863 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14864 if (l
->sec
->flags
& SEC_GROUP
)
14866 asection
*first
= elf_next_in_group (l
->sec
);
14869 && elf_next_in_group (first
) == first
14870 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
14872 sec
->output_section
= bfd_abs_section_ptr
;
14873 sec
->kept_section
= first
;
14878 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14879 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14880 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14881 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14882 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14883 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14884 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14885 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14886 The reverse order cannot happen as there is never a bfd with only the
14887 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14888 matter as here were are looking only for cross-bfd sections. */
14890 if ((flags
& SEC_GROUP
) == 0 && startswith (name
, ".gnu.linkonce.r."))
14891 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14892 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14893 && startswith (l
->sec
->name
, ".gnu.linkonce.t."))
14895 if (abfd
!= l
->sec
->owner
)
14896 sec
->output_section
= bfd_abs_section_ptr
;
14900 /* This is the first section with this name. Record it. */
14901 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
14902 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
14903 return sec
->output_section
== bfd_abs_section_ptr
;
14907 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
14909 return sym
->st_shndx
== SHN_COMMON
;
14913 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14919 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14921 return bfd_com_section_ptr
;
14925 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14926 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14927 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14928 bfd
*ibfd ATTRIBUTE_UNUSED
,
14929 unsigned long symndx ATTRIBUTE_UNUSED
)
14931 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14932 return bed
->s
->arch_size
/ 8;
14935 /* Routines to support the creation of dynamic relocs. */
14937 /* Returns the name of the dynamic reloc section associated with SEC. */
14939 static const char *
14940 get_dynamic_reloc_section_name (bfd
* abfd
,
14945 const char *old_name
= bfd_section_name (sec
);
14946 const char *prefix
= is_rela
? ".rela" : ".rel";
14948 if (old_name
== NULL
)
14951 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14952 sprintf (name
, "%s%s", prefix
, old_name
);
14957 /* Returns the dynamic reloc section associated with SEC.
14958 If necessary compute the name of the dynamic reloc section based
14959 on SEC's name (looked up in ABFD's string table) and the setting
14963 _bfd_elf_get_dynamic_reloc_section (bfd
*abfd
,
14967 asection
*reloc_sec
= elf_section_data (sec
)->sreloc
;
14969 if (reloc_sec
== NULL
)
14971 const char *name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14975 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14977 if (reloc_sec
!= NULL
)
14978 elf_section_data (sec
)->sreloc
= reloc_sec
;
14985 /* Returns the dynamic reloc section associated with SEC. If the
14986 section does not exist it is created and attached to the DYNOBJ
14987 bfd and stored in the SRELOC field of SEC's elf_section_data
14990 ALIGNMENT is the alignment for the newly created section and
14991 IS_RELA defines whether the name should be .rela.<SEC's name>
14992 or .rel.<SEC's name>. The section name is looked up in the
14993 string table associated with ABFD. */
14996 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
14998 unsigned int alignment
,
15002 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
15004 if (reloc_sec
== NULL
)
15006 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15011 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
15013 if (reloc_sec
== NULL
)
15015 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
15016 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
15017 if ((sec
->flags
& SEC_ALLOC
) != 0)
15018 flags
|= SEC_ALLOC
| SEC_LOAD
;
15020 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
15021 if (reloc_sec
!= NULL
)
15023 /* _bfd_elf_get_sec_type_attr chooses a section type by
15024 name. Override as it may be wrong, eg. for a user
15025 section named "auto" we'll get ".relauto" which is
15026 seen to be a .rela section. */
15027 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
15028 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
15033 elf_section_data (sec
)->sreloc
= reloc_sec
;
15039 /* Copy the ELF symbol type and other attributes for a linker script
15040 assignment from HSRC to HDEST. Generally this should be treated as
15041 if we found a strong non-dynamic definition for HDEST (except that
15042 ld ignores multiple definition errors). */
15044 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
15045 struct bfd_link_hash_entry
*hdest
,
15046 struct bfd_link_hash_entry
*hsrc
)
15048 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
15049 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
15050 Elf_Internal_Sym isym
;
15052 ehdest
->type
= ehsrc
->type
;
15053 ehdest
->target_internal
= ehsrc
->target_internal
;
15055 isym
.st_other
= ehsrc
->other
;
15056 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, true, false);
15059 /* Append a RELA relocation REL to section S in BFD. */
15062 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15064 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15065 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
15066 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
15067 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
15070 /* Append a REL relocation REL to section S in BFD. */
15073 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15075 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15076 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
15077 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
15078 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
15081 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15083 struct bfd_link_hash_entry
*
15084 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
15085 const char *symbol
, asection
*sec
)
15087 struct elf_link_hash_entry
*h
;
15089 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
15090 false, false, true);
15091 /* NB: Common symbols will be turned into definition later. */
15093 && !h
->root
.ldscript_def
15094 && (h
->root
.type
== bfd_link_hash_undefined
15095 || h
->root
.type
== bfd_link_hash_undefweak
15096 || ((h
->ref_regular
|| h
->def_dynamic
)
15098 && h
->root
.type
!= bfd_link_hash_common
)))
15100 bool was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15101 h
->verinfo
.verdef
= NULL
;
15102 h
->root
.type
= bfd_link_hash_defined
;
15103 h
->root
.u
.def
.section
= sec
;
15104 h
->root
.u
.def
.value
= 0;
15105 h
->def_regular
= 1;
15106 h
->def_dynamic
= 0;
15108 h
->u2
.start_stop_section
= sec
;
15109 if (symbol
[0] == '.')
15111 /* .startof. and .sizeof. symbols are local. */
15112 const struct elf_backend_data
*bed
;
15113 bed
= get_elf_backend_data (info
->output_bfd
);
15114 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
15118 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15119 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15120 | info
->start_stop_visibility
);
15122 bfd_elf_link_record_dynamic_symbol (info
, h
);
15129 /* Find dynamic relocs for H that apply to read-only sections. */
15132 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15134 struct elf_dyn_relocs
*p
;
15136 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15138 asection
*s
= p
->sec
->output_section
;
15140 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15146 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15147 read-only sections. */
15150 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15154 if (h
->root
.type
== bfd_link_hash_indirect
)
15157 sec
= _bfd_elf_readonly_dynrelocs (h
);
15160 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15162 info
->flags
|= DF_TEXTREL
;
15163 /* xgettext:c-format */
15164 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15165 "in read-only section `%pA'\n"),
15166 sec
->owner
, h
->root
.root
.string
, sec
);
15168 if (bfd_link_textrel_check (info
))
15169 /* xgettext:c-format */
15170 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15171 "in read-only section `%pA'\n"),
15172 sec
->owner
, h
->root
.root
.string
, sec
);
15174 /* Not an error, just cut short the traversal. */
15180 /* Add dynamic tags. */
15183 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15184 bool need_dynamic_reloc
)
15186 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15188 if (htab
->dynamic_sections_created
)
15190 /* Add some entries to the .dynamic section. We fill in the
15191 values later, in finish_dynamic_sections, but we must add
15192 the entries now so that we get the correct size for the
15193 .dynamic section. The DT_DEBUG entry is filled in by the
15194 dynamic linker and used by the debugger. */
15195 #define add_dynamic_entry(TAG, VAL) \
15196 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15198 const struct elf_backend_data
*bed
15199 = get_elf_backend_data (output_bfd
);
15201 if (bfd_link_executable (info
))
15203 if (!add_dynamic_entry (DT_DEBUG
, 0))
15207 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15209 /* DT_PLTGOT is used by prelink even if there is no PLT
15211 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15215 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15217 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15218 || !add_dynamic_entry (DT_PLTREL
,
15219 (bed
->rela_plts_and_copies_p
15220 ? DT_RELA
: DT_REL
))
15221 || !add_dynamic_entry (DT_JMPREL
, 0))
15225 if (htab
->tlsdesc_plt
15226 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15227 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15230 if (need_dynamic_reloc
)
15232 if (bed
->rela_plts_and_copies_p
)
15234 if (!add_dynamic_entry (DT_RELA
, 0)
15235 || !add_dynamic_entry (DT_RELASZ
, 0)
15236 || !add_dynamic_entry (DT_RELAENT
,
15237 bed
->s
->sizeof_rela
))
15242 if (!add_dynamic_entry (DT_REL
, 0)
15243 || !add_dynamic_entry (DT_RELSZ
, 0)
15244 || !add_dynamic_entry (DT_RELENT
,
15245 bed
->s
->sizeof_rel
))
15249 /* If any dynamic relocs apply to a read-only section,
15250 then we need a DT_TEXTREL entry. */
15251 if ((info
->flags
& DF_TEXTREL
) == 0)
15252 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15255 if ((info
->flags
& DF_TEXTREL
) != 0)
15257 if (htab
->ifunc_resolvers
)
15258 info
->callbacks
->einfo
15259 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15260 "may result in a segfault at runtime; recompile with %s\n"),
15261 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15263 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15268 #undef add_dynamic_entry