1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2024 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
43 struct elf_info_failed
45 struct bfd_link_info
*info
;
49 static bool _bfd_elf_fix_symbol_flags
50 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
53 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
54 unsigned long r_symndx
,
57 if (r_symndx
>= cookie
->locsymcount
58 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
60 struct elf_link_hash_entry
*h
;
62 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
64 while (h
->root
.type
== bfd_link_hash_indirect
65 || h
->root
.type
== bfd_link_hash_warning
)
66 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
68 if ((h
->root
.type
== bfd_link_hash_defined
69 || h
->root
.type
== bfd_link_hash_defweak
)
70 && discarded_section (h
->root
.u
.def
.section
))
71 return h
->root
.u
.def
.section
;
77 /* It's not a relocation against a global symbol,
78 but it could be a relocation against a local
79 symbol for a discarded section. */
81 Elf_Internal_Sym
*isym
;
83 /* Need to: get the symbol; get the section. */
84 isym
= &cookie
->locsyms
[r_symndx
];
85 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
87 && discard
? discarded_section (isec
) : 1)
93 /* Define a symbol in a dynamic linkage section. */
95 struct elf_link_hash_entry
*
96 _bfd_elf_define_linkage_sym (bfd
*abfd
,
97 struct bfd_link_info
*info
,
101 struct elf_link_hash_entry
*h
;
102 struct bfd_link_hash_entry
*bh
;
103 const struct elf_backend_data
*bed
;
105 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, false, false, false);
108 /* Zap symbol defined in an as-needed lib that wasn't linked.
109 This is a symptom of a larger problem: Absolute symbols
110 defined in shared libraries can't be overridden, because we
111 lose the link to the bfd which is via the symbol section. */
112 h
->root
.type
= bfd_link_hash_new
;
118 bed
= get_elf_backend_data (abfd
);
119 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
120 sec
, 0, NULL
, false, bed
->collect
,
123 h
= (struct elf_link_hash_entry
*) bh
;
124 BFD_ASSERT (h
!= NULL
);
127 h
->root
.linker_def
= 1;
128 h
->type
= STT_OBJECT
;
129 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
130 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
132 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
137 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
141 struct elf_link_hash_entry
*h
;
142 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
143 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
145 /* This function may be called more than once. */
146 if (htab
->sgot
!= NULL
)
149 flags
= bed
->dynamic_sec_flags
;
151 s
= bfd_make_section_anyway_with_flags (abfd
,
152 (bed
->rela_plts_and_copies_p
153 ? ".rela.got" : ".rel.got"),
154 (bed
->dynamic_sec_flags
157 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
161 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
163 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
167 if (bed
->want_got_plt
)
169 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
171 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
176 /* The first bit of the global offset table is the header. */
177 s
->size
+= bed
->got_header_size
;
179 if (bed
->want_got_sym
)
181 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
182 (or .got.plt) section. We don't do this in the linker script
183 because we don't want to define the symbol if we are not creating
184 a global offset table. */
185 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
186 "_GLOBAL_OFFSET_TABLE_");
187 elf_hash_table (info
)->hgot
= h
;
195 /* Create a strtab to hold the dynamic symbol names. */
197 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
199 struct elf_link_hash_table
*hash_table
;
201 hash_table
= elf_hash_table (info
);
202 if (hash_table
->dynobj
== NULL
)
204 /* We may not set dynobj, an input file holding linker created
205 dynamic sections to abfd, which may be a dynamic object with
206 its own dynamic sections. We need to find a normal input file
207 to hold linker created sections if possible. */
208 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
212 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
214 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0
215 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
216 && elf_object_id (ibfd
) == elf_hash_table_id (hash_table
)
217 && !((s
= ibfd
->sections
) != NULL
218 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
))
224 hash_table
->dynobj
= abfd
;
227 if (hash_table
->dynstr
== NULL
)
229 hash_table
->dynstr
= _bfd_elf_strtab_init ();
230 if (hash_table
->dynstr
== NULL
)
236 /* Create some sections which will be filled in with dynamic linking
237 information. ABFD is an input file which requires dynamic sections
238 to be created. The dynamic sections take up virtual memory space
239 when the final executable is run, so we need to create them before
240 addresses are assigned to the output sections. We work out the
241 actual contents and size of these sections later. */
244 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
248 const struct elf_backend_data
*bed
;
249 struct elf_link_hash_entry
*h
;
251 if (! is_elf_hash_table (info
->hash
))
254 if (elf_hash_table (info
)->dynamic_sections_created
)
257 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
260 abfd
= elf_hash_table (info
)->dynobj
;
261 bed
= get_elf_backend_data (abfd
);
263 flags
= bed
->dynamic_sec_flags
;
265 /* A dynamically linked executable has a .interp section, but a
266 shared library does not. */
267 if (bfd_link_executable (info
) && !info
->nointerp
)
269 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
270 flags
| SEC_READONLY
);
275 /* Create sections to hold version informations. These are removed
276 if they are not needed. */
277 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
278 flags
| SEC_READONLY
);
280 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
283 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
284 flags
| SEC_READONLY
);
286 || !bfd_set_section_alignment (s
, 1))
289 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
290 flags
| SEC_READONLY
);
292 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
295 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
296 flags
| SEC_READONLY
);
298 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
300 elf_hash_table (info
)->dynsym
= s
;
302 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
303 flags
| SEC_READONLY
);
307 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
309 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
312 /* The special symbol _DYNAMIC is always set to the start of the
313 .dynamic section. We could set _DYNAMIC in a linker script, but we
314 only want to define it if we are, in fact, creating a .dynamic
315 section. We don't want to define it if there is no .dynamic
316 section, since on some ELF platforms the start up code examines it
317 to decide how to initialize the process. */
318 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
319 elf_hash_table (info
)->hdynamic
= h
;
325 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
326 flags
| SEC_READONLY
);
328 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
330 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
333 if (info
->emit_gnu_hash
&& bed
->record_xhash_symbol
== NULL
)
335 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
336 flags
| SEC_READONLY
);
338 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
340 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
341 4 32-bit words followed by variable count of 64-bit words, then
342 variable count of 32-bit words. */
343 if (bed
->s
->arch_size
== 64)
344 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
346 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
349 if (info
->enable_dt_relr
)
351 s
= bfd_make_section_anyway_with_flags (abfd
, ".relr.dyn",
352 (bed
->dynamic_sec_flags
355 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
357 elf_hash_table (info
)->srelrdyn
= s
;
360 /* Let the backend create the rest of the sections. This lets the
361 backend set the right flags. The backend will normally create
362 the .got and .plt sections. */
363 if (bed
->elf_backend_create_dynamic_sections
== NULL
364 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
367 elf_hash_table (info
)->dynamic_sections_created
= true;
372 /* Create dynamic sections when linking against a dynamic object. */
375 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
377 flagword flags
, pltflags
;
378 struct elf_link_hash_entry
*h
;
380 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
381 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
383 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
384 .rel[a].bss sections. */
385 flags
= bed
->dynamic_sec_flags
;
388 if (bed
->plt_not_loaded
)
389 /* We do not clear SEC_ALLOC here because we still want the OS to
390 allocate space for the section; it's just that there's nothing
391 to read in from the object file. */
392 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
394 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
395 if (bed
->plt_readonly
)
396 pltflags
|= SEC_READONLY
;
398 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
400 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
404 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
406 if (bed
->want_plt_sym
)
408 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
409 "_PROCEDURE_LINKAGE_TABLE_");
410 elf_hash_table (info
)->hplt
= h
;
415 s
= bfd_make_section_anyway_with_flags (abfd
,
416 (bed
->rela_plts_and_copies_p
417 ? ".rela.plt" : ".rel.plt"),
418 flags
| SEC_READONLY
);
420 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
424 if (! _bfd_elf_create_got_section (abfd
, info
))
427 if (bed
->want_dynbss
)
429 /* The .dynbss section is a place to put symbols which are defined
430 by dynamic objects, are referenced by regular objects, and are
431 not functions. We must allocate space for them in the process
432 image and use a R_*_COPY reloc to tell the dynamic linker to
433 initialize them at run time. The linker script puts the .dynbss
434 section into the .bss section of the final image. */
435 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
436 SEC_ALLOC
| SEC_LINKER_CREATED
);
441 if (bed
->want_dynrelro
)
443 /* Similarly, but for symbols that were originally in read-only
444 sections. This section doesn't really need to have contents,
445 but make it like other .data.rel.ro sections. */
446 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
453 /* The .rel[a].bss section holds copy relocs. This section is not
454 normally needed. We need to create it here, though, so that the
455 linker will map it to an output section. We can't just create it
456 only if we need it, because we will not know whether we need it
457 until we have seen all the input files, and the first time the
458 main linker code calls BFD after examining all the input files
459 (size_dynamic_sections) the input sections have already been
460 mapped to the output sections. If the section turns out not to
461 be needed, we can discard it later. We will never need this
462 section when generating a shared object, since they do not use
464 if (bfd_link_executable (info
))
466 s
= bfd_make_section_anyway_with_flags (abfd
,
467 (bed
->rela_plts_and_copies_p
468 ? ".rela.bss" : ".rel.bss"),
469 flags
| SEC_READONLY
);
471 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
475 if (bed
->want_dynrelro
)
477 s
= (bfd_make_section_anyway_with_flags
478 (abfd
, (bed
->rela_plts_and_copies_p
479 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
480 flags
| SEC_READONLY
));
482 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
484 htab
->sreldynrelro
= s
;
492 /* Record a new dynamic symbol. We record the dynamic symbols as we
493 read the input files, since we need to have a list of all of them
494 before we can determine the final sizes of the output sections.
495 Note that we may actually call this function even though we are not
496 going to output any dynamic symbols; in some cases we know that a
497 symbol should be in the dynamic symbol table, but only if there is
501 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
502 struct elf_link_hash_entry
*h
)
504 if (h
->dynindx
== -1)
506 struct elf_strtab_hash
*dynstr
;
511 if (h
->root
.type
== bfd_link_hash_defined
512 || h
->root
.type
== bfd_link_hash_defweak
)
514 /* An IR symbol should not be made dynamic. */
515 if (h
->root
.u
.def
.section
!= NULL
516 && h
->root
.u
.def
.section
->owner
!= NULL
517 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
521 /* XXX: The ABI draft says the linker must turn hidden and
522 internal symbols into STB_LOCAL symbols when producing the
523 DSO. However, if ld.so honors st_other in the dynamic table,
524 this would not be necessary. */
525 switch (ELF_ST_VISIBILITY (h
->other
))
529 if (h
->root
.type
!= bfd_link_hash_undefined
530 && h
->root
.type
!= bfd_link_hash_undefweak
)
533 if (!elf_hash_table (info
)->is_relocatable_executable
534 || ((h
->root
.type
== bfd_link_hash_defined
535 || h
->root
.type
== bfd_link_hash_defweak
)
536 && h
->root
.u
.def
.section
->owner
!= NULL
537 && h
->root
.u
.def
.section
->owner
->no_export
)
538 || (h
->root
.type
== bfd_link_hash_common
539 && h
->root
.u
.c
.p
->section
->owner
!= NULL
540 && h
->root
.u
.c
.p
->section
->owner
->no_export
))
548 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
549 ++elf_hash_table (info
)->dynsymcount
;
551 dynstr
= elf_hash_table (info
)->dynstr
;
554 /* Create a strtab to hold the dynamic symbol names. */
555 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
560 /* We don't put any version information in the dynamic string
562 name
= h
->root
.root
.string
;
563 p
= strchr (name
, ELF_VER_CHR
);
565 /* We know that the p points into writable memory. In fact,
566 there are only a few symbols that have read-only names, being
567 those like _GLOBAL_OFFSET_TABLE_ that are created specially
568 by the backends. Most symbols will have names pointing into
569 an ELF string table read from a file, or to objalloc memory. */
572 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
577 if (indx
== (size_t) -1)
579 h
->dynstr_index
= indx
;
585 /* Mark a symbol dynamic. */
588 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
589 struct elf_link_hash_entry
*h
,
590 Elf_Internal_Sym
*sym
)
592 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
594 /* It may be called more than once on the same H. */
595 if(h
->dynamic
|| bfd_link_relocatable (info
))
598 if ((info
->dynamic_data
599 && (h
->type
== STT_OBJECT
600 || h
->type
== STT_COMMON
602 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
603 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
606 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
609 /* NB: If a symbol is made dynamic by --dynamic-list, it has
611 h
->root
.non_ir_ref_dynamic
= 1;
615 /* Record an assignment to a symbol made by a linker script. We need
616 this in case some dynamic object refers to this symbol. */
619 bfd_elf_record_link_assignment (bfd
*output_bfd
,
620 struct bfd_link_info
*info
,
625 struct elf_link_hash_entry
*h
, *hv
;
626 struct elf_link_hash_table
*htab
;
627 const struct elf_backend_data
*bed
;
629 if (!is_elf_hash_table (info
->hash
))
632 htab
= elf_hash_table (info
);
633 h
= elf_link_hash_lookup (htab
, name
, !provide
, true, false);
637 if (h
->root
.type
== bfd_link_hash_warning
)
638 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
640 if (h
->versioned
== unknown
)
642 /* Set versioned if symbol version is unknown. */
643 char *version
= strrchr (name
, ELF_VER_CHR
);
646 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
647 h
->versioned
= versioned_hidden
;
649 h
->versioned
= versioned
;
653 /* Symbols defined in a linker script but not referenced anywhere
654 else will have non_elf set. */
657 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
661 switch (h
->root
.type
)
663 case bfd_link_hash_defined
:
664 case bfd_link_hash_defweak
:
665 case bfd_link_hash_common
:
667 case bfd_link_hash_undefweak
:
668 case bfd_link_hash_undefined
:
669 /* Since we're defining the symbol, don't let it seem to have not
670 been defined. record_dynamic_symbol and size_dynamic_sections
671 may depend on this. */
672 h
->root
.type
= bfd_link_hash_new
;
673 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
674 bfd_link_repair_undef_list (&htab
->root
);
676 case bfd_link_hash_new
:
678 case bfd_link_hash_indirect
:
679 /* We had a versioned symbol in a dynamic library. We make the
680 the versioned symbol point to this one. */
681 bed
= get_elf_backend_data (output_bfd
);
683 while (hv
->root
.type
== bfd_link_hash_indirect
684 || hv
->root
.type
== bfd_link_hash_warning
)
685 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
686 /* We don't need to update h->root.u since linker will set them
688 h
->root
.type
= bfd_link_hash_undefined
;
689 hv
->root
.type
= bfd_link_hash_indirect
;
690 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
691 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
698 /* If this symbol is being provided by the linker script, and it is
699 currently defined by a dynamic object, but not by a regular
700 object, then mark it as undefined so that the generic linker will
701 force the correct value. */
705 h
->root
.type
= bfd_link_hash_undefined
;
707 /* If this symbol is currently defined by a dynamic object, but not
708 by a regular object, then clear out any version information because
709 the symbol will not be associated with the dynamic object any
711 if (h
->def_dynamic
&& !h
->def_regular
)
712 h
->verinfo
.verdef
= NULL
;
714 /* Make sure this symbol is not garbage collected. */
721 bed
= get_elf_backend_data (output_bfd
);
722 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
723 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
724 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
727 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
729 if (!bfd_link_relocatable (info
)
731 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
732 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
737 || bfd_link_dll (info
)
738 || elf_hash_table (info
)->is_relocatable_executable
)
742 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
745 /* If this is a weak defined symbol, and we know a corresponding
746 real symbol from the same dynamic object, make sure the real
747 symbol is also made into a dynamic symbol. */
750 struct elf_link_hash_entry
*def
= weakdef (h
);
752 if (def
->dynindx
== -1
753 && !bfd_elf_link_record_dynamic_symbol (info
, def
))
761 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
762 success, and 2 on a failure caused by attempting to record a symbol
763 in a discarded section, eg. a discarded link-once section symbol. */
766 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
771 struct elf_link_local_dynamic_entry
*entry
;
772 struct elf_link_hash_table
*eht
;
773 struct elf_strtab_hash
*dynstr
;
776 Elf_External_Sym_Shndx eshndx
;
777 char esym
[sizeof (Elf64_External_Sym
)];
779 if (! is_elf_hash_table (info
->hash
))
782 /* See if the entry exists already. */
783 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
784 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
787 amt
= sizeof (*entry
);
788 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
792 /* Go find the symbol, so that we can find it's name. */
793 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
794 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
796 bfd_release (input_bfd
, entry
);
800 if (entry
->isym
.st_shndx
!= SHN_UNDEF
801 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
805 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
806 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
808 /* We can still bfd_release here as nothing has done another
809 bfd_alloc. We can't do this later in this function. */
810 bfd_release (input_bfd
, entry
);
815 name
= (bfd_elf_string_from_elf_section
816 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
817 entry
->isym
.st_name
));
819 dynstr
= elf_hash_table (info
)->dynstr
;
822 /* Create a strtab to hold the dynamic symbol names. */
823 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
828 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, false);
829 if (dynstr_index
== (size_t) -1)
831 entry
->isym
.st_name
= dynstr_index
;
833 eht
= elf_hash_table (info
);
835 entry
->next
= eht
->dynlocal
;
836 eht
->dynlocal
= entry
;
837 entry
->input_bfd
= input_bfd
;
838 entry
->input_indx
= input_indx
;
841 /* Whatever binding the symbol had before, it's now local. */
843 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
845 /* The dynindx will be set at the end of size_dynamic_sections. */
850 /* Return the dynindex of a local dynamic symbol. */
853 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
857 struct elf_link_local_dynamic_entry
*e
;
859 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
860 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
865 /* This function is used to renumber the dynamic symbols, if some of
866 them are removed because they are marked as local. This is called
867 via elf_link_hash_traverse. */
870 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
873 size_t *count
= (size_t *) data
;
878 if (h
->dynindx
!= -1)
879 h
->dynindx
= ++(*count
);
885 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
886 STB_LOCAL binding. */
889 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
892 size_t *count
= (size_t *) data
;
894 if (!h
->forced_local
)
897 if (h
->dynindx
!= -1)
898 h
->dynindx
= ++(*count
);
903 /* Return true if the dynamic symbol for a given section should be
904 omitted when creating a shared library. */
906 _bfd_elf_omit_section_dynsym_default (bfd
*output_bfd ATTRIBUTE_UNUSED
,
907 struct bfd_link_info
*info
,
910 struct elf_link_hash_table
*htab
;
913 switch (elf_section_data (p
)->this_hdr
.sh_type
)
917 /* If sh_type is yet undecided, assume it could be
918 SHT_PROGBITS/SHT_NOBITS. */
920 htab
= elf_hash_table (info
);
921 if (htab
->text_index_section
!= NULL
)
922 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
924 return (htab
->dynobj
!= NULL
925 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
926 && ip
->output_section
== p
);
928 /* There shouldn't be section relative relocations
929 against any other section. */
936 _bfd_elf_omit_section_dynsym_all
937 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
938 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
939 asection
*p ATTRIBUTE_UNUSED
)
944 /* Assign dynsym indices. In a shared library we generate a section
945 symbol for each output section, which come first. Next come symbols
946 which have been forced to local binding. Then all of the back-end
947 allocated local dynamic syms, followed by the rest of the global
948 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
949 (This prevents the early call before elf_backend_init_index_section
950 and strip_excluded_output_sections setting dynindx for sections
951 that are stripped.) */
954 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
955 struct bfd_link_info
*info
,
956 unsigned long *section_sym_count
)
958 unsigned long dynsymcount
= 0;
959 bool do_sec
= section_sym_count
!= NULL
;
961 if (bfd_link_pic (info
)
962 || elf_hash_table (info
)->is_relocatable_executable
)
964 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
966 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
967 if ((p
->flags
& SEC_EXCLUDE
) == 0
968 && (p
->flags
& SEC_ALLOC
) != 0
969 && elf_hash_table (info
)->dynamic_relocs
970 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
974 elf_section_data (p
)->dynindx
= dynsymcount
;
977 elf_section_data (p
)->dynindx
= 0;
980 *section_sym_count
= dynsymcount
;
982 elf_link_hash_traverse (elf_hash_table (info
),
983 elf_link_renumber_local_hash_table_dynsyms
,
986 if (elf_hash_table (info
)->dynlocal
)
988 struct elf_link_local_dynamic_entry
*p
;
989 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
990 p
->dynindx
= ++dynsymcount
;
992 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
994 elf_link_hash_traverse (elf_hash_table (info
),
995 elf_link_renumber_hash_table_dynsyms
,
998 /* There is an unused NULL entry at the head of the table which we
999 must account for in our count even if the table is empty since it
1000 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1001 .dynamic section. */
1004 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
1008 /* Merge st_other field. */
1011 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
1012 unsigned int st_other
, asection
*sec
,
1013 bool definition
, bool dynamic
)
1015 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1017 /* If st_other has a processor-specific meaning, specific
1018 code might be needed here. */
1019 if (bed
->elf_backend_merge_symbol_attribute
)
1020 (*bed
->elf_backend_merge_symbol_attribute
) (h
, st_other
, definition
,
1025 unsigned symvis
= ELF_ST_VISIBILITY (st_other
);
1026 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
1028 /* Keep the most constraining visibility. Leave the remainder
1029 of the st_other field to elf_backend_merge_symbol_attribute. */
1030 if (symvis
- 1 < hvis
- 1)
1031 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
1034 && ELF_ST_VISIBILITY (st_other
) != STV_DEFAULT
1035 && (sec
->flags
& SEC_READONLY
) == 0)
1036 h
->protected_def
= 1;
1039 /* This function is called when we want to merge a new symbol with an
1040 existing symbol. It handles the various cases which arise when we
1041 find a definition in a dynamic object, or when there is already a
1042 definition in a dynamic object. The new symbol is described by
1043 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1044 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1045 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1046 of an old common symbol. We set OVERRIDE if the old symbol is
1047 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1048 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1049 to change. By OK to change, we mean that we shouldn't warn if the
1050 type or size does change. */
1053 _bfd_elf_merge_symbol (bfd
*abfd
,
1054 struct bfd_link_info
*info
,
1056 Elf_Internal_Sym
*sym
,
1059 struct elf_link_hash_entry
**sym_hash
,
1062 unsigned int *pold_alignment
,
1065 bool *type_change_ok
,
1066 bool *size_change_ok
,
1069 asection
*sec
, *oldsec
;
1070 struct elf_link_hash_entry
*h
;
1071 struct elf_link_hash_entry
*hi
;
1072 struct elf_link_hash_entry
*flip
;
1075 bool newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1076 bool newweak
, oldweak
, newfunc
, oldfunc
;
1077 const struct elf_backend_data
*bed
;
1079 bool default_sym
= *matched
;
1080 struct elf_link_hash_table
*htab
;
1086 bind
= ELF_ST_BIND (sym
->st_info
);
1088 if (! bfd_is_und_section (sec
))
1089 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
1091 h
= ((struct elf_link_hash_entry
*)
1092 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
1097 bed
= get_elf_backend_data (abfd
);
1099 /* NEW_VERSION is the symbol version of the new symbol. */
1100 if (h
->versioned
!= unversioned
)
1102 /* Symbol version is unknown or versioned. */
1103 new_version
= strrchr (name
, ELF_VER_CHR
);
1106 if (h
->versioned
== unknown
)
1108 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1109 h
->versioned
= versioned_hidden
;
1111 h
->versioned
= versioned
;
1114 if (new_version
[0] == '\0')
1118 h
->versioned
= unversioned
;
1123 /* For merging, we only care about real symbols. But we need to make
1124 sure that indirect symbol dynamic flags are updated. */
1126 while (h
->root
.type
== bfd_link_hash_indirect
1127 || h
->root
.type
== bfd_link_hash_warning
)
1128 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1132 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1136 /* OLD_HIDDEN is true if the existing symbol is only visible
1137 to the symbol with the same symbol version. NEW_HIDDEN is
1138 true if the new symbol is only visible to the symbol with
1139 the same symbol version. */
1140 bool old_hidden
= h
->versioned
== versioned_hidden
;
1141 bool new_hidden
= hi
->versioned
== versioned_hidden
;
1142 if (!old_hidden
&& !new_hidden
)
1143 /* The new symbol matches the existing symbol if both
1148 /* OLD_VERSION is the symbol version of the existing
1152 if (h
->versioned
>= versioned
)
1153 old_version
= strrchr (h
->root
.root
.string
,
1158 /* The new symbol matches the existing symbol if they
1159 have the same symbol version. */
1160 *matched
= (old_version
== new_version
1161 || (old_version
!= NULL
1162 && new_version
!= NULL
1163 && strcmp (old_version
, new_version
) == 0));
1168 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1173 switch (h
->root
.type
)
1178 case bfd_link_hash_undefined
:
1179 case bfd_link_hash_undefweak
:
1180 oldbfd
= h
->root
.u
.undef
.abfd
;
1183 case bfd_link_hash_defined
:
1184 case bfd_link_hash_defweak
:
1185 oldbfd
= h
->root
.u
.def
.section
->owner
;
1186 oldsec
= h
->root
.u
.def
.section
;
1189 case bfd_link_hash_common
:
1190 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1191 oldsec
= h
->root
.u
.c
.p
->section
;
1193 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1196 if (poldbfd
&& *poldbfd
== NULL
)
1199 /* Differentiate strong and weak symbols. */
1200 newweak
= bind
== STB_WEAK
;
1201 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1202 || h
->root
.type
== bfd_link_hash_undefweak
);
1204 *pold_weak
= oldweak
;
1206 /* We have to check it for every instance since the first few may be
1207 references and not all compilers emit symbol type for undefined
1209 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1211 htab
= elf_hash_table (info
);
1213 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1214 respectively, is from a dynamic object. */
1216 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1218 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1219 syms and defined syms in dynamic libraries respectively.
1220 ref_dynamic on the other hand can be set for a symbol defined in
1221 a dynamic library, and def_dynamic may not be set; When the
1222 definition in a dynamic lib is overridden by a definition in the
1223 executable use of the symbol in the dynamic lib becomes a
1224 reference to the executable symbol. */
1227 if (bfd_is_und_section (sec
))
1229 if (bind
!= STB_WEAK
)
1231 h
->ref_dynamic_nonweak
= 1;
1232 hi
->ref_dynamic_nonweak
= 1;
1237 /* Update the existing symbol only if they match. */
1240 hi
->dynamic_def
= 1;
1244 /* If we just created the symbol, mark it as being an ELF symbol.
1245 Other than that, there is nothing to do--there is no merge issue
1246 with a newly defined symbol--so we just return. */
1248 if (h
->root
.type
== bfd_link_hash_new
)
1254 /* In cases involving weak versioned symbols, we may wind up trying
1255 to merge a symbol with itself. Catch that here, to avoid the
1256 confusion that results if we try to override a symbol with
1257 itself. The additional tests catch cases like
1258 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1259 dynamic object, which we do want to handle here. */
1261 && (newweak
|| oldweak
)
1262 && ((abfd
->flags
& DYNAMIC
) == 0
1263 || !h
->def_regular
))
1268 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1269 else if (oldsec
!= NULL
)
1271 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1272 indices used by MIPS ELF. */
1273 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1276 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1277 if (!htab
->handling_dt_needed
1279 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
))
1281 if (newdyn
!= olddyn
)
1283 /* Handle a case where plugin_notice won't be called and thus
1284 won't set the non_ir_ref flags on the first pass over
1286 h
->root
.non_ir_ref_dynamic
= true;
1287 hi
->root
.non_ir_ref_dynamic
= true;
1289 else if ((oldbfd
->flags
& BFD_PLUGIN
) != 0
1290 && hi
->root
.type
== bfd_link_hash_indirect
)
1292 /* Change indirect symbol from IR to undefined. */
1293 hi
->root
.type
= bfd_link_hash_undefined
;
1294 hi
->root
.u
.undef
.abfd
= oldbfd
;
1298 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1299 respectively, appear to be a definition rather than reference. */
1301 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1303 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1304 && h
->root
.type
!= bfd_link_hash_undefweak
1305 && h
->root
.type
!= bfd_link_hash_common
);
1307 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1308 respectively, appear to be a function. */
1310 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1311 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1313 oldfunc
= (h
->type
!= STT_NOTYPE
1314 && bed
->is_function_type (h
->type
));
1316 if (!(newfunc
&& oldfunc
)
1317 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1318 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1319 && h
->type
!= STT_NOTYPE
1320 && (newdef
|| bfd_is_com_section (sec
))
1321 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1323 /* If creating a default indirect symbol ("foo" or "foo@") from
1324 a dynamic versioned definition ("foo@@") skip doing so if
1325 there is an existing regular definition with a different
1326 type. We don't want, for example, a "time" variable in the
1327 executable overriding a "time" function in a shared library. */
1335 /* When adding a symbol from a regular object file after we have
1336 created indirect symbols, undo the indirection and any
1343 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1344 h
->forced_local
= 0;
1348 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1350 h
->root
.type
= bfd_link_hash_undefined
;
1351 h
->root
.u
.undef
.abfd
= abfd
;
1355 h
->root
.type
= bfd_link_hash_new
;
1356 h
->root
.u
.undef
.abfd
= NULL
;
1362 /* Check TLS symbols. We don't check undefined symbols introduced
1363 by "ld -u" which have no type (and oldbfd NULL), and we don't
1364 check symbols from plugins because they also have no type. */
1366 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1367 && (abfd
->flags
& BFD_PLUGIN
) == 0
1368 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1369 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1373 asection
*ntsec
, *tsec
;
1375 if (h
->type
== STT_TLS
)
1396 /* xgettext:c-format */
1397 (_("%s: TLS definition in %pB section %pA "
1398 "mismatches non-TLS definition in %pB section %pA"),
1399 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1400 else if (!tdef
&& !ntdef
)
1402 /* xgettext:c-format */
1403 (_("%s: TLS reference in %pB "
1404 "mismatches non-TLS reference in %pB"),
1405 h
->root
.root
.string
, tbfd
, ntbfd
);
1408 /* xgettext:c-format */
1409 (_("%s: TLS definition in %pB section %pA "
1410 "mismatches non-TLS reference in %pB"),
1411 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1414 /* xgettext:c-format */
1415 (_("%s: TLS reference in %pB "
1416 "mismatches non-TLS definition in %pB section %pA"),
1417 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1419 bfd_set_error (bfd_error_bad_value
);
1423 /* If the old symbol has non-default visibility, we ignore the new
1424 definition from a dynamic object. */
1426 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1427 && !bfd_is_und_section (sec
))
1430 /* Make sure this symbol is dynamic. */
1432 hi
->ref_dynamic
= 1;
1433 /* A protected symbol has external availability. Make sure it is
1434 recorded as dynamic.
1436 FIXME: Should we check type and size for protected symbol? */
1437 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1438 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1443 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1446 /* If the new symbol with non-default visibility comes from a
1447 relocatable file and the old definition comes from a dynamic
1448 object, we remove the old definition. */
1449 if (hi
->root
.type
== bfd_link_hash_indirect
)
1451 /* Handle the case where the old dynamic definition is
1452 default versioned. We need to copy the symbol info from
1453 the symbol with default version to the normal one if it
1454 was referenced before. */
1457 hi
->root
.type
= h
->root
.type
;
1458 h
->root
.type
= bfd_link_hash_indirect
;
1459 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1461 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1462 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1464 /* If the new symbol is hidden or internal, completely undo
1465 any dynamic link state. */
1466 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1467 h
->forced_local
= 0;
1474 /* FIXME: Should we check type and size for protected symbol? */
1484 /* If the old symbol was undefined before, then it will still be
1485 on the undefs list. If the new symbol is undefined or
1486 common, we can't make it bfd_link_hash_new here, because new
1487 undefined or common symbols will be added to the undefs list
1488 by _bfd_generic_link_add_one_symbol. Symbols may not be
1489 added twice to the undefs list. Also, if the new symbol is
1490 undefweak then we don't want to lose the strong undef. */
1491 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1493 h
->root
.type
= bfd_link_hash_undefined
;
1494 h
->root
.u
.undef
.abfd
= abfd
;
1498 h
->root
.type
= bfd_link_hash_new
;
1499 h
->root
.u
.undef
.abfd
= NULL
;
1502 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1504 /* If the new symbol is hidden or internal, completely undo
1505 any dynamic link state. */
1506 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1507 h
->forced_local
= 0;
1513 /* FIXME: Should we check type and size for protected symbol? */
1519 /* If a new weak symbol definition comes from a regular file and the
1520 old symbol comes from a dynamic library, we treat the new one as
1521 strong. Similarly, an old weak symbol definition from a regular
1522 file is treated as strong when the new symbol comes from a dynamic
1523 library. Further, an old weak symbol from a dynamic library is
1524 treated as strong if the new symbol is from a dynamic library.
1525 This reflects the way glibc's ld.so works.
1527 Also allow a weak symbol to override a linker script symbol
1528 defined by an early pass over the script. This is done so the
1529 linker knows the symbol is defined in an object file, for the
1530 DEFINED script function.
1532 Do this before setting *type_change_ok or *size_change_ok so that
1533 we warn properly when dynamic library symbols are overridden. */
1535 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1537 if (olddef
&& newdyn
)
1540 /* Allow changes between different types of function symbol. */
1541 if (newfunc
&& oldfunc
)
1542 *type_change_ok
= true;
1544 /* It's OK to change the type if either the existing symbol or the
1545 new symbol is weak. A type change is also OK if the old symbol
1546 is undefined and the new symbol is defined. */
1551 && h
->root
.type
== bfd_link_hash_undefined
))
1552 *type_change_ok
= true;
1554 /* It's OK to change the size if either the existing symbol or the
1555 new symbol is weak, or if the old symbol is undefined. */
1558 || h
->root
.type
== bfd_link_hash_undefined
)
1559 *size_change_ok
= true;
1561 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1562 symbol, respectively, appears to be a common symbol in a dynamic
1563 object. If a symbol appears in an uninitialized section, and is
1564 not weak, and is not a function, then it may be a common symbol
1565 which was resolved when the dynamic object was created. We want
1566 to treat such symbols specially, because they raise special
1567 considerations when setting the symbol size: if the symbol
1568 appears as a common symbol in a regular object, and the size in
1569 the regular object is larger, we must make sure that we use the
1570 larger size. This problematic case can always be avoided in C,
1571 but it must be handled correctly when using Fortran shared
1574 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1575 likewise for OLDDYNCOMMON and OLDDEF.
1577 Note that this test is just a heuristic, and that it is quite
1578 possible to have an uninitialized symbol in a shared object which
1579 is really a definition, rather than a common symbol. This could
1580 lead to some minor confusion when the symbol really is a common
1581 symbol in some regular object. However, I think it will be
1587 && (sec
->flags
& SEC_ALLOC
) != 0
1588 && (sec
->flags
& SEC_LOAD
) == 0
1591 newdyncommon
= true;
1593 newdyncommon
= false;
1597 && h
->root
.type
== bfd_link_hash_defined
1599 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1600 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1603 olddyncommon
= true;
1605 olddyncommon
= false;
1607 /* We now know everything about the old and new symbols. We ask the
1608 backend to check if we can merge them. */
1609 if (bed
->merge_symbol
!= NULL
)
1611 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1616 /* There are multiple definitions of a normal symbol. Skip the
1617 default symbol as well as definition from an IR object. */
1618 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1619 && !default_sym
&& h
->def_regular
1621 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1622 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1624 /* Handle a multiple definition. */
1625 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1626 abfd
, sec
, *pvalue
);
1631 /* If both the old and the new symbols look like common symbols in a
1632 dynamic object, set the size of the symbol to the larger of the
1637 && sym
->st_size
!= h
->size
)
1639 /* Since we think we have two common symbols, issue a multiple
1640 common warning if desired. Note that we only warn if the
1641 size is different. If the size is the same, we simply let
1642 the old symbol override the new one as normally happens with
1643 symbols defined in dynamic objects. */
1645 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1646 bfd_link_hash_common
, sym
->st_size
);
1647 if (sym
->st_size
> h
->size
)
1648 h
->size
= sym
->st_size
;
1650 *size_change_ok
= true;
1653 /* If we are looking at a dynamic object, and we have found a
1654 definition, we need to see if the symbol was already defined by
1655 some other object. If so, we want to use the existing
1656 definition, and we do not want to report a multiple symbol
1657 definition error; we do this by clobbering *PSEC to be
1658 bfd_und_section_ptr.
1660 We treat a common symbol as a definition if the symbol in the
1661 shared library is a function, since common symbols always
1662 represent variables; this can cause confusion in principle, but
1663 any such confusion would seem to indicate an erroneous program or
1664 shared library. We also permit a common symbol in a regular
1665 object to override a weak symbol in a shared object. */
1670 || (h
->root
.type
== bfd_link_hash_common
1671 && (newweak
|| newfunc
))))
1675 newdyncommon
= false;
1677 *psec
= sec
= bfd_und_section_ptr
;
1678 *size_change_ok
= true;
1680 /* If we get here when the old symbol is a common symbol, then
1681 we are explicitly letting it override a weak symbol or
1682 function in a dynamic object, and we don't want to warn about
1683 a type change. If the old symbol is a defined symbol, a type
1684 change warning may still be appropriate. */
1686 if (h
->root
.type
== bfd_link_hash_common
)
1687 *type_change_ok
= true;
1690 /* Handle the special case of an old common symbol merging with a
1691 new symbol which looks like a common symbol in a shared object.
1692 We change *PSEC and *PVALUE to make the new symbol look like a
1693 common symbol, and let _bfd_generic_link_add_one_symbol do the
1697 && h
->root
.type
== bfd_link_hash_common
)
1701 newdyncommon
= false;
1702 *pvalue
= sym
->st_size
;
1703 *psec
= sec
= bed
->common_section (oldsec
);
1704 *size_change_ok
= true;
1707 /* Skip weak definitions of symbols that are already defined. */
1708 if (newdef
&& olddef
&& newweak
)
1710 /* Don't skip new non-IR weak syms. */
1711 if (!(oldbfd
!= NULL
1712 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1713 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1719 /* Merge st_other. If the symbol already has a dynamic index,
1720 but visibility says it should not be visible, turn it into a
1722 elf_merge_st_other (abfd
, h
, sym
->st_other
, sec
, newdef
, newdyn
);
1723 if (h
->dynindx
!= -1)
1724 switch (ELF_ST_VISIBILITY (h
->other
))
1728 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1733 /* If the old symbol is from a dynamic object, and the new symbol is
1734 a definition which is not from a dynamic object, then the new
1735 symbol overrides the old symbol. Symbols from regular files
1736 always take precedence over symbols from dynamic objects, even if
1737 they are defined after the dynamic object in the link.
1739 As above, we again permit a common symbol in a regular object to
1740 override a definition in a shared object if the shared object
1741 symbol is a function or is weak. */
1746 || (bfd_is_com_section (sec
)
1747 && (oldweak
|| oldfunc
)))
1752 /* Change the hash table entry to undefined, and let
1753 _bfd_generic_link_add_one_symbol do the right thing with the
1756 h
->root
.type
= bfd_link_hash_undefined
;
1757 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1758 *size_change_ok
= true;
1761 olddyncommon
= false;
1763 /* We again permit a type change when a common symbol may be
1764 overriding a function. */
1766 if (bfd_is_com_section (sec
))
1770 /* If a common symbol overrides a function, make sure
1771 that it isn't defined dynamically nor has type
1774 h
->type
= STT_NOTYPE
;
1776 *type_change_ok
= true;
1779 if (hi
->root
.type
== bfd_link_hash_indirect
)
1782 /* This union may have been set to be non-NULL when this symbol
1783 was seen in a dynamic object. We must force the union to be
1784 NULL, so that it is correct for a regular symbol. */
1785 h
->verinfo
.vertree
= NULL
;
1788 /* Handle the special case of a new common symbol merging with an
1789 old symbol that looks like it might be a common symbol defined in
1790 a shared object. Note that we have already handled the case in
1791 which a new common symbol should simply override the definition
1792 in the shared library. */
1795 && bfd_is_com_section (sec
)
1798 /* It would be best if we could set the hash table entry to a
1799 common symbol, but we don't know what to use for the section
1800 or the alignment. */
1801 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1802 bfd_link_hash_common
, sym
->st_size
);
1804 /* If the presumed common symbol in the dynamic object is
1805 larger, pretend that the new symbol has its size. */
1807 if (h
->size
> *pvalue
)
1810 /* We need to remember the alignment required by the symbol
1811 in the dynamic object. */
1812 BFD_ASSERT (pold_alignment
);
1813 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1816 olddyncommon
= false;
1818 h
->root
.type
= bfd_link_hash_undefined
;
1819 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1821 *size_change_ok
= true;
1822 *type_change_ok
= true;
1824 if (hi
->root
.type
== bfd_link_hash_indirect
)
1827 h
->verinfo
.vertree
= NULL
;
1832 /* Handle the case where we had a versioned symbol in a dynamic
1833 library and now find a definition in a normal object. In this
1834 case, we make the versioned symbol point to the normal one. */
1835 flip
->root
.type
= h
->root
.type
;
1836 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1837 h
->root
.type
= bfd_link_hash_indirect
;
1838 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1839 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1843 flip
->ref_dynamic
= 1;
1850 /* This function is called to create an indirect symbol from the
1851 default for the symbol with the default version if needed. The
1852 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1853 set DYNSYM if the new indirect symbol is dynamic. */
1856 _bfd_elf_add_default_symbol (bfd
*abfd
,
1857 struct bfd_link_info
*info
,
1858 struct elf_link_hash_entry
*h
,
1860 Elf_Internal_Sym
*sym
,
1866 bool type_change_ok
;
1867 bool size_change_ok
;
1870 struct elf_link_hash_entry
*hi
;
1871 struct bfd_link_hash_entry
*bh
;
1872 const struct elf_backend_data
*bed
;
1877 size_t len
, shortlen
;
1881 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1884 /* If this symbol has a version, and it is the default version, we
1885 create an indirect symbol from the default name to the fully
1886 decorated name. This will cause external references which do not
1887 specify a version to be bound to this version of the symbol. */
1888 p
= strchr (name
, ELF_VER_CHR
);
1889 if (h
->versioned
== unknown
)
1893 h
->versioned
= unversioned
;
1898 if (p
[1] != ELF_VER_CHR
)
1900 h
->versioned
= versioned_hidden
;
1904 h
->versioned
= versioned
;
1909 /* PR ld/19073: We may see an unversioned definition after the
1915 bed
= get_elf_backend_data (abfd
);
1916 collect
= bed
->collect
;
1917 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1919 shortlen
= p
- name
;
1920 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1921 if (shortname
== NULL
)
1923 memcpy (shortname
, name
, shortlen
);
1924 shortname
[shortlen
] = '\0';
1926 /* We are going to create a new symbol. Merge it with any existing
1927 symbol with this name. For the purposes of the merge, act as
1928 though we were defining the symbol we just defined, although we
1929 actually going to define an indirect symbol. */
1930 type_change_ok
= false;
1931 size_change_ok
= false;
1934 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1935 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1936 &type_change_ok
, &size_change_ok
, &matched
))
1942 if (hi
->def_regular
|| ELF_COMMON_DEF_P (hi
))
1944 /* If the undecorated symbol will have a version added by a
1945 script different to H, then don't indirect to/from the
1946 undecorated symbol. This isn't ideal because we may not yet
1947 have seen symbol versions, if given by a script on the
1948 command line rather than via --version-script. */
1949 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1954 = bfd_find_version_for_sym (info
->version_info
,
1955 hi
->root
.root
.string
, &hide
);
1956 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1958 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
1962 if (hi
->verinfo
.vertree
!= NULL
1963 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1969 /* Add the default symbol if not performing a relocatable link. */
1970 if (! bfd_link_relocatable (info
))
1973 if (bh
->type
== bfd_link_hash_defined
1974 && bh
->u
.def
.section
->owner
!= NULL
1975 && (bh
->u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
1977 /* Mark the previous definition from IR object as
1978 undefined so that the generic linker will override
1980 bh
->type
= bfd_link_hash_undefined
;
1981 bh
->u
.undef
.abfd
= bh
->u
.def
.section
->owner
;
1983 if (! (_bfd_generic_link_add_one_symbol
1984 (info
, abfd
, shortname
, BSF_INDIRECT
,
1985 bfd_ind_section_ptr
,
1986 0, name
, false, collect
, &bh
)))
1988 hi
= (struct elf_link_hash_entry
*) bh
;
1993 /* In this case the symbol named SHORTNAME is overriding the
1994 indirect symbol we want to add. We were planning on making
1995 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1996 is the name without a version. NAME is the fully versioned
1997 name, and it is the default version.
1999 Overriding means that we already saw a definition for the
2000 symbol SHORTNAME in a regular object, and it is overriding
2001 the symbol defined in the dynamic object.
2003 When this happens, we actually want to change NAME, the
2004 symbol we just added, to refer to SHORTNAME. This will cause
2005 references to NAME in the shared object to become references
2006 to SHORTNAME in the regular object. This is what we expect
2007 when we override a function in a shared object: that the
2008 references in the shared object will be mapped to the
2009 definition in the regular object. */
2011 while (hi
->root
.type
== bfd_link_hash_indirect
2012 || hi
->root
.type
== bfd_link_hash_warning
)
2013 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2015 h
->root
.type
= bfd_link_hash_indirect
;
2016 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
2020 hi
->ref_dynamic
= 1;
2024 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
2029 /* Now set HI to H, so that the following code will set the
2030 other fields correctly. */
2034 /* Check if HI is a warning symbol. */
2035 if (hi
->root
.type
== bfd_link_hash_warning
)
2036 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2038 /* If there is a duplicate definition somewhere, then HI may not
2039 point to an indirect symbol. We will have reported an error to
2040 the user in that case. */
2042 if (hi
->root
.type
== bfd_link_hash_indirect
)
2044 struct elf_link_hash_entry
*ht
;
2046 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2047 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2049 /* If we first saw a reference to SHORTNAME with non-default
2050 visibility, merge that visibility to the @@VER symbol. */
2051 elf_merge_st_other (abfd
, ht
, hi
->other
, sec
, true, dynamic
);
2053 /* A reference to the SHORTNAME symbol from a dynamic library
2054 will be satisfied by the versioned symbol at runtime. In
2055 effect, we have a reference to the versioned symbol. */
2056 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2057 hi
->dynamic_def
|= ht
->dynamic_def
;
2059 /* See if the new flags lead us to realize that the symbol must
2065 if (! bfd_link_executable (info
)
2072 if (hi
->ref_regular
)
2078 /* We also need to define an indirection from the nondefault version
2082 len
= strlen (name
);
2083 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2084 if (shortname
== NULL
)
2086 memcpy (shortname
, name
, shortlen
);
2087 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2089 /* Once again, merge with any existing symbol. */
2090 type_change_ok
= false;
2091 size_change_ok
= false;
2093 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2094 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2095 &type_change_ok
, &size_change_ok
, &matched
))
2101 && h
->root
.type
== bfd_link_hash_defweak
2102 && hi
->root
.type
== bfd_link_hash_defined
)
2104 /* We are handling a weak sym@@ver and attempting to define
2105 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2106 new weak sym@ver because there is already a strong sym@ver.
2107 However, sym@ver and sym@@ver are really the same symbol.
2108 The existing strong sym@ver ought to override sym@@ver. */
2109 h
->root
.type
= bfd_link_hash_defined
;
2110 h
->root
.u
.def
.section
= hi
->root
.u
.def
.section
;
2111 h
->root
.u
.def
.value
= hi
->root
.u
.def
.value
;
2112 hi
->root
.type
= bfd_link_hash_indirect
;
2113 hi
->root
.u
.i
.link
= &h
->root
;
2120 /* Here SHORTNAME is a versioned name, so we don't expect to see
2121 the type of override we do in the case above unless it is
2122 overridden by a versioned definition. */
2123 if (hi
->root
.type
!= bfd_link_hash_defined
2124 && hi
->root
.type
!= bfd_link_hash_defweak
)
2126 /* xgettext:c-format */
2127 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2134 if (! (_bfd_generic_link_add_one_symbol
2135 (info
, abfd
, shortname
, BSF_INDIRECT
,
2136 bfd_ind_section_ptr
, 0, name
, false, collect
, &bh
)))
2138 hi
= (struct elf_link_hash_entry
*) bh
;
2141 /* If there is a duplicate definition somewhere, then HI may not
2142 point to an indirect symbol. We will have reported an error
2143 to the user in that case. */
2144 if (hi
->root
.type
== bfd_link_hash_indirect
)
2146 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2147 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2148 hi
->dynamic_def
|= h
->dynamic_def
;
2150 /* If we first saw a reference to @VER symbol with
2151 non-default visibility, merge that visibility to the
2153 elf_merge_st_other (abfd
, h
, hi
->other
, sec
, true, dynamic
);
2155 /* See if the new flags lead us to realize that the symbol
2161 if (! bfd_link_executable (info
)
2167 if (hi
->ref_regular
)
2176 /* This routine is used to export all defined symbols into the dynamic
2177 symbol table. It is called via elf_link_hash_traverse. */
2180 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2182 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2184 /* Ignore indirect symbols. These are added by the versioning code. */
2185 if (h
->root
.type
== bfd_link_hash_indirect
)
2188 /* Ignore this if we won't export it. */
2189 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2192 if (h
->dynindx
== -1
2193 && (h
->def_regular
|| h
->ref_regular
)
2194 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2195 h
->root
.root
.string
))
2197 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2207 /* Return the glibc version reference if VERSION_DEP is added to the
2208 list of glibc version dependencies successfully. VERSION_DEP will
2209 be put into the .gnu.version_r section. */
2211 static Elf_Internal_Verneed
*
2212 elf_link_add_glibc_verneed (struct elf_find_verdep_info
*rinfo
,
2213 Elf_Internal_Verneed
*glibc_verref
,
2214 const char *version_dep
)
2216 Elf_Internal_Verneed
*t
;
2217 Elf_Internal_Vernaux
*a
;
2220 if (glibc_verref
!= NULL
)
2224 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2226 /* Return if VERSION_DEP dependency has been added. */
2227 if (a
->vna_nodename
== version_dep
2228 || strcmp (a
->vna_nodename
, version_dep
) == 0)
2236 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2240 const char *soname
= bfd_elf_get_dt_soname (t
->vn_bfd
);
2241 if (soname
!= NULL
&& startswith (soname
, "libc.so."))
2245 /* Skip the shared library if it isn't libc.so. */
2250 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2252 /* Return if VERSION_DEP dependency has been added. */
2253 if (a
->vna_nodename
== version_dep
2254 || strcmp (a
->vna_nodename
, version_dep
) == 0)
2257 /* Check if libc.so provides GLIBC_2.XX version. */
2258 if (!is_glibc
&& startswith (a
->vna_nodename
, "GLIBC_2."))
2262 /* Skip if it isn't linked against glibc. */
2268 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2271 rinfo
->failed
= true;
2275 a
->vna_nodename
= version_dep
;
2277 a
->vna_nextptr
= t
->vn_auxptr
;
2278 a
->vna_other
= rinfo
->vers
+ 1;
2286 /* Add VERSION_DEP to the list of version dependencies when linked
2290 _bfd_elf_link_add_glibc_version_dependency
2291 (struct elf_find_verdep_info
*rinfo
,
2292 const char *version_dep
[])
2294 Elf_Internal_Verneed
*t
= NULL
;
2298 t
= elf_link_add_glibc_verneed (rinfo
, t
, *version_dep
);
2299 /* Return if there is no glibc version reference. */
2304 while (*version_dep
!= NULL
);
2307 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2308 linked against glibc. */
2311 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info
*rinfo
)
2313 if (rinfo
->info
->enable_dt_relr
)
2315 const char *version
[] =
2317 "GLIBC_ABI_DT_RELR",
2320 _bfd_elf_link_add_glibc_version_dependency (rinfo
, version
);
2324 /* Look through the symbols which are defined in other shared
2325 libraries and referenced here. Update the list of version
2326 dependencies. This will be put into the .gnu.version_r section.
2327 This function is called via elf_link_hash_traverse. */
2330 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2333 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2334 Elf_Internal_Verneed
*t
;
2335 Elf_Internal_Vernaux
*a
;
2338 /* We only care about symbols defined in shared objects with version
2343 || h
->verinfo
.verdef
== NULL
2344 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2345 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2348 /* See if we already know about this version. */
2349 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2353 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2356 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2357 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2363 /* This is a new version. Add it to tree we are building. */
2368 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2371 rinfo
->failed
= true;
2375 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2376 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2377 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2381 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2384 rinfo
->failed
= true;
2388 /* Note that we are copying a string pointer here, and testing it
2389 above. If bfd_elf_string_from_elf_section is ever changed to
2390 discard the string data when low in memory, this will have to be
2392 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2394 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2395 a
->vna_nextptr
= t
->vn_auxptr
;
2397 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2400 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2407 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2408 hidden. Set *T_P to NULL if there is no match. */
2411 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2412 struct elf_link_hash_entry
*h
,
2413 const char *version_p
,
2414 struct bfd_elf_version_tree
**t_p
,
2417 struct bfd_elf_version_tree
*t
;
2419 /* Look for the version. If we find it, it is no longer weak. */
2420 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2422 if (strcmp (t
->name
, version_p
) == 0)
2426 struct bfd_elf_version_expr
*d
;
2428 len
= version_p
- h
->root
.root
.string
;
2429 alc
= (char *) bfd_malloc (len
);
2432 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2433 alc
[len
- 1] = '\0';
2434 if (alc
[len
- 2] == ELF_VER_CHR
)
2435 alc
[len
- 2] = '\0';
2437 h
->verinfo
.vertree
= t
;
2441 if (t
->globals
.list
!= NULL
)
2442 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2444 /* See if there is anything to force this symbol to
2446 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2448 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2451 && ! info
->export_dynamic
)
2465 /* Return TRUE if the symbol H is hidden by version script. */
2468 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2469 struct elf_link_hash_entry
*h
)
2473 const struct elf_backend_data
*bed
2474 = get_elf_backend_data (info
->output_bfd
);
2476 /* Version script only hides symbols defined in regular objects. */
2477 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2480 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2481 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2483 struct bfd_elf_version_tree
*t
;
2486 if (*p
== ELF_VER_CHR
)
2490 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2494 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2499 /* If we don't have a version for this symbol, see if we can find
2501 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2504 = bfd_find_version_for_sym (info
->version_info
,
2505 h
->root
.root
.string
, &hide
);
2506 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2508 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2516 /* Figure out appropriate versions for all the symbols. We may not
2517 have the version number script until we have read all of the input
2518 files, so until that point we don't know which symbols should be
2519 local. This function is called via elf_link_hash_traverse. */
2522 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2524 struct elf_info_failed
*sinfo
;
2525 struct bfd_link_info
*info
;
2526 const struct elf_backend_data
*bed
;
2527 struct elf_info_failed eif
;
2531 sinfo
= (struct elf_info_failed
*) data
;
2534 /* Fix the symbol flags. */
2537 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2540 sinfo
->failed
= true;
2544 bed
= get_elf_backend_data (info
->output_bfd
);
2546 /* We only need version numbers for symbols defined in regular
2548 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2550 /* Hide symbols defined in discarded input sections. */
2551 if ((h
->root
.type
== bfd_link_hash_defined
2552 || h
->root
.type
== bfd_link_hash_defweak
)
2553 && discarded_section (h
->root
.u
.def
.section
))
2554 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2559 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2560 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2562 struct bfd_elf_version_tree
*t
;
2565 if (*p
== ELF_VER_CHR
)
2568 /* If there is no version string, we can just return out. */
2572 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2574 sinfo
->failed
= true;
2579 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2581 /* If we are building an application, we need to create a
2582 version node for this version. */
2583 if (t
== NULL
&& bfd_link_executable (info
))
2585 struct bfd_elf_version_tree
**pp
;
2588 /* If we aren't going to export this symbol, we don't need
2589 to worry about it. */
2590 if (h
->dynindx
== -1)
2593 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2597 sinfo
->failed
= true;
2602 t
->name_indx
= (unsigned int) -1;
2606 /* Don't count anonymous version tag. */
2607 if (sinfo
->info
->version_info
!= NULL
2608 && sinfo
->info
->version_info
->vernum
== 0)
2610 for (pp
= &sinfo
->info
->version_info
;
2614 t
->vernum
= version_index
;
2618 h
->verinfo
.vertree
= t
;
2622 /* We could not find the version for a symbol when
2623 generating a shared archive. Return an error. */
2625 /* xgettext:c-format */
2626 (_("%pB: version node not found for symbol %s"),
2627 info
->output_bfd
, h
->root
.root
.string
);
2628 bfd_set_error (bfd_error_bad_value
);
2629 sinfo
->failed
= true;
2634 /* If we don't have a version for this symbol, see if we can find
2637 && h
->verinfo
.vertree
== NULL
2638 && sinfo
->info
->version_info
!= NULL
)
2641 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2642 h
->root
.root
.string
, &hide
);
2643 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2644 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2650 /* Read and swap the relocs from the section indicated by SHDR. This
2651 may be either a REL or a RELA section. The relocations are
2652 translated into RELA relocations and stored in INTERNAL_RELOCS,
2653 which should have already been allocated to contain enough space.
2654 The EXTERNAL_RELOCS are a buffer where the external form of the
2655 relocations should be stored.
2657 Returns FALSE if something goes wrong. */
2660 elf_link_read_relocs_from_section (bfd
*abfd
,
2662 Elf_Internal_Shdr
*shdr
,
2663 void *external_relocs
,
2664 Elf_Internal_Rela
*internal_relocs
)
2666 const struct elf_backend_data
*bed
;
2667 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2668 const bfd_byte
*erela
;
2669 const bfd_byte
*erelaend
;
2670 Elf_Internal_Rela
*irela
;
2671 Elf_Internal_Shdr
*symtab_hdr
;
2674 /* Position ourselves at the start of the section. */
2675 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2678 /* Read the relocations. */
2679 if (bfd_read (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2682 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2683 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2685 bed
= get_elf_backend_data (abfd
);
2687 /* Convert the external relocations to the internal format. */
2688 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2689 swap_in
= bed
->s
->swap_reloc_in
;
2690 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2691 swap_in
= bed
->s
->swap_reloca_in
;
2694 bfd_set_error (bfd_error_wrong_format
);
2698 erela
= (const bfd_byte
*) external_relocs
;
2699 /* Setting erelaend like this and comparing with <= handles case of
2700 a fuzzed object with sh_size not a multiple of sh_entsize. */
2701 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2702 irela
= internal_relocs
;
2703 while (erela
<= erelaend
)
2707 (*swap_in
) (abfd
, erela
, irela
);
2708 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2709 if (bed
->s
->arch_size
== 64)
2713 if ((size_t) r_symndx
>= nsyms
)
2716 /* xgettext:c-format */
2717 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2718 " for offset %#" PRIx64
" in section `%pA'"),
2719 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2720 (uint64_t) irela
->r_offset
, sec
);
2721 bfd_set_error (bfd_error_bad_value
);
2725 else if (r_symndx
!= STN_UNDEF
)
2728 /* xgettext:c-format */
2729 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2730 " for offset %#" PRIx64
" in section `%pA'"
2731 " when the object file has no symbol table"),
2732 abfd
, (uint64_t) r_symndx
,
2733 (uint64_t) irela
->r_offset
, sec
);
2734 bfd_set_error (bfd_error_bad_value
);
2737 irela
+= bed
->s
->int_rels_per_ext_rel
;
2738 erela
+= shdr
->sh_entsize
;
2744 /* Read and swap the relocs for a section O. They may have been
2745 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2746 not NULL, they are used as buffers to read into. They are known to
2747 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2748 the return value is allocated using either malloc or bfd_alloc,
2749 according to the KEEP_MEMORY argument. If O has two relocation
2750 sections (both REL and RELA relocations), then the REL_HDR
2751 relocations will appear first in INTERNAL_RELOCS, followed by the
2752 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2753 update cache_size. */
2756 _bfd_elf_link_info_read_relocs (bfd
*abfd
,
2757 struct bfd_link_info
*info
,
2759 void *external_relocs
,
2760 Elf_Internal_Rela
*internal_relocs
,
2763 void *alloc1
= NULL
;
2764 Elf_Internal_Rela
*alloc2
= NULL
;
2765 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2766 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2767 Elf_Internal_Rela
*internal_rela_relocs
;
2769 if (esdo
->relocs
!= NULL
)
2770 return esdo
->relocs
;
2772 if (o
->reloc_count
== 0)
2775 if (internal_relocs
== NULL
)
2779 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2782 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2784 info
->cache_size
+= size
;
2787 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2788 if (internal_relocs
== NULL
)
2792 if (external_relocs
== NULL
)
2794 bfd_size_type size
= 0;
2797 size
+= esdo
->rel
.hdr
->sh_size
;
2799 size
+= esdo
->rela
.hdr
->sh_size
;
2801 alloc1
= bfd_malloc (size
);
2804 external_relocs
= alloc1
;
2807 internal_rela_relocs
= internal_relocs
;
2810 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2814 external_relocs
= (((bfd_byte
*) external_relocs
)
2815 + esdo
->rel
.hdr
->sh_size
);
2816 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2817 * bed
->s
->int_rels_per_ext_rel
);
2821 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2823 internal_rela_relocs
)))
2826 /* Cache the results for next time, if we can. */
2828 esdo
->relocs
= internal_relocs
;
2832 /* Don't free alloc2, since if it was allocated we are passing it
2833 back (under the name of internal_relocs). */
2835 return internal_relocs
;
2842 bfd_release (abfd
, alloc2
);
2849 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2850 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2851 struct bfd_link_info. */
2854 _bfd_elf_link_read_relocs (bfd
*abfd
,
2856 void *external_relocs
,
2857 Elf_Internal_Rela
*internal_relocs
,
2860 return _bfd_elf_link_info_read_relocs (abfd
, NULL
, o
, external_relocs
,
2861 internal_relocs
, keep_memory
);
2865 /* Compute the size of, and allocate space for, REL_HDR which is the
2866 section header for a section containing relocations for O. */
2869 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2870 struct bfd_elf_section_reloc_data
*reldata
)
2872 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2874 /* That allows us to calculate the size of the section. */
2875 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2877 /* The contents field must last into write_object_contents, so we
2878 allocate it with bfd_alloc rather than malloc. Also since we
2879 cannot be sure that the contents will actually be filled in,
2880 we zero the allocated space. */
2881 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2882 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2885 if (reldata
->hashes
== NULL
&& reldata
->count
)
2887 struct elf_link_hash_entry
**p
;
2889 p
= ((struct elf_link_hash_entry
**)
2890 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2894 reldata
->hashes
= p
;
2900 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2901 originated from the section given by INPUT_REL_HDR) to the
2905 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2906 asection
*input_section
,
2907 Elf_Internal_Shdr
*input_rel_hdr
,
2908 Elf_Internal_Rela
*internal_relocs
,
2909 struct elf_link_hash_entry
**rel_hash
2912 Elf_Internal_Rela
*irela
;
2913 Elf_Internal_Rela
*irelaend
;
2915 struct bfd_elf_section_reloc_data
*output_reldata
;
2916 asection
*output_section
;
2917 const struct elf_backend_data
*bed
;
2918 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2919 struct bfd_elf_section_data
*esdo
;
2921 output_section
= input_section
->output_section
;
2923 bed
= get_elf_backend_data (output_bfd
);
2924 esdo
= elf_section_data (output_section
);
2925 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2927 output_reldata
= &esdo
->rel
;
2928 swap_out
= bed
->s
->swap_reloc_out
;
2930 else if (esdo
->rela
.hdr
2931 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2933 output_reldata
= &esdo
->rela
;
2934 swap_out
= bed
->s
->swap_reloca_out
;
2939 /* xgettext:c-format */
2940 (_("%pB: relocation size mismatch in %pB section %pA"),
2941 output_bfd
, input_section
->owner
, input_section
);
2942 bfd_set_error (bfd_error_wrong_format
);
2946 erel
= output_reldata
->hdr
->contents
;
2947 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2948 irela
= internal_relocs
;
2949 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2950 * bed
->s
->int_rels_per_ext_rel
);
2951 while (irela
< irelaend
)
2953 (*swap_out
) (output_bfd
, irela
, erel
);
2954 irela
+= bed
->s
->int_rels_per_ext_rel
;
2955 erel
+= input_rel_hdr
->sh_entsize
;
2958 /* Bump the counter, so that we know where to add the next set of
2960 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2965 /* Make weak undefined symbols in PIE dynamic. */
2968 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2969 struct elf_link_hash_entry
*h
)
2971 if (bfd_link_pie (info
)
2973 && h
->root
.type
== bfd_link_hash_undefweak
)
2974 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2979 /* Fix up the flags for a symbol. This handles various cases which
2980 can only be fixed after all the input files are seen. This is
2981 currently called by both adjust_dynamic_symbol and
2982 assign_sym_version, which is unnecessary but perhaps more robust in
2983 the face of future changes. */
2986 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2987 struct elf_info_failed
*eif
)
2989 const struct elf_backend_data
*bed
;
2991 /* If this symbol was mentioned in a non-ELF file, try to set
2992 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2993 permit a non-ELF file to correctly refer to a symbol defined in
2994 an ELF dynamic object. */
2997 while (h
->root
.type
== bfd_link_hash_indirect
)
2998 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3000 if (h
->root
.type
!= bfd_link_hash_defined
3001 && h
->root
.type
!= bfd_link_hash_defweak
)
3004 h
->ref_regular_nonweak
= 1;
3008 if (h
->root
.u
.def
.section
->owner
!= NULL
3009 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3010 == bfd_target_elf_flavour
))
3013 h
->ref_regular_nonweak
= 1;
3019 if (h
->dynindx
== -1
3023 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3032 /* Unfortunately, NON_ELF is only correct if the symbol
3033 was first seen in a non-ELF file. Fortunately, if the symbol
3034 was first seen in an ELF file, we're probably OK unless the
3035 symbol was defined in a non-ELF file. Catch that case here.
3036 FIXME: We're still in trouble if the symbol was first seen in
3037 a dynamic object, and then later in a non-ELF regular object. */
3038 if ((h
->root
.type
== bfd_link_hash_defined
3039 || h
->root
.type
== bfd_link_hash_defweak
)
3041 && (h
->root
.u
.def
.section
->owner
!= NULL
3042 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3043 != bfd_target_elf_flavour
)
3044 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3045 && !h
->def_dynamic
)))
3049 /* Backend specific symbol fixup. */
3050 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3051 if (bed
->elf_backend_fixup_symbol
3052 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
3055 /* If this is a final link, and the symbol was defined as a common
3056 symbol in a regular object file, and there was no definition in
3057 any dynamic object, then the linker will have allocated space for
3058 the symbol in a common section but the DEF_REGULAR
3059 flag will not have been set. */
3060 if (h
->root
.type
== bfd_link_hash_defined
3064 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
3067 /* Symbols defined in discarded sections shouldn't be dynamic. */
3068 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
3069 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3071 /* If a weak undefined symbol has non-default visibility, we also
3072 hide it from the dynamic linker. */
3073 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
3074 && h
->root
.type
== bfd_link_hash_undefweak
)
3075 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3077 /* A hidden versioned symbol in executable should be forced local if
3078 it is is locally defined, not referenced by shared library and not
3080 else if (bfd_link_executable (eif
->info
)
3081 && h
->versioned
== versioned_hidden
3082 && !eif
->info
->export_dynamic
3086 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3088 /* If -Bsymbolic was used (which means to bind references to global
3089 symbols to the definition within the shared object), and this
3090 symbol was defined in a regular object, then it actually doesn't
3091 need a PLT entry. Likewise, if the symbol has non-default
3092 visibility. If the symbol has hidden or internal visibility, we
3093 will force it local. */
3094 else if (h
->needs_plt
3095 && bfd_link_pic (eif
->info
)
3096 && is_elf_hash_table (eif
->info
->hash
)
3097 && (SYMBOLIC_BIND (eif
->info
, h
)
3098 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
3103 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3104 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
3105 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3108 /* If this is a weak defined symbol in a dynamic object, and we know
3109 the real definition in the dynamic object, copy interesting flags
3110 over to the real definition. */
3111 if (h
->is_weakalias
)
3113 struct elf_link_hash_entry
*def
= weakdef (h
);
3115 /* If the real definition is defined by a regular object file,
3116 don't do anything special. See the longer description in
3117 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3118 bfd_link_hash_defined as it was when put on the alias list
3119 then it must have originally been a versioned symbol (for
3120 which a non-versioned indirect symbol is created) and later
3121 a definition for the non-versioned symbol is found. In that
3122 case the indirection is flipped with the versioned symbol
3123 becoming an indirect pointing at the non-versioned symbol.
3124 Thus, not an alias any more. */
3125 if (def
->def_regular
3126 || def
->root
.type
!= bfd_link_hash_defined
)
3129 while ((h
= h
->u
.alias
) != def
)
3130 h
->is_weakalias
= 0;
3134 while (h
->root
.type
== bfd_link_hash_indirect
)
3135 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3136 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3137 || h
->root
.type
== bfd_link_hash_defweak
);
3138 BFD_ASSERT (def
->def_dynamic
);
3139 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
3146 /* Make the backend pick a good value for a dynamic symbol. This is
3147 called via elf_link_hash_traverse, and also calls itself
3151 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
3153 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3154 struct elf_link_hash_table
*htab
;
3155 const struct elf_backend_data
*bed
;
3157 if (! is_elf_hash_table (eif
->info
->hash
))
3160 /* Ignore indirect symbols. These are added by the versioning code. */
3161 if (h
->root
.type
== bfd_link_hash_indirect
)
3164 /* Fix the symbol flags. */
3165 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
3168 htab
= elf_hash_table (eif
->info
);
3169 bed
= get_elf_backend_data (htab
->dynobj
);
3171 if (h
->root
.type
== bfd_link_hash_undefweak
)
3173 if (eif
->info
->dynamic_undefined_weak
== 0)
3174 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3175 else if (eif
->info
->dynamic_undefined_weak
> 0
3177 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3178 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
3179 h
->root
.root
.string
))
3181 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3189 /* If this symbol does not require a PLT entry, and it is not
3190 defined by a dynamic object, or is not referenced by a regular
3191 object, ignore it. We do have to handle a weak defined symbol,
3192 even if no regular object refers to it, if we decided to add it
3193 to the dynamic symbol table. FIXME: Do we normally need to worry
3194 about symbols which are defined by one dynamic object and
3195 referenced by another one? */
3197 && h
->type
!= STT_GNU_IFUNC
3201 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3203 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3207 /* If we've already adjusted this symbol, don't do it again. This
3208 can happen via a recursive call. */
3209 if (h
->dynamic_adjusted
)
3212 /* Don't look at this symbol again. Note that we must set this
3213 after checking the above conditions, because we may look at a
3214 symbol once, decide not to do anything, and then get called
3215 recursively later after REF_REGULAR is set below. */
3216 h
->dynamic_adjusted
= 1;
3218 /* If this is a weak definition, and we know a real definition, and
3219 the real symbol is not itself defined by a regular object file,
3220 then get a good value for the real definition. We handle the
3221 real symbol first, for the convenience of the backend routine.
3223 Note that there is a confusing case here. If the real definition
3224 is defined by a regular object file, we don't get the real symbol
3225 from the dynamic object, but we do get the weak symbol. If the
3226 processor backend uses a COPY reloc, then if some routine in the
3227 dynamic object changes the real symbol, we will not see that
3228 change in the corresponding weak symbol. This is the way other
3229 ELF linkers work as well, and seems to be a result of the shared
3232 I will clarify this issue. Most SVR4 shared libraries define the
3233 variable _timezone and define timezone as a weak synonym. The
3234 tzset call changes _timezone. If you write
3235 extern int timezone;
3237 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3238 you might expect that, since timezone is a synonym for _timezone,
3239 the same number will print both times. However, if the processor
3240 backend uses a COPY reloc, then actually timezone will be copied
3241 into your process image, and, since you define _timezone
3242 yourself, _timezone will not. Thus timezone and _timezone will
3243 wind up at different memory locations. The tzset call will set
3244 _timezone, leaving timezone unchanged. */
3246 if (h
->is_weakalias
)
3248 struct elf_link_hash_entry
*def
= weakdef (h
);
3250 /* If we get to this point, there is an implicit reference to
3251 the alias by a regular object file via the weak symbol H. */
3252 def
->ref_regular
= 1;
3254 /* Ensure that the backend adjust_dynamic_symbol function sees
3255 the strong alias before H by recursively calling ourselves. */
3256 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3260 /* If a symbol has no type and no size and does not require a PLT
3261 entry, then we are probably about to do the wrong thing here: we
3262 are probably going to create a COPY reloc for an empty object.
3263 This case can arise when a shared object is built with assembly
3264 code, and the assembly code fails to set the symbol type. */
3266 && h
->type
== STT_NOTYPE
3269 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3270 h
->root
.root
.string
);
3272 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3281 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3285 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3286 struct elf_link_hash_entry
*h
,
3289 unsigned int power_of_two
;
3291 asection
*sec
= h
->root
.u
.def
.section
;
3293 /* The section alignment of the definition is the maximum alignment
3294 requirement of symbols defined in the section. Since we don't
3295 know the symbol alignment requirement, we start with the
3296 maximum alignment and check low bits of the symbol address
3297 for the minimum alignment. */
3298 power_of_two
= bfd_section_alignment (sec
);
3299 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3300 while ((h
->root
.u
.def
.value
& mask
) != 0)
3306 if (power_of_two
> bfd_section_alignment (dynbss
))
3308 /* Adjust the section alignment if needed. */
3309 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3313 /* We make sure that the symbol will be aligned properly. */
3314 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3316 /* Define the symbol as being at this point in DYNBSS. */
3317 h
->root
.u
.def
.section
= dynbss
;
3318 h
->root
.u
.def
.value
= dynbss
->size
;
3320 /* Increment the size of DYNBSS to make room for the symbol. */
3321 dynbss
->size
+= h
->size
;
3323 /* No error if extern_protected_data is true. */
3324 if (h
->protected_def
3325 && (!info
->extern_protected_data
3326 || (info
->extern_protected_data
< 0
3327 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3328 info
->callbacks
->einfo
3329 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3330 h
->root
.root
.string
);
3335 /* Adjust all external symbols pointing into SEC_MERGE sections
3336 to reflect the object merging within the sections. */
3339 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3343 if ((h
->root
.type
== bfd_link_hash_defined
3344 || h
->root
.type
== bfd_link_hash_defweak
)
3345 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3346 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3348 bfd
*output_bfd
= (bfd
*) data
;
3350 h
->root
.u
.def
.value
=
3351 _bfd_merged_section_offset (output_bfd
,
3352 &h
->root
.u
.def
.section
,
3353 elf_section_data (sec
)->sec_info
,
3354 h
->root
.u
.def
.value
);
3360 /* Returns false if the symbol referred to by H should be considered
3361 to resolve local to the current module, and true if it should be
3362 considered to bind dynamically. */
3365 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3366 struct bfd_link_info
*info
,
3367 bool not_local_protected
)
3369 bool binding_stays_local_p
;
3370 const struct elf_backend_data
*bed
;
3371 struct elf_link_hash_table
*hash_table
;
3376 while (h
->root
.type
== bfd_link_hash_indirect
3377 || h
->root
.type
== bfd_link_hash_warning
)
3378 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3380 /* If it was forced local, then clearly it's not dynamic. */
3381 if (h
->dynindx
== -1)
3383 if (h
->forced_local
)
3386 /* Identify the cases where name binding rules say that a
3387 visible symbol resolves locally. */
3388 binding_stays_local_p
= (bfd_link_executable (info
)
3389 || SYMBOLIC_BIND (info
, h
));
3391 switch (ELF_ST_VISIBILITY (h
->other
))
3398 hash_table
= elf_hash_table (info
);
3399 if (!is_elf_hash_table (&hash_table
->root
))
3402 bed
= get_elf_backend_data (hash_table
->dynobj
);
3404 /* Proper resolution for function pointer equality may require
3405 that these symbols perhaps be resolved dynamically, even though
3406 we should be resolving them to the current module. */
3407 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3408 binding_stays_local_p
= true;
3415 /* If it isn't defined locally, then clearly it's dynamic. */
3416 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3419 /* Otherwise, the symbol is dynamic if binding rules don't tell
3420 us that it remains local. */
3421 return !binding_stays_local_p
;
3424 /* Return true if the symbol referred to by H should be considered
3425 to resolve local to the current module, and false otherwise. Differs
3426 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3427 undefined symbols. The two functions are virtually identical except
3428 for the place where dynindx == -1 is tested. If that test is true,
3429 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3430 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3432 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3433 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3434 treatment of undefined weak symbols. For those that do not make
3435 undefined weak symbols dynamic, both functions may return false. */
3438 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3439 struct bfd_link_info
*info
,
3440 bool local_protected
)
3442 const struct elf_backend_data
*bed
;
3443 struct elf_link_hash_table
*hash_table
;
3445 /* If it's a local sym, of course we resolve locally. */
3449 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3450 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3451 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3454 /* Forced local symbols resolve locally. */
3455 if (h
->forced_local
)
3458 /* Common symbols that become definitions don't get the DEF_REGULAR
3459 flag set, so test it first, and don't bail out. */
3460 if (ELF_COMMON_DEF_P (h
))
3462 /* If we don't have a definition in a regular file, then we can't
3463 resolve locally. The sym is either undefined or dynamic. */
3464 else if (!h
->def_regular
)
3467 /* Non-dynamic symbols resolve locally. */
3468 if (h
->dynindx
== -1)
3471 /* At this point, we know the symbol is defined and dynamic. In an
3472 executable it must resolve locally, likewise when building symbolic
3473 shared libraries. */
3474 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3477 /* Now deal with defined dynamic symbols in shared libraries. Ones
3478 with default visibility might not resolve locally. */
3479 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3482 hash_table
= elf_hash_table (info
);
3483 if (!is_elf_hash_table (&hash_table
->root
))
3486 /* STV_PROTECTED symbols with indirect external access are local. */
3487 if (info
->indirect_extern_access
> 0)
3490 bed
= get_elf_backend_data (hash_table
->dynobj
);
3492 /* If extern_protected_data is false, STV_PROTECTED non-function
3493 symbols are local. */
3494 if ((!info
->extern_protected_data
3495 || (info
->extern_protected_data
< 0
3496 && !bed
->extern_protected_data
))
3497 && !bed
->is_function_type (h
->type
))
3500 /* Function pointer equality tests may require that STV_PROTECTED
3501 symbols be treated as dynamic symbols. If the address of a
3502 function not defined in an executable is set to that function's
3503 plt entry in the executable, then the address of the function in
3504 a shared library must also be the plt entry in the executable. */
3505 return local_protected
;
3508 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3509 aligned. Returns the first TLS output section. */
3511 struct bfd_section
*
3512 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3514 struct bfd_section
*sec
, *tls
;
3515 unsigned int align
= 0;
3517 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3518 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3522 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3523 if (sec
->alignment_power
> align
)
3524 align
= sec
->alignment_power
;
3526 elf_hash_table (info
)->tls_sec
= tls
;
3528 /* Ensure the alignment of the first section (usually .tdata) is the largest
3529 alignment, so that the tls segment starts aligned. */
3531 tls
->alignment_power
= align
;
3536 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3538 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3539 Elf_Internal_Sym
*sym
)
3541 const struct elf_backend_data
*bed
;
3543 /* Local symbols do not count, but target specific ones might. */
3544 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3545 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3548 bed
= get_elf_backend_data (abfd
);
3549 /* Function symbols do not count. */
3550 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3553 /* If the section is undefined, then so is the symbol. */
3554 if (sym
->st_shndx
== SHN_UNDEF
)
3557 /* If the symbol is defined in the common section, then
3558 it is a common definition and so does not count. */
3559 if (bed
->common_definition (sym
))
3562 /* If the symbol is in a target specific section then we
3563 must rely upon the backend to tell us what it is. */
3564 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3565 /* FIXME - this function is not coded yet:
3567 return _bfd_is_global_symbol_definition (abfd, sym);
3569 Instead for now assume that the definition is not global,
3570 Even if this is wrong, at least the linker will behave
3571 in the same way that it used to do. */
3577 /* Search the symbol table of the archive element of the archive ABFD
3578 whose archive map contains a mention of SYMDEF, and determine if
3579 the symbol is defined in this element. */
3581 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3583 Elf_Internal_Shdr
* hdr
;
3587 Elf_Internal_Sym
*isymbuf
;
3588 Elf_Internal_Sym
*isym
;
3589 Elf_Internal_Sym
*isymend
;
3592 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
, NULL
);
3596 if (! bfd_check_format (abfd
, bfd_object
))
3599 /* Select the appropriate symbol table. If we don't know if the
3600 object file is an IR object, give linker LTO plugin a chance to
3601 get the correct symbol table. */
3602 if (abfd
->plugin_format
== bfd_plugin_yes
3603 #if BFD_SUPPORTS_PLUGINS
3604 || (abfd
->plugin_format
== bfd_plugin_unknown
3605 && bfd_link_plugin_object_p (abfd
))
3609 /* Use the IR symbol table if the object has been claimed by
3611 abfd
= abfd
->plugin_dummy_bfd
;
3612 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3616 if (elf_use_dt_symtab_p (abfd
))
3618 bfd_set_error (bfd_error_wrong_format
);
3622 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3623 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3625 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3628 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3630 /* The sh_info field of the symtab header tells us where the
3631 external symbols start. We don't care about the local symbols. */
3632 if (elf_bad_symtab (abfd
))
3634 extsymcount
= symcount
;
3639 extsymcount
= symcount
- hdr
->sh_info
;
3640 extsymoff
= hdr
->sh_info
;
3643 if (extsymcount
== 0)
3646 /* Read in the symbol table. */
3647 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3649 if (isymbuf
== NULL
)
3652 /* Scan the symbol table looking for SYMDEF. */
3654 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3658 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3663 if (strcmp (name
, symdef
->name
) == 0)
3665 result
= is_global_data_symbol_definition (abfd
, isym
);
3675 /* Add an entry to the .dynamic table. */
3678 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3682 struct elf_link_hash_table
*hash_table
;
3683 const struct elf_backend_data
*bed
;
3685 bfd_size_type newsize
;
3686 bfd_byte
*newcontents
;
3687 Elf_Internal_Dyn dyn
;
3689 hash_table
= elf_hash_table (info
);
3690 if (! is_elf_hash_table (&hash_table
->root
))
3693 if (tag
== DT_RELA
|| tag
== DT_REL
)
3694 hash_table
->dynamic_relocs
= true;
3696 bed
= get_elf_backend_data (hash_table
->dynobj
);
3697 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3698 BFD_ASSERT (s
!= NULL
);
3700 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3701 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3702 if (newcontents
== NULL
)
3706 dyn
.d_un
.d_val
= val
;
3707 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3710 s
->contents
= newcontents
;
3715 /* Strip zero-sized dynamic sections. */
3718 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3720 struct elf_link_hash_table
*hash_table
;
3721 const struct elf_backend_data
*bed
;
3722 asection
*s
, *sdynamic
, **pp
;
3723 asection
*rela_dyn
, *rel_dyn
;
3724 Elf_Internal_Dyn dyn
;
3725 bfd_byte
*extdyn
, *next
;
3726 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3727 bool strip_zero_sized
;
3728 bool strip_zero_sized_plt
;
3730 if (bfd_link_relocatable (info
))
3733 hash_table
= elf_hash_table (info
);
3734 if (!is_elf_hash_table (&hash_table
->root
))
3737 if (!hash_table
->dynobj
)
3740 sdynamic
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3744 bed
= get_elf_backend_data (hash_table
->dynobj
);
3745 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3747 strip_zero_sized
= false;
3748 strip_zero_sized_plt
= false;
3750 /* Strip zero-sized dynamic sections. */
3751 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3752 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3753 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3757 || s
== hash_table
->srelplt
->output_section
3758 || s
== hash_table
->splt
->output_section
))
3761 info
->output_bfd
->section_count
--;
3762 strip_zero_sized
= true;
3767 else if (s
== hash_table
->splt
->output_section
)
3769 s
= hash_table
->splt
;
3770 strip_zero_sized_plt
= true;
3773 s
= hash_table
->srelplt
;
3774 s
->flags
|= SEC_EXCLUDE
;
3775 s
->output_section
= bfd_abs_section_ptr
;
3780 if (strip_zero_sized_plt
&& sdynamic
->size
!= 0)
3781 for (extdyn
= sdynamic
->contents
;
3782 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3785 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3786 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3794 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3795 the procedure linkage table (the .plt section) has been
3797 memmove (extdyn
, next
,
3798 sdynamic
->size
- (next
- sdynamic
->contents
));
3803 if (strip_zero_sized
)
3805 /* Regenerate program headers. */
3806 elf_seg_map (info
->output_bfd
) = NULL
;
3807 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
,
3814 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3815 1 if a DT_NEEDED tag already exists, and 0 on success. */
3818 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3820 struct elf_link_hash_table
*hash_table
;
3824 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3827 hash_table
= elf_hash_table (info
);
3828 soname
= elf_dt_name (abfd
);
3829 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, false);
3830 if (strindex
== (size_t) -1)
3833 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3836 const struct elf_backend_data
*bed
;
3839 bed
= get_elf_backend_data (hash_table
->dynobj
);
3840 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3841 if (sdyn
!= NULL
&& sdyn
->size
!= 0)
3842 for (extdyn
= sdyn
->contents
;
3843 extdyn
< sdyn
->contents
+ sdyn
->size
;
3844 extdyn
+= bed
->s
->sizeof_dyn
)
3846 Elf_Internal_Dyn dyn
;
3848 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3849 if (dyn
.d_tag
== DT_NEEDED
3850 && dyn
.d_un
.d_val
== strindex
)
3852 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3858 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3861 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3867 /* Return true if SONAME is on the needed list between NEEDED and STOP
3868 (or the end of list if STOP is NULL), and needed by a library that
3872 on_needed_list (const char *soname
,
3873 struct bfd_link_needed_list
*needed
,
3874 struct bfd_link_needed_list
*stop
)
3876 struct bfd_link_needed_list
*look
;
3877 for (look
= needed
; look
!= stop
; look
= look
->next
)
3878 if (strcmp (soname
, look
->name
) == 0
3879 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3880 /* If needed by a library that itself is not directly
3881 needed, recursively check whether that library is
3882 indirectly needed. Since we add DT_NEEDED entries to
3883 the end of the list, library dependencies appear after
3884 the library. Therefore search prior to the current
3885 LOOK, preventing possible infinite recursion. */
3886 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3892 /* Sort symbol by value, section, size, and type. */
3894 elf_sort_symbol (const void *arg1
, const void *arg2
)
3896 const struct elf_link_hash_entry
*h1
;
3897 const struct elf_link_hash_entry
*h2
;
3898 bfd_signed_vma vdiff
;
3903 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3904 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3905 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3907 return vdiff
> 0 ? 1 : -1;
3909 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3913 /* Sort so that sized symbols are selected over zero size symbols. */
3914 vdiff
= h1
->size
- h2
->size
;
3916 return vdiff
> 0 ? 1 : -1;
3918 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3919 if (h1
->type
!= h2
->type
)
3920 return h1
->type
- h2
->type
;
3922 /* If symbols are properly sized and typed, and multiple strong
3923 aliases are not defined in a shared library by the user we
3924 shouldn't get here. Unfortunately linker script symbols like
3925 __bss_start sometimes match a user symbol defined at the start of
3926 .bss without proper size and type. We'd like to preference the
3927 user symbol over reserved system symbols. Sort on leading
3929 n1
= h1
->root
.root
.string
;
3930 n2
= h2
->root
.root
.string
;
3943 /* Final sort on name selects user symbols like '_u' over reserved
3944 system symbols like '_Z' and also will avoid qsort instability. */
3948 /* This function is used to adjust offsets into .dynstr for
3949 dynamic symbols. This is called via elf_link_hash_traverse. */
3952 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3954 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3956 if (h
->dynindx
!= -1)
3957 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3961 /* Assign string offsets in .dynstr, update all structures referencing
3965 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3967 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3968 struct elf_link_local_dynamic_entry
*entry
;
3969 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3970 bfd
*dynobj
= hash_table
->dynobj
;
3973 const struct elf_backend_data
*bed
;
3976 _bfd_elf_strtab_finalize (dynstr
);
3977 size
= _bfd_elf_strtab_size (dynstr
);
3979 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3981 if (info
->callbacks
->examine_strtab
)
3982 info
->callbacks
->examine_strtab (dynstr
);
3984 bed
= get_elf_backend_data (dynobj
);
3985 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3986 BFD_ASSERT (sdyn
!= NULL
);
3988 /* Update all .dynamic entries referencing .dynstr strings. */
3989 for (extdyn
= sdyn
->contents
;
3990 extdyn
< PTR_ADD (sdyn
->contents
, sdyn
->size
);
3991 extdyn
+= bed
->s
->sizeof_dyn
)
3993 Elf_Internal_Dyn dyn
;
3995 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3999 dyn
.d_un
.d_val
= size
;
4009 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
4014 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
4017 /* Now update local dynamic symbols. */
4018 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
4019 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
4020 entry
->isym
.st_name
);
4022 /* And the rest of dynamic symbols. */
4023 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
4025 /* Adjust version definitions. */
4026 if (elf_tdata (output_bfd
)->cverdefs
)
4031 Elf_Internal_Verdef def
;
4032 Elf_Internal_Verdaux defaux
;
4034 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
4038 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
4040 p
+= sizeof (Elf_External_Verdef
);
4041 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
4043 for (i
= 0; i
< def
.vd_cnt
; ++i
)
4045 _bfd_elf_swap_verdaux_in (output_bfd
,
4046 (Elf_External_Verdaux
*) p
, &defaux
);
4047 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
4049 _bfd_elf_swap_verdaux_out (output_bfd
,
4050 &defaux
, (Elf_External_Verdaux
*) p
);
4051 p
+= sizeof (Elf_External_Verdaux
);
4054 while (def
.vd_next
);
4057 /* Adjust version references. */
4058 if (elf_tdata (output_bfd
)->verref
)
4063 Elf_Internal_Verneed need
;
4064 Elf_Internal_Vernaux needaux
;
4066 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
4070 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
4072 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
4073 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
4074 (Elf_External_Verneed
*) p
);
4075 p
+= sizeof (Elf_External_Verneed
);
4076 for (i
= 0; i
< need
.vn_cnt
; ++i
)
4078 _bfd_elf_swap_vernaux_in (output_bfd
,
4079 (Elf_External_Vernaux
*) p
, &needaux
);
4080 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
4082 _bfd_elf_swap_vernaux_out (output_bfd
,
4084 (Elf_External_Vernaux
*) p
);
4085 p
+= sizeof (Elf_External_Vernaux
);
4088 while (need
.vn_next
);
4094 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4095 The default is to only match when the INPUT and OUTPUT are exactly
4099 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
4100 const bfd_target
*output
)
4102 return input
== output
;
4105 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4106 This version is used when different targets for the same architecture
4107 are virtually identical. */
4110 _bfd_elf_relocs_compatible (const bfd_target
*input
,
4111 const bfd_target
*output
)
4113 const struct elf_backend_data
*obed
, *ibed
;
4115 if (input
== output
)
4118 ibed
= xvec_get_elf_backend_data (input
);
4119 obed
= xvec_get_elf_backend_data (output
);
4121 if (ibed
->arch
!= obed
->arch
)
4124 /* If both backends are using this function, deem them compatible. */
4125 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
4128 /* Make a special call to the linker "notice" function to tell it that
4129 we are about to handle an as-needed lib, or have finished
4130 processing the lib. */
4133 _bfd_elf_notice_as_needed (bfd
*ibfd
,
4134 struct bfd_link_info
*info
,
4135 enum notice_asneeded_action act
)
4137 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
4140 /* Call ACTION on each relocation in an ELF object file. */
4143 _bfd_elf_link_iterate_on_relocs
4144 (bfd
*abfd
, struct bfd_link_info
*info
,
4145 bool (*action
) (bfd
*, struct bfd_link_info
*, asection
*,
4146 const Elf_Internal_Rela
*))
4148 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4149 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4151 /* If this object is the same format as the output object, and it is
4152 not a shared library, then let the backend look through the
4155 This is required to build global offset table entries and to
4156 arrange for dynamic relocs. It is not required for the
4157 particular common case of linking non PIC code, even when linking
4158 against shared libraries, but unfortunately there is no way of
4159 knowing whether an object file has been compiled PIC or not.
4160 Looking through the relocs is not particularly time consuming.
4161 The problem is that we must either (1) keep the relocs in memory,
4162 which causes the linker to require additional runtime memory or
4163 (2) read the relocs twice from the input file, which wastes time.
4164 This would be a good case for using mmap.
4166 I have no idea how to handle linking PIC code into a file of a
4167 different format. It probably can't be done. */
4168 if ((abfd
->flags
& DYNAMIC
) == 0
4169 && is_elf_hash_table (&htab
->root
)
4170 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4171 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4175 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4177 Elf_Internal_Rela
*internal_relocs
;
4180 /* Don't check relocations in excluded sections. Don't do
4181 anything special with non-loaded, non-alloced sections.
4182 In particular, any relocs in such sections should not
4183 affect GOT and PLT reference counting (ie. we don't
4184 allow them to create GOT or PLT entries), there's no
4185 possibility or desire to optimize TLS relocs, and
4186 there's not much point in propagating relocs to shared
4187 libs that the dynamic linker won't relocate. */
4188 if ((o
->flags
& SEC_ALLOC
) == 0
4189 || (o
->flags
& SEC_RELOC
) == 0
4190 || (o
->flags
& SEC_EXCLUDE
) != 0
4191 || o
->reloc_count
== 0
4192 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4193 && (o
->flags
& SEC_DEBUGGING
) != 0)
4194 || bfd_is_abs_section (o
->output_section
))
4197 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
4200 _bfd_link_keep_memory (info
));
4201 if (internal_relocs
== NULL
)
4204 ok
= action (abfd
, info
, o
, internal_relocs
);
4206 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4207 free (internal_relocs
);
4217 /* Check relocations in an ELF object file. This is called after
4218 all input files have been opened. */
4221 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
4223 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4224 if (bed
->check_relocs
!= NULL
)
4225 return _bfd_elf_link_iterate_on_relocs (abfd
, info
,
4230 /* Add symbols from an ELF object file to the linker hash table. */
4233 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4235 Elf_Internal_Ehdr
*ehdr
;
4236 Elf_Internal_Shdr
*hdr
;
4240 struct elf_link_hash_entry
**sym_hash
;
4242 Elf_External_Versym
*extversym
= NULL
;
4243 Elf_External_Versym
*extversym_end
= NULL
;
4244 Elf_External_Versym
*ever
;
4245 struct elf_link_hash_entry
*weaks
;
4246 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4247 size_t nondeflt_vers_cnt
= 0;
4248 Elf_Internal_Sym
*isymbuf
= NULL
;
4249 Elf_Internal_Sym
*isym
;
4250 Elf_Internal_Sym
*isymend
;
4251 const struct elf_backend_data
*bed
;
4253 struct elf_link_hash_table
*htab
;
4254 void *alloc_mark
= NULL
;
4255 struct bfd_hash_entry
**old_table
= NULL
;
4256 unsigned int old_size
= 0;
4257 unsigned int old_count
= 0;
4258 void *old_tab
= NULL
;
4260 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4261 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4262 void *old_strtab
= NULL
;
4267 htab
= elf_hash_table (info
);
4268 bed
= get_elf_backend_data (abfd
);
4270 if (elf_use_dt_symtab_p (abfd
))
4272 bfd_set_error (bfd_error_wrong_format
);
4276 if ((abfd
->flags
& DYNAMIC
) == 0)
4282 /* You can't use -r against a dynamic object. Also, there's no
4283 hope of using a dynamic object which does not exactly match
4284 the format of the output file. */
4285 if (bfd_link_relocatable (info
)
4286 || !is_elf_hash_table (&htab
->root
)
4287 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4289 if (bfd_link_relocatable (info
))
4290 bfd_set_error (bfd_error_invalid_operation
);
4292 bfd_set_error (bfd_error_wrong_format
);
4297 ehdr
= elf_elfheader (abfd
);
4298 if (info
->warn_alternate_em
4299 && bed
->elf_machine_code
!= ehdr
->e_machine
4300 && ((bed
->elf_machine_alt1
!= 0
4301 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4302 || (bed
->elf_machine_alt2
!= 0
4303 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4305 /* xgettext:c-format */
4306 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4307 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4309 /* As a GNU extension, any input sections which are named
4310 .gnu.warning.SYMBOL are treated as warning symbols for the given
4311 symbol. This differs from .gnu.warning sections, which generate
4312 warnings when they are included in an output file. */
4313 /* PR 12761: Also generate this warning when building shared libraries. */
4314 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4318 name
= bfd_section_name (s
);
4319 if (startswith (name
, ".gnu.warning."))
4324 name
+= sizeof ".gnu.warning." - 1;
4326 /* If this is a shared object, then look up the symbol
4327 in the hash table. If it is there, and it is already
4328 been defined, then we will not be using the entry
4329 from this shared object, so we don't need to warn.
4330 FIXME: If we see the definition in a regular object
4331 later on, we will warn, but we shouldn't. The only
4332 fix is to keep track of what warnings we are supposed
4333 to emit, and then handle them all at the end of the
4337 struct elf_link_hash_entry
*h
;
4339 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
4341 /* FIXME: What about bfd_link_hash_common? */
4343 && (h
->root
.type
== bfd_link_hash_defined
4344 || h
->root
.type
== bfd_link_hash_defweak
))
4349 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4353 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4358 if (! (_bfd_generic_link_add_one_symbol
4359 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4360 false, bed
->collect
, NULL
)))
4363 if (bfd_link_executable (info
))
4365 /* Clobber the section size so that the warning does
4366 not get copied into the output file. */
4369 /* Also set SEC_EXCLUDE, so that symbols defined in
4370 the warning section don't get copied to the output. */
4371 s
->flags
|= SEC_EXCLUDE
;
4376 just_syms
= ((s
= abfd
->sections
) != NULL
4377 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4382 /* If we are creating a shared library, create all the dynamic
4383 sections immediately. We need to attach them to something,
4384 so we attach them to this BFD, provided it is the right
4385 format and is not from ld --just-symbols. Always create the
4386 dynamic sections for -E/--dynamic-list. FIXME: If there
4387 are no input BFD's of the same format as the output, we can't
4388 make a shared library. */
4390 && (bfd_link_pic (info
)
4391 || (!bfd_link_relocatable (info
)
4393 && (info
->export_dynamic
|| info
->dynamic
)))
4394 && is_elf_hash_table (&htab
->root
)
4395 && info
->output_bfd
->xvec
== abfd
->xvec
4396 && !htab
->dynamic_sections_created
)
4398 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4402 else if (!is_elf_hash_table (&htab
->root
))
4406 const char *soname
= NULL
;
4408 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4409 const Elf_Internal_Phdr
*phdr
;
4410 struct elf_link_loaded_list
*loaded_lib
;
4412 /* ld --just-symbols and dynamic objects don't mix very well.
4413 ld shouldn't allow it. */
4417 /* If this dynamic lib was specified on the command line with
4418 --as-needed in effect, then we don't want to add a DT_NEEDED
4419 tag unless the lib is actually used. Similary for libs brought
4420 in by another lib's DT_NEEDED. When --no-add-needed is used
4421 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4422 any dynamic library in DT_NEEDED tags in the dynamic lib at
4424 add_needed
= (elf_dyn_lib_class (abfd
)
4425 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4426 | DYN_NO_NEEDED
)) == 0;
4428 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4429 if (s
!= NULL
&& s
->size
!= 0 && (s
->flags
& SEC_HAS_CONTENTS
) != 0)
4433 unsigned int elfsec
;
4434 unsigned long shlink
;
4436 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4443 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4444 if (elfsec
== SHN_BAD
)
4445 goto error_free_dyn
;
4446 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4448 for (extdyn
= dynbuf
;
4449 (size_t) (dynbuf
+ s
->size
- extdyn
) >= bed
->s
->sizeof_dyn
;
4450 extdyn
+= bed
->s
->sizeof_dyn
)
4452 Elf_Internal_Dyn dyn
;
4454 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4455 if (dyn
.d_tag
== DT_SONAME
)
4457 unsigned int tagv
= dyn
.d_un
.d_val
;
4458 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4460 goto error_free_dyn
;
4462 if (dyn
.d_tag
== DT_NEEDED
)
4464 struct bfd_link_needed_list
*n
, **pn
;
4466 unsigned int tagv
= dyn
.d_un
.d_val
;
4467 size_t amt
= sizeof (struct bfd_link_needed_list
);
4469 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4470 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4471 if (n
== NULL
|| fnm
== NULL
)
4472 goto error_free_dyn
;
4473 amt
= strlen (fnm
) + 1;
4474 anm
= (char *) bfd_alloc (abfd
, amt
);
4476 goto error_free_dyn
;
4477 memcpy (anm
, fnm
, amt
);
4481 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4485 if (dyn
.d_tag
== DT_RUNPATH
)
4487 struct bfd_link_needed_list
*n
, **pn
;
4489 unsigned int tagv
= dyn
.d_un
.d_val
;
4490 size_t amt
= sizeof (struct bfd_link_needed_list
);
4492 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4493 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4494 if (n
== NULL
|| fnm
== NULL
)
4495 goto error_free_dyn
;
4496 amt
= strlen (fnm
) + 1;
4497 anm
= (char *) bfd_alloc (abfd
, amt
);
4499 goto error_free_dyn
;
4500 memcpy (anm
, fnm
, amt
);
4504 for (pn
= & runpath
;
4510 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4511 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4513 struct bfd_link_needed_list
*n
, **pn
;
4515 unsigned int tagv
= dyn
.d_un
.d_val
;
4516 size_t amt
= sizeof (struct bfd_link_needed_list
);
4518 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4519 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4520 if (n
== NULL
|| fnm
== NULL
)
4521 goto error_free_dyn
;
4522 amt
= strlen (fnm
) + 1;
4523 anm
= (char *) bfd_alloc (abfd
, amt
);
4525 goto error_free_dyn
;
4526 memcpy (anm
, fnm
, amt
);
4536 if (dyn
.d_tag
== DT_AUDIT
)
4538 unsigned int tagv
= dyn
.d_un
.d_val
;
4539 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4541 if (dyn
.d_tag
== DT_FLAGS_1
)
4542 elf_tdata (abfd
)->is_pie
= (dyn
.d_un
.d_val
& DF_1_PIE
) != 0;
4548 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4549 frees all more recently bfd_alloc'd blocks as well. */
4555 struct bfd_link_needed_list
**pn
;
4556 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4561 /* If we have a PT_GNU_RELRO program header, mark as read-only
4562 all sections contained fully therein. This makes relro
4563 shared library sections appear as they will at run-time. */
4564 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4565 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4566 if (phdr
->p_type
== PT_GNU_RELRO
)
4568 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4570 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4572 if ((s
->flags
& SEC_ALLOC
) != 0
4573 && s
->vma
* opb
>= phdr
->p_vaddr
4574 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4575 s
->flags
|= SEC_READONLY
;
4580 /* We do not want to include any of the sections in a dynamic
4581 object in the output file. We hack by simply clobbering the
4582 list of sections in the BFD. This could be handled more
4583 cleanly by, say, a new section flag; the existing
4584 SEC_NEVER_LOAD flag is not the one we want, because that one
4585 still implies that the section takes up space in the output
4587 bfd_section_list_clear (abfd
);
4589 /* Find the name to use in a DT_NEEDED entry that refers to this
4590 object. If the object has a DT_SONAME entry, we use it.
4591 Otherwise, if the generic linker stuck something in
4592 elf_dt_name, we use that. Otherwise, we just use the file
4594 if (soname
== NULL
|| *soname
== '\0')
4596 soname
= elf_dt_name (abfd
);
4597 if (soname
== NULL
|| *soname
== '\0')
4598 soname
= bfd_get_filename (abfd
);
4601 /* Save the SONAME because sometimes the linker emulation code
4602 will need to know it. */
4603 elf_dt_name (abfd
) = soname
;
4605 /* If we have already included this dynamic object in the
4606 link, just ignore it. There is no reason to include a
4607 particular dynamic object more than once. */
4608 for (loaded_lib
= htab
->dyn_loaded
;
4610 loaded_lib
= loaded_lib
->next
)
4612 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4616 /* Create dynamic sections for backends that require that be done
4617 before setup_gnu_properties. */
4619 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4622 /* Save the DT_AUDIT entry for the linker emulation code. */
4623 elf_dt_audit (abfd
) = audit
;
4626 /* If this is a dynamic object, we always link against the .dynsym
4627 symbol table, not the .symtab symbol table. The dynamic linker
4628 will only see the .dynsym symbol table, so there is no reason to
4629 look at .symtab for a dynamic object. */
4631 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4632 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4634 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4636 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4638 /* The sh_info field of the symtab header tells us where the
4639 external symbols start. We don't care about the local symbols at
4641 if (elf_bad_symtab (abfd
))
4643 extsymcount
= symcount
;
4648 extsymcount
= symcount
- hdr
->sh_info
;
4649 extsymoff
= hdr
->sh_info
;
4652 sym_hash
= elf_sym_hashes (abfd
);
4653 if (extsymcount
!= 0)
4655 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4657 if (isymbuf
== NULL
)
4660 if (sym_hash
== NULL
)
4662 /* We store a pointer to the hash table entry for each
4664 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4665 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4666 if (sym_hash
== NULL
)
4667 goto error_free_sym
;
4668 elf_sym_hashes (abfd
) = sym_hash
;
4674 /* Read in any version definitions. */
4675 if (!_bfd_elf_slurp_version_tables (abfd
,
4676 info
->default_imported_symver
))
4677 goto error_free_sym
;
4679 /* Read in the symbol versions, but don't bother to convert them
4680 to internal format. */
4681 if (elf_dynversym (abfd
) != 0)
4683 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4684 bfd_size_type amt
= versymhdr
->sh_size
;
4686 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4687 goto error_free_sym
;
4688 extversym
= (Elf_External_Versym
*)
4689 _bfd_malloc_and_read (abfd
, amt
, amt
);
4690 if (extversym
== NULL
)
4691 goto error_free_sym
;
4692 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4696 /* If we are loading an as-needed shared lib, save the symbol table
4697 state before we start adding symbols. If the lib turns out
4698 to be unneeded, restore the state. */
4699 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4704 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4706 struct bfd_hash_entry
*p
;
4707 struct elf_link_hash_entry
*h
;
4709 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4711 h
= (struct elf_link_hash_entry
*) p
;
4712 entsize
+= htab
->root
.table
.entsize
;
4713 if (h
->root
.type
== bfd_link_hash_warning
)
4715 entsize
+= htab
->root
.table
.entsize
;
4716 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4718 if (h
->root
.type
== bfd_link_hash_common
)
4719 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4723 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4724 old_tab
= bfd_malloc (tabsize
+ entsize
);
4725 if (old_tab
== NULL
)
4726 goto error_free_vers
;
4728 /* Remember the current objalloc pointer, so that all mem for
4729 symbols added can later be reclaimed. */
4730 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4731 if (alloc_mark
== NULL
)
4732 goto error_free_vers
;
4734 /* Make a special call to the linker "notice" function to
4735 tell it that we are about to handle an as-needed lib. */
4736 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4737 goto error_free_vers
;
4739 /* Clone the symbol table. Remember some pointers into the
4740 symbol table, and dynamic symbol count. */
4741 old_ent
= (char *) old_tab
+ tabsize
;
4742 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4743 old_undefs
= htab
->root
.undefs
;
4744 old_undefs_tail
= htab
->root
.undefs_tail
;
4745 old_table
= htab
->root
.table
.table
;
4746 old_size
= htab
->root
.table
.size
;
4747 old_count
= htab
->root
.table
.count
;
4749 if (htab
->dynstr
!= NULL
)
4751 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4752 if (old_strtab
== NULL
)
4753 goto error_free_vers
;
4756 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4758 struct bfd_hash_entry
*p
;
4759 struct elf_link_hash_entry
*h
;
4761 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4763 h
= (struct elf_link_hash_entry
*) p
;
4764 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4765 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4766 if (h
->root
.type
== bfd_link_hash_warning
)
4768 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4769 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4770 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4772 if (h
->root
.type
== bfd_link_hash_common
)
4774 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4775 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4782 if (extversym
== NULL
)
4784 else if (extversym
+ extsymoff
< extversym_end
)
4785 ever
= extversym
+ extsymoff
;
4788 /* xgettext:c-format */
4789 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4790 abfd
, (long) extsymoff
,
4791 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4792 bfd_set_error (bfd_error_bad_value
);
4793 goto error_free_vers
;
4796 if (!bfd_link_relocatable (info
)
4797 && abfd
->lto_slim_object
)
4800 (_("%pB: plugin needed to handle lto object"), abfd
);
4803 for (isym
= isymbuf
, isymend
= PTR_ADD (isymbuf
, extsymcount
);
4805 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4809 asection
*sec
, *new_sec
;
4812 struct elf_link_hash_entry
*h
;
4813 struct elf_link_hash_entry
*hi
;
4815 bool size_change_ok
;
4816 bool type_change_ok
;
4822 unsigned int old_alignment
;
4823 unsigned int shindex
;
4829 flags
= BSF_NO_FLAGS
;
4831 value
= isym
->st_value
;
4832 common
= bed
->common_definition (isym
);
4833 if (common
&& info
->inhibit_common_definition
)
4835 /* Treat common symbol as undefined for --no-define-common. */
4836 isym
->st_shndx
= SHN_UNDEF
;
4841 bind
= ELF_ST_BIND (isym
->st_info
);
4845 /* This should be impossible, since ELF requires that all
4846 global symbols follow all local symbols, and that sh_info
4847 point to the first global symbol. Unfortunately, Irix 5
4849 if (elf_bad_symtab (abfd
))
4852 /* If we aren't prepared to handle locals within the globals
4853 then we'll likely segfault on a NULL symbol hash if the
4854 symbol is ever referenced in relocations. */
4855 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4856 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4857 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4858 " (>= sh_info of %lu)"),
4859 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4862 /* Dynamic object relocations are not processed by ld, so
4863 ld won't run into the problem mentioned above. */
4866 bfd_set_error (bfd_error_bad_value
);
4867 goto error_free_vers
;
4870 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4878 case STB_GNU_UNIQUE
:
4879 flags
= BSF_GNU_UNIQUE
;
4883 /* Leave it up to the processor backend. */
4887 if (isym
->st_shndx
== SHN_UNDEF
)
4888 sec
= bfd_und_section_ptr
;
4889 else if (isym
->st_shndx
== SHN_ABS
)
4890 sec
= bfd_abs_section_ptr
;
4891 else if (isym
->st_shndx
== SHN_COMMON
)
4893 sec
= bfd_com_section_ptr
;
4894 /* What ELF calls the size we call the value. What ELF
4895 calls the value we call the alignment. */
4896 value
= isym
->st_size
;
4900 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4902 sec
= bfd_abs_section_ptr
;
4903 else if (discarded_section (sec
))
4905 /* Symbols from discarded section are undefined. We keep
4907 sec
= bfd_und_section_ptr
;
4909 isym
->st_shndx
= SHN_UNDEF
;
4911 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4915 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4918 goto error_free_vers
;
4920 if (isym
->st_shndx
== SHN_COMMON
4921 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4923 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4927 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4929 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4931 goto error_free_vers
;
4935 else if (isym
->st_shndx
== SHN_COMMON
4936 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4937 && !bfd_link_relocatable (info
))
4939 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4943 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4944 | SEC_LINKER_CREATED
);
4945 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4947 goto error_free_vers
;
4951 else if (bed
->elf_add_symbol_hook
)
4953 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4955 goto error_free_vers
;
4957 /* The hook function sets the name to NULL if this symbol
4958 should be skipped for some reason. */
4963 /* Sanity check that all possibilities were handled. */
4967 /* Silently discard TLS symbols from --just-syms. There's
4968 no way to combine a static TLS block with a new TLS block
4969 for this executable. */
4970 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4971 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4974 if (bfd_is_und_section (sec
)
4975 || bfd_is_com_section (sec
))
4980 size_change_ok
= false;
4981 type_change_ok
= bed
->type_change_ok
;
4988 if (is_elf_hash_table (&htab
->root
))
4990 Elf_Internal_Versym iver
;
4991 unsigned int vernum
= 0;
4996 if (info
->default_imported_symver
)
4997 /* Use the default symbol version created earlier. */
4998 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
5002 else if (ever
>= extversym_end
)
5004 /* xgettext:c-format */
5005 _bfd_error_handler (_("%pB: not enough version information"),
5007 bfd_set_error (bfd_error_bad_value
);
5008 goto error_free_vers
;
5011 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
5013 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
5015 /* If this is a hidden symbol, or if it is not version
5016 1, we append the version name to the symbol name.
5017 However, we do not modify a non-hidden absolute symbol
5018 if it is not a function, because it might be the version
5019 symbol itself. FIXME: What if it isn't? */
5020 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
5022 && (!bfd_is_abs_section (sec
)
5023 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
5026 size_t namelen
, verlen
, newlen
;
5029 if (isym
->st_shndx
!= SHN_UNDEF
)
5031 if (vernum
> elf_tdata (abfd
)->cverdefs
)
5033 else if (vernum
> 1)
5035 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
5042 /* xgettext:c-format */
5043 (_("%pB: %s: invalid version %u (max %d)"),
5045 elf_tdata (abfd
)->cverdefs
);
5046 bfd_set_error (bfd_error_bad_value
);
5047 goto error_free_vers
;
5052 /* We cannot simply test for the number of
5053 entries in the VERNEED section since the
5054 numbers for the needed versions do not start
5056 Elf_Internal_Verneed
*t
;
5059 for (t
= elf_tdata (abfd
)->verref
;
5063 Elf_Internal_Vernaux
*a
;
5065 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5067 if (a
->vna_other
== vernum
)
5069 verstr
= a
->vna_nodename
;
5079 /* xgettext:c-format */
5080 (_("%pB: %s: invalid needed version %d"),
5081 abfd
, name
, vernum
);
5082 bfd_set_error (bfd_error_bad_value
);
5083 goto error_free_vers
;
5087 namelen
= strlen (name
);
5088 verlen
= strlen (verstr
);
5089 newlen
= namelen
+ verlen
+ 2;
5090 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5091 && isym
->st_shndx
!= SHN_UNDEF
)
5094 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
5095 if (newname
== NULL
)
5096 goto error_free_vers
;
5097 memcpy (newname
, name
, namelen
);
5098 p
= newname
+ namelen
;
5100 /* If this is a defined non-hidden version symbol,
5101 we add another @ to the name. This indicates the
5102 default version of the symbol. */
5103 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5104 && isym
->st_shndx
!= SHN_UNDEF
)
5106 memcpy (p
, verstr
, verlen
+ 1);
5111 /* If this symbol has default visibility and the user has
5112 requested we not re-export it, then mark it as hidden. */
5113 if (!bfd_is_und_section (sec
)
5116 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
5117 isym
->st_other
= (STV_HIDDEN
5118 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
5120 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
5121 sym_hash
, &old_bfd
, &old_weak
,
5122 &old_alignment
, &skip
, &override
,
5123 &type_change_ok
, &size_change_ok
,
5125 goto error_free_vers
;
5130 /* Override a definition only if the new symbol matches the
5132 if (override
&& matched
)
5136 while (h
->root
.type
== bfd_link_hash_indirect
5137 || h
->root
.type
== bfd_link_hash_warning
)
5138 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5140 if (h
->versioned
!= unversioned
5141 && elf_tdata (abfd
)->verdef
!= NULL
5144 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
5147 if (! (_bfd_generic_link_add_one_symbol
5148 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
5149 NULL
, false, bed
->collect
,
5150 (struct bfd_link_hash_entry
**) sym_hash
)))
5151 goto error_free_vers
;
5154 /* We need to make sure that indirect symbol dynamic flags are
5157 while (h
->root
.type
== bfd_link_hash_indirect
5158 || h
->root
.type
== bfd_link_hash_warning
)
5159 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5163 /* Setting the index to -3 tells elf_link_output_extsym that
5164 this symbol is defined in a discarded section. */
5165 if (discarded
&& is_elf_hash_table (&htab
->root
))
5168 new_weak
= (flags
& BSF_WEAK
) != 0;
5172 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
5173 && is_elf_hash_table (&htab
->root
)
5174 && h
->u
.alias
== NULL
)
5176 /* Keep a list of all weak defined non function symbols from
5177 a dynamic object, using the alias field. Later in this
5178 function we will set the alias field to the correct
5179 value. We only put non-function symbols from dynamic
5180 objects on this list, because that happens to be the only
5181 time we need to know the normal symbol corresponding to a
5182 weak symbol, and the information is time consuming to
5183 figure out. If the alias field is not already NULL,
5184 then this symbol was already defined by some previous
5185 dynamic object, and we will be using that previous
5186 definition anyhow. */
5192 /* Set the alignment of a common symbol. */
5193 if ((common
|| bfd_is_com_section (sec
))
5194 && h
->root
.type
== bfd_link_hash_common
)
5199 align
= bfd_log2 (isym
->st_value
);
5202 /* The new symbol is a common symbol in a shared object.
5203 We need to get the alignment from the section. */
5204 align
= new_sec
->alignment_power
;
5206 if (align
> old_alignment
)
5207 h
->root
.u
.c
.p
->alignment_power
= align
;
5209 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5212 if (is_elf_hash_table (&htab
->root
))
5214 /* Set a flag in the hash table entry indicating the type of
5215 reference or definition we just found. A dynamic symbol
5216 is one which is referenced or defined by both a regular
5217 object and a shared object. */
5218 bool dynsym
= false;
5220 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5221 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5223 /* Except for this flag to track nonweak references. */
5225 && bind
!= STB_WEAK
)
5226 h
->ref_ir_nonweak
= 1;
5233 if (bind
!= STB_WEAK
)
5234 h
->ref_regular_nonweak
= 1;
5251 hi
->ref_dynamic
= 1;
5256 hi
->def_dynamic
= 1;
5260 /* If an indirect symbol has been forced local, don't
5261 make the real symbol dynamic. */
5262 if (h
!= hi
&& hi
->forced_local
)
5266 if (bfd_link_dll (info
)
5276 && weakdef (h
)->dynindx
!= -1))
5280 /* Check to see if we need to add an indirect symbol for
5281 the default name. */
5283 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5285 && hi
->versioned
== versioned_hidden
))
5286 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5287 sec
, value
, &old_bfd
, &dynsym
))
5288 goto error_free_vers
;
5290 /* Check the alignment when a common symbol is involved. This
5291 can change when a common symbol is overridden by a normal
5292 definition or a common symbol is ignored due to the old
5293 normal definition. We need to make sure the maximum
5294 alignment is maintained. */
5295 if ((old_alignment
|| common
)
5296 && h
->root
.type
!= bfd_link_hash_common
)
5298 unsigned int common_align
;
5299 unsigned int normal_align
;
5300 unsigned int symbol_align
;
5304 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5305 || h
->root
.type
== bfd_link_hash_defweak
);
5307 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5308 if (h
->root
.u
.def
.section
->owner
!= NULL
5309 && (h
->root
.u
.def
.section
->owner
->flags
5310 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5312 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5313 if (normal_align
> symbol_align
)
5314 normal_align
= symbol_align
;
5317 normal_align
= symbol_align
;
5321 common_align
= old_alignment
;
5322 common_bfd
= old_bfd
;
5327 common_align
= bfd_log2 (isym
->st_value
);
5329 normal_bfd
= old_bfd
;
5332 if (normal_align
< common_align
)
5334 /* PR binutils/2735 */
5335 if (normal_bfd
== NULL
)
5337 /* xgettext:c-format */
5338 (_("warning: alignment %u of common symbol `%s' in %pB is"
5339 " greater than the alignment (%u) of its section %pA"),
5340 1 << common_align
, name
, common_bfd
,
5341 1 << normal_align
, h
->root
.u
.def
.section
);
5344 /* xgettext:c-format */
5345 (_("warning: alignment %u of normal symbol `%s' in %pB"
5346 " is smaller than %u used by the common definition in %pB"),
5347 1 << normal_align
, name
, normal_bfd
,
5348 1 << common_align
, common_bfd
);
5350 /* PR 30499: make sure that users understand that this warning is serious. */
5352 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5356 /* Remember the symbol size if it isn't undefined. */
5357 if (isym
->st_size
!= 0
5358 && isym
->st_shndx
!= SHN_UNDEF
5359 && (definition
|| h
->size
== 0))
5362 && h
->size
!= isym
->st_size
5363 && ! size_change_ok
)
5366 /* xgettext:c-format */
5367 (_("warning: size of symbol `%s' changed"
5368 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5369 name
, (uint64_t) h
->size
, old_bfd
,
5370 (uint64_t) isym
->st_size
, abfd
);
5372 /* PR 30499: make sure that users understand that this warning is serious. */
5374 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5377 h
->size
= isym
->st_size
;
5380 /* If this is a common symbol, then we always want H->SIZE
5381 to be the size of the common symbol. The code just above
5382 won't fix the size if a common symbol becomes larger. We
5383 don't warn about a size change here, because that is
5384 covered by --warn-common. Allow changes between different
5386 if (h
->root
.type
== bfd_link_hash_common
)
5387 h
->size
= h
->root
.u
.c
.size
;
5389 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5390 && ((definition
&& !new_weak
)
5391 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5392 || h
->type
== STT_NOTYPE
))
5394 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5396 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5398 if (type
== STT_GNU_IFUNC
5399 && (abfd
->flags
& DYNAMIC
) != 0)
5402 if (h
->type
!= type
)
5404 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5405 /* xgettext:c-format */
5407 (_("warning: type of symbol `%s' changed"
5408 " from %d to %d in %pB"),
5409 name
, h
->type
, type
, abfd
);
5415 /* Merge st_other field. */
5416 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5417 definition
, dynamic
);
5419 /* We don't want to make debug symbol dynamic. */
5421 && (sec
->flags
& SEC_DEBUGGING
)
5422 && !bfd_link_relocatable (info
))
5425 /* Nor should we make plugin symbols dynamic. */
5426 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5431 h
->target_internal
= isym
->st_target_internal
;
5432 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5435 /* Don't add indirect symbols for .symver x, x@FOO aliases
5436 in IR. Since all data or text symbols in IR have the
5437 same type, value and section, we can't tell if a symbol
5438 is an alias of another symbol by their types, values and
5442 && (abfd
->flags
& BFD_PLUGIN
) == 0)
5444 char *p
= strchr (name
, ELF_VER_CHR
);
5445 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5447 /* Queue non-default versions so that .symver x, x@FOO
5448 aliases can be checked. */
5451 size_t amt
= ((isymend
- isym
+ 1)
5452 * sizeof (struct elf_link_hash_entry
*));
5454 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5456 goto error_free_vers
;
5458 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5462 if (dynsym
&& h
->dynindx
== -1)
5464 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5465 goto error_free_vers
;
5467 && weakdef (h
)->dynindx
== -1)
5469 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5470 goto error_free_vers
;
5473 else if (h
->dynindx
!= -1)
5474 /* If the symbol already has a dynamic index, but
5475 visibility says it should not be visible, turn it into
5477 switch (ELF_ST_VISIBILITY (h
->other
))
5481 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
5489 && h
->root
.type
!= bfd_link_hash_indirect
5491 && h
->ref_regular_nonweak
)
5493 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5494 && h
->ref_ir_nonweak
5495 && !info
->lto_all_symbols_read
)
5496 || (h
->ref_dynamic_nonweak
5497 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5498 && !on_needed_list (elf_dt_name (abfd
),
5499 htab
->needed
, NULL
))))
5501 const char *soname
= elf_dt_name (abfd
);
5503 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5504 h
->root
.root
.string
);
5506 /* A symbol from a library loaded via DT_NEEDED of some
5507 other library is referenced by a regular object.
5508 Add a DT_NEEDED entry for it. Issue an error if
5509 --no-add-needed is used and the reference was not
5512 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5515 /* xgettext:c-format */
5516 (_("%pB: undefined reference to symbol '%s'"),
5518 bfd_set_error (bfd_error_missing_dso
);
5519 goto error_free_vers
;
5522 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5523 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5525 /* Create dynamic sections for backends that require
5526 that be done before setup_gnu_properties. */
5527 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5534 if (info
->lto_plugin_active
5535 && !bfd_link_relocatable (info
)
5536 && (abfd
->flags
& BFD_PLUGIN
) == 0
5542 if (bed
->s
->arch_size
== 32)
5547 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5548 referenced in regular objects so that linker plugin will get
5549 the correct symbol resolution. */
5551 sym_hash
= elf_sym_hashes (abfd
);
5552 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5554 Elf_Internal_Rela
*internal_relocs
;
5555 Elf_Internal_Rela
*rel
, *relend
;
5557 /* Don't check relocations in excluded sections. */
5558 if ((s
->flags
& SEC_RELOC
) == 0
5559 || s
->reloc_count
== 0
5560 || (s
->flags
& SEC_EXCLUDE
) != 0
5561 || ((info
->strip
== strip_all
5562 || info
->strip
== strip_debugger
)
5563 && (s
->flags
& SEC_DEBUGGING
) != 0))
5566 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
5569 _bfd_link_keep_memory (info
));
5570 if (internal_relocs
== NULL
)
5571 goto error_free_vers
;
5573 rel
= internal_relocs
;
5574 relend
= rel
+ s
->reloc_count
;
5575 for ( ; rel
< relend
; rel
++)
5577 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5578 struct elf_link_hash_entry
*h
;
5580 /* Skip local symbols. */
5581 if (r_symndx
< extsymoff
)
5584 h
= sym_hash
[r_symndx
- extsymoff
];
5586 h
->root
.non_ir_ref_regular
= 1;
5589 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5590 free (internal_relocs
);
5599 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5603 /* Restore the symbol table. */
5604 old_ent
= (char *) old_tab
+ tabsize
;
5605 memset (elf_sym_hashes (abfd
), 0,
5606 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5607 htab
->root
.table
.table
= old_table
;
5608 htab
->root
.table
.size
= old_size
;
5609 htab
->root
.table
.count
= old_count
;
5610 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5611 htab
->root
.undefs
= old_undefs
;
5612 htab
->root
.undefs_tail
= old_undefs_tail
;
5613 if (htab
->dynstr
!= NULL
)
5614 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5617 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5619 struct bfd_hash_entry
*p
;
5620 struct elf_link_hash_entry
*h
;
5621 unsigned int non_ir_ref_dynamic
;
5623 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5625 /* Preserve non_ir_ref_dynamic so that this symbol
5626 will be exported when the dynamic lib becomes needed
5627 in the second pass. */
5628 h
= (struct elf_link_hash_entry
*) p
;
5629 if (h
->root
.type
== bfd_link_hash_warning
)
5630 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5631 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5633 h
= (struct elf_link_hash_entry
*) p
;
5634 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5635 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5636 if (h
->root
.type
== bfd_link_hash_warning
)
5638 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5639 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5640 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5642 if (h
->root
.type
== bfd_link_hash_common
)
5644 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5645 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5647 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5651 /* Make a special call to the linker "notice" function to
5652 tell it that symbols added for crefs may need to be removed. */
5653 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5654 goto error_free_vers
;
5657 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5659 free (nondeflt_vers
);
5663 if (old_tab
!= NULL
)
5665 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5666 goto error_free_vers
;
5671 /* Now that all the symbols from this input file are created, if
5672 not performing a relocatable link, handle .symver foo, foo@BAR
5673 such that any relocs against foo become foo@BAR. */
5674 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5678 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5680 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5681 char *shortname
, *p
;
5684 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5686 || (h
->root
.type
!= bfd_link_hash_defined
5687 && h
->root
.type
!= bfd_link_hash_defweak
))
5690 amt
= p
- h
->root
.root
.string
;
5691 shortname
= (char *) bfd_malloc (amt
+ 1);
5693 goto error_free_vers
;
5694 memcpy (shortname
, h
->root
.root
.string
, amt
);
5695 shortname
[amt
] = '\0';
5697 hi
= (struct elf_link_hash_entry
*)
5698 bfd_link_hash_lookup (&htab
->root
, shortname
,
5699 false, false, false);
5701 && hi
->root
.type
== h
->root
.type
5702 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5703 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5705 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
5706 hi
->root
.type
= bfd_link_hash_indirect
;
5707 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5708 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5709 sym_hash
= elf_sym_hashes (abfd
);
5711 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5712 if (sym_hash
[symidx
] == hi
)
5714 sym_hash
[symidx
] = h
;
5720 free (nondeflt_vers
);
5721 nondeflt_vers
= NULL
;
5724 /* Now set the alias field correctly for all the weak defined
5725 symbols we found. The only way to do this is to search all the
5726 symbols. Since we only need the information for non functions in
5727 dynamic objects, that's the only time we actually put anything on
5728 the list WEAKS. We need this information so that if a regular
5729 object refers to a symbol defined weakly in a dynamic object, the
5730 real symbol in the dynamic object is also put in the dynamic
5731 symbols; we also must arrange for both symbols to point to the
5732 same memory location. We could handle the general case of symbol
5733 aliasing, but a general symbol alias can only be generated in
5734 assembler code, handling it correctly would be very time
5735 consuming, and other ELF linkers don't handle general aliasing
5739 struct elf_link_hash_entry
**hpp
;
5740 struct elf_link_hash_entry
**hppend
;
5741 struct elf_link_hash_entry
**sorted_sym_hash
;
5742 struct elf_link_hash_entry
*h
;
5743 size_t sym_count
, amt
;
5745 /* Since we have to search the whole symbol list for each weak
5746 defined symbol, search time for N weak defined symbols will be
5747 O(N^2). Binary search will cut it down to O(NlogN). */
5748 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5749 sorted_sym_hash
= bfd_malloc (amt
);
5750 if (sorted_sym_hash
== NULL
)
5752 sym_hash
= sorted_sym_hash
;
5753 hpp
= elf_sym_hashes (abfd
);
5754 hppend
= hpp
+ extsymcount
;
5756 for (; hpp
< hppend
; hpp
++)
5760 && h
->root
.type
== bfd_link_hash_defined
5761 && !bed
->is_function_type (h
->type
))
5769 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5772 while (weaks
!= NULL
)
5774 struct elf_link_hash_entry
*hlook
;
5777 size_t i
, j
, idx
= 0;
5780 weaks
= hlook
->u
.alias
;
5781 hlook
->u
.alias
= NULL
;
5783 if (hlook
->root
.type
!= bfd_link_hash_defined
5784 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5787 slook
= hlook
->root
.u
.def
.section
;
5788 vlook
= hlook
->root
.u
.def
.value
;
5794 bfd_signed_vma vdiff
;
5796 h
= sorted_sym_hash
[idx
];
5797 vdiff
= vlook
- h
->root
.u
.def
.value
;
5804 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5814 /* We didn't find a value/section match. */
5818 /* With multiple aliases, or when the weak symbol is already
5819 strongly defined, we have multiple matching symbols and
5820 the binary search above may land on any of them. Step
5821 one past the matching symbol(s). */
5824 h
= sorted_sym_hash
[idx
];
5825 if (h
->root
.u
.def
.section
!= slook
5826 || h
->root
.u
.def
.value
!= vlook
)
5830 /* Now look back over the aliases. Since we sorted by size
5831 as well as value and section, we'll choose the one with
5832 the largest size. */
5835 h
= sorted_sym_hash
[idx
];
5837 /* Stop if value or section doesn't match. */
5838 if (h
->root
.u
.def
.section
!= slook
5839 || h
->root
.u
.def
.value
!= vlook
)
5841 else if (h
!= hlook
)
5843 struct elf_link_hash_entry
*t
;
5846 hlook
->is_weakalias
= 1;
5848 if (t
->u
.alias
!= NULL
)
5849 while (t
->u
.alias
!= h
)
5853 /* If the weak definition is in the list of dynamic
5854 symbols, make sure the real definition is put
5856 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5858 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5861 free (sorted_sym_hash
);
5866 /* If the real definition is in the list of dynamic
5867 symbols, make sure the weak definition is put
5868 there as well. If we don't do this, then the
5869 dynamic loader might not merge the entries for the
5870 real definition and the weak definition. */
5871 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5873 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5874 goto err_free_sym_hash
;
5881 free (sorted_sym_hash
);
5884 if (bed
->check_directives
5885 && !(*bed
->check_directives
) (abfd
, info
))
5888 /* If this is a non-traditional link, try to optimize the handling
5889 of the .stab/.stabstr sections. */
5891 && ! info
->traditional_format
5892 && is_elf_hash_table (&htab
->root
)
5893 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5897 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5898 if (stabstr
!= NULL
)
5900 bfd_size_type string_offset
= 0;
5903 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5904 if (startswith (stab
->name
, ".stab")
5905 && (!stab
->name
[5] ||
5906 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5907 && (stab
->flags
& SEC_MERGE
) == 0
5908 && !bfd_is_abs_section (stab
->output_section
))
5910 struct bfd_elf_section_data
*secdata
;
5912 secdata
= elf_section_data (stab
);
5913 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5914 stabstr
, &secdata
->sec_info
,
5917 if (secdata
->sec_info
)
5918 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5923 if (dynamic
&& add_needed
)
5925 /* Add this bfd to the loaded list. */
5926 struct elf_link_loaded_list
*n
;
5928 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5932 n
->next
= htab
->dyn_loaded
;
5933 htab
->dyn_loaded
= n
;
5935 if (dynamic
&& !add_needed
5936 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5937 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5944 free (nondeflt_vers
);
5952 /* Return the linker hash table entry of a symbol that might be
5953 satisfied by an archive symbol. Return -1 on error. */
5955 struct bfd_link_hash_entry
*
5956 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5957 struct bfd_link_info
*info
,
5960 struct bfd_link_hash_entry
*h
;
5964 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
5968 /* If this is a default version (the name contains @@), look up the
5969 symbol again with only one `@' as well as without the version.
5970 The effect is that references to the symbol with and without the
5971 version will be matched by the default symbol in the archive. */
5973 p
= strchr (name
, ELF_VER_CHR
);
5974 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5977 /* First check with only one `@'. */
5978 len
= strlen (name
);
5979 copy
= (char *) bfd_alloc (abfd
, len
);
5981 return (struct bfd_link_hash_entry
*) -1;
5983 first
= p
- name
+ 1;
5984 memcpy (copy
, name
, first
);
5985 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5987 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5990 /* We also need to check references to the symbol without the
5992 copy
[first
- 1] = '\0';
5993 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5996 bfd_release (abfd
, copy
);
6000 /* Add symbols from an ELF archive file to the linker hash table. We
6001 don't use _bfd_generic_link_add_archive_symbols because we need to
6002 handle versioned symbols.
6004 Fortunately, ELF archive handling is simpler than that done by
6005 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6006 oddities. In ELF, if we find a symbol in the archive map, and the
6007 symbol is currently undefined, we know that we must pull in that
6010 Unfortunately, we do have to make multiple passes over the symbol
6011 table until nothing further is resolved. */
6014 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6017 unsigned char *included
= NULL
;
6021 const struct elf_backend_data
*bed
;
6022 struct bfd_link_hash_entry
* (*archive_symbol_lookup
)
6023 (bfd
*, struct bfd_link_info
*, const char *);
6025 if (! bfd_has_map (abfd
))
6027 /* An empty archive is a special case. */
6028 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
6030 bfd_set_error (bfd_error_no_armap
);
6034 /* Keep track of all symbols we know to be already defined, and all
6035 files we know to be already included. This is to speed up the
6036 second and subsequent passes. */
6037 c
= bfd_ardata (abfd
)->symdef_count
;
6040 amt
= c
* sizeof (*included
);
6041 included
= (unsigned char *) bfd_zmalloc (amt
);
6042 if (included
== NULL
)
6045 symdefs
= bfd_ardata (abfd
)->symdefs
;
6046 bed
= get_elf_backend_data (abfd
);
6047 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
6060 symdefend
= symdef
+ c
;
6061 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
6063 struct bfd_link_hash_entry
*h
;
6065 struct bfd_link_hash_entry
*undefs_tail
;
6070 if (symdef
->file_offset
== last
)
6076 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
6077 if (h
== (struct bfd_link_hash_entry
*) -1)
6083 if (h
->type
== bfd_link_hash_undefined
)
6085 /* If the archive element has already been loaded then one
6086 of the symbols defined by that element might have been
6087 made undefined due to being in a discarded section. */
6088 if (is_elf_hash_table (info
->hash
)
6089 && ((struct elf_link_hash_entry
*) h
)->indx
== -3)
6092 else if (h
->type
== bfd_link_hash_common
)
6094 /* We currently have a common symbol. The archive map contains
6095 a reference to this symbol, so we may want to include it. We
6096 only want to include it however, if this archive element
6097 contains a definition of the symbol, not just another common
6100 Unfortunately some archivers (including GNU ar) will put
6101 declarations of common symbols into their archive maps, as
6102 well as real definitions, so we cannot just go by the archive
6103 map alone. Instead we must read in the element's symbol
6104 table and check that to see what kind of symbol definition
6106 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
6111 if (h
->type
!= bfd_link_hash_undefweak
)
6112 /* Symbol must be defined. Don't check it again. */
6117 /* We need to include this archive member. */
6118 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
,
6120 if (element
== NULL
)
6123 if (! bfd_check_format (element
, bfd_object
))
6126 undefs_tail
= info
->hash
->undefs_tail
;
6128 if (!(*info
->callbacks
6129 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
6131 if (!bfd_link_add_symbols (element
, info
))
6134 /* If there are any new undefined symbols, we need to make
6135 another pass through the archive in order to see whether
6136 they can be defined. FIXME: This isn't perfect, because
6137 common symbols wind up on undefs_tail and because an
6138 undefined symbol which is defined later on in this pass
6139 does not require another pass. This isn't a bug, but it
6140 does make the code less efficient than it could be. */
6141 if (undefs_tail
!= info
->hash
->undefs_tail
)
6144 /* Look backward to mark all symbols from this object file
6145 which we have already seen in this pass. */
6149 included
[mark
] = true;
6154 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
6156 /* We mark subsequent symbols from this object file as we go
6157 on through the loop. */
6158 last
= symdef
->file_offset
;
6171 /* Given an ELF BFD, add symbols to the global hash table as
6175 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6177 switch (bfd_get_format (abfd
))
6180 return elf_link_add_object_symbols (abfd
, info
);
6182 return elf_link_add_archive_symbols (abfd
, info
);
6184 bfd_set_error (bfd_error_wrong_format
);
6189 struct hash_codes_info
6191 unsigned long *hashcodes
;
6195 /* This function will be called though elf_link_hash_traverse to store
6196 all hash value of the exported symbols in an array. */
6199 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6201 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
6206 /* Ignore indirect symbols. These are added by the versioning code. */
6207 if (h
->dynindx
== -1)
6210 name
= h
->root
.root
.string
;
6211 if (h
->versioned
>= versioned
)
6213 char *p
= strchr (name
, ELF_VER_CHR
);
6216 alc
= (char *) bfd_malloc (p
- name
+ 1);
6222 memcpy (alc
, name
, p
- name
);
6223 alc
[p
- name
] = '\0';
6228 /* Compute the hash value. */
6229 ha
= bfd_elf_hash (name
);
6231 /* Store the found hash value in the array given as the argument. */
6232 *(inf
->hashcodes
)++ = ha
;
6234 /* And store it in the struct so that we can put it in the hash table
6236 h
->u
.elf_hash_value
= ha
;
6242 struct collect_gnu_hash_codes
6245 const struct elf_backend_data
*bed
;
6246 unsigned long int nsyms
;
6247 unsigned long int maskbits
;
6248 unsigned long int *hashcodes
;
6249 unsigned long int *hashval
;
6250 unsigned long int *indx
;
6251 unsigned long int *counts
;
6255 long int min_dynindx
;
6256 unsigned long int bucketcount
;
6257 unsigned long int symindx
;
6258 long int local_indx
;
6259 long int shift1
, shift2
;
6260 unsigned long int mask
;
6264 /* This function will be called though elf_link_hash_traverse to store
6265 all hash value of the exported symbols in an array. */
6268 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6270 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6275 /* Ignore indirect symbols. These are added by the versioning code. */
6276 if (h
->dynindx
== -1)
6279 /* Ignore also local symbols and undefined symbols. */
6280 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6283 name
= h
->root
.root
.string
;
6284 if (h
->versioned
>= versioned
)
6286 char *p
= strchr (name
, ELF_VER_CHR
);
6289 alc
= (char *) bfd_malloc (p
- name
+ 1);
6295 memcpy (alc
, name
, p
- name
);
6296 alc
[p
- name
] = '\0';
6301 /* Compute the hash value. */
6302 ha
= bfd_elf_gnu_hash (name
);
6304 /* Store the found hash value in the array for compute_bucket_count,
6305 and also for .dynsym reordering purposes. */
6306 s
->hashcodes
[s
->nsyms
] = ha
;
6307 s
->hashval
[h
->dynindx
] = ha
;
6309 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6310 s
->min_dynindx
= h
->dynindx
;
6316 /* This function will be called though elf_link_hash_traverse to do
6317 final dynamic symbol renumbering in case of .gnu.hash.
6318 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6319 to the translation table. */
6322 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6324 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6325 unsigned long int bucket
;
6326 unsigned long int val
;
6328 /* Ignore indirect symbols. */
6329 if (h
->dynindx
== -1)
6332 /* Ignore also local symbols and undefined symbols. */
6333 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6335 if (h
->dynindx
>= s
->min_dynindx
)
6337 if (s
->bed
->record_xhash_symbol
!= NULL
)
6339 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6343 h
->dynindx
= s
->local_indx
++;
6348 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6349 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6350 & ((s
->maskbits
>> s
->shift1
) - 1);
6351 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6353 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6354 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6355 if (s
->counts
[bucket
] == 1)
6356 /* Last element terminates the chain. */
6358 bfd_put_32 (s
->output_bfd
, val
,
6359 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6360 --s
->counts
[bucket
];
6361 if (s
->bed
->record_xhash_symbol
!= NULL
)
6363 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6365 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6368 h
->dynindx
= s
->indx
[bucket
]++;
6372 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6375 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6377 return !(h
->forced_local
6378 || h
->root
.type
== bfd_link_hash_undefined
6379 || h
->root
.type
== bfd_link_hash_undefweak
6380 || ((h
->root
.type
== bfd_link_hash_defined
6381 || h
->root
.type
== bfd_link_hash_defweak
)
6382 && h
->root
.u
.def
.section
->output_section
== NULL
));
6385 /* Array used to determine the number of hash table buckets to use
6386 based on the number of symbols there are. If there are fewer than
6387 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6388 fewer than 37 we use 17 buckets, and so forth. We never use more
6389 than 32771 buckets. */
6391 static const size_t elf_buckets
[] =
6393 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6397 /* Compute bucket count for hashing table. We do not use a static set
6398 of possible tables sizes anymore. Instead we determine for all
6399 possible reasonable sizes of the table the outcome (i.e., the
6400 number of collisions etc) and choose the best solution. The
6401 weighting functions are not too simple to allow the table to grow
6402 without bounds. Instead one of the weighting factors is the size.
6403 Therefore the result is always a good payoff between few collisions
6404 (= short chain lengths) and table size. */
6406 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6407 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6408 unsigned long int nsyms
,
6411 size_t best_size
= 0;
6412 unsigned long int i
;
6418 uint64_t best_chlen
= ~((uint64_t) 0);
6419 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6420 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6421 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6422 unsigned long int *counts
;
6424 unsigned int no_improvement_count
= 0;
6426 /* Possible optimization parameters: if we have NSYMS symbols we say
6427 that the hashing table must at least have NSYMS/4 and at most
6429 minsize
= nsyms
/ 4;
6432 best_size
= maxsize
= nsyms
* 2;
6437 if ((best_size
& 31) == 0)
6441 /* Create array where we count the collisions in. We must use bfd_malloc
6442 since the size could be large. */
6444 amt
*= sizeof (unsigned long int);
6445 counts
= (unsigned long int *) bfd_malloc (amt
);
6449 /* Compute the "optimal" size for the hash table. The criteria is a
6450 minimal chain length. The minor criteria is (of course) the size
6452 for (i
= minsize
; i
< maxsize
; ++i
)
6454 /* Walk through the array of hashcodes and count the collisions. */
6456 unsigned long int j
;
6457 unsigned long int fact
;
6459 if (gnu_hash
&& (i
& 31) == 0)
6462 memset (counts
, '\0', i
* sizeof (unsigned long int));
6464 /* Determine how often each hash bucket is used. */
6465 for (j
= 0; j
< nsyms
; ++j
)
6466 ++counts
[hashcodes
[j
] % i
];
6468 /* For the weight function we need some information about the
6469 pagesize on the target. This is information need not be 100%
6470 accurate. Since this information is not available (so far) we
6471 define it here to a reasonable default value. If it is crucial
6472 to have a better value some day simply define this value. */
6473 # ifndef BFD_TARGET_PAGESIZE
6474 # define BFD_TARGET_PAGESIZE (4096)
6477 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6479 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6482 /* Variant 1: optimize for short chains. We add the squares
6483 of all the chain lengths (which favors many small chain
6484 over a few long chains). */
6485 for (j
= 0; j
< i
; ++j
)
6486 max
+= counts
[j
] * counts
[j
];
6488 /* This adds penalties for the overall size of the table. */
6489 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6492 /* Variant 2: Optimize a lot more for small table. Here we
6493 also add squares of the size but we also add penalties for
6494 empty slots (the +1 term). */
6495 for (j
= 0; j
< i
; ++j
)
6496 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6498 /* The overall size of the table is considered, but not as
6499 strong as in variant 1, where it is squared. */
6500 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6504 /* Compare with current best results. */
6505 if (max
< best_chlen
)
6509 no_improvement_count
= 0;
6511 /* PR 11843: Avoid futile long searches for the best bucket size
6512 when there are a large number of symbols. */
6513 else if (++no_improvement_count
== 100)
6521 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6523 best_size
= elf_buckets
[i
];
6524 if (nsyms
< elf_buckets
[i
+ 1])
6527 if (gnu_hash
&& best_size
< 2)
6534 /* Size any SHT_GROUP section for ld -r. */
6537 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6542 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6543 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6544 && (s
= ibfd
->sections
) != NULL
6545 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6546 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6551 /* Set a default stack segment size. The value in INFO wins. If it
6552 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6553 undefined it is initialized. */
6556 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6557 struct bfd_link_info
*info
,
6558 const char *legacy_symbol
,
6559 bfd_vma default_size
)
6561 struct elf_link_hash_entry
*h
= NULL
;
6563 /* Look for legacy symbol. */
6565 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6566 false, false, false);
6567 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6568 || h
->root
.type
== bfd_link_hash_defweak
)
6570 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6572 /* The symbol has no type if specified on the command line. */
6573 h
->type
= STT_OBJECT
;
6574 if (info
->stacksize
)
6575 /* xgettext:c-format */
6576 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6577 output_bfd
, legacy_symbol
);
6578 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6579 /* xgettext:c-format */
6580 _bfd_error_handler (_("%pB: %s not absolute"),
6581 output_bfd
, legacy_symbol
);
6583 info
->stacksize
= h
->root
.u
.def
.value
;
6586 if (!info
->stacksize
)
6587 /* If the user didn't set a size, or explicitly inhibit the
6588 size, set it now. */
6589 info
->stacksize
= default_size
;
6591 /* Provide the legacy symbol, if it is referenced. */
6592 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6593 || h
->root
.type
== bfd_link_hash_undefweak
))
6595 struct bfd_link_hash_entry
*bh
= NULL
;
6597 if (!(_bfd_generic_link_add_one_symbol
6598 (info
, output_bfd
, legacy_symbol
,
6599 BSF_GLOBAL
, bfd_abs_section_ptr
,
6600 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6601 NULL
, false, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6604 h
= (struct elf_link_hash_entry
*) bh
;
6606 h
->type
= STT_OBJECT
;
6612 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6614 struct elf_gc_sweep_symbol_info
6616 struct bfd_link_info
*info
;
6617 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6622 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6625 && (((h
->root
.type
== bfd_link_hash_defined
6626 || h
->root
.type
== bfd_link_hash_defweak
)
6627 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6628 && h
->root
.u
.def
.section
->gc_mark
))
6629 || h
->root
.type
== bfd_link_hash_undefined
6630 || h
->root
.type
== bfd_link_hash_undefweak
))
6632 struct elf_gc_sweep_symbol_info
*inf
;
6634 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6635 (*inf
->hide_symbol
) (inf
->info
, h
, true);
6638 h
->ref_regular_nonweak
= 0;
6644 /* Set up the sizes and contents of the ELF dynamic sections. This is
6645 called by the ELF linker emulation before_allocation routine. We
6646 must set the sizes of the sections before the linker sets the
6647 addresses of the various sections. */
6650 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6653 const char *filter_shlib
,
6655 const char *depaudit
,
6656 const char * const *auxiliary_filters
,
6657 struct bfd_link_info
*info
,
6658 asection
**sinterpptr
)
6661 const struct elf_backend_data
*bed
;
6665 if (!is_elf_hash_table (info
->hash
))
6668 /* Any syms created from now on start with -1 in
6669 got.refcount/offset and plt.refcount/offset. */
6670 elf_hash_table (info
)->init_got_refcount
6671 = elf_hash_table (info
)->init_got_offset
;
6672 elf_hash_table (info
)->init_plt_refcount
6673 = elf_hash_table (info
)->init_plt_offset
;
6675 bed
= get_elf_backend_data (output_bfd
);
6677 /* The backend may have to create some sections regardless of whether
6678 we're dynamic or not. */
6679 if (bed
->elf_backend_always_size_sections
6680 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6683 dynobj
= elf_hash_table (info
)->dynobj
;
6685 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6687 struct bfd_elf_version_tree
*verdefs
;
6688 struct elf_info_failed asvinfo
;
6689 struct bfd_elf_version_tree
*t
;
6690 struct bfd_elf_version_expr
*d
;
6694 /* If we are supposed to export all symbols into the dynamic symbol
6695 table (this is not the normal case), then do so. */
6696 if (info
->export_dynamic
6697 || (bfd_link_executable (info
) && info
->dynamic
))
6699 struct elf_info_failed eif
;
6703 elf_link_hash_traverse (elf_hash_table (info
),
6704 _bfd_elf_export_symbol
,
6712 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6714 if (soname_indx
== (size_t) -1
6715 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6719 soname_indx
= (size_t) -1;
6721 /* Make all global versions with definition. */
6722 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6723 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6724 if (!d
->symver
&& d
->literal
)
6726 const char *verstr
, *name
;
6727 size_t namelen
, verlen
, newlen
;
6728 char *newname
, *p
, leading_char
;
6729 struct elf_link_hash_entry
*newh
;
6731 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6733 namelen
= strlen (name
) + (leading_char
!= '\0');
6735 verlen
= strlen (verstr
);
6736 newlen
= namelen
+ verlen
+ 3;
6738 newname
= (char *) bfd_malloc (newlen
);
6739 if (newname
== NULL
)
6741 newname
[0] = leading_char
;
6742 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6744 /* Check the hidden versioned definition. */
6745 p
= newname
+ namelen
;
6747 memcpy (p
, verstr
, verlen
+ 1);
6748 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6749 newname
, false, false,
6752 || (newh
->root
.type
!= bfd_link_hash_defined
6753 && newh
->root
.type
!= bfd_link_hash_defweak
))
6755 /* Check the default versioned definition. */
6757 memcpy (p
, verstr
, verlen
+ 1);
6758 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6759 newname
, false, false,
6764 /* Mark this version if there is a definition and it is
6765 not defined in a shared object. */
6767 && !newh
->def_dynamic
6768 && (newh
->root
.type
== bfd_link_hash_defined
6769 || newh
->root
.type
== bfd_link_hash_defweak
))
6773 /* Attach all the symbols to their version information. */
6774 asvinfo
.info
= info
;
6775 asvinfo
.failed
= false;
6777 elf_link_hash_traverse (elf_hash_table (info
),
6778 _bfd_elf_link_assign_sym_version
,
6783 if (!info
->allow_undefined_version
)
6785 /* Check if all global versions have a definition. */
6786 bool all_defined
= true;
6787 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6788 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6789 if (d
->literal
&& !d
->symver
&& !d
->script
)
6792 (_("%s: undefined version: %s"),
6793 d
->pattern
, t
->name
);
6794 all_defined
= false;
6799 bfd_set_error (bfd_error_bad_value
);
6804 /* Set up the version definition section. */
6805 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6806 BFD_ASSERT (s
!= NULL
);
6808 /* We may have created additional version definitions if we are
6809 just linking a regular application. */
6810 verdefs
= info
->version_info
;
6812 /* Skip anonymous version tag. */
6813 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6814 verdefs
= verdefs
->next
;
6816 if (verdefs
== NULL
&& !info
->create_default_symver
)
6817 s
->flags
|= SEC_EXCLUDE
;
6823 Elf_Internal_Verdef def
;
6824 Elf_Internal_Verdaux defaux
;
6825 struct bfd_link_hash_entry
*bh
;
6826 struct elf_link_hash_entry
*h
;
6832 /* Make space for the base version. */
6833 size
+= sizeof (Elf_External_Verdef
);
6834 size
+= sizeof (Elf_External_Verdaux
);
6837 /* Make space for the default version. */
6838 if (info
->create_default_symver
)
6840 size
+= sizeof (Elf_External_Verdef
);
6844 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6846 struct bfd_elf_version_deps
*n
;
6848 /* Don't emit base version twice. */
6852 size
+= sizeof (Elf_External_Verdef
);
6853 size
+= sizeof (Elf_External_Verdaux
);
6856 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6857 size
+= sizeof (Elf_External_Verdaux
);
6861 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6862 if (s
->contents
== NULL
&& s
->size
!= 0)
6865 /* Fill in the version definition section. */
6869 def
.vd_version
= VER_DEF_CURRENT
;
6870 def
.vd_flags
= VER_FLG_BASE
;
6873 if (info
->create_default_symver
)
6875 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6876 def
.vd_next
= sizeof (Elf_External_Verdef
);
6880 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6881 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6882 + sizeof (Elf_External_Verdaux
));
6885 if (soname_indx
!= (size_t) -1)
6887 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6889 def
.vd_hash
= bfd_elf_hash (soname
);
6890 defaux
.vda_name
= soname_indx
;
6897 name
= lbasename (bfd_get_filename (output_bfd
));
6898 def
.vd_hash
= bfd_elf_hash (name
);
6899 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6901 if (indx
== (size_t) -1)
6903 defaux
.vda_name
= indx
;
6905 defaux
.vda_next
= 0;
6907 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6908 (Elf_External_Verdef
*) p
);
6909 p
+= sizeof (Elf_External_Verdef
);
6910 if (info
->create_default_symver
)
6912 /* Add a symbol representing this version. */
6914 if (! (_bfd_generic_link_add_one_symbol
6915 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6917 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6919 h
= (struct elf_link_hash_entry
*) bh
;
6922 h
->type
= STT_OBJECT
;
6923 h
->verinfo
.vertree
= NULL
;
6925 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6928 /* Create a duplicate of the base version with the same
6929 aux block, but different flags. */
6932 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6934 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6935 + sizeof (Elf_External_Verdaux
));
6938 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6939 (Elf_External_Verdef
*) p
);
6940 p
+= sizeof (Elf_External_Verdef
);
6942 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6943 (Elf_External_Verdaux
*) p
);
6944 p
+= sizeof (Elf_External_Verdaux
);
6946 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6949 struct bfd_elf_version_deps
*n
;
6951 /* Don't emit the base version twice. */
6956 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6959 /* Add a symbol representing this version. */
6961 if (! (_bfd_generic_link_add_one_symbol
6962 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6964 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6966 h
= (struct elf_link_hash_entry
*) bh
;
6969 h
->type
= STT_OBJECT
;
6970 h
->verinfo
.vertree
= t
;
6972 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6975 def
.vd_version
= VER_DEF_CURRENT
;
6977 if (t
->globals
.list
== NULL
6978 && t
->locals
.list
== NULL
6980 def
.vd_flags
|= VER_FLG_WEAK
;
6981 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6982 def
.vd_cnt
= cdeps
+ 1;
6983 def
.vd_hash
= bfd_elf_hash (t
->name
);
6984 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6987 /* If a basever node is next, it *must* be the last node in
6988 the chain, otherwise Verdef construction breaks. */
6989 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6990 BFD_ASSERT (t
->next
->next
== NULL
);
6992 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6993 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6994 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6996 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6997 (Elf_External_Verdef
*) p
);
6998 p
+= sizeof (Elf_External_Verdef
);
7000 defaux
.vda_name
= h
->dynstr_index
;
7001 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
7003 defaux
.vda_next
= 0;
7004 if (t
->deps
!= NULL
)
7005 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
7006 t
->name_indx
= defaux
.vda_name
;
7008 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7009 (Elf_External_Verdaux
*) p
);
7010 p
+= sizeof (Elf_External_Verdaux
);
7012 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
7014 if (n
->version_needed
== NULL
)
7016 /* This can happen if there was an error in the
7018 defaux
.vda_name
= 0;
7022 defaux
.vda_name
= n
->version_needed
->name_indx
;
7023 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
7026 if (n
->next
== NULL
)
7027 defaux
.vda_next
= 0;
7029 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
7031 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7032 (Elf_External_Verdaux
*) p
);
7033 p
+= sizeof (Elf_External_Verdaux
);
7037 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
7041 if (info
->gc_sections
&& bed
->can_gc_sections
)
7043 struct elf_gc_sweep_symbol_info sweep_info
;
7045 /* Remove the symbols that were in the swept sections from the
7046 dynamic symbol table. */
7047 sweep_info
.info
= info
;
7048 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
7049 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
7053 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7056 struct elf_find_verdep_info sinfo
;
7058 /* Work out the size of the version reference section. */
7060 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
7061 BFD_ASSERT (s
!= NULL
);
7064 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
7065 if (sinfo
.vers
== 0)
7067 sinfo
.failed
= false;
7069 elf_link_hash_traverse (elf_hash_table (info
),
7070 _bfd_elf_link_find_version_dependencies
,
7075 bed
->elf_backend_add_glibc_version_dependency (&sinfo
);
7079 if (elf_tdata (output_bfd
)->verref
== NULL
)
7080 s
->flags
|= SEC_EXCLUDE
;
7083 Elf_Internal_Verneed
*vn
;
7088 /* Build the version dependency section. */
7091 for (vn
= elf_tdata (output_bfd
)->verref
;
7093 vn
= vn
->vn_nextref
)
7095 Elf_Internal_Vernaux
*a
;
7097 size
+= sizeof (Elf_External_Verneed
);
7099 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7100 size
+= sizeof (Elf_External_Vernaux
);
7104 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7105 if (s
->contents
== NULL
)
7109 for (vn
= elf_tdata (output_bfd
)->verref
;
7111 vn
= vn
->vn_nextref
)
7114 Elf_Internal_Vernaux
*a
;
7118 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7121 vn
->vn_version
= VER_NEED_CURRENT
;
7123 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7124 elf_dt_name (vn
->vn_bfd
) != NULL
7125 ? elf_dt_name (vn
->vn_bfd
)
7126 : lbasename (bfd_get_filename
7129 if (indx
== (size_t) -1)
7132 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
7133 if (vn
->vn_nextref
== NULL
)
7136 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
7137 + caux
* sizeof (Elf_External_Vernaux
));
7139 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
7140 (Elf_External_Verneed
*) p
);
7141 p
+= sizeof (Elf_External_Verneed
);
7143 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7145 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
7146 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7147 a
->vna_nodename
, false);
7148 if (indx
== (size_t) -1)
7151 if (a
->vna_nextptr
== NULL
)
7154 a
->vna_next
= sizeof (Elf_External_Vernaux
);
7156 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
7157 (Elf_External_Vernaux
*) p
);
7158 p
+= sizeof (Elf_External_Vernaux
);
7162 elf_tdata (output_bfd
)->cverrefs
= crefs
;
7166 if (bfd_link_relocatable (info
)
7167 && !_bfd_elf_size_group_sections (info
))
7170 /* Determine any GNU_STACK segment requirements, after the backend
7171 has had a chance to set a default segment size. */
7172 if (info
->execstack
)
7174 /* If the user has explicitly requested warnings, then generate one even
7175 though the choice is the result of another command line option. */
7176 if (info
->warn_execstack
== 1)
7178 if (info
->error_execstack
)
7182 error: creating an executable stack because of -z execstack command line option"));
7188 warning: enabling an executable stack because of -z execstack command line option"));
7191 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
7193 else if (info
->noexecstack
)
7194 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
7198 asection
*notesec
= NULL
;
7199 bfd
*noteobj
= NULL
;
7200 bfd
*emptyobj
= NULL
;
7203 for (inputobj
= info
->input_bfds
;
7205 inputobj
= inputobj
->link
.next
)
7210 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
7212 s
= inputobj
->sections
;
7213 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
7216 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
7220 if (s
->flags
& SEC_CODE
)
7224 /* There is no point in scanning the remaining bfds. */
7228 else if (bed
->default_execstack
&& info
->default_execstack
)
7231 emptyobj
= inputobj
;
7235 if (notesec
|| info
->stacksize
> 0)
7239 if (info
->warn_execstack
!= 0)
7241 /* PR 29072: Because an executable stack is a serious
7242 security risk, make sure that the user knows that it is
7243 being enabled despite the fact that it was not requested
7244 on the command line. */
7247 if (info
->error_execstack
)
7249 _bfd_error_handler (_("\
7250 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7251 bfd_get_filename (noteobj
));
7255 _bfd_error_handler (_("\
7256 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7257 bfd_get_filename (noteobj
));
7261 if (info
->error_execstack
)
7263 _bfd_error_handler (_("\
7264 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7265 bfd_get_filename (emptyobj
));
7269 _bfd_error_handler (_("\
7270 warning: %s: missing .note.GNU-stack section implies executable stack"),
7271 bfd_get_filename (emptyobj
));
7272 _bfd_error_handler (_("\
7273 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7277 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
7280 if (notesec
&& exec
&& bfd_link_relocatable (info
)
7281 && notesec
->output_section
!= bfd_abs_section_ptr
)
7282 notesec
->output_section
->flags
|= SEC_CODE
;
7285 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7287 struct elf_info_failed eif
;
7288 struct elf_link_hash_entry
*h
;
7292 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7293 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7297 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7299 info
->flags
|= DF_SYMBOLIC
;
7307 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7309 if (indx
== (size_t) -1)
7312 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7313 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7317 if (filter_shlib
!= NULL
)
7321 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7322 filter_shlib
, true);
7323 if (indx
== (size_t) -1
7324 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7328 if (auxiliary_filters
!= NULL
)
7330 const char * const *p
;
7332 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7336 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7338 if (indx
== (size_t) -1
7339 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7348 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7350 if (indx
== (size_t) -1
7351 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7355 if (depaudit
!= NULL
)
7359 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7361 if (indx
== (size_t) -1
7362 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7369 /* Find all symbols which were defined in a dynamic object and make
7370 the backend pick a reasonable value for them. */
7371 elf_link_hash_traverse (elf_hash_table (info
),
7372 _bfd_elf_adjust_dynamic_symbol
,
7377 /* Add some entries to the .dynamic section. We fill in some of the
7378 values later, in bfd_elf_final_link, but we must add the entries
7379 now so that we know the final size of the .dynamic section. */
7381 /* If there are initialization and/or finalization functions to
7382 call then add the corresponding DT_INIT/DT_FINI entries. */
7383 h
= (info
->init_function
7384 ? elf_link_hash_lookup (elf_hash_table (info
),
7385 info
->init_function
, false,
7392 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7395 h
= (info
->fini_function
7396 ? elf_link_hash_lookup (elf_hash_table (info
),
7397 info
->fini_function
, false,
7404 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7408 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7409 if (s
!= NULL
&& s
->linker_has_input
)
7411 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7412 if (! bfd_link_executable (info
))
7417 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7418 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7419 && (o
= sub
->sections
) != NULL
7420 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7421 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7422 if (elf_section_data (o
)->this_hdr
.sh_type
7423 == SHT_PREINIT_ARRAY
)
7426 (_("%pB: .preinit_array section is not allowed in DSO"),
7431 bfd_set_error (bfd_error_nonrepresentable_section
);
7435 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7436 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7439 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7440 if (s
!= NULL
&& s
->linker_has_input
)
7442 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7443 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7446 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7447 if (s
!= NULL
&& s
->linker_has_input
)
7449 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7450 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7454 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7455 /* If .dynstr is excluded from the link, we don't want any of
7456 these tags. Strictly, we should be checking each section
7457 individually; This quick check covers for the case where
7458 someone does a /DISCARD/ : { *(*) }. */
7459 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7461 bfd_size_type strsize
;
7463 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7464 if ((info
->emit_hash
7465 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7466 || (info
->emit_gnu_hash
7467 && (bed
->record_xhash_symbol
== NULL
7468 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7469 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7470 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7471 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7472 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7474 || (info
->gnu_flags_1
7475 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_FLAGS_1
,
7476 info
->gnu_flags_1
)))
7481 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7484 /* The backend must work out the sizes of all the other dynamic
7487 && bed
->elf_backend_size_dynamic_sections
!= NULL
7488 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7491 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7493 if (elf_tdata (output_bfd
)->cverdefs
)
7495 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7497 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7498 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7502 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7504 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7507 else if (info
->flags
& DF_BIND_NOW
)
7509 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7515 if (bfd_link_executable (info
))
7516 info
->flags_1
&= ~ (DF_1_INITFIRST
7519 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7523 if (elf_tdata (output_bfd
)->cverrefs
)
7525 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7527 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7528 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7532 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7533 && elf_tdata (output_bfd
)->cverdefs
== 0)
7534 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7538 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7539 s
->flags
|= SEC_EXCLUDE
;
7545 /* Find the first non-excluded output section. We'll use its
7546 section symbol for some emitted relocs. */
7548 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7551 asection
*found
= NULL
;
7553 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7554 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7555 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7558 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7561 elf_hash_table (info
)->text_index_section
= found
;
7564 /* Find two non-excluded output sections, one for code, one for data.
7565 We'll use their section symbols for some emitted relocs. */
7567 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7570 asection
*found
= NULL
;
7572 /* Data first, since setting text_index_section changes
7573 _bfd_elf_omit_section_dynsym_default. */
7574 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7575 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7576 && !(s
->flags
& SEC_READONLY
)
7577 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7580 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7583 elf_hash_table (info
)->data_index_section
= found
;
7585 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7586 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7587 && (s
->flags
& SEC_READONLY
)
7588 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7593 elf_hash_table (info
)->text_index_section
= found
;
7596 #define GNU_HASH_SECTION_NAME(bed) \
7597 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7600 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7602 const struct elf_backend_data
*bed
;
7603 unsigned long section_sym_count
;
7604 bfd_size_type dynsymcount
= 0;
7606 if (!is_elf_hash_table (info
->hash
))
7609 bed
= get_elf_backend_data (output_bfd
);
7610 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7612 /* Assign dynsym indices. In a shared library we generate a section
7613 symbol for each output section, which come first. Next come all
7614 of the back-end allocated local dynamic syms, followed by the rest
7615 of the global symbols.
7617 This is usually not needed for static binaries, however backends
7618 can request to always do it, e.g. the MIPS backend uses dynamic
7619 symbol counts to lay out GOT, which will be produced in the
7620 presence of GOT relocations even in static binaries (holding fixed
7621 data in that case, to satisfy those relocations). */
7623 if (elf_hash_table (info
)->dynamic_sections_created
7624 || bed
->always_renumber_dynsyms
)
7625 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7626 §ion_sym_count
);
7628 if (elf_hash_table (info
)->dynamic_sections_created
)
7632 unsigned int dtagcount
;
7634 dynobj
= elf_hash_table (info
)->dynobj
;
7636 /* Work out the size of the symbol version section. */
7637 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7638 BFD_ASSERT (s
!= NULL
);
7639 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7641 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7642 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7643 if (s
->contents
== NULL
)
7646 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7650 /* Set the size of the .dynsym and .hash sections. We counted
7651 the number of dynamic symbols in elf_link_add_object_symbols.
7652 We will build the contents of .dynsym and .hash when we build
7653 the final symbol table, because until then we do not know the
7654 correct value to give the symbols. We built the .dynstr
7655 section as we went along in elf_link_add_object_symbols. */
7656 s
= elf_hash_table (info
)->dynsym
;
7657 BFD_ASSERT (s
!= NULL
);
7658 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7660 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7661 if (s
->contents
== NULL
)
7664 /* The first entry in .dynsym is a dummy symbol. Clear all the
7665 section syms, in case we don't output them all. */
7666 ++section_sym_count
;
7667 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7669 elf_hash_table (info
)->bucketcount
= 0;
7671 /* Compute the size of the hashing table. As a side effect this
7672 computes the hash values for all the names we export. */
7673 if (info
->emit_hash
)
7675 unsigned long int *hashcodes
;
7676 struct hash_codes_info hashinf
;
7678 unsigned long int nsyms
;
7680 size_t hash_entry_size
;
7682 /* Compute the hash values for all exported symbols. At the same
7683 time store the values in an array so that we could use them for
7685 amt
= dynsymcount
* sizeof (unsigned long int);
7686 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7687 if (hashcodes
== NULL
)
7689 hashinf
.hashcodes
= hashcodes
;
7690 hashinf
.error
= false;
7692 /* Put all hash values in HASHCODES. */
7693 elf_link_hash_traverse (elf_hash_table (info
),
7694 elf_collect_hash_codes
, &hashinf
);
7701 nsyms
= hashinf
.hashcodes
- hashcodes
;
7703 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7706 if (bucketcount
== 0 && nsyms
> 0)
7709 elf_hash_table (info
)->bucketcount
= bucketcount
;
7711 s
= bfd_get_linker_section (dynobj
, ".hash");
7712 BFD_ASSERT (s
!= NULL
);
7713 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7714 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7715 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7716 if (s
->contents
== NULL
)
7719 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7720 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7721 s
->contents
+ hash_entry_size
);
7724 if (info
->emit_gnu_hash
)
7727 unsigned char *contents
;
7728 struct collect_gnu_hash_codes cinfo
;
7732 memset (&cinfo
, 0, sizeof (cinfo
));
7734 /* Compute the hash values for all exported symbols. At the same
7735 time store the values in an array so that we could use them for
7737 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7738 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7739 if (cinfo
.hashcodes
== NULL
)
7742 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7743 cinfo
.min_dynindx
= -1;
7744 cinfo
.output_bfd
= output_bfd
;
7747 /* Put all hash values in HASHCODES. */
7748 elf_link_hash_traverse (elf_hash_table (info
),
7749 elf_collect_gnu_hash_codes
, &cinfo
);
7752 free (cinfo
.hashcodes
);
7757 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7759 if (bucketcount
== 0)
7761 free (cinfo
.hashcodes
);
7765 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7766 BFD_ASSERT (s
!= NULL
);
7768 if (cinfo
.nsyms
== 0)
7770 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7771 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7772 free (cinfo
.hashcodes
);
7773 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7774 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7775 if (contents
== NULL
)
7777 s
->contents
= contents
;
7778 /* 1 empty bucket. */
7779 bfd_put_32 (output_bfd
, 1, contents
);
7780 /* SYMIDX above the special symbol 0. */
7781 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7782 /* Just one word for bitmask. */
7783 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7784 /* Only hash fn bloom filter. */
7785 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7786 /* No hashes are valid - empty bitmask. */
7787 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7788 /* No hashes in the only bucket. */
7789 bfd_put_32 (output_bfd
, 0,
7790 contents
+ 16 + bed
->s
->arch_size
/ 8);
7794 unsigned long int maskwords
, maskbitslog2
, x
;
7795 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7799 while ((x
>>= 1) != 0)
7801 if (maskbitslog2
< 3)
7803 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7804 maskbitslog2
= maskbitslog2
+ 3;
7806 maskbitslog2
= maskbitslog2
+ 2;
7807 if (bed
->s
->arch_size
== 64)
7809 if (maskbitslog2
== 5)
7815 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7816 cinfo
.shift2
= maskbitslog2
;
7817 cinfo
.maskbits
= 1 << maskbitslog2
;
7818 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7819 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7820 amt
+= maskwords
* sizeof (bfd_vma
);
7821 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7822 if (cinfo
.bitmask
== NULL
)
7824 free (cinfo
.hashcodes
);
7828 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7829 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7830 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7831 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7833 /* Determine how often each hash bucket is used. */
7834 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7835 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7836 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7838 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7839 if (cinfo
.counts
[i
] != 0)
7841 cinfo
.indx
[i
] = cnt
;
7842 cnt
+= cinfo
.counts
[i
];
7844 BFD_ASSERT (cnt
== dynsymcount
);
7845 cinfo
.bucketcount
= bucketcount
;
7846 cinfo
.local_indx
= cinfo
.min_dynindx
;
7848 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7849 s
->size
+= cinfo
.maskbits
/ 8;
7850 if (bed
->record_xhash_symbol
!= NULL
)
7851 s
->size
+= cinfo
.nsyms
* 4;
7852 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7853 if (contents
== NULL
)
7855 free (cinfo
.bitmask
);
7856 free (cinfo
.hashcodes
);
7860 s
->contents
= contents
;
7861 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7862 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7863 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7864 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7865 contents
+= 16 + cinfo
.maskbits
/ 8;
7867 for (i
= 0; i
< bucketcount
; ++i
)
7869 if (cinfo
.counts
[i
] == 0)
7870 bfd_put_32 (output_bfd
, 0, contents
);
7872 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7876 cinfo
.contents
= contents
;
7878 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7879 /* Renumber dynamic symbols, if populating .gnu.hash section.
7880 If using .MIPS.xhash, populate the translation table. */
7881 elf_link_hash_traverse (elf_hash_table (info
),
7882 elf_gnu_hash_process_symidx
, &cinfo
);
7884 contents
= s
->contents
+ 16;
7885 for (i
= 0; i
< maskwords
; ++i
)
7887 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7889 contents
+= bed
->s
->arch_size
/ 8;
7892 free (cinfo
.bitmask
);
7893 free (cinfo
.hashcodes
);
7897 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7898 BFD_ASSERT (s
!= NULL
);
7900 elf_finalize_dynstr (output_bfd
, info
);
7902 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7904 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7905 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7912 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7915 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7918 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7919 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7922 /* Finish SHF_MERGE section merging. */
7925 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7930 if (!is_elf_hash_table (info
->hash
))
7933 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7934 if ((ibfd
->flags
& DYNAMIC
) == 0
7935 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7936 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7937 == get_elf_backend_data (obfd
)->s
->elfclass
))
7938 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7939 if ((sec
->flags
& SEC_MERGE
) != 0
7940 && !bfd_is_abs_section (sec
->output_section
))
7942 struct bfd_elf_section_data
*secdata
;
7944 secdata
= elf_section_data (sec
);
7945 if (! _bfd_add_merge_section (obfd
,
7946 &elf_hash_table (info
)->merge_info
,
7947 sec
, &secdata
->sec_info
))
7949 else if (secdata
->sec_info
)
7950 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7953 if (elf_hash_table (info
)->merge_info
!= NULL
)
7954 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7955 merge_sections_remove_hook
);
7959 /* Create an entry in an ELF linker hash table. */
7961 struct bfd_hash_entry
*
7962 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7963 struct bfd_hash_table
*table
,
7966 /* Allocate the structure if it has not already been allocated by a
7970 entry
= (struct bfd_hash_entry
*)
7971 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7976 /* Call the allocation method of the superclass. */
7977 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7980 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7981 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7983 /* Set local fields. */
7986 ret
->got
= htab
->init_got_refcount
;
7987 ret
->plt
= htab
->init_plt_refcount
;
7988 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7989 - offsetof (struct elf_link_hash_entry
, size
)));
7990 /* Assume that we have been called by a non-ELF symbol reader.
7991 This flag is then reset by the code which reads an ELF input
7992 file. This ensures that a symbol created by a non-ELF symbol
7993 reader will have the flag set correctly. */
8000 /* Copy data from an indirect symbol to its direct symbol, hiding the
8001 old indirect symbol. Also used for copying flags to a weakdef. */
8004 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
8005 struct elf_link_hash_entry
*dir
,
8006 struct elf_link_hash_entry
*ind
)
8008 struct elf_link_hash_table
*htab
;
8010 if (ind
->dyn_relocs
!= NULL
)
8012 if (dir
->dyn_relocs
!= NULL
)
8014 struct elf_dyn_relocs
**pp
;
8015 struct elf_dyn_relocs
*p
;
8017 /* Add reloc counts against the indirect sym to the direct sym
8018 list. Merge any entries against the same section. */
8019 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
8021 struct elf_dyn_relocs
*q
;
8023 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
8024 if (q
->sec
== p
->sec
)
8026 q
->pc_count
+= p
->pc_count
;
8027 q
->count
+= p
->count
;
8034 *pp
= dir
->dyn_relocs
;
8037 dir
->dyn_relocs
= ind
->dyn_relocs
;
8038 ind
->dyn_relocs
= NULL
;
8041 /* Copy down any references that we may have already seen to the
8042 symbol which just became indirect. */
8044 if (dir
->versioned
!= versioned_hidden
)
8045 dir
->ref_dynamic
|= ind
->ref_dynamic
;
8046 dir
->ref_regular
|= ind
->ref_regular
;
8047 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
8048 dir
->non_got_ref
|= ind
->non_got_ref
;
8049 dir
->needs_plt
|= ind
->needs_plt
;
8050 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
8052 if (ind
->root
.type
!= bfd_link_hash_indirect
)
8055 /* Copy over the global and procedure linkage table refcount entries.
8056 These may have been already set up by a check_relocs routine. */
8057 htab
= elf_hash_table (info
);
8058 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
8060 if (dir
->got
.refcount
< 0)
8061 dir
->got
.refcount
= 0;
8062 dir
->got
.refcount
+= ind
->got
.refcount
;
8063 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
8066 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
8068 if (dir
->plt
.refcount
< 0)
8069 dir
->plt
.refcount
= 0;
8070 dir
->plt
.refcount
+= ind
->plt
.refcount
;
8071 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
8074 if (ind
->dynindx
!= -1)
8076 if (dir
->dynindx
!= -1)
8077 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
8078 dir
->dynindx
= ind
->dynindx
;
8079 dir
->dynstr_index
= ind
->dynstr_index
;
8081 ind
->dynstr_index
= 0;
8086 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
8087 struct elf_link_hash_entry
*h
,
8090 /* STT_GNU_IFUNC symbol must go through PLT. */
8091 if (h
->type
!= STT_GNU_IFUNC
)
8093 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
8098 h
->forced_local
= 1;
8099 if (h
->dynindx
!= -1)
8101 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
8104 h
->dynstr_index
= 0;
8109 /* Hide a symbol. */
8112 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
8113 struct bfd_link_info
*info
,
8114 struct bfd_link_hash_entry
*h
)
8116 if (is_elf_hash_table (info
->hash
))
8118 const struct elf_backend_data
*bed
8119 = get_elf_backend_data (output_bfd
);
8120 struct elf_link_hash_entry
*eh
8121 = (struct elf_link_hash_entry
*) h
;
8122 bed
->elf_backend_hide_symbol (info
, eh
, true);
8123 eh
->def_dynamic
= 0;
8124 eh
->ref_dynamic
= 0;
8125 eh
->dynamic_def
= 0;
8129 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8133 _bfd_elf_link_hash_table_init
8134 (struct elf_link_hash_table
*table
,
8136 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
8137 struct bfd_hash_table
*,
8139 unsigned int entsize
,
8140 enum elf_target_id target_id
)
8143 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
8145 table
->init_got_refcount
.refcount
= can_refcount
- 1;
8146 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
8147 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
8148 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
8149 /* The first dynamic symbol is a dummy. */
8150 table
->dynsymcount
= 1;
8152 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
8154 table
->root
.type
= bfd_link_elf_hash_table
;
8155 table
->hash_table_id
= target_id
;
8156 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
8161 /* Create an ELF linker hash table. */
8163 struct bfd_link_hash_table
*
8164 _bfd_elf_link_hash_table_create (bfd
*abfd
)
8166 struct elf_link_hash_table
*ret
;
8167 size_t amt
= sizeof (struct elf_link_hash_table
);
8169 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
8173 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
8174 sizeof (struct elf_link_hash_entry
),
8180 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
8185 /* Destroy an ELF linker hash table. */
8188 _bfd_elf_link_hash_table_free (bfd
*obfd
)
8190 struct elf_link_hash_table
*htab
;
8192 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
8193 if (htab
->dynstr
!= NULL
)
8194 _bfd_elf_strtab_free (htab
->dynstr
);
8195 _bfd_merge_sections_free (htab
->merge_info
);
8196 _bfd_generic_link_hash_table_free (obfd
);
8199 /* This is a hook for the ELF emulation code in the generic linker to
8200 tell the backend linker what file name to use for the DT_NEEDED
8201 entry for a dynamic object. */
8204 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
8206 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8207 && bfd_get_format (abfd
) == bfd_object
)
8208 elf_dt_name (abfd
) = name
;
8212 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
8215 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8216 && bfd_get_format (abfd
) == bfd_object
)
8217 lib_class
= elf_dyn_lib_class (abfd
);
8224 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
8226 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8227 && bfd_get_format (abfd
) == bfd_object
)
8228 elf_dyn_lib_class (abfd
) = lib_class
;
8231 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8232 the linker ELF emulation code. */
8234 struct bfd_link_needed_list
*
8235 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8236 struct bfd_link_info
*info
)
8238 if (! is_elf_hash_table (info
->hash
))
8240 return elf_hash_table (info
)->needed
;
8243 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8244 hook for the linker ELF emulation code. */
8246 struct bfd_link_needed_list
*
8247 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8248 struct bfd_link_info
*info
)
8250 if (! is_elf_hash_table (info
->hash
))
8252 return elf_hash_table (info
)->runpath
;
8255 /* Get the name actually used for a dynamic object for a link. This
8256 is the SONAME entry if there is one. Otherwise, it is the string
8257 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8260 bfd_elf_get_dt_soname (bfd
*abfd
)
8262 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8263 && bfd_get_format (abfd
) == bfd_object
)
8264 return elf_dt_name (abfd
);
8268 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8269 the ELF linker emulation code. */
8272 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
8273 struct bfd_link_needed_list
**pneeded
)
8276 bfd_byte
*dynbuf
= NULL
;
8277 unsigned int elfsec
;
8278 unsigned long shlink
;
8279 bfd_byte
*extdyn
, *extdynend
;
8281 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
8285 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
8286 || bfd_get_format (abfd
) != bfd_object
)
8289 s
= bfd_get_section_by_name (abfd
, ".dynamic");
8290 if (s
== NULL
|| s
->size
== 0 || (s
->flags
& SEC_HAS_CONTENTS
) == 0)
8293 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
8296 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8297 if (elfsec
== SHN_BAD
)
8300 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8302 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8303 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8305 for (extdyn
= dynbuf
, extdynend
= dynbuf
+ s
->size
;
8306 (size_t) (extdynend
- extdyn
) >= extdynsize
;
8307 extdyn
+= extdynsize
)
8309 Elf_Internal_Dyn dyn
;
8311 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8313 if (dyn
.d_tag
== DT_NULL
)
8316 if (dyn
.d_tag
== DT_NEEDED
)
8319 struct bfd_link_needed_list
*l
;
8320 unsigned int tagv
= dyn
.d_un
.d_val
;
8323 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8328 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8348 struct elf_symbuf_symbol
8350 unsigned long st_name
; /* Symbol name, index in string tbl */
8351 unsigned char st_info
; /* Type and binding attributes */
8352 unsigned char st_other
; /* Visibilty, and target specific */
8355 struct elf_symbuf_head
8357 struct elf_symbuf_symbol
*ssym
;
8359 unsigned int st_shndx
;
8366 Elf_Internal_Sym
*isym
;
8367 struct elf_symbuf_symbol
*ssym
;
8373 /* Sort references to symbols by ascending section number. */
8376 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8378 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8379 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8381 if (s1
->st_shndx
!= s2
->st_shndx
)
8382 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8383 /* Final sort by the address of the sym in the symbuf ensures
8386 return s1
> s2
? 1 : -1;
8391 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8393 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8394 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8395 int ret
= strcmp (s1
->name
, s2
->name
);
8398 if (s1
->u
.p
!= s2
->u
.p
)
8399 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8403 static struct elf_symbuf_head
*
8404 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8406 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8407 struct elf_symbuf_symbol
*ssym
;
8408 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8409 size_t i
, shndx_count
, total_size
, amt
;
8411 amt
= symcount
* sizeof (*indbuf
);
8412 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8416 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8417 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8418 *ind
++ = &isymbuf
[i
];
8421 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8422 elf_sort_elf_symbol
);
8425 if (indbufend
> indbuf
)
8426 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8427 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8430 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8431 + (indbufend
- indbuf
) * sizeof (*ssym
));
8432 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8433 if (ssymbuf
== NULL
)
8439 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8440 ssymbuf
->ssym
= NULL
;
8441 ssymbuf
->count
= shndx_count
;
8442 ssymbuf
->st_shndx
= 0;
8443 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8445 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8448 ssymhead
->ssym
= ssym
;
8449 ssymhead
->count
= 0;
8450 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8452 ssym
->st_name
= (*ind
)->st_name
;
8453 ssym
->st_info
= (*ind
)->st_info
;
8454 ssym
->st_other
= (*ind
)->st_other
;
8457 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8458 && (uintptr_t) ssym
- (uintptr_t) ssymbuf
== total_size
);
8464 /* Check if 2 sections define the same set of local and global
8468 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8469 struct bfd_link_info
*info
)
8472 const struct elf_backend_data
*bed1
, *bed2
;
8473 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8474 size_t symcount1
, symcount2
;
8475 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8476 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8477 Elf_Internal_Sym
*isym
, *isymend
;
8478 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8479 size_t count1
, count2
, sec_count1
, sec_count2
, i
;
8480 unsigned int shndx1
, shndx2
;
8482 bool ignore_section_symbol_p
;
8487 /* Both sections have to be in ELF. */
8488 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8489 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8492 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8495 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8496 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8497 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8500 bed1
= get_elf_backend_data (bfd1
);
8501 bed2
= get_elf_backend_data (bfd2
);
8502 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8503 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8504 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8505 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8507 if (symcount1
== 0 || symcount2
== 0)
8513 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8514 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8516 /* Ignore section symbols only when matching non-debugging sections
8517 or linkonce section with comdat section. */
8518 ignore_section_symbol_p
8519 = ((sec1
->flags
& SEC_DEBUGGING
) == 0
8520 || ((elf_section_flags (sec1
) & SHF_GROUP
)
8521 != (elf_section_flags (sec2
) & SHF_GROUP
)));
8523 if (ssymbuf1
== NULL
)
8525 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8527 if (isymbuf1
== NULL
)
8530 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8532 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8533 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8537 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8539 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8541 if (isymbuf2
== NULL
)
8544 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8546 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8547 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8551 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8553 /* Optimized faster version. */
8555 struct elf_symbol
*symp
;
8556 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8559 hi
= ssymbuf1
->count
;
8565 mid
= (lo
+ hi
) / 2;
8566 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8568 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8572 count1
= ssymbuf1
[mid
].count
;
8577 if (ignore_section_symbol_p
)
8579 for (i
= 0; i
< count1
; i
++)
8580 if (ELF_ST_TYPE (ssymbuf1
->ssym
[i
].st_info
) == STT_SECTION
)
8582 count1
-= sec_count1
;
8586 hi
= ssymbuf2
->count
;
8592 mid
= (lo
+ hi
) / 2;
8593 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8595 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8599 count2
= ssymbuf2
[mid
].count
;
8604 if (ignore_section_symbol_p
)
8606 for (i
= 0; i
< count2
; i
++)
8607 if (ELF_ST_TYPE (ssymbuf2
->ssym
[i
].st_info
) == STT_SECTION
)
8609 count2
-= sec_count2
;
8612 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8616 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8618 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8619 if (symtable1
== NULL
|| symtable2
== NULL
)
8623 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
+ sec_count1
;
8624 ssym
< ssymend
; ssym
++)
8626 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8628 symp
->u
.ssym
= ssym
;
8629 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8636 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
+ sec_count2
;
8637 ssym
< ssymend
; ssym
++)
8639 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8641 symp
->u
.ssym
= ssym
;
8642 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8648 /* Sort symbol by name. */
8649 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8650 elf_sym_name_compare
);
8651 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8652 elf_sym_name_compare
);
8654 for (i
= 0; i
< count1
; i
++)
8655 /* Two symbols must have the same binding, type and name. */
8656 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8657 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8658 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8665 symtable1
= (struct elf_symbol
*)
8666 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8667 symtable2
= (struct elf_symbol
*)
8668 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8669 if (symtable1
== NULL
|| symtable2
== NULL
)
8672 /* Count definitions in the section. */
8674 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8675 if (isym
->st_shndx
== shndx1
8676 && (!ignore_section_symbol_p
8677 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8678 symtable1
[count1
++].u
.isym
= isym
;
8681 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8682 if (isym
->st_shndx
== shndx2
8683 && (!ignore_section_symbol_p
8684 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8685 symtable2
[count2
++].u
.isym
= isym
;
8687 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8690 for (i
= 0; i
< count1
; i
++)
8692 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8693 symtable1
[i
].u
.isym
->st_name
);
8695 for (i
= 0; i
< count2
; i
++)
8697 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8698 symtable2
[i
].u
.isym
->st_name
);
8700 /* Sort symbol by name. */
8701 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8702 elf_sym_name_compare
);
8703 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8704 elf_sym_name_compare
);
8706 for (i
= 0; i
< count1
; i
++)
8707 /* Two symbols must have the same binding, type and name. */
8708 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8709 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8710 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8724 /* Return TRUE if 2 section types are compatible. */
8727 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8728 bfd
*bbfd
, const asection
*bsec
)
8732 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8733 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8736 return elf_section_type (asec
) == elf_section_type (bsec
);
8739 /* Final phase of ELF linker. */
8741 /* A structure we use to avoid passing large numbers of arguments. */
8743 struct elf_final_link_info
8745 /* General link information. */
8746 struct bfd_link_info
*info
;
8749 /* Symbol string table. */
8750 struct elf_strtab_hash
*symstrtab
;
8751 /* .hash section. */
8753 /* symbol version section (.gnu.version). */
8754 asection
*symver_sec
;
8755 /* Buffer large enough to hold contents of any section. */
8757 /* Buffer large enough to hold external relocs of any section. */
8758 void *external_relocs
;
8759 /* Buffer large enough to hold internal relocs of any section. */
8760 Elf_Internal_Rela
*internal_relocs
;
8761 /* Buffer large enough to hold external local symbols of any input
8763 bfd_byte
*external_syms
;
8764 /* And a buffer for symbol section indices. */
8765 Elf_External_Sym_Shndx
*locsym_shndx
;
8766 /* Buffer large enough to hold internal local symbols of any input
8768 Elf_Internal_Sym
*internal_syms
;
8769 /* Array large enough to hold a symbol index for each local symbol
8770 of any input BFD. */
8772 /* Array large enough to hold a section pointer for each local
8773 symbol of any input BFD. */
8774 asection
**sections
;
8775 /* Buffer for SHT_SYMTAB_SHNDX section. */
8776 Elf_External_Sym_Shndx
*symshndxbuf
;
8777 /* Number of STT_FILE syms seen. */
8778 size_t filesym_count
;
8779 /* Local symbol hash table. */
8780 struct bfd_hash_table local_hash_table
;
8783 struct local_hash_entry
8785 /* Base hash table entry structure. */
8786 struct bfd_hash_entry root
;
8787 /* Size of the local symbol name. */
8789 /* Number of the duplicated local symbol names. */
8793 /* Create an entry in the local symbol hash table. */
8795 static struct bfd_hash_entry
*
8796 local_hash_newfunc (struct bfd_hash_entry
*entry
,
8797 struct bfd_hash_table
*table
,
8801 /* Allocate the structure if it has not already been allocated by a
8805 entry
= bfd_hash_allocate (table
,
8806 sizeof (struct local_hash_entry
));
8811 /* Call the allocation method of the superclass. */
8812 entry
= bfd_hash_newfunc (entry
, table
, string
);
8815 ((struct local_hash_entry
*) entry
)->count
= 0;
8816 ((struct local_hash_entry
*) entry
)->size
= 0;
8822 /* This struct is used to pass information to elf_link_output_extsym. */
8824 struct elf_outext_info
8829 struct elf_final_link_info
*flinfo
;
8833 /* Support for evaluating a complex relocation.
8835 Complex relocations are generalized, self-describing relocations. The
8836 implementation of them consists of two parts: complex symbols, and the
8837 relocations themselves.
8839 The relocations use a reserved elf-wide relocation type code (R_RELC
8840 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8841 information (start bit, end bit, word width, etc) into the addend. This
8842 information is extracted from CGEN-generated operand tables within gas.
8844 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8845 internal) representing prefix-notation expressions, including but not
8846 limited to those sorts of expressions normally encoded as addends in the
8847 addend field. The symbol mangling format is:
8850 | <unary-operator> ':' <node>
8851 | <binary-operator> ':' <node> ':' <node>
8854 <literal> := 's' <digits=N> ':' <N character symbol name>
8855 | 'S' <digits=N> ':' <N character section name>
8859 <binary-operator> := as in C
8860 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8863 set_symbol_value (bfd
*bfd_with_globals
,
8864 Elf_Internal_Sym
*isymbuf
,
8869 struct elf_link_hash_entry
**sym_hashes
;
8870 struct elf_link_hash_entry
*h
;
8871 size_t extsymoff
= locsymcount
;
8873 if (symidx
< locsymcount
)
8875 Elf_Internal_Sym
*sym
;
8877 sym
= isymbuf
+ symidx
;
8878 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8880 /* It is a local symbol: move it to the
8881 "absolute" section and give it a value. */
8882 sym
->st_shndx
= SHN_ABS
;
8883 sym
->st_value
= val
;
8886 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8890 /* It is a global symbol: set its link type
8891 to "defined" and give it a value. */
8893 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8894 h
= sym_hashes
[symidx
- extsymoff
];
8895 while (h
->root
.type
== bfd_link_hash_indirect
8896 || h
->root
.type
== bfd_link_hash_warning
)
8897 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8898 h
->root
.type
= bfd_link_hash_defined
;
8899 h
->root
.u
.def
.value
= val
;
8900 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8904 resolve_symbol (const char *name
,
8906 struct elf_final_link_info
*flinfo
,
8908 Elf_Internal_Sym
*isymbuf
,
8911 Elf_Internal_Sym
*sym
;
8912 struct bfd_link_hash_entry
*global_entry
;
8913 const char *candidate
= NULL
;
8914 Elf_Internal_Shdr
*symtab_hdr
;
8917 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8919 for (i
= 0; i
< locsymcount
; ++ i
)
8923 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8926 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8927 symtab_hdr
->sh_link
,
8930 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8931 name
, candidate
, (unsigned long) sym
->st_value
);
8933 if (candidate
&& strcmp (candidate
, name
) == 0)
8935 asection
*sec
= flinfo
->sections
[i
];
8937 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8938 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8940 printf ("Found symbol with value %8.8lx\n",
8941 (unsigned long) *result
);
8947 /* Hmm, haven't found it yet. perhaps it is a global. */
8948 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8949 false, false, true);
8953 if (global_entry
->type
== bfd_link_hash_defined
8954 || global_entry
->type
== bfd_link_hash_defweak
)
8956 *result
= (global_entry
->u
.def
.value
8957 + global_entry
->u
.def
.section
->output_section
->vma
8958 + global_entry
->u
.def
.section
->output_offset
);
8960 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8961 global_entry
->root
.string
, (unsigned long) *result
);
8969 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8970 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8971 names like "foo.end" which is the end address of section "foo". */
8974 resolve_section (const char *name
,
8982 for (curr
= sections
; curr
; curr
= curr
->next
)
8983 if (strcmp (curr
->name
, name
) == 0)
8985 *result
= curr
->vma
;
8989 /* Hmm. still haven't found it. try pseudo-section names. */
8990 /* FIXME: This could be coded more efficiently... */
8991 for (curr
= sections
; curr
; curr
= curr
->next
)
8993 len
= strlen (curr
->name
);
8994 if (len
> strlen (name
))
8997 if (strncmp (curr
->name
, name
, len
) == 0)
8999 if (startswith (name
+ len
, ".end"))
9001 *result
= (curr
->vma
9002 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
9006 /* Insert more pseudo-section names here, if you like. */
9014 undefined_reference (const char *reftype
, const char *name
)
9016 /* xgettext:c-format */
9017 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9019 bfd_set_error (bfd_error_bad_value
);
9023 eval_symbol (bfd_vma
*result
,
9026 struct elf_final_link_info
*flinfo
,
9028 Elf_Internal_Sym
*isymbuf
,
9037 const char *sym
= *symp
;
9039 bool symbol_is_section
= false;
9044 if (len
< 1 || len
> sizeof (symbuf
))
9046 bfd_set_error (bfd_error_invalid_operation
);
9059 *result
= strtoul (sym
, (char **) symp
, 16);
9063 symbol_is_section
= true;
9067 symlen
= strtol (sym
, (char **) symp
, 10);
9068 sym
= *symp
+ 1; /* Skip the trailing ':'. */
9070 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
9072 bfd_set_error (bfd_error_invalid_operation
);
9076 memcpy (symbuf
, sym
, symlen
);
9077 symbuf
[symlen
] = '\0';
9078 *symp
= sym
+ symlen
;
9080 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9081 the symbol as a section, or vice-versa. so we're pretty liberal in our
9082 interpretation here; section means "try section first", not "must be a
9083 section", and likewise with symbol. */
9085 if (symbol_is_section
)
9087 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
9088 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
9089 isymbuf
, locsymcount
))
9091 undefined_reference ("section", symbuf
);
9097 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
9098 isymbuf
, locsymcount
)
9099 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
9102 undefined_reference ("symbol", symbuf
);
9109 /* All that remains are operators. */
9111 #define UNARY_OP(op) \
9112 if (startswith (sym, #op)) \
9114 sym += strlen (#op); \
9118 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9119 isymbuf, locsymcount, signed_p)) \
9122 *result = op ((bfd_signed_vma) a); \
9128 #define BINARY_OP_HEAD(op) \
9129 if (startswith (sym, #op)) \
9131 sym += strlen (#op); \
9135 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9136 isymbuf, locsymcount, signed_p)) \
9139 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9140 isymbuf, locsymcount, signed_p)) \
9142 #define BINARY_OP_TAIL(op) \
9144 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9149 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9153 BINARY_OP_HEAD (<<);
9154 if (b
>= sizeof (a
) * CHAR_BIT
)
9160 BINARY_OP_TAIL (<<);
9161 BINARY_OP_HEAD (>>);
9162 if (b
>= sizeof (a
) * CHAR_BIT
)
9164 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
9167 BINARY_OP_TAIL (>>);
9180 _bfd_error_handler (_("division by zero"));
9181 bfd_set_error (bfd_error_bad_value
);
9188 _bfd_error_handler (_("division by zero"));
9189 bfd_set_error (bfd_error_bad_value
);
9202 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
9203 bfd_set_error (bfd_error_invalid_operation
);
9209 put_value (bfd_vma size
,
9210 unsigned long chunksz
,
9215 location
+= (size
- chunksz
);
9217 for (; size
; size
-= chunksz
, location
-= chunksz
)
9222 bfd_put_8 (input_bfd
, x
, location
);
9226 bfd_put_16 (input_bfd
, x
, location
);
9230 bfd_put_32 (input_bfd
, x
, location
);
9231 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9237 bfd_put_64 (input_bfd
, x
, location
);
9238 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9251 get_value (bfd_vma size
,
9252 unsigned long chunksz
,
9259 /* Sanity checks. */
9260 BFD_ASSERT (chunksz
<= sizeof (x
)
9263 && (size
% chunksz
) == 0
9264 && input_bfd
!= NULL
9265 && location
!= NULL
);
9267 if (chunksz
== sizeof (x
))
9269 BFD_ASSERT (size
== chunksz
);
9271 /* Make sure that we do not perform an undefined shift operation.
9272 We know that size == chunksz so there will only be one iteration
9273 of the loop below. */
9277 shift
= 8 * chunksz
;
9279 for (; size
; size
-= chunksz
, location
+= chunksz
)
9284 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
9287 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
9290 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
9294 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
9305 decode_complex_addend (unsigned long *start
, /* in bits */
9306 unsigned long *oplen
, /* in bits */
9307 unsigned long *len
, /* in bits */
9308 unsigned long *wordsz
, /* in bytes */
9309 unsigned long *chunksz
, /* in bytes */
9310 unsigned long *lsb0_p
,
9311 unsigned long *signed_p
,
9312 unsigned long *trunc_p
,
9313 unsigned long encoded
)
9315 * start
= encoded
& 0x3F;
9316 * len
= (encoded
>> 6) & 0x3F;
9317 * oplen
= (encoded
>> 12) & 0x3F;
9318 * wordsz
= (encoded
>> 18) & 0xF;
9319 * chunksz
= (encoded
>> 22) & 0xF;
9320 * lsb0_p
= (encoded
>> 27) & 1;
9321 * signed_p
= (encoded
>> 28) & 1;
9322 * trunc_p
= (encoded
>> 29) & 1;
9325 bfd_reloc_status_type
9326 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
9327 asection
*input_section
,
9329 Elf_Internal_Rela
*rel
,
9332 bfd_vma shift
, x
, mask
;
9333 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9334 bfd_reloc_status_type r
;
9335 bfd_size_type octets
;
9337 /* Perform this reloc, since it is complex.
9338 (this is not to say that it necessarily refers to a complex
9339 symbol; merely that it is a self-describing CGEN based reloc.
9340 i.e. the addend has the complete reloc information (bit start, end,
9341 word size, etc) encoded within it.). */
9343 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9344 &chunksz
, &lsb0_p
, &signed_p
,
9345 &trunc_p
, rel
->r_addend
);
9347 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9350 shift
= (start
+ 1) - len
;
9352 shift
= (8 * wordsz
) - (start
+ len
);
9354 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9355 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9358 printf ("Doing complex reloc: "
9359 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9360 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9361 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9362 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9363 oplen
, (unsigned long) x
, (unsigned long) mask
,
9364 (unsigned long) relocation
);
9369 /* Now do an overflow check. */
9370 r
= bfd_check_overflow ((signed_p
9371 ? complain_overflow_signed
9372 : complain_overflow_unsigned
),
9373 len
, 0, (8 * wordsz
),
9377 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9380 printf (" relocation: %8.8lx\n"
9381 " shifted mask: %8.8lx\n"
9382 " shifted/masked reloc: %8.8lx\n"
9383 " result: %8.8lx\n",
9384 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9385 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9387 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9391 /* Functions to read r_offset from external (target order) reloc
9392 entry. Faster than bfd_getl32 et al, because we let the compiler
9393 know the value is aligned. */
9396 ext32l_r_offset (const void *p
)
9403 const union aligned32
*a
9404 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9406 uint32_t aval
= ( (uint32_t) a
->c
[0]
9407 | (uint32_t) a
->c
[1] << 8
9408 | (uint32_t) a
->c
[2] << 16
9409 | (uint32_t) a
->c
[3] << 24);
9414 ext32b_r_offset (const void *p
)
9421 const union aligned32
*a
9422 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9424 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9425 | (uint32_t) a
->c
[1] << 16
9426 | (uint32_t) a
->c
[2] << 8
9427 | (uint32_t) a
->c
[3]);
9432 ext64l_r_offset (const void *p
)
9439 const union aligned64
*a
9440 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9442 uint64_t aval
= ( (uint64_t) a
->c
[0]
9443 | (uint64_t) a
->c
[1] << 8
9444 | (uint64_t) a
->c
[2] << 16
9445 | (uint64_t) a
->c
[3] << 24
9446 | (uint64_t) a
->c
[4] << 32
9447 | (uint64_t) a
->c
[5] << 40
9448 | (uint64_t) a
->c
[6] << 48
9449 | (uint64_t) a
->c
[7] << 56);
9454 ext64b_r_offset (const void *p
)
9461 const union aligned64
*a
9462 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9464 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9465 | (uint64_t) a
->c
[1] << 48
9466 | (uint64_t) a
->c
[2] << 40
9467 | (uint64_t) a
->c
[3] << 32
9468 | (uint64_t) a
->c
[4] << 24
9469 | (uint64_t) a
->c
[5] << 16
9470 | (uint64_t) a
->c
[6] << 8
9471 | (uint64_t) a
->c
[7]);
9475 /* When performing a relocatable link, the input relocations are
9476 preserved. But, if they reference global symbols, the indices
9477 referenced must be updated. Update all the relocations found in
9481 elf_link_adjust_relocs (bfd
*abfd
,
9483 struct bfd_elf_section_reloc_data
*reldata
,
9485 struct bfd_link_info
*info
)
9488 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9490 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9491 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9492 bfd_vma r_type_mask
;
9494 unsigned int count
= reldata
->count
;
9495 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9497 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9499 swap_in
= bed
->s
->swap_reloc_in
;
9500 swap_out
= bed
->s
->swap_reloc_out
;
9502 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9504 swap_in
= bed
->s
->swap_reloca_in
;
9505 swap_out
= bed
->s
->swap_reloca_out
;
9510 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9513 if (bed
->s
->arch_size
== 32)
9520 r_type_mask
= 0xffffffff;
9524 erela
= reldata
->hdr
->contents
;
9525 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9527 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9530 if (*rel_hash
== NULL
)
9533 if ((*rel_hash
)->indx
== -2
9534 && info
->gc_sections
9535 && ! info
->gc_keep_exported
)
9537 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9538 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9540 (*rel_hash
)->root
.root
.string
);
9541 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9543 bfd_set_error (bfd_error_invalid_operation
);
9546 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9548 (*swap_in
) (abfd
, erela
, irela
);
9549 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9550 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9551 | (irela
[j
].r_info
& r_type_mask
));
9552 (*swap_out
) (abfd
, irela
, erela
);
9555 if (bed
->elf_backend_update_relocs
)
9556 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9558 if (sort
&& count
!= 0)
9560 bfd_vma (*ext_r_off
) (const void *);
9563 bfd_byte
*base
, *end
, *p
, *loc
;
9564 bfd_byte
*buf
= NULL
;
9566 if (bed
->s
->arch_size
== 32)
9568 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9569 ext_r_off
= ext32l_r_offset
;
9570 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9571 ext_r_off
= ext32b_r_offset
;
9577 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9578 ext_r_off
= ext64l_r_offset
;
9579 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9580 ext_r_off
= ext64b_r_offset
;
9585 /* Must use a stable sort here. A modified insertion sort,
9586 since the relocs are mostly sorted already. */
9587 elt_size
= reldata
->hdr
->sh_entsize
;
9588 base
= reldata
->hdr
->contents
;
9589 end
= base
+ count
* elt_size
;
9590 if (elt_size
> sizeof (Elf64_External_Rela
))
9593 /* Ensure the first element is lowest. This acts as a sentinel,
9594 speeding the main loop below. */
9595 r_off
= (*ext_r_off
) (base
);
9596 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9598 bfd_vma r_off2
= (*ext_r_off
) (p
);
9607 /* Don't just swap *base and *loc as that changes the order
9608 of the original base[0] and base[1] if they happen to
9609 have the same r_offset. */
9610 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9611 memcpy (onebuf
, loc
, elt_size
);
9612 memmove (base
+ elt_size
, base
, loc
- base
);
9613 memcpy (base
, onebuf
, elt_size
);
9616 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9618 /* base to p is sorted, *p is next to insert. */
9619 r_off
= (*ext_r_off
) (p
);
9620 /* Search the sorted region for location to insert. */
9622 while (r_off
< (*ext_r_off
) (loc
))
9627 /* Chances are there is a run of relocs to insert here,
9628 from one of more input files. Files are not always
9629 linked in order due to the way elf_link_input_bfd is
9630 called. See pr17666. */
9631 size_t sortlen
= p
- loc
;
9632 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9633 size_t runlen
= elt_size
;
9634 bfd_vma r_off_runend
= r_off
;
9635 bfd_vma r_off_runend_next
;
9636 size_t buf_size
= 96 * 1024;
9637 while (p
+ runlen
< end
9638 && (sortlen
<= buf_size
9639 || runlen
+ elt_size
<= buf_size
)
9640 /* run must not break the ordering of base..loc+1 */
9641 && r_off2
> (r_off_runend_next
= (*ext_r_off
) (p
+ runlen
))
9642 /* run must be already sorted */
9643 && r_off_runend_next
>= r_off_runend
)
9646 r_off_runend
= r_off_runend_next
;
9650 buf
= bfd_malloc (buf_size
);
9654 if (runlen
< sortlen
)
9656 memcpy (buf
, p
, runlen
);
9657 memmove (loc
+ runlen
, loc
, sortlen
);
9658 memcpy (loc
, buf
, runlen
);
9662 memcpy (buf
, loc
, sortlen
);
9663 memmove (loc
, p
, runlen
);
9664 memcpy (loc
+ runlen
, buf
, sortlen
);
9666 p
+= runlen
- elt_size
;
9669 /* Hashes are no longer valid. */
9670 free (reldata
->hashes
);
9671 reldata
->hashes
= NULL
;
9677 struct elf_link_sort_rela
9683 enum elf_reloc_type_class type
;
9684 /* We use this as an array of size int_rels_per_ext_rel. */
9685 Elf_Internal_Rela rela
[1];
9688 /* qsort stability here and for cmp2 is only an issue if multiple
9689 dynamic relocations are emitted at the same address. But targets
9690 that apply a series of dynamic relocations each operating on the
9691 result of the prior relocation can't use -z combreloc as
9692 implemented anyway. Such schemes tend to be broken by sorting on
9693 symbol index. That leaves dynamic NONE relocs as the only other
9694 case where ld might emit multiple relocs at the same address, and
9695 those are only emitted due to target bugs. */
9698 elf_link_sort_cmp1 (const void *A
, const void *B
)
9700 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9701 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9702 int relativea
, relativeb
;
9704 relativea
= a
->type
== reloc_class_relative
;
9705 relativeb
= b
->type
== reloc_class_relative
;
9707 if (relativea
< relativeb
)
9709 if (relativea
> relativeb
)
9711 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9713 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9715 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9717 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9723 elf_link_sort_cmp2 (const void *A
, const void *B
)
9725 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9726 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9728 if (a
->type
< b
->type
)
9730 if (a
->type
> b
->type
)
9732 if (a
->u
.offset
< b
->u
.offset
)
9734 if (a
->u
.offset
> b
->u
.offset
)
9736 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9738 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9744 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9746 asection
*dynamic_relocs
;
9749 bfd_size_type count
, size
;
9750 size_t i
, ret
, sort_elt
, ext_size
;
9751 bfd_byte
*sort
, *s_non_relative
, *p
;
9752 struct elf_link_sort_rela
*sq
;
9753 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9754 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9755 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9756 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9757 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9758 struct bfd_link_order
*lo
;
9762 /* Find a dynamic reloc section. */
9763 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9764 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9765 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9766 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9768 bool use_rela_initialised
= false;
9770 /* This is just here to stop gcc from complaining.
9771 Its initialization checking code is not perfect. */
9774 /* Both sections are present. Examine the sizes
9775 of the indirect sections to help us choose. */
9776 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9777 if (lo
->type
== bfd_indirect_link_order
)
9779 asection
*o
= lo
->u
.indirect
.section
;
9781 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9783 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9784 /* Section size is divisible by both rel and rela sizes.
9785 It is of no help to us. */
9789 /* Section size is only divisible by rela. */
9790 if (use_rela_initialised
&& !use_rela
)
9792 _bfd_error_handler (_("%pB: unable to sort relocs - "
9793 "they are in more than one size"),
9795 bfd_set_error (bfd_error_invalid_operation
);
9801 use_rela_initialised
= true;
9805 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9807 /* Section size is only divisible by rel. */
9808 if (use_rela_initialised
&& use_rela
)
9810 _bfd_error_handler (_("%pB: unable to sort relocs - "
9811 "they are in more than one size"),
9813 bfd_set_error (bfd_error_invalid_operation
);
9819 use_rela_initialised
= true;
9824 /* The section size is not divisible by either -
9825 something is wrong. */
9826 _bfd_error_handler (_("%pB: unable to sort relocs - "
9827 "they are of an unknown size"), abfd
);
9828 bfd_set_error (bfd_error_invalid_operation
);
9833 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9834 if (lo
->type
== bfd_indirect_link_order
)
9836 asection
*o
= lo
->u
.indirect
.section
;
9838 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9840 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9841 /* Section size is divisible by both rel and rela sizes.
9842 It is of no help to us. */
9846 /* Section size is only divisible by rela. */
9847 if (use_rela_initialised
&& !use_rela
)
9849 _bfd_error_handler (_("%pB: unable to sort relocs - "
9850 "they are in more than one size"),
9852 bfd_set_error (bfd_error_invalid_operation
);
9858 use_rela_initialised
= true;
9862 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9864 /* Section size is only divisible by rel. */
9865 if (use_rela_initialised
&& use_rela
)
9867 _bfd_error_handler (_("%pB: unable to sort relocs - "
9868 "they are in more than one size"),
9870 bfd_set_error (bfd_error_invalid_operation
);
9876 use_rela_initialised
= true;
9881 /* The section size is not divisible by either -
9882 something is wrong. */
9883 _bfd_error_handler (_("%pB: unable to sort relocs - "
9884 "they are of an unknown size"), abfd
);
9885 bfd_set_error (bfd_error_invalid_operation
);
9890 if (! use_rela_initialised
)
9894 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9896 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9903 dynamic_relocs
= rela_dyn
;
9904 ext_size
= bed
->s
->sizeof_rela
;
9905 swap_in
= bed
->s
->swap_reloca_in
;
9906 swap_out
= bed
->s
->swap_reloca_out
;
9910 dynamic_relocs
= rel_dyn
;
9911 ext_size
= bed
->s
->sizeof_rel
;
9912 swap_in
= bed
->s
->swap_reloc_in
;
9913 swap_out
= bed
->s
->swap_reloc_out
;
9917 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9918 if (lo
->type
== bfd_indirect_link_order
)
9919 size
+= lo
->u
.indirect
.section
->size
;
9921 if (size
!= dynamic_relocs
->size
)
9924 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9925 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9927 count
= dynamic_relocs
->size
/ ext_size
;
9930 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9934 (*info
->callbacks
->warning
)
9935 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9939 if (bed
->s
->arch_size
== 32)
9940 r_sym_mask
= ~(bfd_vma
) 0xff;
9942 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9944 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9945 if (lo
->type
== bfd_indirect_link_order
)
9947 bfd_byte
*erel
, *erelend
;
9948 asection
*o
= lo
->u
.indirect
.section
;
9950 if (o
->contents
== NULL
&& o
->size
!= 0)
9952 /* This is a reloc section that is being handled as a normal
9953 section. See bfd_section_from_shdr. We can't combine
9954 relocs in this case. */
9959 erelend
= o
->contents
+ o
->size
;
9960 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9962 while (erel
< erelend
)
9964 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9966 (*swap_in
) (abfd
, erel
, s
->rela
);
9967 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9968 s
->u
.sym_mask
= r_sym_mask
;
9974 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9976 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9978 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9979 if (s
->type
!= reloc_class_relative
)
9985 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9986 for (; i
< count
; i
++, p
+= sort_elt
)
9988 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9989 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9991 sp
->u
.offset
= sq
->rela
->r_offset
;
9994 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9996 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9997 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9999 /* We have plt relocs in .rela.dyn. */
10000 sq
= (struct elf_link_sort_rela
*) sort
;
10001 for (i
= 0; i
< count
; i
++)
10002 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
10004 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
10006 struct bfd_link_order
**plo
;
10007 /* Put srelplt link_order last. This is so the output_offset
10008 set in the next loop is correct for DT_JMPREL. */
10009 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
10010 if ((*plo
)->type
== bfd_indirect_link_order
10011 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
10017 plo
= &(*plo
)->next
;
10020 dynamic_relocs
->map_tail
.link_order
= lo
;
10025 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10026 if (lo
->type
== bfd_indirect_link_order
)
10028 bfd_byte
*erel
, *erelend
;
10029 asection
*o
= lo
->u
.indirect
.section
;
10031 erel
= o
->contents
;
10032 erelend
= o
->contents
+ o
->size
;
10033 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
10034 while (erel
< erelend
)
10036 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
10037 (*swap_out
) (abfd
, s
->rela
, erel
);
10044 *psec
= dynamic_relocs
;
10048 /* Add a symbol to the output symbol string table. */
10051 elf_link_output_symstrtab (void *finf
,
10053 Elf_Internal_Sym
*elfsym
,
10054 asection
*input_sec
,
10055 struct elf_link_hash_entry
*h
)
10057 struct elf_final_link_info
*flinfo
= finf
;
10058 int (*output_symbol_hook
)
10059 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
10060 struct elf_link_hash_entry
*);
10061 struct elf_link_hash_table
*hash_table
;
10062 const struct elf_backend_data
*bed
;
10063 bfd_size_type strtabsize
;
10065 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10067 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10068 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
10069 if (output_symbol_hook
!= NULL
)
10071 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
10076 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
10077 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
10078 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
10079 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
10081 if (name
== NULL
|| *name
== '\0')
10082 elfsym
->st_name
= (unsigned long) -1;
10085 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10086 to get the final offset for st_name. */
10087 char *versioned_name
= (char *) name
;
10090 if (h
->versioned
== versioned
&& h
->def_dynamic
)
10092 /* Keep only one '@' for versioned symbols defined in
10094 char *version
= strrchr (name
, ELF_VER_CHR
);
10095 char *base_end
= strchr (name
, ELF_VER_CHR
);
10096 if (version
!= base_end
)
10099 size_t len
= strlen (name
);
10100 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
10101 if (versioned_name
== NULL
)
10103 base_len
= base_end
- name
;
10104 memcpy (versioned_name
, name
, base_len
);
10105 memcpy (versioned_name
+ base_len
, version
,
10110 else if (flinfo
->info
->unique_symbol
10111 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
10113 struct local_hash_entry
*lh
;
10117 switch (ELF_ST_TYPE (elfsym
->st_info
))
10123 lh
= (struct local_hash_entry
*) bfd_hash_lookup
10124 (&flinfo
->local_hash_table
, name
, true, false);
10127 /* Always append ".COUNT" to local symbols to avoid
10128 potential conflicts with local symbol "XXX.COUNT". */
10129 sprintf (buf
, "%lx", lh
->count
);
10130 base_len
= lh
->size
;
10133 base_len
= strlen (name
);
10134 lh
->size
= base_len
;
10136 count_len
= strlen (buf
);
10137 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
10138 base_len
+ count_len
+ 2);
10139 if (versioned_name
== NULL
)
10141 memcpy (versioned_name
, name
, base_len
);
10142 versioned_name
[base_len
] = '.';
10143 memcpy (versioned_name
+ base_len
+ 1, buf
,
10150 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
10151 versioned_name
, false);
10152 if (elfsym
->st_name
== (unsigned long) -1)
10156 hash_table
= elf_hash_table (flinfo
->info
);
10157 strtabsize
= hash_table
->strtabsize
;
10158 if (strtabsize
<= flinfo
->output_bfd
->symcount
)
10160 strtabsize
+= strtabsize
;
10161 hash_table
->strtabsize
= strtabsize
;
10162 strtabsize
*= sizeof (*hash_table
->strtab
);
10164 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
10166 if (hash_table
->strtab
== NULL
)
10169 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].sym
= *elfsym
;
10170 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].dest_index
10171 = flinfo
->output_bfd
->symcount
;
10172 flinfo
->output_bfd
->symcount
+= 1;
10177 /* Swap symbols out to the symbol table and flush the output symbols to
10181 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
10183 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
10186 const struct elf_backend_data
*bed
;
10188 Elf_Internal_Shdr
*hdr
;
10192 if (flinfo
->output_bfd
->symcount
== 0)
10195 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10197 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10199 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10200 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
10201 if (symbuf
== NULL
)
10204 if (flinfo
->symshndxbuf
)
10206 amt
= sizeof (Elf_External_Sym_Shndx
);
10207 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
10208 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
10209 if (flinfo
->symshndxbuf
== NULL
)
10216 /* Now swap out the symbols. */
10217 for (i
= 0; i
< flinfo
->output_bfd
->symcount
; i
++)
10219 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
10220 if (elfsym
->sym
.st_name
== (unsigned long) -1)
10221 elfsym
->sym
.st_name
= 0;
10223 elfsym
->sym
.st_name
10224 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
10225 elfsym
->sym
.st_name
);
10227 /* Inform the linker of the addition of this symbol. */
10229 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
10230 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
10233 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
10234 ((bfd_byte
*) symbuf
10235 + (elfsym
->dest_index
10236 * bed
->s
->sizeof_sym
)),
10237 NPTR_ADD (flinfo
->symshndxbuf
,
10238 elfsym
->dest_index
));
10241 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
10242 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
10243 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10244 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
10245 && bfd_write (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
10247 hdr
->sh_size
+= amt
;
10255 free (hash_table
->strtab
);
10256 hash_table
->strtab
= NULL
;
10261 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10264 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
10266 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
10267 && sym
->st_shndx
< SHN_LORESERVE
)
10269 /* The gABI doesn't support dynamic symbols in output sections
10272 /* xgettext:c-format */
10273 (_("%pB: too many sections: %d (>= %d)"),
10274 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
10275 bfd_set_error (bfd_error_nonrepresentable_section
);
10281 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10282 allowing an unsatisfied unversioned symbol in the DSO to match a
10283 versioned symbol that would normally require an explicit version.
10284 We also handle the case that a DSO references a hidden symbol
10285 which may be satisfied by a versioned symbol in another DSO. */
10288 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
10289 const struct elf_backend_data
*bed
,
10290 struct elf_link_hash_entry
*h
)
10293 struct elf_link_loaded_list
*loaded
;
10295 if (!is_elf_hash_table (info
->hash
))
10298 /* Check indirect symbol. */
10299 while (h
->root
.type
== bfd_link_hash_indirect
)
10300 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10302 switch (h
->root
.type
)
10308 case bfd_link_hash_undefined
:
10309 case bfd_link_hash_undefweak
:
10310 abfd
= h
->root
.u
.undef
.abfd
;
10312 || (abfd
->flags
& DYNAMIC
) == 0
10313 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
10317 case bfd_link_hash_defined
:
10318 case bfd_link_hash_defweak
:
10319 abfd
= h
->root
.u
.def
.section
->owner
;
10322 case bfd_link_hash_common
:
10323 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10326 BFD_ASSERT (abfd
!= NULL
);
10328 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10330 loaded
= loaded
->next
)
10333 Elf_Internal_Shdr
*hdr
;
10335 size_t extsymcount
;
10337 Elf_Internal_Shdr
*versymhdr
;
10338 Elf_Internal_Sym
*isym
;
10339 Elf_Internal_Sym
*isymend
;
10340 Elf_Internal_Sym
*isymbuf
;
10341 Elf_External_Versym
*ever
;
10342 Elf_External_Versym
*extversym
;
10344 input
= loaded
->abfd
;
10346 /* We check each DSO for a possible hidden versioned definition. */
10348 || elf_dynversym (input
) == 0)
10351 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10353 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10354 if (elf_bad_symtab (input
))
10356 extsymcount
= symcount
;
10361 extsymcount
= symcount
- hdr
->sh_info
;
10362 extsymoff
= hdr
->sh_info
;
10365 if (extsymcount
== 0)
10368 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10370 if (isymbuf
== NULL
)
10373 /* Read in any version definitions. */
10374 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10375 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10376 || (extversym
= (Elf_External_Versym
*)
10377 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10378 versymhdr
->sh_size
)) == NULL
)
10384 ever
= extversym
+ extsymoff
;
10385 isymend
= isymbuf
+ extsymcount
;
10386 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10389 Elf_Internal_Versym iver
;
10390 unsigned short version_index
;
10392 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10393 || isym
->st_shndx
== SHN_UNDEF
)
10396 name
= bfd_elf_string_from_elf_section (input
,
10399 if (strcmp (name
, h
->root
.root
.string
) != 0)
10402 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10404 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10405 && !(h
->def_regular
10406 && h
->forced_local
))
10408 /* If we have a non-hidden versioned sym, then it should
10409 have provided a definition for the undefined sym unless
10410 it is defined in a non-shared object and forced local.
10415 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10416 if (version_index
== 1 || version_index
== 2)
10418 /* This is the base or first version. We can use it. */
10432 /* Convert ELF common symbol TYPE. */
10435 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10437 /* Commom symbol can only appear in relocatable link. */
10438 if (!bfd_link_relocatable (info
))
10440 switch (info
->elf_stt_common
)
10444 case elf_stt_common
:
10447 case no_elf_stt_common
:
10454 /* Add an external symbol to the symbol table. This is called from
10455 the hash table traversal routine. When generating a shared object,
10456 we go through the symbol table twice. The first time we output
10457 anything that might have been forced to local scope in a version
10458 script. The second time we output the symbols that are still
10462 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10464 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10465 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10466 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10468 Elf_Internal_Sym sym
;
10469 asection
*input_sec
;
10470 const struct elf_backend_data
*bed
;
10475 if (h
->root
.type
== bfd_link_hash_warning
)
10477 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10478 if (h
->root
.type
== bfd_link_hash_new
)
10482 /* Decide whether to output this symbol in this pass. */
10483 if (eoinfo
->localsyms
)
10485 if (!h
->forced_local
)
10490 if (h
->forced_local
)
10494 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10496 if (h
->root
.type
== bfd_link_hash_undefined
)
10498 /* If we have an undefined symbol reference here then it must have
10499 come from a shared library that is being linked in. (Undefined
10500 references in regular files have already been handled unless
10501 they are in unreferenced sections which are removed by garbage
10503 bool ignore_undef
= false;
10505 /* Some symbols may be special in that the fact that they're
10506 undefined can be safely ignored - let backend determine that. */
10507 if (bed
->elf_backend_ignore_undef_symbol
)
10508 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10510 /* If we are reporting errors for this situation then do so now. */
10512 && h
->ref_dynamic_nonweak
10513 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10514 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10515 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10517 flinfo
->info
->callbacks
->undefined_symbol
10518 (flinfo
->info
, h
->root
.root
.string
,
10519 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10520 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10521 && !flinfo
->info
->warn_unresolved_syms
);
10524 /* Strip a global symbol defined in a discarded section. */
10529 /* We should also warn if a forced local symbol is referenced from
10530 shared libraries. */
10531 if (bfd_link_executable (flinfo
->info
)
10536 && h
->ref_dynamic_nonweak
10537 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10541 struct elf_link_hash_entry
*hi
= h
;
10543 /* Check indirect symbol. */
10544 while (hi
->root
.type
== bfd_link_hash_indirect
)
10545 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10547 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10548 /* xgettext:c-format */
10549 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10550 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10551 /* xgettext:c-format */
10552 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10554 /* xgettext:c-format */
10555 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10556 def_bfd
= flinfo
->output_bfd
;
10557 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10558 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10559 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10560 h
->root
.root
.string
, def_bfd
);
10561 bfd_set_error (bfd_error_bad_value
);
10562 eoinfo
->failed
= true;
10566 /* We don't want to output symbols that have never been mentioned by
10567 a regular file, or that we have been told to strip. However, if
10568 h->indx is set to -2, the symbol is used by a reloc and we must
10573 else if ((h
->def_dynamic
10575 || h
->root
.type
== bfd_link_hash_new
)
10577 && !h
->ref_regular
)
10579 else if (flinfo
->info
->strip
== strip_all
)
10581 else if (flinfo
->info
->strip
== strip_some
10582 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10583 h
->root
.root
.string
, false, false) == NULL
)
10585 else if ((h
->root
.type
== bfd_link_hash_defined
10586 || h
->root
.type
== bfd_link_hash_defweak
)
10587 && ((flinfo
->info
->strip_discarded
10588 && discarded_section (h
->root
.u
.def
.section
))
10589 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10590 && h
->root
.u
.def
.section
->owner
!= NULL
10591 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10593 else if ((h
->root
.type
== bfd_link_hash_undefined
10594 || h
->root
.type
== bfd_link_hash_undefweak
)
10595 && h
->root
.u
.undef
.abfd
!= NULL
10596 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10601 /* If we're stripping it, and it's not a dynamic symbol, there's
10602 nothing else to do. However, if it is a forced local symbol or
10603 an ifunc symbol we need to give the backend finish_dynamic_symbol
10604 function a chance to make it dynamic. */
10606 && h
->dynindx
== -1
10607 && type
!= STT_GNU_IFUNC
10608 && !h
->forced_local
)
10612 sym
.st_size
= h
->size
;
10613 sym
.st_other
= h
->other
;
10614 switch (h
->root
.type
)
10617 case bfd_link_hash_new
:
10618 case bfd_link_hash_warning
:
10622 case bfd_link_hash_undefined
:
10623 case bfd_link_hash_undefweak
:
10624 input_sec
= bfd_und_section_ptr
;
10625 sym
.st_shndx
= SHN_UNDEF
;
10628 case bfd_link_hash_defined
:
10629 case bfd_link_hash_defweak
:
10631 input_sec
= h
->root
.u
.def
.section
;
10632 if (input_sec
->output_section
!= NULL
)
10635 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10636 input_sec
->output_section
);
10637 if (sym
.st_shndx
== SHN_BAD
)
10640 /* xgettext:c-format */
10641 (_("%pB: could not find output section %pA for input section %pA"),
10642 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10643 bfd_set_error (bfd_error_nonrepresentable_section
);
10644 eoinfo
->failed
= true;
10648 /* ELF symbols in relocatable files are section relative,
10649 but in nonrelocatable files they are virtual
10651 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10652 if (!bfd_link_relocatable (flinfo
->info
))
10654 sym
.st_value
+= input_sec
->output_section
->vma
;
10655 if (h
->type
== STT_TLS
)
10657 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10658 if (tls_sec
!= NULL
)
10659 sym
.st_value
-= tls_sec
->vma
;
10665 BFD_ASSERT (input_sec
->owner
== NULL
10666 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10667 sym
.st_shndx
= SHN_UNDEF
;
10668 input_sec
= bfd_und_section_ptr
;
10673 case bfd_link_hash_common
:
10674 input_sec
= h
->root
.u
.c
.p
->section
;
10675 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10676 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10679 case bfd_link_hash_indirect
:
10680 /* These symbols are created by symbol versioning. They point
10681 to the decorated version of the name. For example, if the
10682 symbol foo@@GNU_1.2 is the default, which should be used when
10683 foo is used with no version, then we add an indirect symbol
10684 foo which points to foo@@GNU_1.2. We ignore these symbols,
10685 since the indirected symbol is already in the hash table. */
10689 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10690 switch (h
->root
.type
)
10692 case bfd_link_hash_common
:
10693 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10695 case bfd_link_hash_defined
:
10696 case bfd_link_hash_defweak
:
10697 if (bed
->common_definition (&sym
))
10698 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10702 case bfd_link_hash_undefined
:
10703 case bfd_link_hash_undefweak
:
10709 if (h
->forced_local
)
10711 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10712 /* Turn off visibility on local symbol. */
10713 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10715 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10716 else if (h
->unique_global
&& h
->def_regular
)
10717 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10718 else if (h
->root
.type
== bfd_link_hash_undefweak
10719 || h
->root
.type
== bfd_link_hash_defweak
)
10720 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10722 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10723 sym
.st_target_internal
= h
->target_internal
;
10725 /* Give the processor backend a chance to tweak the symbol value,
10726 and also to finish up anything that needs to be done for this
10727 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10728 forced local syms when non-shared is due to a historical quirk.
10729 STT_GNU_IFUNC symbol must go through PLT. */
10730 if ((h
->type
== STT_GNU_IFUNC
10732 && !bfd_link_relocatable (flinfo
->info
))
10733 || ((h
->dynindx
!= -1
10734 || h
->forced_local
)
10735 && ((bfd_link_pic (flinfo
->info
)
10736 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10737 || h
->root
.type
!= bfd_link_hash_undefweak
))
10738 || !h
->forced_local
)
10739 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10741 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10742 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10744 eoinfo
->failed
= true;
10749 /* If we are marking the symbol as undefined, and there are no
10750 non-weak references to this symbol from a regular object, then
10751 mark the symbol as weak undefined; if there are non-weak
10752 references, mark the symbol as strong. We can't do this earlier,
10753 because it might not be marked as undefined until the
10754 finish_dynamic_symbol routine gets through with it. */
10755 if (sym
.st_shndx
== SHN_UNDEF
10757 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10758 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10761 type
= ELF_ST_TYPE (sym
.st_info
);
10763 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10764 if (type
== STT_GNU_IFUNC
)
10767 if (h
->ref_regular_nonweak
)
10768 bindtype
= STB_GLOBAL
;
10770 bindtype
= STB_WEAK
;
10771 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10774 /* If this is a symbol defined in a dynamic library, don't use the
10775 symbol size from the dynamic library. Relinking an executable
10776 against a new library may introduce gratuitous changes in the
10777 executable's symbols if we keep the size. */
10778 if (sym
.st_shndx
== SHN_UNDEF
10783 /* If a non-weak symbol with non-default visibility is not defined
10784 locally, it is a fatal error. */
10785 if (!bfd_link_relocatable (flinfo
->info
)
10786 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10787 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10788 && h
->root
.type
== bfd_link_hash_undefined
10789 && !h
->def_regular
)
10793 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10794 /* xgettext:c-format */
10795 msg
= _("%pB: protected symbol `%s' isn't defined");
10796 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10797 /* xgettext:c-format */
10798 msg
= _("%pB: internal symbol `%s' isn't defined");
10800 /* xgettext:c-format */
10801 msg
= _("%pB: hidden symbol `%s' isn't defined");
10802 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10803 bfd_set_error (bfd_error_bad_value
);
10804 eoinfo
->failed
= true;
10808 /* If this symbol should be put in the .dynsym section, then put it
10809 there now. We already know the symbol index. We also fill in
10810 the entry in the .hash section. */
10811 if (h
->dynindx
!= -1
10812 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10813 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10814 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10818 /* Since there is no version information in the dynamic string,
10819 if there is no version info in symbol version section, we will
10820 have a run-time problem if not linking executable, referenced
10821 by shared library, or not bound locally. */
10822 if (h
->verinfo
.verdef
== NULL
10823 && (!bfd_link_executable (flinfo
->info
)
10825 || !h
->def_regular
))
10827 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10829 if (p
&& p
[1] != '\0')
10832 /* xgettext:c-format */
10833 (_("%pB: no symbol version section for versioned symbol `%s'"),
10834 flinfo
->output_bfd
, h
->root
.root
.string
);
10835 eoinfo
->failed
= true;
10840 sym
.st_name
= h
->dynstr_index
;
10841 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10842 + h
->dynindx
* bed
->s
->sizeof_sym
);
10843 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10845 eoinfo
->failed
= true;
10849 /* Inform the linker of the addition of this symbol. */
10851 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
10852 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
10854 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10856 if (flinfo
->hash_sec
!= NULL
)
10858 size_t hash_entry_size
;
10859 bfd_byte
*bucketpos
;
10861 size_t bucketcount
;
10864 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10865 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10868 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10869 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10870 + (bucket
+ 2) * hash_entry_size
);
10871 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10872 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10874 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10875 ((bfd_byte
*) flinfo
->hash_sec
->contents
10876 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10879 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10881 Elf_Internal_Versym iversym
;
10882 Elf_External_Versym
*eversym
;
10884 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10886 if (h
->verinfo
.verdef
== NULL
10887 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10888 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10889 iversym
.vs_vers
= 1;
10891 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10895 if (h
->verinfo
.vertree
== NULL
)
10896 iversym
.vs_vers
= 1;
10898 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10899 if (flinfo
->info
->create_default_symver
)
10903 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10904 defined locally. */
10905 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10906 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10908 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10909 eversym
+= h
->dynindx
;
10910 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10914 /* If the symbol is undefined, and we didn't output it to .dynsym,
10915 strip it from .symtab too. Obviously we can't do this for
10916 relocatable output or when needed for --emit-relocs. */
10917 else if (input_sec
== bfd_und_section_ptr
10919 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10920 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10921 && !bfd_link_relocatable (flinfo
->info
))
10924 /* Also strip others that we couldn't earlier due to dynamic symbol
10928 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10931 /* Output a FILE symbol so that following locals are not associated
10932 with the wrong input file. We need one for forced local symbols
10933 if we've seen more than one FILE symbol or when we have exactly
10934 one FILE symbol but global symbols are present in a file other
10935 than the one with the FILE symbol. We also need one if linker
10936 defined symbols are present. In practice these conditions are
10937 always met, so just emit the FILE symbol unconditionally. */
10938 if (eoinfo
->localsyms
10939 && !eoinfo
->file_sym_done
10940 && eoinfo
->flinfo
->filesym_count
!= 0)
10942 Elf_Internal_Sym fsym
;
10944 memset (&fsym
, 0, sizeof (fsym
));
10945 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10946 fsym
.st_shndx
= SHN_ABS
;
10947 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10948 bfd_und_section_ptr
, NULL
))
10951 eoinfo
->file_sym_done
= true;
10954 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10955 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10959 eoinfo
->failed
= true;
10964 else if (h
->indx
== -2)
10970 /* Return TRUE if special handling is done for relocs in SEC against
10971 symbols defined in discarded sections. */
10974 elf_section_ignore_discarded_relocs (asection
*sec
)
10976 const struct elf_backend_data
*bed
;
10978 switch (sec
->sec_info_type
)
10980 case SEC_INFO_TYPE_STABS
:
10981 case SEC_INFO_TYPE_EH_FRAME
:
10982 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10983 case SEC_INFO_TYPE_SFRAME
:
10989 bed
= get_elf_backend_data (sec
->owner
);
10990 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10991 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10997 /* Return a mask saying how ld should treat relocations in SEC against
10998 symbols defined in discarded sections. If this function returns
10999 COMPLAIN set, ld will issue a warning message. If this function
11000 returns PRETEND set, and the discarded section was link-once and the
11001 same size as the kept link-once section, ld will pretend that the
11002 symbol was actually defined in the kept section. Otherwise ld will
11003 zero the reloc (at least that is the intent, but some cooperation by
11004 the target dependent code is needed, particularly for REL targets). */
11007 _bfd_elf_default_action_discarded (asection
*sec
)
11009 const struct elf_backend_data
*bed
;
11010 bed
= get_elf_backend_data (sec
->owner
);
11012 if (sec
->flags
& SEC_DEBUGGING
)
11015 if (strcmp (".eh_frame", sec
->name
) == 0)
11018 if (bed
->elf_backend_can_make_multiple_eh_frame
11019 && strncmp (sec
->name
, ".eh_frame.", 10) == 0)
11022 if (strcmp (".sframe", sec
->name
) == 0)
11025 if (strcmp (".gcc_except_table", sec
->name
) == 0)
11028 return COMPLAIN
| PRETEND
;
11031 /* Find a match between a section and a member of a section group. */
11034 match_group_member (asection
*sec
, asection
*group
,
11035 struct bfd_link_info
*info
)
11037 asection
*first
= elf_next_in_group (group
);
11038 asection
*s
= first
;
11042 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
11045 s
= elf_next_in_group (s
);
11053 /* Check if the kept section of a discarded section SEC can be used
11054 to replace it. Return the replacement if it is OK. Otherwise return
11058 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
11062 kept
= sec
->kept_section
;
11065 if ((kept
->flags
& SEC_GROUP
) != 0)
11066 kept
= match_group_member (sec
, kept
, info
);
11069 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
11070 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
11074 /* Get the real kept section. */
11076 for (next
= kept
->kept_section
;
11078 next
= next
->kept_section
)
11082 sec
->kept_section
= kept
;
11087 /* Link an input file into the linker output file. This function
11088 handles all the sections and relocations of the input file at once.
11089 This is so that we only have to read the local symbols once, and
11090 don't have to keep them in memory. */
11093 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
11095 int (*relocate_section
)
11096 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
11097 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
11099 Elf_Internal_Shdr
*symtab_hdr
;
11100 size_t locsymcount
;
11102 Elf_Internal_Sym
*isymbuf
;
11103 Elf_Internal_Sym
*isym
;
11104 Elf_Internal_Sym
*isymend
;
11106 asection
**ppsection
;
11108 const struct elf_backend_data
*bed
;
11109 struct elf_link_hash_entry
**sym_hashes
;
11110 bfd_size_type address_size
;
11111 bfd_vma r_type_mask
;
11113 bool have_file_sym
= false;
11115 output_bfd
= flinfo
->output_bfd
;
11116 bed
= get_elf_backend_data (output_bfd
);
11117 relocate_section
= bed
->elf_backend_relocate_section
;
11119 /* If this is a dynamic object, we don't want to do anything here:
11120 we don't want the local symbols, and we don't want the section
11122 if ((input_bfd
->flags
& DYNAMIC
) != 0)
11125 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
11126 if (elf_bad_symtab (input_bfd
))
11128 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11133 locsymcount
= symtab_hdr
->sh_info
;
11134 extsymoff
= symtab_hdr
->sh_info
;
11137 /* Enable GNU OSABI features in the output BFD that are used in the input
11139 if (bed
->elf_osabi
== ELFOSABI_NONE
11140 || bed
->elf_osabi
== ELFOSABI_GNU
11141 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
11142 elf_tdata (output_bfd
)->has_gnu_osabi
11143 |= (elf_tdata (input_bfd
)->has_gnu_osabi
11144 & (bfd_link_relocatable (flinfo
->info
)
11145 ? -1 : ~elf_gnu_osabi_retain
));
11147 /* Read the local symbols. */
11148 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11149 if (isymbuf
== NULL
&& locsymcount
!= 0)
11151 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
11152 flinfo
->internal_syms
,
11153 flinfo
->external_syms
,
11154 flinfo
->locsym_shndx
);
11155 if (isymbuf
== NULL
)
11159 /* Find local symbol sections and adjust values of symbols in
11160 SEC_MERGE sections. Write out those local symbols we know are
11161 going into the output file. */
11162 isymend
= PTR_ADD (isymbuf
, locsymcount
);
11163 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
11165 isym
++, pindex
++, ppsection
++)
11169 Elf_Internal_Sym osym
;
11175 if (elf_bad_symtab (input_bfd
))
11177 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
11184 if (isym
->st_shndx
== SHN_UNDEF
)
11185 isec
= bfd_und_section_ptr
;
11186 else if (isym
->st_shndx
== SHN_ABS
)
11187 isec
= bfd_abs_section_ptr
;
11188 else if (isym
->st_shndx
== SHN_COMMON
)
11189 isec
= bfd_com_section_ptr
;
11192 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
11195 /* Don't attempt to output symbols with st_shnx in the
11196 reserved range other than SHN_ABS and SHN_COMMON. */
11197 isec
= bfd_und_section_ptr
;
11199 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
11200 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
11202 _bfd_merged_section_offset (output_bfd
, &isec
,
11203 elf_section_data (isec
)->sec_info
,
11209 /* Don't output the first, undefined, symbol. In fact, don't
11210 output any undefined local symbol. */
11211 if (isec
== bfd_und_section_ptr
)
11214 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
11216 /* We never output section symbols. Instead, we use the
11217 section symbol of the corresponding section in the output
11222 /* If we are stripping all symbols, we don't want to output this
11224 if (flinfo
->info
->strip
== strip_all
)
11227 /* If we are discarding all local symbols, we don't want to
11228 output this one. If we are generating a relocatable output
11229 file, then some of the local symbols may be required by
11230 relocs; we output them below as we discover that they are
11232 if (flinfo
->info
->discard
== discard_all
)
11235 /* If this symbol is defined in a section which we are
11236 discarding, we don't need to keep it. */
11237 if (isym
->st_shndx
< SHN_LORESERVE
11238 && (isec
->output_section
== NULL
11239 || bfd_section_removed_from_list (output_bfd
,
11240 isec
->output_section
)))
11243 /* Get the name of the symbol. */
11244 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
11249 /* See if we are discarding symbols with this name. */
11250 if ((flinfo
->info
->strip
== strip_some
11251 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, false, false)
11253 || (((flinfo
->info
->discard
== discard_sec_merge
11254 && (isec
->flags
& SEC_MERGE
)
11255 && !bfd_link_relocatable (flinfo
->info
))
11256 || flinfo
->info
->discard
== discard_l
)
11257 && bfd_is_local_label_name (input_bfd
, name
)))
11260 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
11262 if (input_bfd
->lto_output
)
11263 /* -flto puts a temp file name here. This means builds
11264 are not reproducible. Discard the symbol. */
11266 have_file_sym
= true;
11267 flinfo
->filesym_count
+= 1;
11269 if (!have_file_sym
)
11271 /* In the absence of debug info, bfd_find_nearest_line uses
11272 FILE symbols to determine the source file for local
11273 function symbols. Provide a FILE symbol here if input
11274 files lack such, so that their symbols won't be
11275 associated with a previous input file. It's not the
11276 source file, but the best we can do. */
11277 const char *filename
;
11278 have_file_sym
= true;
11279 flinfo
->filesym_count
+= 1;
11280 memset (&osym
, 0, sizeof (osym
));
11281 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11282 osym
.st_shndx
= SHN_ABS
;
11283 if (input_bfd
->lto_output
)
11286 filename
= lbasename (bfd_get_filename (input_bfd
));
11287 if (!elf_link_output_symstrtab (flinfo
, filename
, &osym
,
11288 bfd_abs_section_ptr
, NULL
))
11294 /* Adjust the section index for the output file. */
11295 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11296 isec
->output_section
);
11297 if (osym
.st_shndx
== SHN_BAD
)
11300 /* ELF symbols in relocatable files are section relative, but
11301 in executable files they are virtual addresses. Note that
11302 this code assumes that all ELF sections have an associated
11303 BFD section with a reasonable value for output_offset; below
11304 we assume that they also have a reasonable value for
11305 output_section. Any special sections must be set up to meet
11306 these requirements. */
11307 osym
.st_value
+= isec
->output_offset
;
11308 if (!bfd_link_relocatable (flinfo
->info
))
11310 osym
.st_value
+= isec
->output_section
->vma
;
11311 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
11313 /* STT_TLS symbols are relative to PT_TLS segment base. */
11314 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
11315 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
11317 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
11322 indx
= bfd_get_symcount (output_bfd
);
11323 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11330 if (bed
->s
->arch_size
== 32)
11332 r_type_mask
= 0xff;
11338 r_type_mask
= 0xffffffff;
11343 /* Relocate the contents of each section. */
11344 sym_hashes
= elf_sym_hashes (input_bfd
);
11345 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11347 bfd_byte
*contents
;
11349 if (! o
->linker_mark
)
11351 /* This section was omitted from the link. */
11355 if (!flinfo
->info
->resolve_section_groups
11356 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11358 /* Deal with the group signature symbol. */
11359 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11360 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11361 asection
*osec
= o
->output_section
;
11363 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11364 if (symndx
>= locsymcount
11365 || (elf_bad_symtab (input_bfd
)
11366 && flinfo
->sections
[symndx
] == NULL
))
11368 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
11369 while (h
->root
.type
== bfd_link_hash_indirect
11370 || h
->root
.type
== bfd_link_hash_warning
)
11371 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11372 /* Arrange for symbol to be output. */
11374 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11376 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11378 /* We'll use the output section target_index. */
11379 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11380 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11384 if (flinfo
->indices
[symndx
] == -1)
11386 /* Otherwise output the local symbol now. */
11387 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11388 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11393 name
= bfd_elf_string_from_elf_section (input_bfd
,
11394 symtab_hdr
->sh_link
,
11399 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11401 if (sym
.st_shndx
== SHN_BAD
)
11404 sym
.st_value
+= o
->output_offset
;
11406 indx
= bfd_get_symcount (output_bfd
);
11407 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11412 flinfo
->indices
[symndx
] = indx
;
11416 elf_section_data (osec
)->this_hdr
.sh_info
11417 = flinfo
->indices
[symndx
];
11421 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11422 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11425 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11427 /* Section was created by _bfd_elf_link_create_dynamic_sections
11432 /* Get the contents of the section. They have been cached by a
11433 relaxation routine. Note that o is a section in an input
11434 file, so the contents field will not have been set by any of
11435 the routines which work on output files. */
11436 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11438 contents
= elf_section_data (o
)->this_hdr
.contents
;
11439 if (bed
->caches_rawsize
11441 && o
->rawsize
< o
->size
)
11443 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11444 contents
= flinfo
->contents
;
11447 else if (!(o
->flags
& SEC_RELOC
)
11448 && !bed
->elf_backend_write_section
11449 && o
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
11450 /* A MERGE section that has no relocations doesn't need the
11451 contents anymore, they have been recorded earlier. Except
11452 if the backend has special provisions for writing sections. */
11456 contents
= flinfo
->contents
;
11457 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
11461 if ((o
->flags
& SEC_RELOC
) != 0)
11463 Elf_Internal_Rela
*internal_relocs
;
11464 Elf_Internal_Rela
*rel
, *relend
;
11465 int action_discarded
;
11468 /* Get the swapped relocs. */
11470 = _bfd_elf_link_info_read_relocs (input_bfd
, flinfo
->info
, o
,
11471 flinfo
->external_relocs
,
11472 flinfo
->internal_relocs
,
11474 if (internal_relocs
== NULL
11475 && o
->reloc_count
> 0)
11478 action_discarded
= -1;
11479 if (!elf_section_ignore_discarded_relocs (o
))
11480 action_discarded
= (*bed
->action_discarded
) (o
);
11482 /* Run through the relocs evaluating complex reloc symbols and
11483 looking for relocs against symbols from discarded sections
11484 or section symbols from removed link-once sections.
11485 Complain about relocs against discarded sections. Zero
11486 relocs against removed link-once sections. */
11488 rel
= internal_relocs
;
11489 relend
= rel
+ o
->reloc_count
;
11490 for ( ; rel
< relend
; rel
++)
11492 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11493 unsigned int s_type
;
11494 asection
**ps
, *sec
;
11495 struct elf_link_hash_entry
*h
= NULL
;
11496 const char *sym_name
;
11498 if (r_symndx
== STN_UNDEF
)
11501 if (r_symndx
>= locsymcount
11502 || (elf_bad_symtab (input_bfd
)
11503 && flinfo
->sections
[r_symndx
] == NULL
))
11505 h
= sym_hashes
[r_symndx
- extsymoff
];
11507 /* Badly formatted input files can contain relocs that
11508 reference non-existant symbols. Check here so that
11509 we do not seg fault. */
11513 /* xgettext:c-format */
11514 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11515 "that references a non-existent global symbol"),
11516 input_bfd
, (uint64_t) rel
->r_info
, o
);
11517 bfd_set_error (bfd_error_bad_value
);
11521 while (h
->root
.type
== bfd_link_hash_indirect
11522 || h
->root
.type
== bfd_link_hash_warning
)
11523 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11527 /* If a plugin symbol is referenced from a non-IR file,
11528 mark the symbol as undefined. Note that the
11529 linker may attach linker created dynamic sections
11530 to the plugin bfd. Symbols defined in linker
11531 created sections are not plugin symbols. */
11532 if ((h
->root
.non_ir_ref_regular
11533 || h
->root
.non_ir_ref_dynamic
)
11534 && (h
->root
.type
== bfd_link_hash_defined
11535 || h
->root
.type
== bfd_link_hash_defweak
)
11536 && (h
->root
.u
.def
.section
->flags
11537 & SEC_LINKER_CREATED
) == 0
11538 && h
->root
.u
.def
.section
->owner
!= NULL
11539 && (h
->root
.u
.def
.section
->owner
->flags
11540 & BFD_PLUGIN
) != 0)
11542 h
->root
.type
= bfd_link_hash_undefined
;
11543 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11547 if (h
->root
.type
== bfd_link_hash_defined
11548 || h
->root
.type
== bfd_link_hash_defweak
)
11549 ps
= &h
->root
.u
.def
.section
;
11551 sym_name
= h
->root
.root
.string
;
11555 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11557 s_type
= ELF_ST_TYPE (sym
->st_info
);
11558 ps
= &flinfo
->sections
[r_symndx
];
11559 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11563 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11564 && !bfd_link_relocatable (flinfo
->info
))
11567 bfd_vma dot
= (rel
->r_offset
11568 + o
->output_offset
+ o
->output_section
->vma
);
11570 printf ("Encountered a complex symbol!");
11571 printf (" (input_bfd %s, section %s, reloc %ld\n",
11572 bfd_get_filename (input_bfd
), o
->name
,
11573 (long) (rel
- internal_relocs
));
11574 printf (" symbol: idx %8.8lx, name %s\n",
11575 r_symndx
, sym_name
);
11576 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11577 (unsigned long) rel
->r_info
,
11578 (unsigned long) rel
->r_offset
);
11580 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11581 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11584 /* Symbol evaluated OK. Update to absolute value. */
11585 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11590 if (action_discarded
!= -1 && ps
!= NULL
)
11592 /* Complain if the definition comes from a
11593 discarded section. */
11594 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11596 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11597 if (action_discarded
& COMPLAIN
)
11598 (*flinfo
->info
->callbacks
->einfo
)
11599 /* xgettext:c-format */
11600 (_("%X`%s' referenced in section `%pA' of %pB: "
11601 "defined in discarded section `%pA' of %pB\n"),
11602 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11604 /* Try to do the best we can to support buggy old
11605 versions of gcc. Pretend that the symbol is
11606 really defined in the kept linkonce section.
11607 FIXME: This is quite broken. Modifying the
11608 symbol here means we will be changing all later
11609 uses of the symbol, not just in this section. */
11610 if (action_discarded
& PRETEND
)
11614 kept
= _bfd_elf_check_kept_section (sec
,
11626 /* Relocate the section by invoking a back end routine.
11628 The back end routine is responsible for adjusting the
11629 section contents as necessary, and (if using Rela relocs
11630 and generating a relocatable output file) adjusting the
11631 reloc addend as necessary.
11633 The back end routine does not have to worry about setting
11634 the reloc address or the reloc symbol index.
11636 The back end routine is given a pointer to the swapped in
11637 internal symbols, and can access the hash table entries
11638 for the external symbols via elf_sym_hashes (input_bfd).
11640 When generating relocatable output, the back end routine
11641 must handle STB_LOCAL/STT_SECTION symbols specially. The
11642 output symbol is going to be a section symbol
11643 corresponding to the output section, which will require
11644 the addend to be adjusted. */
11646 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11647 input_bfd
, o
, contents
,
11655 || bfd_link_relocatable (flinfo
->info
)
11656 || flinfo
->info
->emitrelocations
)
11658 Elf_Internal_Rela
*irela
;
11659 Elf_Internal_Rela
*irelaend
, *irelamid
;
11660 bfd_vma last_offset
;
11661 struct elf_link_hash_entry
**rel_hash
;
11662 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11663 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11664 unsigned int next_erel
;
11666 struct bfd_elf_section_data
*esdi
, *esdo
;
11668 esdi
= elf_section_data (o
);
11669 esdo
= elf_section_data (o
->output_section
);
11670 rela_normal
= false;
11672 /* Adjust the reloc addresses and symbol indices. */
11674 irela
= internal_relocs
;
11675 irelaend
= irela
+ o
->reloc_count
;
11676 rel_hash
= PTR_ADD (esdo
->rel
.hashes
, esdo
->rel
.count
);
11677 /* We start processing the REL relocs, if any. When we reach
11678 IRELAMID in the loop, we switch to the RELA relocs. */
11680 if (esdi
->rel
.hdr
!= NULL
)
11681 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11682 * bed
->s
->int_rels_per_ext_rel
);
11683 rel_hash_list
= rel_hash
;
11684 rela_hash_list
= NULL
;
11685 last_offset
= o
->output_offset
;
11686 if (!bfd_link_relocatable (flinfo
->info
))
11687 last_offset
+= o
->output_section
->vma
;
11688 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11690 unsigned long r_symndx
;
11692 Elf_Internal_Sym sym
;
11694 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11700 if (irela
== irelamid
)
11702 rel_hash
= PTR_ADD (esdo
->rela
.hashes
, esdo
->rela
.count
);
11703 rela_hash_list
= rel_hash
;
11704 rela_normal
= bed
->rela_normal
;
11707 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11710 if (irela
->r_offset
>= (bfd_vma
) -2)
11712 /* This is a reloc for a deleted entry or somesuch.
11713 Turn it into an R_*_NONE reloc, at the same
11714 offset as the last reloc. elf_eh_frame.c and
11715 bfd_elf_discard_info rely on reloc offsets
11717 irela
->r_offset
= last_offset
;
11719 irela
->r_addend
= 0;
11723 irela
->r_offset
+= o
->output_offset
;
11725 /* Relocs in an executable have to be virtual addresses. */
11726 if (!bfd_link_relocatable (flinfo
->info
))
11727 irela
->r_offset
+= o
->output_section
->vma
;
11729 last_offset
= irela
->r_offset
;
11731 r_symndx
= irela
->r_info
>> r_sym_shift
;
11732 if (r_symndx
== STN_UNDEF
)
11735 if (r_symndx
>= locsymcount
11736 || (elf_bad_symtab (input_bfd
)
11737 && flinfo
->sections
[r_symndx
] == NULL
))
11739 struct elf_link_hash_entry
*rh
;
11740 unsigned long indx
;
11742 /* This is a reloc against a global symbol. We
11743 have not yet output all the local symbols, so
11744 we do not know the symbol index of any global
11745 symbol. We set the rel_hash entry for this
11746 reloc to point to the global hash table entry
11747 for this symbol. The symbol index is then
11748 set at the end of bfd_elf_final_link. */
11749 indx
= r_symndx
- extsymoff
;
11750 rh
= elf_sym_hashes (input_bfd
)[indx
];
11751 while (rh
->root
.type
== bfd_link_hash_indirect
11752 || rh
->root
.type
== bfd_link_hash_warning
)
11753 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11755 /* Setting the index to -2 tells
11756 elf_link_output_extsym that this symbol is
11757 used by a reloc. */
11758 BFD_ASSERT (rh
->indx
< 0);
11765 /* This is a reloc against a local symbol. */
11768 sym
= isymbuf
[r_symndx
];
11769 sec
= flinfo
->sections
[r_symndx
];
11770 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11772 /* I suppose the backend ought to fill in the
11773 section of any STT_SECTION symbol against a
11774 processor specific section. */
11775 r_symndx
= STN_UNDEF
;
11776 if (bfd_is_abs_section (sec
))
11778 else if (sec
== NULL
|| sec
->owner
== NULL
)
11780 bfd_set_error (bfd_error_bad_value
);
11785 asection
*osec
= sec
->output_section
;
11787 /* If we have discarded a section, the output
11788 section will be the absolute section. In
11789 case of discarded SEC_MERGE sections, use
11790 the kept section. relocate_section should
11791 have already handled discarded linkonce
11793 if (bfd_is_abs_section (osec
)
11794 && sec
->kept_section
!= NULL
11795 && sec
->kept_section
->output_section
!= NULL
)
11797 osec
= sec
->kept_section
->output_section
;
11798 irela
->r_addend
-= osec
->vma
;
11801 if (!bfd_is_abs_section (osec
))
11803 r_symndx
= osec
->target_index
;
11804 if (r_symndx
== STN_UNDEF
)
11806 irela
->r_addend
+= osec
->vma
;
11807 osec
= _bfd_nearby_section (output_bfd
, osec
,
11809 irela
->r_addend
-= osec
->vma
;
11810 r_symndx
= osec
->target_index
;
11815 /* Adjust the addend according to where the
11816 section winds up in the output section. */
11818 irela
->r_addend
+= sec
->output_offset
;
11822 if (flinfo
->indices
[r_symndx
] == -1)
11824 unsigned long shlink
;
11829 if (flinfo
->info
->strip
== strip_all
)
11831 /* You can't do ld -r -s. */
11832 bfd_set_error (bfd_error_invalid_operation
);
11836 /* This symbol was skipped earlier, but
11837 since it is needed by a reloc, we
11838 must output it now. */
11839 shlink
= symtab_hdr
->sh_link
;
11840 name
= (bfd_elf_string_from_elf_section
11841 (input_bfd
, shlink
, sym
.st_name
));
11845 osec
= sec
->output_section
;
11847 _bfd_elf_section_from_bfd_section (output_bfd
,
11849 if (sym
.st_shndx
== SHN_BAD
)
11852 sym
.st_value
+= sec
->output_offset
;
11853 if (!bfd_link_relocatable (flinfo
->info
))
11855 sym
.st_value
+= osec
->vma
;
11856 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11858 struct elf_link_hash_table
*htab
11859 = elf_hash_table (flinfo
->info
);
11861 /* STT_TLS symbols are relative to PT_TLS
11863 if (htab
->tls_sec
!= NULL
)
11864 sym
.st_value
-= htab
->tls_sec
->vma
;
11867 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11872 indx
= bfd_get_symcount (output_bfd
);
11873 ret
= elf_link_output_symstrtab (flinfo
, name
,
11879 flinfo
->indices
[r_symndx
] = indx
;
11884 r_symndx
= flinfo
->indices
[r_symndx
];
11887 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11888 | (irela
->r_info
& r_type_mask
));
11891 /* Swap out the relocs. */
11892 input_rel_hdr
= esdi
->rel
.hdr
;
11893 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11895 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11900 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11901 * bed
->s
->int_rels_per_ext_rel
);
11902 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11905 input_rela_hdr
= esdi
->rela
.hdr
;
11906 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11908 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11917 /* Write out the modified section contents. */
11918 if (bed
->elf_backend_write_section
11919 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11922 /* Section written out. */
11924 else switch (o
->sec_info_type
)
11926 case SEC_INFO_TYPE_STABS
:
11927 if (! (_bfd_write_section_stabs
11929 &elf_hash_table (flinfo
->info
)->stab_info
,
11930 o
, &elf_section_data (o
)->sec_info
, contents
)))
11933 case SEC_INFO_TYPE_MERGE
:
11934 if (! _bfd_write_merged_section (output_bfd
, o
,
11935 elf_section_data (o
)->sec_info
))
11938 case SEC_INFO_TYPE_EH_FRAME
:
11940 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11945 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11947 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11953 case SEC_INFO_TYPE_SFRAME
:
11955 /* Merge .sframe sections into the ctf frame encoder
11956 context of the output_bfd's section. The final .sframe
11957 output section will be written out later. */
11958 if (!_bfd_elf_merge_section_sframe (output_bfd
, flinfo
->info
,
11965 if (! (o
->flags
& SEC_EXCLUDE
))
11967 file_ptr offset
= (file_ptr
) o
->output_offset
;
11968 bfd_size_type todo
= o
->size
;
11970 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11972 if ((o
->flags
& SEC_ELF_REVERSE_COPY
)
11973 && o
->size
> address_size
)
11975 /* Reverse-copy input section to output. */
11977 if ((o
->size
& (address_size
- 1)) != 0
11978 || (o
->reloc_count
!= 0
11979 && (o
->size
* bed
->s
->int_rels_per_ext_rel
11980 != o
->reloc_count
* address_size
)))
11983 /* xgettext:c-format */
11984 (_("error: %pB: size of section %pA is not "
11985 "multiple of address size"),
11987 bfd_set_error (bfd_error_bad_value
);
11993 todo
-= address_size
;
11994 if (! bfd_set_section_contents (output_bfd
,
12002 offset
+= address_size
;
12006 else if (! bfd_set_section_contents (output_bfd
,
12020 /* Generate a reloc when linking an ELF file. This is a reloc
12021 requested by the linker, and does not come from any input file. This
12022 is used to build constructor and destructor tables when linking
12026 elf_reloc_link_order (bfd
*output_bfd
,
12027 struct bfd_link_info
*info
,
12028 asection
*output_section
,
12029 struct bfd_link_order
*link_order
)
12031 reloc_howto_type
*howto
;
12035 struct bfd_elf_section_reloc_data
*reldata
;
12036 struct elf_link_hash_entry
**rel_hash_ptr
;
12037 Elf_Internal_Shdr
*rel_hdr
;
12038 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
12039 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
12042 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
12044 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
12047 bfd_set_error (bfd_error_bad_value
);
12051 addend
= link_order
->u
.reloc
.p
->addend
;
12054 reldata
= &esdo
->rel
;
12055 else if (esdo
->rela
.hdr
)
12056 reldata
= &esdo
->rela
;
12063 /* Figure out the symbol index. */
12064 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
12065 if (link_order
->type
== bfd_section_reloc_link_order
)
12067 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
12068 BFD_ASSERT (indx
!= 0);
12069 *rel_hash_ptr
= NULL
;
12073 struct elf_link_hash_entry
*h
;
12075 /* Treat a reloc against a defined symbol as though it were
12076 actually against the section. */
12077 h
= ((struct elf_link_hash_entry
*)
12078 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
12079 link_order
->u
.reloc
.p
->u
.name
,
12080 false, false, true));
12082 && (h
->root
.type
== bfd_link_hash_defined
12083 || h
->root
.type
== bfd_link_hash_defweak
))
12087 section
= h
->root
.u
.def
.section
;
12088 indx
= section
->output_section
->target_index
;
12089 *rel_hash_ptr
= NULL
;
12090 /* It seems that we ought to add the symbol value to the
12091 addend here, but in practice it has already been added
12092 because it was passed to constructor_callback. */
12093 addend
+= section
->output_section
->vma
+ section
->output_offset
;
12095 else if (h
!= NULL
)
12097 /* Setting the index to -2 tells elf_link_output_extsym that
12098 this symbol is used by a reloc. */
12105 (*info
->callbacks
->unattached_reloc
)
12106 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
12111 /* If this is an inplace reloc, we must write the addend into the
12113 if (howto
->partial_inplace
&& addend
!= 0)
12115 bfd_size_type size
;
12116 bfd_reloc_status_type rstat
;
12119 const char *sym_name
;
12120 bfd_size_type octets
;
12122 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
12123 buf
= (bfd_byte
*) bfd_zmalloc (size
);
12124 if (buf
== NULL
&& size
!= 0)
12126 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
12133 case bfd_reloc_outofrange
:
12136 case bfd_reloc_overflow
:
12137 if (link_order
->type
== bfd_section_reloc_link_order
)
12138 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
12140 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
12141 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
12142 howto
->name
, addend
, NULL
, NULL
,
12147 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
12149 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
12156 /* The address of a reloc is relative to the section in a
12157 relocatable file, and is a virtual address in an executable
12159 offset
= link_order
->offset
;
12160 if (! bfd_link_relocatable (info
))
12161 offset
+= output_section
->vma
;
12163 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
12165 irel
[i
].r_offset
= offset
;
12166 irel
[i
].r_info
= 0;
12167 irel
[i
].r_addend
= 0;
12169 if (bed
->s
->arch_size
== 32)
12170 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
12172 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
12174 rel_hdr
= reldata
->hdr
;
12175 erel
= rel_hdr
->contents
;
12176 if (rel_hdr
->sh_type
== SHT_REL
)
12178 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
12179 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
12183 irel
[0].r_addend
= addend
;
12184 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
12185 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
12193 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12194 Returns TRUE upon success, FALSE otherwise. */
12197 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
12201 const struct elf_backend_data
*bed
;
12203 enum bfd_architecture arch
;
12205 asymbol
**sympp
= NULL
;
12209 elf_symbol_type
*osymbuf
;
12212 implib_bfd
= info
->out_implib_bfd
;
12213 bed
= get_elf_backend_data (abfd
);
12215 if (!bfd_set_format (implib_bfd
, bfd_object
))
12218 /* Use flag from executable but make it a relocatable object. */
12219 flags
= bfd_get_file_flags (abfd
);
12220 flags
&= ~HAS_RELOC
;
12221 if (!bfd_set_start_address (implib_bfd
, 0)
12222 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
12225 /* Copy architecture of output file to import library file. */
12226 arch
= bfd_get_arch (abfd
);
12227 mach
= bfd_get_mach (abfd
);
12228 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
12229 && (abfd
->target_defaulted
12230 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
12233 /* Get symbol table size. */
12234 symsize
= bfd_get_symtab_upper_bound (abfd
);
12238 /* Read in the symbol table. */
12239 sympp
= (asymbol
**) bfd_malloc (symsize
);
12243 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
12247 /* Allow the BFD backend to copy any private header data it
12248 understands from the output BFD to the import library BFD. */
12249 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
12252 /* Filter symbols to appear in the import library. */
12253 if (bed
->elf_backend_filter_implib_symbols
)
12254 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
12257 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
12260 bfd_set_error (bfd_error_no_symbols
);
12261 _bfd_error_handler (_("%pB: no symbol found for import library"),
12267 /* Make symbols absolute. */
12268 amt
= symcount
* sizeof (*osymbuf
);
12269 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
12270 if (osymbuf
== NULL
)
12273 for (src_count
= 0; src_count
< symcount
; src_count
++)
12275 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
12276 sizeof (*osymbuf
));
12277 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
12278 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
12279 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
12280 osymbuf
[src_count
].internal_elf_sym
.st_value
=
12281 osymbuf
[src_count
].symbol
.value
;
12282 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
12285 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
12287 /* Allow the BFD backend to copy any private data it understands
12288 from the output BFD to the import library BFD. This is done last
12289 to permit the routine to look at the filtered symbol table. */
12290 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
12293 if (!bfd_close (implib_bfd
))
12304 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
12308 if (flinfo
->symstrtab
!= NULL
)
12309 _bfd_elf_strtab_free (flinfo
->symstrtab
);
12310 free (flinfo
->contents
);
12311 free (flinfo
->external_relocs
);
12312 free (flinfo
->internal_relocs
);
12313 free (flinfo
->external_syms
);
12314 free (flinfo
->locsym_shndx
);
12315 free (flinfo
->internal_syms
);
12316 free (flinfo
->indices
);
12317 free (flinfo
->sections
);
12318 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12319 free (flinfo
->symshndxbuf
);
12320 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12322 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12323 free (esdo
->rel
.hashes
);
12324 free (esdo
->rela
.hashes
);
12328 /* Do the final step of an ELF link. */
12331 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12336 struct elf_final_link_info flinfo
;
12338 struct bfd_link_order
*p
;
12340 bfd_size_type max_contents_size
;
12341 bfd_size_type max_external_reloc_size
;
12342 bfd_size_type max_internal_reloc_count
;
12343 bfd_size_type max_sym_count
;
12344 bfd_size_type max_sym_shndx_count
;
12345 Elf_Internal_Sym elfsym
;
12347 Elf_Internal_Shdr
*symtab_hdr
;
12348 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12349 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12350 struct elf_outext_info eoinfo
;
12352 size_t relativecount
;
12353 size_t relr_entsize
;
12354 asection
*reldyn
= 0;
12356 asection
*attr_section
= NULL
;
12357 bfd_vma attr_size
= 0;
12358 const char *std_attrs_section
;
12359 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12360 bool sections_removed
;
12363 if (!is_elf_hash_table (&htab
->root
))
12366 if (bfd_link_pic (info
))
12367 abfd
->flags
|= DYNAMIC
;
12369 dynamic
= htab
->dynamic_sections_created
;
12370 dynobj
= htab
->dynobj
;
12372 emit_relocs
= (bfd_link_relocatable (info
)
12373 || info
->emitrelocations
);
12375 memset (&flinfo
, 0, sizeof (flinfo
));
12376 flinfo
.info
= info
;
12377 flinfo
.output_bfd
= abfd
;
12378 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12379 if (flinfo
.symstrtab
== NULL
)
12384 flinfo
.hash_sec
= NULL
;
12385 flinfo
.symver_sec
= NULL
;
12389 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12390 /* Note that dynsym_sec can be NULL (on VMS). */
12391 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12392 /* Note that it is OK if symver_sec is NULL. */
12395 if (info
->unique_symbol
12396 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12397 local_hash_newfunc
,
12398 sizeof (struct local_hash_entry
)))
12401 /* The object attributes have been merged. Remove the input
12402 sections from the link, and set the contents of the output
12404 sections_removed
= false;
12405 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12406 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12408 bool remove_section
= false;
12410 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12411 || strcmp (o
->name
, ".gnu.attributes") == 0)
12413 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12415 asection
*input_section
;
12417 if (p
->type
!= bfd_indirect_link_order
)
12419 input_section
= p
->u
.indirect
.section
;
12420 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12421 elf_link_input_bfd ignores this section. */
12422 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12425 attr_size
= bfd_elf_obj_attr_size (abfd
);
12426 bfd_set_section_size (o
, attr_size
);
12427 /* Skip this section later on. */
12428 o
->map_head
.link_order
= NULL
;
12432 remove_section
= true;
12434 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12436 /* Remove empty group section from linker output. */
12437 remove_section
= true;
12439 if (remove_section
)
12441 o
->flags
|= SEC_EXCLUDE
;
12442 bfd_section_list_remove (abfd
, o
);
12443 abfd
->section_count
--;
12444 sections_removed
= true;
12447 if (sections_removed
)
12448 _bfd_fix_excluded_sec_syms (abfd
, info
);
12450 /* Count up the number of relocations we will output for each output
12451 section, so that we know the sizes of the reloc sections. We
12452 also figure out some maximum sizes. */
12453 max_contents_size
= 0;
12454 max_external_reloc_size
= 0;
12455 max_internal_reloc_count
= 0;
12457 max_sym_shndx_count
= 0;
12459 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12461 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12462 o
->reloc_count
= 0;
12464 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12466 unsigned int reloc_count
= 0;
12467 unsigned int additional_reloc_count
= 0;
12468 struct bfd_elf_section_data
*esdi
= NULL
;
12470 if (p
->type
== bfd_section_reloc_link_order
12471 || p
->type
== bfd_symbol_reloc_link_order
)
12473 else if (p
->type
== bfd_indirect_link_order
)
12477 sec
= p
->u
.indirect
.section
;
12479 /* Mark all sections which are to be included in the
12480 link. This will normally be every section. We need
12481 to do this so that we can identify any sections which
12482 the linker has decided to not include. */
12483 sec
->linker_mark
= true;
12485 if (sec
->flags
& SEC_MERGE
)
12488 if (sec
->rawsize
> max_contents_size
)
12489 max_contents_size
= sec
->rawsize
;
12490 if (sec
->size
> max_contents_size
)
12491 max_contents_size
= sec
->size
;
12493 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12494 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12498 /* We are interested in just local symbols, not all
12500 if (elf_bad_symtab (sec
->owner
))
12501 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12502 / bed
->s
->sizeof_sym
);
12504 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12506 if (sym_count
> max_sym_count
)
12507 max_sym_count
= sym_count
;
12509 if (sym_count
> max_sym_shndx_count
12510 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12511 max_sym_shndx_count
= sym_count
;
12513 esdi
= elf_section_data (sec
);
12515 if (esdi
->this_hdr
.sh_type
== SHT_REL
12516 || esdi
->this_hdr
.sh_type
== SHT_RELA
)
12517 /* Some backends use reloc_count in relocation sections
12518 to count particular types of relocs. Of course,
12519 reloc sections themselves can't have relocations. */
12521 else if (emit_relocs
)
12523 reloc_count
= sec
->reloc_count
;
12524 if (bed
->elf_backend_count_additional_relocs
)
12527 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12528 additional_reloc_count
+= c
;
12531 else if (bed
->elf_backend_count_relocs
)
12532 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12534 if ((sec
->flags
& SEC_RELOC
) != 0)
12536 size_t ext_size
= 0;
12538 if (esdi
->rel
.hdr
!= NULL
)
12539 ext_size
= esdi
->rel
.hdr
->sh_size
;
12540 if (esdi
->rela
.hdr
!= NULL
)
12541 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12543 if (ext_size
> max_external_reloc_size
)
12544 max_external_reloc_size
= ext_size
;
12545 if (sec
->reloc_count
> max_internal_reloc_count
)
12546 max_internal_reloc_count
= sec
->reloc_count
;
12551 if (reloc_count
== 0)
12554 reloc_count
+= additional_reloc_count
;
12555 o
->reloc_count
+= reloc_count
;
12557 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12561 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12562 esdo
->rel
.count
+= additional_reloc_count
;
12564 if (esdi
->rela
.hdr
)
12566 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12567 esdo
->rela
.count
+= additional_reloc_count
;
12573 esdo
->rela
.count
+= reloc_count
;
12575 esdo
->rel
.count
+= reloc_count
;
12579 if (o
->reloc_count
> 0)
12580 o
->flags
|= SEC_RELOC
;
12583 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12584 set it (this is probably a bug) and if it is set
12585 assign_section_numbers will create a reloc section. */
12586 o
->flags
&=~ SEC_RELOC
;
12589 /* If the SEC_ALLOC flag is not set, force the section VMA to
12590 zero. This is done in elf_fake_sections as well, but forcing
12591 the VMA to 0 here will ensure that relocs against these
12592 sections are handled correctly. */
12593 if ((o
->flags
& SEC_ALLOC
) == 0
12594 && ! o
->user_set_vma
)
12598 if (! bfd_link_relocatable (info
) && merged
)
12599 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12601 /* Figure out the file positions for everything but the symbol table
12602 and the relocs. We set symcount to force assign_section_numbers
12603 to create a symbol table. */
12604 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12605 BFD_ASSERT (! abfd
->output_has_begun
);
12606 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12609 /* Set sizes, and assign file positions for reloc sections. */
12610 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12612 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12613 if ((o
->flags
& SEC_RELOC
) != 0)
12616 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12620 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12624 /* _bfd_elf_compute_section_file_positions makes temporary use
12625 of target_index. Reset it. */
12626 o
->target_index
= 0;
12628 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12629 to count upwards while actually outputting the relocations. */
12630 esdo
->rel
.count
= 0;
12631 esdo
->rela
.count
= 0;
12633 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12634 && !bfd_section_is_ctf (o
))
12636 /* Cache the section contents so that they can be compressed
12637 later. Use bfd_malloc since it will be freed by
12638 bfd_compress_section_contents. */
12639 unsigned char *contents
= esdo
->this_hdr
.contents
;
12640 if (contents
!= NULL
)
12643 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12644 if (contents
== NULL
)
12646 esdo
->this_hdr
.contents
= contents
;
12650 /* We have now assigned file positions for all the sections except .symtab,
12651 .strtab, and non-loaded reloc and compressed debugging sections. We start
12652 the .symtab section at the current file position, and write directly to it.
12653 We build the .strtab section in memory. */
12654 abfd
->symcount
= 0;
12655 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12656 /* sh_name is set in prep_headers. */
12657 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12658 /* sh_flags, sh_addr and sh_size all start off zero. */
12659 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12660 /* sh_link is set in assign_section_numbers. */
12661 /* sh_info is set below. */
12662 /* sh_offset is set just below. */
12663 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12665 if (max_sym_count
< 20)
12666 max_sym_count
= 20;
12667 htab
->strtabsize
= max_sym_count
;
12668 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12669 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12670 if (htab
->strtab
== NULL
)
12672 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12674 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12675 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12677 if (info
->strip
!= strip_all
|| emit_relocs
)
12679 file_ptr off
= elf_next_file_pos (abfd
);
12681 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
12683 /* Note that at this point elf_next_file_pos (abfd) is
12684 incorrect. We do not yet know the size of the .symtab section.
12685 We correct next_file_pos below, after we do know the size. */
12687 /* Start writing out the symbol table. The first symbol is always a
12689 elfsym
.st_value
= 0;
12690 elfsym
.st_size
= 0;
12691 elfsym
.st_info
= 0;
12692 elfsym
.st_other
= 0;
12693 elfsym
.st_shndx
= SHN_UNDEF
;
12694 elfsym
.st_target_internal
= 0;
12695 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12696 bfd_und_section_ptr
, NULL
) != 1)
12699 /* Output a symbol for each section if asked or they are used for
12700 relocs. These symbols usually have no names. We store the
12701 index of each one in the index field of the section, so that
12702 we can find it again when outputting relocs. */
12704 if (bfd_keep_unused_section_symbols (abfd
) || emit_relocs
)
12706 bool name_local_sections
12707 = (bed
->elf_backend_name_local_section_symbols
12708 && bed
->elf_backend_name_local_section_symbols (abfd
));
12709 const char *name
= NULL
;
12711 elfsym
.st_size
= 0;
12712 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12713 elfsym
.st_other
= 0;
12714 elfsym
.st_value
= 0;
12715 elfsym
.st_target_internal
= 0;
12716 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12718 o
= bfd_section_from_elf_index (abfd
, i
);
12721 o
->target_index
= bfd_get_symcount (abfd
);
12722 elfsym
.st_shndx
= i
;
12723 if (!bfd_link_relocatable (info
))
12724 elfsym
.st_value
= o
->vma
;
12725 if (name_local_sections
)
12727 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12735 /* On some targets like Irix 5 the symbol split between local and global
12736 ones recorded in the sh_info field needs to be done between section
12737 and all other symbols. */
12738 if (bed
->elf_backend_elfsym_local_is_section
12739 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12740 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12742 /* Allocate some memory to hold information read in from the input
12744 if (max_contents_size
!= 0)
12746 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12747 if (flinfo
.contents
== NULL
)
12751 if (max_external_reloc_size
!= 0)
12753 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12754 if (flinfo
.external_relocs
== NULL
)
12758 if (max_internal_reloc_count
!= 0)
12760 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12761 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12762 if (flinfo
.internal_relocs
== NULL
)
12766 if (max_sym_count
!= 0)
12768 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12769 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12770 if (flinfo
.external_syms
== NULL
)
12773 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12774 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12775 if (flinfo
.internal_syms
== NULL
)
12778 amt
= max_sym_count
* sizeof (long);
12779 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12780 if (flinfo
.indices
== NULL
)
12783 amt
= max_sym_count
* sizeof (asection
*);
12784 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12785 if (flinfo
.sections
== NULL
)
12789 if (max_sym_shndx_count
!= 0)
12791 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12792 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12793 if (flinfo
.locsym_shndx
== NULL
)
12799 bfd_vma base
, end
= 0; /* Both bytes. */
12802 for (sec
= htab
->tls_sec
;
12803 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12806 bfd_size_type size
= sec
->size
;
12807 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12810 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12812 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12815 size
= ord
->offset
* opb
+ ord
->size
;
12817 end
= sec
->vma
+ size
/ opb
;
12819 base
= htab
->tls_sec
->vma
;
12820 /* Only align end of TLS section if static TLS doesn't have special
12821 alignment requirements. */
12822 if (bed
->static_tls_alignment
== 1)
12823 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12824 htab
->tls_size
= end
- base
;
12827 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12830 /* Finish relative relocations here after regular symbol processing
12831 is finished if DT_RELR is enabled. */
12832 if (info
->enable_dt_relr
12833 && bed
->finish_relative_relocs
12834 && !bed
->finish_relative_relocs (info
))
12835 info
->callbacks
->einfo
12836 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd
);
12838 /* Since ELF permits relocations to be against local symbols, we
12839 must have the local symbols available when we do the relocations.
12840 Since we would rather only read the local symbols once, and we
12841 would rather not keep them in memory, we handle all the
12842 relocations for a single input file at the same time.
12844 Unfortunately, there is no way to know the total number of local
12845 symbols until we have seen all of them, and the local symbol
12846 indices precede the global symbol indices. This means that when
12847 we are generating relocatable output, and we see a reloc against
12848 a global symbol, we can not know the symbol index until we have
12849 finished examining all the local symbols to see which ones we are
12850 going to output. To deal with this, we keep the relocations in
12851 memory, and don't output them until the end of the link. This is
12852 an unfortunate waste of memory, but I don't see a good way around
12853 it. Fortunately, it only happens when performing a relocatable
12854 link, which is not the common case. FIXME: If keep_memory is set
12855 we could write the relocs out and then read them again; I don't
12856 know how bad the memory loss will be. */
12858 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12859 sub
->output_has_begun
= false;
12860 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12862 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12864 if (p
->type
== bfd_indirect_link_order
12865 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12866 == bfd_target_elf_flavour
)
12867 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12869 if (! sub
->output_has_begun
)
12871 if (! elf_link_input_bfd (&flinfo
, sub
))
12873 sub
->output_has_begun
= true;
12876 else if (p
->type
== bfd_section_reloc_link_order
12877 || p
->type
== bfd_symbol_reloc_link_order
)
12879 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12884 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12886 if (p
->type
== bfd_indirect_link_order
12887 && (bfd_get_flavour (sub
)
12888 == bfd_target_elf_flavour
)
12889 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12890 != bed
->s
->elfclass
))
12892 const char *iclass
, *oclass
;
12894 switch (bed
->s
->elfclass
)
12896 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12897 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12898 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12902 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12904 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12905 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12906 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12910 bfd_set_error (bfd_error_wrong_format
);
12912 /* xgettext:c-format */
12913 (_("%pB: file class %s incompatible with %s"),
12914 sub
, iclass
, oclass
);
12923 /* Free symbol buffer if needed. */
12924 if (!info
->reduce_memory_overheads
)
12926 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12927 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
12929 free (elf_tdata (sub
)->symbuf
);
12930 elf_tdata (sub
)->symbuf
= NULL
;
12936 /* Output any global symbols that got converted to local in a
12937 version script or due to symbol visibility. We do this in a
12938 separate step since ELF requires all local symbols to appear
12939 prior to any global symbols. FIXME: We should only do this if
12940 some global symbols were, in fact, converted to become local.
12941 FIXME: Will this work correctly with the Irix 5 linker? */
12942 eoinfo
.failed
= false;
12943 eoinfo
.flinfo
= &flinfo
;
12944 eoinfo
.localsyms
= true;
12945 eoinfo
.file_sym_done
= false;
12946 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12950 goto return_local_hash_table
;
12953 /* If backend needs to output some local symbols not present in the hash
12954 table, do it now. */
12955 if (bed
->elf_backend_output_arch_local_syms
)
12957 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12958 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
12961 goto return_local_hash_table
;
12965 /* That wrote out all the local symbols. Finish up the symbol table
12966 with the global symbols. Even if we want to strip everything we
12967 can, we still need to deal with those global symbols that got
12968 converted to local in a version script. */
12970 /* The sh_info field records the index of the first non local symbol. */
12971 if (!symtab_hdr
->sh_info
)
12972 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12975 && htab
->dynsym
!= NULL
12976 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12978 Elf_Internal_Sym sym
;
12979 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12981 o
= htab
->dynsym
->output_section
;
12982 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12984 /* Write out the section symbols for the output sections. */
12985 if (bfd_link_pic (info
)
12986 || htab
->is_relocatable_executable
)
12992 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12994 sym
.st_target_internal
= 0;
12996 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
13002 dynindx
= elf_section_data (s
)->dynindx
;
13005 indx
= elf_section_data (s
)->this_idx
;
13006 BFD_ASSERT (indx
> 0);
13007 sym
.st_shndx
= indx
;
13008 if (! check_dynsym (abfd
, &sym
))
13011 goto return_local_hash_table
;
13013 sym
.st_value
= s
->vma
;
13014 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
13016 /* Inform the linker of the addition of this symbol. */
13018 if (info
->callbacks
->ctf_new_dynsym
)
13019 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
13021 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
13025 /* Write out the local dynsyms. */
13026 if (htab
->dynlocal
)
13028 struct elf_link_local_dynamic_entry
*e
;
13029 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
13034 /* Copy the internal symbol and turn off visibility.
13035 Note that we saved a word of storage and overwrote
13036 the original st_name with the dynstr_index. */
13038 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
13039 sym
.st_shndx
= SHN_UNDEF
;
13041 s
= bfd_section_from_elf_index (e
->input_bfd
,
13044 && s
->output_section
!= NULL
13045 && elf_section_data (s
->output_section
) != NULL
)
13048 elf_section_data (s
->output_section
)->this_idx
;
13049 if (! check_dynsym (abfd
, &sym
))
13052 goto return_local_hash_table
;
13054 sym
.st_value
= (s
->output_section
->vma
13056 + e
->isym
.st_value
);
13059 /* Inform the linker of the addition of this symbol. */
13061 if (info
->callbacks
->ctf_new_dynsym
)
13062 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
13064 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
13065 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
13070 /* We get the global symbols from the hash table. */
13071 eoinfo
.failed
= false;
13072 eoinfo
.localsyms
= false;
13073 eoinfo
.flinfo
= &flinfo
;
13074 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
13078 goto return_local_hash_table
;
13081 /* If backend needs to output some symbols not present in the hash
13082 table, do it now. */
13083 if (bed
->elf_backend_output_arch_syms
13084 && (info
->strip
!= strip_all
|| emit_relocs
))
13086 if (! ((*bed
->elf_backend_output_arch_syms
)
13087 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
13090 goto return_local_hash_table
;
13094 /* Finalize the .strtab section. */
13095 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
13097 /* Swap out the .strtab section. */
13098 if (!elf_link_swap_symbols_out (&flinfo
))
13101 goto return_local_hash_table
;
13104 /* Now we know the size of the symtab section. */
13105 if (bfd_get_symcount (abfd
) > 0)
13107 /* Finish up and write out the symbol string table (.strtab)
13109 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
13110 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
13112 if (elf_symtab_shndx_list (abfd
))
13114 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
13116 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
13118 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
13119 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
13120 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
13121 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
13122 symtab_shndx_hdr
->sh_size
= amt
;
13124 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
13127 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
13128 || (bfd_write (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
13131 goto return_local_hash_table
;
13136 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
13137 /* sh_name was set in prep_headers. */
13138 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
13139 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
13140 symstrtab_hdr
->sh_addr
= 0;
13141 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
13142 symstrtab_hdr
->sh_entsize
= 0;
13143 symstrtab_hdr
->sh_link
= 0;
13144 symstrtab_hdr
->sh_info
= 0;
13145 /* sh_offset is set just below. */
13146 symstrtab_hdr
->sh_addralign
= 1;
13148 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
13150 elf_next_file_pos (abfd
) = off
;
13152 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
13153 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
13156 goto return_local_hash_table
;
13160 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
13162 _bfd_error_handler (_("%pB: failed to generate import library"),
13163 info
->out_implib_bfd
);
13165 goto return_local_hash_table
;
13168 /* Adjust the relocs to have the correct symbol indices. */
13169 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
13171 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
13174 if ((o
->flags
& SEC_RELOC
) == 0)
13177 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
13178 if (esdo
->rel
.hdr
!= NULL
13179 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
13182 goto return_local_hash_table
;
13184 if (esdo
->rela
.hdr
!= NULL
13185 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
13188 goto return_local_hash_table
;
13191 /* Set the reloc_count field to 0 to prevent write_relocs from
13192 trying to swap the relocs out itself. */
13193 o
->reloc_count
= 0;
13197 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
13198 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
13201 if (htab
->srelrdyn
!= NULL
13202 && htab
->srelrdyn
->output_section
!= NULL
13203 && htab
->srelrdyn
->size
!= 0)
13205 asection
*s
= htab
->srelrdyn
->output_section
;
13206 relr_entsize
= elf_section_data (s
)->this_hdr
.sh_entsize
;
13207 if (relr_entsize
== 0)
13209 relr_entsize
= bed
->s
->arch_size
/ 8;
13210 elf_section_data (s
)->this_hdr
.sh_entsize
= relr_entsize
;
13214 /* If we are linking against a dynamic object, or generating a
13215 shared library, finish up the dynamic linking information. */
13218 bfd_byte
*dyncon
, *dynconend
;
13220 /* Fix up .dynamic entries. */
13221 o
= bfd_get_linker_section (dynobj
, ".dynamic");
13222 BFD_ASSERT (o
!= NULL
);
13224 dyncon
= o
->contents
;
13225 dynconend
= PTR_ADD (o
->contents
, o
->size
);
13226 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13228 Elf_Internal_Dyn dyn
;
13231 bfd_size_type sh_size
;
13234 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13241 if (relativecount
!= 0)
13243 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
13245 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
13246 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
13248 if (dyn
.d_tag
!= DT_NULL
13249 && dynconend
- dyncon
>= bed
->s
->sizeof_dyn
)
13251 dyn
.d_un
.d_val
= relativecount
;
13257 if (relr_entsize
!= 0)
13259 if (dynconend
- dyncon
>= 3 * bed
->s
->sizeof_dyn
)
13261 asection
*s
= htab
->srelrdyn
;
13262 dyn
.d_tag
= DT_RELR
;
13264 = s
->output_section
->vma
+ s
->output_offset
;
13265 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13266 dyncon
+= bed
->s
->sizeof_dyn
;
13268 dyn
.d_tag
= DT_RELRSZ
;
13269 dyn
.d_un
.d_val
= s
->size
;
13270 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13271 dyncon
+= bed
->s
->sizeof_dyn
;
13273 dyn
.d_tag
= DT_RELRENT
;
13274 dyn
.d_un
.d_val
= relr_entsize
;
13283 name
= info
->init_function
;
13286 name
= info
->fini_function
;
13289 struct elf_link_hash_entry
*h
;
13291 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
13293 && (h
->root
.type
== bfd_link_hash_defined
13294 || h
->root
.type
== bfd_link_hash_defweak
))
13296 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
13297 o
= h
->root
.u
.def
.section
;
13298 if (o
->output_section
!= NULL
)
13299 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
13300 + o
->output_offset
);
13303 /* The symbol is imported from another shared
13304 library and does not apply to this one. */
13305 dyn
.d_un
.d_ptr
= 0;
13312 case DT_PREINIT_ARRAYSZ
:
13313 name
= ".preinit_array";
13315 case DT_INIT_ARRAYSZ
:
13316 name
= ".init_array";
13318 case DT_FINI_ARRAYSZ
:
13319 name
= ".fini_array";
13321 o
= bfd_get_section_by_name (abfd
, name
);
13325 (_("could not find section %s"), name
);
13330 (_("warning: %s section has zero size"), name
);
13331 dyn
.d_un
.d_val
= o
->size
;
13334 case DT_PREINIT_ARRAY
:
13335 name
= ".preinit_array";
13337 case DT_INIT_ARRAY
:
13338 name
= ".init_array";
13340 case DT_FINI_ARRAY
:
13341 name
= ".fini_array";
13343 o
= bfd_get_section_by_name (abfd
, name
);
13350 name
= ".gnu.hash";
13359 name
= ".gnu.version_d";
13362 name
= ".gnu.version_r";
13365 name
= ".gnu.version";
13367 o
= bfd_get_linker_section (dynobj
, name
);
13369 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13372 (_("could not find section %s"), name
);
13375 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13378 (_("warning: section '%s' is being made into a note"), name
);
13379 bfd_set_error (bfd_error_nonrepresentable_section
);
13382 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13389 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13395 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13397 Elf_Internal_Shdr
*hdr
;
13399 hdr
= elf_elfsections (abfd
)[i
];
13400 if (hdr
->sh_type
== type
13401 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13403 sh_size
+= hdr
->sh_size
;
13405 || sh_addr
> hdr
->sh_addr
)
13406 sh_addr
= hdr
->sh_addr
;
13410 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13412 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13414 /* Don't count procedure linkage table relocs in the
13415 overall reloc count. */
13416 sh_size
-= htab
->srelplt
->size
;
13418 /* If the size is zero, make the address zero too.
13419 This is to avoid a glibc bug. If the backend
13420 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13421 zero, then we'll put DT_RELA at the end of
13422 DT_JMPREL. glibc will interpret the end of
13423 DT_RELA matching the end of DT_JMPREL as the
13424 case where DT_RELA includes DT_JMPREL, and for
13425 LD_BIND_NOW will decide that processing DT_RELA
13426 will process the PLT relocs too. Net result:
13427 No PLT relocs applied. */
13430 /* If .rela.plt is the first .rela section, exclude
13431 it from DT_RELA. */
13432 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13433 + htab
->srelplt
->output_offset
) * opb
)
13434 sh_addr
+= htab
->srelplt
->size
;
13437 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13438 dyn
.d_un
.d_val
= sh_size
;
13440 dyn
.d_un
.d_ptr
= sh_addr
;
13443 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13447 /* If we have created any dynamic sections, then output them. */
13448 if (dynobj
!= NULL
)
13450 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13453 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13454 if (bfd_link_textrel_check (info
)
13455 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
13458 bfd_byte
*dyncon
, *dynconend
;
13460 dyncon
= o
->contents
;
13461 dynconend
= o
->contents
+ o
->size
;
13462 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13464 Elf_Internal_Dyn dyn
;
13466 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13468 if (dyn
.d_tag
== DT_TEXTREL
)
13470 if (info
->textrel_check
== textrel_check_error
)
13471 info
->callbacks
->einfo
13472 (_("%P%X: read-only segment has dynamic relocations\n"));
13473 else if (bfd_link_dll (info
))
13474 info
->callbacks
->einfo
13475 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13476 else if (bfd_link_pde (info
))
13477 info
->callbacks
->einfo
13478 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13480 info
->callbacks
->einfo
13481 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13487 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13489 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13491 || o
->output_section
== bfd_abs_section_ptr
)
13493 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13495 /* At this point, we are only interested in sections
13496 created by _bfd_elf_link_create_dynamic_sections. */
13499 if (htab
->stab_info
.stabstr
== o
)
13501 if (htab
->eh_info
.hdr_sec
== o
)
13503 if (strcmp (o
->name
, ".dynstr") != 0)
13505 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13506 * bfd_octets_per_byte (abfd
, o
));
13507 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13508 o
->contents
, octets
, o
->size
))
13513 /* The contents of the .dynstr section are actually in a
13517 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13518 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13519 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13525 if (!info
->resolve_section_groups
)
13527 bool failed
= false;
13529 BFD_ASSERT (bfd_link_relocatable (info
));
13530 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13535 /* If we have optimized stabs strings, output them. */
13536 if (htab
->stab_info
.stabstr
!= NULL
)
13538 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13542 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13545 if (! _bfd_elf_write_section_sframe (abfd
, info
))
13548 if (info
->callbacks
->emit_ctf
)
13549 info
->callbacks
->emit_ctf ();
13551 elf_final_link_free (abfd
, &flinfo
);
13555 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13556 if (contents
== NULL
)
13558 /* Bail out and fail. */
13560 goto return_local_hash_table
;
13562 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13563 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13567 return_local_hash_table
:
13568 if (info
->unique_symbol
)
13569 bfd_hash_table_free (&flinfo
.local_hash_table
);
13573 elf_final_link_free (abfd
, &flinfo
);
13575 goto return_local_hash_table
;
13578 /* Initialize COOKIE for input bfd ABFD. */
13581 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13582 struct bfd_link_info
*info
, bfd
*abfd
)
13584 Elf_Internal_Shdr
*symtab_hdr
;
13585 const struct elf_backend_data
*bed
;
13587 bed
= get_elf_backend_data (abfd
);
13588 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13590 cookie
->abfd
= abfd
;
13591 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13592 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13593 if (cookie
->bad_symtab
)
13595 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13596 cookie
->extsymoff
= 0;
13600 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13601 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13604 if (bed
->s
->arch_size
== 32)
13605 cookie
->r_sym_shift
= 8;
13607 cookie
->r_sym_shift
= 32;
13609 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13610 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13612 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13613 cookie
->locsymcount
, 0,
13615 if (cookie
->locsyms
== NULL
)
13617 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13620 if (_bfd_link_keep_memory (info
) )
13622 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13623 info
->cache_size
+= (cookie
->locsymcount
13624 * sizeof (Elf_External_Sym_Shndx
));
13630 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13633 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13635 Elf_Internal_Shdr
*symtab_hdr
;
13637 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13638 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13639 free (cookie
->locsyms
);
13642 /* Initialize the relocation information in COOKIE for input section SEC
13643 of input bfd ABFD. */
13646 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13647 struct bfd_link_info
*info
, bfd
*abfd
,
13650 if (sec
->reloc_count
== 0)
13652 cookie
->rels
= NULL
;
13653 cookie
->relend
= NULL
;
13657 cookie
->rels
= _bfd_elf_link_info_read_relocs (abfd
, info
, sec
,
13659 _bfd_link_keep_memory (info
));
13660 if (cookie
->rels
== NULL
)
13662 cookie
->rel
= cookie
->rels
;
13663 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13665 cookie
->rel
= cookie
->rels
;
13669 /* Free the memory allocated by init_reloc_cookie_rels,
13673 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13676 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13677 free (cookie
->rels
);
13680 /* Initialize the whole of COOKIE for input section SEC. */
13683 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13684 struct bfd_link_info
*info
,
13687 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13689 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13694 fini_reloc_cookie (cookie
, sec
->owner
);
13699 /* Free the memory allocated by init_reloc_cookie_for_section,
13703 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13706 fini_reloc_cookie_rels (cookie
, sec
);
13707 fini_reloc_cookie (cookie
, sec
->owner
);
13710 /* Garbage collect unused sections. */
13712 /* Default gc_mark_hook. */
13715 _bfd_elf_gc_mark_hook (asection
*sec
,
13716 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13717 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13718 struct elf_link_hash_entry
*h
,
13719 Elf_Internal_Sym
*sym
)
13723 switch (h
->root
.type
)
13725 case bfd_link_hash_defined
:
13726 case bfd_link_hash_defweak
:
13727 return h
->root
.u
.def
.section
;
13729 case bfd_link_hash_common
:
13730 return h
->root
.u
.c
.p
->section
;
13737 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13742 /* Return the debug definition section. */
13745 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13746 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13747 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13748 struct elf_link_hash_entry
*h
,
13749 Elf_Internal_Sym
*sym
)
13753 /* Return the global debug definition section. */
13754 if ((h
->root
.type
== bfd_link_hash_defined
13755 || h
->root
.type
== bfd_link_hash_defweak
)
13756 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13757 return h
->root
.u
.def
.section
;
13761 /* Return the local debug definition section. */
13762 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13764 if (isec
!= NULL
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13771 /* COOKIE->rel describes a relocation against section SEC, which is
13772 a section we've decided to keep. Return the section that contains
13773 the relocation symbol, or NULL if no section contains it. */
13776 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13777 elf_gc_mark_hook_fn gc_mark_hook
,
13778 struct elf_reloc_cookie
*cookie
,
13781 unsigned long r_symndx
;
13782 struct elf_link_hash_entry
*h
, *hw
;
13784 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13785 if (r_symndx
== STN_UNDEF
)
13788 if (r_symndx
>= cookie
->locsymcount
13789 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13793 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13796 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13800 while (h
->root
.type
== bfd_link_hash_indirect
13801 || h
->root
.type
== bfd_link_hash_warning
)
13802 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13804 was_marked
= h
->mark
;
13806 /* Keep all aliases of the symbol too. If an object symbol
13807 needs to be copied into .dynbss then all of its aliases
13808 should be present as dynamic symbols, not just the one used
13809 on the copy relocation. */
13811 while (hw
->is_weakalias
)
13817 if (!was_marked
&& h
->start_stop
&& !h
->root
.ldscript_def
)
13819 if (info
->start_stop_gc
)
13822 /* To work around a glibc bug, mark XXX input sections
13823 when there is a reference to __start_XXX or __stop_XXX
13825 else if (start_stop
!= NULL
)
13827 asection
*s
= h
->u2
.start_stop_section
;
13828 *start_stop
= true;
13833 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13836 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13837 &cookie
->locsyms
[r_symndx
]);
13840 /* COOKIE->rel describes a relocation against section SEC, which is
13841 a section we've decided to keep. Mark the section that contains
13842 the relocation symbol. */
13845 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13847 elf_gc_mark_hook_fn gc_mark_hook
,
13848 struct elf_reloc_cookie
*cookie
)
13851 bool start_stop
= false;
13853 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13854 while (rsec
!= NULL
)
13856 if (!rsec
->gc_mark
)
13858 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13859 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13861 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13866 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13871 /* The mark phase of garbage collection. For a given section, mark
13872 it and any sections in this section's group, and all the sections
13873 which define symbols to which it refers. */
13876 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13878 elf_gc_mark_hook_fn gc_mark_hook
)
13881 asection
*group_sec
, *eh_frame
;
13885 /* Mark all the sections in the group. */
13886 group_sec
= elf_section_data (sec
)->next_in_group
;
13887 if (group_sec
&& !group_sec
->gc_mark
)
13888 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13891 /* Look through the section relocs. */
13893 eh_frame
= elf_eh_frame_section (sec
->owner
);
13894 if ((sec
->flags
& SEC_RELOC
) != 0
13895 && sec
->reloc_count
> 0
13896 && sec
!= eh_frame
)
13898 struct elf_reloc_cookie cookie
;
13900 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13904 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13905 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13910 fini_reloc_cookie_for_section (&cookie
, sec
);
13914 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13916 struct elf_reloc_cookie cookie
;
13918 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13922 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13923 gc_mark_hook
, &cookie
))
13925 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13929 eh_frame
= elf_section_eh_frame_entry (sec
);
13930 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13931 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13937 /* Scan and mark sections in a special or debug section group. */
13940 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13942 /* Point to first section of section group. */
13944 /* Used to iterate the section group. */
13947 bool is_special_grp
= true;
13948 bool is_debug_grp
= true;
13950 /* First scan to see if group contains any section other than debug
13951 and special section. */
13952 ssec
= msec
= elf_next_in_group (grp
);
13955 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13956 is_debug_grp
= false;
13958 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13959 is_special_grp
= false;
13961 msec
= elf_next_in_group (msec
);
13963 while (msec
!= ssec
);
13965 /* If this is a pure debug section group or pure special section group,
13966 keep all sections in this group. */
13967 if (is_debug_grp
|| is_special_grp
)
13972 msec
= elf_next_in_group (msec
);
13974 while (msec
!= ssec
);
13978 /* Keep debug and special sections. */
13981 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13982 elf_gc_mark_hook_fn mark_hook
)
13986 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13990 bool debug_frag_seen
;
13991 bool has_kept_debug_info
;
13993 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13995 isec
= ibfd
->sections
;
13996 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13999 /* Ensure all linker created sections are kept,
14000 see if any other section is already marked,
14001 and note if we have any fragmented debug sections. */
14002 debug_frag_seen
= some_kept
= has_kept_debug_info
= false;
14003 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14005 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
14007 else if (isec
->gc_mark
14008 && (isec
->flags
& SEC_ALLOC
) != 0
14009 && elf_section_type (isec
) != SHT_NOTE
)
14013 /* Since all sections, except for backend specific ones,
14014 have been garbage collected, call mark_hook on this
14015 section if any of its linked-to sections is marked. */
14016 asection
*linked_to_sec
;
14017 for (linked_to_sec
= elf_linked_to_section (isec
);
14018 linked_to_sec
!= NULL
&& !linked_to_sec
->linker_mark
;
14019 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
14021 if (linked_to_sec
->gc_mark
)
14023 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
14027 linked_to_sec
->linker_mark
= 1;
14029 for (linked_to_sec
= elf_linked_to_section (isec
);
14030 linked_to_sec
!= NULL
&& linked_to_sec
->linker_mark
;
14031 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
14032 linked_to_sec
->linker_mark
= 0;
14035 if (!debug_frag_seen
14036 && (isec
->flags
& SEC_DEBUGGING
)
14037 && startswith (isec
->name
, ".debug_line."))
14038 debug_frag_seen
= true;
14039 else if (strcmp (bfd_section_name (isec
),
14040 "__patchable_function_entries") == 0
14041 && elf_linked_to_section (isec
) == NULL
)
14042 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
14043 "need linked-to section "
14044 "for --gc-sections\n"),
14045 isec
->owner
, isec
);
14048 /* If no non-note alloc section in this file will be kept, then
14049 we can toss out the debug and special sections. */
14053 /* Keep debug and special sections like .comment when they are
14054 not part of a group. Also keep section groups that contain
14055 just debug sections or special sections. NB: Sections with
14056 linked-to section has been handled above. */
14057 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14059 if ((isec
->flags
& SEC_GROUP
) != 0)
14060 _bfd_elf_gc_mark_debug_special_section_group (isec
);
14061 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
14062 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
14063 && elf_next_in_group (isec
) == NULL
14064 && elf_linked_to_section (isec
) == NULL
)
14066 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
14067 has_kept_debug_info
= true;
14070 /* Look for CODE sections which are going to be discarded,
14071 and find and discard any fragmented debug sections which
14072 are associated with that code section. */
14073 if (debug_frag_seen
)
14074 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14075 if ((isec
->flags
& SEC_CODE
) != 0
14076 && isec
->gc_mark
== 0)
14081 ilen
= strlen (isec
->name
);
14083 /* Association is determined by the name of the debug
14084 section containing the name of the code section as
14085 a suffix. For example .debug_line.text.foo is a
14086 debug section associated with .text.foo. */
14087 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
14091 if (dsec
->gc_mark
== 0
14092 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
14095 dlen
= strlen (dsec
->name
);
14098 && strncmp (dsec
->name
+ (dlen
- ilen
),
14099 isec
->name
, ilen
) == 0)
14104 /* Mark debug sections referenced by kept debug sections. */
14105 if (has_kept_debug_info
)
14106 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14108 && (isec
->flags
& SEC_DEBUGGING
) != 0)
14109 if (!_bfd_elf_gc_mark (info
, isec
,
14110 elf_gc_mark_debug_section
))
14117 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
14120 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14122 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14126 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14127 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
14128 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14131 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14134 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14136 /* When any section in a section group is kept, we keep all
14137 sections in the section group. If the first member of
14138 the section group is excluded, we will also exclude the
14140 if (o
->flags
& SEC_GROUP
)
14142 asection
*first
= elf_next_in_group (o
);
14143 o
->gc_mark
= first
->gc_mark
;
14149 /* Skip sweeping sections already excluded. */
14150 if (o
->flags
& SEC_EXCLUDE
)
14153 /* Since this is early in the link process, it is simple
14154 to remove a section from the output. */
14155 o
->flags
|= SEC_EXCLUDE
;
14157 if (info
->print_gc_sections
&& o
->size
!= 0)
14158 /* xgettext:c-format */
14159 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14167 /* Propagate collected vtable information. This is called through
14168 elf_link_hash_traverse. */
14171 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
14173 /* Those that are not vtables. */
14175 || h
->u2
.vtable
== NULL
14176 || h
->u2
.vtable
->parent
== NULL
)
14179 /* Those vtables that do not have parents, we cannot merge. */
14180 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
14183 /* If we've already been done, exit. */
14184 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
14187 /* Make sure the parent's table is up to date. */
14188 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
14190 if (h
->u2
.vtable
->used
== NULL
)
14192 /* None of this table's entries were referenced. Re-use the
14194 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14195 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
14202 /* Or the parent's entries into ours. */
14203 cu
= h
->u2
.vtable
->used
;
14205 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14208 const struct elf_backend_data
*bed
;
14209 unsigned int log_file_align
;
14211 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
14212 log_file_align
= bed
->s
->log_file_align
;
14213 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
14227 struct link_info_ok
14229 struct bfd_link_info
*info
;
14234 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
,
14238 bfd_vma hstart
, hend
;
14239 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
14240 const struct elf_backend_data
*bed
;
14241 unsigned int log_file_align
;
14242 struct link_info_ok
*info
= (struct link_info_ok
*) ptr
;
14244 /* Take care of both those symbols that do not describe vtables as
14245 well as those that are not loaded. */
14247 || h
->u2
.vtable
== NULL
14248 || h
->u2
.vtable
->parent
== NULL
)
14251 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
14252 || h
->root
.type
== bfd_link_hash_defweak
);
14254 sec
= h
->root
.u
.def
.section
;
14255 hstart
= h
->root
.u
.def
.value
;
14256 hend
= hstart
+ h
->size
;
14258 relstart
= _bfd_elf_link_info_read_relocs (sec
->owner
, info
->info
,
14259 sec
, NULL
, NULL
, true);
14261 return info
->ok
= false;
14262 bed
= get_elf_backend_data (sec
->owner
);
14263 log_file_align
= bed
->s
->log_file_align
;
14265 relend
= relstart
+ sec
->reloc_count
;
14267 for (rel
= relstart
; rel
< relend
; ++rel
)
14268 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
14270 /* If the entry is in use, do nothing. */
14271 if (h
->u2
.vtable
->used
14272 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
14274 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
14275 if (h
->u2
.vtable
->used
[entry
])
14278 /* Otherwise, kill it. */
14279 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
14285 /* Mark sections containing dynamically referenced symbols. When
14286 building shared libraries, we must assume that any visible symbol is
14290 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
14292 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
14293 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
14295 if ((h
->root
.type
== bfd_link_hash_defined
14296 || h
->root
.type
== bfd_link_hash_defweak
)
14298 || h
->root
.ldscript_def
14299 || !info
->start_stop_gc
)
14300 && ((h
->ref_dynamic
&& !h
->forced_local
)
14301 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
14302 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
14303 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
14304 && (!bfd_link_executable (info
)
14305 || info
->gc_keep_exported
14306 || info
->export_dynamic
14309 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
14310 && (h
->versioned
>= versioned
14311 || !bfd_hide_sym_by_version (info
->version_info
,
14312 h
->root
.root
.string
)))))
14313 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14318 /* Keep all sections containing symbols undefined on the command-line,
14319 and the section containing the entry symbol. */
14322 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
14324 struct bfd_sym_chain
*sym
;
14326 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
14328 struct elf_link_hash_entry
*h
;
14330 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
14331 false, false, false);
14334 && (h
->root
.type
== bfd_link_hash_defined
14335 || h
->root
.type
== bfd_link_hash_defweak
)
14336 && !bfd_is_const_section (h
->root
.u
.def
.section
))
14337 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14342 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
14343 struct bfd_link_info
*info
)
14345 bfd
*ibfd
= info
->input_bfds
;
14347 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14350 struct elf_reloc_cookie cookie
;
14352 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14354 sec
= ibfd
->sections
;
14355 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14358 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
14361 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
14363 if (startswith (bfd_section_name (sec
), ".eh_frame_entry")
14364 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
14366 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
14367 fini_reloc_cookie_rels (&cookie
, sec
);
14374 /* Do mark and sweep of unused sections. */
14377 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14381 elf_gc_mark_hook_fn gc_mark_hook
;
14382 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14383 struct elf_link_hash_table
*htab
;
14384 struct link_info_ok info_ok
;
14386 if (!bed
->can_gc_sections
14387 || !is_elf_hash_table (info
->hash
))
14389 _bfd_error_handler(_("warning: gc-sections option ignored"));
14393 bed
->gc_keep (info
);
14394 htab
= elf_hash_table (info
);
14396 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14397 at the .eh_frame section if we can mark the FDEs individually. */
14398 for (sub
= info
->input_bfds
;
14399 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14400 sub
= sub
->link
.next
)
14403 struct elf_reloc_cookie cookie
;
14405 sec
= sub
->sections
;
14406 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14408 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14409 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
14411 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14412 if (elf_section_data (sec
)->sec_info
14413 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14414 elf_eh_frame_section (sub
) = sec
;
14415 fini_reloc_cookie_for_section (&cookie
, sec
);
14416 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14420 /* Apply transitive closure to the vtable entry usage info. */
14421 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14425 /* Kill the vtable relocations that were not used. */
14426 info_ok
.info
= info
;
14428 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &info_ok
);
14432 /* Mark dynamically referenced symbols. */
14433 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14434 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14436 /* Grovel through relocs to find out who stays ... */
14437 gc_mark_hook
= bed
->gc_mark_hook
;
14438 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14442 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14443 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14444 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14448 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14451 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14452 Also treat note sections as a root, if the section is not part
14453 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14454 well as FINI_ARRAY sections for ld -r. */
14455 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14457 && (o
->flags
& SEC_EXCLUDE
) == 0
14458 && ((o
->flags
& SEC_KEEP
) != 0
14459 || (bfd_link_relocatable (info
)
14460 && ((elf_section_data (o
)->this_hdr
.sh_type
14461 == SHT_PREINIT_ARRAY
)
14462 || (elf_section_data (o
)->this_hdr
.sh_type
14464 || (elf_section_data (o
)->this_hdr
.sh_type
14465 == SHT_FINI_ARRAY
)))
14466 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14467 && elf_next_in_group (o
) == NULL
14468 && elf_linked_to_section (o
) == NULL
)
14469 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14470 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14472 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14477 /* Allow the backend to mark additional target specific sections. */
14478 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14480 /* ... and mark SEC_EXCLUDE for those that go. */
14481 return elf_gc_sweep (abfd
, info
);
14484 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14487 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14489 struct elf_link_hash_entry
*h
,
14492 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14493 struct elf_link_hash_entry
**search
, *child
;
14494 size_t extsymcount
;
14495 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14497 /* The sh_info field of the symtab header tells us where the
14498 external symbols start. We don't care about the local symbols at
14500 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14501 if (!elf_bad_symtab (abfd
))
14502 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14504 sym_hashes
= elf_sym_hashes (abfd
);
14505 sym_hashes_end
= PTR_ADD (sym_hashes
, extsymcount
);
14507 /* Hunt down the child symbol, which is in this section at the same
14508 offset as the relocation. */
14509 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14511 if ((child
= *search
) != NULL
14512 && (child
->root
.type
== bfd_link_hash_defined
14513 || child
->root
.type
== bfd_link_hash_defweak
)
14514 && child
->root
.u
.def
.section
== sec
14515 && child
->root
.u
.def
.value
== offset
)
14519 /* xgettext:c-format */
14520 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14521 abfd
, sec
, (uint64_t) offset
);
14522 bfd_set_error (bfd_error_invalid_operation
);
14526 if (!child
->u2
.vtable
)
14528 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14529 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14530 if (!child
->u2
.vtable
)
14535 /* This *should* only be the absolute section. It could potentially
14536 be that someone has defined a non-global vtable though, which
14537 would be bad. It isn't worth paging in the local symbols to be
14538 sure though; that case should simply be handled by the assembler. */
14540 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14543 child
->u2
.vtable
->parent
= h
;
14548 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14551 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14552 struct elf_link_hash_entry
*h
,
14555 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14556 unsigned int log_file_align
= bed
->s
->log_file_align
;
14560 /* xgettext:c-format */
14561 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14563 bfd_set_error (bfd_error_bad_value
);
14569 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14570 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14575 if (addend
>= h
->u2
.vtable
->size
)
14577 size_t size
, bytes
, file_align
;
14578 bool *ptr
= h
->u2
.vtable
->used
;
14580 /* While the symbol is undefined, we have to be prepared to handle
14582 file_align
= 1 << log_file_align
;
14583 if (h
->root
.type
== bfd_link_hash_undefined
)
14584 size
= addend
+ file_align
;
14588 if (addend
>= size
)
14590 /* Oops! We've got a reference past the defined end of
14591 the table. This is probably a bug -- shall we warn? */
14592 size
= addend
+ file_align
;
14595 size
= (size
+ file_align
- 1) & -file_align
;
14597 /* Allocate one extra entry for use as a "done" flag for the
14598 consolidation pass. */
14599 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bool);
14603 ptr
= (bool *) bfd_realloc (ptr
- 1, bytes
);
14609 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14611 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14615 ptr
= (bool *) bfd_zmalloc (bytes
);
14620 /* And arrange for that done flag to be at index -1. */
14621 h
->u2
.vtable
->used
= ptr
+ 1;
14622 h
->u2
.vtable
->size
= size
;
14625 h
->u2
.vtable
->used
[addend
>> log_file_align
] = true;
14630 /* Map an ELF section header flag to its corresponding string. */
14634 flagword flag_value
;
14635 } elf_flags_to_name_table
;
14637 static const elf_flags_to_name_table elf_flags_to_names
[] =
14639 { "SHF_WRITE", SHF_WRITE
},
14640 { "SHF_ALLOC", SHF_ALLOC
},
14641 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14642 { "SHF_MERGE", SHF_MERGE
},
14643 { "SHF_STRINGS", SHF_STRINGS
},
14644 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14645 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14646 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14647 { "SHF_GROUP", SHF_GROUP
},
14648 { "SHF_TLS", SHF_TLS
},
14649 { "SHF_MASKOS", SHF_MASKOS
},
14650 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14653 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14655 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14656 struct flag_info
*flaginfo
,
14659 const bfd_vma sh_flags
= elf_section_flags (section
);
14661 if (!flaginfo
->flags_initialized
)
14663 bfd
*obfd
= info
->output_bfd
;
14664 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14665 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14667 int without_hex
= 0;
14669 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14672 flagword (*lookup
) (char *);
14674 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14675 if (lookup
!= NULL
)
14677 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14681 if (tf
->with
== with_flags
)
14682 with_hex
|= hexval
;
14683 else if (tf
->with
== without_flags
)
14684 without_hex
|= hexval
;
14689 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14691 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14693 if (tf
->with
== with_flags
)
14694 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14695 else if (tf
->with
== without_flags
)
14696 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14703 info
->callbacks
->einfo
14704 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14708 flaginfo
->flags_initialized
= true;
14709 flaginfo
->only_with_flags
|= with_hex
;
14710 flaginfo
->not_with_flags
|= without_hex
;
14713 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14716 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14722 struct alloc_got_off_arg
{
14724 struct bfd_link_info
*info
;
14727 /* We need a special top-level link routine to convert got reference counts
14728 to real got offsets. */
14731 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14733 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14734 bfd
*obfd
= gofarg
->info
->output_bfd
;
14735 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14737 if (h
->got
.refcount
> 0)
14739 h
->got
.offset
= gofarg
->gotoff
;
14740 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14743 h
->got
.offset
= (bfd_vma
) -1;
14748 /* And an accompanying bit to work out final got entry offsets once
14749 we're done. Should be called from final_link. */
14752 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14753 struct bfd_link_info
*info
)
14756 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14758 struct alloc_got_off_arg gofarg
;
14760 BFD_ASSERT (abfd
== info
->output_bfd
);
14762 if (! is_elf_hash_table (info
->hash
))
14765 /* The GOT offset is relative to the .got section, but the GOT header is
14766 put into the .got.plt section, if the backend uses it. */
14767 if (bed
->want_got_plt
)
14770 gotoff
= bed
->got_header_size
;
14772 /* Do the local .got entries first. */
14773 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14775 bfd_signed_vma
*local_got
;
14776 size_t j
, locsymcount
;
14777 Elf_Internal_Shdr
*symtab_hdr
;
14779 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14782 local_got
= elf_local_got_refcounts (i
);
14786 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14787 if (elf_bad_symtab (i
))
14788 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14790 locsymcount
= symtab_hdr
->sh_info
;
14792 for (j
= 0; j
< locsymcount
; ++j
)
14794 if (local_got
[j
] > 0)
14796 local_got
[j
] = gotoff
;
14797 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14800 local_got
[j
] = (bfd_vma
) -1;
14804 /* Then the global .got entries. .plt refcounts are handled by
14805 adjust_dynamic_symbol */
14806 gofarg
.gotoff
= gotoff
;
14807 gofarg
.info
= info
;
14808 elf_link_hash_traverse (elf_hash_table (info
),
14809 elf_gc_allocate_got_offsets
,
14814 /* Many folk need no more in the way of final link than this, once
14815 got entry reference counting is enabled. */
14818 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14820 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14823 /* Invoke the regular ELF backend linker to do all the work. */
14824 return bfd_elf_final_link (abfd
, info
);
14828 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14830 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14832 if (rcookie
->bad_symtab
)
14833 rcookie
->rel
= rcookie
->rels
;
14835 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14837 unsigned long r_symndx
;
14839 if (! rcookie
->bad_symtab
)
14840 if (rcookie
->rel
->r_offset
> offset
)
14842 if (rcookie
->rel
->r_offset
!= offset
)
14845 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14846 if (r_symndx
== STN_UNDEF
)
14849 if (r_symndx
>= rcookie
->locsymcount
14850 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14852 struct elf_link_hash_entry
*h
;
14854 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14856 while (h
->root
.type
== bfd_link_hash_indirect
14857 || h
->root
.type
== bfd_link_hash_warning
)
14858 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14860 if ((h
->root
.type
== bfd_link_hash_defined
14861 || h
->root
.type
== bfd_link_hash_defweak
)
14862 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14863 || h
->root
.u
.def
.section
->kept_section
!= NULL
14864 || discarded_section (h
->root
.u
.def
.section
)))
14869 /* It's not a relocation against a global symbol,
14870 but it could be a relocation against a local
14871 symbol for a discarded section. */
14873 Elf_Internal_Sym
*isym
;
14875 /* Need to: get the symbol; get the section. */
14876 isym
= &rcookie
->locsyms
[r_symndx
];
14877 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14879 && (isec
->kept_section
!= NULL
14880 || discarded_section (isec
)))
14888 /* Discard unneeded references to discarded sections.
14889 Returns -1 on error, 1 if any section's size was changed, 0 if
14890 nothing changed. This function assumes that the relocations are in
14891 sorted order, which is true for all known assemblers. */
14894 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14896 struct elf_reloc_cookie cookie
;
14901 if (info
->traditional_format
14902 || !is_elf_hash_table (info
->hash
))
14905 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14910 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14913 || i
->reloc_count
== 0
14914 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14918 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14921 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14924 if (_bfd_discard_section_stabs (abfd
, i
,
14925 elf_section_data (i
)->sec_info
,
14926 bfd_elf_reloc_symbol_deleted_p
,
14930 fini_reloc_cookie_for_section (&cookie
, i
);
14935 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14936 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14940 int eh_changed
= 0;
14941 unsigned int eh_alignment
; /* Octets. */
14943 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14949 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14952 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14955 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14956 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14957 bfd_elf_reloc_symbol_deleted_p
,
14961 if (i
->size
!= i
->rawsize
)
14965 fini_reloc_cookie_for_section (&cookie
, i
);
14968 eh_alignment
= ((1 << o
->alignment_power
)
14969 * bfd_octets_per_byte (output_bfd
, o
));
14970 /* Skip over zero terminator, and prevent empty sections from
14971 adding alignment padding at the end. */
14972 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14974 i
->flags
|= SEC_EXCLUDE
;
14975 else if (i
->size
> 4)
14977 /* The last non-empty eh_frame section doesn't need padding. */
14980 /* Any prior sections must pad the last FDE out to the output
14981 section alignment. Otherwise we might have zero padding
14982 between sections, which would be seen as a terminator. */
14983 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14985 /* All but the last zero terminator should have been removed. */
14990 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14991 if (i
->size
!= size
)
14999 elf_link_hash_traverse (elf_hash_table (info
),
15000 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
15003 o
= bfd_get_section_by_name (output_bfd
, ".sframe");
15008 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
15014 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15017 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
15020 if (_bfd_elf_parse_sframe (abfd
, info
, i
, &cookie
))
15022 if (_bfd_elf_discard_section_sframe (i
,
15023 bfd_elf_reloc_symbol_deleted_p
,
15026 if (i
->size
!= i
->rawsize
)
15030 fini_reloc_cookie_for_section (&cookie
, i
);
15032 /* Update the reference to the output .sframe section. Used to
15033 determine later if PT_GNU_SFRAME segment is to be generated. */
15034 if (!_bfd_elf_set_section_sframe (output_bfd
, info
))
15038 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
15040 const struct elf_backend_data
*bed
;
15043 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15045 s
= abfd
->sections
;
15046 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
15049 bed
= get_elf_backend_data (abfd
);
15051 if (bed
->elf_backend_discard_info
!= NULL
)
15053 if (!init_reloc_cookie (&cookie
, info
, abfd
))
15056 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
15059 fini_reloc_cookie (&cookie
, abfd
);
15063 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
15064 _bfd_elf_end_eh_frame_parsing (info
);
15066 if (info
->eh_frame_hdr_type
15067 && !bfd_link_relocatable (info
)
15068 && _bfd_elf_discard_section_eh_frame_hdr (info
))
15075 _bfd_elf_section_already_linked (bfd
*abfd
,
15077 struct bfd_link_info
*info
)
15080 const char *name
, *key
;
15081 struct bfd_section_already_linked
*l
;
15082 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
15084 if (sec
->output_section
== bfd_abs_section_ptr
)
15087 flags
= sec
->flags
;
15089 /* Return if it isn't a linkonce section. A comdat group section
15090 also has SEC_LINK_ONCE set. */
15091 if ((flags
& SEC_LINK_ONCE
) == 0)
15094 /* Don't put group member sections on our list of already linked
15095 sections. They are handled as a group via their group section. */
15096 if (elf_sec_group (sec
) != NULL
)
15099 /* For a SHT_GROUP section, use the group signature as the key. */
15101 if ((flags
& SEC_GROUP
) != 0
15102 && elf_next_in_group (sec
) != NULL
15103 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
15104 key
= elf_group_name (elf_next_in_group (sec
));
15107 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15108 if (startswith (name
, ".gnu.linkonce.")
15109 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
15112 /* Must be a user linkonce section that doesn't follow gcc's
15113 naming convention. In this case we won't be matching
15114 single member groups. */
15118 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
15120 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15122 /* We may have 2 different types of sections on the list: group
15123 sections with a signature of <key> (<key> is some string),
15124 and linkonce sections named .gnu.linkonce.<type>.<key>.
15125 Match like sections. LTO plugin sections are an exception.
15126 They are always named .gnu.linkonce.t.<key> and match either
15127 type of section. */
15128 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
15129 && ((flags
& SEC_GROUP
) != 0
15130 || strcmp (name
, l
->sec
->name
) == 0))
15131 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
15132 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
15134 /* The section has already been linked. See if we should
15135 issue a warning. */
15136 if (!_bfd_handle_already_linked (sec
, l
, info
))
15139 if (flags
& SEC_GROUP
)
15141 asection
*first
= elf_next_in_group (sec
);
15142 asection
*s
= first
;
15146 s
->output_section
= bfd_abs_section_ptr
;
15147 /* Record which group discards it. */
15148 s
->kept_section
= l
->sec
;
15149 s
= elf_next_in_group (s
);
15150 /* These lists are circular. */
15160 /* A single member comdat group section may be discarded by a
15161 linkonce section and vice versa. */
15162 if ((flags
& SEC_GROUP
) != 0)
15164 asection
*first
= elf_next_in_group (sec
);
15166 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
15167 /* Check this single member group against linkonce sections. */
15168 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15169 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15170 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
15172 first
->output_section
= bfd_abs_section_ptr
;
15173 first
->kept_section
= l
->sec
;
15174 sec
->output_section
= bfd_abs_section_ptr
;
15179 /* Check this linkonce section against single member groups. */
15180 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15181 if (l
->sec
->flags
& SEC_GROUP
)
15183 asection
*first
= elf_next_in_group (l
->sec
);
15186 && elf_next_in_group (first
) == first
15187 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
15189 sec
->output_section
= bfd_abs_section_ptr
;
15190 sec
->kept_section
= first
;
15195 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15196 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15197 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15198 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15199 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15200 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15201 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15202 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15203 The reverse order cannot happen as there is never a bfd with only the
15204 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15205 matter as here were are looking only for cross-bfd sections. */
15207 if ((flags
& SEC_GROUP
) == 0 && startswith (name
, ".gnu.linkonce.r."))
15208 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15209 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15210 && startswith (l
->sec
->name
, ".gnu.linkonce.t."))
15212 if (abfd
!= l
->sec
->owner
)
15213 sec
->output_section
= bfd_abs_section_ptr
;
15217 /* This is the first section with this name. Record it. */
15218 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
15219 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
15220 return sec
->output_section
== bfd_abs_section_ptr
;
15224 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
15226 return sym
->st_shndx
== SHN_COMMON
;
15230 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
15236 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
15238 return bfd_com_section_ptr
;
15242 _bfd_elf_default_got_elt_size (bfd
*abfd
,
15243 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
15244 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
15245 bfd
*ibfd ATTRIBUTE_UNUSED
,
15246 unsigned long symndx ATTRIBUTE_UNUSED
)
15248 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15249 return bed
->s
->arch_size
/ 8;
15252 /* Routines to support the creation of dynamic relocs. */
15254 /* Returns the name of the dynamic reloc section associated with SEC. */
15256 static const char *
15257 get_dynamic_reloc_section_name (bfd
* abfd
,
15262 const char *old_name
= bfd_section_name (sec
);
15263 const char *prefix
= is_rela
? ".rela" : ".rel";
15265 if (old_name
== NULL
)
15268 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
15269 sprintf (name
, "%s%s", prefix
, old_name
);
15274 /* Returns the dynamic reloc section associated with SEC.
15275 If necessary compute the name of the dynamic reloc section based
15276 on SEC's name (looked up in ABFD's string table) and the setting
15280 _bfd_elf_get_dynamic_reloc_section (bfd
*abfd
,
15284 asection
*reloc_sec
= elf_section_data (sec
)->sreloc
;
15286 if (reloc_sec
== NULL
)
15288 const char *name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15292 reloc_sec
= bfd_get_linker_section (abfd
, name
);
15294 if (reloc_sec
!= NULL
)
15295 elf_section_data (sec
)->sreloc
= reloc_sec
;
15302 /* Returns the dynamic reloc section associated with SEC. If the
15303 section does not exist it is created and attached to the DYNOBJ
15304 bfd and stored in the SRELOC field of SEC's elf_section_data
15307 ALIGNMENT is the alignment for the newly created section and
15308 IS_RELA defines whether the name should be .rela.<SEC's name>
15309 or .rel.<SEC's name>. The section name is looked up in the
15310 string table associated with ABFD. */
15313 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
15315 unsigned int alignment
,
15319 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
15321 if (reloc_sec
== NULL
)
15323 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15328 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
15330 if (reloc_sec
== NULL
)
15332 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
15333 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
15334 if ((sec
->flags
& SEC_ALLOC
) != 0)
15335 flags
|= SEC_ALLOC
| SEC_LOAD
;
15337 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
15338 if (reloc_sec
!= NULL
)
15340 /* _bfd_elf_get_sec_type_attr chooses a section type by
15341 name. Override as it may be wrong, eg. for a user
15342 section named "auto" we'll get ".relauto" which is
15343 seen to be a .rela section. */
15344 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
15345 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
15350 elf_section_data (sec
)->sreloc
= reloc_sec
;
15356 /* Copy the ELF symbol type and other attributes for a linker script
15357 assignment from HSRC to HDEST. Generally this should be treated as
15358 if we found a strong non-dynamic definition for HDEST (except that
15359 ld ignores multiple definition errors). */
15361 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
15362 struct bfd_link_hash_entry
*hdest
,
15363 struct bfd_link_hash_entry
*hsrc
)
15365 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
15366 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
15367 Elf_Internal_Sym isym
;
15369 ehdest
->type
= ehsrc
->type
;
15370 ehdest
->target_internal
= ehsrc
->target_internal
;
15372 isym
.st_other
= ehsrc
->other
;
15373 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, true, false);
15376 /* Append a RELA relocation REL to section S in BFD. */
15379 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15381 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15382 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
15383 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
15384 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
15387 /* Append a REL relocation REL to section S in BFD. */
15390 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15392 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15393 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
15394 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
15395 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
15398 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15400 struct bfd_link_hash_entry
*
15401 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
15402 const char *symbol
, asection
*sec
)
15404 struct elf_link_hash_entry
*h
;
15406 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
15407 false, false, true);
15408 /* NB: Common symbols will be turned into definition later. */
15410 && !h
->root
.ldscript_def
15411 && (h
->root
.type
== bfd_link_hash_undefined
15412 || h
->root
.type
== bfd_link_hash_undefweak
15413 || ((h
->ref_regular
|| h
->def_dynamic
)
15415 && h
->root
.type
!= bfd_link_hash_common
)))
15417 bool was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15418 h
->verinfo
.verdef
= NULL
;
15419 h
->root
.type
= bfd_link_hash_defined
;
15420 h
->root
.u
.def
.section
= sec
;
15421 h
->root
.u
.def
.value
= 0;
15422 h
->def_regular
= 1;
15423 h
->def_dynamic
= 0;
15425 h
->u2
.start_stop_section
= sec
;
15426 if (symbol
[0] == '.')
15428 /* .startof. and .sizeof. symbols are local. */
15429 const struct elf_backend_data
*bed
;
15430 bed
= get_elf_backend_data (info
->output_bfd
);
15431 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
15435 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15436 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15437 | info
->start_stop_visibility
);
15439 bfd_elf_link_record_dynamic_symbol (info
, h
);
15446 /* Find dynamic relocs for H that apply to read-only sections. */
15449 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15451 struct elf_dyn_relocs
*p
;
15453 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15455 asection
*s
= p
->sec
->output_section
;
15457 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15463 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15464 read-only sections. */
15467 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15471 if (h
->root
.type
== bfd_link_hash_indirect
)
15474 sec
= _bfd_elf_readonly_dynrelocs (h
);
15477 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15479 info
->flags
|= DF_TEXTREL
;
15480 /* xgettext:c-format */
15481 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15482 "in read-only section `%pA'\n"),
15483 sec
->owner
, h
->root
.root
.string
, sec
);
15485 if (bfd_link_textrel_check (info
))
15486 /* xgettext:c-format */
15487 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15488 "in read-only section `%pA'\n"),
15489 sec
->owner
, h
->root
.root
.string
, sec
);
15491 /* Not an error, just cut short the traversal. */
15497 /* Add dynamic tags. */
15500 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15501 bool need_dynamic_reloc
)
15503 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15505 if (htab
->dynamic_sections_created
)
15507 /* Add some entries to the .dynamic section. We fill in the
15508 values later, in finish_dynamic_sections, but we must add
15509 the entries now so that we get the correct size for the
15510 .dynamic section. The DT_DEBUG entry is filled in by the
15511 dynamic linker and used by the debugger. */
15512 #define add_dynamic_entry(TAG, VAL) \
15513 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15515 const struct elf_backend_data
*bed
15516 = get_elf_backend_data (output_bfd
);
15518 if (bfd_link_executable (info
))
15520 if (!add_dynamic_entry (DT_DEBUG
, 0))
15524 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15526 /* DT_PLTGOT is used by prelink even if there is no PLT
15528 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15532 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15534 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15535 || !add_dynamic_entry (DT_PLTREL
,
15536 (bed
->rela_plts_and_copies_p
15537 ? DT_RELA
: DT_REL
))
15538 || !add_dynamic_entry (DT_JMPREL
, 0))
15542 if (htab
->tlsdesc_plt
15543 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15544 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15547 if (need_dynamic_reloc
)
15549 if (bed
->rela_plts_and_copies_p
)
15551 if (!add_dynamic_entry (DT_RELA
, 0)
15552 || !add_dynamic_entry (DT_RELASZ
, 0)
15553 || !add_dynamic_entry (DT_RELAENT
,
15554 bed
->s
->sizeof_rela
))
15559 if (!add_dynamic_entry (DT_REL
, 0)
15560 || !add_dynamic_entry (DT_RELSZ
, 0)
15561 || !add_dynamic_entry (DT_RELENT
,
15562 bed
->s
->sizeof_rel
))
15566 /* If any dynamic relocs apply to a read-only section,
15567 then we need a DT_TEXTREL entry. */
15568 if ((info
->flags
& DF_TEXTREL
) == 0)
15569 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15572 if ((info
->flags
& DF_TEXTREL
) != 0)
15574 if (htab
->ifunc_resolvers
)
15575 info
->callbacks
->einfo
15576 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15577 "may result in a segfault at runtime; recompile with %s\n"),
15578 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15580 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15585 #undef add_dynamic_entry