1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2021 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
42 /* This struct is used to pass information to routines called via
43 elf_link_hash_traverse which must return failure. */
45 struct elf_info_failed
47 struct bfd_link_info
*info
;
51 /* This structure is used to pass information to
52 _bfd_elf_link_find_version_dependencies. */
54 struct elf_find_verdep_info
56 /* General link information. */
57 struct bfd_link_info
*info
;
58 /* The number of dependencies. */
60 /* Whether we had a failure. */
64 static bfd_boolean _bfd_elf_fix_symbol_flags
65 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
68 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
69 unsigned long r_symndx
,
72 if (r_symndx
>= cookie
->locsymcount
73 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
75 struct elf_link_hash_entry
*h
;
77 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
79 while (h
->root
.type
== bfd_link_hash_indirect
80 || h
->root
.type
== bfd_link_hash_warning
)
81 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
83 if ((h
->root
.type
== bfd_link_hash_defined
84 || h
->root
.type
== bfd_link_hash_defweak
)
85 && discarded_section (h
->root
.u
.def
.section
))
86 return h
->root
.u
.def
.section
;
92 /* It's not a relocation against a global symbol,
93 but it could be a relocation against a local
94 symbol for a discarded section. */
96 Elf_Internal_Sym
*isym
;
98 /* Need to: get the symbol; get the section. */
99 isym
= &cookie
->locsyms
[r_symndx
];
100 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
102 && discard
? discarded_section (isec
) : 1)
108 /* Define a symbol in a dynamic linkage section. */
110 struct elf_link_hash_entry
*
111 _bfd_elf_define_linkage_sym (bfd
*abfd
,
112 struct bfd_link_info
*info
,
116 struct elf_link_hash_entry
*h
;
117 struct bfd_link_hash_entry
*bh
;
118 const struct elf_backend_data
*bed
;
120 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
123 /* Zap symbol defined in an as-needed lib that wasn't linked.
124 This is a symptom of a larger problem: Absolute symbols
125 defined in shared libraries can't be overridden, because we
126 lose the link to the bfd which is via the symbol section. */
127 h
->root
.type
= bfd_link_hash_new
;
133 bed
= get_elf_backend_data (abfd
);
134 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
135 sec
, 0, NULL
, FALSE
, bed
->collect
,
138 h
= (struct elf_link_hash_entry
*) bh
;
139 BFD_ASSERT (h
!= NULL
);
142 h
->root
.linker_def
= 1;
143 h
->type
= STT_OBJECT
;
144 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
145 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
147 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
152 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
156 struct elf_link_hash_entry
*h
;
157 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
158 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
160 /* This function may be called more than once. */
161 if (htab
->sgot
!= NULL
)
164 flags
= bed
->dynamic_sec_flags
;
166 s
= bfd_make_section_anyway_with_flags (abfd
,
167 (bed
->rela_plts_and_copies_p
168 ? ".rela.got" : ".rel.got"),
169 (bed
->dynamic_sec_flags
172 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
176 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
178 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
182 if (bed
->want_got_plt
)
184 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
186 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
191 /* The first bit of the global offset table is the header. */
192 s
->size
+= bed
->got_header_size
;
194 if (bed
->want_got_sym
)
196 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
197 (or .got.plt) section. We don't do this in the linker script
198 because we don't want to define the symbol if we are not creating
199 a global offset table. */
200 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
201 "_GLOBAL_OFFSET_TABLE_");
202 elf_hash_table (info
)->hgot
= h
;
210 /* Create a strtab to hold the dynamic symbol names. */
212 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
214 struct elf_link_hash_table
*hash_table
;
216 hash_table
= elf_hash_table (info
);
217 if (hash_table
->dynobj
== NULL
)
219 /* We may not set dynobj, an input file holding linker created
220 dynamic sections to abfd, which may be a dynamic object with
221 its own dynamic sections. We need to find a normal input file
222 to hold linker created sections if possible. */
223 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
227 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
229 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0
230 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
231 && elf_object_id (ibfd
) == elf_hash_table_id (hash_table
)
232 && !((s
= ibfd
->sections
) != NULL
233 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
))
239 hash_table
->dynobj
= abfd
;
242 if (hash_table
->dynstr
== NULL
)
244 hash_table
->dynstr
= _bfd_elf_strtab_init ();
245 if (hash_table
->dynstr
== NULL
)
251 /* Create some sections which will be filled in with dynamic linking
252 information. ABFD is an input file which requires dynamic sections
253 to be created. The dynamic sections take up virtual memory space
254 when the final executable is run, so we need to create them before
255 addresses are assigned to the output sections. We work out the
256 actual contents and size of these sections later. */
259 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
263 const struct elf_backend_data
*bed
;
264 struct elf_link_hash_entry
*h
;
266 if (! is_elf_hash_table (info
->hash
))
269 if (elf_hash_table (info
)->dynamic_sections_created
)
272 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
275 abfd
= elf_hash_table (info
)->dynobj
;
276 bed
= get_elf_backend_data (abfd
);
278 flags
= bed
->dynamic_sec_flags
;
280 /* A dynamically linked executable has a .interp section, but a
281 shared library does not. */
282 if (bfd_link_executable (info
) && !info
->nointerp
)
284 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
285 flags
| SEC_READONLY
);
290 /* Create sections to hold version informations. These are removed
291 if they are not needed. */
292 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
293 flags
| SEC_READONLY
);
295 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
298 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
299 flags
| SEC_READONLY
);
301 || !bfd_set_section_alignment (s
, 1))
304 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
305 flags
| SEC_READONLY
);
307 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
310 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
311 flags
| SEC_READONLY
);
313 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
315 elf_hash_table (info
)->dynsym
= s
;
317 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
318 flags
| SEC_READONLY
);
322 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
324 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
327 /* The special symbol _DYNAMIC is always set to the start of the
328 .dynamic section. We could set _DYNAMIC in a linker script, but we
329 only want to define it if we are, in fact, creating a .dynamic
330 section. We don't want to define it if there is no .dynamic
331 section, since on some ELF platforms the start up code examines it
332 to decide how to initialize the process. */
333 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
334 elf_hash_table (info
)->hdynamic
= h
;
340 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
341 flags
| SEC_READONLY
);
343 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
345 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
348 if (info
->emit_gnu_hash
&& bed
->record_xhash_symbol
== NULL
)
350 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
351 flags
| SEC_READONLY
);
353 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
355 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
356 4 32-bit words followed by variable count of 64-bit words, then
357 variable count of 32-bit words. */
358 if (bed
->s
->arch_size
== 64)
359 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
361 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
364 /* Let the backend create the rest of the sections. This lets the
365 backend set the right flags. The backend will normally create
366 the .got and .plt sections. */
367 if (bed
->elf_backend_create_dynamic_sections
== NULL
368 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
371 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
376 /* Create dynamic sections when linking against a dynamic object. */
379 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
381 flagword flags
, pltflags
;
382 struct elf_link_hash_entry
*h
;
384 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
385 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
387 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
388 .rel[a].bss sections. */
389 flags
= bed
->dynamic_sec_flags
;
392 if (bed
->plt_not_loaded
)
393 /* We do not clear SEC_ALLOC here because we still want the OS to
394 allocate space for the section; it's just that there's nothing
395 to read in from the object file. */
396 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
398 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
399 if (bed
->plt_readonly
)
400 pltflags
|= SEC_READONLY
;
402 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
404 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
408 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
410 if (bed
->want_plt_sym
)
412 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
413 "_PROCEDURE_LINKAGE_TABLE_");
414 elf_hash_table (info
)->hplt
= h
;
419 s
= bfd_make_section_anyway_with_flags (abfd
,
420 (bed
->rela_plts_and_copies_p
421 ? ".rela.plt" : ".rel.plt"),
422 flags
| SEC_READONLY
);
424 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
428 if (! _bfd_elf_create_got_section (abfd
, info
))
431 if (bed
->want_dynbss
)
433 /* The .dynbss section is a place to put symbols which are defined
434 by dynamic objects, are referenced by regular objects, and are
435 not functions. We must allocate space for them in the process
436 image and use a R_*_COPY reloc to tell the dynamic linker to
437 initialize them at run time. The linker script puts the .dynbss
438 section into the .bss section of the final image. */
439 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
440 SEC_ALLOC
| SEC_LINKER_CREATED
);
445 if (bed
->want_dynrelro
)
447 /* Similarly, but for symbols that were originally in read-only
448 sections. This section doesn't really need to have contents,
449 but make it like other .data.rel.ro sections. */
450 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
457 /* The .rel[a].bss section holds copy relocs. This section is not
458 normally needed. We need to create it here, though, so that the
459 linker will map it to an output section. We can't just create it
460 only if we need it, because we will not know whether we need it
461 until we have seen all the input files, and the first time the
462 main linker code calls BFD after examining all the input files
463 (size_dynamic_sections) the input sections have already been
464 mapped to the output sections. If the section turns out not to
465 be needed, we can discard it later. We will never need this
466 section when generating a shared object, since they do not use
468 if (bfd_link_executable (info
))
470 s
= bfd_make_section_anyway_with_flags (abfd
,
471 (bed
->rela_plts_and_copies_p
472 ? ".rela.bss" : ".rel.bss"),
473 flags
| SEC_READONLY
);
475 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
479 if (bed
->want_dynrelro
)
481 s
= (bfd_make_section_anyway_with_flags
482 (abfd
, (bed
->rela_plts_and_copies_p
483 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
484 flags
| SEC_READONLY
));
486 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
488 htab
->sreldynrelro
= s
;
496 /* Record a new dynamic symbol. We record the dynamic symbols as we
497 read the input files, since we need to have a list of all of them
498 before we can determine the final sizes of the output sections.
499 Note that we may actually call this function even though we are not
500 going to output any dynamic symbols; in some cases we know that a
501 symbol should be in the dynamic symbol table, but only if there is
505 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
506 struct elf_link_hash_entry
*h
)
508 if (h
->dynindx
== -1)
510 struct elf_strtab_hash
*dynstr
;
515 if (h
->root
.type
== bfd_link_hash_defined
516 || h
->root
.type
== bfd_link_hash_defweak
)
518 /* An IR symbol should not be made dynamic. */
519 if (h
->root
.u
.def
.section
!= NULL
520 && h
->root
.u
.def
.section
->owner
!= NULL
521 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
525 /* XXX: The ABI draft says the linker must turn hidden and
526 internal symbols into STB_LOCAL symbols when producing the
527 DSO. However, if ld.so honors st_other in the dynamic table,
528 this would not be necessary. */
529 switch (ELF_ST_VISIBILITY (h
->other
))
533 if (h
->root
.type
!= bfd_link_hash_undefined
534 && h
->root
.type
!= bfd_link_hash_undefweak
)
537 if (!elf_hash_table (info
)->is_relocatable_executable
538 || ((h
->root
.type
== bfd_link_hash_defined
539 || h
->root
.type
== bfd_link_hash_defweak
)
540 && h
->root
.u
.def
.section
->owner
!= NULL
541 && h
->root
.u
.def
.section
->owner
->no_export
)
542 || (h
->root
.type
== bfd_link_hash_common
543 && h
->root
.u
.c
.p
->section
->owner
!= NULL
544 && h
->root
.u
.c
.p
->section
->owner
->no_export
))
552 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
553 ++elf_hash_table (info
)->dynsymcount
;
555 dynstr
= elf_hash_table (info
)->dynstr
;
558 /* Create a strtab to hold the dynamic symbol names. */
559 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
564 /* We don't put any version information in the dynamic string
566 name
= h
->root
.root
.string
;
567 p
= strchr (name
, ELF_VER_CHR
);
569 /* We know that the p points into writable memory. In fact,
570 there are only a few symbols that have read-only names, being
571 those like _GLOBAL_OFFSET_TABLE_ that are created specially
572 by the backends. Most symbols will have names pointing into
573 an ELF string table read from a file, or to objalloc memory. */
576 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
581 if (indx
== (size_t) -1)
583 h
->dynstr_index
= indx
;
589 /* Mark a symbol dynamic. */
592 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
593 struct elf_link_hash_entry
*h
,
594 Elf_Internal_Sym
*sym
)
596 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
598 /* It may be called more than once on the same H. */
599 if(h
->dynamic
|| bfd_link_relocatable (info
))
602 if ((info
->dynamic_data
603 && (h
->type
== STT_OBJECT
604 || h
->type
== STT_COMMON
606 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
607 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
610 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
613 /* NB: If a symbol is made dynamic by --dynamic-list, it has
615 h
->root
.non_ir_ref_dynamic
= 1;
619 /* Record an assignment to a symbol made by a linker script. We need
620 this in case some dynamic object refers to this symbol. */
623 bfd_elf_record_link_assignment (bfd
*output_bfd
,
624 struct bfd_link_info
*info
,
629 struct elf_link_hash_entry
*h
, *hv
;
630 struct elf_link_hash_table
*htab
;
631 const struct elf_backend_data
*bed
;
633 if (!is_elf_hash_table (info
->hash
))
636 htab
= elf_hash_table (info
);
637 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
641 if (h
->root
.type
== bfd_link_hash_warning
)
642 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
644 if (h
->versioned
== unknown
)
646 /* Set versioned if symbol version is unknown. */
647 char *version
= strrchr (name
, ELF_VER_CHR
);
650 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
651 h
->versioned
= versioned_hidden
;
653 h
->versioned
= versioned
;
657 /* Symbols defined in a linker script but not referenced anywhere
658 else will have non_elf set. */
661 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
665 switch (h
->root
.type
)
667 case bfd_link_hash_defined
:
668 case bfd_link_hash_defweak
:
669 case bfd_link_hash_common
:
671 case bfd_link_hash_undefweak
:
672 case bfd_link_hash_undefined
:
673 /* Since we're defining the symbol, don't let it seem to have not
674 been defined. record_dynamic_symbol and size_dynamic_sections
675 may depend on this. */
676 h
->root
.type
= bfd_link_hash_new
;
677 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
678 bfd_link_repair_undef_list (&htab
->root
);
680 case bfd_link_hash_new
:
682 case bfd_link_hash_indirect
:
683 /* We had a versioned symbol in a dynamic library. We make the
684 the versioned symbol point to this one. */
685 bed
= get_elf_backend_data (output_bfd
);
687 while (hv
->root
.type
== bfd_link_hash_indirect
688 || hv
->root
.type
== bfd_link_hash_warning
)
689 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
690 /* We don't need to update h->root.u since linker will set them
692 h
->root
.type
= bfd_link_hash_undefined
;
693 hv
->root
.type
= bfd_link_hash_indirect
;
694 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
695 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
702 /* If this symbol is being provided by the linker script, and it is
703 currently defined by a dynamic object, but not by a regular
704 object, then mark it as undefined so that the generic linker will
705 force the correct value. */
709 h
->root
.type
= bfd_link_hash_undefined
;
711 /* If this symbol is currently defined by a dynamic object, but not
712 by a regular object, then clear out any version information because
713 the symbol will not be associated with the dynamic object any
715 if (h
->def_dynamic
&& !h
->def_regular
)
716 h
->verinfo
.verdef
= NULL
;
718 /* Make sure this symbol is not garbage collected. */
725 bed
= get_elf_backend_data (output_bfd
);
726 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
727 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
728 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
731 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
733 if (!bfd_link_relocatable (info
)
735 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
736 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
741 || bfd_link_dll (info
)
742 || elf_hash_table (info
)->is_relocatable_executable
)
746 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
749 /* If this is a weak defined symbol, and we know a corresponding
750 real symbol from the same dynamic object, make sure the real
751 symbol is also made into a dynamic symbol. */
754 struct elf_link_hash_entry
*def
= weakdef (h
);
756 if (def
->dynindx
== -1
757 && !bfd_elf_link_record_dynamic_symbol (info
, def
))
765 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
766 success, and 2 on a failure caused by attempting to record a symbol
767 in a discarded section, eg. a discarded link-once section symbol. */
770 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
775 struct elf_link_local_dynamic_entry
*entry
;
776 struct elf_link_hash_table
*eht
;
777 struct elf_strtab_hash
*dynstr
;
780 Elf_External_Sym_Shndx eshndx
;
781 char esym
[sizeof (Elf64_External_Sym
)];
783 if (! is_elf_hash_table (info
->hash
))
786 /* See if the entry exists already. */
787 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
788 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
791 amt
= sizeof (*entry
);
792 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
796 /* Go find the symbol, so that we can find it's name. */
797 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
798 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
800 bfd_release (input_bfd
, entry
);
804 if (entry
->isym
.st_shndx
!= SHN_UNDEF
805 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
809 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
810 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
812 /* We can still bfd_release here as nothing has done another
813 bfd_alloc. We can't do this later in this function. */
814 bfd_release (input_bfd
, entry
);
819 name
= (bfd_elf_string_from_elf_section
820 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
821 entry
->isym
.st_name
));
823 dynstr
= elf_hash_table (info
)->dynstr
;
826 /* Create a strtab to hold the dynamic symbol names. */
827 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
832 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
833 if (dynstr_index
== (size_t) -1)
835 entry
->isym
.st_name
= dynstr_index
;
837 eht
= elf_hash_table (info
);
839 entry
->next
= eht
->dynlocal
;
840 eht
->dynlocal
= entry
;
841 entry
->input_bfd
= input_bfd
;
842 entry
->input_indx
= input_indx
;
845 /* Whatever binding the symbol had before, it's now local. */
847 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
849 /* The dynindx will be set at the end of size_dynamic_sections. */
854 /* Return the dynindex of a local dynamic symbol. */
857 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
861 struct elf_link_local_dynamic_entry
*e
;
863 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
864 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
869 /* This function is used to renumber the dynamic symbols, if some of
870 them are removed because they are marked as local. This is called
871 via elf_link_hash_traverse. */
874 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
877 size_t *count
= (size_t *) data
;
882 if (h
->dynindx
!= -1)
883 h
->dynindx
= ++(*count
);
889 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
890 STB_LOCAL binding. */
893 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
896 size_t *count
= (size_t *) data
;
898 if (!h
->forced_local
)
901 if (h
->dynindx
!= -1)
902 h
->dynindx
= ++(*count
);
907 /* Return true if the dynamic symbol for a given section should be
908 omitted when creating a shared library. */
910 _bfd_elf_omit_section_dynsym_default (bfd
*output_bfd ATTRIBUTE_UNUSED
,
911 struct bfd_link_info
*info
,
914 struct elf_link_hash_table
*htab
;
917 switch (elf_section_data (p
)->this_hdr
.sh_type
)
921 /* If sh_type is yet undecided, assume it could be
922 SHT_PROGBITS/SHT_NOBITS. */
924 htab
= elf_hash_table (info
);
925 if (htab
->text_index_section
!= NULL
)
926 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
928 return (htab
->dynobj
!= NULL
929 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
930 && ip
->output_section
== p
);
932 /* There shouldn't be section relative relocations
933 against any other section. */
940 _bfd_elf_omit_section_dynsym_all
941 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
942 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
943 asection
*p ATTRIBUTE_UNUSED
)
948 /* Assign dynsym indices. In a shared library we generate a section
949 symbol for each output section, which come first. Next come symbols
950 which have been forced to local binding. Then all of the back-end
951 allocated local dynamic syms, followed by the rest of the global
952 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
953 (This prevents the early call before elf_backend_init_index_section
954 and strip_excluded_output_sections setting dynindx for sections
955 that are stripped.) */
958 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
959 struct bfd_link_info
*info
,
960 unsigned long *section_sym_count
)
962 unsigned long dynsymcount
= 0;
963 bfd_boolean do_sec
= section_sym_count
!= NULL
;
965 if (bfd_link_pic (info
)
966 || elf_hash_table (info
)->is_relocatable_executable
)
968 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
970 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
971 if ((p
->flags
& SEC_EXCLUDE
) == 0
972 && (p
->flags
& SEC_ALLOC
) != 0
973 && elf_hash_table (info
)->dynamic_relocs
974 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
978 elf_section_data (p
)->dynindx
= dynsymcount
;
981 elf_section_data (p
)->dynindx
= 0;
984 *section_sym_count
= dynsymcount
;
986 elf_link_hash_traverse (elf_hash_table (info
),
987 elf_link_renumber_local_hash_table_dynsyms
,
990 if (elf_hash_table (info
)->dynlocal
)
992 struct elf_link_local_dynamic_entry
*p
;
993 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
994 p
->dynindx
= ++dynsymcount
;
996 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
998 elf_link_hash_traverse (elf_hash_table (info
),
999 elf_link_renumber_hash_table_dynsyms
,
1002 /* There is an unused NULL entry at the head of the table which we
1003 must account for in our count even if the table is empty since it
1004 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1005 .dynamic section. */
1008 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
1012 /* Merge st_other field. */
1015 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
1016 unsigned int st_other
, asection
*sec
,
1017 bfd_boolean definition
, bfd_boolean dynamic
)
1019 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1021 /* If st_other has a processor-specific meaning, specific
1022 code might be needed here. */
1023 if (bed
->elf_backend_merge_symbol_attribute
)
1024 (*bed
->elf_backend_merge_symbol_attribute
) (h
, st_other
, definition
,
1029 unsigned symvis
= ELF_ST_VISIBILITY (st_other
);
1030 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
1032 /* Keep the most constraining visibility. Leave the remainder
1033 of the st_other field to elf_backend_merge_symbol_attribute. */
1034 if (symvis
- 1 < hvis
- 1)
1035 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
1038 && ELF_ST_VISIBILITY (st_other
) != STV_DEFAULT
1039 && (sec
->flags
& SEC_READONLY
) == 0)
1040 h
->protected_def
= 1;
1043 /* This function is called when we want to merge a new symbol with an
1044 existing symbol. It handles the various cases which arise when we
1045 find a definition in a dynamic object, or when there is already a
1046 definition in a dynamic object. The new symbol is described by
1047 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1048 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1049 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1050 of an old common symbol. We set OVERRIDE if the old symbol is
1051 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1052 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1053 to change. By OK to change, we mean that we shouldn't warn if the
1054 type or size does change. */
1057 _bfd_elf_merge_symbol (bfd
*abfd
,
1058 struct bfd_link_info
*info
,
1060 Elf_Internal_Sym
*sym
,
1063 struct elf_link_hash_entry
**sym_hash
,
1065 bfd_boolean
*pold_weak
,
1066 unsigned int *pold_alignment
,
1069 bfd_boolean
*type_change_ok
,
1070 bfd_boolean
*size_change_ok
,
1071 bfd_boolean
*matched
)
1073 asection
*sec
, *oldsec
;
1074 struct elf_link_hash_entry
*h
;
1075 struct elf_link_hash_entry
*hi
;
1076 struct elf_link_hash_entry
*flip
;
1079 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1080 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
1081 const struct elf_backend_data
*bed
;
1083 bfd_boolean default_sym
= *matched
;
1089 bind
= ELF_ST_BIND (sym
->st_info
);
1091 if (! bfd_is_und_section (sec
))
1092 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1094 h
= ((struct elf_link_hash_entry
*)
1095 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1100 bed
= get_elf_backend_data (abfd
);
1102 /* NEW_VERSION is the symbol version of the new symbol. */
1103 if (h
->versioned
!= unversioned
)
1105 /* Symbol version is unknown or versioned. */
1106 new_version
= strrchr (name
, ELF_VER_CHR
);
1109 if (h
->versioned
== unknown
)
1111 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1112 h
->versioned
= versioned_hidden
;
1114 h
->versioned
= versioned
;
1117 if (new_version
[0] == '\0')
1121 h
->versioned
= unversioned
;
1126 /* For merging, we only care about real symbols. But we need to make
1127 sure that indirect symbol dynamic flags are updated. */
1129 while (h
->root
.type
== bfd_link_hash_indirect
1130 || h
->root
.type
== bfd_link_hash_warning
)
1131 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1135 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1139 /* OLD_HIDDEN is true if the existing symbol is only visible
1140 to the symbol with the same symbol version. NEW_HIDDEN is
1141 true if the new symbol is only visible to the symbol with
1142 the same symbol version. */
1143 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1144 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1145 if (!old_hidden
&& !new_hidden
)
1146 /* The new symbol matches the existing symbol if both
1151 /* OLD_VERSION is the symbol version of the existing
1155 if (h
->versioned
>= versioned
)
1156 old_version
= strrchr (h
->root
.root
.string
,
1161 /* The new symbol matches the existing symbol if they
1162 have the same symbol version. */
1163 *matched
= (old_version
== new_version
1164 || (old_version
!= NULL
1165 && new_version
!= NULL
1166 && strcmp (old_version
, new_version
) == 0));
1171 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1176 switch (h
->root
.type
)
1181 case bfd_link_hash_undefined
:
1182 case bfd_link_hash_undefweak
:
1183 oldbfd
= h
->root
.u
.undef
.abfd
;
1186 case bfd_link_hash_defined
:
1187 case bfd_link_hash_defweak
:
1188 oldbfd
= h
->root
.u
.def
.section
->owner
;
1189 oldsec
= h
->root
.u
.def
.section
;
1192 case bfd_link_hash_common
:
1193 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1194 oldsec
= h
->root
.u
.c
.p
->section
;
1196 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1199 if (poldbfd
&& *poldbfd
== NULL
)
1202 /* Differentiate strong and weak symbols. */
1203 newweak
= bind
== STB_WEAK
;
1204 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1205 || h
->root
.type
== bfd_link_hash_undefweak
);
1207 *pold_weak
= oldweak
;
1209 /* We have to check it for every instance since the first few may be
1210 references and not all compilers emit symbol type for undefined
1212 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1214 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1215 respectively, is from a dynamic object. */
1217 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1219 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1220 syms and defined syms in dynamic libraries respectively.
1221 ref_dynamic on the other hand can be set for a symbol defined in
1222 a dynamic library, and def_dynamic may not be set; When the
1223 definition in a dynamic lib is overridden by a definition in the
1224 executable use of the symbol in the dynamic lib becomes a
1225 reference to the executable symbol. */
1228 if (bfd_is_und_section (sec
))
1230 if (bind
!= STB_WEAK
)
1232 h
->ref_dynamic_nonweak
= 1;
1233 hi
->ref_dynamic_nonweak
= 1;
1238 /* Update the existing symbol only if they match. */
1241 hi
->dynamic_def
= 1;
1245 /* If we just created the symbol, mark it as being an ELF symbol.
1246 Other than that, there is nothing to do--there is no merge issue
1247 with a newly defined symbol--so we just return. */
1249 if (h
->root
.type
== bfd_link_hash_new
)
1255 /* In cases involving weak versioned symbols, we may wind up trying
1256 to merge a symbol with itself. Catch that here, to avoid the
1257 confusion that results if we try to override a symbol with
1258 itself. The additional tests catch cases like
1259 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1260 dynamic object, which we do want to handle here. */
1262 && (newweak
|| oldweak
)
1263 && ((abfd
->flags
& DYNAMIC
) == 0
1264 || !h
->def_regular
))
1269 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1270 else if (oldsec
!= NULL
)
1272 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1273 indices used by MIPS ELF. */
1274 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1277 /* Handle a case where plugin_notice won't be called and thus won't
1278 set the non_ir_ref flags on the first pass over symbols. */
1280 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
)
1281 && newdyn
!= olddyn
)
1283 h
->root
.non_ir_ref_dynamic
= TRUE
;
1284 hi
->root
.non_ir_ref_dynamic
= TRUE
;
1287 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1288 respectively, appear to be a definition rather than reference. */
1290 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1292 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1293 && h
->root
.type
!= bfd_link_hash_undefweak
1294 && h
->root
.type
!= bfd_link_hash_common
);
1296 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1297 respectively, appear to be a function. */
1299 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1300 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1302 oldfunc
= (h
->type
!= STT_NOTYPE
1303 && bed
->is_function_type (h
->type
));
1305 if (!(newfunc
&& oldfunc
)
1306 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1307 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1308 && h
->type
!= STT_NOTYPE
1309 && (newdef
|| bfd_is_com_section (sec
))
1310 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1312 /* If creating a default indirect symbol ("foo" or "foo@") from
1313 a dynamic versioned definition ("foo@@") skip doing so if
1314 there is an existing regular definition with a different
1315 type. We don't want, for example, a "time" variable in the
1316 executable overriding a "time" function in a shared library. */
1324 /* When adding a symbol from a regular object file after we have
1325 created indirect symbols, undo the indirection and any
1332 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1333 h
->forced_local
= 0;
1337 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1339 h
->root
.type
= bfd_link_hash_undefined
;
1340 h
->root
.u
.undef
.abfd
= abfd
;
1344 h
->root
.type
= bfd_link_hash_new
;
1345 h
->root
.u
.undef
.abfd
= NULL
;
1351 /* Check TLS symbols. We don't check undefined symbols introduced
1352 by "ld -u" which have no type (and oldbfd NULL), and we don't
1353 check symbols from plugins because they also have no type. */
1355 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1356 && (abfd
->flags
& BFD_PLUGIN
) == 0
1357 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1358 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1361 bfd_boolean ntdef
, tdef
;
1362 asection
*ntsec
, *tsec
;
1364 if (h
->type
== STT_TLS
)
1385 /* xgettext:c-format */
1386 (_("%s: TLS definition in %pB section %pA "
1387 "mismatches non-TLS definition in %pB section %pA"),
1388 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1389 else if (!tdef
&& !ntdef
)
1391 /* xgettext:c-format */
1392 (_("%s: TLS reference in %pB "
1393 "mismatches non-TLS reference in %pB"),
1394 h
->root
.root
.string
, tbfd
, ntbfd
);
1397 /* xgettext:c-format */
1398 (_("%s: TLS definition in %pB section %pA "
1399 "mismatches non-TLS reference in %pB"),
1400 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1403 /* xgettext:c-format */
1404 (_("%s: TLS reference in %pB "
1405 "mismatches non-TLS definition in %pB section %pA"),
1406 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1408 bfd_set_error (bfd_error_bad_value
);
1412 /* If the old symbol has non-default visibility, we ignore the new
1413 definition from a dynamic object. */
1415 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1416 && !bfd_is_und_section (sec
))
1419 /* Make sure this symbol is dynamic. */
1421 hi
->ref_dynamic
= 1;
1422 /* A protected symbol has external availability. Make sure it is
1423 recorded as dynamic.
1425 FIXME: Should we check type and size for protected symbol? */
1426 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1427 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1432 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1435 /* If the new symbol with non-default visibility comes from a
1436 relocatable file and the old definition comes from a dynamic
1437 object, we remove the old definition. */
1438 if (hi
->root
.type
== bfd_link_hash_indirect
)
1440 /* Handle the case where the old dynamic definition is
1441 default versioned. We need to copy the symbol info from
1442 the symbol with default version to the normal one if it
1443 was referenced before. */
1446 hi
->root
.type
= h
->root
.type
;
1447 h
->root
.type
= bfd_link_hash_indirect
;
1448 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1450 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1451 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1453 /* If the new symbol is hidden or internal, completely undo
1454 any dynamic link state. */
1455 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1456 h
->forced_local
= 0;
1463 /* FIXME: Should we check type and size for protected symbol? */
1473 /* If the old symbol was undefined before, then it will still be
1474 on the undefs list. If the new symbol is undefined or
1475 common, we can't make it bfd_link_hash_new here, because new
1476 undefined or common symbols will be added to the undefs list
1477 by _bfd_generic_link_add_one_symbol. Symbols may not be
1478 added twice to the undefs list. Also, if the new symbol is
1479 undefweak then we don't want to lose the strong undef. */
1480 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1482 h
->root
.type
= bfd_link_hash_undefined
;
1483 h
->root
.u
.undef
.abfd
= abfd
;
1487 h
->root
.type
= bfd_link_hash_new
;
1488 h
->root
.u
.undef
.abfd
= NULL
;
1491 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1493 /* If the new symbol is hidden or internal, completely undo
1494 any dynamic link state. */
1495 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1496 h
->forced_local
= 0;
1502 /* FIXME: Should we check type and size for protected symbol? */
1508 /* If a new weak symbol definition comes from a regular file and the
1509 old symbol comes from a dynamic library, we treat the new one as
1510 strong. Similarly, an old weak symbol definition from a regular
1511 file is treated as strong when the new symbol comes from a dynamic
1512 library. Further, an old weak symbol from a dynamic library is
1513 treated as strong if the new symbol is from a dynamic library.
1514 This reflects the way glibc's ld.so works.
1516 Also allow a weak symbol to override a linker script symbol
1517 defined by an early pass over the script. This is done so the
1518 linker knows the symbol is defined in an object file, for the
1519 DEFINED script function.
1521 Do this before setting *type_change_ok or *size_change_ok so that
1522 we warn properly when dynamic library symbols are overridden. */
1524 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1526 if (olddef
&& newdyn
)
1529 /* Allow changes between different types of function symbol. */
1530 if (newfunc
&& oldfunc
)
1531 *type_change_ok
= TRUE
;
1533 /* It's OK to change the type if either the existing symbol or the
1534 new symbol is weak. A type change is also OK if the old symbol
1535 is undefined and the new symbol is defined. */
1540 && h
->root
.type
== bfd_link_hash_undefined
))
1541 *type_change_ok
= TRUE
;
1543 /* It's OK to change the size if either the existing symbol or the
1544 new symbol is weak, or if the old symbol is undefined. */
1547 || h
->root
.type
== bfd_link_hash_undefined
)
1548 *size_change_ok
= TRUE
;
1550 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1551 symbol, respectively, appears to be a common symbol in a dynamic
1552 object. If a symbol appears in an uninitialized section, and is
1553 not weak, and is not a function, then it may be a common symbol
1554 which was resolved when the dynamic object was created. We want
1555 to treat such symbols specially, because they raise special
1556 considerations when setting the symbol size: if the symbol
1557 appears as a common symbol in a regular object, and the size in
1558 the regular object is larger, we must make sure that we use the
1559 larger size. This problematic case can always be avoided in C,
1560 but it must be handled correctly when using Fortran shared
1563 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1564 likewise for OLDDYNCOMMON and OLDDEF.
1566 Note that this test is just a heuristic, and that it is quite
1567 possible to have an uninitialized symbol in a shared object which
1568 is really a definition, rather than a common symbol. This could
1569 lead to some minor confusion when the symbol really is a common
1570 symbol in some regular object. However, I think it will be
1576 && (sec
->flags
& SEC_ALLOC
) != 0
1577 && (sec
->flags
& SEC_LOAD
) == 0
1580 newdyncommon
= TRUE
;
1582 newdyncommon
= FALSE
;
1586 && h
->root
.type
== bfd_link_hash_defined
1588 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1589 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1592 olddyncommon
= TRUE
;
1594 olddyncommon
= FALSE
;
1596 /* We now know everything about the old and new symbols. We ask the
1597 backend to check if we can merge them. */
1598 if (bed
->merge_symbol
!= NULL
)
1600 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1605 /* There are multiple definitions of a normal symbol. Skip the
1606 default symbol as well as definition from an IR object. */
1607 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1608 && !default_sym
&& h
->def_regular
1610 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1611 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1613 /* Handle a multiple definition. */
1614 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1615 abfd
, sec
, *pvalue
);
1620 /* If both the old and the new symbols look like common symbols in a
1621 dynamic object, set the size of the symbol to the larger of the
1626 && sym
->st_size
!= h
->size
)
1628 /* Since we think we have two common symbols, issue a multiple
1629 common warning if desired. Note that we only warn if the
1630 size is different. If the size is the same, we simply let
1631 the old symbol override the new one as normally happens with
1632 symbols defined in dynamic objects. */
1634 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1635 bfd_link_hash_common
, sym
->st_size
);
1636 if (sym
->st_size
> h
->size
)
1637 h
->size
= sym
->st_size
;
1639 *size_change_ok
= TRUE
;
1642 /* If we are looking at a dynamic object, and we have found a
1643 definition, we need to see if the symbol was already defined by
1644 some other object. If so, we want to use the existing
1645 definition, and we do not want to report a multiple symbol
1646 definition error; we do this by clobbering *PSEC to be
1647 bfd_und_section_ptr.
1649 We treat a common symbol as a definition if the symbol in the
1650 shared library is a function, since common symbols always
1651 represent variables; this can cause confusion in principle, but
1652 any such confusion would seem to indicate an erroneous program or
1653 shared library. We also permit a common symbol in a regular
1654 object to override a weak symbol in a shared object. */
1659 || (h
->root
.type
== bfd_link_hash_common
1660 && (newweak
|| newfunc
))))
1664 newdyncommon
= FALSE
;
1666 *psec
= sec
= bfd_und_section_ptr
;
1667 *size_change_ok
= TRUE
;
1669 /* If we get here when the old symbol is a common symbol, then
1670 we are explicitly letting it override a weak symbol or
1671 function in a dynamic object, and we don't want to warn about
1672 a type change. If the old symbol is a defined symbol, a type
1673 change warning may still be appropriate. */
1675 if (h
->root
.type
== bfd_link_hash_common
)
1676 *type_change_ok
= TRUE
;
1679 /* Handle the special case of an old common symbol merging with a
1680 new symbol which looks like a common symbol in a shared object.
1681 We change *PSEC and *PVALUE to make the new symbol look like a
1682 common symbol, and let _bfd_generic_link_add_one_symbol do the
1686 && h
->root
.type
== bfd_link_hash_common
)
1690 newdyncommon
= FALSE
;
1691 *pvalue
= sym
->st_size
;
1692 *psec
= sec
= bed
->common_section (oldsec
);
1693 *size_change_ok
= TRUE
;
1696 /* Skip weak definitions of symbols that are already defined. */
1697 if (newdef
&& olddef
&& newweak
)
1699 /* Don't skip new non-IR weak syms. */
1700 if (!(oldbfd
!= NULL
1701 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1702 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1708 /* Merge st_other. If the symbol already has a dynamic index,
1709 but visibility says it should not be visible, turn it into a
1711 elf_merge_st_other (abfd
, h
, sym
->st_other
, sec
, newdef
, newdyn
);
1712 if (h
->dynindx
!= -1)
1713 switch (ELF_ST_VISIBILITY (h
->other
))
1717 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1722 /* If the old symbol is from a dynamic object, and the new symbol is
1723 a definition which is not from a dynamic object, then the new
1724 symbol overrides the old symbol. Symbols from regular files
1725 always take precedence over symbols from dynamic objects, even if
1726 they are defined after the dynamic object in the link.
1728 As above, we again permit a common symbol in a regular object to
1729 override a definition in a shared object if the shared object
1730 symbol is a function or is weak. */
1735 || (bfd_is_com_section (sec
)
1736 && (oldweak
|| oldfunc
)))
1741 /* Change the hash table entry to undefined, and let
1742 _bfd_generic_link_add_one_symbol do the right thing with the
1745 h
->root
.type
= bfd_link_hash_undefined
;
1746 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1747 *size_change_ok
= TRUE
;
1750 olddyncommon
= FALSE
;
1752 /* We again permit a type change when a common symbol may be
1753 overriding a function. */
1755 if (bfd_is_com_section (sec
))
1759 /* If a common symbol overrides a function, make sure
1760 that it isn't defined dynamically nor has type
1763 h
->type
= STT_NOTYPE
;
1765 *type_change_ok
= TRUE
;
1768 if (hi
->root
.type
== bfd_link_hash_indirect
)
1771 /* This union may have been set to be non-NULL when this symbol
1772 was seen in a dynamic object. We must force the union to be
1773 NULL, so that it is correct for a regular symbol. */
1774 h
->verinfo
.vertree
= NULL
;
1777 /* Handle the special case of a new common symbol merging with an
1778 old symbol that looks like it might be a common symbol defined in
1779 a shared object. Note that we have already handled the case in
1780 which a new common symbol should simply override the definition
1781 in the shared library. */
1784 && bfd_is_com_section (sec
)
1787 /* It would be best if we could set the hash table entry to a
1788 common symbol, but we don't know what to use for the section
1789 or the alignment. */
1790 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1791 bfd_link_hash_common
, sym
->st_size
);
1793 /* If the presumed common symbol in the dynamic object is
1794 larger, pretend that the new symbol has its size. */
1796 if (h
->size
> *pvalue
)
1799 /* We need to remember the alignment required by the symbol
1800 in the dynamic object. */
1801 BFD_ASSERT (pold_alignment
);
1802 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1805 olddyncommon
= FALSE
;
1807 h
->root
.type
= bfd_link_hash_undefined
;
1808 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1810 *size_change_ok
= TRUE
;
1811 *type_change_ok
= TRUE
;
1813 if (hi
->root
.type
== bfd_link_hash_indirect
)
1816 h
->verinfo
.vertree
= NULL
;
1821 /* Handle the case where we had a versioned symbol in a dynamic
1822 library and now find a definition in a normal object. In this
1823 case, we make the versioned symbol point to the normal one. */
1824 flip
->root
.type
= h
->root
.type
;
1825 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1826 h
->root
.type
= bfd_link_hash_indirect
;
1827 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1828 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1832 flip
->ref_dynamic
= 1;
1839 /* This function is called to create an indirect symbol from the
1840 default for the symbol with the default version if needed. The
1841 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1842 set DYNSYM if the new indirect symbol is dynamic. */
1845 _bfd_elf_add_default_symbol (bfd
*abfd
,
1846 struct bfd_link_info
*info
,
1847 struct elf_link_hash_entry
*h
,
1849 Elf_Internal_Sym
*sym
,
1853 bfd_boolean
*dynsym
)
1855 bfd_boolean type_change_ok
;
1856 bfd_boolean size_change_ok
;
1859 struct elf_link_hash_entry
*hi
;
1860 struct bfd_link_hash_entry
*bh
;
1861 const struct elf_backend_data
*bed
;
1862 bfd_boolean collect
;
1863 bfd_boolean dynamic
;
1866 size_t len
, shortlen
;
1868 bfd_boolean matched
;
1870 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1873 /* If this symbol has a version, and it is the default version, we
1874 create an indirect symbol from the default name to the fully
1875 decorated name. This will cause external references which do not
1876 specify a version to be bound to this version of the symbol. */
1877 p
= strchr (name
, ELF_VER_CHR
);
1878 if (h
->versioned
== unknown
)
1882 h
->versioned
= unversioned
;
1887 if (p
[1] != ELF_VER_CHR
)
1889 h
->versioned
= versioned_hidden
;
1893 h
->versioned
= versioned
;
1898 /* PR ld/19073: We may see an unversioned definition after the
1904 bed
= get_elf_backend_data (abfd
);
1905 collect
= bed
->collect
;
1906 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1908 shortlen
= p
- name
;
1909 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1910 if (shortname
== NULL
)
1912 memcpy (shortname
, name
, shortlen
);
1913 shortname
[shortlen
] = '\0';
1915 /* We are going to create a new symbol. Merge it with any existing
1916 symbol with this name. For the purposes of the merge, act as
1917 though we were defining the symbol we just defined, although we
1918 actually going to define an indirect symbol. */
1919 type_change_ok
= FALSE
;
1920 size_change_ok
= FALSE
;
1923 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1924 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1925 &type_change_ok
, &size_change_ok
, &matched
))
1931 if (hi
->def_regular
|| ELF_COMMON_DEF_P (hi
))
1933 /* If the undecorated symbol will have a version added by a
1934 script different to H, then don't indirect to/from the
1935 undecorated symbol. This isn't ideal because we may not yet
1936 have seen symbol versions, if given by a script on the
1937 command line rather than via --version-script. */
1938 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1943 = bfd_find_version_for_sym (info
->version_info
,
1944 hi
->root
.root
.string
, &hide
);
1945 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1947 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1951 if (hi
->verinfo
.vertree
!= NULL
1952 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1958 /* Add the default symbol if not performing a relocatable link. */
1959 if (! bfd_link_relocatable (info
))
1962 if (bh
->type
== bfd_link_hash_defined
1963 && bh
->u
.def
.section
->owner
!= NULL
1964 && (bh
->u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
1966 /* Mark the previous definition from IR object as
1967 undefined so that the generic linker will override
1969 bh
->type
= bfd_link_hash_undefined
;
1970 bh
->u
.undef
.abfd
= bh
->u
.def
.section
->owner
;
1972 if (! (_bfd_generic_link_add_one_symbol
1973 (info
, abfd
, shortname
, BSF_INDIRECT
,
1974 bfd_ind_section_ptr
,
1975 0, name
, FALSE
, collect
, &bh
)))
1977 hi
= (struct elf_link_hash_entry
*) bh
;
1982 /* In this case the symbol named SHORTNAME is overriding the
1983 indirect symbol we want to add. We were planning on making
1984 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1985 is the name without a version. NAME is the fully versioned
1986 name, and it is the default version.
1988 Overriding means that we already saw a definition for the
1989 symbol SHORTNAME in a regular object, and it is overriding
1990 the symbol defined in the dynamic object.
1992 When this happens, we actually want to change NAME, the
1993 symbol we just added, to refer to SHORTNAME. This will cause
1994 references to NAME in the shared object to become references
1995 to SHORTNAME in the regular object. This is what we expect
1996 when we override a function in a shared object: that the
1997 references in the shared object will be mapped to the
1998 definition in the regular object. */
2000 while (hi
->root
.type
== bfd_link_hash_indirect
2001 || hi
->root
.type
== bfd_link_hash_warning
)
2002 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2004 h
->root
.type
= bfd_link_hash_indirect
;
2005 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
2009 hi
->ref_dynamic
= 1;
2013 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
2018 /* Now set HI to H, so that the following code will set the
2019 other fields correctly. */
2023 /* Check if HI is a warning symbol. */
2024 if (hi
->root
.type
== bfd_link_hash_warning
)
2025 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2027 /* If there is a duplicate definition somewhere, then HI may not
2028 point to an indirect symbol. We will have reported an error to
2029 the user in that case. */
2031 if (hi
->root
.type
== bfd_link_hash_indirect
)
2033 struct elf_link_hash_entry
*ht
;
2035 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2036 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2038 /* If we first saw a reference to SHORTNAME with non-default
2039 visibility, merge that visibility to the @@VER symbol. */
2040 elf_merge_st_other (abfd
, ht
, hi
->other
, sec
, TRUE
, dynamic
);
2042 /* A reference to the SHORTNAME symbol from a dynamic library
2043 will be satisfied by the versioned symbol at runtime. In
2044 effect, we have a reference to the versioned symbol. */
2045 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2046 hi
->dynamic_def
|= ht
->dynamic_def
;
2048 /* See if the new flags lead us to realize that the symbol must
2054 if (! bfd_link_executable (info
)
2061 if (hi
->ref_regular
)
2067 /* We also need to define an indirection from the nondefault version
2071 len
= strlen (name
);
2072 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2073 if (shortname
== NULL
)
2075 memcpy (shortname
, name
, shortlen
);
2076 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2078 /* Once again, merge with any existing symbol. */
2079 type_change_ok
= FALSE
;
2080 size_change_ok
= FALSE
;
2082 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2083 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2084 &type_change_ok
, &size_change_ok
, &matched
))
2090 && h
->root
.type
== bfd_link_hash_defweak
2091 && hi
->root
.type
== bfd_link_hash_defined
)
2093 /* We are handling a weak sym@@ver and attempting to define
2094 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2095 new weak sym@ver because there is already a strong sym@ver.
2096 However, sym@ver and sym@@ver are really the same symbol.
2097 The existing strong sym@ver ought to override sym@@ver. */
2098 h
->root
.type
= bfd_link_hash_defined
;
2099 h
->root
.u
.def
.section
= hi
->root
.u
.def
.section
;
2100 h
->root
.u
.def
.value
= hi
->root
.u
.def
.value
;
2101 hi
->root
.type
= bfd_link_hash_indirect
;
2102 hi
->root
.u
.i
.link
= &h
->root
;
2109 /* Here SHORTNAME is a versioned name, so we don't expect to see
2110 the type of override we do in the case above unless it is
2111 overridden by a versioned definition. */
2112 if (hi
->root
.type
!= bfd_link_hash_defined
2113 && hi
->root
.type
!= bfd_link_hash_defweak
)
2115 /* xgettext:c-format */
2116 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2123 if (! (_bfd_generic_link_add_one_symbol
2124 (info
, abfd
, shortname
, BSF_INDIRECT
,
2125 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
2127 hi
= (struct elf_link_hash_entry
*) bh
;
2130 /* If there is a duplicate definition somewhere, then HI may not
2131 point to an indirect symbol. We will have reported an error
2132 to the user in that case. */
2133 if (hi
->root
.type
== bfd_link_hash_indirect
)
2135 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2136 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2137 hi
->dynamic_def
|= h
->dynamic_def
;
2139 /* If we first saw a reference to @VER symbol with
2140 non-default visibility, merge that visibility to the
2142 elf_merge_st_other (abfd
, h
, hi
->other
, sec
, TRUE
, dynamic
);
2144 /* See if the new flags lead us to realize that the symbol
2150 if (! bfd_link_executable (info
)
2156 if (hi
->ref_regular
)
2165 /* This routine is used to export all defined symbols into the dynamic
2166 symbol table. It is called via elf_link_hash_traverse. */
2169 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2171 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2173 /* Ignore indirect symbols. These are added by the versioning code. */
2174 if (h
->root
.type
== bfd_link_hash_indirect
)
2177 /* Ignore this if we won't export it. */
2178 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2181 if (h
->dynindx
== -1
2182 && (h
->def_regular
|| h
->ref_regular
)
2183 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2184 h
->root
.root
.string
))
2186 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2196 /* Look through the symbols which are defined in other shared
2197 libraries and referenced here. Update the list of version
2198 dependencies. This will be put into the .gnu.version_r section.
2199 This function is called via elf_link_hash_traverse. */
2202 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2205 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2206 Elf_Internal_Verneed
*t
;
2207 Elf_Internal_Vernaux
*a
;
2210 /* We only care about symbols defined in shared objects with version
2215 || h
->verinfo
.verdef
== NULL
2216 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2217 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2220 /* See if we already know about this version. */
2221 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2225 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2228 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2229 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2235 /* This is a new version. Add it to tree we are building. */
2240 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2243 rinfo
->failed
= TRUE
;
2247 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2248 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2249 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2253 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2256 rinfo
->failed
= TRUE
;
2260 /* Note that we are copying a string pointer here, and testing it
2261 above. If bfd_elf_string_from_elf_section is ever changed to
2262 discard the string data when low in memory, this will have to be
2264 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2266 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2267 a
->vna_nextptr
= t
->vn_auxptr
;
2269 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2272 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2279 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2280 hidden. Set *T_P to NULL if there is no match. */
2283 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2284 struct elf_link_hash_entry
*h
,
2285 const char *version_p
,
2286 struct bfd_elf_version_tree
**t_p
,
2289 struct bfd_elf_version_tree
*t
;
2291 /* Look for the version. If we find it, it is no longer weak. */
2292 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2294 if (strcmp (t
->name
, version_p
) == 0)
2298 struct bfd_elf_version_expr
*d
;
2300 len
= version_p
- h
->root
.root
.string
;
2301 alc
= (char *) bfd_malloc (len
);
2304 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2305 alc
[len
- 1] = '\0';
2306 if (alc
[len
- 2] == ELF_VER_CHR
)
2307 alc
[len
- 2] = '\0';
2309 h
->verinfo
.vertree
= t
;
2313 if (t
->globals
.list
!= NULL
)
2314 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2316 /* See if there is anything to force this symbol to
2318 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2320 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2323 && ! info
->export_dynamic
)
2337 /* Return TRUE if the symbol H is hidden by version script. */
2340 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2341 struct elf_link_hash_entry
*h
)
2344 bfd_boolean hide
= FALSE
;
2345 const struct elf_backend_data
*bed
2346 = get_elf_backend_data (info
->output_bfd
);
2348 /* Version script only hides symbols defined in regular objects. */
2349 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2352 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2353 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2355 struct bfd_elf_version_tree
*t
;
2358 if (*p
== ELF_VER_CHR
)
2362 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2366 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2371 /* If we don't have a version for this symbol, see if we can find
2373 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2376 = bfd_find_version_for_sym (info
->version_info
,
2377 h
->root
.root
.string
, &hide
);
2378 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2380 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2388 /* Figure out appropriate versions for all the symbols. We may not
2389 have the version number script until we have read all of the input
2390 files, so until that point we don't know which symbols should be
2391 local. This function is called via elf_link_hash_traverse. */
2394 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2396 struct elf_info_failed
*sinfo
;
2397 struct bfd_link_info
*info
;
2398 const struct elf_backend_data
*bed
;
2399 struct elf_info_failed eif
;
2403 sinfo
= (struct elf_info_failed
*) data
;
2406 /* Fix the symbol flags. */
2409 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2412 sinfo
->failed
= TRUE
;
2416 bed
= get_elf_backend_data (info
->output_bfd
);
2418 /* We only need version numbers for symbols defined in regular
2420 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2422 /* Hide symbols defined in discarded input sections. */
2423 if ((h
->root
.type
== bfd_link_hash_defined
2424 || h
->root
.type
== bfd_link_hash_defweak
)
2425 && discarded_section (h
->root
.u
.def
.section
))
2426 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2431 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2432 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2434 struct bfd_elf_version_tree
*t
;
2437 if (*p
== ELF_VER_CHR
)
2440 /* If there is no version string, we can just return out. */
2444 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2446 sinfo
->failed
= TRUE
;
2451 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2453 /* If we are building an application, we need to create a
2454 version node for this version. */
2455 if (t
== NULL
&& bfd_link_executable (info
))
2457 struct bfd_elf_version_tree
**pp
;
2460 /* If we aren't going to export this symbol, we don't need
2461 to worry about it. */
2462 if (h
->dynindx
== -1)
2465 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2469 sinfo
->failed
= TRUE
;
2474 t
->name_indx
= (unsigned int) -1;
2478 /* Don't count anonymous version tag. */
2479 if (sinfo
->info
->version_info
!= NULL
2480 && sinfo
->info
->version_info
->vernum
== 0)
2482 for (pp
= &sinfo
->info
->version_info
;
2486 t
->vernum
= version_index
;
2490 h
->verinfo
.vertree
= t
;
2494 /* We could not find the version for a symbol when
2495 generating a shared archive. Return an error. */
2497 /* xgettext:c-format */
2498 (_("%pB: version node not found for symbol %s"),
2499 info
->output_bfd
, h
->root
.root
.string
);
2500 bfd_set_error (bfd_error_bad_value
);
2501 sinfo
->failed
= TRUE
;
2506 /* If we don't have a version for this symbol, see if we can find
2509 && h
->verinfo
.vertree
== NULL
2510 && sinfo
->info
->version_info
!= NULL
)
2513 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2514 h
->root
.root
.string
, &hide
);
2515 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2516 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2522 /* Read and swap the relocs from the section indicated by SHDR. This
2523 may be either a REL or a RELA section. The relocations are
2524 translated into RELA relocations and stored in INTERNAL_RELOCS,
2525 which should have already been allocated to contain enough space.
2526 The EXTERNAL_RELOCS are a buffer where the external form of the
2527 relocations should be stored.
2529 Returns FALSE if something goes wrong. */
2532 elf_link_read_relocs_from_section (bfd
*abfd
,
2534 Elf_Internal_Shdr
*shdr
,
2535 void *external_relocs
,
2536 Elf_Internal_Rela
*internal_relocs
)
2538 const struct elf_backend_data
*bed
;
2539 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2540 const bfd_byte
*erela
;
2541 const bfd_byte
*erelaend
;
2542 Elf_Internal_Rela
*irela
;
2543 Elf_Internal_Shdr
*symtab_hdr
;
2546 /* Position ourselves at the start of the section. */
2547 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2550 /* Read the relocations. */
2551 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2554 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2555 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2557 bed
= get_elf_backend_data (abfd
);
2559 /* Convert the external relocations to the internal format. */
2560 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2561 swap_in
= bed
->s
->swap_reloc_in
;
2562 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2563 swap_in
= bed
->s
->swap_reloca_in
;
2566 bfd_set_error (bfd_error_wrong_format
);
2570 erela
= (const bfd_byte
*) external_relocs
;
2571 /* Setting erelaend like this and comparing with <= handles case of
2572 a fuzzed object with sh_size not a multiple of sh_entsize. */
2573 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2574 irela
= internal_relocs
;
2575 while (erela
<= erelaend
)
2579 (*swap_in
) (abfd
, erela
, irela
);
2580 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2581 if (bed
->s
->arch_size
== 64)
2585 if ((size_t) r_symndx
>= nsyms
)
2588 /* xgettext:c-format */
2589 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2590 " for offset %#" PRIx64
" in section `%pA'"),
2591 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2592 (uint64_t) irela
->r_offset
, sec
);
2593 bfd_set_error (bfd_error_bad_value
);
2597 else if (r_symndx
!= STN_UNDEF
)
2600 /* xgettext:c-format */
2601 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2602 " for offset %#" PRIx64
" in section `%pA'"
2603 " when the object file has no symbol table"),
2604 abfd
, (uint64_t) r_symndx
,
2605 (uint64_t) irela
->r_offset
, sec
);
2606 bfd_set_error (bfd_error_bad_value
);
2609 irela
+= bed
->s
->int_rels_per_ext_rel
;
2610 erela
+= shdr
->sh_entsize
;
2616 /* Read and swap the relocs for a section O. They may have been
2617 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2618 not NULL, they are used as buffers to read into. They are known to
2619 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2620 the return value is allocated using either malloc or bfd_alloc,
2621 according to the KEEP_MEMORY argument. If O has two relocation
2622 sections (both REL and RELA relocations), then the REL_HDR
2623 relocations will appear first in INTERNAL_RELOCS, followed by the
2624 RELA_HDR relocations. */
2627 _bfd_elf_link_read_relocs (bfd
*abfd
,
2629 void *external_relocs
,
2630 Elf_Internal_Rela
*internal_relocs
,
2631 bfd_boolean keep_memory
)
2633 void *alloc1
= NULL
;
2634 Elf_Internal_Rela
*alloc2
= NULL
;
2635 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2636 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2637 Elf_Internal_Rela
*internal_rela_relocs
;
2639 if (esdo
->relocs
!= NULL
)
2640 return esdo
->relocs
;
2642 if (o
->reloc_count
== 0)
2645 if (internal_relocs
== NULL
)
2649 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2651 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2653 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2654 if (internal_relocs
== NULL
)
2658 if (external_relocs
== NULL
)
2660 bfd_size_type size
= 0;
2663 size
+= esdo
->rel
.hdr
->sh_size
;
2665 size
+= esdo
->rela
.hdr
->sh_size
;
2667 alloc1
= bfd_malloc (size
);
2670 external_relocs
= alloc1
;
2673 internal_rela_relocs
= internal_relocs
;
2676 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2680 external_relocs
= (((bfd_byte
*) external_relocs
)
2681 + esdo
->rel
.hdr
->sh_size
);
2682 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2683 * bed
->s
->int_rels_per_ext_rel
);
2687 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2689 internal_rela_relocs
)))
2692 /* Cache the results for next time, if we can. */
2694 esdo
->relocs
= internal_relocs
;
2698 /* Don't free alloc2, since if it was allocated we are passing it
2699 back (under the name of internal_relocs). */
2701 return internal_relocs
;
2708 bfd_release (abfd
, alloc2
);
2715 /* Compute the size of, and allocate space for, REL_HDR which is the
2716 section header for a section containing relocations for O. */
2719 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2720 struct bfd_elf_section_reloc_data
*reldata
)
2722 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2724 /* That allows us to calculate the size of the section. */
2725 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2727 /* The contents field must last into write_object_contents, so we
2728 allocate it with bfd_alloc rather than malloc. Also since we
2729 cannot be sure that the contents will actually be filled in,
2730 we zero the allocated space. */
2731 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2732 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2735 if (reldata
->hashes
== NULL
&& reldata
->count
)
2737 struct elf_link_hash_entry
**p
;
2739 p
= ((struct elf_link_hash_entry
**)
2740 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2744 reldata
->hashes
= p
;
2750 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2751 originated from the section given by INPUT_REL_HDR) to the
2755 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2756 asection
*input_section
,
2757 Elf_Internal_Shdr
*input_rel_hdr
,
2758 Elf_Internal_Rela
*internal_relocs
,
2759 struct elf_link_hash_entry
**rel_hash
2762 Elf_Internal_Rela
*irela
;
2763 Elf_Internal_Rela
*irelaend
;
2765 struct bfd_elf_section_reloc_data
*output_reldata
;
2766 asection
*output_section
;
2767 const struct elf_backend_data
*bed
;
2768 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2769 struct bfd_elf_section_data
*esdo
;
2771 output_section
= input_section
->output_section
;
2773 bed
= get_elf_backend_data (output_bfd
);
2774 esdo
= elf_section_data (output_section
);
2775 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2777 output_reldata
= &esdo
->rel
;
2778 swap_out
= bed
->s
->swap_reloc_out
;
2780 else if (esdo
->rela
.hdr
2781 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2783 output_reldata
= &esdo
->rela
;
2784 swap_out
= bed
->s
->swap_reloca_out
;
2789 /* xgettext:c-format */
2790 (_("%pB: relocation size mismatch in %pB section %pA"),
2791 output_bfd
, input_section
->owner
, input_section
);
2792 bfd_set_error (bfd_error_wrong_format
);
2796 erel
= output_reldata
->hdr
->contents
;
2797 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2798 irela
= internal_relocs
;
2799 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2800 * bed
->s
->int_rels_per_ext_rel
);
2801 while (irela
< irelaend
)
2803 (*swap_out
) (output_bfd
, irela
, erel
);
2804 irela
+= bed
->s
->int_rels_per_ext_rel
;
2805 erel
+= input_rel_hdr
->sh_entsize
;
2808 /* Bump the counter, so that we know where to add the next set of
2810 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2815 /* Make weak undefined symbols in PIE dynamic. */
2818 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2819 struct elf_link_hash_entry
*h
)
2821 if (bfd_link_pie (info
)
2823 && h
->root
.type
== bfd_link_hash_undefweak
)
2824 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2829 /* Fix up the flags for a symbol. This handles various cases which
2830 can only be fixed after all the input files are seen. This is
2831 currently called by both adjust_dynamic_symbol and
2832 assign_sym_version, which is unnecessary but perhaps more robust in
2833 the face of future changes. */
2836 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2837 struct elf_info_failed
*eif
)
2839 const struct elf_backend_data
*bed
;
2841 /* If this symbol was mentioned in a non-ELF file, try to set
2842 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2843 permit a non-ELF file to correctly refer to a symbol defined in
2844 an ELF dynamic object. */
2847 while (h
->root
.type
== bfd_link_hash_indirect
)
2848 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2850 if (h
->root
.type
!= bfd_link_hash_defined
2851 && h
->root
.type
!= bfd_link_hash_defweak
)
2854 h
->ref_regular_nonweak
= 1;
2858 if (h
->root
.u
.def
.section
->owner
!= NULL
2859 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2860 == bfd_target_elf_flavour
))
2863 h
->ref_regular_nonweak
= 1;
2869 if (h
->dynindx
== -1
2873 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2882 /* Unfortunately, NON_ELF is only correct if the symbol
2883 was first seen in a non-ELF file. Fortunately, if the symbol
2884 was first seen in an ELF file, we're probably OK unless the
2885 symbol was defined in a non-ELF file. Catch that case here.
2886 FIXME: We're still in trouble if the symbol was first seen in
2887 a dynamic object, and then later in a non-ELF regular object. */
2888 if ((h
->root
.type
== bfd_link_hash_defined
2889 || h
->root
.type
== bfd_link_hash_defweak
)
2891 && (h
->root
.u
.def
.section
->owner
!= NULL
2892 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2893 != bfd_target_elf_flavour
)
2894 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2895 && !h
->def_dynamic
)))
2899 /* Backend specific symbol fixup. */
2900 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2901 if (bed
->elf_backend_fixup_symbol
2902 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2905 /* If this is a final link, and the symbol was defined as a common
2906 symbol in a regular object file, and there was no definition in
2907 any dynamic object, then the linker will have allocated space for
2908 the symbol in a common section but the DEF_REGULAR
2909 flag will not have been set. */
2910 if (h
->root
.type
== bfd_link_hash_defined
2914 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2917 /* Symbols defined in discarded sections shouldn't be dynamic. */
2918 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
2919 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2921 /* If a weak undefined symbol has non-default visibility, we also
2922 hide it from the dynamic linker. */
2923 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2924 && h
->root
.type
== bfd_link_hash_undefweak
)
2925 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2927 /* A hidden versioned symbol in executable should be forced local if
2928 it is is locally defined, not referenced by shared library and not
2930 else if (bfd_link_executable (eif
->info
)
2931 && h
->versioned
== versioned_hidden
2932 && !eif
->info
->export_dynamic
2936 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2938 /* If -Bsymbolic was used (which means to bind references to global
2939 symbols to the definition within the shared object), and this
2940 symbol was defined in a regular object, then it actually doesn't
2941 need a PLT entry. Likewise, if the symbol has non-default
2942 visibility. If the symbol has hidden or internal visibility, we
2943 will force it local. */
2944 else if (h
->needs_plt
2945 && bfd_link_pic (eif
->info
)
2946 && is_elf_hash_table (eif
->info
->hash
)
2947 && (SYMBOLIC_BIND (eif
->info
, h
)
2948 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2951 bfd_boolean force_local
;
2953 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2954 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2955 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2958 /* If this is a weak defined symbol in a dynamic object, and we know
2959 the real definition in the dynamic object, copy interesting flags
2960 over to the real definition. */
2961 if (h
->is_weakalias
)
2963 struct elf_link_hash_entry
*def
= weakdef (h
);
2965 /* If the real definition is defined by a regular object file,
2966 don't do anything special. See the longer description in
2967 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2968 bfd_link_hash_defined as it was when put on the alias list
2969 then it must have originally been a versioned symbol (for
2970 which a non-versioned indirect symbol is created) and later
2971 a definition for the non-versioned symbol is found. In that
2972 case the indirection is flipped with the versioned symbol
2973 becoming an indirect pointing at the non-versioned symbol.
2974 Thus, not an alias any more. */
2975 if (def
->def_regular
2976 || def
->root
.type
!= bfd_link_hash_defined
)
2979 while ((h
= h
->u
.alias
) != def
)
2980 h
->is_weakalias
= 0;
2984 while (h
->root
.type
== bfd_link_hash_indirect
)
2985 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2986 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2987 || h
->root
.type
== bfd_link_hash_defweak
);
2988 BFD_ASSERT (def
->def_dynamic
);
2989 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
2996 /* Make the backend pick a good value for a dynamic symbol. This is
2997 called via elf_link_hash_traverse, and also calls itself
3001 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
3003 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3004 struct elf_link_hash_table
*htab
;
3005 const struct elf_backend_data
*bed
;
3007 if (! is_elf_hash_table (eif
->info
->hash
))
3010 /* Ignore indirect symbols. These are added by the versioning code. */
3011 if (h
->root
.type
== bfd_link_hash_indirect
)
3014 /* Fix the symbol flags. */
3015 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
3018 htab
= elf_hash_table (eif
->info
);
3019 bed
= get_elf_backend_data (htab
->dynobj
);
3021 if (h
->root
.type
== bfd_link_hash_undefweak
)
3023 if (eif
->info
->dynamic_undefined_weak
== 0)
3024 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
3025 else if (eif
->info
->dynamic_undefined_weak
> 0
3027 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3028 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
3029 h
->root
.root
.string
))
3031 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3039 /* If this symbol does not require a PLT entry, and it is not
3040 defined by a dynamic object, or is not referenced by a regular
3041 object, ignore it. We do have to handle a weak defined symbol,
3042 even if no regular object refers to it, if we decided to add it
3043 to the dynamic symbol table. FIXME: Do we normally need to worry
3044 about symbols which are defined by one dynamic object and
3045 referenced by another one? */
3047 && h
->type
!= STT_GNU_IFUNC
3051 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3053 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3057 /* If we've already adjusted this symbol, don't do it again. This
3058 can happen via a recursive call. */
3059 if (h
->dynamic_adjusted
)
3062 /* Don't look at this symbol again. Note that we must set this
3063 after checking the above conditions, because we may look at a
3064 symbol once, decide not to do anything, and then get called
3065 recursively later after REF_REGULAR is set below. */
3066 h
->dynamic_adjusted
= 1;
3068 /* If this is a weak definition, and we know a real definition, and
3069 the real symbol is not itself defined by a regular object file,
3070 then get a good value for the real definition. We handle the
3071 real symbol first, for the convenience of the backend routine.
3073 Note that there is a confusing case here. If the real definition
3074 is defined by a regular object file, we don't get the real symbol
3075 from the dynamic object, but we do get the weak symbol. If the
3076 processor backend uses a COPY reloc, then if some routine in the
3077 dynamic object changes the real symbol, we will not see that
3078 change in the corresponding weak symbol. This is the way other
3079 ELF linkers work as well, and seems to be a result of the shared
3082 I will clarify this issue. Most SVR4 shared libraries define the
3083 variable _timezone and define timezone as a weak synonym. The
3084 tzset call changes _timezone. If you write
3085 extern int timezone;
3087 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3088 you might expect that, since timezone is a synonym for _timezone,
3089 the same number will print both times. However, if the processor
3090 backend uses a COPY reloc, then actually timezone will be copied
3091 into your process image, and, since you define _timezone
3092 yourself, _timezone will not. Thus timezone and _timezone will
3093 wind up at different memory locations. The tzset call will set
3094 _timezone, leaving timezone unchanged. */
3096 if (h
->is_weakalias
)
3098 struct elf_link_hash_entry
*def
= weakdef (h
);
3100 /* If we get to this point, there is an implicit reference to
3101 the alias by a regular object file via the weak symbol H. */
3102 def
->ref_regular
= 1;
3104 /* Ensure that the backend adjust_dynamic_symbol function sees
3105 the strong alias before H by recursively calling ourselves. */
3106 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3110 /* If a symbol has no type and no size and does not require a PLT
3111 entry, then we are probably about to do the wrong thing here: we
3112 are probably going to create a COPY reloc for an empty object.
3113 This case can arise when a shared object is built with assembly
3114 code, and the assembly code fails to set the symbol type. */
3116 && h
->type
== STT_NOTYPE
3119 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3120 h
->root
.root
.string
);
3122 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3131 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3135 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3136 struct elf_link_hash_entry
*h
,
3139 unsigned int power_of_two
;
3141 asection
*sec
= h
->root
.u
.def
.section
;
3143 /* The section alignment of the definition is the maximum alignment
3144 requirement of symbols defined in the section. Since we don't
3145 know the symbol alignment requirement, we start with the
3146 maximum alignment and check low bits of the symbol address
3147 for the minimum alignment. */
3148 power_of_two
= bfd_section_alignment (sec
);
3149 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3150 while ((h
->root
.u
.def
.value
& mask
) != 0)
3156 if (power_of_two
> bfd_section_alignment (dynbss
))
3158 /* Adjust the section alignment if needed. */
3159 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3163 /* We make sure that the symbol will be aligned properly. */
3164 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3166 /* Define the symbol as being at this point in DYNBSS. */
3167 h
->root
.u
.def
.section
= dynbss
;
3168 h
->root
.u
.def
.value
= dynbss
->size
;
3170 /* Increment the size of DYNBSS to make room for the symbol. */
3171 dynbss
->size
+= h
->size
;
3173 /* No error if extern_protected_data is true. */
3174 if (h
->protected_def
3175 && (!info
->extern_protected_data
3176 || (info
->extern_protected_data
< 0
3177 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3178 info
->callbacks
->einfo
3179 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3180 h
->root
.root
.string
);
3185 /* Adjust all external symbols pointing into SEC_MERGE sections
3186 to reflect the object merging within the sections. */
3189 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3193 if ((h
->root
.type
== bfd_link_hash_defined
3194 || h
->root
.type
== bfd_link_hash_defweak
)
3195 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3196 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3198 bfd
*output_bfd
= (bfd
*) data
;
3200 h
->root
.u
.def
.value
=
3201 _bfd_merged_section_offset (output_bfd
,
3202 &h
->root
.u
.def
.section
,
3203 elf_section_data (sec
)->sec_info
,
3204 h
->root
.u
.def
.value
);
3210 /* Returns false if the symbol referred to by H should be considered
3211 to resolve local to the current module, and true if it should be
3212 considered to bind dynamically. */
3215 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3216 struct bfd_link_info
*info
,
3217 bfd_boolean not_local_protected
)
3219 bfd_boolean binding_stays_local_p
;
3220 const struct elf_backend_data
*bed
;
3221 struct elf_link_hash_table
*hash_table
;
3226 while (h
->root
.type
== bfd_link_hash_indirect
3227 || h
->root
.type
== bfd_link_hash_warning
)
3228 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3230 /* If it was forced local, then clearly it's not dynamic. */
3231 if (h
->dynindx
== -1)
3233 if (h
->forced_local
)
3236 /* Identify the cases where name binding rules say that a
3237 visible symbol resolves locally. */
3238 binding_stays_local_p
= (bfd_link_executable (info
)
3239 || SYMBOLIC_BIND (info
, h
));
3241 switch (ELF_ST_VISIBILITY (h
->other
))
3248 hash_table
= elf_hash_table (info
);
3249 if (!is_elf_hash_table (hash_table
))
3252 bed
= get_elf_backend_data (hash_table
->dynobj
);
3254 /* Proper resolution for function pointer equality may require
3255 that these symbols perhaps be resolved dynamically, even though
3256 we should be resolving them to the current module. */
3257 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3258 binding_stays_local_p
= TRUE
;
3265 /* If it isn't defined locally, then clearly it's dynamic. */
3266 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3269 /* Otherwise, the symbol is dynamic if binding rules don't tell
3270 us that it remains local. */
3271 return !binding_stays_local_p
;
3274 /* Return true if the symbol referred to by H should be considered
3275 to resolve local to the current module, and false otherwise. Differs
3276 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3277 undefined symbols. The two functions are virtually identical except
3278 for the place where dynindx == -1 is tested. If that test is true,
3279 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3280 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3282 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3283 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3284 treatment of undefined weak symbols. For those that do not make
3285 undefined weak symbols dynamic, both functions may return false. */
3288 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3289 struct bfd_link_info
*info
,
3290 bfd_boolean local_protected
)
3292 const struct elf_backend_data
*bed
;
3293 struct elf_link_hash_table
*hash_table
;
3295 /* If it's a local sym, of course we resolve locally. */
3299 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3300 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3301 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3304 /* Forced local symbols resolve locally. */
3305 if (h
->forced_local
)
3308 /* Common symbols that become definitions don't get the DEF_REGULAR
3309 flag set, so test it first, and don't bail out. */
3310 if (ELF_COMMON_DEF_P (h
))
3312 /* If we don't have a definition in a regular file, then we can't
3313 resolve locally. The sym is either undefined or dynamic. */
3314 else if (!h
->def_regular
)
3317 /* Non-dynamic symbols resolve locally. */
3318 if (h
->dynindx
== -1)
3321 /* At this point, we know the symbol is defined and dynamic. In an
3322 executable it must resolve locally, likewise when building symbolic
3323 shared libraries. */
3324 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3327 /* Now deal with defined dynamic symbols in shared libraries. Ones
3328 with default visibility might not resolve locally. */
3329 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3332 hash_table
= elf_hash_table (info
);
3333 if (!is_elf_hash_table (hash_table
))
3336 bed
= get_elf_backend_data (hash_table
->dynobj
);
3338 /* If extern_protected_data is false, STV_PROTECTED non-function
3339 symbols are local. */
3340 if ((!info
->extern_protected_data
3341 || (info
->extern_protected_data
< 0
3342 && !bed
->extern_protected_data
))
3343 && !bed
->is_function_type (h
->type
))
3346 /* Function pointer equality tests may require that STV_PROTECTED
3347 symbols be treated as dynamic symbols. If the address of a
3348 function not defined in an executable is set to that function's
3349 plt entry in the executable, then the address of the function in
3350 a shared library must also be the plt entry in the executable. */
3351 return local_protected
;
3354 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3355 aligned. Returns the first TLS output section. */
3357 struct bfd_section
*
3358 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3360 struct bfd_section
*sec
, *tls
;
3361 unsigned int align
= 0;
3363 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3364 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3368 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3369 if (sec
->alignment_power
> align
)
3370 align
= sec
->alignment_power
;
3372 elf_hash_table (info
)->tls_sec
= tls
;
3374 /* Ensure the alignment of the first section (usually .tdata) is the largest
3375 alignment, so that the tls segment starts aligned. */
3377 tls
->alignment_power
= align
;
3382 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3384 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3385 Elf_Internal_Sym
*sym
)
3387 const struct elf_backend_data
*bed
;
3389 /* Local symbols do not count, but target specific ones might. */
3390 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3391 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3394 bed
= get_elf_backend_data (abfd
);
3395 /* Function symbols do not count. */
3396 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3399 /* If the section is undefined, then so is the symbol. */
3400 if (sym
->st_shndx
== SHN_UNDEF
)
3403 /* If the symbol is defined in the common section, then
3404 it is a common definition and so does not count. */
3405 if (bed
->common_definition (sym
))
3408 /* If the symbol is in a target specific section then we
3409 must rely upon the backend to tell us what it is. */
3410 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3411 /* FIXME - this function is not coded yet:
3413 return _bfd_is_global_symbol_definition (abfd, sym);
3415 Instead for now assume that the definition is not global,
3416 Even if this is wrong, at least the linker will behave
3417 in the same way that it used to do. */
3423 /* Search the symbol table of the archive element of the archive ABFD
3424 whose archive map contains a mention of SYMDEF, and determine if
3425 the symbol is defined in this element. */
3427 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3429 Elf_Internal_Shdr
* hdr
;
3433 Elf_Internal_Sym
*isymbuf
;
3434 Elf_Internal_Sym
*isym
;
3435 Elf_Internal_Sym
*isymend
;
3438 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3442 if (! bfd_check_format (abfd
, bfd_object
))
3445 /* Select the appropriate symbol table. If we don't know if the
3446 object file is an IR object, give linker LTO plugin a chance to
3447 get the correct symbol table. */
3448 if (abfd
->plugin_format
== bfd_plugin_yes
3449 #if BFD_SUPPORTS_PLUGINS
3450 || (abfd
->plugin_format
== bfd_plugin_unknown
3451 && bfd_link_plugin_object_p (abfd
))
3455 /* Use the IR symbol table if the object has been claimed by
3457 abfd
= abfd
->plugin_dummy_bfd
;
3458 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3460 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3461 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3463 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3465 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3467 /* The sh_info field of the symtab header tells us where the
3468 external symbols start. We don't care about the local symbols. */
3469 if (elf_bad_symtab (abfd
))
3471 extsymcount
= symcount
;
3476 extsymcount
= symcount
- hdr
->sh_info
;
3477 extsymoff
= hdr
->sh_info
;
3480 if (extsymcount
== 0)
3483 /* Read in the symbol table. */
3484 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3486 if (isymbuf
== NULL
)
3489 /* Scan the symbol table looking for SYMDEF. */
3491 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3495 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3500 if (strcmp (name
, symdef
->name
) == 0)
3502 result
= is_global_data_symbol_definition (abfd
, isym
);
3512 /* Add an entry to the .dynamic table. */
3515 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3519 struct elf_link_hash_table
*hash_table
;
3520 const struct elf_backend_data
*bed
;
3522 bfd_size_type newsize
;
3523 bfd_byte
*newcontents
;
3524 Elf_Internal_Dyn dyn
;
3526 hash_table
= elf_hash_table (info
);
3527 if (! is_elf_hash_table (hash_table
))
3530 if (tag
== DT_RELA
|| tag
== DT_REL
)
3531 hash_table
->dynamic_relocs
= TRUE
;
3533 bed
= get_elf_backend_data (hash_table
->dynobj
);
3534 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3535 BFD_ASSERT (s
!= NULL
);
3537 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3538 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3539 if (newcontents
== NULL
)
3543 dyn
.d_un
.d_val
= val
;
3544 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3547 s
->contents
= newcontents
;
3552 /* Strip zero-sized dynamic sections. */
3555 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3557 struct elf_link_hash_table
*hash_table
;
3558 const struct elf_backend_data
*bed
;
3559 asection
*s
, *sdynamic
, **pp
;
3560 asection
*rela_dyn
, *rel_dyn
;
3561 Elf_Internal_Dyn dyn
;
3562 bfd_byte
*extdyn
, *next
;
3563 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3564 bfd_boolean strip_zero_sized
;
3565 bfd_boolean strip_zero_sized_plt
;
3567 if (bfd_link_relocatable (info
))
3570 hash_table
= elf_hash_table (info
);
3571 if (!is_elf_hash_table (hash_table
))
3574 if (!hash_table
->dynobj
)
3577 sdynamic
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3581 bed
= get_elf_backend_data (hash_table
->dynobj
);
3582 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3584 strip_zero_sized
= FALSE
;
3585 strip_zero_sized_plt
= FALSE
;
3587 /* Strip zero-sized dynamic sections. */
3588 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3589 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3590 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3594 || s
== hash_table
->srelplt
->output_section
3595 || s
== hash_table
->splt
->output_section
))
3598 info
->output_bfd
->section_count
--;
3599 strip_zero_sized
= TRUE
;
3604 else if (s
== hash_table
->splt
->output_section
)
3606 s
= hash_table
->splt
;
3607 strip_zero_sized_plt
= TRUE
;
3610 s
= hash_table
->srelplt
;
3611 s
->flags
|= SEC_EXCLUDE
;
3612 s
->output_section
= bfd_abs_section_ptr
;
3617 if (strip_zero_sized_plt
)
3618 for (extdyn
= sdynamic
->contents
;
3619 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3622 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3623 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3631 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3632 the procedure linkage table (the .plt section) has been
3634 memmove (extdyn
, next
,
3635 sdynamic
->size
- (next
- sdynamic
->contents
));
3640 if (strip_zero_sized
)
3642 /* Regenerate program headers. */
3643 elf_seg_map (info
->output_bfd
) = NULL
;
3644 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
);
3650 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3651 1 if a DT_NEEDED tag already exists, and 0 on success. */
3654 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3656 struct elf_link_hash_table
*hash_table
;
3660 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3663 hash_table
= elf_hash_table (info
);
3664 soname
= elf_dt_name (abfd
);
3665 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3666 if (strindex
== (size_t) -1)
3669 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3672 const struct elf_backend_data
*bed
;
3675 bed
= get_elf_backend_data (hash_table
->dynobj
);
3676 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3678 for (extdyn
= sdyn
->contents
;
3679 extdyn
< sdyn
->contents
+ sdyn
->size
;
3680 extdyn
+= bed
->s
->sizeof_dyn
)
3682 Elf_Internal_Dyn dyn
;
3684 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3685 if (dyn
.d_tag
== DT_NEEDED
3686 && dyn
.d_un
.d_val
== strindex
)
3688 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3694 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3697 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3703 /* Return true if SONAME is on the needed list between NEEDED and STOP
3704 (or the end of list if STOP is NULL), and needed by a library that
3708 on_needed_list (const char *soname
,
3709 struct bfd_link_needed_list
*needed
,
3710 struct bfd_link_needed_list
*stop
)
3712 struct bfd_link_needed_list
*look
;
3713 for (look
= needed
; look
!= stop
; look
= look
->next
)
3714 if (strcmp (soname
, look
->name
) == 0
3715 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3716 /* If needed by a library that itself is not directly
3717 needed, recursively check whether that library is
3718 indirectly needed. Since we add DT_NEEDED entries to
3719 the end of the list, library dependencies appear after
3720 the library. Therefore search prior to the current
3721 LOOK, preventing possible infinite recursion. */
3722 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3728 /* Sort symbol by value, section, size, and type. */
3730 elf_sort_symbol (const void *arg1
, const void *arg2
)
3732 const struct elf_link_hash_entry
*h1
;
3733 const struct elf_link_hash_entry
*h2
;
3734 bfd_signed_vma vdiff
;
3739 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3740 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3741 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3743 return vdiff
> 0 ? 1 : -1;
3745 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3749 /* Sort so that sized symbols are selected over zero size symbols. */
3750 vdiff
= h1
->size
- h2
->size
;
3752 return vdiff
> 0 ? 1 : -1;
3754 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3755 if (h1
->type
!= h2
->type
)
3756 return h1
->type
- h2
->type
;
3758 /* If symbols are properly sized and typed, and multiple strong
3759 aliases are not defined in a shared library by the user we
3760 shouldn't get here. Unfortunately linker script symbols like
3761 __bss_start sometimes match a user symbol defined at the start of
3762 .bss without proper size and type. We'd like to preference the
3763 user symbol over reserved system symbols. Sort on leading
3765 n1
= h1
->root
.root
.string
;
3766 n2
= h2
->root
.root
.string
;
3779 /* Final sort on name selects user symbols like '_u' over reserved
3780 system symbols like '_Z' and also will avoid qsort instability. */
3784 /* This function is used to adjust offsets into .dynstr for
3785 dynamic symbols. This is called via elf_link_hash_traverse. */
3788 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3790 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3792 if (h
->dynindx
!= -1)
3793 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3797 /* Assign string offsets in .dynstr, update all structures referencing
3801 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3803 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3804 struct elf_link_local_dynamic_entry
*entry
;
3805 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3806 bfd
*dynobj
= hash_table
->dynobj
;
3809 const struct elf_backend_data
*bed
;
3812 _bfd_elf_strtab_finalize (dynstr
);
3813 size
= _bfd_elf_strtab_size (dynstr
);
3815 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3817 if (info
->callbacks
->examine_strtab
)
3818 info
->callbacks
->examine_strtab (dynstr
);
3820 bed
= get_elf_backend_data (dynobj
);
3821 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3822 BFD_ASSERT (sdyn
!= NULL
);
3824 /* Update all .dynamic entries referencing .dynstr strings. */
3825 for (extdyn
= sdyn
->contents
;
3826 extdyn
< sdyn
->contents
+ sdyn
->size
;
3827 extdyn
+= bed
->s
->sizeof_dyn
)
3829 Elf_Internal_Dyn dyn
;
3831 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3835 dyn
.d_un
.d_val
= size
;
3845 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3850 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3853 /* Now update local dynamic symbols. */
3854 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3855 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3856 entry
->isym
.st_name
);
3858 /* And the rest of dynamic symbols. */
3859 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3861 /* Adjust version definitions. */
3862 if (elf_tdata (output_bfd
)->cverdefs
)
3867 Elf_Internal_Verdef def
;
3868 Elf_Internal_Verdaux defaux
;
3870 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3874 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3876 p
+= sizeof (Elf_External_Verdef
);
3877 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3879 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3881 _bfd_elf_swap_verdaux_in (output_bfd
,
3882 (Elf_External_Verdaux
*) p
, &defaux
);
3883 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3885 _bfd_elf_swap_verdaux_out (output_bfd
,
3886 &defaux
, (Elf_External_Verdaux
*) p
);
3887 p
+= sizeof (Elf_External_Verdaux
);
3890 while (def
.vd_next
);
3893 /* Adjust version references. */
3894 if (elf_tdata (output_bfd
)->verref
)
3899 Elf_Internal_Verneed need
;
3900 Elf_Internal_Vernaux needaux
;
3902 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3906 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3908 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3909 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3910 (Elf_External_Verneed
*) p
);
3911 p
+= sizeof (Elf_External_Verneed
);
3912 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3914 _bfd_elf_swap_vernaux_in (output_bfd
,
3915 (Elf_External_Vernaux
*) p
, &needaux
);
3916 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3918 _bfd_elf_swap_vernaux_out (output_bfd
,
3920 (Elf_External_Vernaux
*) p
);
3921 p
+= sizeof (Elf_External_Vernaux
);
3924 while (need
.vn_next
);
3930 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3931 The default is to only match when the INPUT and OUTPUT are exactly
3935 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3936 const bfd_target
*output
)
3938 return input
== output
;
3941 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3942 This version is used when different targets for the same architecture
3943 are virtually identical. */
3946 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3947 const bfd_target
*output
)
3949 const struct elf_backend_data
*obed
, *ibed
;
3951 if (input
== output
)
3954 ibed
= xvec_get_elf_backend_data (input
);
3955 obed
= xvec_get_elf_backend_data (output
);
3957 if (ibed
->arch
!= obed
->arch
)
3960 /* If both backends are using this function, deem them compatible. */
3961 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3964 /* Make a special call to the linker "notice" function to tell it that
3965 we are about to handle an as-needed lib, or have finished
3966 processing the lib. */
3969 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3970 struct bfd_link_info
*info
,
3971 enum notice_asneeded_action act
)
3973 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3976 /* Check relocations an ELF object file. */
3979 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3981 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3982 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3984 /* If this object is the same format as the output object, and it is
3985 not a shared library, then let the backend look through the
3988 This is required to build global offset table entries and to
3989 arrange for dynamic relocs. It is not required for the
3990 particular common case of linking non PIC code, even when linking
3991 against shared libraries, but unfortunately there is no way of
3992 knowing whether an object file has been compiled PIC or not.
3993 Looking through the relocs is not particularly time consuming.
3994 The problem is that we must either (1) keep the relocs in memory,
3995 which causes the linker to require additional runtime memory or
3996 (2) read the relocs twice from the input file, which wastes time.
3997 This would be a good case for using mmap.
3999 I have no idea how to handle linking PIC code into a file of a
4000 different format. It probably can't be done. */
4001 if ((abfd
->flags
& DYNAMIC
) == 0
4002 && is_elf_hash_table (htab
)
4003 && bed
->check_relocs
!= NULL
4004 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4005 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4009 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4011 Elf_Internal_Rela
*internal_relocs
;
4014 /* Don't check relocations in excluded sections. Don't do
4015 anything special with non-loaded, non-alloced sections.
4016 In particular, any relocs in such sections should not
4017 affect GOT and PLT reference counting (ie. we don't
4018 allow them to create GOT or PLT entries), there's no
4019 possibility or desire to optimize TLS relocs, and
4020 there's not much point in propagating relocs to shared
4021 libs that the dynamic linker won't relocate. */
4022 if ((o
->flags
& SEC_ALLOC
) == 0
4023 || (o
->flags
& SEC_RELOC
) == 0
4024 || (o
->flags
& SEC_EXCLUDE
) != 0
4025 || o
->reloc_count
== 0
4026 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4027 && (o
->flags
& SEC_DEBUGGING
) != 0)
4028 || bfd_is_abs_section (o
->output_section
))
4031 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4033 if (internal_relocs
== NULL
)
4036 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4038 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4039 free (internal_relocs
);
4049 /* Add symbols from an ELF object file to the linker hash table. */
4052 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4054 Elf_Internal_Ehdr
*ehdr
;
4055 Elf_Internal_Shdr
*hdr
;
4059 struct elf_link_hash_entry
**sym_hash
;
4060 bfd_boolean dynamic
;
4061 Elf_External_Versym
*extversym
= NULL
;
4062 Elf_External_Versym
*extversym_end
= NULL
;
4063 Elf_External_Versym
*ever
;
4064 struct elf_link_hash_entry
*weaks
;
4065 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4066 size_t nondeflt_vers_cnt
= 0;
4067 Elf_Internal_Sym
*isymbuf
= NULL
;
4068 Elf_Internal_Sym
*isym
;
4069 Elf_Internal_Sym
*isymend
;
4070 const struct elf_backend_data
*bed
;
4071 bfd_boolean add_needed
;
4072 struct elf_link_hash_table
*htab
;
4073 void *alloc_mark
= NULL
;
4074 struct bfd_hash_entry
**old_table
= NULL
;
4075 unsigned int old_size
= 0;
4076 unsigned int old_count
= 0;
4077 void *old_tab
= NULL
;
4079 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4080 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4081 void *old_strtab
= NULL
;
4084 bfd_boolean just_syms
;
4086 htab
= elf_hash_table (info
);
4087 bed
= get_elf_backend_data (abfd
);
4089 if ((abfd
->flags
& DYNAMIC
) == 0)
4095 /* You can't use -r against a dynamic object. Also, there's no
4096 hope of using a dynamic object which does not exactly match
4097 the format of the output file. */
4098 if (bfd_link_relocatable (info
)
4099 || !is_elf_hash_table (htab
)
4100 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4102 if (bfd_link_relocatable (info
))
4103 bfd_set_error (bfd_error_invalid_operation
);
4105 bfd_set_error (bfd_error_wrong_format
);
4110 ehdr
= elf_elfheader (abfd
);
4111 if (info
->warn_alternate_em
4112 && bed
->elf_machine_code
!= ehdr
->e_machine
4113 && ((bed
->elf_machine_alt1
!= 0
4114 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4115 || (bed
->elf_machine_alt2
!= 0
4116 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4118 /* xgettext:c-format */
4119 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4120 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4122 /* As a GNU extension, any input sections which are named
4123 .gnu.warning.SYMBOL are treated as warning symbols for the given
4124 symbol. This differs from .gnu.warning sections, which generate
4125 warnings when they are included in an output file. */
4126 /* PR 12761: Also generate this warning when building shared libraries. */
4127 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4131 name
= bfd_section_name (s
);
4132 if (CONST_STRNEQ (name
, ".gnu.warning."))
4137 name
+= sizeof ".gnu.warning." - 1;
4139 /* If this is a shared object, then look up the symbol
4140 in the hash table. If it is there, and it is already
4141 been defined, then we will not be using the entry
4142 from this shared object, so we don't need to warn.
4143 FIXME: If we see the definition in a regular object
4144 later on, we will warn, but we shouldn't. The only
4145 fix is to keep track of what warnings we are supposed
4146 to emit, and then handle them all at the end of the
4150 struct elf_link_hash_entry
*h
;
4152 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
4154 /* FIXME: What about bfd_link_hash_common? */
4156 && (h
->root
.type
== bfd_link_hash_defined
4157 || h
->root
.type
== bfd_link_hash_defweak
))
4162 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4166 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4171 if (! (_bfd_generic_link_add_one_symbol
4172 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4173 FALSE
, bed
->collect
, NULL
)))
4176 if (bfd_link_executable (info
))
4178 /* Clobber the section size so that the warning does
4179 not get copied into the output file. */
4182 /* Also set SEC_EXCLUDE, so that symbols defined in
4183 the warning section don't get copied to the output. */
4184 s
->flags
|= SEC_EXCLUDE
;
4189 just_syms
= ((s
= abfd
->sections
) != NULL
4190 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4195 /* If we are creating a shared library, create all the dynamic
4196 sections immediately. We need to attach them to something,
4197 so we attach them to this BFD, provided it is the right
4198 format and is not from ld --just-symbols. Always create the
4199 dynamic sections for -E/--dynamic-list. FIXME: If there
4200 are no input BFD's of the same format as the output, we can't
4201 make a shared library. */
4203 && (bfd_link_pic (info
)
4204 || (!bfd_link_relocatable (info
)
4206 && (info
->export_dynamic
|| info
->dynamic
)))
4207 && is_elf_hash_table (htab
)
4208 && info
->output_bfd
->xvec
== abfd
->xvec
4209 && !htab
->dynamic_sections_created
)
4211 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4215 else if (!is_elf_hash_table (htab
))
4219 const char *soname
= NULL
;
4221 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4222 const Elf_Internal_Phdr
*phdr
;
4223 struct elf_link_loaded_list
*loaded_lib
;
4225 /* ld --just-symbols and dynamic objects don't mix very well.
4226 ld shouldn't allow it. */
4230 /* If this dynamic lib was specified on the command line with
4231 --as-needed in effect, then we don't want to add a DT_NEEDED
4232 tag unless the lib is actually used. Similary for libs brought
4233 in by another lib's DT_NEEDED. When --no-add-needed is used
4234 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4235 any dynamic library in DT_NEEDED tags in the dynamic lib at
4237 add_needed
= (elf_dyn_lib_class (abfd
)
4238 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4239 | DYN_NO_NEEDED
)) == 0;
4241 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4246 unsigned int elfsec
;
4247 unsigned long shlink
;
4249 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4256 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4257 if (elfsec
== SHN_BAD
)
4258 goto error_free_dyn
;
4259 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4261 for (extdyn
= dynbuf
;
4262 extdyn
<= dynbuf
+ s
->size
- bed
->s
->sizeof_dyn
;
4263 extdyn
+= bed
->s
->sizeof_dyn
)
4265 Elf_Internal_Dyn dyn
;
4267 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4268 if (dyn
.d_tag
== DT_SONAME
)
4270 unsigned int tagv
= dyn
.d_un
.d_val
;
4271 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4273 goto error_free_dyn
;
4275 if (dyn
.d_tag
== DT_NEEDED
)
4277 struct bfd_link_needed_list
*n
, **pn
;
4279 unsigned int tagv
= dyn
.d_un
.d_val
;
4280 size_t amt
= sizeof (struct bfd_link_needed_list
);
4282 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4283 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4284 if (n
== NULL
|| fnm
== NULL
)
4285 goto error_free_dyn
;
4286 amt
= strlen (fnm
) + 1;
4287 anm
= (char *) bfd_alloc (abfd
, amt
);
4289 goto error_free_dyn
;
4290 memcpy (anm
, fnm
, amt
);
4294 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4298 if (dyn
.d_tag
== DT_RUNPATH
)
4300 struct bfd_link_needed_list
*n
, **pn
;
4302 unsigned int tagv
= dyn
.d_un
.d_val
;
4303 size_t amt
= sizeof (struct bfd_link_needed_list
);
4305 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4306 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4307 if (n
== NULL
|| fnm
== NULL
)
4308 goto error_free_dyn
;
4309 amt
= strlen (fnm
) + 1;
4310 anm
= (char *) bfd_alloc (abfd
, amt
);
4312 goto error_free_dyn
;
4313 memcpy (anm
, fnm
, amt
);
4317 for (pn
= & runpath
;
4323 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4324 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4326 struct bfd_link_needed_list
*n
, **pn
;
4328 unsigned int tagv
= dyn
.d_un
.d_val
;
4329 size_t amt
= sizeof (struct bfd_link_needed_list
);
4331 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4332 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4333 if (n
== NULL
|| fnm
== NULL
)
4334 goto error_free_dyn
;
4335 amt
= strlen (fnm
) + 1;
4336 anm
= (char *) bfd_alloc (abfd
, amt
);
4338 goto error_free_dyn
;
4339 memcpy (anm
, fnm
, amt
);
4349 if (dyn
.d_tag
== DT_AUDIT
)
4351 unsigned int tagv
= dyn
.d_un
.d_val
;
4352 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4359 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4360 frees all more recently bfd_alloc'd blocks as well. */
4366 struct bfd_link_needed_list
**pn
;
4367 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4372 /* If we have a PT_GNU_RELRO program header, mark as read-only
4373 all sections contained fully therein. This makes relro
4374 shared library sections appear as they will at run-time. */
4375 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4376 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4377 if (phdr
->p_type
== PT_GNU_RELRO
)
4379 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4381 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4383 if ((s
->flags
& SEC_ALLOC
) != 0
4384 && s
->vma
* opb
>= phdr
->p_vaddr
4385 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4386 s
->flags
|= SEC_READONLY
;
4391 /* We do not want to include any of the sections in a dynamic
4392 object in the output file. We hack by simply clobbering the
4393 list of sections in the BFD. This could be handled more
4394 cleanly by, say, a new section flag; the existing
4395 SEC_NEVER_LOAD flag is not the one we want, because that one
4396 still implies that the section takes up space in the output
4398 bfd_section_list_clear (abfd
);
4400 /* Find the name to use in a DT_NEEDED entry that refers to this
4401 object. If the object has a DT_SONAME entry, we use it.
4402 Otherwise, if the generic linker stuck something in
4403 elf_dt_name, we use that. Otherwise, we just use the file
4405 if (soname
== NULL
|| *soname
== '\0')
4407 soname
= elf_dt_name (abfd
);
4408 if (soname
== NULL
|| *soname
== '\0')
4409 soname
= bfd_get_filename (abfd
);
4412 /* Save the SONAME because sometimes the linker emulation code
4413 will need to know it. */
4414 elf_dt_name (abfd
) = soname
;
4416 /* If we have already included this dynamic object in the
4417 link, just ignore it. There is no reason to include a
4418 particular dynamic object more than once. */
4419 for (loaded_lib
= htab
->dyn_loaded
;
4421 loaded_lib
= loaded_lib
->next
)
4423 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4427 /* Create dynamic sections for backends that require that be done
4428 before setup_gnu_properties. */
4430 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4433 /* Save the DT_AUDIT entry for the linker emulation code. */
4434 elf_dt_audit (abfd
) = audit
;
4437 /* If this is a dynamic object, we always link against the .dynsym
4438 symbol table, not the .symtab symbol table. The dynamic linker
4439 will only see the .dynsym symbol table, so there is no reason to
4440 look at .symtab for a dynamic object. */
4442 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4443 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4445 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4447 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4449 /* The sh_info field of the symtab header tells us where the
4450 external symbols start. We don't care about the local symbols at
4452 if (elf_bad_symtab (abfd
))
4454 extsymcount
= symcount
;
4459 extsymcount
= symcount
- hdr
->sh_info
;
4460 extsymoff
= hdr
->sh_info
;
4463 sym_hash
= elf_sym_hashes (abfd
);
4464 if (extsymcount
!= 0)
4466 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4468 if (isymbuf
== NULL
)
4471 if (sym_hash
== NULL
)
4473 /* We store a pointer to the hash table entry for each
4475 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4476 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4477 if (sym_hash
== NULL
)
4478 goto error_free_sym
;
4479 elf_sym_hashes (abfd
) = sym_hash
;
4485 /* Read in any version definitions. */
4486 if (!_bfd_elf_slurp_version_tables (abfd
,
4487 info
->default_imported_symver
))
4488 goto error_free_sym
;
4490 /* Read in the symbol versions, but don't bother to convert them
4491 to internal format. */
4492 if (elf_dynversym (abfd
) != 0)
4494 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4495 bfd_size_type amt
= versymhdr
->sh_size
;
4497 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4498 goto error_free_sym
;
4499 extversym
= (Elf_External_Versym
*)
4500 _bfd_malloc_and_read (abfd
, amt
, amt
);
4501 if (extversym
== NULL
)
4502 goto error_free_sym
;
4503 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4507 /* If we are loading an as-needed shared lib, save the symbol table
4508 state before we start adding symbols. If the lib turns out
4509 to be unneeded, restore the state. */
4510 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4515 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4517 struct bfd_hash_entry
*p
;
4518 struct elf_link_hash_entry
*h
;
4520 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4522 h
= (struct elf_link_hash_entry
*) p
;
4523 entsize
+= htab
->root
.table
.entsize
;
4524 if (h
->root
.type
== bfd_link_hash_warning
)
4526 entsize
+= htab
->root
.table
.entsize
;
4527 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4529 if (h
->root
.type
== bfd_link_hash_common
)
4530 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4534 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4535 old_tab
= bfd_malloc (tabsize
+ entsize
);
4536 if (old_tab
== NULL
)
4537 goto error_free_vers
;
4539 /* Remember the current objalloc pointer, so that all mem for
4540 symbols added can later be reclaimed. */
4541 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4542 if (alloc_mark
== NULL
)
4543 goto error_free_vers
;
4545 /* Make a special call to the linker "notice" function to
4546 tell it that we are about to handle an as-needed lib. */
4547 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4548 goto error_free_vers
;
4550 /* Clone the symbol table. Remember some pointers into the
4551 symbol table, and dynamic symbol count. */
4552 old_ent
= (char *) old_tab
+ tabsize
;
4553 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4554 old_undefs
= htab
->root
.undefs
;
4555 old_undefs_tail
= htab
->root
.undefs_tail
;
4556 old_table
= htab
->root
.table
.table
;
4557 old_size
= htab
->root
.table
.size
;
4558 old_count
= htab
->root
.table
.count
;
4560 if (htab
->dynstr
!= NULL
)
4562 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4563 if (old_strtab
== NULL
)
4564 goto error_free_vers
;
4567 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4569 struct bfd_hash_entry
*p
;
4570 struct elf_link_hash_entry
*h
;
4572 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4574 h
= (struct elf_link_hash_entry
*) p
;
4575 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4576 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4577 if (h
->root
.type
== bfd_link_hash_warning
)
4579 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4580 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4581 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4583 if (h
->root
.type
== bfd_link_hash_common
)
4585 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4586 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4593 if (extversym
== NULL
)
4595 else if (extversym
+ extsymoff
< extversym_end
)
4596 ever
= extversym
+ extsymoff
;
4599 /* xgettext:c-format */
4600 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4601 abfd
, (long) extsymoff
,
4602 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4603 bfd_set_error (bfd_error_bad_value
);
4604 goto error_free_vers
;
4607 if (!bfd_link_relocatable (info
)
4608 && abfd
->lto_slim_object
)
4611 (_("%pB: plugin needed to handle lto object"), abfd
);
4614 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4616 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4620 asection
*sec
, *new_sec
;
4623 struct elf_link_hash_entry
*h
;
4624 struct elf_link_hash_entry
*hi
;
4625 bfd_boolean definition
;
4626 bfd_boolean size_change_ok
;
4627 bfd_boolean type_change_ok
;
4628 bfd_boolean new_weak
;
4629 bfd_boolean old_weak
;
4632 bfd_boolean discarded
;
4633 unsigned int old_alignment
;
4634 unsigned int shindex
;
4636 bfd_boolean matched
;
4640 flags
= BSF_NO_FLAGS
;
4642 value
= isym
->st_value
;
4643 common
= bed
->common_definition (isym
);
4644 if (common
&& info
->inhibit_common_definition
)
4646 /* Treat common symbol as undefined for --no-define-common. */
4647 isym
->st_shndx
= SHN_UNDEF
;
4652 bind
= ELF_ST_BIND (isym
->st_info
);
4656 /* This should be impossible, since ELF requires that all
4657 global symbols follow all local symbols, and that sh_info
4658 point to the first global symbol. Unfortunately, Irix 5
4660 if (elf_bad_symtab (abfd
))
4663 /* If we aren't prepared to handle locals within the globals
4664 then we'll likely segfault on a NULL symbol hash if the
4665 symbol is ever referenced in relocations. */
4666 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4667 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4668 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4669 " (>= sh_info of %lu)"),
4670 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4673 /* Dynamic object relocations are not processed by ld, so
4674 ld won't run into the problem mentioned above. */
4677 bfd_set_error (bfd_error_bad_value
);
4678 goto error_free_vers
;
4681 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4689 case STB_GNU_UNIQUE
:
4690 flags
= BSF_GNU_UNIQUE
;
4694 /* Leave it up to the processor backend. */
4698 if (isym
->st_shndx
== SHN_UNDEF
)
4699 sec
= bfd_und_section_ptr
;
4700 else if (isym
->st_shndx
== SHN_ABS
)
4701 sec
= bfd_abs_section_ptr
;
4702 else if (isym
->st_shndx
== SHN_COMMON
)
4704 sec
= bfd_com_section_ptr
;
4705 /* What ELF calls the size we call the value. What ELF
4706 calls the value we call the alignment. */
4707 value
= isym
->st_size
;
4711 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4713 sec
= bfd_abs_section_ptr
;
4714 else if (discarded_section (sec
))
4716 /* Symbols from discarded section are undefined. We keep
4718 sec
= bfd_und_section_ptr
;
4720 isym
->st_shndx
= SHN_UNDEF
;
4722 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4726 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4729 goto error_free_vers
;
4731 if (isym
->st_shndx
== SHN_COMMON
4732 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4734 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4738 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4740 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4742 goto error_free_vers
;
4746 else if (isym
->st_shndx
== SHN_COMMON
4747 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4748 && !bfd_link_relocatable (info
))
4750 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4754 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4755 | SEC_LINKER_CREATED
);
4756 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4758 goto error_free_vers
;
4762 else if (bed
->elf_add_symbol_hook
)
4764 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4766 goto error_free_vers
;
4768 /* The hook function sets the name to NULL if this symbol
4769 should be skipped for some reason. */
4774 /* Sanity check that all possibilities were handled. */
4778 /* Silently discard TLS symbols from --just-syms. There's
4779 no way to combine a static TLS block with a new TLS block
4780 for this executable. */
4781 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4782 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4785 if (bfd_is_und_section (sec
)
4786 || bfd_is_com_section (sec
))
4791 size_change_ok
= FALSE
;
4792 type_change_ok
= bed
->type_change_ok
;
4799 if (is_elf_hash_table (htab
))
4801 Elf_Internal_Versym iver
;
4802 unsigned int vernum
= 0;
4807 if (info
->default_imported_symver
)
4808 /* Use the default symbol version created earlier. */
4809 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4813 else if (ever
>= extversym_end
)
4815 /* xgettext:c-format */
4816 _bfd_error_handler (_("%pB: not enough version information"),
4818 bfd_set_error (bfd_error_bad_value
);
4819 goto error_free_vers
;
4822 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4824 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4826 /* If this is a hidden symbol, or if it is not version
4827 1, we append the version name to the symbol name.
4828 However, we do not modify a non-hidden absolute symbol
4829 if it is not a function, because it might be the version
4830 symbol itself. FIXME: What if it isn't? */
4831 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4833 && (!bfd_is_abs_section (sec
)
4834 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4837 size_t namelen
, verlen
, newlen
;
4840 if (isym
->st_shndx
!= SHN_UNDEF
)
4842 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4844 else if (vernum
> 1)
4846 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4853 /* xgettext:c-format */
4854 (_("%pB: %s: invalid version %u (max %d)"),
4856 elf_tdata (abfd
)->cverdefs
);
4857 bfd_set_error (bfd_error_bad_value
);
4858 goto error_free_vers
;
4863 /* We cannot simply test for the number of
4864 entries in the VERNEED section since the
4865 numbers for the needed versions do not start
4867 Elf_Internal_Verneed
*t
;
4870 for (t
= elf_tdata (abfd
)->verref
;
4874 Elf_Internal_Vernaux
*a
;
4876 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4878 if (a
->vna_other
== vernum
)
4880 verstr
= a
->vna_nodename
;
4890 /* xgettext:c-format */
4891 (_("%pB: %s: invalid needed version %d"),
4892 abfd
, name
, vernum
);
4893 bfd_set_error (bfd_error_bad_value
);
4894 goto error_free_vers
;
4898 namelen
= strlen (name
);
4899 verlen
= strlen (verstr
);
4900 newlen
= namelen
+ verlen
+ 2;
4901 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4902 && isym
->st_shndx
!= SHN_UNDEF
)
4905 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4906 if (newname
== NULL
)
4907 goto error_free_vers
;
4908 memcpy (newname
, name
, namelen
);
4909 p
= newname
+ namelen
;
4911 /* If this is a defined non-hidden version symbol,
4912 we add another @ to the name. This indicates the
4913 default version of the symbol. */
4914 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4915 && isym
->st_shndx
!= SHN_UNDEF
)
4917 memcpy (p
, verstr
, verlen
+ 1);
4922 /* If this symbol has default visibility and the user has
4923 requested we not re-export it, then mark it as hidden. */
4924 if (!bfd_is_und_section (sec
)
4927 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4928 isym
->st_other
= (STV_HIDDEN
4929 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4931 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4932 sym_hash
, &old_bfd
, &old_weak
,
4933 &old_alignment
, &skip
, &override
,
4934 &type_change_ok
, &size_change_ok
,
4936 goto error_free_vers
;
4941 /* Override a definition only if the new symbol matches the
4943 if (override
&& matched
)
4947 while (h
->root
.type
== bfd_link_hash_indirect
4948 || h
->root
.type
== bfd_link_hash_warning
)
4949 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4951 if (elf_tdata (abfd
)->verdef
!= NULL
4954 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4957 if (! (_bfd_generic_link_add_one_symbol
4958 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
4959 NULL
, FALSE
, bed
->collect
,
4960 (struct bfd_link_hash_entry
**) sym_hash
)))
4961 goto error_free_vers
;
4964 /* We need to make sure that indirect symbol dynamic flags are
4967 while (h
->root
.type
== bfd_link_hash_indirect
4968 || h
->root
.type
== bfd_link_hash_warning
)
4969 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4971 /* Setting the index to -3 tells elf_link_output_extsym that
4972 this symbol is defined in a discarded section. */
4978 new_weak
= (flags
& BSF_WEAK
) != 0;
4982 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4983 && is_elf_hash_table (htab
)
4984 && h
->u
.alias
== NULL
)
4986 /* Keep a list of all weak defined non function symbols from
4987 a dynamic object, using the alias field. Later in this
4988 function we will set the alias field to the correct
4989 value. We only put non-function symbols from dynamic
4990 objects on this list, because that happens to be the only
4991 time we need to know the normal symbol corresponding to a
4992 weak symbol, and the information is time consuming to
4993 figure out. If the alias field is not already NULL,
4994 then this symbol was already defined by some previous
4995 dynamic object, and we will be using that previous
4996 definition anyhow. */
5002 /* Set the alignment of a common symbol. */
5003 if ((common
|| bfd_is_com_section (sec
))
5004 && h
->root
.type
== bfd_link_hash_common
)
5009 align
= bfd_log2 (isym
->st_value
);
5012 /* The new symbol is a common symbol in a shared object.
5013 We need to get the alignment from the section. */
5014 align
= new_sec
->alignment_power
;
5016 if (align
> old_alignment
)
5017 h
->root
.u
.c
.p
->alignment_power
= align
;
5019 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5022 if (is_elf_hash_table (htab
))
5024 /* Set a flag in the hash table entry indicating the type of
5025 reference or definition we just found. A dynamic symbol
5026 is one which is referenced or defined by both a regular
5027 object and a shared object. */
5028 bfd_boolean dynsym
= FALSE
;
5030 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5031 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5033 /* Except for this flag to track nonweak references. */
5035 && bind
!= STB_WEAK
)
5036 h
->ref_ir_nonweak
= 1;
5043 if (bind
!= STB_WEAK
)
5044 h
->ref_regular_nonweak
= 1;
5061 hi
->ref_dynamic
= 1;
5066 hi
->def_dynamic
= 1;
5070 /* If an indirect symbol has been forced local, don't
5071 make the real symbol dynamic. */
5072 if (h
!= hi
&& hi
->forced_local
)
5076 if (bfd_link_dll (info
)
5086 && weakdef (h
)->dynindx
!= -1))
5090 /* Check to see if we need to add an indirect symbol for
5091 the default name. */
5093 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5095 && hi
->versioned
== versioned_hidden
))
5096 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5097 sec
, value
, &old_bfd
, &dynsym
))
5098 goto error_free_vers
;
5100 /* Check the alignment when a common symbol is involved. This
5101 can change when a common symbol is overridden by a normal
5102 definition or a common symbol is ignored due to the old
5103 normal definition. We need to make sure the maximum
5104 alignment is maintained. */
5105 if ((old_alignment
|| common
)
5106 && h
->root
.type
!= bfd_link_hash_common
)
5108 unsigned int common_align
;
5109 unsigned int normal_align
;
5110 unsigned int symbol_align
;
5114 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5115 || h
->root
.type
== bfd_link_hash_defweak
);
5117 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5118 if (h
->root
.u
.def
.section
->owner
!= NULL
5119 && (h
->root
.u
.def
.section
->owner
->flags
5120 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5122 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5123 if (normal_align
> symbol_align
)
5124 normal_align
= symbol_align
;
5127 normal_align
= symbol_align
;
5131 common_align
= old_alignment
;
5132 common_bfd
= old_bfd
;
5137 common_align
= bfd_log2 (isym
->st_value
);
5139 normal_bfd
= old_bfd
;
5142 if (normal_align
< common_align
)
5144 /* PR binutils/2735 */
5145 if (normal_bfd
== NULL
)
5147 /* xgettext:c-format */
5148 (_("warning: alignment %u of common symbol `%s' in %pB is"
5149 " greater than the alignment (%u) of its section %pA"),
5150 1 << common_align
, name
, common_bfd
,
5151 1 << normal_align
, h
->root
.u
.def
.section
);
5154 /* xgettext:c-format */
5155 (_("warning: alignment %u of symbol `%s' in %pB"
5156 " is smaller than %u in %pB"),
5157 1 << normal_align
, name
, normal_bfd
,
5158 1 << common_align
, common_bfd
);
5162 /* Remember the symbol size if it isn't undefined. */
5163 if (isym
->st_size
!= 0
5164 && isym
->st_shndx
!= SHN_UNDEF
5165 && (definition
|| h
->size
== 0))
5168 && h
->size
!= isym
->st_size
5169 && ! size_change_ok
)
5171 /* xgettext:c-format */
5172 (_("warning: size of symbol `%s' changed"
5173 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5174 name
, (uint64_t) h
->size
, old_bfd
,
5175 (uint64_t) isym
->st_size
, abfd
);
5177 h
->size
= isym
->st_size
;
5180 /* If this is a common symbol, then we always want H->SIZE
5181 to be the size of the common symbol. The code just above
5182 won't fix the size if a common symbol becomes larger. We
5183 don't warn about a size change here, because that is
5184 covered by --warn-common. Allow changes between different
5186 if (h
->root
.type
== bfd_link_hash_common
)
5187 h
->size
= h
->root
.u
.c
.size
;
5189 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5190 && ((definition
&& !new_weak
)
5191 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5192 || h
->type
== STT_NOTYPE
))
5194 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5196 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5198 if (type
== STT_GNU_IFUNC
5199 && (abfd
->flags
& DYNAMIC
) != 0)
5202 if (h
->type
!= type
)
5204 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5205 /* xgettext:c-format */
5207 (_("warning: type of symbol `%s' changed"
5208 " from %d to %d in %pB"),
5209 name
, h
->type
, type
, abfd
);
5215 /* Merge st_other field. */
5216 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5217 definition
, dynamic
);
5219 /* We don't want to make debug symbol dynamic. */
5221 && (sec
->flags
& SEC_DEBUGGING
)
5222 && !bfd_link_relocatable (info
))
5225 /* Nor should we make plugin symbols dynamic. */
5226 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5231 h
->target_internal
= isym
->st_target_internal
;
5232 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5235 if (definition
&& !dynamic
)
5237 char *p
= strchr (name
, ELF_VER_CHR
);
5238 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5240 /* Queue non-default versions so that .symver x, x@FOO
5241 aliases can be checked. */
5244 size_t amt
= ((isymend
- isym
+ 1)
5245 * sizeof (struct elf_link_hash_entry
*));
5247 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5249 goto error_free_vers
;
5251 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5255 if (dynsym
&& h
->dynindx
== -1)
5257 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5258 goto error_free_vers
;
5260 && weakdef (h
)->dynindx
== -1)
5262 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5263 goto error_free_vers
;
5266 else if (h
->dynindx
!= -1)
5267 /* If the symbol already has a dynamic index, but
5268 visibility says it should not be visible, turn it into
5270 switch (ELF_ST_VISIBILITY (h
->other
))
5274 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
5282 && h
->root
.type
!= bfd_link_hash_indirect
5284 && h
->ref_regular_nonweak
)
5286 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5287 && h
->ref_ir_nonweak
5288 && !info
->lto_all_symbols_read
)
5289 || (h
->ref_dynamic_nonweak
5290 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5291 && !on_needed_list (elf_dt_name (abfd
),
5292 htab
->needed
, NULL
))))
5294 const char *soname
= elf_dt_name (abfd
);
5296 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5297 h
->root
.root
.string
);
5299 /* A symbol from a library loaded via DT_NEEDED of some
5300 other library is referenced by a regular object.
5301 Add a DT_NEEDED entry for it. Issue an error if
5302 --no-add-needed is used and the reference was not
5305 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5308 /* xgettext:c-format */
5309 (_("%pB: undefined reference to symbol '%s'"),
5311 bfd_set_error (bfd_error_missing_dso
);
5312 goto error_free_vers
;
5315 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5316 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5318 /* Create dynamic sections for backends that require
5319 that be done before setup_gnu_properties. */
5320 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5327 if (info
->lto_plugin_active
5328 && !bfd_link_relocatable (info
)
5329 && (abfd
->flags
& BFD_PLUGIN
) == 0
5335 if (bed
->s
->arch_size
== 32)
5340 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5341 referenced in regular objects so that linker plugin will get
5342 the correct symbol resolution. */
5344 sym_hash
= elf_sym_hashes (abfd
);
5345 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5347 Elf_Internal_Rela
*internal_relocs
;
5348 Elf_Internal_Rela
*rel
, *relend
;
5350 /* Don't check relocations in excluded sections. */
5351 if ((s
->flags
& SEC_RELOC
) == 0
5352 || s
->reloc_count
== 0
5353 || (s
->flags
& SEC_EXCLUDE
) != 0
5354 || ((info
->strip
== strip_all
5355 || info
->strip
== strip_debugger
)
5356 && (s
->flags
& SEC_DEBUGGING
) != 0))
5359 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, s
, NULL
,
5362 if (internal_relocs
== NULL
)
5363 goto error_free_vers
;
5365 rel
= internal_relocs
;
5366 relend
= rel
+ s
->reloc_count
;
5367 for ( ; rel
< relend
; rel
++)
5369 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5370 struct elf_link_hash_entry
*h
;
5372 /* Skip local symbols. */
5373 if (r_symndx
< extsymoff
)
5376 h
= sym_hash
[r_symndx
- extsymoff
];
5378 h
->root
.non_ir_ref_regular
= 1;
5381 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5382 free (internal_relocs
);
5391 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5395 /* Restore the symbol table. */
5396 old_ent
= (char *) old_tab
+ tabsize
;
5397 memset (elf_sym_hashes (abfd
), 0,
5398 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5399 htab
->root
.table
.table
= old_table
;
5400 htab
->root
.table
.size
= old_size
;
5401 htab
->root
.table
.count
= old_count
;
5402 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5403 htab
->root
.undefs
= old_undefs
;
5404 htab
->root
.undefs_tail
= old_undefs_tail
;
5405 if (htab
->dynstr
!= NULL
)
5406 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5409 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5411 struct bfd_hash_entry
*p
;
5412 struct elf_link_hash_entry
*h
;
5413 unsigned int non_ir_ref_dynamic
;
5415 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5417 /* Preserve non_ir_ref_dynamic so that this symbol
5418 will be exported when the dynamic lib becomes needed
5419 in the second pass. */
5420 h
= (struct elf_link_hash_entry
*) p
;
5421 if (h
->root
.type
== bfd_link_hash_warning
)
5422 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5423 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5425 h
= (struct elf_link_hash_entry
*) p
;
5426 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5427 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5428 if (h
->root
.type
== bfd_link_hash_warning
)
5430 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5431 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5432 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5434 if (h
->root
.type
== bfd_link_hash_common
)
5436 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5437 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5439 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5443 /* Make a special call to the linker "notice" function to
5444 tell it that symbols added for crefs may need to be removed. */
5445 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5446 goto error_free_vers
;
5449 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5451 free (nondeflt_vers
);
5455 if (old_tab
!= NULL
)
5457 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5458 goto error_free_vers
;
5463 /* Now that all the symbols from this input file are created, if
5464 not performing a relocatable link, handle .symver foo, foo@BAR
5465 such that any relocs against foo become foo@BAR. */
5466 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5470 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5472 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5473 char *shortname
, *p
;
5476 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5478 || (h
->root
.type
!= bfd_link_hash_defined
5479 && h
->root
.type
!= bfd_link_hash_defweak
))
5482 amt
= p
- h
->root
.root
.string
;
5483 shortname
= (char *) bfd_malloc (amt
+ 1);
5485 goto error_free_vers
;
5486 memcpy (shortname
, h
->root
.root
.string
, amt
);
5487 shortname
[amt
] = '\0';
5489 hi
= (struct elf_link_hash_entry
*)
5490 bfd_link_hash_lookup (&htab
->root
, shortname
,
5491 FALSE
, FALSE
, FALSE
);
5493 && hi
->root
.type
== h
->root
.type
5494 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5495 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5497 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
5498 hi
->root
.type
= bfd_link_hash_indirect
;
5499 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5500 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5501 sym_hash
= elf_sym_hashes (abfd
);
5503 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5504 if (sym_hash
[symidx
] == hi
)
5506 sym_hash
[symidx
] = h
;
5512 free (nondeflt_vers
);
5513 nondeflt_vers
= NULL
;
5516 /* Now set the alias field correctly for all the weak defined
5517 symbols we found. The only way to do this is to search all the
5518 symbols. Since we only need the information for non functions in
5519 dynamic objects, that's the only time we actually put anything on
5520 the list WEAKS. We need this information so that if a regular
5521 object refers to a symbol defined weakly in a dynamic object, the
5522 real symbol in the dynamic object is also put in the dynamic
5523 symbols; we also must arrange for both symbols to point to the
5524 same memory location. We could handle the general case of symbol
5525 aliasing, but a general symbol alias can only be generated in
5526 assembler code, handling it correctly would be very time
5527 consuming, and other ELF linkers don't handle general aliasing
5531 struct elf_link_hash_entry
**hpp
;
5532 struct elf_link_hash_entry
**hppend
;
5533 struct elf_link_hash_entry
**sorted_sym_hash
;
5534 struct elf_link_hash_entry
*h
;
5535 size_t sym_count
, amt
;
5537 /* Since we have to search the whole symbol list for each weak
5538 defined symbol, search time for N weak defined symbols will be
5539 O(N^2). Binary search will cut it down to O(NlogN). */
5540 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5541 sorted_sym_hash
= bfd_malloc (amt
);
5542 if (sorted_sym_hash
== NULL
)
5544 sym_hash
= sorted_sym_hash
;
5545 hpp
= elf_sym_hashes (abfd
);
5546 hppend
= hpp
+ extsymcount
;
5548 for (; hpp
< hppend
; hpp
++)
5552 && h
->root
.type
== bfd_link_hash_defined
5553 && !bed
->is_function_type (h
->type
))
5561 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5564 while (weaks
!= NULL
)
5566 struct elf_link_hash_entry
*hlook
;
5569 size_t i
, j
, idx
= 0;
5572 weaks
= hlook
->u
.alias
;
5573 hlook
->u
.alias
= NULL
;
5575 if (hlook
->root
.type
!= bfd_link_hash_defined
5576 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5579 slook
= hlook
->root
.u
.def
.section
;
5580 vlook
= hlook
->root
.u
.def
.value
;
5586 bfd_signed_vma vdiff
;
5588 h
= sorted_sym_hash
[idx
];
5589 vdiff
= vlook
- h
->root
.u
.def
.value
;
5596 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5606 /* We didn't find a value/section match. */
5610 /* With multiple aliases, or when the weak symbol is already
5611 strongly defined, we have multiple matching symbols and
5612 the binary search above may land on any of them. Step
5613 one past the matching symbol(s). */
5616 h
= sorted_sym_hash
[idx
];
5617 if (h
->root
.u
.def
.section
!= slook
5618 || h
->root
.u
.def
.value
!= vlook
)
5622 /* Now look back over the aliases. Since we sorted by size
5623 as well as value and section, we'll choose the one with
5624 the largest size. */
5627 h
= sorted_sym_hash
[idx
];
5629 /* Stop if value or section doesn't match. */
5630 if (h
->root
.u
.def
.section
!= slook
5631 || h
->root
.u
.def
.value
!= vlook
)
5633 else if (h
!= hlook
)
5635 struct elf_link_hash_entry
*t
;
5638 hlook
->is_weakalias
= 1;
5640 if (t
->u
.alias
!= NULL
)
5641 while (t
->u
.alias
!= h
)
5645 /* If the weak definition is in the list of dynamic
5646 symbols, make sure the real definition is put
5648 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5650 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5653 free (sorted_sym_hash
);
5658 /* If the real definition is in the list of dynamic
5659 symbols, make sure the weak definition is put
5660 there as well. If we don't do this, then the
5661 dynamic loader might not merge the entries for the
5662 real definition and the weak definition. */
5663 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5665 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5666 goto err_free_sym_hash
;
5673 free (sorted_sym_hash
);
5676 if (bed
->check_directives
5677 && !(*bed
->check_directives
) (abfd
, info
))
5680 /* If this is a non-traditional link, try to optimize the handling
5681 of the .stab/.stabstr sections. */
5683 && ! info
->traditional_format
5684 && is_elf_hash_table (htab
)
5685 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5689 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5690 if (stabstr
!= NULL
)
5692 bfd_size_type string_offset
= 0;
5695 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5696 if (CONST_STRNEQ (stab
->name
, ".stab")
5697 && (!stab
->name
[5] ||
5698 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5699 && (stab
->flags
& SEC_MERGE
) == 0
5700 && !bfd_is_abs_section (stab
->output_section
))
5702 struct bfd_elf_section_data
*secdata
;
5704 secdata
= elf_section_data (stab
);
5705 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5706 stabstr
, &secdata
->sec_info
,
5709 if (secdata
->sec_info
)
5710 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5715 if (dynamic
&& add_needed
)
5717 /* Add this bfd to the loaded list. */
5718 struct elf_link_loaded_list
*n
;
5720 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5724 n
->next
= htab
->dyn_loaded
;
5725 htab
->dyn_loaded
= n
;
5727 if (dynamic
&& !add_needed
5728 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5729 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5736 free (nondeflt_vers
);
5744 /* Return the linker hash table entry of a symbol that might be
5745 satisfied by an archive symbol. Return -1 on error. */
5747 struct elf_link_hash_entry
*
5748 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5749 struct bfd_link_info
*info
,
5752 struct elf_link_hash_entry
*h
;
5756 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5760 /* If this is a default version (the name contains @@), look up the
5761 symbol again with only one `@' as well as without the version.
5762 The effect is that references to the symbol with and without the
5763 version will be matched by the default symbol in the archive. */
5765 p
= strchr (name
, ELF_VER_CHR
);
5766 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5769 /* First check with only one `@'. */
5770 len
= strlen (name
);
5771 copy
= (char *) bfd_alloc (abfd
, len
);
5773 return (struct elf_link_hash_entry
*) -1;
5775 first
= p
- name
+ 1;
5776 memcpy (copy
, name
, first
);
5777 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5779 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5782 /* We also need to check references to the symbol without the
5784 copy
[first
- 1] = '\0';
5785 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5786 FALSE
, FALSE
, TRUE
);
5789 bfd_release (abfd
, copy
);
5793 /* Add symbols from an ELF archive file to the linker hash table. We
5794 don't use _bfd_generic_link_add_archive_symbols because we need to
5795 handle versioned symbols.
5797 Fortunately, ELF archive handling is simpler than that done by
5798 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5799 oddities. In ELF, if we find a symbol in the archive map, and the
5800 symbol is currently undefined, we know that we must pull in that
5803 Unfortunately, we do have to make multiple passes over the symbol
5804 table until nothing further is resolved. */
5807 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5810 unsigned char *included
= NULL
;
5814 const struct elf_backend_data
*bed
;
5815 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5816 (bfd
*, struct bfd_link_info
*, const char *);
5818 if (! bfd_has_map (abfd
))
5820 /* An empty archive is a special case. */
5821 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5823 bfd_set_error (bfd_error_no_armap
);
5827 /* Keep track of all symbols we know to be already defined, and all
5828 files we know to be already included. This is to speed up the
5829 second and subsequent passes. */
5830 c
= bfd_ardata (abfd
)->symdef_count
;
5833 amt
= c
* sizeof (*included
);
5834 included
= (unsigned char *) bfd_zmalloc (amt
);
5835 if (included
== NULL
)
5838 symdefs
= bfd_ardata (abfd
)->symdefs
;
5839 bed
= get_elf_backend_data (abfd
);
5840 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5853 symdefend
= symdef
+ c
;
5854 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5856 struct elf_link_hash_entry
*h
;
5858 struct bfd_link_hash_entry
*undefs_tail
;
5863 if (symdef
->file_offset
== last
)
5869 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5870 if (h
== (struct elf_link_hash_entry
*) -1)
5876 if (h
->root
.type
== bfd_link_hash_undefined
)
5878 /* If the archive element has already been loaded then one
5879 of the symbols defined by that element might have been
5880 made undefined due to being in a discarded section. */
5884 else if (h
->root
.type
== bfd_link_hash_common
)
5886 /* We currently have a common symbol. The archive map contains
5887 a reference to this symbol, so we may want to include it. We
5888 only want to include it however, if this archive element
5889 contains a definition of the symbol, not just another common
5892 Unfortunately some archivers (including GNU ar) will put
5893 declarations of common symbols into their archive maps, as
5894 well as real definitions, so we cannot just go by the archive
5895 map alone. Instead we must read in the element's symbol
5896 table and check that to see what kind of symbol definition
5898 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5903 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5904 /* Symbol must be defined. Don't check it again. */
5909 /* We need to include this archive member. */
5910 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5911 if (element
== NULL
)
5914 if (! bfd_check_format (element
, bfd_object
))
5917 undefs_tail
= info
->hash
->undefs_tail
;
5919 if (!(*info
->callbacks
5920 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5922 if (!bfd_link_add_symbols (element
, info
))
5925 /* If there are any new undefined symbols, we need to make
5926 another pass through the archive in order to see whether
5927 they can be defined. FIXME: This isn't perfect, because
5928 common symbols wind up on undefs_tail and because an
5929 undefined symbol which is defined later on in this pass
5930 does not require another pass. This isn't a bug, but it
5931 does make the code less efficient than it could be. */
5932 if (undefs_tail
!= info
->hash
->undefs_tail
)
5935 /* Look backward to mark all symbols from this object file
5936 which we have already seen in this pass. */
5940 included
[mark
] = TRUE
;
5945 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5947 /* We mark subsequent symbols from this object file as we go
5948 on through the loop. */
5949 last
= symdef
->file_offset
;
5962 /* Given an ELF BFD, add symbols to the global hash table as
5966 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5968 switch (bfd_get_format (abfd
))
5971 return elf_link_add_object_symbols (abfd
, info
);
5973 return elf_link_add_archive_symbols (abfd
, info
);
5975 bfd_set_error (bfd_error_wrong_format
);
5980 struct hash_codes_info
5982 unsigned long *hashcodes
;
5986 /* This function will be called though elf_link_hash_traverse to store
5987 all hash value of the exported symbols in an array. */
5990 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5992 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5997 /* Ignore indirect symbols. These are added by the versioning code. */
5998 if (h
->dynindx
== -1)
6001 name
= h
->root
.root
.string
;
6002 if (h
->versioned
>= versioned
)
6004 char *p
= strchr (name
, ELF_VER_CHR
);
6007 alc
= (char *) bfd_malloc (p
- name
+ 1);
6013 memcpy (alc
, name
, p
- name
);
6014 alc
[p
- name
] = '\0';
6019 /* Compute the hash value. */
6020 ha
= bfd_elf_hash (name
);
6022 /* Store the found hash value in the array given as the argument. */
6023 *(inf
->hashcodes
)++ = ha
;
6025 /* And store it in the struct so that we can put it in the hash table
6027 h
->u
.elf_hash_value
= ha
;
6033 struct collect_gnu_hash_codes
6036 const struct elf_backend_data
*bed
;
6037 unsigned long int nsyms
;
6038 unsigned long int maskbits
;
6039 unsigned long int *hashcodes
;
6040 unsigned long int *hashval
;
6041 unsigned long int *indx
;
6042 unsigned long int *counts
;
6046 long int min_dynindx
;
6047 unsigned long int bucketcount
;
6048 unsigned long int symindx
;
6049 long int local_indx
;
6050 long int shift1
, shift2
;
6051 unsigned long int mask
;
6055 /* This function will be called though elf_link_hash_traverse to store
6056 all hash value of the exported symbols in an array. */
6059 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6061 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6066 /* Ignore indirect symbols. These are added by the versioning code. */
6067 if (h
->dynindx
== -1)
6070 /* Ignore also local symbols and undefined symbols. */
6071 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6074 name
= h
->root
.root
.string
;
6075 if (h
->versioned
>= versioned
)
6077 char *p
= strchr (name
, ELF_VER_CHR
);
6080 alc
= (char *) bfd_malloc (p
- name
+ 1);
6086 memcpy (alc
, name
, p
- name
);
6087 alc
[p
- name
] = '\0';
6092 /* Compute the hash value. */
6093 ha
= bfd_elf_gnu_hash (name
);
6095 /* Store the found hash value in the array for compute_bucket_count,
6096 and also for .dynsym reordering purposes. */
6097 s
->hashcodes
[s
->nsyms
] = ha
;
6098 s
->hashval
[h
->dynindx
] = ha
;
6100 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6101 s
->min_dynindx
= h
->dynindx
;
6107 /* This function will be called though elf_link_hash_traverse to do
6108 final dynamic symbol renumbering in case of .gnu.hash.
6109 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6110 to the translation table. */
6113 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6115 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6116 unsigned long int bucket
;
6117 unsigned long int val
;
6119 /* Ignore indirect symbols. */
6120 if (h
->dynindx
== -1)
6123 /* Ignore also local symbols and undefined symbols. */
6124 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6126 if (h
->dynindx
>= s
->min_dynindx
)
6128 if (s
->bed
->record_xhash_symbol
!= NULL
)
6130 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6134 h
->dynindx
= s
->local_indx
++;
6139 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6140 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6141 & ((s
->maskbits
>> s
->shift1
) - 1);
6142 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6144 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6145 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6146 if (s
->counts
[bucket
] == 1)
6147 /* Last element terminates the chain. */
6149 bfd_put_32 (s
->output_bfd
, val
,
6150 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6151 --s
->counts
[bucket
];
6152 if (s
->bed
->record_xhash_symbol
!= NULL
)
6154 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6156 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6159 h
->dynindx
= s
->indx
[bucket
]++;
6163 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6166 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6168 return !(h
->forced_local
6169 || h
->root
.type
== bfd_link_hash_undefined
6170 || h
->root
.type
== bfd_link_hash_undefweak
6171 || ((h
->root
.type
== bfd_link_hash_defined
6172 || h
->root
.type
== bfd_link_hash_defweak
)
6173 && h
->root
.u
.def
.section
->output_section
== NULL
));
6176 /* Array used to determine the number of hash table buckets to use
6177 based on the number of symbols there are. If there are fewer than
6178 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6179 fewer than 37 we use 17 buckets, and so forth. We never use more
6180 than 32771 buckets. */
6182 static const size_t elf_buckets
[] =
6184 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6188 /* Compute bucket count for hashing table. We do not use a static set
6189 of possible tables sizes anymore. Instead we determine for all
6190 possible reasonable sizes of the table the outcome (i.e., the
6191 number of collisions etc) and choose the best solution. The
6192 weighting functions are not too simple to allow the table to grow
6193 without bounds. Instead one of the weighting factors is the size.
6194 Therefore the result is always a good payoff between few collisions
6195 (= short chain lengths) and table size. */
6197 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6198 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6199 unsigned long int nsyms
,
6202 size_t best_size
= 0;
6203 unsigned long int i
;
6205 /* We have a problem here. The following code to optimize the table
6206 size requires an integer type with more the 32 bits. If
6207 BFD_HOST_U_64_BIT is set we know about such a type. */
6208 #ifdef BFD_HOST_U_64_BIT
6213 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
6214 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6215 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6216 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6217 unsigned long int *counts
;
6219 unsigned int no_improvement_count
= 0;
6221 /* Possible optimization parameters: if we have NSYMS symbols we say
6222 that the hashing table must at least have NSYMS/4 and at most
6224 minsize
= nsyms
/ 4;
6227 best_size
= maxsize
= nsyms
* 2;
6232 if ((best_size
& 31) == 0)
6236 /* Create array where we count the collisions in. We must use bfd_malloc
6237 since the size could be large. */
6239 amt
*= sizeof (unsigned long int);
6240 counts
= (unsigned long int *) bfd_malloc (amt
);
6244 /* Compute the "optimal" size for the hash table. The criteria is a
6245 minimal chain length. The minor criteria is (of course) the size
6247 for (i
= minsize
; i
< maxsize
; ++i
)
6249 /* Walk through the array of hashcodes and count the collisions. */
6250 BFD_HOST_U_64_BIT max
;
6251 unsigned long int j
;
6252 unsigned long int fact
;
6254 if (gnu_hash
&& (i
& 31) == 0)
6257 memset (counts
, '\0', i
* sizeof (unsigned long int));
6259 /* Determine how often each hash bucket is used. */
6260 for (j
= 0; j
< nsyms
; ++j
)
6261 ++counts
[hashcodes
[j
] % i
];
6263 /* For the weight function we need some information about the
6264 pagesize on the target. This is information need not be 100%
6265 accurate. Since this information is not available (so far) we
6266 define it here to a reasonable default value. If it is crucial
6267 to have a better value some day simply define this value. */
6268 # ifndef BFD_TARGET_PAGESIZE
6269 # define BFD_TARGET_PAGESIZE (4096)
6272 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6274 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6277 /* Variant 1: optimize for short chains. We add the squares
6278 of all the chain lengths (which favors many small chain
6279 over a few long chains). */
6280 for (j
= 0; j
< i
; ++j
)
6281 max
+= counts
[j
] * counts
[j
];
6283 /* This adds penalties for the overall size of the table. */
6284 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6287 /* Variant 2: Optimize a lot more for small table. Here we
6288 also add squares of the size but we also add penalties for
6289 empty slots (the +1 term). */
6290 for (j
= 0; j
< i
; ++j
)
6291 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6293 /* The overall size of the table is considered, but not as
6294 strong as in variant 1, where it is squared. */
6295 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6299 /* Compare with current best results. */
6300 if (max
< best_chlen
)
6304 no_improvement_count
= 0;
6306 /* PR 11843: Avoid futile long searches for the best bucket size
6307 when there are a large number of symbols. */
6308 else if (++no_improvement_count
== 100)
6315 #endif /* defined (BFD_HOST_U_64_BIT) */
6317 /* This is the fallback solution if no 64bit type is available or if we
6318 are not supposed to spend much time on optimizations. We select the
6319 bucket count using a fixed set of numbers. */
6320 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6322 best_size
= elf_buckets
[i
];
6323 if (nsyms
< elf_buckets
[i
+ 1])
6326 if (gnu_hash
&& best_size
< 2)
6333 /* Size any SHT_GROUP section for ld -r. */
6336 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6341 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6342 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6343 && (s
= ibfd
->sections
) != NULL
6344 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6345 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6350 /* Set a default stack segment size. The value in INFO wins. If it
6351 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6352 undefined it is initialized. */
6355 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6356 struct bfd_link_info
*info
,
6357 const char *legacy_symbol
,
6358 bfd_vma default_size
)
6360 struct elf_link_hash_entry
*h
= NULL
;
6362 /* Look for legacy symbol. */
6364 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6365 FALSE
, FALSE
, FALSE
);
6366 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6367 || h
->root
.type
== bfd_link_hash_defweak
)
6369 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6371 /* The symbol has no type if specified on the command line. */
6372 h
->type
= STT_OBJECT
;
6373 if (info
->stacksize
)
6374 /* xgettext:c-format */
6375 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6376 output_bfd
, legacy_symbol
);
6377 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6378 /* xgettext:c-format */
6379 _bfd_error_handler (_("%pB: %s not absolute"),
6380 output_bfd
, legacy_symbol
);
6382 info
->stacksize
= h
->root
.u
.def
.value
;
6385 if (!info
->stacksize
)
6386 /* If the user didn't set a size, or explicitly inhibit the
6387 size, set it now. */
6388 info
->stacksize
= default_size
;
6390 /* Provide the legacy symbol, if it is referenced. */
6391 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6392 || h
->root
.type
== bfd_link_hash_undefweak
))
6394 struct bfd_link_hash_entry
*bh
= NULL
;
6396 if (!(_bfd_generic_link_add_one_symbol
6397 (info
, output_bfd
, legacy_symbol
,
6398 BSF_GLOBAL
, bfd_abs_section_ptr
,
6399 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6400 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6403 h
= (struct elf_link_hash_entry
*) bh
;
6405 h
->type
= STT_OBJECT
;
6411 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6413 struct elf_gc_sweep_symbol_info
6415 struct bfd_link_info
*info
;
6416 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6421 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6424 && (((h
->root
.type
== bfd_link_hash_defined
6425 || h
->root
.type
== bfd_link_hash_defweak
)
6426 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6427 && h
->root
.u
.def
.section
->gc_mark
))
6428 || h
->root
.type
== bfd_link_hash_undefined
6429 || h
->root
.type
== bfd_link_hash_undefweak
))
6431 struct elf_gc_sweep_symbol_info
*inf
;
6433 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6434 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
6437 h
->ref_regular_nonweak
= 0;
6443 /* Set up the sizes and contents of the ELF dynamic sections. This is
6444 called by the ELF linker emulation before_allocation routine. We
6445 must set the sizes of the sections before the linker sets the
6446 addresses of the various sections. */
6449 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6452 const char *filter_shlib
,
6454 const char *depaudit
,
6455 const char * const *auxiliary_filters
,
6456 struct bfd_link_info
*info
,
6457 asection
**sinterpptr
)
6460 const struct elf_backend_data
*bed
;
6464 if (!is_elf_hash_table (info
->hash
))
6467 dynobj
= elf_hash_table (info
)->dynobj
;
6469 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6471 struct bfd_elf_version_tree
*verdefs
;
6472 struct elf_info_failed asvinfo
;
6473 struct bfd_elf_version_tree
*t
;
6474 struct bfd_elf_version_expr
*d
;
6478 /* If we are supposed to export all symbols into the dynamic symbol
6479 table (this is not the normal case), then do so. */
6480 if (info
->export_dynamic
6481 || (bfd_link_executable (info
) && info
->dynamic
))
6483 struct elf_info_failed eif
;
6487 elf_link_hash_traverse (elf_hash_table (info
),
6488 _bfd_elf_export_symbol
,
6496 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6498 if (soname_indx
== (size_t) -1
6499 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6503 soname_indx
= (size_t) -1;
6505 /* Make all global versions with definition. */
6506 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6507 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6508 if (!d
->symver
&& d
->literal
)
6510 const char *verstr
, *name
;
6511 size_t namelen
, verlen
, newlen
;
6512 char *newname
, *p
, leading_char
;
6513 struct elf_link_hash_entry
*newh
;
6515 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6517 namelen
= strlen (name
) + (leading_char
!= '\0');
6519 verlen
= strlen (verstr
);
6520 newlen
= namelen
+ verlen
+ 3;
6522 newname
= (char *) bfd_malloc (newlen
);
6523 if (newname
== NULL
)
6525 newname
[0] = leading_char
;
6526 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6528 /* Check the hidden versioned definition. */
6529 p
= newname
+ namelen
;
6531 memcpy (p
, verstr
, verlen
+ 1);
6532 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6533 newname
, FALSE
, FALSE
,
6536 || (newh
->root
.type
!= bfd_link_hash_defined
6537 && newh
->root
.type
!= bfd_link_hash_defweak
))
6539 /* Check the default versioned definition. */
6541 memcpy (p
, verstr
, verlen
+ 1);
6542 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6543 newname
, FALSE
, FALSE
,
6548 /* Mark this version if there is a definition and it is
6549 not defined in a shared object. */
6551 && !newh
->def_dynamic
6552 && (newh
->root
.type
== bfd_link_hash_defined
6553 || newh
->root
.type
== bfd_link_hash_defweak
))
6557 /* Attach all the symbols to their version information. */
6558 asvinfo
.info
= info
;
6559 asvinfo
.failed
= FALSE
;
6561 elf_link_hash_traverse (elf_hash_table (info
),
6562 _bfd_elf_link_assign_sym_version
,
6567 if (!info
->allow_undefined_version
)
6569 /* Check if all global versions have a definition. */
6570 bfd_boolean all_defined
= TRUE
;
6571 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6572 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6573 if (d
->literal
&& !d
->symver
&& !d
->script
)
6576 (_("%s: undefined version: %s"),
6577 d
->pattern
, t
->name
);
6578 all_defined
= FALSE
;
6583 bfd_set_error (bfd_error_bad_value
);
6588 /* Set up the version definition section. */
6589 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6590 BFD_ASSERT (s
!= NULL
);
6592 /* We may have created additional version definitions if we are
6593 just linking a regular application. */
6594 verdefs
= info
->version_info
;
6596 /* Skip anonymous version tag. */
6597 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6598 verdefs
= verdefs
->next
;
6600 if (verdefs
== NULL
&& !info
->create_default_symver
)
6601 s
->flags
|= SEC_EXCLUDE
;
6607 Elf_Internal_Verdef def
;
6608 Elf_Internal_Verdaux defaux
;
6609 struct bfd_link_hash_entry
*bh
;
6610 struct elf_link_hash_entry
*h
;
6616 /* Make space for the base version. */
6617 size
+= sizeof (Elf_External_Verdef
);
6618 size
+= sizeof (Elf_External_Verdaux
);
6621 /* Make space for the default version. */
6622 if (info
->create_default_symver
)
6624 size
+= sizeof (Elf_External_Verdef
);
6628 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6630 struct bfd_elf_version_deps
*n
;
6632 /* Don't emit base version twice. */
6636 size
+= sizeof (Elf_External_Verdef
);
6637 size
+= sizeof (Elf_External_Verdaux
);
6640 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6641 size
+= sizeof (Elf_External_Verdaux
);
6645 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6646 if (s
->contents
== NULL
&& s
->size
!= 0)
6649 /* Fill in the version definition section. */
6653 def
.vd_version
= VER_DEF_CURRENT
;
6654 def
.vd_flags
= VER_FLG_BASE
;
6657 if (info
->create_default_symver
)
6659 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6660 def
.vd_next
= sizeof (Elf_External_Verdef
);
6664 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6665 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6666 + sizeof (Elf_External_Verdaux
));
6669 if (soname_indx
!= (size_t) -1)
6671 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6673 def
.vd_hash
= bfd_elf_hash (soname
);
6674 defaux
.vda_name
= soname_indx
;
6681 name
= lbasename (bfd_get_filename (output_bfd
));
6682 def
.vd_hash
= bfd_elf_hash (name
);
6683 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6685 if (indx
== (size_t) -1)
6687 defaux
.vda_name
= indx
;
6689 defaux
.vda_next
= 0;
6691 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6692 (Elf_External_Verdef
*) p
);
6693 p
+= sizeof (Elf_External_Verdef
);
6694 if (info
->create_default_symver
)
6696 /* Add a symbol representing this version. */
6698 if (! (_bfd_generic_link_add_one_symbol
6699 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6701 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6703 h
= (struct elf_link_hash_entry
*) bh
;
6706 h
->type
= STT_OBJECT
;
6707 h
->verinfo
.vertree
= NULL
;
6709 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6712 /* Create a duplicate of the base version with the same
6713 aux block, but different flags. */
6716 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6718 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6719 + sizeof (Elf_External_Verdaux
));
6722 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6723 (Elf_External_Verdef
*) p
);
6724 p
+= sizeof (Elf_External_Verdef
);
6726 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6727 (Elf_External_Verdaux
*) p
);
6728 p
+= sizeof (Elf_External_Verdaux
);
6730 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6733 struct bfd_elf_version_deps
*n
;
6735 /* Don't emit the base version twice. */
6740 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6743 /* Add a symbol representing this version. */
6745 if (! (_bfd_generic_link_add_one_symbol
6746 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6748 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6750 h
= (struct elf_link_hash_entry
*) bh
;
6753 h
->type
= STT_OBJECT
;
6754 h
->verinfo
.vertree
= t
;
6756 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6759 def
.vd_version
= VER_DEF_CURRENT
;
6761 if (t
->globals
.list
== NULL
6762 && t
->locals
.list
== NULL
6764 def
.vd_flags
|= VER_FLG_WEAK
;
6765 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6766 def
.vd_cnt
= cdeps
+ 1;
6767 def
.vd_hash
= bfd_elf_hash (t
->name
);
6768 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6771 /* If a basever node is next, it *must* be the last node in
6772 the chain, otherwise Verdef construction breaks. */
6773 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6774 BFD_ASSERT (t
->next
->next
== NULL
);
6776 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6777 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6778 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6780 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6781 (Elf_External_Verdef
*) p
);
6782 p
+= sizeof (Elf_External_Verdef
);
6784 defaux
.vda_name
= h
->dynstr_index
;
6785 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6787 defaux
.vda_next
= 0;
6788 if (t
->deps
!= NULL
)
6789 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6790 t
->name_indx
= defaux
.vda_name
;
6792 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6793 (Elf_External_Verdaux
*) p
);
6794 p
+= sizeof (Elf_External_Verdaux
);
6796 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6798 if (n
->version_needed
== NULL
)
6800 /* This can happen if there was an error in the
6802 defaux
.vda_name
= 0;
6806 defaux
.vda_name
= n
->version_needed
->name_indx
;
6807 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6810 if (n
->next
== NULL
)
6811 defaux
.vda_next
= 0;
6813 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6815 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6816 (Elf_External_Verdaux
*) p
);
6817 p
+= sizeof (Elf_External_Verdaux
);
6821 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6825 bed
= get_elf_backend_data (output_bfd
);
6827 if (info
->gc_sections
&& bed
->can_gc_sections
)
6829 struct elf_gc_sweep_symbol_info sweep_info
;
6831 /* Remove the symbols that were in the swept sections from the
6832 dynamic symbol table. */
6833 sweep_info
.info
= info
;
6834 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6835 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6839 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6842 struct elf_find_verdep_info sinfo
;
6844 /* Work out the size of the version reference section. */
6846 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6847 BFD_ASSERT (s
!= NULL
);
6850 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6851 if (sinfo
.vers
== 0)
6853 sinfo
.failed
= FALSE
;
6855 elf_link_hash_traverse (elf_hash_table (info
),
6856 _bfd_elf_link_find_version_dependencies
,
6861 if (elf_tdata (output_bfd
)->verref
== NULL
)
6862 s
->flags
|= SEC_EXCLUDE
;
6865 Elf_Internal_Verneed
*vn
;
6870 /* Build the version dependency section. */
6873 for (vn
= elf_tdata (output_bfd
)->verref
;
6875 vn
= vn
->vn_nextref
)
6877 Elf_Internal_Vernaux
*a
;
6879 size
+= sizeof (Elf_External_Verneed
);
6881 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6882 size
+= sizeof (Elf_External_Vernaux
);
6886 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6887 if (s
->contents
== NULL
)
6891 for (vn
= elf_tdata (output_bfd
)->verref
;
6893 vn
= vn
->vn_nextref
)
6896 Elf_Internal_Vernaux
*a
;
6900 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6903 vn
->vn_version
= VER_NEED_CURRENT
;
6905 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6906 elf_dt_name (vn
->vn_bfd
) != NULL
6907 ? elf_dt_name (vn
->vn_bfd
)
6908 : lbasename (bfd_get_filename
6911 if (indx
== (size_t) -1)
6914 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6915 if (vn
->vn_nextref
== NULL
)
6918 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
6919 + caux
* sizeof (Elf_External_Vernaux
));
6921 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
6922 (Elf_External_Verneed
*) p
);
6923 p
+= sizeof (Elf_External_Verneed
);
6925 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6927 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6928 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6929 a
->vna_nodename
, FALSE
);
6930 if (indx
== (size_t) -1)
6933 if (a
->vna_nextptr
== NULL
)
6936 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6938 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6939 (Elf_External_Vernaux
*) p
);
6940 p
+= sizeof (Elf_External_Vernaux
);
6944 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6948 /* Any syms created from now on start with -1 in
6949 got.refcount/offset and plt.refcount/offset. */
6950 elf_hash_table (info
)->init_got_refcount
6951 = elf_hash_table (info
)->init_got_offset
;
6952 elf_hash_table (info
)->init_plt_refcount
6953 = elf_hash_table (info
)->init_plt_offset
;
6955 if (bfd_link_relocatable (info
)
6956 && !_bfd_elf_size_group_sections (info
))
6959 /* The backend may have to create some sections regardless of whether
6960 we're dynamic or not. */
6961 if (bed
->elf_backend_always_size_sections
6962 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6965 /* Determine any GNU_STACK segment requirements, after the backend
6966 has had a chance to set a default segment size. */
6967 if (info
->execstack
)
6968 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
6969 else if (info
->noexecstack
)
6970 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
6974 asection
*notesec
= NULL
;
6977 for (inputobj
= info
->input_bfds
;
6979 inputobj
= inputobj
->link
.next
)
6984 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
6986 s
= inputobj
->sections
;
6987 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
6990 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
6993 if (s
->flags
& SEC_CODE
)
6997 else if (bed
->default_execstack
)
7000 if (notesec
|| info
->stacksize
> 0)
7001 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
7002 if (notesec
&& exec
&& bfd_link_relocatable (info
)
7003 && notesec
->output_section
!= bfd_abs_section_ptr
)
7004 notesec
->output_section
->flags
|= SEC_CODE
;
7007 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7009 struct elf_info_failed eif
;
7010 struct elf_link_hash_entry
*h
;
7014 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7015 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7019 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7021 info
->flags
|= DF_SYMBOLIC
;
7029 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7031 if (indx
== (size_t) -1)
7034 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7035 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7039 if (filter_shlib
!= NULL
)
7043 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7044 filter_shlib
, TRUE
);
7045 if (indx
== (size_t) -1
7046 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7050 if (auxiliary_filters
!= NULL
)
7052 const char * const *p
;
7054 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7058 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7060 if (indx
== (size_t) -1
7061 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7070 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7072 if (indx
== (size_t) -1
7073 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7077 if (depaudit
!= NULL
)
7081 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7083 if (indx
== (size_t) -1
7084 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7091 /* Find all symbols which were defined in a dynamic object and make
7092 the backend pick a reasonable value for them. */
7093 elf_link_hash_traverse (elf_hash_table (info
),
7094 _bfd_elf_adjust_dynamic_symbol
,
7099 /* Add some entries to the .dynamic section. We fill in some of the
7100 values later, in bfd_elf_final_link, but we must add the entries
7101 now so that we know the final size of the .dynamic section. */
7103 /* If there are initialization and/or finalization functions to
7104 call then add the corresponding DT_INIT/DT_FINI entries. */
7105 h
= (info
->init_function
7106 ? elf_link_hash_lookup (elf_hash_table (info
),
7107 info
->init_function
, FALSE
,
7114 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7117 h
= (info
->fini_function
7118 ? elf_link_hash_lookup (elf_hash_table (info
),
7119 info
->fini_function
, FALSE
,
7126 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7130 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7131 if (s
!= NULL
&& s
->linker_has_input
)
7133 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7134 if (! bfd_link_executable (info
))
7139 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7140 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7141 && (o
= sub
->sections
) != NULL
7142 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7143 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7144 if (elf_section_data (o
)->this_hdr
.sh_type
7145 == SHT_PREINIT_ARRAY
)
7148 (_("%pB: .preinit_array section is not allowed in DSO"),
7153 bfd_set_error (bfd_error_nonrepresentable_section
);
7157 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7158 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7161 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7162 if (s
!= NULL
&& s
->linker_has_input
)
7164 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7165 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7168 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7169 if (s
!= NULL
&& s
->linker_has_input
)
7171 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7172 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7176 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7177 /* If .dynstr is excluded from the link, we don't want any of
7178 these tags. Strictly, we should be checking each section
7179 individually; This quick check covers for the case where
7180 someone does a /DISCARD/ : { *(*) }. */
7181 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7183 bfd_size_type strsize
;
7185 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7186 if ((info
->emit_hash
7187 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7188 || (info
->emit_gnu_hash
7189 && (bed
->record_xhash_symbol
== NULL
7190 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7191 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7192 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7193 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7194 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7196 || (info
->gnu_flags_1
7197 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_FLAGS_1
,
7198 info
->gnu_flags_1
)))
7203 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7206 /* The backend must work out the sizes of all the other dynamic
7209 && bed
->elf_backend_size_dynamic_sections
!= NULL
7210 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7213 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7215 if (elf_tdata (output_bfd
)->cverdefs
)
7217 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7219 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7220 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7224 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7226 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7229 else if (info
->flags
& DF_BIND_NOW
)
7231 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7237 if (bfd_link_executable (info
))
7238 info
->flags_1
&= ~ (DF_1_INITFIRST
7241 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7245 if (elf_tdata (output_bfd
)->cverrefs
)
7247 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7249 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7250 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7254 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7255 && elf_tdata (output_bfd
)->cverdefs
== 0)
7256 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7260 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7261 s
->flags
|= SEC_EXCLUDE
;
7267 /* Find the first non-excluded output section. We'll use its
7268 section symbol for some emitted relocs. */
7270 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7273 asection
*found
= NULL
;
7275 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7276 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7277 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7280 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7283 elf_hash_table (info
)->text_index_section
= found
;
7286 /* Find two non-excluded output sections, one for code, one for data.
7287 We'll use their section symbols for some emitted relocs. */
7289 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7292 asection
*found
= NULL
;
7294 /* Data first, since setting text_index_section changes
7295 _bfd_elf_omit_section_dynsym_default. */
7296 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7297 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7298 && !(s
->flags
& SEC_READONLY
)
7299 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7302 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7305 elf_hash_table (info
)->data_index_section
= found
;
7307 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7308 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7309 && (s
->flags
& SEC_READONLY
)
7310 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7315 elf_hash_table (info
)->text_index_section
= found
;
7318 #define GNU_HASH_SECTION_NAME(bed) \
7319 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7322 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7324 const struct elf_backend_data
*bed
;
7325 unsigned long section_sym_count
;
7326 bfd_size_type dynsymcount
= 0;
7328 if (!is_elf_hash_table (info
->hash
))
7331 bed
= get_elf_backend_data (output_bfd
);
7332 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7334 /* Assign dynsym indices. In a shared library we generate a section
7335 symbol for each output section, which come first. Next come all
7336 of the back-end allocated local dynamic syms, followed by the rest
7337 of the global symbols.
7339 This is usually not needed for static binaries, however backends
7340 can request to always do it, e.g. the MIPS backend uses dynamic
7341 symbol counts to lay out GOT, which will be produced in the
7342 presence of GOT relocations even in static binaries (holding fixed
7343 data in that case, to satisfy those relocations). */
7345 if (elf_hash_table (info
)->dynamic_sections_created
7346 || bed
->always_renumber_dynsyms
)
7347 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7348 §ion_sym_count
);
7350 if (elf_hash_table (info
)->dynamic_sections_created
)
7354 unsigned int dtagcount
;
7356 dynobj
= elf_hash_table (info
)->dynobj
;
7358 /* Work out the size of the symbol version section. */
7359 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7360 BFD_ASSERT (s
!= NULL
);
7361 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7363 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7364 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7365 if (s
->contents
== NULL
)
7368 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7372 /* Set the size of the .dynsym and .hash sections. We counted
7373 the number of dynamic symbols in elf_link_add_object_symbols.
7374 We will build the contents of .dynsym and .hash when we build
7375 the final symbol table, because until then we do not know the
7376 correct value to give the symbols. We built the .dynstr
7377 section as we went along in elf_link_add_object_symbols. */
7378 s
= elf_hash_table (info
)->dynsym
;
7379 BFD_ASSERT (s
!= NULL
);
7380 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7382 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7383 if (s
->contents
== NULL
)
7386 /* The first entry in .dynsym is a dummy symbol. Clear all the
7387 section syms, in case we don't output them all. */
7388 ++section_sym_count
;
7389 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7391 elf_hash_table (info
)->bucketcount
= 0;
7393 /* Compute the size of the hashing table. As a side effect this
7394 computes the hash values for all the names we export. */
7395 if (info
->emit_hash
)
7397 unsigned long int *hashcodes
;
7398 struct hash_codes_info hashinf
;
7400 unsigned long int nsyms
;
7402 size_t hash_entry_size
;
7404 /* Compute the hash values for all exported symbols. At the same
7405 time store the values in an array so that we could use them for
7407 amt
= dynsymcount
* sizeof (unsigned long int);
7408 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7409 if (hashcodes
== NULL
)
7411 hashinf
.hashcodes
= hashcodes
;
7412 hashinf
.error
= FALSE
;
7414 /* Put all hash values in HASHCODES. */
7415 elf_link_hash_traverse (elf_hash_table (info
),
7416 elf_collect_hash_codes
, &hashinf
);
7423 nsyms
= hashinf
.hashcodes
- hashcodes
;
7425 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7428 if (bucketcount
== 0 && nsyms
> 0)
7431 elf_hash_table (info
)->bucketcount
= bucketcount
;
7433 s
= bfd_get_linker_section (dynobj
, ".hash");
7434 BFD_ASSERT (s
!= NULL
);
7435 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7436 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7437 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7438 if (s
->contents
== NULL
)
7441 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7442 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7443 s
->contents
+ hash_entry_size
);
7446 if (info
->emit_gnu_hash
)
7449 unsigned char *contents
;
7450 struct collect_gnu_hash_codes cinfo
;
7454 memset (&cinfo
, 0, sizeof (cinfo
));
7456 /* Compute the hash values for all exported symbols. At the same
7457 time store the values in an array so that we could use them for
7459 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7460 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7461 if (cinfo
.hashcodes
== NULL
)
7464 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7465 cinfo
.min_dynindx
= -1;
7466 cinfo
.output_bfd
= output_bfd
;
7469 /* Put all hash values in HASHCODES. */
7470 elf_link_hash_traverse (elf_hash_table (info
),
7471 elf_collect_gnu_hash_codes
, &cinfo
);
7474 free (cinfo
.hashcodes
);
7479 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7481 if (bucketcount
== 0)
7483 free (cinfo
.hashcodes
);
7487 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7488 BFD_ASSERT (s
!= NULL
);
7490 if (cinfo
.nsyms
== 0)
7492 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7493 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7494 free (cinfo
.hashcodes
);
7495 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7496 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7497 if (contents
== NULL
)
7499 s
->contents
= contents
;
7500 /* 1 empty bucket. */
7501 bfd_put_32 (output_bfd
, 1, contents
);
7502 /* SYMIDX above the special symbol 0. */
7503 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7504 /* Just one word for bitmask. */
7505 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7506 /* Only hash fn bloom filter. */
7507 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7508 /* No hashes are valid - empty bitmask. */
7509 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7510 /* No hashes in the only bucket. */
7511 bfd_put_32 (output_bfd
, 0,
7512 contents
+ 16 + bed
->s
->arch_size
/ 8);
7516 unsigned long int maskwords
, maskbitslog2
, x
;
7517 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7521 while ((x
>>= 1) != 0)
7523 if (maskbitslog2
< 3)
7525 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7526 maskbitslog2
= maskbitslog2
+ 3;
7528 maskbitslog2
= maskbitslog2
+ 2;
7529 if (bed
->s
->arch_size
== 64)
7531 if (maskbitslog2
== 5)
7537 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7538 cinfo
.shift2
= maskbitslog2
;
7539 cinfo
.maskbits
= 1 << maskbitslog2
;
7540 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7541 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7542 amt
+= maskwords
* sizeof (bfd_vma
);
7543 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7544 if (cinfo
.bitmask
== NULL
)
7546 free (cinfo
.hashcodes
);
7550 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7551 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7552 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7553 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7555 /* Determine how often each hash bucket is used. */
7556 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7557 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7558 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7560 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7561 if (cinfo
.counts
[i
] != 0)
7563 cinfo
.indx
[i
] = cnt
;
7564 cnt
+= cinfo
.counts
[i
];
7566 BFD_ASSERT (cnt
== dynsymcount
);
7567 cinfo
.bucketcount
= bucketcount
;
7568 cinfo
.local_indx
= cinfo
.min_dynindx
;
7570 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7571 s
->size
+= cinfo
.maskbits
/ 8;
7572 if (bed
->record_xhash_symbol
!= NULL
)
7573 s
->size
+= cinfo
.nsyms
* 4;
7574 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7575 if (contents
== NULL
)
7577 free (cinfo
.bitmask
);
7578 free (cinfo
.hashcodes
);
7582 s
->contents
= contents
;
7583 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7584 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7585 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7586 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7587 contents
+= 16 + cinfo
.maskbits
/ 8;
7589 for (i
= 0; i
< bucketcount
; ++i
)
7591 if (cinfo
.counts
[i
] == 0)
7592 bfd_put_32 (output_bfd
, 0, contents
);
7594 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7598 cinfo
.contents
= contents
;
7600 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7601 /* Renumber dynamic symbols, if populating .gnu.hash section.
7602 If using .MIPS.xhash, populate the translation table. */
7603 elf_link_hash_traverse (elf_hash_table (info
),
7604 elf_gnu_hash_process_symidx
, &cinfo
);
7606 contents
= s
->contents
+ 16;
7607 for (i
= 0; i
< maskwords
; ++i
)
7609 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7611 contents
+= bed
->s
->arch_size
/ 8;
7614 free (cinfo
.bitmask
);
7615 free (cinfo
.hashcodes
);
7619 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7620 BFD_ASSERT (s
!= NULL
);
7622 elf_finalize_dynstr (output_bfd
, info
);
7624 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7626 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7627 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7634 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7637 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7640 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7641 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7644 /* Finish SHF_MERGE section merging. */
7647 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7652 if (!is_elf_hash_table (info
->hash
))
7655 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7656 if ((ibfd
->flags
& DYNAMIC
) == 0
7657 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7658 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7659 == get_elf_backend_data (obfd
)->s
->elfclass
))
7660 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7661 if ((sec
->flags
& SEC_MERGE
) != 0
7662 && !bfd_is_abs_section (sec
->output_section
))
7664 struct bfd_elf_section_data
*secdata
;
7666 secdata
= elf_section_data (sec
);
7667 if (! _bfd_add_merge_section (obfd
,
7668 &elf_hash_table (info
)->merge_info
,
7669 sec
, &secdata
->sec_info
))
7671 else if (secdata
->sec_info
)
7672 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7675 if (elf_hash_table (info
)->merge_info
!= NULL
)
7676 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7677 merge_sections_remove_hook
);
7681 /* Create an entry in an ELF linker hash table. */
7683 struct bfd_hash_entry
*
7684 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7685 struct bfd_hash_table
*table
,
7688 /* Allocate the structure if it has not already been allocated by a
7692 entry
= (struct bfd_hash_entry
*)
7693 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7698 /* Call the allocation method of the superclass. */
7699 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7702 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7703 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7705 /* Set local fields. */
7708 ret
->got
= htab
->init_got_refcount
;
7709 ret
->plt
= htab
->init_plt_refcount
;
7710 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7711 - offsetof (struct elf_link_hash_entry
, size
)));
7712 /* Assume that we have been called by a non-ELF symbol reader.
7713 This flag is then reset by the code which reads an ELF input
7714 file. This ensures that a symbol created by a non-ELF symbol
7715 reader will have the flag set correctly. */
7722 /* Copy data from an indirect symbol to its direct symbol, hiding the
7723 old indirect symbol. Also used for copying flags to a weakdef. */
7726 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7727 struct elf_link_hash_entry
*dir
,
7728 struct elf_link_hash_entry
*ind
)
7730 struct elf_link_hash_table
*htab
;
7732 if (ind
->dyn_relocs
!= NULL
)
7734 if (dir
->dyn_relocs
!= NULL
)
7736 struct elf_dyn_relocs
**pp
;
7737 struct elf_dyn_relocs
*p
;
7739 /* Add reloc counts against the indirect sym to the direct sym
7740 list. Merge any entries against the same section. */
7741 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
7743 struct elf_dyn_relocs
*q
;
7745 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
7746 if (q
->sec
== p
->sec
)
7748 q
->pc_count
+= p
->pc_count
;
7749 q
->count
+= p
->count
;
7756 *pp
= dir
->dyn_relocs
;
7759 dir
->dyn_relocs
= ind
->dyn_relocs
;
7760 ind
->dyn_relocs
= NULL
;
7763 /* Copy down any references that we may have already seen to the
7764 symbol which just became indirect. */
7766 if (dir
->versioned
!= versioned_hidden
)
7767 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7768 dir
->ref_regular
|= ind
->ref_regular
;
7769 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7770 dir
->non_got_ref
|= ind
->non_got_ref
;
7771 dir
->needs_plt
|= ind
->needs_plt
;
7772 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7774 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7777 /* Copy over the global and procedure linkage table refcount entries.
7778 These may have been already set up by a check_relocs routine. */
7779 htab
= elf_hash_table (info
);
7780 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7782 if (dir
->got
.refcount
< 0)
7783 dir
->got
.refcount
= 0;
7784 dir
->got
.refcount
+= ind
->got
.refcount
;
7785 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7788 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7790 if (dir
->plt
.refcount
< 0)
7791 dir
->plt
.refcount
= 0;
7792 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7793 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7796 if (ind
->dynindx
!= -1)
7798 if (dir
->dynindx
!= -1)
7799 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7800 dir
->dynindx
= ind
->dynindx
;
7801 dir
->dynstr_index
= ind
->dynstr_index
;
7803 ind
->dynstr_index
= 0;
7808 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7809 struct elf_link_hash_entry
*h
,
7810 bfd_boolean force_local
)
7812 /* STT_GNU_IFUNC symbol must go through PLT. */
7813 if (h
->type
!= STT_GNU_IFUNC
)
7815 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7820 h
->forced_local
= 1;
7821 if (h
->dynindx
!= -1)
7823 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7826 h
->dynstr_index
= 0;
7831 /* Hide a symbol. */
7834 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
7835 struct bfd_link_info
*info
,
7836 struct bfd_link_hash_entry
*h
)
7838 if (is_elf_hash_table (info
->hash
))
7840 const struct elf_backend_data
*bed
7841 = get_elf_backend_data (output_bfd
);
7842 struct elf_link_hash_entry
*eh
7843 = (struct elf_link_hash_entry
*) h
;
7844 bed
->elf_backend_hide_symbol (info
, eh
, TRUE
);
7845 eh
->def_dynamic
= 0;
7846 eh
->ref_dynamic
= 0;
7847 eh
->dynamic_def
= 0;
7851 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7855 _bfd_elf_link_hash_table_init
7856 (struct elf_link_hash_table
*table
,
7858 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7859 struct bfd_hash_table
*,
7861 unsigned int entsize
,
7862 enum elf_target_id target_id
)
7865 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7867 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7868 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7869 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7870 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7871 /* The first dynamic symbol is a dummy. */
7872 table
->dynsymcount
= 1;
7874 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7876 table
->root
.type
= bfd_link_elf_hash_table
;
7877 table
->hash_table_id
= target_id
;
7878 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
7883 /* Create an ELF linker hash table. */
7885 struct bfd_link_hash_table
*
7886 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7888 struct elf_link_hash_table
*ret
;
7889 size_t amt
= sizeof (struct elf_link_hash_table
);
7891 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7895 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7896 sizeof (struct elf_link_hash_entry
),
7902 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7907 /* Destroy an ELF linker hash table. */
7910 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7912 struct elf_link_hash_table
*htab
;
7914 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7915 if (htab
->dynstr
!= NULL
)
7916 _bfd_elf_strtab_free (htab
->dynstr
);
7917 _bfd_merge_sections_free (htab
->merge_info
);
7918 _bfd_generic_link_hash_table_free (obfd
);
7921 /* This is a hook for the ELF emulation code in the generic linker to
7922 tell the backend linker what file name to use for the DT_NEEDED
7923 entry for a dynamic object. */
7926 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7928 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7929 && bfd_get_format (abfd
) == bfd_object
)
7930 elf_dt_name (abfd
) = name
;
7934 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7937 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7938 && bfd_get_format (abfd
) == bfd_object
)
7939 lib_class
= elf_dyn_lib_class (abfd
);
7946 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7948 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7949 && bfd_get_format (abfd
) == bfd_object
)
7950 elf_dyn_lib_class (abfd
) = lib_class
;
7953 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7954 the linker ELF emulation code. */
7956 struct bfd_link_needed_list
*
7957 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7958 struct bfd_link_info
*info
)
7960 if (! is_elf_hash_table (info
->hash
))
7962 return elf_hash_table (info
)->needed
;
7965 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7966 hook for the linker ELF emulation code. */
7968 struct bfd_link_needed_list
*
7969 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7970 struct bfd_link_info
*info
)
7972 if (! is_elf_hash_table (info
->hash
))
7974 return elf_hash_table (info
)->runpath
;
7977 /* Get the name actually used for a dynamic object for a link. This
7978 is the SONAME entry if there is one. Otherwise, it is the string
7979 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7982 bfd_elf_get_dt_soname (bfd
*abfd
)
7984 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7985 && bfd_get_format (abfd
) == bfd_object
)
7986 return elf_dt_name (abfd
);
7990 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7991 the ELF linker emulation code. */
7994 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7995 struct bfd_link_needed_list
**pneeded
)
7998 bfd_byte
*dynbuf
= NULL
;
7999 unsigned int elfsec
;
8000 unsigned long shlink
;
8001 bfd_byte
*extdyn
, *extdynend
;
8003 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
8007 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
8008 || bfd_get_format (abfd
) != bfd_object
)
8011 s
= bfd_get_section_by_name (abfd
, ".dynamic");
8012 if (s
== NULL
|| s
->size
== 0)
8015 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
8018 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8019 if (elfsec
== SHN_BAD
)
8022 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8024 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8025 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8028 extdynend
= extdyn
+ s
->size
;
8029 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
8031 Elf_Internal_Dyn dyn
;
8033 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8035 if (dyn
.d_tag
== DT_NULL
)
8038 if (dyn
.d_tag
== DT_NEEDED
)
8041 struct bfd_link_needed_list
*l
;
8042 unsigned int tagv
= dyn
.d_un
.d_val
;
8045 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8050 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8070 struct elf_symbuf_symbol
8072 unsigned long st_name
; /* Symbol name, index in string tbl */
8073 unsigned char st_info
; /* Type and binding attributes */
8074 unsigned char st_other
; /* Visibilty, and target specific */
8077 struct elf_symbuf_head
8079 struct elf_symbuf_symbol
*ssym
;
8081 unsigned int st_shndx
;
8088 Elf_Internal_Sym
*isym
;
8089 struct elf_symbuf_symbol
*ssym
;
8095 /* Sort references to symbols by ascending section number. */
8098 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8100 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8101 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8103 if (s1
->st_shndx
!= s2
->st_shndx
)
8104 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8105 /* Final sort by the address of the sym in the symbuf ensures
8108 return s1
> s2
? 1 : -1;
8113 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8115 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8116 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8117 int ret
= strcmp (s1
->name
, s2
->name
);
8120 if (s1
->u
.p
!= s2
->u
.p
)
8121 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8125 static struct elf_symbuf_head
*
8126 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8128 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8129 struct elf_symbuf_symbol
*ssym
;
8130 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8131 size_t i
, shndx_count
, total_size
, amt
;
8133 amt
= symcount
* sizeof (*indbuf
);
8134 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8138 /* NB: When checking if 2 sections define the same set of local and
8139 global symbols, ignore both undefined and section symbols in the
8141 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8142 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
8143 && ELF_ST_TYPE (isymbuf
[i
].st_info
) != STT_SECTION
)
8144 *ind
++ = &isymbuf
[i
];
8147 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8148 elf_sort_elf_symbol
);
8151 if (indbufend
> indbuf
)
8152 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8153 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8156 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8157 + (indbufend
- indbuf
) * sizeof (*ssym
));
8158 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8159 if (ssymbuf
== NULL
)
8165 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8166 ssymbuf
->ssym
= NULL
;
8167 ssymbuf
->count
= shndx_count
;
8168 ssymbuf
->st_shndx
= 0;
8169 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8171 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8174 ssymhead
->ssym
= ssym
;
8175 ssymhead
->count
= 0;
8176 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8178 ssym
->st_name
= (*ind
)->st_name
;
8179 ssym
->st_info
= (*ind
)->st_info
;
8180 ssym
->st_other
= (*ind
)->st_other
;
8183 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8184 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
8191 /* Check if 2 sections define the same set of local and global
8195 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8196 struct bfd_link_info
*info
)
8199 const struct elf_backend_data
*bed1
, *bed2
;
8200 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8201 size_t symcount1
, symcount2
;
8202 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8203 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8204 Elf_Internal_Sym
*isym
, *isymend
;
8205 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8206 size_t count1
, count2
, i
;
8207 unsigned int shndx1
, shndx2
;
8213 /* Both sections have to be in ELF. */
8214 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8215 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8218 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8221 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8222 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8223 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8226 bed1
= get_elf_backend_data (bfd1
);
8227 bed2
= get_elf_backend_data (bfd2
);
8228 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8229 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8230 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8231 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8233 if (symcount1
== 0 || symcount2
== 0)
8239 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8240 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8242 if (ssymbuf1
== NULL
)
8244 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8246 if (isymbuf1
== NULL
)
8249 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8251 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8252 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8256 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8258 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8260 if (isymbuf2
== NULL
)
8263 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8265 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8266 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8270 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8272 /* Optimized faster version. */
8274 struct elf_symbol
*symp
;
8275 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8278 hi
= ssymbuf1
->count
;
8283 mid
= (lo
+ hi
) / 2;
8284 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8286 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8290 count1
= ssymbuf1
[mid
].count
;
8297 hi
= ssymbuf2
->count
;
8302 mid
= (lo
+ hi
) / 2;
8303 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8305 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8309 count2
= ssymbuf2
[mid
].count
;
8315 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8319 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8321 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8322 if (symtable1
== NULL
|| symtable2
== NULL
)
8326 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
8327 ssym
< ssymend
; ssym
++, symp
++)
8329 symp
->u
.ssym
= ssym
;
8330 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8336 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
8337 ssym
< ssymend
; ssym
++, symp
++)
8339 symp
->u
.ssym
= ssym
;
8340 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8345 /* Sort symbol by name. */
8346 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8347 elf_sym_name_compare
);
8348 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8349 elf_sym_name_compare
);
8351 for (i
= 0; i
< count1
; i
++)
8352 /* Two symbols must have the same binding, type and name. */
8353 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8354 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8355 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8362 symtable1
= (struct elf_symbol
*)
8363 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8364 symtable2
= (struct elf_symbol
*)
8365 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8366 if (symtable1
== NULL
|| symtable2
== NULL
)
8369 /* Count definitions in the section. */
8371 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8372 if (isym
->st_shndx
== shndx1
)
8373 symtable1
[count1
++].u
.isym
= isym
;
8376 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8377 if (isym
->st_shndx
== shndx2
)
8378 symtable2
[count2
++].u
.isym
= isym
;
8380 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8383 for (i
= 0; i
< count1
; i
++)
8385 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8386 symtable1
[i
].u
.isym
->st_name
);
8388 for (i
= 0; i
< count2
; i
++)
8390 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8391 symtable2
[i
].u
.isym
->st_name
);
8393 /* Sort symbol by name. */
8394 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8395 elf_sym_name_compare
);
8396 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8397 elf_sym_name_compare
);
8399 for (i
= 0; i
< count1
; i
++)
8400 /* Two symbols must have the same binding, type and name. */
8401 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8402 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8403 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8417 /* Return TRUE if 2 section types are compatible. */
8420 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8421 bfd
*bbfd
, const asection
*bsec
)
8425 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8426 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8429 return elf_section_type (asec
) == elf_section_type (bsec
);
8432 /* Final phase of ELF linker. */
8434 /* A structure we use to avoid passing large numbers of arguments. */
8436 struct elf_final_link_info
8438 /* General link information. */
8439 struct bfd_link_info
*info
;
8442 /* Symbol string table. */
8443 struct elf_strtab_hash
*symstrtab
;
8444 /* .hash section. */
8446 /* symbol version section (.gnu.version). */
8447 asection
*symver_sec
;
8448 /* Buffer large enough to hold contents of any section. */
8450 /* Buffer large enough to hold external relocs of any section. */
8451 void *external_relocs
;
8452 /* Buffer large enough to hold internal relocs of any section. */
8453 Elf_Internal_Rela
*internal_relocs
;
8454 /* Buffer large enough to hold external local symbols of any input
8456 bfd_byte
*external_syms
;
8457 /* And a buffer for symbol section indices. */
8458 Elf_External_Sym_Shndx
*locsym_shndx
;
8459 /* Buffer large enough to hold internal local symbols of any input
8461 Elf_Internal_Sym
*internal_syms
;
8462 /* Array large enough to hold a symbol index for each local symbol
8463 of any input BFD. */
8465 /* Array large enough to hold a section pointer for each local
8466 symbol of any input BFD. */
8467 asection
**sections
;
8468 /* Buffer for SHT_SYMTAB_SHNDX section. */
8469 Elf_External_Sym_Shndx
*symshndxbuf
;
8470 /* Number of STT_FILE syms seen. */
8471 size_t filesym_count
;
8472 /* Local symbol hash table. */
8473 struct bfd_hash_table local_hash_table
;
8476 struct local_hash_entry
8478 /* Base hash table entry structure. */
8479 struct bfd_hash_entry root
;
8480 /* Size of the local symbol name. */
8482 /* Number of the duplicated local symbol names. */
8486 /* Create an entry in the local symbol hash table. */
8488 static struct bfd_hash_entry
*
8489 local_hash_newfunc (struct bfd_hash_entry
*entry
,
8490 struct bfd_hash_table
*table
,
8494 /* Allocate the structure if it has not already been allocated by a
8498 entry
= bfd_hash_allocate (table
,
8499 sizeof (struct local_hash_entry
));
8504 /* Call the allocation method of the superclass. */
8505 entry
= bfd_hash_newfunc (entry
, table
, string
);
8508 ((struct local_hash_entry
*) entry
)->count
= 0;
8509 ((struct local_hash_entry
*) entry
)->size
= 0;
8515 /* This struct is used to pass information to elf_link_output_extsym. */
8517 struct elf_outext_info
8520 bfd_boolean localsyms
;
8521 bfd_boolean file_sym_done
;
8522 struct elf_final_link_info
*flinfo
;
8526 /* Support for evaluating a complex relocation.
8528 Complex relocations are generalized, self-describing relocations. The
8529 implementation of them consists of two parts: complex symbols, and the
8530 relocations themselves.
8532 The relocations use a reserved elf-wide relocation type code (R_RELC
8533 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8534 information (start bit, end bit, word width, etc) into the addend. This
8535 information is extracted from CGEN-generated operand tables within gas.
8537 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8538 internal) representing prefix-notation expressions, including but not
8539 limited to those sorts of expressions normally encoded as addends in the
8540 addend field. The symbol mangling format is:
8543 | <unary-operator> ':' <node>
8544 | <binary-operator> ':' <node> ':' <node>
8547 <literal> := 's' <digits=N> ':' <N character symbol name>
8548 | 'S' <digits=N> ':' <N character section name>
8552 <binary-operator> := as in C
8553 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8556 set_symbol_value (bfd
*bfd_with_globals
,
8557 Elf_Internal_Sym
*isymbuf
,
8562 struct elf_link_hash_entry
**sym_hashes
;
8563 struct elf_link_hash_entry
*h
;
8564 size_t extsymoff
= locsymcount
;
8566 if (symidx
< locsymcount
)
8568 Elf_Internal_Sym
*sym
;
8570 sym
= isymbuf
+ symidx
;
8571 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8573 /* It is a local symbol: move it to the
8574 "absolute" section and give it a value. */
8575 sym
->st_shndx
= SHN_ABS
;
8576 sym
->st_value
= val
;
8579 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8583 /* It is a global symbol: set its link type
8584 to "defined" and give it a value. */
8586 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8587 h
= sym_hashes
[symidx
- extsymoff
];
8588 while (h
->root
.type
== bfd_link_hash_indirect
8589 || h
->root
.type
== bfd_link_hash_warning
)
8590 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8591 h
->root
.type
= bfd_link_hash_defined
;
8592 h
->root
.u
.def
.value
= val
;
8593 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8597 resolve_symbol (const char *name
,
8599 struct elf_final_link_info
*flinfo
,
8601 Elf_Internal_Sym
*isymbuf
,
8604 Elf_Internal_Sym
*sym
;
8605 struct bfd_link_hash_entry
*global_entry
;
8606 const char *candidate
= NULL
;
8607 Elf_Internal_Shdr
*symtab_hdr
;
8610 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8612 for (i
= 0; i
< locsymcount
; ++ i
)
8616 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8619 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8620 symtab_hdr
->sh_link
,
8623 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8624 name
, candidate
, (unsigned long) sym
->st_value
);
8626 if (candidate
&& strcmp (candidate
, name
) == 0)
8628 asection
*sec
= flinfo
->sections
[i
];
8630 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8631 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8633 printf ("Found symbol with value %8.8lx\n",
8634 (unsigned long) *result
);
8640 /* Hmm, haven't found it yet. perhaps it is a global. */
8641 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8642 FALSE
, FALSE
, TRUE
);
8646 if (global_entry
->type
== bfd_link_hash_defined
8647 || global_entry
->type
== bfd_link_hash_defweak
)
8649 *result
= (global_entry
->u
.def
.value
8650 + global_entry
->u
.def
.section
->output_section
->vma
8651 + global_entry
->u
.def
.section
->output_offset
);
8653 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8654 global_entry
->root
.string
, (unsigned long) *result
);
8662 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8663 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8664 names like "foo.end" which is the end address of section "foo". */
8667 resolve_section (const char *name
,
8675 for (curr
= sections
; curr
; curr
= curr
->next
)
8676 if (strcmp (curr
->name
, name
) == 0)
8678 *result
= curr
->vma
;
8682 /* Hmm. still haven't found it. try pseudo-section names. */
8683 /* FIXME: This could be coded more efficiently... */
8684 for (curr
= sections
; curr
; curr
= curr
->next
)
8686 len
= strlen (curr
->name
);
8687 if (len
> strlen (name
))
8690 if (strncmp (curr
->name
, name
, len
) == 0)
8692 if (strncmp (".end", name
+ len
, 4) == 0)
8694 *result
= (curr
->vma
8695 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
8699 /* Insert more pseudo-section names here, if you like. */
8707 undefined_reference (const char *reftype
, const char *name
)
8709 /* xgettext:c-format */
8710 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8712 bfd_set_error (bfd_error_bad_value
);
8716 eval_symbol (bfd_vma
*result
,
8719 struct elf_final_link_info
*flinfo
,
8721 Elf_Internal_Sym
*isymbuf
,
8730 const char *sym
= *symp
;
8732 bfd_boolean symbol_is_section
= FALSE
;
8737 if (len
< 1 || len
> sizeof (symbuf
))
8739 bfd_set_error (bfd_error_invalid_operation
);
8752 *result
= strtoul (sym
, (char **) symp
, 16);
8756 symbol_is_section
= TRUE
;
8760 symlen
= strtol (sym
, (char **) symp
, 10);
8761 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8763 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8765 bfd_set_error (bfd_error_invalid_operation
);
8769 memcpy (symbuf
, sym
, symlen
);
8770 symbuf
[symlen
] = '\0';
8771 *symp
= sym
+ symlen
;
8773 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8774 the symbol as a section, or vice-versa. so we're pretty liberal in our
8775 interpretation here; section means "try section first", not "must be a
8776 section", and likewise with symbol. */
8778 if (symbol_is_section
)
8780 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8781 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8782 isymbuf
, locsymcount
))
8784 undefined_reference ("section", symbuf
);
8790 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8791 isymbuf
, locsymcount
)
8792 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8795 undefined_reference ("symbol", symbuf
);
8802 /* All that remains are operators. */
8804 #define UNARY_OP(op) \
8805 if (strncmp (sym, #op, strlen (#op)) == 0) \
8807 sym += strlen (#op); \
8811 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8812 isymbuf, locsymcount, signed_p)) \
8815 *result = op ((bfd_signed_vma) a); \
8821 #define BINARY_OP_HEAD(op) \
8822 if (strncmp (sym, #op, strlen (#op)) == 0) \
8824 sym += strlen (#op); \
8828 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8829 isymbuf, locsymcount, signed_p)) \
8832 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8833 isymbuf, locsymcount, signed_p)) \
8835 #define BINARY_OP_TAIL(op) \
8837 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8842 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8846 BINARY_OP_HEAD (<<);
8847 if (b
>= sizeof (a
) * CHAR_BIT
)
8853 BINARY_OP_TAIL (<<);
8854 BINARY_OP_HEAD (>>);
8855 if (b
>= sizeof (a
) * CHAR_BIT
)
8857 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
8860 BINARY_OP_TAIL (>>);
8873 _bfd_error_handler (_("division by zero"));
8874 bfd_set_error (bfd_error_bad_value
);
8881 _bfd_error_handler (_("division by zero"));
8882 bfd_set_error (bfd_error_bad_value
);
8895 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8896 bfd_set_error (bfd_error_invalid_operation
);
8902 put_value (bfd_vma size
,
8903 unsigned long chunksz
,
8908 location
+= (size
- chunksz
);
8910 for (; size
; size
-= chunksz
, location
-= chunksz
)
8915 bfd_put_8 (input_bfd
, x
, location
);
8919 bfd_put_16 (input_bfd
, x
, location
);
8923 bfd_put_32 (input_bfd
, x
, location
);
8924 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8930 bfd_put_64 (input_bfd
, x
, location
);
8931 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8944 get_value (bfd_vma size
,
8945 unsigned long chunksz
,
8952 /* Sanity checks. */
8953 BFD_ASSERT (chunksz
<= sizeof (x
)
8956 && (size
% chunksz
) == 0
8957 && input_bfd
!= NULL
8958 && location
!= NULL
);
8960 if (chunksz
== sizeof (x
))
8962 BFD_ASSERT (size
== chunksz
);
8964 /* Make sure that we do not perform an undefined shift operation.
8965 We know that size == chunksz so there will only be one iteration
8966 of the loop below. */
8970 shift
= 8 * chunksz
;
8972 for (; size
; size
-= chunksz
, location
+= chunksz
)
8977 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8980 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8983 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8987 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8998 decode_complex_addend (unsigned long *start
, /* in bits */
8999 unsigned long *oplen
, /* in bits */
9000 unsigned long *len
, /* in bits */
9001 unsigned long *wordsz
, /* in bytes */
9002 unsigned long *chunksz
, /* in bytes */
9003 unsigned long *lsb0_p
,
9004 unsigned long *signed_p
,
9005 unsigned long *trunc_p
,
9006 unsigned long encoded
)
9008 * start
= encoded
& 0x3F;
9009 * len
= (encoded
>> 6) & 0x3F;
9010 * oplen
= (encoded
>> 12) & 0x3F;
9011 * wordsz
= (encoded
>> 18) & 0xF;
9012 * chunksz
= (encoded
>> 22) & 0xF;
9013 * lsb0_p
= (encoded
>> 27) & 1;
9014 * signed_p
= (encoded
>> 28) & 1;
9015 * trunc_p
= (encoded
>> 29) & 1;
9018 bfd_reloc_status_type
9019 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
9020 asection
*input_section
,
9022 Elf_Internal_Rela
*rel
,
9025 bfd_vma shift
, x
, mask
;
9026 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9027 bfd_reloc_status_type r
;
9028 bfd_size_type octets
;
9030 /* Perform this reloc, since it is complex.
9031 (this is not to say that it necessarily refers to a complex
9032 symbol; merely that it is a self-describing CGEN based reloc.
9033 i.e. the addend has the complete reloc information (bit start, end,
9034 word size, etc) encoded within it.). */
9036 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9037 &chunksz
, &lsb0_p
, &signed_p
,
9038 &trunc_p
, rel
->r_addend
);
9040 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9043 shift
= (start
+ 1) - len
;
9045 shift
= (8 * wordsz
) - (start
+ len
);
9047 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9048 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9051 printf ("Doing complex reloc: "
9052 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9053 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9054 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9055 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9056 oplen
, (unsigned long) x
, (unsigned long) mask
,
9057 (unsigned long) relocation
);
9062 /* Now do an overflow check. */
9063 r
= bfd_check_overflow ((signed_p
9064 ? complain_overflow_signed
9065 : complain_overflow_unsigned
),
9066 len
, 0, (8 * wordsz
),
9070 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9073 printf (" relocation: %8.8lx\n"
9074 " shifted mask: %8.8lx\n"
9075 " shifted/masked reloc: %8.8lx\n"
9076 " result: %8.8lx\n",
9077 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9078 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9080 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9084 /* Functions to read r_offset from external (target order) reloc
9085 entry. Faster than bfd_getl32 et al, because we let the compiler
9086 know the value is aligned. */
9089 ext32l_r_offset (const void *p
)
9096 const union aligned32
*a
9097 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9099 uint32_t aval
= ( (uint32_t) a
->c
[0]
9100 | (uint32_t) a
->c
[1] << 8
9101 | (uint32_t) a
->c
[2] << 16
9102 | (uint32_t) a
->c
[3] << 24);
9107 ext32b_r_offset (const void *p
)
9114 const union aligned32
*a
9115 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9117 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9118 | (uint32_t) a
->c
[1] << 16
9119 | (uint32_t) a
->c
[2] << 8
9120 | (uint32_t) a
->c
[3]);
9124 #ifdef BFD_HOST_64_BIT
9126 ext64l_r_offset (const void *p
)
9133 const union aligned64
*a
9134 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9136 uint64_t aval
= ( (uint64_t) a
->c
[0]
9137 | (uint64_t) a
->c
[1] << 8
9138 | (uint64_t) a
->c
[2] << 16
9139 | (uint64_t) a
->c
[3] << 24
9140 | (uint64_t) a
->c
[4] << 32
9141 | (uint64_t) a
->c
[5] << 40
9142 | (uint64_t) a
->c
[6] << 48
9143 | (uint64_t) a
->c
[7] << 56);
9148 ext64b_r_offset (const void *p
)
9155 const union aligned64
*a
9156 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9158 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9159 | (uint64_t) a
->c
[1] << 48
9160 | (uint64_t) a
->c
[2] << 40
9161 | (uint64_t) a
->c
[3] << 32
9162 | (uint64_t) a
->c
[4] << 24
9163 | (uint64_t) a
->c
[5] << 16
9164 | (uint64_t) a
->c
[6] << 8
9165 | (uint64_t) a
->c
[7]);
9170 /* When performing a relocatable link, the input relocations are
9171 preserved. But, if they reference global symbols, the indices
9172 referenced must be updated. Update all the relocations found in
9176 elf_link_adjust_relocs (bfd
*abfd
,
9178 struct bfd_elf_section_reloc_data
*reldata
,
9180 struct bfd_link_info
*info
)
9183 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9185 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9186 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9187 bfd_vma r_type_mask
;
9189 unsigned int count
= reldata
->count
;
9190 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9192 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9194 swap_in
= bed
->s
->swap_reloc_in
;
9195 swap_out
= bed
->s
->swap_reloc_out
;
9197 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9199 swap_in
= bed
->s
->swap_reloca_in
;
9200 swap_out
= bed
->s
->swap_reloca_out
;
9205 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9208 if (bed
->s
->arch_size
== 32)
9215 r_type_mask
= 0xffffffff;
9219 erela
= reldata
->hdr
->contents
;
9220 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9222 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9225 if (*rel_hash
== NULL
)
9228 if ((*rel_hash
)->indx
== -2
9229 && info
->gc_sections
9230 && ! info
->gc_keep_exported
)
9232 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9233 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9235 (*rel_hash
)->root
.root
.string
);
9236 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9238 bfd_set_error (bfd_error_invalid_operation
);
9241 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9243 (*swap_in
) (abfd
, erela
, irela
);
9244 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9245 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9246 | (irela
[j
].r_info
& r_type_mask
));
9247 (*swap_out
) (abfd
, irela
, erela
);
9250 if (bed
->elf_backend_update_relocs
)
9251 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9253 if (sort
&& count
!= 0)
9255 bfd_vma (*ext_r_off
) (const void *);
9258 bfd_byte
*base
, *end
, *p
, *loc
;
9259 bfd_byte
*buf
= NULL
;
9261 if (bed
->s
->arch_size
== 32)
9263 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9264 ext_r_off
= ext32l_r_offset
;
9265 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9266 ext_r_off
= ext32b_r_offset
;
9272 #ifdef BFD_HOST_64_BIT
9273 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9274 ext_r_off
= ext64l_r_offset
;
9275 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9276 ext_r_off
= ext64b_r_offset
;
9282 /* Must use a stable sort here. A modified insertion sort,
9283 since the relocs are mostly sorted already. */
9284 elt_size
= reldata
->hdr
->sh_entsize
;
9285 base
= reldata
->hdr
->contents
;
9286 end
= base
+ count
* elt_size
;
9287 if (elt_size
> sizeof (Elf64_External_Rela
))
9290 /* Ensure the first element is lowest. This acts as a sentinel,
9291 speeding the main loop below. */
9292 r_off
= (*ext_r_off
) (base
);
9293 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9295 bfd_vma r_off2
= (*ext_r_off
) (p
);
9304 /* Don't just swap *base and *loc as that changes the order
9305 of the original base[0] and base[1] if they happen to
9306 have the same r_offset. */
9307 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9308 memcpy (onebuf
, loc
, elt_size
);
9309 memmove (base
+ elt_size
, base
, loc
- base
);
9310 memcpy (base
, onebuf
, elt_size
);
9313 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9315 /* base to p is sorted, *p is next to insert. */
9316 r_off
= (*ext_r_off
) (p
);
9317 /* Search the sorted region for location to insert. */
9319 while (r_off
< (*ext_r_off
) (loc
))
9324 /* Chances are there is a run of relocs to insert here,
9325 from one of more input files. Files are not always
9326 linked in order due to the way elf_link_input_bfd is
9327 called. See pr17666. */
9328 size_t sortlen
= p
- loc
;
9329 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9330 size_t runlen
= elt_size
;
9331 size_t buf_size
= 96 * 1024;
9332 while (p
+ runlen
< end
9333 && (sortlen
<= buf_size
9334 || runlen
+ elt_size
<= buf_size
)
9335 && r_off2
> (*ext_r_off
) (p
+ runlen
))
9339 buf
= bfd_malloc (buf_size
);
9343 if (runlen
< sortlen
)
9345 memcpy (buf
, p
, runlen
);
9346 memmove (loc
+ runlen
, loc
, sortlen
);
9347 memcpy (loc
, buf
, runlen
);
9351 memcpy (buf
, loc
, sortlen
);
9352 memmove (loc
, p
, runlen
);
9353 memcpy (loc
+ runlen
, buf
, sortlen
);
9355 p
+= runlen
- elt_size
;
9358 /* Hashes are no longer valid. */
9359 free (reldata
->hashes
);
9360 reldata
->hashes
= NULL
;
9366 struct elf_link_sort_rela
9372 enum elf_reloc_type_class type
;
9373 /* We use this as an array of size int_rels_per_ext_rel. */
9374 Elf_Internal_Rela rela
[1];
9377 /* qsort stability here and for cmp2 is only an issue if multiple
9378 dynamic relocations are emitted at the same address. But targets
9379 that apply a series of dynamic relocations each operating on the
9380 result of the prior relocation can't use -z combreloc as
9381 implemented anyway. Such schemes tend to be broken by sorting on
9382 symbol index. That leaves dynamic NONE relocs as the only other
9383 case where ld might emit multiple relocs at the same address, and
9384 those are only emitted due to target bugs. */
9387 elf_link_sort_cmp1 (const void *A
, const void *B
)
9389 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9390 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9391 int relativea
, relativeb
;
9393 relativea
= a
->type
== reloc_class_relative
;
9394 relativeb
= b
->type
== reloc_class_relative
;
9396 if (relativea
< relativeb
)
9398 if (relativea
> relativeb
)
9400 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9402 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9404 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9406 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9412 elf_link_sort_cmp2 (const void *A
, const void *B
)
9414 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9415 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9417 if (a
->type
< b
->type
)
9419 if (a
->type
> b
->type
)
9421 if (a
->u
.offset
< b
->u
.offset
)
9423 if (a
->u
.offset
> b
->u
.offset
)
9425 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9427 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9433 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9435 asection
*dynamic_relocs
;
9438 bfd_size_type count
, size
;
9439 size_t i
, ret
, sort_elt
, ext_size
;
9440 bfd_byte
*sort
, *s_non_relative
, *p
;
9441 struct elf_link_sort_rela
*sq
;
9442 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9443 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9444 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9445 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9446 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9447 struct bfd_link_order
*lo
;
9449 bfd_boolean use_rela
;
9451 /* Find a dynamic reloc section. */
9452 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9453 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9454 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9455 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9457 bfd_boolean use_rela_initialised
= FALSE
;
9459 /* This is just here to stop gcc from complaining.
9460 Its initialization checking code is not perfect. */
9463 /* Both sections are present. Examine the sizes
9464 of the indirect sections to help us choose. */
9465 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9466 if (lo
->type
== bfd_indirect_link_order
)
9468 asection
*o
= lo
->u
.indirect
.section
;
9470 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9472 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9473 /* Section size is divisible by both rel and rela sizes.
9474 It is of no help to us. */
9478 /* Section size is only divisible by rela. */
9479 if (use_rela_initialised
&& !use_rela
)
9481 _bfd_error_handler (_("%pB: unable to sort relocs - "
9482 "they are in more than one size"),
9484 bfd_set_error (bfd_error_invalid_operation
);
9490 use_rela_initialised
= TRUE
;
9494 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9496 /* Section size is only divisible by rel. */
9497 if (use_rela_initialised
&& use_rela
)
9499 _bfd_error_handler (_("%pB: unable to sort relocs - "
9500 "they are in more than one size"),
9502 bfd_set_error (bfd_error_invalid_operation
);
9508 use_rela_initialised
= TRUE
;
9513 /* The section size is not divisible by either -
9514 something is wrong. */
9515 _bfd_error_handler (_("%pB: unable to sort relocs - "
9516 "they are of an unknown size"), abfd
);
9517 bfd_set_error (bfd_error_invalid_operation
);
9522 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9523 if (lo
->type
== bfd_indirect_link_order
)
9525 asection
*o
= lo
->u
.indirect
.section
;
9527 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9529 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9530 /* Section size is divisible by both rel and rela sizes.
9531 It is of no help to us. */
9535 /* Section size is only divisible by rela. */
9536 if (use_rela_initialised
&& !use_rela
)
9538 _bfd_error_handler (_("%pB: unable to sort relocs - "
9539 "they are in more than one size"),
9541 bfd_set_error (bfd_error_invalid_operation
);
9547 use_rela_initialised
= TRUE
;
9551 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9553 /* Section size is only divisible by rel. */
9554 if (use_rela_initialised
&& use_rela
)
9556 _bfd_error_handler (_("%pB: unable to sort relocs - "
9557 "they are in more than one size"),
9559 bfd_set_error (bfd_error_invalid_operation
);
9565 use_rela_initialised
= TRUE
;
9570 /* The section size is not divisible by either -
9571 something is wrong. */
9572 _bfd_error_handler (_("%pB: unable to sort relocs - "
9573 "they are of an unknown size"), abfd
);
9574 bfd_set_error (bfd_error_invalid_operation
);
9579 if (! use_rela_initialised
)
9583 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9585 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9592 dynamic_relocs
= rela_dyn
;
9593 ext_size
= bed
->s
->sizeof_rela
;
9594 swap_in
= bed
->s
->swap_reloca_in
;
9595 swap_out
= bed
->s
->swap_reloca_out
;
9599 dynamic_relocs
= rel_dyn
;
9600 ext_size
= bed
->s
->sizeof_rel
;
9601 swap_in
= bed
->s
->swap_reloc_in
;
9602 swap_out
= bed
->s
->swap_reloc_out
;
9606 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9607 if (lo
->type
== bfd_indirect_link_order
)
9608 size
+= lo
->u
.indirect
.section
->size
;
9610 if (size
!= dynamic_relocs
->size
)
9613 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9614 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9616 count
= dynamic_relocs
->size
/ ext_size
;
9619 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9623 (*info
->callbacks
->warning
)
9624 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9628 if (bed
->s
->arch_size
== 32)
9629 r_sym_mask
= ~(bfd_vma
) 0xff;
9631 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9633 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9634 if (lo
->type
== bfd_indirect_link_order
)
9636 bfd_byte
*erel
, *erelend
;
9637 asection
*o
= lo
->u
.indirect
.section
;
9639 if (o
->contents
== NULL
&& o
->size
!= 0)
9641 /* This is a reloc section that is being handled as a normal
9642 section. See bfd_section_from_shdr. We can't combine
9643 relocs in this case. */
9648 erelend
= o
->contents
+ o
->size
;
9649 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9651 while (erel
< erelend
)
9653 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9655 (*swap_in
) (abfd
, erel
, s
->rela
);
9656 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9657 s
->u
.sym_mask
= r_sym_mask
;
9663 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9665 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9667 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9668 if (s
->type
!= reloc_class_relative
)
9674 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9675 for (; i
< count
; i
++, p
+= sort_elt
)
9677 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9678 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9680 sp
->u
.offset
= sq
->rela
->r_offset
;
9683 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9685 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9686 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9688 /* We have plt relocs in .rela.dyn. */
9689 sq
= (struct elf_link_sort_rela
*) sort
;
9690 for (i
= 0; i
< count
; i
++)
9691 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9693 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9695 struct bfd_link_order
**plo
;
9696 /* Put srelplt link_order last. This is so the output_offset
9697 set in the next loop is correct for DT_JMPREL. */
9698 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9699 if ((*plo
)->type
== bfd_indirect_link_order
9700 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9706 plo
= &(*plo
)->next
;
9709 dynamic_relocs
->map_tail
.link_order
= lo
;
9714 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9715 if (lo
->type
== bfd_indirect_link_order
)
9717 bfd_byte
*erel
, *erelend
;
9718 asection
*o
= lo
->u
.indirect
.section
;
9721 erelend
= o
->contents
+ o
->size
;
9722 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9723 while (erel
< erelend
)
9725 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9726 (*swap_out
) (abfd
, s
->rela
, erel
);
9733 *psec
= dynamic_relocs
;
9737 /* Add a symbol to the output symbol string table. */
9740 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
9742 Elf_Internal_Sym
*elfsym
,
9743 asection
*input_sec
,
9744 struct elf_link_hash_entry
*h
)
9746 int (*output_symbol_hook
)
9747 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9748 struct elf_link_hash_entry
*);
9749 struct elf_link_hash_table
*hash_table
;
9750 const struct elf_backend_data
*bed
;
9751 bfd_size_type strtabsize
;
9753 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9755 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9756 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9757 if (output_symbol_hook
!= NULL
)
9759 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9764 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
9765 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
9766 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
9767 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
9771 || (input_sec
->flags
& SEC_EXCLUDE
))
9772 elfsym
->st_name
= (unsigned long) -1;
9775 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9776 to get the final offset for st_name. */
9777 char *versioned_name
= (char *) name
;
9780 if (h
->versioned
== versioned
&& h
->def_dynamic
)
9782 /* Keep only one '@' for versioned symbols defined in
9784 char *version
= strrchr (name
, ELF_VER_CHR
);
9785 char *base_end
= strchr (name
, ELF_VER_CHR
);
9786 if (version
!= base_end
)
9789 size_t len
= strlen (name
);
9790 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
9791 if (versioned_name
== NULL
)
9793 base_len
= base_end
- name
;
9794 memcpy (versioned_name
, name
, base_len
);
9795 memcpy (versioned_name
+ base_len
, version
,
9800 else if (flinfo
->info
->unique_symbol
9801 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
9803 struct local_hash_entry
*lh
;
9804 switch (ELF_ST_TYPE (elfsym
->st_info
))
9810 lh
= (struct local_hash_entry
*) bfd_hash_lookup
9811 (&flinfo
->local_hash_table
, name
, TRUE
, FALSE
);
9816 /* Append ".COUNT" to duplicated local symbols. */
9818 size_t base_len
= lh
->size
;
9820 sprintf (buf
, "%lx", lh
->count
);
9823 base_len
= strlen (name
);
9824 lh
->size
= base_len
;
9826 count_len
= strlen (buf
);
9827 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
9828 base_len
+ count_len
+ 2);
9829 if (versioned_name
== NULL
)
9831 memcpy (versioned_name
, name
, base_len
);
9832 versioned_name
[base_len
] = '.';
9833 memcpy (versioned_name
+ base_len
+ 1, buf
,
9841 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9842 versioned_name
, FALSE
);
9843 if (elfsym
->st_name
== (unsigned long) -1)
9847 hash_table
= elf_hash_table (flinfo
->info
);
9848 strtabsize
= hash_table
->strtabsize
;
9849 if (strtabsize
<= hash_table
->strtabcount
)
9851 strtabsize
+= strtabsize
;
9852 hash_table
->strtabsize
= strtabsize
;
9853 strtabsize
*= sizeof (*hash_table
->strtab
);
9855 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9857 if (hash_table
->strtab
== NULL
)
9860 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
9861 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
9862 = hash_table
->strtabcount
;
9863 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
9864 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
9866 flinfo
->output_bfd
->symcount
+= 1;
9867 hash_table
->strtabcount
+= 1;
9872 /* Swap symbols out to the symbol table and flush the output symbols to
9876 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9878 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9881 const struct elf_backend_data
*bed
;
9883 Elf_Internal_Shdr
*hdr
;
9887 if (!hash_table
->strtabcount
)
9890 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9892 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9894 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
9895 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9899 if (flinfo
->symshndxbuf
)
9901 amt
= sizeof (Elf_External_Sym_Shndx
);
9902 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9903 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9904 if (flinfo
->symshndxbuf
== NULL
)
9911 /* Now swap out the symbols. */
9912 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
9914 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
9915 if (elfsym
->sym
.st_name
== (unsigned long) -1)
9916 elfsym
->sym
.st_name
= 0;
9919 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
9920 elfsym
->sym
.st_name
);
9922 /* Inform the linker of the addition of this symbol. */
9924 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
9925 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
9928 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
9929 ((bfd_byte
*) symbuf
9930 + (elfsym
->dest_index
9931 * bed
->s
->sizeof_sym
)),
9932 (flinfo
->symshndxbuf
9933 + elfsym
->destshndx_index
));
9936 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
9937 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
9938 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
9939 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
9940 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
9942 hdr
->sh_size
+= amt
;
9950 free (hash_table
->strtab
);
9951 hash_table
->strtab
= NULL
;
9956 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9959 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9961 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9962 && sym
->st_shndx
< SHN_LORESERVE
)
9964 /* The gABI doesn't support dynamic symbols in output sections
9967 /* xgettext:c-format */
9968 (_("%pB: too many sections: %d (>= %d)"),
9969 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9970 bfd_set_error (bfd_error_nonrepresentable_section
);
9976 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9977 allowing an unsatisfied unversioned symbol in the DSO to match a
9978 versioned symbol that would normally require an explicit version.
9979 We also handle the case that a DSO references a hidden symbol
9980 which may be satisfied by a versioned symbol in another DSO. */
9983 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9984 const struct elf_backend_data
*bed
,
9985 struct elf_link_hash_entry
*h
)
9988 struct elf_link_loaded_list
*loaded
;
9990 if (!is_elf_hash_table (info
->hash
))
9993 /* Check indirect symbol. */
9994 while (h
->root
.type
== bfd_link_hash_indirect
)
9995 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9997 switch (h
->root
.type
)
10003 case bfd_link_hash_undefined
:
10004 case bfd_link_hash_undefweak
:
10005 abfd
= h
->root
.u
.undef
.abfd
;
10007 || (abfd
->flags
& DYNAMIC
) == 0
10008 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
10012 case bfd_link_hash_defined
:
10013 case bfd_link_hash_defweak
:
10014 abfd
= h
->root
.u
.def
.section
->owner
;
10017 case bfd_link_hash_common
:
10018 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10021 BFD_ASSERT (abfd
!= NULL
);
10023 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10025 loaded
= loaded
->next
)
10028 Elf_Internal_Shdr
*hdr
;
10030 size_t extsymcount
;
10032 Elf_Internal_Shdr
*versymhdr
;
10033 Elf_Internal_Sym
*isym
;
10034 Elf_Internal_Sym
*isymend
;
10035 Elf_Internal_Sym
*isymbuf
;
10036 Elf_External_Versym
*ever
;
10037 Elf_External_Versym
*extversym
;
10039 input
= loaded
->abfd
;
10041 /* We check each DSO for a possible hidden versioned definition. */
10043 || elf_dynversym (input
) == 0)
10046 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10048 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10049 if (elf_bad_symtab (input
))
10051 extsymcount
= symcount
;
10056 extsymcount
= symcount
- hdr
->sh_info
;
10057 extsymoff
= hdr
->sh_info
;
10060 if (extsymcount
== 0)
10063 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10065 if (isymbuf
== NULL
)
10068 /* Read in any version definitions. */
10069 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10070 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10071 || (extversym
= (Elf_External_Versym
*)
10072 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10073 versymhdr
->sh_size
)) == NULL
)
10079 ever
= extversym
+ extsymoff
;
10080 isymend
= isymbuf
+ extsymcount
;
10081 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10084 Elf_Internal_Versym iver
;
10085 unsigned short version_index
;
10087 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10088 || isym
->st_shndx
== SHN_UNDEF
)
10091 name
= bfd_elf_string_from_elf_section (input
,
10094 if (strcmp (name
, h
->root
.root
.string
) != 0)
10097 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10099 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10100 && !(h
->def_regular
10101 && h
->forced_local
))
10103 /* If we have a non-hidden versioned sym, then it should
10104 have provided a definition for the undefined sym unless
10105 it is defined in a non-shared object and forced local.
10110 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10111 if (version_index
== 1 || version_index
== 2)
10113 /* This is the base or first version. We can use it. */
10127 /* Convert ELF common symbol TYPE. */
10130 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10132 /* Commom symbol can only appear in relocatable link. */
10133 if (!bfd_link_relocatable (info
))
10135 switch (info
->elf_stt_common
)
10139 case elf_stt_common
:
10142 case no_elf_stt_common
:
10149 /* Add an external symbol to the symbol table. This is called from
10150 the hash table traversal routine. When generating a shared object,
10151 we go through the symbol table twice. The first time we output
10152 anything that might have been forced to local scope in a version
10153 script. The second time we output the symbols that are still
10157 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10159 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10160 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10161 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10163 Elf_Internal_Sym sym
;
10164 asection
*input_sec
;
10165 const struct elf_backend_data
*bed
;
10170 if (h
->root
.type
== bfd_link_hash_warning
)
10172 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10173 if (h
->root
.type
== bfd_link_hash_new
)
10177 /* Decide whether to output this symbol in this pass. */
10178 if (eoinfo
->localsyms
)
10180 if (!h
->forced_local
)
10185 if (h
->forced_local
)
10189 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10191 if (h
->root
.type
== bfd_link_hash_undefined
)
10193 /* If we have an undefined symbol reference here then it must have
10194 come from a shared library that is being linked in. (Undefined
10195 references in regular files have already been handled unless
10196 they are in unreferenced sections which are removed by garbage
10198 bfd_boolean ignore_undef
= FALSE
;
10200 /* Some symbols may be special in that the fact that they're
10201 undefined can be safely ignored - let backend determine that. */
10202 if (bed
->elf_backend_ignore_undef_symbol
)
10203 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10205 /* If we are reporting errors for this situation then do so now. */
10207 && h
->ref_dynamic_nonweak
10208 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10209 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10210 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10212 flinfo
->info
->callbacks
->undefined_symbol
10213 (flinfo
->info
, h
->root
.root
.string
,
10214 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10215 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10216 && !flinfo
->info
->warn_unresolved_syms
);
10219 /* Strip a global symbol defined in a discarded section. */
10224 /* We should also warn if a forced local symbol is referenced from
10225 shared libraries. */
10226 if (bfd_link_executable (flinfo
->info
)
10231 && h
->ref_dynamic_nonweak
10232 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10236 struct elf_link_hash_entry
*hi
= h
;
10238 /* Check indirect symbol. */
10239 while (hi
->root
.type
== bfd_link_hash_indirect
)
10240 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10242 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10243 /* xgettext:c-format */
10244 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10245 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10246 /* xgettext:c-format */
10247 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10249 /* xgettext:c-format */
10250 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10251 def_bfd
= flinfo
->output_bfd
;
10252 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10253 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10254 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10255 h
->root
.root
.string
, def_bfd
);
10256 bfd_set_error (bfd_error_bad_value
);
10257 eoinfo
->failed
= TRUE
;
10261 /* We don't want to output symbols that have never been mentioned by
10262 a regular file, or that we have been told to strip. However, if
10263 h->indx is set to -2, the symbol is used by a reloc and we must
10268 else if ((h
->def_dynamic
10270 || h
->root
.type
== bfd_link_hash_new
)
10272 && !h
->ref_regular
)
10274 else if (flinfo
->info
->strip
== strip_all
)
10276 else if (flinfo
->info
->strip
== strip_some
10277 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10278 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
10280 else if ((h
->root
.type
== bfd_link_hash_defined
10281 || h
->root
.type
== bfd_link_hash_defweak
)
10282 && ((flinfo
->info
->strip_discarded
10283 && discarded_section (h
->root
.u
.def
.section
))
10284 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10285 && h
->root
.u
.def
.section
->owner
!= NULL
10286 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10288 else if ((h
->root
.type
== bfd_link_hash_undefined
10289 || h
->root
.type
== bfd_link_hash_undefweak
)
10290 && h
->root
.u
.undef
.abfd
!= NULL
10291 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10296 /* If we're stripping it, and it's not a dynamic symbol, there's
10297 nothing else to do. However, if it is a forced local symbol or
10298 an ifunc symbol we need to give the backend finish_dynamic_symbol
10299 function a chance to make it dynamic. */
10301 && h
->dynindx
== -1
10302 && type
!= STT_GNU_IFUNC
10303 && !h
->forced_local
)
10307 sym
.st_size
= h
->size
;
10308 sym
.st_other
= h
->other
;
10309 switch (h
->root
.type
)
10312 case bfd_link_hash_new
:
10313 case bfd_link_hash_warning
:
10317 case bfd_link_hash_undefined
:
10318 case bfd_link_hash_undefweak
:
10319 input_sec
= bfd_und_section_ptr
;
10320 sym
.st_shndx
= SHN_UNDEF
;
10323 case bfd_link_hash_defined
:
10324 case bfd_link_hash_defweak
:
10326 input_sec
= h
->root
.u
.def
.section
;
10327 if (input_sec
->output_section
!= NULL
)
10330 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10331 input_sec
->output_section
);
10332 if (sym
.st_shndx
== SHN_BAD
)
10335 /* xgettext:c-format */
10336 (_("%pB: could not find output section %pA for input section %pA"),
10337 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10338 bfd_set_error (bfd_error_nonrepresentable_section
);
10339 eoinfo
->failed
= TRUE
;
10343 /* ELF symbols in relocatable files are section relative,
10344 but in nonrelocatable files they are virtual
10346 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10347 if (!bfd_link_relocatable (flinfo
->info
))
10349 sym
.st_value
+= input_sec
->output_section
->vma
;
10350 if (h
->type
== STT_TLS
)
10352 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10353 if (tls_sec
!= NULL
)
10354 sym
.st_value
-= tls_sec
->vma
;
10360 BFD_ASSERT (input_sec
->owner
== NULL
10361 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10362 sym
.st_shndx
= SHN_UNDEF
;
10363 input_sec
= bfd_und_section_ptr
;
10368 case bfd_link_hash_common
:
10369 input_sec
= h
->root
.u
.c
.p
->section
;
10370 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10371 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10374 case bfd_link_hash_indirect
:
10375 /* These symbols are created by symbol versioning. They point
10376 to the decorated version of the name. For example, if the
10377 symbol foo@@GNU_1.2 is the default, which should be used when
10378 foo is used with no version, then we add an indirect symbol
10379 foo which points to foo@@GNU_1.2. We ignore these symbols,
10380 since the indirected symbol is already in the hash table. */
10384 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10385 switch (h
->root
.type
)
10387 case bfd_link_hash_common
:
10388 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10390 case bfd_link_hash_defined
:
10391 case bfd_link_hash_defweak
:
10392 if (bed
->common_definition (&sym
))
10393 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10397 case bfd_link_hash_undefined
:
10398 case bfd_link_hash_undefweak
:
10404 if (h
->forced_local
)
10406 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10407 /* Turn off visibility on local symbol. */
10408 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10410 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10411 else if (h
->unique_global
&& h
->def_regular
)
10412 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10413 else if (h
->root
.type
== bfd_link_hash_undefweak
10414 || h
->root
.type
== bfd_link_hash_defweak
)
10415 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10417 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10418 sym
.st_target_internal
= h
->target_internal
;
10420 /* Give the processor backend a chance to tweak the symbol value,
10421 and also to finish up anything that needs to be done for this
10422 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10423 forced local syms when non-shared is due to a historical quirk.
10424 STT_GNU_IFUNC symbol must go through PLT. */
10425 if ((h
->type
== STT_GNU_IFUNC
10427 && !bfd_link_relocatable (flinfo
->info
))
10428 || ((h
->dynindx
!= -1
10429 || h
->forced_local
)
10430 && ((bfd_link_pic (flinfo
->info
)
10431 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10432 || h
->root
.type
!= bfd_link_hash_undefweak
))
10433 || !h
->forced_local
)
10434 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10436 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10437 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10439 eoinfo
->failed
= TRUE
;
10444 /* If we are marking the symbol as undefined, and there are no
10445 non-weak references to this symbol from a regular object, then
10446 mark the symbol as weak undefined; if there are non-weak
10447 references, mark the symbol as strong. We can't do this earlier,
10448 because it might not be marked as undefined until the
10449 finish_dynamic_symbol routine gets through with it. */
10450 if (sym
.st_shndx
== SHN_UNDEF
10452 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10453 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10456 type
= ELF_ST_TYPE (sym
.st_info
);
10458 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10459 if (type
== STT_GNU_IFUNC
)
10462 if (h
->ref_regular_nonweak
)
10463 bindtype
= STB_GLOBAL
;
10465 bindtype
= STB_WEAK
;
10466 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10469 /* If this is a symbol defined in a dynamic library, don't use the
10470 symbol size from the dynamic library. Relinking an executable
10471 against a new library may introduce gratuitous changes in the
10472 executable's symbols if we keep the size. */
10473 if (sym
.st_shndx
== SHN_UNDEF
10478 /* If a non-weak symbol with non-default visibility is not defined
10479 locally, it is a fatal error. */
10480 if (!bfd_link_relocatable (flinfo
->info
)
10481 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10482 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10483 && h
->root
.type
== bfd_link_hash_undefined
10484 && !h
->def_regular
)
10488 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10489 /* xgettext:c-format */
10490 msg
= _("%pB: protected symbol `%s' isn't defined");
10491 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10492 /* xgettext:c-format */
10493 msg
= _("%pB: internal symbol `%s' isn't defined");
10495 /* xgettext:c-format */
10496 msg
= _("%pB: hidden symbol `%s' isn't defined");
10497 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10498 bfd_set_error (bfd_error_bad_value
);
10499 eoinfo
->failed
= TRUE
;
10503 /* If this symbol should be put in the .dynsym section, then put it
10504 there now. We already know the symbol index. We also fill in
10505 the entry in the .hash section. */
10506 if (h
->dynindx
!= -1
10507 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10508 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10509 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10513 /* Since there is no version information in the dynamic string,
10514 if there is no version info in symbol version section, we will
10515 have a run-time problem if not linking executable, referenced
10516 by shared library, or not bound locally. */
10517 if (h
->verinfo
.verdef
== NULL
10518 && (!bfd_link_executable (flinfo
->info
)
10520 || !h
->def_regular
))
10522 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10524 if (p
&& p
[1] != '\0')
10527 /* xgettext:c-format */
10528 (_("%pB: no symbol version section for versioned symbol `%s'"),
10529 flinfo
->output_bfd
, h
->root
.root
.string
);
10530 eoinfo
->failed
= TRUE
;
10535 sym
.st_name
= h
->dynstr_index
;
10536 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10537 + h
->dynindx
* bed
->s
->sizeof_sym
);
10538 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10540 eoinfo
->failed
= TRUE
;
10544 /* Inform the linker of the addition of this symbol. */
10546 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
10547 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
10549 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10551 if (flinfo
->hash_sec
!= NULL
)
10553 size_t hash_entry_size
;
10554 bfd_byte
*bucketpos
;
10556 size_t bucketcount
;
10559 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10560 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10563 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10564 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10565 + (bucket
+ 2) * hash_entry_size
);
10566 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10567 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10569 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10570 ((bfd_byte
*) flinfo
->hash_sec
->contents
10571 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10574 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10576 Elf_Internal_Versym iversym
;
10577 Elf_External_Versym
*eversym
;
10579 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10581 if (h
->verinfo
.verdef
== NULL
10582 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10583 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10584 iversym
.vs_vers
= 1;
10586 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10590 if (h
->verinfo
.vertree
== NULL
)
10591 iversym
.vs_vers
= 1;
10593 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10594 if (flinfo
->info
->create_default_symver
)
10598 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10599 defined locally. */
10600 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10601 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10603 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10604 eversym
+= h
->dynindx
;
10605 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10609 /* If the symbol is undefined, and we didn't output it to .dynsym,
10610 strip it from .symtab too. Obviously we can't do this for
10611 relocatable output or when needed for --emit-relocs. */
10612 else if (input_sec
== bfd_und_section_ptr
10614 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10615 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10616 && !bfd_link_relocatable (flinfo
->info
))
10619 /* Also strip others that we couldn't earlier due to dynamic symbol
10623 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10626 /* Output a FILE symbol so that following locals are not associated
10627 with the wrong input file. We need one for forced local symbols
10628 if we've seen more than one FILE symbol or when we have exactly
10629 one FILE symbol but global symbols are present in a file other
10630 than the one with the FILE symbol. We also need one if linker
10631 defined symbols are present. In practice these conditions are
10632 always met, so just emit the FILE symbol unconditionally. */
10633 if (eoinfo
->localsyms
10634 && !eoinfo
->file_sym_done
10635 && eoinfo
->flinfo
->filesym_count
!= 0)
10637 Elf_Internal_Sym fsym
;
10639 memset (&fsym
, 0, sizeof (fsym
));
10640 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10641 fsym
.st_shndx
= SHN_ABS
;
10642 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10643 bfd_und_section_ptr
, NULL
))
10646 eoinfo
->file_sym_done
= TRUE
;
10649 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10650 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10654 eoinfo
->failed
= TRUE
;
10659 else if (h
->indx
== -2)
10665 /* Return TRUE if special handling is done for relocs in SEC against
10666 symbols defined in discarded sections. */
10669 elf_section_ignore_discarded_relocs (asection
*sec
)
10671 const struct elf_backend_data
*bed
;
10673 switch (sec
->sec_info_type
)
10675 case SEC_INFO_TYPE_STABS
:
10676 case SEC_INFO_TYPE_EH_FRAME
:
10677 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10683 bed
= get_elf_backend_data (sec
->owner
);
10684 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10685 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10691 /* Return a mask saying how ld should treat relocations in SEC against
10692 symbols defined in discarded sections. If this function returns
10693 COMPLAIN set, ld will issue a warning message. If this function
10694 returns PRETEND set, and the discarded section was link-once and the
10695 same size as the kept link-once section, ld will pretend that the
10696 symbol was actually defined in the kept section. Otherwise ld will
10697 zero the reloc (at least that is the intent, but some cooperation by
10698 the target dependent code is needed, particularly for REL targets). */
10701 _bfd_elf_default_action_discarded (asection
*sec
)
10703 if (sec
->flags
& SEC_DEBUGGING
)
10706 if (strcmp (".eh_frame", sec
->name
) == 0)
10709 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10712 return COMPLAIN
| PRETEND
;
10715 /* Find a match between a section and a member of a section group. */
10718 match_group_member (asection
*sec
, asection
*group
,
10719 struct bfd_link_info
*info
)
10721 asection
*first
= elf_next_in_group (group
);
10722 asection
*s
= first
;
10726 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10729 s
= elf_next_in_group (s
);
10737 /* Check if the kept section of a discarded section SEC can be used
10738 to replace it. Return the replacement if it is OK. Otherwise return
10742 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10746 kept
= sec
->kept_section
;
10749 if ((kept
->flags
& SEC_GROUP
) != 0)
10750 kept
= match_group_member (sec
, kept
, info
);
10753 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10754 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
10758 /* Get the real kept section. */
10760 for (next
= kept
->kept_section
;
10762 next
= next
->kept_section
)
10766 sec
->kept_section
= kept
;
10771 /* Link an input file into the linker output file. This function
10772 handles all the sections and relocations of the input file at once.
10773 This is so that we only have to read the local symbols once, and
10774 don't have to keep them in memory. */
10777 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10779 int (*relocate_section
)
10780 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10781 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10783 Elf_Internal_Shdr
*symtab_hdr
;
10784 size_t locsymcount
;
10786 Elf_Internal_Sym
*isymbuf
;
10787 Elf_Internal_Sym
*isym
;
10788 Elf_Internal_Sym
*isymend
;
10790 asection
**ppsection
;
10792 const struct elf_backend_data
*bed
;
10793 struct elf_link_hash_entry
**sym_hashes
;
10794 bfd_size_type address_size
;
10795 bfd_vma r_type_mask
;
10797 bfd_boolean have_file_sym
= FALSE
;
10799 output_bfd
= flinfo
->output_bfd
;
10800 bed
= get_elf_backend_data (output_bfd
);
10801 relocate_section
= bed
->elf_backend_relocate_section
;
10803 /* If this is a dynamic object, we don't want to do anything here:
10804 we don't want the local symbols, and we don't want the section
10806 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10809 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10810 if (elf_bad_symtab (input_bfd
))
10812 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10817 locsymcount
= symtab_hdr
->sh_info
;
10818 extsymoff
= symtab_hdr
->sh_info
;
10821 /* Enable GNU OSABI features in the output BFD that are used in the input
10823 if (bed
->elf_osabi
== ELFOSABI_NONE
10824 || bed
->elf_osabi
== ELFOSABI_GNU
10825 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
10826 elf_tdata (output_bfd
)->has_gnu_osabi
10827 |= (elf_tdata (input_bfd
)->has_gnu_osabi
10828 & (bfd_link_relocatable (flinfo
->info
)
10829 ? -1 : ~elf_gnu_osabi_retain
));
10831 /* Read the local symbols. */
10832 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10833 if (isymbuf
== NULL
&& locsymcount
!= 0)
10835 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10836 flinfo
->internal_syms
,
10837 flinfo
->external_syms
,
10838 flinfo
->locsym_shndx
);
10839 if (isymbuf
== NULL
)
10843 /* Find local symbol sections and adjust values of symbols in
10844 SEC_MERGE sections. Write out those local symbols we know are
10845 going into the output file. */
10846 isymend
= isymbuf
+ locsymcount
;
10847 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10849 isym
++, pindex
++, ppsection
++)
10853 Elf_Internal_Sym osym
;
10859 if (elf_bad_symtab (input_bfd
))
10861 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10868 if (isym
->st_shndx
== SHN_UNDEF
)
10869 isec
= bfd_und_section_ptr
;
10870 else if (isym
->st_shndx
== SHN_ABS
)
10871 isec
= bfd_abs_section_ptr
;
10872 else if (isym
->st_shndx
== SHN_COMMON
)
10873 isec
= bfd_com_section_ptr
;
10876 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10879 /* Don't attempt to output symbols with st_shnx in the
10880 reserved range other than SHN_ABS and SHN_COMMON. */
10881 isec
= bfd_und_section_ptr
;
10883 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10884 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10886 _bfd_merged_section_offset (output_bfd
, &isec
,
10887 elf_section_data (isec
)->sec_info
,
10893 /* Don't output the first, undefined, symbol. In fact, don't
10894 output any undefined local symbol. */
10895 if (isec
== bfd_und_section_ptr
)
10898 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10900 /* We never output section symbols. Instead, we use the
10901 section symbol of the corresponding section in the output
10906 /* If we are stripping all symbols, we don't want to output this
10908 if (flinfo
->info
->strip
== strip_all
)
10911 /* If we are discarding all local symbols, we don't want to
10912 output this one. If we are generating a relocatable output
10913 file, then some of the local symbols may be required by
10914 relocs; we output them below as we discover that they are
10916 if (flinfo
->info
->discard
== discard_all
)
10919 /* If this symbol is defined in a section which we are
10920 discarding, we don't need to keep it. */
10921 if (isym
->st_shndx
!= SHN_UNDEF
10922 && isym
->st_shndx
< SHN_LORESERVE
10923 && isec
->output_section
== NULL
10924 && flinfo
->info
->non_contiguous_regions
10925 && flinfo
->info
->non_contiguous_regions_warnings
)
10927 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10928 "discards section `%s' from '%s'\n"),
10929 isec
->name
, bfd_get_filename (isec
->owner
));
10933 if (isym
->st_shndx
!= SHN_UNDEF
10934 && isym
->st_shndx
< SHN_LORESERVE
10935 && bfd_section_removed_from_list (output_bfd
,
10936 isec
->output_section
))
10939 /* Get the name of the symbol. */
10940 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
10945 /* See if we are discarding symbols with this name. */
10946 if ((flinfo
->info
->strip
== strip_some
10947 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
10949 || (((flinfo
->info
->discard
== discard_sec_merge
10950 && (isec
->flags
& SEC_MERGE
)
10951 && !bfd_link_relocatable (flinfo
->info
))
10952 || flinfo
->info
->discard
== discard_l
)
10953 && bfd_is_local_label_name (input_bfd
, name
)))
10956 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
10958 if (input_bfd
->lto_output
)
10959 /* -flto puts a temp file name here. This means builds
10960 are not reproducible. Discard the symbol. */
10962 have_file_sym
= TRUE
;
10963 flinfo
->filesym_count
+= 1;
10965 if (!have_file_sym
)
10967 /* In the absence of debug info, bfd_find_nearest_line uses
10968 FILE symbols to determine the source file for local
10969 function symbols. Provide a FILE symbol here if input
10970 files lack such, so that their symbols won't be
10971 associated with a previous input file. It's not the
10972 source file, but the best we can do. */
10973 const char *filename
;
10974 have_file_sym
= TRUE
;
10975 flinfo
->filesym_count
+= 1;
10976 memset (&osym
, 0, sizeof (osym
));
10977 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10978 osym
.st_shndx
= SHN_ABS
;
10979 if (input_bfd
->lto_output
)
10982 filename
= lbasename (bfd_get_filename (input_bfd
));
10983 if (!elf_link_output_symstrtab (flinfo
, filename
, &osym
,
10984 bfd_abs_section_ptr
, NULL
))
10990 /* Adjust the section index for the output file. */
10991 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10992 isec
->output_section
);
10993 if (osym
.st_shndx
== SHN_BAD
)
10996 /* ELF symbols in relocatable files are section relative, but
10997 in executable files they are virtual addresses. Note that
10998 this code assumes that all ELF sections have an associated
10999 BFD section with a reasonable value for output_offset; below
11000 we assume that they also have a reasonable value for
11001 output_section. Any special sections must be set up to meet
11002 these requirements. */
11003 osym
.st_value
+= isec
->output_offset
;
11004 if (!bfd_link_relocatable (flinfo
->info
))
11006 osym
.st_value
+= isec
->output_section
->vma
;
11007 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
11009 /* STT_TLS symbols are relative to PT_TLS segment base. */
11010 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
11011 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
11013 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
11018 indx
= bfd_get_symcount (output_bfd
);
11019 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11026 if (bed
->s
->arch_size
== 32)
11028 r_type_mask
= 0xff;
11034 r_type_mask
= 0xffffffff;
11039 /* Relocate the contents of each section. */
11040 sym_hashes
= elf_sym_hashes (input_bfd
);
11041 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11043 bfd_byte
*contents
;
11045 if (! o
->linker_mark
)
11047 /* This section was omitted from the link. */
11051 if (!flinfo
->info
->resolve_section_groups
11052 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11054 /* Deal with the group signature symbol. */
11055 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11056 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11057 asection
*osec
= o
->output_section
;
11059 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11060 if (symndx
>= locsymcount
11061 || (elf_bad_symtab (input_bfd
)
11062 && flinfo
->sections
[symndx
] == NULL
))
11064 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
11065 while (h
->root
.type
== bfd_link_hash_indirect
11066 || h
->root
.type
== bfd_link_hash_warning
)
11067 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11068 /* Arrange for symbol to be output. */
11070 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11072 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11074 /* We'll use the output section target_index. */
11075 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11076 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11080 if (flinfo
->indices
[symndx
] == -1)
11082 /* Otherwise output the local symbol now. */
11083 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11084 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11089 name
= bfd_elf_string_from_elf_section (input_bfd
,
11090 symtab_hdr
->sh_link
,
11095 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11097 if (sym
.st_shndx
== SHN_BAD
)
11100 sym
.st_value
+= o
->output_offset
;
11102 indx
= bfd_get_symcount (output_bfd
);
11103 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11108 flinfo
->indices
[symndx
] = indx
;
11112 elf_section_data (osec
)->this_hdr
.sh_info
11113 = flinfo
->indices
[symndx
];
11117 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11118 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11121 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11123 /* Section was created by _bfd_elf_link_create_dynamic_sections
11128 /* Get the contents of the section. They have been cached by a
11129 relaxation routine. Note that o is a section in an input
11130 file, so the contents field will not have been set by any of
11131 the routines which work on output files. */
11132 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11134 contents
= elf_section_data (o
)->this_hdr
.contents
;
11135 if (bed
->caches_rawsize
11137 && o
->rawsize
< o
->size
)
11139 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11140 contents
= flinfo
->contents
;
11145 contents
= flinfo
->contents
;
11146 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
11150 if ((o
->flags
& SEC_RELOC
) != 0)
11152 Elf_Internal_Rela
*internal_relocs
;
11153 Elf_Internal_Rela
*rel
, *relend
;
11154 int action_discarded
;
11157 /* Get the swapped relocs. */
11159 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
11160 flinfo
->internal_relocs
, FALSE
);
11161 if (internal_relocs
== NULL
11162 && o
->reloc_count
> 0)
11165 /* We need to reverse-copy input .ctors/.dtors sections if
11166 they are placed in .init_array/.finit_array for output. */
11167 if (o
->size
> address_size
11168 && ((strncmp (o
->name
, ".ctors", 6) == 0
11169 && strcmp (o
->output_section
->name
,
11170 ".init_array") == 0)
11171 || (strncmp (o
->name
, ".dtors", 6) == 0
11172 && strcmp (o
->output_section
->name
,
11173 ".fini_array") == 0))
11174 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
11176 if (o
->size
* bed
->s
->int_rels_per_ext_rel
11177 != o
->reloc_count
* address_size
)
11180 /* xgettext:c-format */
11181 (_("error: %pB: size of section %pA is not "
11182 "multiple of address size"),
11184 bfd_set_error (bfd_error_bad_value
);
11187 o
->flags
|= SEC_ELF_REVERSE_COPY
;
11190 action_discarded
= -1;
11191 if (!elf_section_ignore_discarded_relocs (o
))
11192 action_discarded
= (*bed
->action_discarded
) (o
);
11194 /* Run through the relocs evaluating complex reloc symbols and
11195 looking for relocs against symbols from discarded sections
11196 or section symbols from removed link-once sections.
11197 Complain about relocs against discarded sections. Zero
11198 relocs against removed link-once sections. */
11200 rel
= internal_relocs
;
11201 relend
= rel
+ o
->reloc_count
;
11202 for ( ; rel
< relend
; rel
++)
11204 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11205 unsigned int s_type
;
11206 asection
**ps
, *sec
;
11207 struct elf_link_hash_entry
*h
= NULL
;
11208 const char *sym_name
;
11210 if (r_symndx
== STN_UNDEF
)
11213 if (r_symndx
>= locsymcount
11214 || (elf_bad_symtab (input_bfd
)
11215 && flinfo
->sections
[r_symndx
] == NULL
))
11217 h
= sym_hashes
[r_symndx
- extsymoff
];
11219 /* Badly formatted input files can contain relocs that
11220 reference non-existant symbols. Check here so that
11221 we do not seg fault. */
11225 /* xgettext:c-format */
11226 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11227 "that references a non-existent global symbol"),
11228 input_bfd
, (uint64_t) rel
->r_info
, o
);
11229 bfd_set_error (bfd_error_bad_value
);
11233 while (h
->root
.type
== bfd_link_hash_indirect
11234 || h
->root
.type
== bfd_link_hash_warning
)
11235 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11239 /* If a plugin symbol is referenced from a non-IR file,
11240 mark the symbol as undefined. Note that the
11241 linker may attach linker created dynamic sections
11242 to the plugin bfd. Symbols defined in linker
11243 created sections are not plugin symbols. */
11244 if ((h
->root
.non_ir_ref_regular
11245 || h
->root
.non_ir_ref_dynamic
)
11246 && (h
->root
.type
== bfd_link_hash_defined
11247 || h
->root
.type
== bfd_link_hash_defweak
)
11248 && (h
->root
.u
.def
.section
->flags
11249 & SEC_LINKER_CREATED
) == 0
11250 && h
->root
.u
.def
.section
->owner
!= NULL
11251 && (h
->root
.u
.def
.section
->owner
->flags
11252 & BFD_PLUGIN
) != 0)
11254 h
->root
.type
= bfd_link_hash_undefined
;
11255 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11259 if (h
->root
.type
== bfd_link_hash_defined
11260 || h
->root
.type
== bfd_link_hash_defweak
)
11261 ps
= &h
->root
.u
.def
.section
;
11263 sym_name
= h
->root
.root
.string
;
11267 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11269 s_type
= ELF_ST_TYPE (sym
->st_info
);
11270 ps
= &flinfo
->sections
[r_symndx
];
11271 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11275 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11276 && !bfd_link_relocatable (flinfo
->info
))
11279 bfd_vma dot
= (rel
->r_offset
11280 + o
->output_offset
+ o
->output_section
->vma
);
11282 printf ("Encountered a complex symbol!");
11283 printf (" (input_bfd %s, section %s, reloc %ld\n",
11284 bfd_get_filename (input_bfd
), o
->name
,
11285 (long) (rel
- internal_relocs
));
11286 printf (" symbol: idx %8.8lx, name %s\n",
11287 r_symndx
, sym_name
);
11288 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11289 (unsigned long) rel
->r_info
,
11290 (unsigned long) rel
->r_offset
);
11292 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11293 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11296 /* Symbol evaluated OK. Update to absolute value. */
11297 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11302 if (action_discarded
!= -1 && ps
!= NULL
)
11304 /* Complain if the definition comes from a
11305 discarded section. */
11306 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11308 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11309 if (action_discarded
& COMPLAIN
)
11310 (*flinfo
->info
->callbacks
->einfo
)
11311 /* xgettext:c-format */
11312 (_("%X`%s' referenced in section `%pA' of %pB: "
11313 "defined in discarded section `%pA' of %pB\n"),
11314 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11316 /* Try to do the best we can to support buggy old
11317 versions of gcc. Pretend that the symbol is
11318 really defined in the kept linkonce section.
11319 FIXME: This is quite broken. Modifying the
11320 symbol here means we will be changing all later
11321 uses of the symbol, not just in this section. */
11322 if (action_discarded
& PRETEND
)
11326 kept
= _bfd_elf_check_kept_section (sec
,
11338 /* Relocate the section by invoking a back end routine.
11340 The back end routine is responsible for adjusting the
11341 section contents as necessary, and (if using Rela relocs
11342 and generating a relocatable output file) adjusting the
11343 reloc addend as necessary.
11345 The back end routine does not have to worry about setting
11346 the reloc address or the reloc symbol index.
11348 The back end routine is given a pointer to the swapped in
11349 internal symbols, and can access the hash table entries
11350 for the external symbols via elf_sym_hashes (input_bfd).
11352 When generating relocatable output, the back end routine
11353 must handle STB_LOCAL/STT_SECTION symbols specially. The
11354 output symbol is going to be a section symbol
11355 corresponding to the output section, which will require
11356 the addend to be adjusted. */
11358 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11359 input_bfd
, o
, contents
,
11367 || bfd_link_relocatable (flinfo
->info
)
11368 || flinfo
->info
->emitrelocations
)
11370 Elf_Internal_Rela
*irela
;
11371 Elf_Internal_Rela
*irelaend
, *irelamid
;
11372 bfd_vma last_offset
;
11373 struct elf_link_hash_entry
**rel_hash
;
11374 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11375 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11376 unsigned int next_erel
;
11377 bfd_boolean rela_normal
;
11378 struct bfd_elf_section_data
*esdi
, *esdo
;
11380 esdi
= elf_section_data (o
);
11381 esdo
= elf_section_data (o
->output_section
);
11382 rela_normal
= FALSE
;
11384 /* Adjust the reloc addresses and symbol indices. */
11386 irela
= internal_relocs
;
11387 irelaend
= irela
+ o
->reloc_count
;
11388 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
11389 /* We start processing the REL relocs, if any. When we reach
11390 IRELAMID in the loop, we switch to the RELA relocs. */
11392 if (esdi
->rel
.hdr
!= NULL
)
11393 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11394 * bed
->s
->int_rels_per_ext_rel
);
11395 rel_hash_list
= rel_hash
;
11396 rela_hash_list
= NULL
;
11397 last_offset
= o
->output_offset
;
11398 if (!bfd_link_relocatable (flinfo
->info
))
11399 last_offset
+= o
->output_section
->vma
;
11400 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11402 unsigned long r_symndx
;
11404 Elf_Internal_Sym sym
;
11406 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11412 if (irela
== irelamid
)
11414 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
11415 rela_hash_list
= rel_hash
;
11416 rela_normal
= bed
->rela_normal
;
11419 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11422 if (irela
->r_offset
>= (bfd_vma
) -2)
11424 /* This is a reloc for a deleted entry or somesuch.
11425 Turn it into an R_*_NONE reloc, at the same
11426 offset as the last reloc. elf_eh_frame.c and
11427 bfd_elf_discard_info rely on reloc offsets
11429 irela
->r_offset
= last_offset
;
11431 irela
->r_addend
= 0;
11435 irela
->r_offset
+= o
->output_offset
;
11437 /* Relocs in an executable have to be virtual addresses. */
11438 if (!bfd_link_relocatable (flinfo
->info
))
11439 irela
->r_offset
+= o
->output_section
->vma
;
11441 last_offset
= irela
->r_offset
;
11443 r_symndx
= irela
->r_info
>> r_sym_shift
;
11444 if (r_symndx
== STN_UNDEF
)
11447 if (r_symndx
>= locsymcount
11448 || (elf_bad_symtab (input_bfd
)
11449 && flinfo
->sections
[r_symndx
] == NULL
))
11451 struct elf_link_hash_entry
*rh
;
11452 unsigned long indx
;
11454 /* This is a reloc against a global symbol. We
11455 have not yet output all the local symbols, so
11456 we do not know the symbol index of any global
11457 symbol. We set the rel_hash entry for this
11458 reloc to point to the global hash table entry
11459 for this symbol. The symbol index is then
11460 set at the end of bfd_elf_final_link. */
11461 indx
= r_symndx
- extsymoff
;
11462 rh
= elf_sym_hashes (input_bfd
)[indx
];
11463 while (rh
->root
.type
== bfd_link_hash_indirect
11464 || rh
->root
.type
== bfd_link_hash_warning
)
11465 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11467 /* Setting the index to -2 tells
11468 elf_link_output_extsym that this symbol is
11469 used by a reloc. */
11470 BFD_ASSERT (rh
->indx
< 0);
11477 /* This is a reloc against a local symbol. */
11480 sym
= isymbuf
[r_symndx
];
11481 sec
= flinfo
->sections
[r_symndx
];
11482 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11484 /* I suppose the backend ought to fill in the
11485 section of any STT_SECTION symbol against a
11486 processor specific section. */
11487 r_symndx
= STN_UNDEF
;
11488 if (bfd_is_abs_section (sec
))
11490 else if (sec
== NULL
|| sec
->owner
== NULL
)
11492 bfd_set_error (bfd_error_bad_value
);
11497 asection
*osec
= sec
->output_section
;
11499 /* If we have discarded a section, the output
11500 section will be the absolute section. In
11501 case of discarded SEC_MERGE sections, use
11502 the kept section. relocate_section should
11503 have already handled discarded linkonce
11505 if (bfd_is_abs_section (osec
)
11506 && sec
->kept_section
!= NULL
11507 && sec
->kept_section
->output_section
!= NULL
)
11509 osec
= sec
->kept_section
->output_section
;
11510 irela
->r_addend
-= osec
->vma
;
11513 if (!bfd_is_abs_section (osec
))
11515 r_symndx
= osec
->target_index
;
11516 if (r_symndx
== STN_UNDEF
)
11518 irela
->r_addend
+= osec
->vma
;
11519 osec
= _bfd_nearby_section (output_bfd
, osec
,
11521 irela
->r_addend
-= osec
->vma
;
11522 r_symndx
= osec
->target_index
;
11527 /* Adjust the addend according to where the
11528 section winds up in the output section. */
11530 irela
->r_addend
+= sec
->output_offset
;
11534 if (flinfo
->indices
[r_symndx
] == -1)
11536 unsigned long shlink
;
11541 if (flinfo
->info
->strip
== strip_all
)
11543 /* You can't do ld -r -s. */
11544 bfd_set_error (bfd_error_invalid_operation
);
11548 /* This symbol was skipped earlier, but
11549 since it is needed by a reloc, we
11550 must output it now. */
11551 shlink
= symtab_hdr
->sh_link
;
11552 name
= (bfd_elf_string_from_elf_section
11553 (input_bfd
, shlink
, sym
.st_name
));
11557 osec
= sec
->output_section
;
11559 _bfd_elf_section_from_bfd_section (output_bfd
,
11561 if (sym
.st_shndx
== SHN_BAD
)
11564 sym
.st_value
+= sec
->output_offset
;
11565 if (!bfd_link_relocatable (flinfo
->info
))
11567 sym
.st_value
+= osec
->vma
;
11568 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11570 struct elf_link_hash_table
*htab
11571 = elf_hash_table (flinfo
->info
);
11573 /* STT_TLS symbols are relative to PT_TLS
11575 if (htab
->tls_sec
!= NULL
)
11576 sym
.st_value
-= htab
->tls_sec
->vma
;
11579 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11584 indx
= bfd_get_symcount (output_bfd
);
11585 ret
= elf_link_output_symstrtab (flinfo
, name
,
11591 flinfo
->indices
[r_symndx
] = indx
;
11596 r_symndx
= flinfo
->indices
[r_symndx
];
11599 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11600 | (irela
->r_info
& r_type_mask
));
11603 /* Swap out the relocs. */
11604 input_rel_hdr
= esdi
->rel
.hdr
;
11605 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11607 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11612 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11613 * bed
->s
->int_rels_per_ext_rel
);
11614 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11617 input_rela_hdr
= esdi
->rela
.hdr
;
11618 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11620 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11629 /* Write out the modified section contents. */
11630 if (bed
->elf_backend_write_section
11631 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11634 /* Section written out. */
11636 else switch (o
->sec_info_type
)
11638 case SEC_INFO_TYPE_STABS
:
11639 if (! (_bfd_write_section_stabs
11641 &elf_hash_table (flinfo
->info
)->stab_info
,
11642 o
, &elf_section_data (o
)->sec_info
, contents
)))
11645 case SEC_INFO_TYPE_MERGE
:
11646 if (! _bfd_write_merged_section (output_bfd
, o
,
11647 elf_section_data (o
)->sec_info
))
11650 case SEC_INFO_TYPE_EH_FRAME
:
11652 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11657 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11659 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11667 if (! (o
->flags
& SEC_EXCLUDE
))
11669 file_ptr offset
= (file_ptr
) o
->output_offset
;
11670 bfd_size_type todo
= o
->size
;
11672 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11674 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
11676 /* Reverse-copy input section to output. */
11679 todo
-= address_size
;
11680 if (! bfd_set_section_contents (output_bfd
,
11688 offset
+= address_size
;
11692 else if (! bfd_set_section_contents (output_bfd
,
11706 /* Generate a reloc when linking an ELF file. This is a reloc
11707 requested by the linker, and does not come from any input file. This
11708 is used to build constructor and destructor tables when linking
11712 elf_reloc_link_order (bfd
*output_bfd
,
11713 struct bfd_link_info
*info
,
11714 asection
*output_section
,
11715 struct bfd_link_order
*link_order
)
11717 reloc_howto_type
*howto
;
11721 struct bfd_elf_section_reloc_data
*reldata
;
11722 struct elf_link_hash_entry
**rel_hash_ptr
;
11723 Elf_Internal_Shdr
*rel_hdr
;
11724 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11725 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11728 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11730 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11733 bfd_set_error (bfd_error_bad_value
);
11737 addend
= link_order
->u
.reloc
.p
->addend
;
11740 reldata
= &esdo
->rel
;
11741 else if (esdo
->rela
.hdr
)
11742 reldata
= &esdo
->rela
;
11749 /* Figure out the symbol index. */
11750 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11751 if (link_order
->type
== bfd_section_reloc_link_order
)
11753 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11754 BFD_ASSERT (indx
!= 0);
11755 *rel_hash_ptr
= NULL
;
11759 struct elf_link_hash_entry
*h
;
11761 /* Treat a reloc against a defined symbol as though it were
11762 actually against the section. */
11763 h
= ((struct elf_link_hash_entry
*)
11764 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11765 link_order
->u
.reloc
.p
->u
.name
,
11766 FALSE
, FALSE
, TRUE
));
11768 && (h
->root
.type
== bfd_link_hash_defined
11769 || h
->root
.type
== bfd_link_hash_defweak
))
11773 section
= h
->root
.u
.def
.section
;
11774 indx
= section
->output_section
->target_index
;
11775 *rel_hash_ptr
= NULL
;
11776 /* It seems that we ought to add the symbol value to the
11777 addend here, but in practice it has already been added
11778 because it was passed to constructor_callback. */
11779 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11781 else if (h
!= NULL
)
11783 /* Setting the index to -2 tells elf_link_output_extsym that
11784 this symbol is used by a reloc. */
11791 (*info
->callbacks
->unattached_reloc
)
11792 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11797 /* If this is an inplace reloc, we must write the addend into the
11799 if (howto
->partial_inplace
&& addend
!= 0)
11801 bfd_size_type size
;
11802 bfd_reloc_status_type rstat
;
11805 const char *sym_name
;
11806 bfd_size_type octets
;
11808 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11809 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11810 if (buf
== NULL
&& size
!= 0)
11812 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11819 case bfd_reloc_outofrange
:
11822 case bfd_reloc_overflow
:
11823 if (link_order
->type
== bfd_section_reloc_link_order
)
11824 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
11826 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11827 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11828 howto
->name
, addend
, NULL
, NULL
,
11833 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
11835 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11842 /* The address of a reloc is relative to the section in a
11843 relocatable file, and is a virtual address in an executable
11845 offset
= link_order
->offset
;
11846 if (! bfd_link_relocatable (info
))
11847 offset
+= output_section
->vma
;
11849 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11851 irel
[i
].r_offset
= offset
;
11852 irel
[i
].r_info
= 0;
11853 irel
[i
].r_addend
= 0;
11855 if (bed
->s
->arch_size
== 32)
11856 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11858 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11860 rel_hdr
= reldata
->hdr
;
11861 erel
= rel_hdr
->contents
;
11862 if (rel_hdr
->sh_type
== SHT_REL
)
11864 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11865 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11869 irel
[0].r_addend
= addend
;
11870 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11871 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11879 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11880 Returns TRUE upon success, FALSE otherwise. */
11883 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11885 bfd_boolean ret
= FALSE
;
11887 const struct elf_backend_data
*bed
;
11889 enum bfd_architecture arch
;
11891 asymbol
**sympp
= NULL
;
11895 elf_symbol_type
*osymbuf
;
11898 implib_bfd
= info
->out_implib_bfd
;
11899 bed
= get_elf_backend_data (abfd
);
11901 if (!bfd_set_format (implib_bfd
, bfd_object
))
11904 /* Use flag from executable but make it a relocatable object. */
11905 flags
= bfd_get_file_flags (abfd
);
11906 flags
&= ~HAS_RELOC
;
11907 if (!bfd_set_start_address (implib_bfd
, 0)
11908 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
11911 /* Copy architecture of output file to import library file. */
11912 arch
= bfd_get_arch (abfd
);
11913 mach
= bfd_get_mach (abfd
);
11914 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11915 && (abfd
->target_defaulted
11916 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11919 /* Get symbol table size. */
11920 symsize
= bfd_get_symtab_upper_bound (abfd
);
11924 /* Read in the symbol table. */
11925 sympp
= (asymbol
**) bfd_malloc (symsize
);
11929 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11933 /* Allow the BFD backend to copy any private header data it
11934 understands from the output BFD to the import library BFD. */
11935 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11938 /* Filter symbols to appear in the import library. */
11939 if (bed
->elf_backend_filter_implib_symbols
)
11940 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11943 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11946 bfd_set_error (bfd_error_no_symbols
);
11947 _bfd_error_handler (_("%pB: no symbol found for import library"),
11953 /* Make symbols absolute. */
11954 amt
= symcount
* sizeof (*osymbuf
);
11955 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
11956 if (osymbuf
== NULL
)
11959 for (src_count
= 0; src_count
< symcount
; src_count
++)
11961 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11962 sizeof (*osymbuf
));
11963 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11964 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11965 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11966 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11967 osymbuf
[src_count
].symbol
.value
;
11968 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11971 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11973 /* Allow the BFD backend to copy any private data it understands
11974 from the output BFD to the import library BFD. This is done last
11975 to permit the routine to look at the filtered symbol table. */
11976 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11979 if (!bfd_close (implib_bfd
))
11990 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11994 if (flinfo
->symstrtab
!= NULL
)
11995 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11996 free (flinfo
->contents
);
11997 free (flinfo
->external_relocs
);
11998 free (flinfo
->internal_relocs
);
11999 free (flinfo
->external_syms
);
12000 free (flinfo
->locsym_shndx
);
12001 free (flinfo
->internal_syms
);
12002 free (flinfo
->indices
);
12003 free (flinfo
->sections
);
12004 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12005 free (flinfo
->symshndxbuf
);
12006 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12008 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12009 free (esdo
->rel
.hashes
);
12010 free (esdo
->rela
.hashes
);
12014 /* Do the final step of an ELF link. */
12017 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12019 bfd_boolean dynamic
;
12020 bfd_boolean emit_relocs
;
12022 struct elf_final_link_info flinfo
;
12024 struct bfd_link_order
*p
;
12026 bfd_size_type max_contents_size
;
12027 bfd_size_type max_external_reloc_size
;
12028 bfd_size_type max_internal_reloc_count
;
12029 bfd_size_type max_sym_count
;
12030 bfd_size_type max_sym_shndx_count
;
12031 Elf_Internal_Sym elfsym
;
12033 Elf_Internal_Shdr
*symtab_hdr
;
12034 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12035 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12036 struct elf_outext_info eoinfo
;
12037 bfd_boolean merged
;
12038 size_t relativecount
= 0;
12039 asection
*reldyn
= 0;
12041 asection
*attr_section
= NULL
;
12042 bfd_vma attr_size
= 0;
12043 const char *std_attrs_section
;
12044 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12045 bfd_boolean sections_removed
;
12048 if (!is_elf_hash_table (htab
))
12051 if (bfd_link_pic (info
))
12052 abfd
->flags
|= DYNAMIC
;
12054 dynamic
= htab
->dynamic_sections_created
;
12055 dynobj
= htab
->dynobj
;
12057 emit_relocs
= (bfd_link_relocatable (info
)
12058 || info
->emitrelocations
);
12060 memset (&flinfo
, 0, sizeof (flinfo
));
12061 flinfo
.info
= info
;
12062 flinfo
.output_bfd
= abfd
;
12063 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12064 if (flinfo
.symstrtab
== NULL
)
12069 flinfo
.hash_sec
= NULL
;
12070 flinfo
.symver_sec
= NULL
;
12074 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12075 /* Note that dynsym_sec can be NULL (on VMS). */
12076 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12077 /* Note that it is OK if symver_sec is NULL. */
12080 if (info
->unique_symbol
12081 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12082 local_hash_newfunc
,
12083 sizeof (struct local_hash_entry
)))
12086 /* The object attributes have been merged. Remove the input
12087 sections from the link, and set the contents of the output
12089 sections_removed
= FALSE
;
12090 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12091 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12093 bfd_boolean remove_section
= FALSE
;
12095 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12096 || strcmp (o
->name
, ".gnu.attributes") == 0)
12098 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12100 asection
*input_section
;
12102 if (p
->type
!= bfd_indirect_link_order
)
12104 input_section
= p
->u
.indirect
.section
;
12105 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12106 elf_link_input_bfd ignores this section. */
12107 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12110 attr_size
= bfd_elf_obj_attr_size (abfd
);
12111 bfd_set_section_size (o
, attr_size
);
12112 /* Skip this section later on. */
12113 o
->map_head
.link_order
= NULL
;
12117 remove_section
= TRUE
;
12119 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12121 /* Remove empty group section from linker output. */
12122 remove_section
= TRUE
;
12124 if (remove_section
)
12126 o
->flags
|= SEC_EXCLUDE
;
12127 bfd_section_list_remove (abfd
, o
);
12128 abfd
->section_count
--;
12129 sections_removed
= TRUE
;
12132 if (sections_removed
)
12133 _bfd_fix_excluded_sec_syms (abfd
, info
);
12135 /* Count up the number of relocations we will output for each output
12136 section, so that we know the sizes of the reloc sections. We
12137 also figure out some maximum sizes. */
12138 max_contents_size
= 0;
12139 max_external_reloc_size
= 0;
12140 max_internal_reloc_count
= 0;
12142 max_sym_shndx_count
= 0;
12144 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12146 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12147 o
->reloc_count
= 0;
12149 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12151 unsigned int reloc_count
= 0;
12152 unsigned int additional_reloc_count
= 0;
12153 struct bfd_elf_section_data
*esdi
= NULL
;
12155 if (p
->type
== bfd_section_reloc_link_order
12156 || p
->type
== bfd_symbol_reloc_link_order
)
12158 else if (p
->type
== bfd_indirect_link_order
)
12162 sec
= p
->u
.indirect
.section
;
12164 /* Mark all sections which are to be included in the
12165 link. This will normally be every section. We need
12166 to do this so that we can identify any sections which
12167 the linker has decided to not include. */
12168 sec
->linker_mark
= TRUE
;
12170 if (sec
->flags
& SEC_MERGE
)
12173 if (sec
->rawsize
> max_contents_size
)
12174 max_contents_size
= sec
->rawsize
;
12175 if (sec
->size
> max_contents_size
)
12176 max_contents_size
= sec
->size
;
12178 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12179 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12183 /* We are interested in just local symbols, not all
12185 if (elf_bad_symtab (sec
->owner
))
12186 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12187 / bed
->s
->sizeof_sym
);
12189 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12191 if (sym_count
> max_sym_count
)
12192 max_sym_count
= sym_count
;
12194 if (sym_count
> max_sym_shndx_count
12195 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12196 max_sym_shndx_count
= sym_count
;
12198 if (esdo
->this_hdr
.sh_type
== SHT_REL
12199 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
12200 /* Some backends use reloc_count in relocation sections
12201 to count particular types of relocs. Of course,
12202 reloc sections themselves can't have relocations. */
12204 else if (emit_relocs
)
12206 reloc_count
= sec
->reloc_count
;
12207 if (bed
->elf_backend_count_additional_relocs
)
12210 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12211 additional_reloc_count
+= c
;
12214 else if (bed
->elf_backend_count_relocs
)
12215 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12217 esdi
= elf_section_data (sec
);
12219 if ((sec
->flags
& SEC_RELOC
) != 0)
12221 size_t ext_size
= 0;
12223 if (esdi
->rel
.hdr
!= NULL
)
12224 ext_size
= esdi
->rel
.hdr
->sh_size
;
12225 if (esdi
->rela
.hdr
!= NULL
)
12226 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12228 if (ext_size
> max_external_reloc_size
)
12229 max_external_reloc_size
= ext_size
;
12230 if (sec
->reloc_count
> max_internal_reloc_count
)
12231 max_internal_reloc_count
= sec
->reloc_count
;
12236 if (reloc_count
== 0)
12239 reloc_count
+= additional_reloc_count
;
12240 o
->reloc_count
+= reloc_count
;
12242 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12246 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12247 esdo
->rel
.count
+= additional_reloc_count
;
12249 if (esdi
->rela
.hdr
)
12251 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12252 esdo
->rela
.count
+= additional_reloc_count
;
12258 esdo
->rela
.count
+= reloc_count
;
12260 esdo
->rel
.count
+= reloc_count
;
12264 if (o
->reloc_count
> 0)
12265 o
->flags
|= SEC_RELOC
;
12268 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12269 set it (this is probably a bug) and if it is set
12270 assign_section_numbers will create a reloc section. */
12271 o
->flags
&=~ SEC_RELOC
;
12274 /* If the SEC_ALLOC flag is not set, force the section VMA to
12275 zero. This is done in elf_fake_sections as well, but forcing
12276 the VMA to 0 here will ensure that relocs against these
12277 sections are handled correctly. */
12278 if ((o
->flags
& SEC_ALLOC
) == 0
12279 && ! o
->user_set_vma
)
12283 if (! bfd_link_relocatable (info
) && merged
)
12284 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12286 /* Figure out the file positions for everything but the symbol table
12287 and the relocs. We set symcount to force assign_section_numbers
12288 to create a symbol table. */
12289 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12290 BFD_ASSERT (! abfd
->output_has_begun
);
12291 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12294 /* Set sizes, and assign file positions for reloc sections. */
12295 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12297 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12298 if ((o
->flags
& SEC_RELOC
) != 0)
12301 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12305 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12309 /* _bfd_elf_compute_section_file_positions makes temporary use
12310 of target_index. Reset it. */
12311 o
->target_index
= 0;
12313 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12314 to count upwards while actually outputting the relocations. */
12315 esdo
->rel
.count
= 0;
12316 esdo
->rela
.count
= 0;
12318 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12319 && !bfd_section_is_ctf (o
))
12321 /* Cache the section contents so that they can be compressed
12322 later. Use bfd_malloc since it will be freed by
12323 bfd_compress_section_contents. */
12324 unsigned char *contents
= esdo
->this_hdr
.contents
;
12325 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
12328 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12329 if (contents
== NULL
)
12331 esdo
->this_hdr
.contents
= contents
;
12335 /* We have now assigned file positions for all the sections except .symtab,
12336 .strtab, and non-loaded reloc and compressed debugging sections. We start
12337 the .symtab section at the current file position, and write directly to it.
12338 We build the .strtab section in memory. */
12339 abfd
->symcount
= 0;
12340 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12341 /* sh_name is set in prep_headers. */
12342 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12343 /* sh_flags, sh_addr and sh_size all start off zero. */
12344 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12345 /* sh_link is set in assign_section_numbers. */
12346 /* sh_info is set below. */
12347 /* sh_offset is set just below. */
12348 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12350 if (max_sym_count
< 20)
12351 max_sym_count
= 20;
12352 htab
->strtabsize
= max_sym_count
;
12353 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12354 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12355 if (htab
->strtab
== NULL
)
12357 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12359 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12360 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12362 if (info
->strip
!= strip_all
|| emit_relocs
)
12364 file_ptr off
= elf_next_file_pos (abfd
);
12366 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
12368 /* Note that at this point elf_next_file_pos (abfd) is
12369 incorrect. We do not yet know the size of the .symtab section.
12370 We correct next_file_pos below, after we do know the size. */
12372 /* Start writing out the symbol table. The first symbol is always a
12374 elfsym
.st_value
= 0;
12375 elfsym
.st_size
= 0;
12376 elfsym
.st_info
= 0;
12377 elfsym
.st_other
= 0;
12378 elfsym
.st_shndx
= SHN_UNDEF
;
12379 elfsym
.st_target_internal
= 0;
12380 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12381 bfd_und_section_ptr
, NULL
) != 1)
12384 /* Output a symbol for each section if asked or they are used for
12385 relocs. These symbols usually have no names. We store the
12386 index of each one in the index field of the section, so that
12387 we can find it again when outputting relocs. */
12389 if (bfd_keep_unused_section_symbols (abfd
) || emit_relocs
)
12391 bfd_boolean name_local_sections
12392 = (bed
->elf_backend_name_local_section_symbols
12393 && bed
->elf_backend_name_local_section_symbols (abfd
));
12394 const char *name
= NULL
;
12396 elfsym
.st_size
= 0;
12397 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12398 elfsym
.st_other
= 0;
12399 elfsym
.st_value
= 0;
12400 elfsym
.st_target_internal
= 0;
12401 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12403 o
= bfd_section_from_elf_index (abfd
, i
);
12406 o
->target_index
= bfd_get_symcount (abfd
);
12407 elfsym
.st_shndx
= i
;
12408 if (!bfd_link_relocatable (info
))
12409 elfsym
.st_value
= o
->vma
;
12410 if (name_local_sections
)
12412 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12420 /* On some targets like Irix 5 the symbol split between local and global
12421 ones recorded in the sh_info field needs to be done between section
12422 and all other symbols. */
12423 if (bed
->elf_backend_elfsym_local_is_section
12424 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12425 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12427 /* Allocate some memory to hold information read in from the input
12429 if (max_contents_size
!= 0)
12431 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12432 if (flinfo
.contents
== NULL
)
12436 if (max_external_reloc_size
!= 0)
12438 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12439 if (flinfo
.external_relocs
== NULL
)
12443 if (max_internal_reloc_count
!= 0)
12445 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12446 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12447 if (flinfo
.internal_relocs
== NULL
)
12451 if (max_sym_count
!= 0)
12453 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12454 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12455 if (flinfo
.external_syms
== NULL
)
12458 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12459 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12460 if (flinfo
.internal_syms
== NULL
)
12463 amt
= max_sym_count
* sizeof (long);
12464 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12465 if (flinfo
.indices
== NULL
)
12468 amt
= max_sym_count
* sizeof (asection
*);
12469 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12470 if (flinfo
.sections
== NULL
)
12474 if (max_sym_shndx_count
!= 0)
12476 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12477 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12478 if (flinfo
.locsym_shndx
== NULL
)
12484 bfd_vma base
, end
= 0; /* Both bytes. */
12487 for (sec
= htab
->tls_sec
;
12488 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12491 bfd_size_type size
= sec
->size
;
12492 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12495 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12497 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12500 size
= ord
->offset
* opb
+ ord
->size
;
12502 end
= sec
->vma
+ size
/ opb
;
12504 base
= htab
->tls_sec
->vma
;
12505 /* Only align end of TLS section if static TLS doesn't have special
12506 alignment requirements. */
12507 if (bed
->static_tls_alignment
== 1)
12508 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12509 htab
->tls_size
= end
- base
;
12512 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12515 /* Since ELF permits relocations to be against local symbols, we
12516 must have the local symbols available when we do the relocations.
12517 Since we would rather only read the local symbols once, and we
12518 would rather not keep them in memory, we handle all the
12519 relocations for a single input file at the same time.
12521 Unfortunately, there is no way to know the total number of local
12522 symbols until we have seen all of them, and the local symbol
12523 indices precede the global symbol indices. This means that when
12524 we are generating relocatable output, and we see a reloc against
12525 a global symbol, we can not know the symbol index until we have
12526 finished examining all the local symbols to see which ones we are
12527 going to output. To deal with this, we keep the relocations in
12528 memory, and don't output them until the end of the link. This is
12529 an unfortunate waste of memory, but I don't see a good way around
12530 it. Fortunately, it only happens when performing a relocatable
12531 link, which is not the common case. FIXME: If keep_memory is set
12532 we could write the relocs out and then read them again; I don't
12533 know how bad the memory loss will be. */
12535 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12536 sub
->output_has_begun
= FALSE
;
12537 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12539 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12541 if (p
->type
== bfd_indirect_link_order
12542 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12543 == bfd_target_elf_flavour
)
12544 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12546 if (! sub
->output_has_begun
)
12548 if (! elf_link_input_bfd (&flinfo
, sub
))
12550 sub
->output_has_begun
= TRUE
;
12553 else if (p
->type
== bfd_section_reloc_link_order
12554 || p
->type
== bfd_symbol_reloc_link_order
)
12556 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12561 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12563 if (p
->type
== bfd_indirect_link_order
12564 && (bfd_get_flavour (sub
)
12565 == bfd_target_elf_flavour
)
12566 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12567 != bed
->s
->elfclass
))
12569 const char *iclass
, *oclass
;
12571 switch (bed
->s
->elfclass
)
12573 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12574 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12575 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12579 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12581 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12582 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12583 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12587 bfd_set_error (bfd_error_wrong_format
);
12589 /* xgettext:c-format */
12590 (_("%pB: file class %s incompatible with %s"),
12591 sub
, iclass
, oclass
);
12600 /* Free symbol buffer if needed. */
12601 if (!info
->reduce_memory_overheads
)
12603 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12604 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
12606 free (elf_tdata (sub
)->symbuf
);
12607 elf_tdata (sub
)->symbuf
= NULL
;
12613 /* Output any global symbols that got converted to local in a
12614 version script or due to symbol visibility. We do this in a
12615 separate step since ELF requires all local symbols to appear
12616 prior to any global symbols. FIXME: We should only do this if
12617 some global symbols were, in fact, converted to become local.
12618 FIXME: Will this work correctly with the Irix 5 linker? */
12619 eoinfo
.failed
= FALSE
;
12620 eoinfo
.flinfo
= &flinfo
;
12621 eoinfo
.localsyms
= TRUE
;
12622 eoinfo
.file_sym_done
= FALSE
;
12623 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12627 goto return_local_hash_table
;
12630 /* If backend needs to output some local symbols not present in the hash
12631 table, do it now. */
12632 if (bed
->elf_backend_output_arch_local_syms
12633 && (info
->strip
!= strip_all
|| emit_relocs
))
12635 typedef int (*out_sym_func
)
12636 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12637 struct elf_link_hash_entry
*);
12639 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12640 (abfd
, info
, &flinfo
,
12641 (out_sym_func
) elf_link_output_symstrtab
)))
12644 goto return_local_hash_table
;
12648 /* That wrote out all the local symbols. Finish up the symbol table
12649 with the global symbols. Even if we want to strip everything we
12650 can, we still need to deal with those global symbols that got
12651 converted to local in a version script. */
12653 /* The sh_info field records the index of the first non local symbol. */
12654 if (!symtab_hdr
->sh_info
)
12655 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12658 && htab
->dynsym
!= NULL
12659 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12661 Elf_Internal_Sym sym
;
12662 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12664 o
= htab
->dynsym
->output_section
;
12665 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12667 /* Write out the section symbols for the output sections. */
12668 if (bfd_link_pic (info
)
12669 || htab
->is_relocatable_executable
)
12675 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12677 sym
.st_target_internal
= 0;
12679 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12685 dynindx
= elf_section_data (s
)->dynindx
;
12688 indx
= elf_section_data (s
)->this_idx
;
12689 BFD_ASSERT (indx
> 0);
12690 sym
.st_shndx
= indx
;
12691 if (! check_dynsym (abfd
, &sym
))
12694 goto return_local_hash_table
;
12696 sym
.st_value
= s
->vma
;
12697 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12699 /* Inform the linker of the addition of this symbol. */
12701 if (info
->callbacks
->ctf_new_dynsym
)
12702 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
12704 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12708 /* Write out the local dynsyms. */
12709 if (htab
->dynlocal
)
12711 struct elf_link_local_dynamic_entry
*e
;
12712 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12717 /* Copy the internal symbol and turn off visibility.
12718 Note that we saved a word of storage and overwrote
12719 the original st_name with the dynstr_index. */
12721 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12722 sym
.st_shndx
= SHN_UNDEF
;
12724 s
= bfd_section_from_elf_index (e
->input_bfd
,
12727 && s
->output_section
!= NULL
12728 && elf_section_data (s
->output_section
) != NULL
)
12731 elf_section_data (s
->output_section
)->this_idx
;
12732 if (! check_dynsym (abfd
, &sym
))
12735 goto return_local_hash_table
;
12737 sym
.st_value
= (s
->output_section
->vma
12739 + e
->isym
.st_value
);
12742 /* Inform the linker of the addition of this symbol. */
12744 if (info
->callbacks
->ctf_new_dynsym
)
12745 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
12747 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12748 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12753 /* We get the global symbols from the hash table. */
12754 eoinfo
.failed
= FALSE
;
12755 eoinfo
.localsyms
= FALSE
;
12756 eoinfo
.flinfo
= &flinfo
;
12757 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12761 goto return_local_hash_table
;
12764 /* If backend needs to output some symbols not present in the hash
12765 table, do it now. */
12766 if (bed
->elf_backend_output_arch_syms
12767 && (info
->strip
!= strip_all
|| emit_relocs
))
12769 typedef int (*out_sym_func
)
12770 (void *, const char *, Elf_Internal_Sym
*, asection
*,
12771 struct elf_link_hash_entry
*);
12773 if (! ((*bed
->elf_backend_output_arch_syms
)
12774 (abfd
, info
, &flinfo
,
12775 (out_sym_func
) elf_link_output_symstrtab
)))
12778 goto return_local_hash_table
;
12782 /* Finalize the .strtab section. */
12783 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12785 /* Swap out the .strtab section. */
12786 if (!elf_link_swap_symbols_out (&flinfo
))
12789 goto return_local_hash_table
;
12792 /* Now we know the size of the symtab section. */
12793 if (bfd_get_symcount (abfd
) > 0)
12795 /* Finish up and write out the symbol string table (.strtab)
12797 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12798 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12800 if (elf_symtab_shndx_list (abfd
))
12802 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12804 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12806 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12807 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12808 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12809 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12810 symtab_shndx_hdr
->sh_size
= amt
;
12812 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12815 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12816 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12819 goto return_local_hash_table
;
12824 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12825 /* sh_name was set in prep_headers. */
12826 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12827 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12828 symstrtab_hdr
->sh_addr
= 0;
12829 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12830 symstrtab_hdr
->sh_entsize
= 0;
12831 symstrtab_hdr
->sh_link
= 0;
12832 symstrtab_hdr
->sh_info
= 0;
12833 /* sh_offset is set just below. */
12834 symstrtab_hdr
->sh_addralign
= 1;
12836 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12838 elf_next_file_pos (abfd
) = off
;
12840 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12841 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12844 goto return_local_hash_table
;
12848 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12850 _bfd_error_handler (_("%pB: failed to generate import library"),
12851 info
->out_implib_bfd
);
12853 goto return_local_hash_table
;
12856 /* Adjust the relocs to have the correct symbol indices. */
12857 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12859 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12862 if ((o
->flags
& SEC_RELOC
) == 0)
12865 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12866 if (esdo
->rel
.hdr
!= NULL
12867 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
12870 goto return_local_hash_table
;
12872 if (esdo
->rela
.hdr
!= NULL
12873 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
12876 goto return_local_hash_table
;
12879 /* Set the reloc_count field to 0 to prevent write_relocs from
12880 trying to swap the relocs out itself. */
12881 o
->reloc_count
= 0;
12884 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12885 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12887 /* If we are linking against a dynamic object, or generating a
12888 shared library, finish up the dynamic linking information. */
12891 bfd_byte
*dyncon
, *dynconend
;
12893 /* Fix up .dynamic entries. */
12894 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12895 BFD_ASSERT (o
!= NULL
);
12897 dyncon
= o
->contents
;
12898 dynconend
= o
->contents
+ o
->size
;
12899 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12901 Elf_Internal_Dyn dyn
;
12904 bfd_size_type sh_size
;
12907 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12914 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12916 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12918 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12919 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12922 dyn
.d_un
.d_val
= relativecount
;
12929 name
= info
->init_function
;
12932 name
= info
->fini_function
;
12935 struct elf_link_hash_entry
*h
;
12937 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
12939 && (h
->root
.type
== bfd_link_hash_defined
12940 || h
->root
.type
== bfd_link_hash_defweak
))
12942 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12943 o
= h
->root
.u
.def
.section
;
12944 if (o
->output_section
!= NULL
)
12945 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12946 + o
->output_offset
);
12949 /* The symbol is imported from another shared
12950 library and does not apply to this one. */
12951 dyn
.d_un
.d_ptr
= 0;
12958 case DT_PREINIT_ARRAYSZ
:
12959 name
= ".preinit_array";
12961 case DT_INIT_ARRAYSZ
:
12962 name
= ".init_array";
12964 case DT_FINI_ARRAYSZ
:
12965 name
= ".fini_array";
12967 o
= bfd_get_section_by_name (abfd
, name
);
12971 (_("could not find section %s"), name
);
12976 (_("warning: %s section has zero size"), name
);
12977 dyn
.d_un
.d_val
= o
->size
;
12980 case DT_PREINIT_ARRAY
:
12981 name
= ".preinit_array";
12983 case DT_INIT_ARRAY
:
12984 name
= ".init_array";
12986 case DT_FINI_ARRAY
:
12987 name
= ".fini_array";
12989 o
= bfd_get_section_by_name (abfd
, name
);
12996 name
= ".gnu.hash";
13005 name
= ".gnu.version_d";
13008 name
= ".gnu.version_r";
13011 name
= ".gnu.version";
13013 o
= bfd_get_linker_section (dynobj
, name
);
13015 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13018 (_("could not find section %s"), name
);
13021 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13024 (_("warning: section '%s' is being made into a note"), name
);
13025 bfd_set_error (bfd_error_nonrepresentable_section
);
13028 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13035 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13041 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13043 Elf_Internal_Shdr
*hdr
;
13045 hdr
= elf_elfsections (abfd
)[i
];
13046 if (hdr
->sh_type
== type
13047 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13049 sh_size
+= hdr
->sh_size
;
13051 || sh_addr
> hdr
->sh_addr
)
13052 sh_addr
= hdr
->sh_addr
;
13056 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13058 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13060 /* Don't count procedure linkage table relocs in the
13061 overall reloc count. */
13062 sh_size
-= htab
->srelplt
->size
;
13064 /* If the size is zero, make the address zero too.
13065 This is to avoid a glibc bug. If the backend
13066 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13067 zero, then we'll put DT_RELA at the end of
13068 DT_JMPREL. glibc will interpret the end of
13069 DT_RELA matching the end of DT_JMPREL as the
13070 case where DT_RELA includes DT_JMPREL, and for
13071 LD_BIND_NOW will decide that processing DT_RELA
13072 will process the PLT relocs too. Net result:
13073 No PLT relocs applied. */
13076 /* If .rela.plt is the first .rela section, exclude
13077 it from DT_RELA. */
13078 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13079 + htab
->srelplt
->output_offset
) * opb
)
13080 sh_addr
+= htab
->srelplt
->size
;
13083 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13084 dyn
.d_un
.d_val
= sh_size
;
13086 dyn
.d_un
.d_ptr
= sh_addr
;
13089 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13093 /* If we have created any dynamic sections, then output them. */
13094 if (dynobj
!= NULL
)
13096 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13099 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13100 if (bfd_link_textrel_check (info
)
13101 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
13103 bfd_byte
*dyncon
, *dynconend
;
13105 dyncon
= o
->contents
;
13106 dynconend
= o
->contents
+ o
->size
;
13107 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13109 Elf_Internal_Dyn dyn
;
13111 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13113 if (dyn
.d_tag
== DT_TEXTREL
)
13115 if (info
->textrel_check
== textrel_check_error
)
13116 info
->callbacks
->einfo
13117 (_("%P%X: read-only segment has dynamic relocations\n"));
13118 else if (bfd_link_dll (info
))
13119 info
->callbacks
->einfo
13120 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13122 info
->callbacks
->einfo
13123 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13129 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13131 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13133 || o
->output_section
== bfd_abs_section_ptr
)
13135 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13137 /* At this point, we are only interested in sections
13138 created by _bfd_elf_link_create_dynamic_sections. */
13141 if (htab
->stab_info
.stabstr
== o
)
13143 if (htab
->eh_info
.hdr_sec
== o
)
13145 if (strcmp (o
->name
, ".dynstr") != 0)
13147 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13148 * bfd_octets_per_byte (abfd
, o
));
13149 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13150 o
->contents
, octets
, o
->size
))
13155 /* The contents of the .dynstr section are actually in a
13159 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13160 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13161 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13167 if (!info
->resolve_section_groups
)
13169 bfd_boolean failed
= FALSE
;
13171 BFD_ASSERT (bfd_link_relocatable (info
));
13172 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13177 /* If we have optimized stabs strings, output them. */
13178 if (htab
->stab_info
.stabstr
!= NULL
)
13180 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13184 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13187 if (info
->callbacks
->emit_ctf
)
13188 info
->callbacks
->emit_ctf ();
13190 elf_final_link_free (abfd
, &flinfo
);
13194 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13195 if (contents
== NULL
)
13197 /* Bail out and fail. */
13199 goto return_local_hash_table
;
13201 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13202 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13206 return_local_hash_table
:
13207 if (info
->unique_symbol
)
13208 bfd_hash_table_free (&flinfo
.local_hash_table
);
13212 elf_final_link_free (abfd
, &flinfo
);
13214 goto return_local_hash_table
;
13217 /* Initialize COOKIE for input bfd ABFD. */
13220 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13221 struct bfd_link_info
*info
, bfd
*abfd
)
13223 Elf_Internal_Shdr
*symtab_hdr
;
13224 const struct elf_backend_data
*bed
;
13226 bed
= get_elf_backend_data (abfd
);
13227 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13229 cookie
->abfd
= abfd
;
13230 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13231 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13232 if (cookie
->bad_symtab
)
13234 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13235 cookie
->extsymoff
= 0;
13239 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13240 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13243 if (bed
->s
->arch_size
== 32)
13244 cookie
->r_sym_shift
= 8;
13246 cookie
->r_sym_shift
= 32;
13248 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13249 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13251 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13252 cookie
->locsymcount
, 0,
13254 if (cookie
->locsyms
== NULL
)
13256 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13259 if (info
->keep_memory
)
13260 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13265 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13268 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13270 Elf_Internal_Shdr
*symtab_hdr
;
13272 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13273 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13274 free (cookie
->locsyms
);
13277 /* Initialize the relocation information in COOKIE for input section SEC
13278 of input bfd ABFD. */
13281 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13282 struct bfd_link_info
*info
, bfd
*abfd
,
13285 if (sec
->reloc_count
== 0)
13287 cookie
->rels
= NULL
;
13288 cookie
->relend
= NULL
;
13292 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
13293 info
->keep_memory
);
13294 if (cookie
->rels
== NULL
)
13296 cookie
->rel
= cookie
->rels
;
13297 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13299 cookie
->rel
= cookie
->rels
;
13303 /* Free the memory allocated by init_reloc_cookie_rels,
13307 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13310 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13311 free (cookie
->rels
);
13314 /* Initialize the whole of COOKIE for input section SEC. */
13317 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13318 struct bfd_link_info
*info
,
13321 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13323 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13328 fini_reloc_cookie (cookie
, sec
->owner
);
13333 /* Free the memory allocated by init_reloc_cookie_for_section,
13337 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13340 fini_reloc_cookie_rels (cookie
, sec
);
13341 fini_reloc_cookie (cookie
, sec
->owner
);
13344 /* Garbage collect unused sections. */
13346 /* Default gc_mark_hook. */
13349 _bfd_elf_gc_mark_hook (asection
*sec
,
13350 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13351 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13352 struct elf_link_hash_entry
*h
,
13353 Elf_Internal_Sym
*sym
)
13357 switch (h
->root
.type
)
13359 case bfd_link_hash_defined
:
13360 case bfd_link_hash_defweak
:
13361 return h
->root
.u
.def
.section
;
13363 case bfd_link_hash_common
:
13364 return h
->root
.u
.c
.p
->section
;
13371 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13376 /* Return the debug definition section. */
13379 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13380 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13381 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13382 struct elf_link_hash_entry
*h
,
13383 Elf_Internal_Sym
*sym
)
13387 /* Return the global debug definition section. */
13388 if ((h
->root
.type
== bfd_link_hash_defined
13389 || h
->root
.type
== bfd_link_hash_defweak
)
13390 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13391 return h
->root
.u
.def
.section
;
13395 /* Return the local debug definition section. */
13396 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13398 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
13405 /* COOKIE->rel describes a relocation against section SEC, which is
13406 a section we've decided to keep. Return the section that contains
13407 the relocation symbol, or NULL if no section contains it. */
13410 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13411 elf_gc_mark_hook_fn gc_mark_hook
,
13412 struct elf_reloc_cookie
*cookie
,
13413 bfd_boolean
*start_stop
)
13415 unsigned long r_symndx
;
13416 struct elf_link_hash_entry
*h
, *hw
;
13418 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13419 if (r_symndx
== STN_UNDEF
)
13422 if (r_symndx
>= cookie
->locsymcount
13423 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13425 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13428 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13432 while (h
->root
.type
== bfd_link_hash_indirect
13433 || h
->root
.type
== bfd_link_hash_warning
)
13434 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13436 /* Keep all aliases of the symbol too. If an object symbol
13437 needs to be copied into .dynbss then all of its aliases
13438 should be present as dynamic symbols, not just the one used
13439 on the copy relocation. */
13441 while (hw
->is_weakalias
)
13447 if (h
->start_stop
&& !h
->root
.ldscript_def
)
13449 if (info
->start_stop_gc
)
13452 /* To work around a glibc bug, mark XXX input sections
13453 when there is a reference to __start_XXX or __stop_XXX
13455 else if (start_stop
!= NULL
)
13457 asection
*s
= h
->u2
.start_stop_section
;
13458 *start_stop
= !s
->gc_mark
;
13463 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13466 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13467 &cookie
->locsyms
[r_symndx
]);
13470 /* COOKIE->rel describes a relocation against section SEC, which is
13471 a section we've decided to keep. Mark the section that contains
13472 the relocation symbol. */
13475 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13477 elf_gc_mark_hook_fn gc_mark_hook
,
13478 struct elf_reloc_cookie
*cookie
)
13481 bfd_boolean start_stop
= FALSE
;
13483 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13484 while (rsec
!= NULL
)
13486 if (!rsec
->gc_mark
)
13488 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13489 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13491 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13496 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13501 /* The mark phase of garbage collection. For a given section, mark
13502 it and any sections in this section's group, and all the sections
13503 which define symbols to which it refers. */
13506 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13508 elf_gc_mark_hook_fn gc_mark_hook
)
13511 asection
*group_sec
, *eh_frame
;
13515 /* Mark all the sections in the group. */
13516 group_sec
= elf_section_data (sec
)->next_in_group
;
13517 if (group_sec
&& !group_sec
->gc_mark
)
13518 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13521 /* Look through the section relocs. */
13523 eh_frame
= elf_eh_frame_section (sec
->owner
);
13524 if ((sec
->flags
& SEC_RELOC
) != 0
13525 && sec
->reloc_count
> 0
13526 && sec
!= eh_frame
)
13528 struct elf_reloc_cookie cookie
;
13530 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13534 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13535 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13540 fini_reloc_cookie_for_section (&cookie
, sec
);
13544 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13546 struct elf_reloc_cookie cookie
;
13548 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13552 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13553 gc_mark_hook
, &cookie
))
13555 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13559 eh_frame
= elf_section_eh_frame_entry (sec
);
13560 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13561 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13567 /* Scan and mark sections in a special or debug section group. */
13570 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13572 /* Point to first section of section group. */
13574 /* Used to iterate the section group. */
13577 bfd_boolean is_special_grp
= TRUE
;
13578 bfd_boolean is_debug_grp
= TRUE
;
13580 /* First scan to see if group contains any section other than debug
13581 and special section. */
13582 ssec
= msec
= elf_next_in_group (grp
);
13585 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13586 is_debug_grp
= FALSE
;
13588 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13589 is_special_grp
= FALSE
;
13591 msec
= elf_next_in_group (msec
);
13593 while (msec
!= ssec
);
13595 /* If this is a pure debug section group or pure special section group,
13596 keep all sections in this group. */
13597 if (is_debug_grp
|| is_special_grp
)
13602 msec
= elf_next_in_group (msec
);
13604 while (msec
!= ssec
);
13608 /* Keep debug and special sections. */
13611 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13612 elf_gc_mark_hook_fn mark_hook
)
13616 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13619 bfd_boolean some_kept
;
13620 bfd_boolean debug_frag_seen
;
13621 bfd_boolean has_kept_debug_info
;
13623 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13625 isec
= ibfd
->sections
;
13626 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13629 /* Ensure all linker created sections are kept,
13630 see if any other section is already marked,
13631 and note if we have any fragmented debug sections. */
13632 debug_frag_seen
= some_kept
= has_kept_debug_info
= FALSE
;
13633 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13635 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13637 else if (isec
->gc_mark
13638 && (isec
->flags
& SEC_ALLOC
) != 0
13639 && elf_section_type (isec
) != SHT_NOTE
)
13643 /* Since all sections, except for backend specific ones,
13644 have been garbage collected, call mark_hook on this
13645 section if any of its linked-to sections is marked. */
13646 asection
*linked_to_sec
;
13647 for (linked_to_sec
= elf_linked_to_section (isec
);
13648 linked_to_sec
!= NULL
&& !linked_to_sec
->linker_mark
;
13649 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13651 if (linked_to_sec
->gc_mark
)
13653 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
13657 linked_to_sec
->linker_mark
= 1;
13659 for (linked_to_sec
= elf_linked_to_section (isec
);
13660 linked_to_sec
!= NULL
&& linked_to_sec
->linker_mark
;
13661 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13662 linked_to_sec
->linker_mark
= 0;
13665 if (!debug_frag_seen
13666 && (isec
->flags
& SEC_DEBUGGING
)
13667 && CONST_STRNEQ (isec
->name
, ".debug_line."))
13668 debug_frag_seen
= TRUE
;
13669 else if (strcmp (bfd_section_name (isec
),
13670 "__patchable_function_entries") == 0
13671 && elf_linked_to_section (isec
) == NULL
)
13672 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
13673 "need linked-to section "
13674 "for --gc-sections\n"),
13675 isec
->owner
, isec
);
13678 /* If no non-note alloc section in this file will be kept, then
13679 we can toss out the debug and special sections. */
13683 /* Keep debug and special sections like .comment when they are
13684 not part of a group. Also keep section groups that contain
13685 just debug sections or special sections. NB: Sections with
13686 linked-to section has been handled above. */
13687 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13689 if ((isec
->flags
& SEC_GROUP
) != 0)
13690 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13691 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13692 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13693 && elf_next_in_group (isec
) == NULL
13694 && elf_linked_to_section (isec
) == NULL
)
13696 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13697 has_kept_debug_info
= TRUE
;
13700 /* Look for CODE sections which are going to be discarded,
13701 and find and discard any fragmented debug sections which
13702 are associated with that code section. */
13703 if (debug_frag_seen
)
13704 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13705 if ((isec
->flags
& SEC_CODE
) != 0
13706 && isec
->gc_mark
== 0)
13711 ilen
= strlen (isec
->name
);
13713 /* Association is determined by the name of the debug
13714 section containing the name of the code section as
13715 a suffix. For example .debug_line.text.foo is a
13716 debug section associated with .text.foo. */
13717 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13721 if (dsec
->gc_mark
== 0
13722 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13725 dlen
= strlen (dsec
->name
);
13728 && strncmp (dsec
->name
+ (dlen
- ilen
),
13729 isec
->name
, ilen
) == 0)
13734 /* Mark debug sections referenced by kept debug sections. */
13735 if (has_kept_debug_info
)
13736 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13738 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13739 if (!_bfd_elf_gc_mark (info
, isec
,
13740 elf_gc_mark_debug_section
))
13747 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13750 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13752 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13756 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13757 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13758 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13761 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13764 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13766 /* When any section in a section group is kept, we keep all
13767 sections in the section group. If the first member of
13768 the section group is excluded, we will also exclude the
13770 if (o
->flags
& SEC_GROUP
)
13772 asection
*first
= elf_next_in_group (o
);
13773 o
->gc_mark
= first
->gc_mark
;
13779 /* Skip sweeping sections already excluded. */
13780 if (o
->flags
& SEC_EXCLUDE
)
13783 /* Since this is early in the link process, it is simple
13784 to remove a section from the output. */
13785 o
->flags
|= SEC_EXCLUDE
;
13787 if (info
->print_gc_sections
&& o
->size
!= 0)
13788 /* xgettext:c-format */
13789 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13797 /* Propagate collected vtable information. This is called through
13798 elf_link_hash_traverse. */
13801 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13803 /* Those that are not vtables. */
13805 || h
->u2
.vtable
== NULL
13806 || h
->u2
.vtable
->parent
== NULL
)
13809 /* Those vtables that do not have parents, we cannot merge. */
13810 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13813 /* If we've already been done, exit. */
13814 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
13817 /* Make sure the parent's table is up to date. */
13818 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
13820 if (h
->u2
.vtable
->used
== NULL
)
13822 /* None of this table's entries were referenced. Re-use the
13824 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13825 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
13830 bfd_boolean
*cu
, *pu
;
13832 /* Or the parent's entries into ours. */
13833 cu
= h
->u2
.vtable
->used
;
13835 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13838 const struct elf_backend_data
*bed
;
13839 unsigned int log_file_align
;
13841 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13842 log_file_align
= bed
->s
->log_file_align
;
13843 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
13858 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
13861 bfd_vma hstart
, hend
;
13862 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13863 const struct elf_backend_data
*bed
;
13864 unsigned int log_file_align
;
13866 /* Take care of both those symbols that do not describe vtables as
13867 well as those that are not loaded. */
13869 || h
->u2
.vtable
== NULL
13870 || h
->u2
.vtable
->parent
== NULL
)
13873 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13874 || h
->root
.type
== bfd_link_hash_defweak
);
13876 sec
= h
->root
.u
.def
.section
;
13877 hstart
= h
->root
.u
.def
.value
;
13878 hend
= hstart
+ h
->size
;
13880 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
13882 return *(bfd_boolean
*) okp
= FALSE
;
13883 bed
= get_elf_backend_data (sec
->owner
);
13884 log_file_align
= bed
->s
->log_file_align
;
13886 relend
= relstart
+ sec
->reloc_count
;
13888 for (rel
= relstart
; rel
< relend
; ++rel
)
13889 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13891 /* If the entry is in use, do nothing. */
13892 if (h
->u2
.vtable
->used
13893 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
13895 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13896 if (h
->u2
.vtable
->used
[entry
])
13899 /* Otherwise, kill it. */
13900 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
13906 /* Mark sections containing dynamically referenced symbols. When
13907 building shared libraries, we must assume that any visible symbol is
13911 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
13913 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
13914 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
13916 if ((h
->root
.type
== bfd_link_hash_defined
13917 || h
->root
.type
== bfd_link_hash_defweak
)
13919 || h
->root
.ldscript_def
13920 || !info
->start_stop_gc
)
13921 && ((h
->ref_dynamic
&& !h
->forced_local
)
13922 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13923 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13924 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13925 && (!bfd_link_executable (info
)
13926 || info
->gc_keep_exported
13927 || info
->export_dynamic
13930 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13931 && (h
->versioned
>= versioned
13932 || !bfd_hide_sym_by_version (info
->version_info
,
13933 h
->root
.root
.string
)))))
13934 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13939 /* Keep all sections containing symbols undefined on the command-line,
13940 and the section containing the entry symbol. */
13943 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13945 struct bfd_sym_chain
*sym
;
13947 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13949 struct elf_link_hash_entry
*h
;
13951 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13952 FALSE
, FALSE
, FALSE
);
13955 && (h
->root
.type
== bfd_link_hash_defined
13956 || h
->root
.type
== bfd_link_hash_defweak
)
13957 && !bfd_is_const_section (h
->root
.u
.def
.section
))
13958 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13963 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13964 struct bfd_link_info
*info
)
13966 bfd
*ibfd
= info
->input_bfds
;
13968 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13971 struct elf_reloc_cookie cookie
;
13973 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13975 sec
= ibfd
->sections
;
13976 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13979 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13982 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13984 if (CONST_STRNEQ (bfd_section_name (sec
), ".eh_frame_entry")
13985 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13987 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13988 fini_reloc_cookie_rels (&cookie
, sec
);
13995 /* Do mark and sweep of unused sections. */
13998 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14000 bfd_boolean ok
= TRUE
;
14002 elf_gc_mark_hook_fn gc_mark_hook
;
14003 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14004 struct elf_link_hash_table
*htab
;
14006 if (!bed
->can_gc_sections
14007 || !is_elf_hash_table (info
->hash
))
14009 _bfd_error_handler(_("warning: gc-sections option ignored"));
14013 bed
->gc_keep (info
);
14014 htab
= elf_hash_table (info
);
14016 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14017 at the .eh_frame section if we can mark the FDEs individually. */
14018 for (sub
= info
->input_bfds
;
14019 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14020 sub
= sub
->link
.next
)
14023 struct elf_reloc_cookie cookie
;
14025 sec
= sub
->sections
;
14026 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14028 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14029 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
14031 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14032 if (elf_section_data (sec
)->sec_info
14033 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14034 elf_eh_frame_section (sub
) = sec
;
14035 fini_reloc_cookie_for_section (&cookie
, sec
);
14036 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14040 /* Apply transitive closure to the vtable entry usage info. */
14041 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14045 /* Kill the vtable relocations that were not used. */
14046 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
14050 /* Mark dynamically referenced symbols. */
14051 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14052 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14054 /* Grovel through relocs to find out who stays ... */
14055 gc_mark_hook
= bed
->gc_mark_hook
;
14056 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14060 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14061 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14062 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14066 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14069 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14070 Also treat note sections as a root, if the section is not part
14071 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14072 well as FINI_ARRAY sections for ld -r. */
14073 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14075 && (o
->flags
& SEC_EXCLUDE
) == 0
14076 && ((o
->flags
& SEC_KEEP
) != 0
14077 || (bfd_link_relocatable (info
)
14078 && ((elf_section_data (o
)->this_hdr
.sh_type
14079 == SHT_PREINIT_ARRAY
)
14080 || (elf_section_data (o
)->this_hdr
.sh_type
14082 || (elf_section_data (o
)->this_hdr
.sh_type
14083 == SHT_FINI_ARRAY
)))
14084 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14085 && elf_next_in_group (o
) == NULL
14086 && elf_linked_to_section (o
) == NULL
)
14087 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14088 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14090 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14095 /* Allow the backend to mark additional target specific sections. */
14096 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14098 /* ... and mark SEC_EXCLUDE for those that go. */
14099 return elf_gc_sweep (abfd
, info
);
14102 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14105 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14107 struct elf_link_hash_entry
*h
,
14110 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14111 struct elf_link_hash_entry
**search
, *child
;
14112 size_t extsymcount
;
14113 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14115 /* The sh_info field of the symtab header tells us where the
14116 external symbols start. We don't care about the local symbols at
14118 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14119 if (!elf_bad_symtab (abfd
))
14120 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14122 sym_hashes
= elf_sym_hashes (abfd
);
14123 sym_hashes_end
= sym_hashes
+ extsymcount
;
14125 /* Hunt down the child symbol, which is in this section at the same
14126 offset as the relocation. */
14127 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14129 if ((child
= *search
) != NULL
14130 && (child
->root
.type
== bfd_link_hash_defined
14131 || child
->root
.type
== bfd_link_hash_defweak
)
14132 && child
->root
.u
.def
.section
== sec
14133 && child
->root
.u
.def
.value
== offset
)
14137 /* xgettext:c-format */
14138 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14139 abfd
, sec
, (uint64_t) offset
);
14140 bfd_set_error (bfd_error_invalid_operation
);
14144 if (!child
->u2
.vtable
)
14146 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14147 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14148 if (!child
->u2
.vtable
)
14153 /* This *should* only be the absolute section. It could potentially
14154 be that someone has defined a non-global vtable though, which
14155 would be bad. It isn't worth paging in the local symbols to be
14156 sure though; that case should simply be handled by the assembler. */
14158 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14161 child
->u2
.vtable
->parent
= h
;
14166 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14169 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14170 struct elf_link_hash_entry
*h
,
14173 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14174 unsigned int log_file_align
= bed
->s
->log_file_align
;
14178 /* xgettext:c-format */
14179 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14181 bfd_set_error (bfd_error_bad_value
);
14187 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14188 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14193 if (addend
>= h
->u2
.vtable
->size
)
14195 size_t size
, bytes
, file_align
;
14196 bfd_boolean
*ptr
= h
->u2
.vtable
->used
;
14198 /* While the symbol is undefined, we have to be prepared to handle
14200 file_align
= 1 << log_file_align
;
14201 if (h
->root
.type
== bfd_link_hash_undefined
)
14202 size
= addend
+ file_align
;
14206 if (addend
>= size
)
14208 /* Oops! We've got a reference past the defined end of
14209 the table. This is probably a bug -- shall we warn? */
14210 size
= addend
+ file_align
;
14213 size
= (size
+ file_align
- 1) & -file_align
;
14215 /* Allocate one extra entry for use as a "done" flag for the
14216 consolidation pass. */
14217 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
14221 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
14227 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14228 * sizeof (bfd_boolean
));
14229 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14233 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
14238 /* And arrange for that done flag to be at index -1. */
14239 h
->u2
.vtable
->used
= ptr
+ 1;
14240 h
->u2
.vtable
->size
= size
;
14243 h
->u2
.vtable
->used
[addend
>> log_file_align
] = TRUE
;
14248 /* Map an ELF section header flag to its corresponding string. */
14252 flagword flag_value
;
14253 } elf_flags_to_name_table
;
14255 static const elf_flags_to_name_table elf_flags_to_names
[] =
14257 { "SHF_WRITE", SHF_WRITE
},
14258 { "SHF_ALLOC", SHF_ALLOC
},
14259 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14260 { "SHF_MERGE", SHF_MERGE
},
14261 { "SHF_STRINGS", SHF_STRINGS
},
14262 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14263 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14264 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14265 { "SHF_GROUP", SHF_GROUP
},
14266 { "SHF_TLS", SHF_TLS
},
14267 { "SHF_MASKOS", SHF_MASKOS
},
14268 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14271 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14273 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14274 struct flag_info
*flaginfo
,
14277 const bfd_vma sh_flags
= elf_section_flags (section
);
14279 if (!flaginfo
->flags_initialized
)
14281 bfd
*obfd
= info
->output_bfd
;
14282 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14283 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14285 int without_hex
= 0;
14287 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14290 flagword (*lookup
) (char *);
14292 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14293 if (lookup
!= NULL
)
14295 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14299 if (tf
->with
== with_flags
)
14300 with_hex
|= hexval
;
14301 else if (tf
->with
== without_flags
)
14302 without_hex
|= hexval
;
14307 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14309 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14311 if (tf
->with
== with_flags
)
14312 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14313 else if (tf
->with
== without_flags
)
14314 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14321 info
->callbacks
->einfo
14322 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14326 flaginfo
->flags_initialized
= TRUE
;
14327 flaginfo
->only_with_flags
|= with_hex
;
14328 flaginfo
->not_with_flags
|= without_hex
;
14331 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14334 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14340 struct alloc_got_off_arg
{
14342 struct bfd_link_info
*info
;
14345 /* We need a special top-level link routine to convert got reference counts
14346 to real got offsets. */
14349 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14351 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14352 bfd
*obfd
= gofarg
->info
->output_bfd
;
14353 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14355 if (h
->got
.refcount
> 0)
14357 h
->got
.offset
= gofarg
->gotoff
;
14358 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14361 h
->got
.offset
= (bfd_vma
) -1;
14366 /* And an accompanying bit to work out final got entry offsets once
14367 we're done. Should be called from final_link. */
14370 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14371 struct bfd_link_info
*info
)
14374 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14376 struct alloc_got_off_arg gofarg
;
14378 BFD_ASSERT (abfd
== info
->output_bfd
);
14380 if (! is_elf_hash_table (info
->hash
))
14383 /* The GOT offset is relative to the .got section, but the GOT header is
14384 put into the .got.plt section, if the backend uses it. */
14385 if (bed
->want_got_plt
)
14388 gotoff
= bed
->got_header_size
;
14390 /* Do the local .got entries first. */
14391 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14393 bfd_signed_vma
*local_got
;
14394 size_t j
, locsymcount
;
14395 Elf_Internal_Shdr
*symtab_hdr
;
14397 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14400 local_got
= elf_local_got_refcounts (i
);
14404 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14405 if (elf_bad_symtab (i
))
14406 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14408 locsymcount
= symtab_hdr
->sh_info
;
14410 for (j
= 0; j
< locsymcount
; ++j
)
14412 if (local_got
[j
] > 0)
14414 local_got
[j
] = gotoff
;
14415 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14418 local_got
[j
] = (bfd_vma
) -1;
14422 /* Then the global .got entries. .plt refcounts are handled by
14423 adjust_dynamic_symbol */
14424 gofarg
.gotoff
= gotoff
;
14425 gofarg
.info
= info
;
14426 elf_link_hash_traverse (elf_hash_table (info
),
14427 elf_gc_allocate_got_offsets
,
14432 /* Many folk need no more in the way of final link than this, once
14433 got entry reference counting is enabled. */
14436 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14438 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14441 /* Invoke the regular ELF backend linker to do all the work. */
14442 return bfd_elf_final_link (abfd
, info
);
14446 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14448 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14450 if (rcookie
->bad_symtab
)
14451 rcookie
->rel
= rcookie
->rels
;
14453 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14455 unsigned long r_symndx
;
14457 if (! rcookie
->bad_symtab
)
14458 if (rcookie
->rel
->r_offset
> offset
)
14460 if (rcookie
->rel
->r_offset
!= offset
)
14463 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14464 if (r_symndx
== STN_UNDEF
)
14467 if (r_symndx
>= rcookie
->locsymcount
14468 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14470 struct elf_link_hash_entry
*h
;
14472 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14474 while (h
->root
.type
== bfd_link_hash_indirect
14475 || h
->root
.type
== bfd_link_hash_warning
)
14476 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14478 if ((h
->root
.type
== bfd_link_hash_defined
14479 || h
->root
.type
== bfd_link_hash_defweak
)
14480 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14481 || h
->root
.u
.def
.section
->kept_section
!= NULL
14482 || discarded_section (h
->root
.u
.def
.section
)))
14487 /* It's not a relocation against a global symbol,
14488 but it could be a relocation against a local
14489 symbol for a discarded section. */
14491 Elf_Internal_Sym
*isym
;
14493 /* Need to: get the symbol; get the section. */
14494 isym
= &rcookie
->locsyms
[r_symndx
];
14495 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14497 && (isec
->kept_section
!= NULL
14498 || discarded_section (isec
)))
14506 /* Discard unneeded references to discarded sections.
14507 Returns -1 on error, 1 if any section's size was changed, 0 if
14508 nothing changed. This function assumes that the relocations are in
14509 sorted order, which is true for all known assemblers. */
14512 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14514 struct elf_reloc_cookie cookie
;
14519 if (info
->traditional_format
14520 || !is_elf_hash_table (info
->hash
))
14523 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14528 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14531 || i
->reloc_count
== 0
14532 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14536 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14539 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14542 if (_bfd_discard_section_stabs (abfd
, i
,
14543 elf_section_data (i
)->sec_info
,
14544 bfd_elf_reloc_symbol_deleted_p
,
14548 fini_reloc_cookie_for_section (&cookie
, i
);
14553 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14554 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14558 int eh_changed
= 0;
14559 unsigned int eh_alignment
; /* Octets. */
14561 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14567 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14570 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14573 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14574 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14575 bfd_elf_reloc_symbol_deleted_p
,
14579 if (i
->size
!= i
->rawsize
)
14583 fini_reloc_cookie_for_section (&cookie
, i
);
14586 eh_alignment
= ((1 << o
->alignment_power
)
14587 * bfd_octets_per_byte (output_bfd
, o
));
14588 /* Skip over zero terminator, and prevent empty sections from
14589 adding alignment padding at the end. */
14590 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14592 i
->flags
|= SEC_EXCLUDE
;
14593 else if (i
->size
> 4)
14595 /* The last non-empty eh_frame section doesn't need padding. */
14598 /* Any prior sections must pad the last FDE out to the output
14599 section alignment. Otherwise we might have zero padding
14600 between sections, which would be seen as a terminator. */
14601 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14603 /* All but the last zero terminator should have been removed. */
14608 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14609 if (i
->size
!= size
)
14617 elf_link_hash_traverse (elf_hash_table (info
),
14618 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14621 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14623 const struct elf_backend_data
*bed
;
14626 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14628 s
= abfd
->sections
;
14629 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14632 bed
= get_elf_backend_data (abfd
);
14634 if (bed
->elf_backend_discard_info
!= NULL
)
14636 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14639 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14642 fini_reloc_cookie (&cookie
, abfd
);
14646 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14647 _bfd_elf_end_eh_frame_parsing (info
);
14649 if (info
->eh_frame_hdr_type
14650 && !bfd_link_relocatable (info
)
14651 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
14658 _bfd_elf_section_already_linked (bfd
*abfd
,
14660 struct bfd_link_info
*info
)
14663 const char *name
, *key
;
14664 struct bfd_section_already_linked
*l
;
14665 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14667 if (sec
->output_section
== bfd_abs_section_ptr
)
14670 flags
= sec
->flags
;
14672 /* Return if it isn't a linkonce section. A comdat group section
14673 also has SEC_LINK_ONCE set. */
14674 if ((flags
& SEC_LINK_ONCE
) == 0)
14677 /* Don't put group member sections on our list of already linked
14678 sections. They are handled as a group via their group section. */
14679 if (elf_sec_group (sec
) != NULL
)
14682 /* For a SHT_GROUP section, use the group signature as the key. */
14684 if ((flags
& SEC_GROUP
) != 0
14685 && elf_next_in_group (sec
) != NULL
14686 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14687 key
= elf_group_name (elf_next_in_group (sec
));
14690 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14691 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
14692 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14695 /* Must be a user linkonce section that doesn't follow gcc's
14696 naming convention. In this case we won't be matching
14697 single member groups. */
14701 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14703 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14705 /* We may have 2 different types of sections on the list: group
14706 sections with a signature of <key> (<key> is some string),
14707 and linkonce sections named .gnu.linkonce.<type>.<key>.
14708 Match like sections. LTO plugin sections are an exception.
14709 They are always named .gnu.linkonce.t.<key> and match either
14710 type of section. */
14711 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14712 && ((flags
& SEC_GROUP
) != 0
14713 || strcmp (name
, l
->sec
->name
) == 0))
14714 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
14715 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14717 /* The section has already been linked. See if we should
14718 issue a warning. */
14719 if (!_bfd_handle_already_linked (sec
, l
, info
))
14722 if (flags
& SEC_GROUP
)
14724 asection
*first
= elf_next_in_group (sec
);
14725 asection
*s
= first
;
14729 s
->output_section
= bfd_abs_section_ptr
;
14730 /* Record which group discards it. */
14731 s
->kept_section
= l
->sec
;
14732 s
= elf_next_in_group (s
);
14733 /* These lists are circular. */
14743 /* A single member comdat group section may be discarded by a
14744 linkonce section and vice versa. */
14745 if ((flags
& SEC_GROUP
) != 0)
14747 asection
*first
= elf_next_in_group (sec
);
14749 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14750 /* Check this single member group against linkonce sections. */
14751 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14752 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14753 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14755 first
->output_section
= bfd_abs_section_ptr
;
14756 first
->kept_section
= l
->sec
;
14757 sec
->output_section
= bfd_abs_section_ptr
;
14762 /* Check this linkonce section against single member groups. */
14763 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14764 if (l
->sec
->flags
& SEC_GROUP
)
14766 asection
*first
= elf_next_in_group (l
->sec
);
14769 && elf_next_in_group (first
) == first
14770 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
14772 sec
->output_section
= bfd_abs_section_ptr
;
14773 sec
->kept_section
= first
;
14778 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14779 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14780 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14781 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14782 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14783 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14784 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14785 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14786 The reverse order cannot happen as there is never a bfd with only the
14787 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14788 matter as here were are looking only for cross-bfd sections. */
14790 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
14791 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14792 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14793 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
14795 if (abfd
!= l
->sec
->owner
)
14796 sec
->output_section
= bfd_abs_section_ptr
;
14800 /* This is the first section with this name. Record it. */
14801 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
14802 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
14803 return sec
->output_section
== bfd_abs_section_ptr
;
14807 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
14809 return sym
->st_shndx
== SHN_COMMON
;
14813 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14819 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14821 return bfd_com_section_ptr
;
14825 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14826 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14827 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14828 bfd
*ibfd ATTRIBUTE_UNUSED
,
14829 unsigned long symndx ATTRIBUTE_UNUSED
)
14831 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14832 return bed
->s
->arch_size
/ 8;
14835 /* Routines to support the creation of dynamic relocs. */
14837 /* Returns the name of the dynamic reloc section associated with SEC. */
14839 static const char *
14840 get_dynamic_reloc_section_name (bfd
* abfd
,
14842 bfd_boolean is_rela
)
14845 const char *old_name
= bfd_section_name (sec
);
14846 const char *prefix
= is_rela
? ".rela" : ".rel";
14848 if (old_name
== NULL
)
14851 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14852 sprintf (name
, "%s%s", prefix
, old_name
);
14857 /* Returns the dynamic reloc section associated with SEC.
14858 If necessary compute the name of the dynamic reloc section based
14859 on SEC's name (looked up in ABFD's string table) and the setting
14863 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
14865 bfd_boolean is_rela
)
14867 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14869 if (reloc_sec
== NULL
)
14871 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14875 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14877 if (reloc_sec
!= NULL
)
14878 elf_section_data (sec
)->sreloc
= reloc_sec
;
14885 /* Returns the dynamic reloc section associated with SEC. If the
14886 section does not exist it is created and attached to the DYNOBJ
14887 bfd and stored in the SRELOC field of SEC's elf_section_data
14890 ALIGNMENT is the alignment for the newly created section and
14891 IS_RELA defines whether the name should be .rela.<SEC's name>
14892 or .rel.<SEC's name>. The section name is looked up in the
14893 string table associated with ABFD. */
14896 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
14898 unsigned int alignment
,
14900 bfd_boolean is_rela
)
14902 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
14904 if (reloc_sec
== NULL
)
14906 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14911 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
14913 if (reloc_sec
== NULL
)
14915 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
14916 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
14917 if ((sec
->flags
& SEC_ALLOC
) != 0)
14918 flags
|= SEC_ALLOC
| SEC_LOAD
;
14920 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
14921 if (reloc_sec
!= NULL
)
14923 /* _bfd_elf_get_sec_type_attr chooses a section type by
14924 name. Override as it may be wrong, eg. for a user
14925 section named "auto" we'll get ".relauto" which is
14926 seen to be a .rela section. */
14927 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
14928 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
14933 elf_section_data (sec
)->sreloc
= reloc_sec
;
14939 /* Copy the ELF symbol type and other attributes for a linker script
14940 assignment from HSRC to HDEST. Generally this should be treated as
14941 if we found a strong non-dynamic definition for HDEST (except that
14942 ld ignores multiple definition errors). */
14944 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
14945 struct bfd_link_hash_entry
*hdest
,
14946 struct bfd_link_hash_entry
*hsrc
)
14948 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
14949 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
14950 Elf_Internal_Sym isym
;
14952 ehdest
->type
= ehsrc
->type
;
14953 ehdest
->target_internal
= ehsrc
->target_internal
;
14955 isym
.st_other
= ehsrc
->other
;
14956 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, TRUE
, FALSE
);
14959 /* Append a RELA relocation REL to section S in BFD. */
14962 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14964 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14965 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
14966 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
14967 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
14970 /* Append a REL relocation REL to section S in BFD. */
14973 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
14975 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14976 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
14977 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
14978 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
14981 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14983 struct bfd_link_hash_entry
*
14984 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
14985 const char *symbol
, asection
*sec
)
14987 struct elf_link_hash_entry
*h
;
14989 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
14990 FALSE
, FALSE
, TRUE
);
14991 /* NB: Common symbols will be turned into definition later. */
14993 && !h
->root
.ldscript_def
14994 && (h
->root
.type
== bfd_link_hash_undefined
14995 || h
->root
.type
== bfd_link_hash_undefweak
14996 || ((h
->ref_regular
|| h
->def_dynamic
)
14998 && h
->root
.type
!= bfd_link_hash_common
)))
15000 bfd_boolean was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15001 h
->verinfo
.verdef
= NULL
;
15002 h
->root
.type
= bfd_link_hash_defined
;
15003 h
->root
.u
.def
.section
= sec
;
15004 h
->root
.u
.def
.value
= 0;
15005 h
->def_regular
= 1;
15006 h
->def_dynamic
= 0;
15008 h
->u2
.start_stop_section
= sec
;
15009 if (symbol
[0] == '.')
15011 /* .startof. and .sizeof. symbols are local. */
15012 const struct elf_backend_data
*bed
;
15013 bed
= get_elf_backend_data (info
->output_bfd
);
15014 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
15018 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15019 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15020 | info
->start_stop_visibility
);
15022 bfd_elf_link_record_dynamic_symbol (info
, h
);
15029 /* Find dynamic relocs for H that apply to read-only sections. */
15032 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15034 struct elf_dyn_relocs
*p
;
15036 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15038 asection
*s
= p
->sec
->output_section
;
15040 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15046 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15047 read-only sections. */
15050 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15054 if (h
->root
.type
== bfd_link_hash_indirect
)
15057 sec
= _bfd_elf_readonly_dynrelocs (h
);
15060 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15062 info
->flags
|= DF_TEXTREL
;
15063 /* xgettext:c-format */
15064 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15065 "in read-only section `%pA'\n"),
15066 sec
->owner
, h
->root
.root
.string
, sec
);
15068 if (bfd_link_textrel_check (info
))
15069 /* xgettext:c-format */
15070 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15071 "in read-only section `%pA'\n"),
15072 sec
->owner
, h
->root
.root
.string
, sec
);
15074 /* Not an error, just cut short the traversal. */
15080 /* Add dynamic tags. */
15083 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15084 bfd_boolean need_dynamic_reloc
)
15086 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15088 if (htab
->dynamic_sections_created
)
15090 /* Add some entries to the .dynamic section. We fill in the
15091 values later, in finish_dynamic_sections, but we must add
15092 the entries now so that we get the correct size for the
15093 .dynamic section. The DT_DEBUG entry is filled in by the
15094 dynamic linker and used by the debugger. */
15095 #define add_dynamic_entry(TAG, VAL) \
15096 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15098 const struct elf_backend_data
*bed
15099 = get_elf_backend_data (output_bfd
);
15101 if (bfd_link_executable (info
))
15103 if (!add_dynamic_entry (DT_DEBUG
, 0))
15107 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15109 /* DT_PLTGOT is used by prelink even if there is no PLT
15111 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15115 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15117 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15118 || !add_dynamic_entry (DT_PLTREL
,
15119 (bed
->rela_plts_and_copies_p
15120 ? DT_RELA
: DT_REL
))
15121 || !add_dynamic_entry (DT_JMPREL
, 0))
15125 if (htab
->tlsdesc_plt
15126 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15127 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15130 if (need_dynamic_reloc
)
15132 if (bed
->rela_plts_and_copies_p
)
15134 if (!add_dynamic_entry (DT_RELA
, 0)
15135 || !add_dynamic_entry (DT_RELASZ
, 0)
15136 || !add_dynamic_entry (DT_RELAENT
,
15137 bed
->s
->sizeof_rela
))
15142 if (!add_dynamic_entry (DT_REL
, 0)
15143 || !add_dynamic_entry (DT_RELSZ
, 0)
15144 || !add_dynamic_entry (DT_RELENT
,
15145 bed
->s
->sizeof_rel
))
15149 /* If any dynamic relocs apply to a read-only section,
15150 then we need a DT_TEXTREL entry. */
15151 if ((info
->flags
& DF_TEXTREL
) == 0)
15152 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15155 if ((info
->flags
& DF_TEXTREL
) != 0)
15157 if (htab
->ifunc_resolvers
)
15158 info
->callbacks
->einfo
15159 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15160 "may result in a segfault at runtime; recompile with %s\n"),
15161 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15163 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15168 #undef add_dynamic_entry