1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2023 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
43 struct elf_info_failed
45 struct bfd_link_info
*info
;
49 /* This structure is used to pass information to
50 _bfd_elf_link_find_version_dependencies. */
52 struct elf_find_verdep_info
54 /* General link information. */
55 struct bfd_link_info
*info
;
56 /* The number of dependencies. */
58 /* Whether we had a failure. */
62 static bool _bfd_elf_fix_symbol_flags
63 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
66 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
67 unsigned long r_symndx
,
70 if (r_symndx
>= cookie
->locsymcount
71 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
73 struct elf_link_hash_entry
*h
;
75 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
77 while (h
->root
.type
== bfd_link_hash_indirect
78 || h
->root
.type
== bfd_link_hash_warning
)
79 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
81 if ((h
->root
.type
== bfd_link_hash_defined
82 || h
->root
.type
== bfd_link_hash_defweak
)
83 && discarded_section (h
->root
.u
.def
.section
))
84 return h
->root
.u
.def
.section
;
90 /* It's not a relocation against a global symbol,
91 but it could be a relocation against a local
92 symbol for a discarded section. */
94 Elf_Internal_Sym
*isym
;
96 /* Need to: get the symbol; get the section. */
97 isym
= &cookie
->locsyms
[r_symndx
];
98 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
100 && discard
? discarded_section (isec
) : 1)
106 /* Define a symbol in a dynamic linkage section. */
108 struct elf_link_hash_entry
*
109 _bfd_elf_define_linkage_sym (bfd
*abfd
,
110 struct bfd_link_info
*info
,
114 struct elf_link_hash_entry
*h
;
115 struct bfd_link_hash_entry
*bh
;
116 const struct elf_backend_data
*bed
;
118 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, false, false, false);
121 /* Zap symbol defined in an as-needed lib that wasn't linked.
122 This is a symptom of a larger problem: Absolute symbols
123 defined in shared libraries can't be overridden, because we
124 lose the link to the bfd which is via the symbol section. */
125 h
->root
.type
= bfd_link_hash_new
;
131 bed
= get_elf_backend_data (abfd
);
132 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
133 sec
, 0, NULL
, false, bed
->collect
,
136 h
= (struct elf_link_hash_entry
*) bh
;
137 BFD_ASSERT (h
!= NULL
);
140 h
->root
.linker_def
= 1;
141 h
->type
= STT_OBJECT
;
142 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
143 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
145 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
150 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
154 struct elf_link_hash_entry
*h
;
155 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
156 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
158 /* This function may be called more than once. */
159 if (htab
->sgot
!= NULL
)
162 flags
= bed
->dynamic_sec_flags
;
164 s
= bfd_make_section_anyway_with_flags (abfd
,
165 (bed
->rela_plts_and_copies_p
166 ? ".rela.got" : ".rel.got"),
167 (bed
->dynamic_sec_flags
170 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
174 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
176 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
180 if (bed
->want_got_plt
)
182 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
184 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
189 /* The first bit of the global offset table is the header. */
190 s
->size
+= bed
->got_header_size
;
192 if (bed
->want_got_sym
)
194 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
195 (or .got.plt) section. We don't do this in the linker script
196 because we don't want to define the symbol if we are not creating
197 a global offset table. */
198 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
199 "_GLOBAL_OFFSET_TABLE_");
200 elf_hash_table (info
)->hgot
= h
;
208 /* Create a strtab to hold the dynamic symbol names. */
210 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
212 struct elf_link_hash_table
*hash_table
;
214 hash_table
= elf_hash_table (info
);
215 if (hash_table
->dynobj
== NULL
)
217 /* We may not set dynobj, an input file holding linker created
218 dynamic sections to abfd, which may be a dynamic object with
219 its own dynamic sections. We need to find a normal input file
220 to hold linker created sections if possible. */
221 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
225 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
227 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0
228 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
229 && elf_object_id (ibfd
) == elf_hash_table_id (hash_table
)
230 && !((s
= ibfd
->sections
) != NULL
231 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
))
237 hash_table
->dynobj
= abfd
;
240 if (hash_table
->dynstr
== NULL
)
242 hash_table
->dynstr
= _bfd_elf_strtab_init ();
243 if (hash_table
->dynstr
== NULL
)
249 /* Create some sections which will be filled in with dynamic linking
250 information. ABFD is an input file which requires dynamic sections
251 to be created. The dynamic sections take up virtual memory space
252 when the final executable is run, so we need to create them before
253 addresses are assigned to the output sections. We work out the
254 actual contents and size of these sections later. */
257 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
261 const struct elf_backend_data
*bed
;
262 struct elf_link_hash_entry
*h
;
264 if (! is_elf_hash_table (info
->hash
))
267 if (elf_hash_table (info
)->dynamic_sections_created
)
270 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
273 abfd
= elf_hash_table (info
)->dynobj
;
274 bed
= get_elf_backend_data (abfd
);
276 flags
= bed
->dynamic_sec_flags
;
278 /* A dynamically linked executable has a .interp section, but a
279 shared library does not. */
280 if (bfd_link_executable (info
) && !info
->nointerp
)
282 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
283 flags
| SEC_READONLY
);
288 /* Create sections to hold version informations. These are removed
289 if they are not needed. */
290 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
291 flags
| SEC_READONLY
);
293 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
296 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
297 flags
| SEC_READONLY
);
299 || !bfd_set_section_alignment (s
, 1))
302 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
303 flags
| SEC_READONLY
);
305 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
308 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
309 flags
| SEC_READONLY
);
311 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
313 elf_hash_table (info
)->dynsym
= s
;
315 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
316 flags
| SEC_READONLY
);
320 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
322 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
325 /* The special symbol _DYNAMIC is always set to the start of the
326 .dynamic section. We could set _DYNAMIC in a linker script, but we
327 only want to define it if we are, in fact, creating a .dynamic
328 section. We don't want to define it if there is no .dynamic
329 section, since on some ELF platforms the start up code examines it
330 to decide how to initialize the process. */
331 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
332 elf_hash_table (info
)->hdynamic
= h
;
338 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
339 flags
| SEC_READONLY
);
341 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
343 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
346 if (info
->emit_gnu_hash
&& bed
->record_xhash_symbol
== NULL
)
348 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
349 flags
| SEC_READONLY
);
351 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
353 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
354 4 32-bit words followed by variable count of 64-bit words, then
355 variable count of 32-bit words. */
356 if (bed
->s
->arch_size
== 64)
357 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
359 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
362 if (info
->enable_dt_relr
)
364 s
= bfd_make_section_anyway_with_flags (abfd
, ".relr.dyn",
365 (bed
->dynamic_sec_flags
368 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
370 elf_hash_table (info
)->srelrdyn
= s
;
373 /* Let the backend create the rest of the sections. This lets the
374 backend set the right flags. The backend will normally create
375 the .got and .plt sections. */
376 if (bed
->elf_backend_create_dynamic_sections
== NULL
377 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
380 elf_hash_table (info
)->dynamic_sections_created
= true;
385 /* Create dynamic sections when linking against a dynamic object. */
388 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
390 flagword flags
, pltflags
;
391 struct elf_link_hash_entry
*h
;
393 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
394 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
396 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
397 .rel[a].bss sections. */
398 flags
= bed
->dynamic_sec_flags
;
401 if (bed
->plt_not_loaded
)
402 /* We do not clear SEC_ALLOC here because we still want the OS to
403 allocate space for the section; it's just that there's nothing
404 to read in from the object file. */
405 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
407 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
408 if (bed
->plt_readonly
)
409 pltflags
|= SEC_READONLY
;
411 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
413 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
417 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
419 if (bed
->want_plt_sym
)
421 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
422 "_PROCEDURE_LINKAGE_TABLE_");
423 elf_hash_table (info
)->hplt
= h
;
428 s
= bfd_make_section_anyway_with_flags (abfd
,
429 (bed
->rela_plts_and_copies_p
430 ? ".rela.plt" : ".rel.plt"),
431 flags
| SEC_READONLY
);
433 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
437 if (! _bfd_elf_create_got_section (abfd
, info
))
440 if (bed
->want_dynbss
)
442 /* The .dynbss section is a place to put symbols which are defined
443 by dynamic objects, are referenced by regular objects, and are
444 not functions. We must allocate space for them in the process
445 image and use a R_*_COPY reloc to tell the dynamic linker to
446 initialize them at run time. The linker script puts the .dynbss
447 section into the .bss section of the final image. */
448 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
449 SEC_ALLOC
| SEC_LINKER_CREATED
);
454 if (bed
->want_dynrelro
)
456 /* Similarly, but for symbols that were originally in read-only
457 sections. This section doesn't really need to have contents,
458 but make it like other .data.rel.ro sections. */
459 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
466 /* The .rel[a].bss section holds copy relocs. This section is not
467 normally needed. We need to create it here, though, so that the
468 linker will map it to an output section. We can't just create it
469 only if we need it, because we will not know whether we need it
470 until we have seen all the input files, and the first time the
471 main linker code calls BFD after examining all the input files
472 (size_dynamic_sections) the input sections have already been
473 mapped to the output sections. If the section turns out not to
474 be needed, we can discard it later. We will never need this
475 section when generating a shared object, since they do not use
477 if (bfd_link_executable (info
))
479 s
= bfd_make_section_anyway_with_flags (abfd
,
480 (bed
->rela_plts_and_copies_p
481 ? ".rela.bss" : ".rel.bss"),
482 flags
| SEC_READONLY
);
484 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
488 if (bed
->want_dynrelro
)
490 s
= (bfd_make_section_anyway_with_flags
491 (abfd
, (bed
->rela_plts_and_copies_p
492 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
493 flags
| SEC_READONLY
));
495 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
497 htab
->sreldynrelro
= s
;
505 /* Record a new dynamic symbol. We record the dynamic symbols as we
506 read the input files, since we need to have a list of all of them
507 before we can determine the final sizes of the output sections.
508 Note that we may actually call this function even though we are not
509 going to output any dynamic symbols; in some cases we know that a
510 symbol should be in the dynamic symbol table, but only if there is
514 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
515 struct elf_link_hash_entry
*h
)
517 if (h
->dynindx
== -1)
519 struct elf_strtab_hash
*dynstr
;
524 if (h
->root
.type
== bfd_link_hash_defined
525 || h
->root
.type
== bfd_link_hash_defweak
)
527 /* An IR symbol should not be made dynamic. */
528 if (h
->root
.u
.def
.section
!= NULL
529 && h
->root
.u
.def
.section
->owner
!= NULL
530 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
534 /* XXX: The ABI draft says the linker must turn hidden and
535 internal symbols into STB_LOCAL symbols when producing the
536 DSO. However, if ld.so honors st_other in the dynamic table,
537 this would not be necessary. */
538 switch (ELF_ST_VISIBILITY (h
->other
))
542 if (h
->root
.type
!= bfd_link_hash_undefined
543 && h
->root
.type
!= bfd_link_hash_undefweak
)
546 if (!elf_hash_table (info
)->is_relocatable_executable
547 || ((h
->root
.type
== bfd_link_hash_defined
548 || h
->root
.type
== bfd_link_hash_defweak
)
549 && h
->root
.u
.def
.section
->owner
!= NULL
550 && h
->root
.u
.def
.section
->owner
->no_export
)
551 || (h
->root
.type
== bfd_link_hash_common
552 && h
->root
.u
.c
.p
->section
->owner
!= NULL
553 && h
->root
.u
.c
.p
->section
->owner
->no_export
))
561 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
562 ++elf_hash_table (info
)->dynsymcount
;
564 dynstr
= elf_hash_table (info
)->dynstr
;
567 /* Create a strtab to hold the dynamic symbol names. */
568 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
573 /* We don't put any version information in the dynamic string
575 name
= h
->root
.root
.string
;
576 p
= strchr (name
, ELF_VER_CHR
);
578 /* We know that the p points into writable memory. In fact,
579 there are only a few symbols that have read-only names, being
580 those like _GLOBAL_OFFSET_TABLE_ that are created specially
581 by the backends. Most symbols will have names pointing into
582 an ELF string table read from a file, or to objalloc memory. */
585 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
590 if (indx
== (size_t) -1)
592 h
->dynstr_index
= indx
;
598 /* Mark a symbol dynamic. */
601 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
602 struct elf_link_hash_entry
*h
,
603 Elf_Internal_Sym
*sym
)
605 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
607 /* It may be called more than once on the same H. */
608 if(h
->dynamic
|| bfd_link_relocatable (info
))
611 if ((info
->dynamic_data
612 && (h
->type
== STT_OBJECT
613 || h
->type
== STT_COMMON
615 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
616 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
619 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
622 /* NB: If a symbol is made dynamic by --dynamic-list, it has
624 h
->root
.non_ir_ref_dynamic
= 1;
628 /* Record an assignment to a symbol made by a linker script. We need
629 this in case some dynamic object refers to this symbol. */
632 bfd_elf_record_link_assignment (bfd
*output_bfd
,
633 struct bfd_link_info
*info
,
638 struct elf_link_hash_entry
*h
, *hv
;
639 struct elf_link_hash_table
*htab
;
640 const struct elf_backend_data
*bed
;
642 if (!is_elf_hash_table (info
->hash
))
645 htab
= elf_hash_table (info
);
646 h
= elf_link_hash_lookup (htab
, name
, !provide
, true, false);
650 if (h
->root
.type
== bfd_link_hash_warning
)
651 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
653 if (h
->versioned
== unknown
)
655 /* Set versioned if symbol version is unknown. */
656 char *version
= strrchr (name
, ELF_VER_CHR
);
659 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
660 h
->versioned
= versioned_hidden
;
662 h
->versioned
= versioned
;
666 /* Symbols defined in a linker script but not referenced anywhere
667 else will have non_elf set. */
670 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
674 switch (h
->root
.type
)
676 case bfd_link_hash_defined
:
677 case bfd_link_hash_defweak
:
678 case bfd_link_hash_common
:
680 case bfd_link_hash_undefweak
:
681 case bfd_link_hash_undefined
:
682 /* Since we're defining the symbol, don't let it seem to have not
683 been defined. record_dynamic_symbol and size_dynamic_sections
684 may depend on this. */
685 h
->root
.type
= bfd_link_hash_new
;
686 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
687 bfd_link_repair_undef_list (&htab
->root
);
689 case bfd_link_hash_new
:
691 case bfd_link_hash_indirect
:
692 /* We had a versioned symbol in a dynamic library. We make the
693 the versioned symbol point to this one. */
694 bed
= get_elf_backend_data (output_bfd
);
696 while (hv
->root
.type
== bfd_link_hash_indirect
697 || hv
->root
.type
== bfd_link_hash_warning
)
698 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
699 /* We don't need to update h->root.u since linker will set them
701 h
->root
.type
= bfd_link_hash_undefined
;
702 hv
->root
.type
= bfd_link_hash_indirect
;
703 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
704 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
711 /* If this symbol is being provided by the linker script, and it is
712 currently defined by a dynamic object, but not by a regular
713 object, then mark it as undefined so that the generic linker will
714 force the correct value. */
718 h
->root
.type
= bfd_link_hash_undefined
;
720 /* If this symbol is currently defined by a dynamic object, but not
721 by a regular object, then clear out any version information because
722 the symbol will not be associated with the dynamic object any
724 if (h
->def_dynamic
&& !h
->def_regular
)
725 h
->verinfo
.verdef
= NULL
;
727 /* Make sure this symbol is not garbage collected. */
734 bed
= get_elf_backend_data (output_bfd
);
735 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
736 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
737 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
740 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
742 if (!bfd_link_relocatable (info
)
744 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
745 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
750 || bfd_link_dll (info
)
751 || elf_hash_table (info
)->is_relocatable_executable
)
755 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
758 /* If this is a weak defined symbol, and we know a corresponding
759 real symbol from the same dynamic object, make sure the real
760 symbol is also made into a dynamic symbol. */
763 struct elf_link_hash_entry
*def
= weakdef (h
);
765 if (def
->dynindx
== -1
766 && !bfd_elf_link_record_dynamic_symbol (info
, def
))
774 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
775 success, and 2 on a failure caused by attempting to record a symbol
776 in a discarded section, eg. a discarded link-once section symbol. */
779 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
784 struct elf_link_local_dynamic_entry
*entry
;
785 struct elf_link_hash_table
*eht
;
786 struct elf_strtab_hash
*dynstr
;
789 Elf_External_Sym_Shndx eshndx
;
790 char esym
[sizeof (Elf64_External_Sym
)];
792 if (! is_elf_hash_table (info
->hash
))
795 /* See if the entry exists already. */
796 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
797 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
800 amt
= sizeof (*entry
);
801 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
805 /* Go find the symbol, so that we can find it's name. */
806 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
807 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
809 bfd_release (input_bfd
, entry
);
813 if (entry
->isym
.st_shndx
!= SHN_UNDEF
814 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
818 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
819 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
821 /* We can still bfd_release here as nothing has done another
822 bfd_alloc. We can't do this later in this function. */
823 bfd_release (input_bfd
, entry
);
828 name
= (bfd_elf_string_from_elf_section
829 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
830 entry
->isym
.st_name
));
832 dynstr
= elf_hash_table (info
)->dynstr
;
835 /* Create a strtab to hold the dynamic symbol names. */
836 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
841 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, false);
842 if (dynstr_index
== (size_t) -1)
844 entry
->isym
.st_name
= dynstr_index
;
846 eht
= elf_hash_table (info
);
848 entry
->next
= eht
->dynlocal
;
849 eht
->dynlocal
= entry
;
850 entry
->input_bfd
= input_bfd
;
851 entry
->input_indx
= input_indx
;
854 /* Whatever binding the symbol had before, it's now local. */
856 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
858 /* The dynindx will be set at the end of size_dynamic_sections. */
863 /* Return the dynindex of a local dynamic symbol. */
866 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
870 struct elf_link_local_dynamic_entry
*e
;
872 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
873 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
878 /* This function is used to renumber the dynamic symbols, if some of
879 them are removed because they are marked as local. This is called
880 via elf_link_hash_traverse. */
883 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
886 size_t *count
= (size_t *) data
;
891 if (h
->dynindx
!= -1)
892 h
->dynindx
= ++(*count
);
898 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
899 STB_LOCAL binding. */
902 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
905 size_t *count
= (size_t *) data
;
907 if (!h
->forced_local
)
910 if (h
->dynindx
!= -1)
911 h
->dynindx
= ++(*count
);
916 /* Return true if the dynamic symbol for a given section should be
917 omitted when creating a shared library. */
919 _bfd_elf_omit_section_dynsym_default (bfd
*output_bfd ATTRIBUTE_UNUSED
,
920 struct bfd_link_info
*info
,
923 struct elf_link_hash_table
*htab
;
926 switch (elf_section_data (p
)->this_hdr
.sh_type
)
930 /* If sh_type is yet undecided, assume it could be
931 SHT_PROGBITS/SHT_NOBITS. */
933 htab
= elf_hash_table (info
);
934 if (htab
->text_index_section
!= NULL
)
935 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
937 return (htab
->dynobj
!= NULL
938 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
939 && ip
->output_section
== p
);
941 /* There shouldn't be section relative relocations
942 against any other section. */
949 _bfd_elf_omit_section_dynsym_all
950 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
951 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
952 asection
*p ATTRIBUTE_UNUSED
)
957 /* Assign dynsym indices. In a shared library we generate a section
958 symbol for each output section, which come first. Next come symbols
959 which have been forced to local binding. Then all of the back-end
960 allocated local dynamic syms, followed by the rest of the global
961 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
962 (This prevents the early call before elf_backend_init_index_section
963 and strip_excluded_output_sections setting dynindx for sections
964 that are stripped.) */
967 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
968 struct bfd_link_info
*info
,
969 unsigned long *section_sym_count
)
971 unsigned long dynsymcount
= 0;
972 bool do_sec
= section_sym_count
!= NULL
;
974 if (bfd_link_pic (info
)
975 || elf_hash_table (info
)->is_relocatable_executable
)
977 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
979 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
980 if ((p
->flags
& SEC_EXCLUDE
) == 0
981 && (p
->flags
& SEC_ALLOC
) != 0
982 && elf_hash_table (info
)->dynamic_relocs
983 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
987 elf_section_data (p
)->dynindx
= dynsymcount
;
990 elf_section_data (p
)->dynindx
= 0;
993 *section_sym_count
= dynsymcount
;
995 elf_link_hash_traverse (elf_hash_table (info
),
996 elf_link_renumber_local_hash_table_dynsyms
,
999 if (elf_hash_table (info
)->dynlocal
)
1001 struct elf_link_local_dynamic_entry
*p
;
1002 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
1003 p
->dynindx
= ++dynsymcount
;
1005 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
1007 elf_link_hash_traverse (elf_hash_table (info
),
1008 elf_link_renumber_hash_table_dynsyms
,
1011 /* There is an unused NULL entry at the head of the table which we
1012 must account for in our count even if the table is empty since it
1013 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1014 .dynamic section. */
1017 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
1021 /* Merge st_other field. */
1024 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
1025 unsigned int st_other
, asection
*sec
,
1026 bool definition
, bool dynamic
)
1028 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1030 /* If st_other has a processor-specific meaning, specific
1031 code might be needed here. */
1032 if (bed
->elf_backend_merge_symbol_attribute
)
1033 (*bed
->elf_backend_merge_symbol_attribute
) (h
, st_other
, definition
,
1038 unsigned symvis
= ELF_ST_VISIBILITY (st_other
);
1039 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
1041 /* Keep the most constraining visibility. Leave the remainder
1042 of the st_other field to elf_backend_merge_symbol_attribute. */
1043 if (symvis
- 1 < hvis
- 1)
1044 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
1047 && ELF_ST_VISIBILITY (st_other
) != STV_DEFAULT
1048 && (sec
->flags
& SEC_READONLY
) == 0)
1049 h
->protected_def
= 1;
1052 /* This function is called when we want to merge a new symbol with an
1053 existing symbol. It handles the various cases which arise when we
1054 find a definition in a dynamic object, or when there is already a
1055 definition in a dynamic object. The new symbol is described by
1056 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1057 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1058 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1059 of an old common symbol. We set OVERRIDE if the old symbol is
1060 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1061 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1062 to change. By OK to change, we mean that we shouldn't warn if the
1063 type or size does change. */
1066 _bfd_elf_merge_symbol (bfd
*abfd
,
1067 struct bfd_link_info
*info
,
1069 Elf_Internal_Sym
*sym
,
1072 struct elf_link_hash_entry
**sym_hash
,
1075 unsigned int *pold_alignment
,
1078 bool *type_change_ok
,
1079 bool *size_change_ok
,
1082 asection
*sec
, *oldsec
;
1083 struct elf_link_hash_entry
*h
;
1084 struct elf_link_hash_entry
*hi
;
1085 struct elf_link_hash_entry
*flip
;
1088 bool newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1089 bool newweak
, oldweak
, newfunc
, oldfunc
;
1090 const struct elf_backend_data
*bed
;
1092 bool default_sym
= *matched
;
1093 struct elf_link_hash_table
*htab
;
1099 bind
= ELF_ST_BIND (sym
->st_info
);
1101 if (! bfd_is_und_section (sec
))
1102 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
1104 h
= ((struct elf_link_hash_entry
*)
1105 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
1110 bed
= get_elf_backend_data (abfd
);
1112 /* NEW_VERSION is the symbol version of the new symbol. */
1113 if (h
->versioned
!= unversioned
)
1115 /* Symbol version is unknown or versioned. */
1116 new_version
= strrchr (name
, ELF_VER_CHR
);
1119 if (h
->versioned
== unknown
)
1121 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1122 h
->versioned
= versioned_hidden
;
1124 h
->versioned
= versioned
;
1127 if (new_version
[0] == '\0')
1131 h
->versioned
= unversioned
;
1136 /* For merging, we only care about real symbols. But we need to make
1137 sure that indirect symbol dynamic flags are updated. */
1139 while (h
->root
.type
== bfd_link_hash_indirect
1140 || h
->root
.type
== bfd_link_hash_warning
)
1141 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1145 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1149 /* OLD_HIDDEN is true if the existing symbol is only visible
1150 to the symbol with the same symbol version. NEW_HIDDEN is
1151 true if the new symbol is only visible to the symbol with
1152 the same symbol version. */
1153 bool old_hidden
= h
->versioned
== versioned_hidden
;
1154 bool new_hidden
= hi
->versioned
== versioned_hidden
;
1155 if (!old_hidden
&& !new_hidden
)
1156 /* The new symbol matches the existing symbol if both
1161 /* OLD_VERSION is the symbol version of the existing
1165 if (h
->versioned
>= versioned
)
1166 old_version
= strrchr (h
->root
.root
.string
,
1171 /* The new symbol matches the existing symbol if they
1172 have the same symbol version. */
1173 *matched
= (old_version
== new_version
1174 || (old_version
!= NULL
1175 && new_version
!= NULL
1176 && strcmp (old_version
, new_version
) == 0));
1181 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1186 switch (h
->root
.type
)
1191 case bfd_link_hash_undefined
:
1192 case bfd_link_hash_undefweak
:
1193 oldbfd
= h
->root
.u
.undef
.abfd
;
1196 case bfd_link_hash_defined
:
1197 case bfd_link_hash_defweak
:
1198 oldbfd
= h
->root
.u
.def
.section
->owner
;
1199 oldsec
= h
->root
.u
.def
.section
;
1202 case bfd_link_hash_common
:
1203 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1204 oldsec
= h
->root
.u
.c
.p
->section
;
1206 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1209 if (poldbfd
&& *poldbfd
== NULL
)
1212 /* Differentiate strong and weak symbols. */
1213 newweak
= bind
== STB_WEAK
;
1214 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1215 || h
->root
.type
== bfd_link_hash_undefweak
);
1217 *pold_weak
= oldweak
;
1219 /* We have to check it for every instance since the first few may be
1220 references and not all compilers emit symbol type for undefined
1222 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1224 htab
= elf_hash_table (info
);
1226 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1227 respectively, is from a dynamic object. */
1229 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1231 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1232 syms and defined syms in dynamic libraries respectively.
1233 ref_dynamic on the other hand can be set for a symbol defined in
1234 a dynamic library, and def_dynamic may not be set; When the
1235 definition in a dynamic lib is overridden by a definition in the
1236 executable use of the symbol in the dynamic lib becomes a
1237 reference to the executable symbol. */
1240 if (bfd_is_und_section (sec
))
1242 if (bind
!= STB_WEAK
)
1244 h
->ref_dynamic_nonweak
= 1;
1245 hi
->ref_dynamic_nonweak
= 1;
1250 /* Update the existing symbol only if they match. */
1253 hi
->dynamic_def
= 1;
1257 /* If we just created the symbol, mark it as being an ELF symbol.
1258 Other than that, there is nothing to do--there is no merge issue
1259 with a newly defined symbol--so we just return. */
1261 if (h
->root
.type
== bfd_link_hash_new
)
1267 /* In cases involving weak versioned symbols, we may wind up trying
1268 to merge a symbol with itself. Catch that here, to avoid the
1269 confusion that results if we try to override a symbol with
1270 itself. The additional tests catch cases like
1271 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1272 dynamic object, which we do want to handle here. */
1274 && (newweak
|| oldweak
)
1275 && ((abfd
->flags
& DYNAMIC
) == 0
1276 || !h
->def_regular
))
1281 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1282 else if (oldsec
!= NULL
)
1284 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1285 indices used by MIPS ELF. */
1286 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1289 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1290 if (!htab
->handling_dt_needed
1292 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
))
1294 if (newdyn
!= olddyn
)
1296 /* Handle a case where plugin_notice won't be called and thus
1297 won't set the non_ir_ref flags on the first pass over
1299 h
->root
.non_ir_ref_dynamic
= true;
1300 hi
->root
.non_ir_ref_dynamic
= true;
1302 else if ((oldbfd
->flags
& BFD_PLUGIN
) != 0
1303 && hi
->root
.type
== bfd_link_hash_indirect
)
1305 /* Change indirect symbol from IR to undefined. */
1306 hi
->root
.type
= bfd_link_hash_undefined
;
1307 hi
->root
.u
.undef
.abfd
= oldbfd
;
1311 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1312 respectively, appear to be a definition rather than reference. */
1314 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1316 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1317 && h
->root
.type
!= bfd_link_hash_undefweak
1318 && h
->root
.type
!= bfd_link_hash_common
);
1320 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1321 respectively, appear to be a function. */
1323 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1324 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1326 oldfunc
= (h
->type
!= STT_NOTYPE
1327 && bed
->is_function_type (h
->type
));
1329 if (!(newfunc
&& oldfunc
)
1330 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1331 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1332 && h
->type
!= STT_NOTYPE
1333 && (newdef
|| bfd_is_com_section (sec
))
1334 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1336 /* If creating a default indirect symbol ("foo" or "foo@") from
1337 a dynamic versioned definition ("foo@@") skip doing so if
1338 there is an existing regular definition with a different
1339 type. We don't want, for example, a "time" variable in the
1340 executable overriding a "time" function in a shared library. */
1348 /* When adding a symbol from a regular object file after we have
1349 created indirect symbols, undo the indirection and any
1356 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1357 h
->forced_local
= 0;
1361 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1363 h
->root
.type
= bfd_link_hash_undefined
;
1364 h
->root
.u
.undef
.abfd
= abfd
;
1368 h
->root
.type
= bfd_link_hash_new
;
1369 h
->root
.u
.undef
.abfd
= NULL
;
1375 /* Check TLS symbols. We don't check undefined symbols introduced
1376 by "ld -u" which have no type (and oldbfd NULL), and we don't
1377 check symbols from plugins because they also have no type. */
1379 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1380 && (abfd
->flags
& BFD_PLUGIN
) == 0
1381 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1382 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1386 asection
*ntsec
, *tsec
;
1388 if (h
->type
== STT_TLS
)
1409 /* xgettext:c-format */
1410 (_("%s: TLS definition in %pB section %pA "
1411 "mismatches non-TLS definition in %pB section %pA"),
1412 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1413 else if (!tdef
&& !ntdef
)
1415 /* xgettext:c-format */
1416 (_("%s: TLS reference in %pB "
1417 "mismatches non-TLS reference in %pB"),
1418 h
->root
.root
.string
, tbfd
, ntbfd
);
1421 /* xgettext:c-format */
1422 (_("%s: TLS definition in %pB section %pA "
1423 "mismatches non-TLS reference in %pB"),
1424 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1427 /* xgettext:c-format */
1428 (_("%s: TLS reference in %pB "
1429 "mismatches non-TLS definition in %pB section %pA"),
1430 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1432 bfd_set_error (bfd_error_bad_value
);
1436 /* If the old symbol has non-default visibility, we ignore the new
1437 definition from a dynamic object. */
1439 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1440 && !bfd_is_und_section (sec
))
1443 /* Make sure this symbol is dynamic. */
1445 hi
->ref_dynamic
= 1;
1446 /* A protected symbol has external availability. Make sure it is
1447 recorded as dynamic.
1449 FIXME: Should we check type and size for protected symbol? */
1450 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1451 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1456 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1459 /* If the new symbol with non-default visibility comes from a
1460 relocatable file and the old definition comes from a dynamic
1461 object, we remove the old definition. */
1462 if (hi
->root
.type
== bfd_link_hash_indirect
)
1464 /* Handle the case where the old dynamic definition is
1465 default versioned. We need to copy the symbol info from
1466 the symbol with default version to the normal one if it
1467 was referenced before. */
1470 hi
->root
.type
= h
->root
.type
;
1471 h
->root
.type
= bfd_link_hash_indirect
;
1472 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1474 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1475 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1477 /* If the new symbol is hidden or internal, completely undo
1478 any dynamic link state. */
1479 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1480 h
->forced_local
= 0;
1487 /* FIXME: Should we check type and size for protected symbol? */
1497 /* If the old symbol was undefined before, then it will still be
1498 on the undefs list. If the new symbol is undefined or
1499 common, we can't make it bfd_link_hash_new here, because new
1500 undefined or common symbols will be added to the undefs list
1501 by _bfd_generic_link_add_one_symbol. Symbols may not be
1502 added twice to the undefs list. Also, if the new symbol is
1503 undefweak then we don't want to lose the strong undef. */
1504 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1506 h
->root
.type
= bfd_link_hash_undefined
;
1507 h
->root
.u
.undef
.abfd
= abfd
;
1511 h
->root
.type
= bfd_link_hash_new
;
1512 h
->root
.u
.undef
.abfd
= NULL
;
1515 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1517 /* If the new symbol is hidden or internal, completely undo
1518 any dynamic link state. */
1519 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1520 h
->forced_local
= 0;
1526 /* FIXME: Should we check type and size for protected symbol? */
1532 /* If a new weak symbol definition comes from a regular file and the
1533 old symbol comes from a dynamic library, we treat the new one as
1534 strong. Similarly, an old weak symbol definition from a regular
1535 file is treated as strong when the new symbol comes from a dynamic
1536 library. Further, an old weak symbol from a dynamic library is
1537 treated as strong if the new symbol is from a dynamic library.
1538 This reflects the way glibc's ld.so works.
1540 Also allow a weak symbol to override a linker script symbol
1541 defined by an early pass over the script. This is done so the
1542 linker knows the symbol is defined in an object file, for the
1543 DEFINED script function.
1545 Do this before setting *type_change_ok or *size_change_ok so that
1546 we warn properly when dynamic library symbols are overridden. */
1548 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1550 if (olddef
&& newdyn
)
1553 /* Allow changes between different types of function symbol. */
1554 if (newfunc
&& oldfunc
)
1555 *type_change_ok
= true;
1557 /* It's OK to change the type if either the existing symbol or the
1558 new symbol is weak. A type change is also OK if the old symbol
1559 is undefined and the new symbol is defined. */
1564 && h
->root
.type
== bfd_link_hash_undefined
))
1565 *type_change_ok
= true;
1567 /* It's OK to change the size if either the existing symbol or the
1568 new symbol is weak, or if the old symbol is undefined. */
1571 || h
->root
.type
== bfd_link_hash_undefined
)
1572 *size_change_ok
= true;
1574 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1575 symbol, respectively, appears to be a common symbol in a dynamic
1576 object. If a symbol appears in an uninitialized section, and is
1577 not weak, and is not a function, then it may be a common symbol
1578 which was resolved when the dynamic object was created. We want
1579 to treat such symbols specially, because they raise special
1580 considerations when setting the symbol size: if the symbol
1581 appears as a common symbol in a regular object, and the size in
1582 the regular object is larger, we must make sure that we use the
1583 larger size. This problematic case can always be avoided in C,
1584 but it must be handled correctly when using Fortran shared
1587 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1588 likewise for OLDDYNCOMMON and OLDDEF.
1590 Note that this test is just a heuristic, and that it is quite
1591 possible to have an uninitialized symbol in a shared object which
1592 is really a definition, rather than a common symbol. This could
1593 lead to some minor confusion when the symbol really is a common
1594 symbol in some regular object. However, I think it will be
1600 && (sec
->flags
& SEC_ALLOC
) != 0
1601 && (sec
->flags
& SEC_LOAD
) == 0
1604 newdyncommon
= true;
1606 newdyncommon
= false;
1610 && h
->root
.type
== bfd_link_hash_defined
1612 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1613 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1616 olddyncommon
= true;
1618 olddyncommon
= false;
1620 /* We now know everything about the old and new symbols. We ask the
1621 backend to check if we can merge them. */
1622 if (bed
->merge_symbol
!= NULL
)
1624 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1629 /* There are multiple definitions of a normal symbol. Skip the
1630 default symbol as well as definition from an IR object. */
1631 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1632 && !default_sym
&& h
->def_regular
1634 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1635 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1637 /* Handle a multiple definition. */
1638 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1639 abfd
, sec
, *pvalue
);
1644 /* If both the old and the new symbols look like common symbols in a
1645 dynamic object, set the size of the symbol to the larger of the
1650 && sym
->st_size
!= h
->size
)
1652 /* Since we think we have two common symbols, issue a multiple
1653 common warning if desired. Note that we only warn if the
1654 size is different. If the size is the same, we simply let
1655 the old symbol override the new one as normally happens with
1656 symbols defined in dynamic objects. */
1658 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1659 bfd_link_hash_common
, sym
->st_size
);
1660 if (sym
->st_size
> h
->size
)
1661 h
->size
= sym
->st_size
;
1663 *size_change_ok
= true;
1666 /* If we are looking at a dynamic object, and we have found a
1667 definition, we need to see if the symbol was already defined by
1668 some other object. If so, we want to use the existing
1669 definition, and we do not want to report a multiple symbol
1670 definition error; we do this by clobbering *PSEC to be
1671 bfd_und_section_ptr.
1673 We treat a common symbol as a definition if the symbol in the
1674 shared library is a function, since common symbols always
1675 represent variables; this can cause confusion in principle, but
1676 any such confusion would seem to indicate an erroneous program or
1677 shared library. We also permit a common symbol in a regular
1678 object to override a weak symbol in a shared object. */
1683 || (h
->root
.type
== bfd_link_hash_common
1684 && (newweak
|| newfunc
))))
1688 newdyncommon
= false;
1690 *psec
= sec
= bfd_und_section_ptr
;
1691 *size_change_ok
= true;
1693 /* If we get here when the old symbol is a common symbol, then
1694 we are explicitly letting it override a weak symbol or
1695 function in a dynamic object, and we don't want to warn about
1696 a type change. If the old symbol is a defined symbol, a type
1697 change warning may still be appropriate. */
1699 if (h
->root
.type
== bfd_link_hash_common
)
1700 *type_change_ok
= true;
1703 /* Handle the special case of an old common symbol merging with a
1704 new symbol which looks like a common symbol in a shared object.
1705 We change *PSEC and *PVALUE to make the new symbol look like a
1706 common symbol, and let _bfd_generic_link_add_one_symbol do the
1710 && h
->root
.type
== bfd_link_hash_common
)
1714 newdyncommon
= false;
1715 *pvalue
= sym
->st_size
;
1716 *psec
= sec
= bed
->common_section (oldsec
);
1717 *size_change_ok
= true;
1720 /* Skip weak definitions of symbols that are already defined. */
1721 if (newdef
&& olddef
&& newweak
)
1723 /* Don't skip new non-IR weak syms. */
1724 if (!(oldbfd
!= NULL
1725 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1726 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1732 /* Merge st_other. If the symbol already has a dynamic index,
1733 but visibility says it should not be visible, turn it into a
1735 elf_merge_st_other (abfd
, h
, sym
->st_other
, sec
, newdef
, newdyn
);
1736 if (h
->dynindx
!= -1)
1737 switch (ELF_ST_VISIBILITY (h
->other
))
1741 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1746 /* If the old symbol is from a dynamic object, and the new symbol is
1747 a definition which is not from a dynamic object, then the new
1748 symbol overrides the old symbol. Symbols from regular files
1749 always take precedence over symbols from dynamic objects, even if
1750 they are defined after the dynamic object in the link.
1752 As above, we again permit a common symbol in a regular object to
1753 override a definition in a shared object if the shared object
1754 symbol is a function or is weak. */
1759 || (bfd_is_com_section (sec
)
1760 && (oldweak
|| oldfunc
)))
1765 /* Change the hash table entry to undefined, and let
1766 _bfd_generic_link_add_one_symbol do the right thing with the
1769 h
->root
.type
= bfd_link_hash_undefined
;
1770 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1771 *size_change_ok
= true;
1774 olddyncommon
= false;
1776 /* We again permit a type change when a common symbol may be
1777 overriding a function. */
1779 if (bfd_is_com_section (sec
))
1783 /* If a common symbol overrides a function, make sure
1784 that it isn't defined dynamically nor has type
1787 h
->type
= STT_NOTYPE
;
1789 *type_change_ok
= true;
1792 if (hi
->root
.type
== bfd_link_hash_indirect
)
1795 /* This union may have been set to be non-NULL when this symbol
1796 was seen in a dynamic object. We must force the union to be
1797 NULL, so that it is correct for a regular symbol. */
1798 h
->verinfo
.vertree
= NULL
;
1801 /* Handle the special case of a new common symbol merging with an
1802 old symbol that looks like it might be a common symbol defined in
1803 a shared object. Note that we have already handled the case in
1804 which a new common symbol should simply override the definition
1805 in the shared library. */
1808 && bfd_is_com_section (sec
)
1811 /* It would be best if we could set the hash table entry to a
1812 common symbol, but we don't know what to use for the section
1813 or the alignment. */
1814 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1815 bfd_link_hash_common
, sym
->st_size
);
1817 /* If the presumed common symbol in the dynamic object is
1818 larger, pretend that the new symbol has its size. */
1820 if (h
->size
> *pvalue
)
1823 /* We need to remember the alignment required by the symbol
1824 in the dynamic object. */
1825 BFD_ASSERT (pold_alignment
);
1826 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1829 olddyncommon
= false;
1831 h
->root
.type
= bfd_link_hash_undefined
;
1832 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1834 *size_change_ok
= true;
1835 *type_change_ok
= true;
1837 if (hi
->root
.type
== bfd_link_hash_indirect
)
1840 h
->verinfo
.vertree
= NULL
;
1845 /* Handle the case where we had a versioned symbol in a dynamic
1846 library and now find a definition in a normal object. In this
1847 case, we make the versioned symbol point to the normal one. */
1848 flip
->root
.type
= h
->root
.type
;
1849 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1850 h
->root
.type
= bfd_link_hash_indirect
;
1851 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1852 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1856 flip
->ref_dynamic
= 1;
1863 /* This function is called to create an indirect symbol from the
1864 default for the symbol with the default version if needed. The
1865 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1866 set DYNSYM if the new indirect symbol is dynamic. */
1869 _bfd_elf_add_default_symbol (bfd
*abfd
,
1870 struct bfd_link_info
*info
,
1871 struct elf_link_hash_entry
*h
,
1873 Elf_Internal_Sym
*sym
,
1879 bool type_change_ok
;
1880 bool size_change_ok
;
1883 struct elf_link_hash_entry
*hi
;
1884 struct bfd_link_hash_entry
*bh
;
1885 const struct elf_backend_data
*bed
;
1890 size_t len
, shortlen
;
1894 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1897 /* If this symbol has a version, and it is the default version, we
1898 create an indirect symbol from the default name to the fully
1899 decorated name. This will cause external references which do not
1900 specify a version to be bound to this version of the symbol. */
1901 p
= strchr (name
, ELF_VER_CHR
);
1902 if (h
->versioned
== unknown
)
1906 h
->versioned
= unversioned
;
1911 if (p
[1] != ELF_VER_CHR
)
1913 h
->versioned
= versioned_hidden
;
1917 h
->versioned
= versioned
;
1922 /* PR ld/19073: We may see an unversioned definition after the
1928 bed
= get_elf_backend_data (abfd
);
1929 collect
= bed
->collect
;
1930 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1932 shortlen
= p
- name
;
1933 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1934 if (shortname
== NULL
)
1936 memcpy (shortname
, name
, shortlen
);
1937 shortname
[shortlen
] = '\0';
1939 /* We are going to create a new symbol. Merge it with any existing
1940 symbol with this name. For the purposes of the merge, act as
1941 though we were defining the symbol we just defined, although we
1942 actually going to define an indirect symbol. */
1943 type_change_ok
= false;
1944 size_change_ok
= false;
1947 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1948 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1949 &type_change_ok
, &size_change_ok
, &matched
))
1955 if (hi
->def_regular
|| ELF_COMMON_DEF_P (hi
))
1957 /* If the undecorated symbol will have a version added by a
1958 script different to H, then don't indirect to/from the
1959 undecorated symbol. This isn't ideal because we may not yet
1960 have seen symbol versions, if given by a script on the
1961 command line rather than via --version-script. */
1962 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1967 = bfd_find_version_for_sym (info
->version_info
,
1968 hi
->root
.root
.string
, &hide
);
1969 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1971 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
1975 if (hi
->verinfo
.vertree
!= NULL
1976 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1982 /* Add the default symbol if not performing a relocatable link. */
1983 if (! bfd_link_relocatable (info
))
1986 if (bh
->type
== bfd_link_hash_defined
1987 && bh
->u
.def
.section
->owner
!= NULL
1988 && (bh
->u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
1990 /* Mark the previous definition from IR object as
1991 undefined so that the generic linker will override
1993 bh
->type
= bfd_link_hash_undefined
;
1994 bh
->u
.undef
.abfd
= bh
->u
.def
.section
->owner
;
1996 if (! (_bfd_generic_link_add_one_symbol
1997 (info
, abfd
, shortname
, BSF_INDIRECT
,
1998 bfd_ind_section_ptr
,
1999 0, name
, false, collect
, &bh
)))
2001 hi
= (struct elf_link_hash_entry
*) bh
;
2006 /* In this case the symbol named SHORTNAME is overriding the
2007 indirect symbol we want to add. We were planning on making
2008 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2009 is the name without a version. NAME is the fully versioned
2010 name, and it is the default version.
2012 Overriding means that we already saw a definition for the
2013 symbol SHORTNAME in a regular object, and it is overriding
2014 the symbol defined in the dynamic object.
2016 When this happens, we actually want to change NAME, the
2017 symbol we just added, to refer to SHORTNAME. This will cause
2018 references to NAME in the shared object to become references
2019 to SHORTNAME in the regular object. This is what we expect
2020 when we override a function in a shared object: that the
2021 references in the shared object will be mapped to the
2022 definition in the regular object. */
2024 while (hi
->root
.type
== bfd_link_hash_indirect
2025 || hi
->root
.type
== bfd_link_hash_warning
)
2026 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2028 h
->root
.type
= bfd_link_hash_indirect
;
2029 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
2033 hi
->ref_dynamic
= 1;
2037 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
2042 /* Now set HI to H, so that the following code will set the
2043 other fields correctly. */
2047 /* Check if HI is a warning symbol. */
2048 if (hi
->root
.type
== bfd_link_hash_warning
)
2049 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2051 /* If there is a duplicate definition somewhere, then HI may not
2052 point to an indirect symbol. We will have reported an error to
2053 the user in that case. */
2055 if (hi
->root
.type
== bfd_link_hash_indirect
)
2057 struct elf_link_hash_entry
*ht
;
2059 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2060 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2062 /* If we first saw a reference to SHORTNAME with non-default
2063 visibility, merge that visibility to the @@VER symbol. */
2064 elf_merge_st_other (abfd
, ht
, hi
->other
, sec
, true, dynamic
);
2066 /* A reference to the SHORTNAME symbol from a dynamic library
2067 will be satisfied by the versioned symbol at runtime. In
2068 effect, we have a reference to the versioned symbol. */
2069 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2070 hi
->dynamic_def
|= ht
->dynamic_def
;
2072 /* See if the new flags lead us to realize that the symbol must
2078 if (! bfd_link_executable (info
)
2085 if (hi
->ref_regular
)
2091 /* We also need to define an indirection from the nondefault version
2095 len
= strlen (name
);
2096 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2097 if (shortname
== NULL
)
2099 memcpy (shortname
, name
, shortlen
);
2100 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2102 /* Once again, merge with any existing symbol. */
2103 type_change_ok
= false;
2104 size_change_ok
= false;
2106 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2107 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2108 &type_change_ok
, &size_change_ok
, &matched
))
2114 && h
->root
.type
== bfd_link_hash_defweak
2115 && hi
->root
.type
== bfd_link_hash_defined
)
2117 /* We are handling a weak sym@@ver and attempting to define
2118 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2119 new weak sym@ver because there is already a strong sym@ver.
2120 However, sym@ver and sym@@ver are really the same symbol.
2121 The existing strong sym@ver ought to override sym@@ver. */
2122 h
->root
.type
= bfd_link_hash_defined
;
2123 h
->root
.u
.def
.section
= hi
->root
.u
.def
.section
;
2124 h
->root
.u
.def
.value
= hi
->root
.u
.def
.value
;
2125 hi
->root
.type
= bfd_link_hash_indirect
;
2126 hi
->root
.u
.i
.link
= &h
->root
;
2133 /* Here SHORTNAME is a versioned name, so we don't expect to see
2134 the type of override we do in the case above unless it is
2135 overridden by a versioned definition. */
2136 if (hi
->root
.type
!= bfd_link_hash_defined
2137 && hi
->root
.type
!= bfd_link_hash_defweak
)
2139 /* xgettext:c-format */
2140 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2147 if (! (_bfd_generic_link_add_one_symbol
2148 (info
, abfd
, shortname
, BSF_INDIRECT
,
2149 bfd_ind_section_ptr
, 0, name
, false, collect
, &bh
)))
2151 hi
= (struct elf_link_hash_entry
*) bh
;
2154 /* If there is a duplicate definition somewhere, then HI may not
2155 point to an indirect symbol. We will have reported an error
2156 to the user in that case. */
2157 if (hi
->root
.type
== bfd_link_hash_indirect
)
2159 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2160 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2161 hi
->dynamic_def
|= h
->dynamic_def
;
2163 /* If we first saw a reference to @VER symbol with
2164 non-default visibility, merge that visibility to the
2166 elf_merge_st_other (abfd
, h
, hi
->other
, sec
, true, dynamic
);
2168 /* See if the new flags lead us to realize that the symbol
2174 if (! bfd_link_executable (info
)
2180 if (hi
->ref_regular
)
2189 /* This routine is used to export all defined symbols into the dynamic
2190 symbol table. It is called via elf_link_hash_traverse. */
2193 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2195 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2197 /* Ignore indirect symbols. These are added by the versioning code. */
2198 if (h
->root
.type
== bfd_link_hash_indirect
)
2201 /* Ignore this if we won't export it. */
2202 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2205 if (h
->dynindx
== -1
2206 && (h
->def_regular
|| h
->ref_regular
)
2207 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2208 h
->root
.root
.string
))
2210 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2220 /* Return true if GLIBC_ABI_DT_RELR is added to the list of version
2221 dependencies successfully. GLIBC_ABI_DT_RELR will be put into the
2222 .gnu.version_r section. */
2225 elf_link_add_dt_relr_dependency (struct elf_find_verdep_info
*rinfo
)
2227 bfd
*glibc_bfd
= NULL
;
2228 Elf_Internal_Verneed
*t
;
2229 Elf_Internal_Vernaux
*a
;
2231 const char *relr
= "GLIBC_ABI_DT_RELR";
2233 /* See if we already know about GLIBC_PRIVATE_DT_RELR. */
2234 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2238 const char *soname
= bfd_elf_get_dt_soname (t
->vn_bfd
);
2239 /* Skip the shared library if it isn't libc.so. */
2240 if (!soname
|| !startswith (soname
, "libc.so."))
2243 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2245 /* Return if GLIBC_PRIVATE_DT_RELR dependency has been
2247 if (a
->vna_nodename
== relr
2248 || strcmp (a
->vna_nodename
, relr
) == 0)
2251 /* Check if libc.so provides GLIBC_2.XX version. */
2252 if (!glibc_bfd
&& startswith (a
->vna_nodename
, "GLIBC_2."))
2253 glibc_bfd
= t
->vn_bfd
;
2259 /* Skip if it isn't linked against glibc. */
2260 if (glibc_bfd
== NULL
)
2263 /* This is a new version. Add it to tree we are building. */
2267 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
,
2271 rinfo
->failed
= true;
2275 t
->vn_bfd
= glibc_bfd
;
2276 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2277 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2281 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2284 rinfo
->failed
= true;
2288 a
->vna_nodename
= relr
;
2290 a
->vna_nextptr
= t
->vn_auxptr
;
2291 a
->vna_other
= rinfo
->vers
+ 1;
2299 /* Look through the symbols which are defined in other shared
2300 libraries and referenced here. Update the list of version
2301 dependencies. This will be put into the .gnu.version_r section.
2302 This function is called via elf_link_hash_traverse. */
2305 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2308 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2309 Elf_Internal_Verneed
*t
;
2310 Elf_Internal_Vernaux
*a
;
2313 /* We only care about symbols defined in shared objects with version
2318 || h
->verinfo
.verdef
== NULL
2319 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2320 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2323 /* See if we already know about this version. */
2324 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2328 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2331 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2332 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2338 /* This is a new version. Add it to tree we are building. */
2343 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2346 rinfo
->failed
= true;
2350 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2351 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2352 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2356 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2359 rinfo
->failed
= true;
2363 /* Note that we are copying a string pointer here, and testing it
2364 above. If bfd_elf_string_from_elf_section is ever changed to
2365 discard the string data when low in memory, this will have to be
2367 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2369 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2370 a
->vna_nextptr
= t
->vn_auxptr
;
2372 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2375 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2382 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2383 hidden. Set *T_P to NULL if there is no match. */
2386 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2387 struct elf_link_hash_entry
*h
,
2388 const char *version_p
,
2389 struct bfd_elf_version_tree
**t_p
,
2392 struct bfd_elf_version_tree
*t
;
2394 /* Look for the version. If we find it, it is no longer weak. */
2395 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2397 if (strcmp (t
->name
, version_p
) == 0)
2401 struct bfd_elf_version_expr
*d
;
2403 len
= version_p
- h
->root
.root
.string
;
2404 alc
= (char *) bfd_malloc (len
);
2407 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2408 alc
[len
- 1] = '\0';
2409 if (alc
[len
- 2] == ELF_VER_CHR
)
2410 alc
[len
- 2] = '\0';
2412 h
->verinfo
.vertree
= t
;
2416 if (t
->globals
.list
!= NULL
)
2417 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2419 /* See if there is anything to force this symbol to
2421 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2423 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2426 && ! info
->export_dynamic
)
2440 /* Return TRUE if the symbol H is hidden by version script. */
2443 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2444 struct elf_link_hash_entry
*h
)
2448 const struct elf_backend_data
*bed
2449 = get_elf_backend_data (info
->output_bfd
);
2451 /* Version script only hides symbols defined in regular objects. */
2452 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2455 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2456 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2458 struct bfd_elf_version_tree
*t
;
2461 if (*p
== ELF_VER_CHR
)
2465 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2469 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2474 /* If we don't have a version for this symbol, see if we can find
2476 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2479 = bfd_find_version_for_sym (info
->version_info
,
2480 h
->root
.root
.string
, &hide
);
2481 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2483 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2491 /* Figure out appropriate versions for all the symbols. We may not
2492 have the version number script until we have read all of the input
2493 files, so until that point we don't know which symbols should be
2494 local. This function is called via elf_link_hash_traverse. */
2497 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2499 struct elf_info_failed
*sinfo
;
2500 struct bfd_link_info
*info
;
2501 const struct elf_backend_data
*bed
;
2502 struct elf_info_failed eif
;
2506 sinfo
= (struct elf_info_failed
*) data
;
2509 /* Fix the symbol flags. */
2512 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2515 sinfo
->failed
= true;
2519 bed
= get_elf_backend_data (info
->output_bfd
);
2521 /* We only need version numbers for symbols defined in regular
2523 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2525 /* Hide symbols defined in discarded input sections. */
2526 if ((h
->root
.type
== bfd_link_hash_defined
2527 || h
->root
.type
== bfd_link_hash_defweak
)
2528 && discarded_section (h
->root
.u
.def
.section
))
2529 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2534 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2535 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2537 struct bfd_elf_version_tree
*t
;
2540 if (*p
== ELF_VER_CHR
)
2543 /* If there is no version string, we can just return out. */
2547 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2549 sinfo
->failed
= true;
2554 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2556 /* If we are building an application, we need to create a
2557 version node for this version. */
2558 if (t
== NULL
&& bfd_link_executable (info
))
2560 struct bfd_elf_version_tree
**pp
;
2563 /* If we aren't going to export this symbol, we don't need
2564 to worry about it. */
2565 if (h
->dynindx
== -1)
2568 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2572 sinfo
->failed
= true;
2577 t
->name_indx
= (unsigned int) -1;
2581 /* Don't count anonymous version tag. */
2582 if (sinfo
->info
->version_info
!= NULL
2583 && sinfo
->info
->version_info
->vernum
== 0)
2585 for (pp
= &sinfo
->info
->version_info
;
2589 t
->vernum
= version_index
;
2593 h
->verinfo
.vertree
= t
;
2597 /* We could not find the version for a symbol when
2598 generating a shared archive. Return an error. */
2600 /* xgettext:c-format */
2601 (_("%pB: version node not found for symbol %s"),
2602 info
->output_bfd
, h
->root
.root
.string
);
2603 bfd_set_error (bfd_error_bad_value
);
2604 sinfo
->failed
= true;
2609 /* If we don't have a version for this symbol, see if we can find
2612 && h
->verinfo
.vertree
== NULL
2613 && sinfo
->info
->version_info
!= NULL
)
2616 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2617 h
->root
.root
.string
, &hide
);
2618 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2619 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2625 /* Read and swap the relocs from the section indicated by SHDR. This
2626 may be either a REL or a RELA section. The relocations are
2627 translated into RELA relocations and stored in INTERNAL_RELOCS,
2628 which should have already been allocated to contain enough space.
2629 The EXTERNAL_RELOCS are a buffer where the external form of the
2630 relocations should be stored.
2632 Returns FALSE if something goes wrong. */
2635 elf_link_read_relocs_from_section (bfd
*abfd
,
2637 Elf_Internal_Shdr
*shdr
,
2638 void *external_relocs
,
2639 Elf_Internal_Rela
*internal_relocs
)
2641 const struct elf_backend_data
*bed
;
2642 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2643 const bfd_byte
*erela
;
2644 const bfd_byte
*erelaend
;
2645 Elf_Internal_Rela
*irela
;
2646 Elf_Internal_Shdr
*symtab_hdr
;
2649 /* Position ourselves at the start of the section. */
2650 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2653 /* Read the relocations. */
2654 if (bfd_read (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2657 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2658 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2660 bed
= get_elf_backend_data (abfd
);
2662 /* Convert the external relocations to the internal format. */
2663 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2664 swap_in
= bed
->s
->swap_reloc_in
;
2665 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2666 swap_in
= bed
->s
->swap_reloca_in
;
2669 bfd_set_error (bfd_error_wrong_format
);
2673 erela
= (const bfd_byte
*) external_relocs
;
2674 /* Setting erelaend like this and comparing with <= handles case of
2675 a fuzzed object with sh_size not a multiple of sh_entsize. */
2676 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2677 irela
= internal_relocs
;
2678 while (erela
<= erelaend
)
2682 (*swap_in
) (abfd
, erela
, irela
);
2683 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2684 if (bed
->s
->arch_size
== 64)
2688 if ((size_t) r_symndx
>= nsyms
)
2691 /* xgettext:c-format */
2692 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2693 " for offset %#" PRIx64
" in section `%pA'"),
2694 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2695 (uint64_t) irela
->r_offset
, sec
);
2696 bfd_set_error (bfd_error_bad_value
);
2700 else if (r_symndx
!= STN_UNDEF
)
2703 /* xgettext:c-format */
2704 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2705 " for offset %#" PRIx64
" in section `%pA'"
2706 " when the object file has no symbol table"),
2707 abfd
, (uint64_t) r_symndx
,
2708 (uint64_t) irela
->r_offset
, sec
);
2709 bfd_set_error (bfd_error_bad_value
);
2712 irela
+= bed
->s
->int_rels_per_ext_rel
;
2713 erela
+= shdr
->sh_entsize
;
2719 /* Read and swap the relocs for a section O. They may have been
2720 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2721 not NULL, they are used as buffers to read into. They are known to
2722 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2723 the return value is allocated using either malloc or bfd_alloc,
2724 according to the KEEP_MEMORY argument. If O has two relocation
2725 sections (both REL and RELA relocations), then the REL_HDR
2726 relocations will appear first in INTERNAL_RELOCS, followed by the
2727 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2728 update cache_size. */
2731 _bfd_elf_link_info_read_relocs (bfd
*abfd
,
2732 struct bfd_link_info
*info
,
2734 void *external_relocs
,
2735 Elf_Internal_Rela
*internal_relocs
,
2738 void *alloc1
= NULL
;
2739 Elf_Internal_Rela
*alloc2
= NULL
;
2740 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2741 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2742 Elf_Internal_Rela
*internal_rela_relocs
;
2744 if (esdo
->relocs
!= NULL
)
2745 return esdo
->relocs
;
2747 if (o
->reloc_count
== 0)
2750 if (internal_relocs
== NULL
)
2754 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2757 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2759 info
->cache_size
+= size
;
2762 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2763 if (internal_relocs
== NULL
)
2767 if (external_relocs
== NULL
)
2769 bfd_size_type size
= 0;
2772 size
+= esdo
->rel
.hdr
->sh_size
;
2774 size
+= esdo
->rela
.hdr
->sh_size
;
2776 alloc1
= bfd_malloc (size
);
2779 external_relocs
= alloc1
;
2782 internal_rela_relocs
= internal_relocs
;
2785 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2789 external_relocs
= (((bfd_byte
*) external_relocs
)
2790 + esdo
->rel
.hdr
->sh_size
);
2791 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2792 * bed
->s
->int_rels_per_ext_rel
);
2796 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2798 internal_rela_relocs
)))
2801 /* Cache the results for next time, if we can. */
2803 esdo
->relocs
= internal_relocs
;
2807 /* Don't free alloc2, since if it was allocated we are passing it
2808 back (under the name of internal_relocs). */
2810 return internal_relocs
;
2817 bfd_release (abfd
, alloc2
);
2824 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2825 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2826 struct bfd_link_info. */
2829 _bfd_elf_link_read_relocs (bfd
*abfd
,
2831 void *external_relocs
,
2832 Elf_Internal_Rela
*internal_relocs
,
2835 return _bfd_elf_link_info_read_relocs (abfd
, NULL
, o
, external_relocs
,
2836 internal_relocs
, keep_memory
);
2840 /* Compute the size of, and allocate space for, REL_HDR which is the
2841 section header for a section containing relocations for O. */
2844 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2845 struct bfd_elf_section_reloc_data
*reldata
)
2847 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2849 /* That allows us to calculate the size of the section. */
2850 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2852 /* The contents field must last into write_object_contents, so we
2853 allocate it with bfd_alloc rather than malloc. Also since we
2854 cannot be sure that the contents will actually be filled in,
2855 we zero the allocated space. */
2856 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2857 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2860 if (reldata
->hashes
== NULL
&& reldata
->count
)
2862 struct elf_link_hash_entry
**p
;
2864 p
= ((struct elf_link_hash_entry
**)
2865 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2869 reldata
->hashes
= p
;
2875 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2876 originated from the section given by INPUT_REL_HDR) to the
2880 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2881 asection
*input_section
,
2882 Elf_Internal_Shdr
*input_rel_hdr
,
2883 Elf_Internal_Rela
*internal_relocs
,
2884 struct elf_link_hash_entry
**rel_hash
2887 Elf_Internal_Rela
*irela
;
2888 Elf_Internal_Rela
*irelaend
;
2890 struct bfd_elf_section_reloc_data
*output_reldata
;
2891 asection
*output_section
;
2892 const struct elf_backend_data
*bed
;
2893 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2894 struct bfd_elf_section_data
*esdo
;
2896 output_section
= input_section
->output_section
;
2898 bed
= get_elf_backend_data (output_bfd
);
2899 esdo
= elf_section_data (output_section
);
2900 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2902 output_reldata
= &esdo
->rel
;
2903 swap_out
= bed
->s
->swap_reloc_out
;
2905 else if (esdo
->rela
.hdr
2906 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2908 output_reldata
= &esdo
->rela
;
2909 swap_out
= bed
->s
->swap_reloca_out
;
2914 /* xgettext:c-format */
2915 (_("%pB: relocation size mismatch in %pB section %pA"),
2916 output_bfd
, input_section
->owner
, input_section
);
2917 bfd_set_error (bfd_error_wrong_format
);
2921 erel
= output_reldata
->hdr
->contents
;
2922 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2923 irela
= internal_relocs
;
2924 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2925 * bed
->s
->int_rels_per_ext_rel
);
2926 while (irela
< irelaend
)
2928 (*swap_out
) (output_bfd
, irela
, erel
);
2929 irela
+= bed
->s
->int_rels_per_ext_rel
;
2930 erel
+= input_rel_hdr
->sh_entsize
;
2933 /* Bump the counter, so that we know where to add the next set of
2935 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2940 /* Make weak undefined symbols in PIE dynamic. */
2943 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2944 struct elf_link_hash_entry
*h
)
2946 if (bfd_link_pie (info
)
2948 && h
->root
.type
== bfd_link_hash_undefweak
)
2949 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2954 /* Fix up the flags for a symbol. This handles various cases which
2955 can only be fixed after all the input files are seen. This is
2956 currently called by both adjust_dynamic_symbol and
2957 assign_sym_version, which is unnecessary but perhaps more robust in
2958 the face of future changes. */
2961 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2962 struct elf_info_failed
*eif
)
2964 const struct elf_backend_data
*bed
;
2966 /* If this symbol was mentioned in a non-ELF file, try to set
2967 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2968 permit a non-ELF file to correctly refer to a symbol defined in
2969 an ELF dynamic object. */
2972 while (h
->root
.type
== bfd_link_hash_indirect
)
2973 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2975 if (h
->root
.type
!= bfd_link_hash_defined
2976 && h
->root
.type
!= bfd_link_hash_defweak
)
2979 h
->ref_regular_nonweak
= 1;
2983 if (h
->root
.u
.def
.section
->owner
!= NULL
2984 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2985 == bfd_target_elf_flavour
))
2988 h
->ref_regular_nonweak
= 1;
2994 if (h
->dynindx
== -1
2998 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3007 /* Unfortunately, NON_ELF is only correct if the symbol
3008 was first seen in a non-ELF file. Fortunately, if the symbol
3009 was first seen in an ELF file, we're probably OK unless the
3010 symbol was defined in a non-ELF file. Catch that case here.
3011 FIXME: We're still in trouble if the symbol was first seen in
3012 a dynamic object, and then later in a non-ELF regular object. */
3013 if ((h
->root
.type
== bfd_link_hash_defined
3014 || h
->root
.type
== bfd_link_hash_defweak
)
3016 && (h
->root
.u
.def
.section
->owner
!= NULL
3017 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3018 != bfd_target_elf_flavour
)
3019 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3020 && !h
->def_dynamic
)))
3024 /* Backend specific symbol fixup. */
3025 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3026 if (bed
->elf_backend_fixup_symbol
3027 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
3030 /* If this is a final link, and the symbol was defined as a common
3031 symbol in a regular object file, and there was no definition in
3032 any dynamic object, then the linker will have allocated space for
3033 the symbol in a common section but the DEF_REGULAR
3034 flag will not have been set. */
3035 if (h
->root
.type
== bfd_link_hash_defined
3039 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
3042 /* Symbols defined in discarded sections shouldn't be dynamic. */
3043 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
3044 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3046 /* If a weak undefined symbol has non-default visibility, we also
3047 hide it from the dynamic linker. */
3048 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
3049 && h
->root
.type
== bfd_link_hash_undefweak
)
3050 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3052 /* A hidden versioned symbol in executable should be forced local if
3053 it is is locally defined, not referenced by shared library and not
3055 else if (bfd_link_executable (eif
->info
)
3056 && h
->versioned
== versioned_hidden
3057 && !eif
->info
->export_dynamic
3061 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3063 /* If -Bsymbolic was used (which means to bind references to global
3064 symbols to the definition within the shared object), and this
3065 symbol was defined in a regular object, then it actually doesn't
3066 need a PLT entry. Likewise, if the symbol has non-default
3067 visibility. If the symbol has hidden or internal visibility, we
3068 will force it local. */
3069 else if (h
->needs_plt
3070 && bfd_link_pic (eif
->info
)
3071 && is_elf_hash_table (eif
->info
->hash
)
3072 && (SYMBOLIC_BIND (eif
->info
, h
)
3073 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
3078 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3079 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
3080 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3083 /* If this is a weak defined symbol in a dynamic object, and we know
3084 the real definition in the dynamic object, copy interesting flags
3085 over to the real definition. */
3086 if (h
->is_weakalias
)
3088 struct elf_link_hash_entry
*def
= weakdef (h
);
3090 /* If the real definition is defined by a regular object file,
3091 don't do anything special. See the longer description in
3092 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3093 bfd_link_hash_defined as it was when put on the alias list
3094 then it must have originally been a versioned symbol (for
3095 which a non-versioned indirect symbol is created) and later
3096 a definition for the non-versioned symbol is found. In that
3097 case the indirection is flipped with the versioned symbol
3098 becoming an indirect pointing at the non-versioned symbol.
3099 Thus, not an alias any more. */
3100 if (def
->def_regular
3101 || def
->root
.type
!= bfd_link_hash_defined
)
3104 while ((h
= h
->u
.alias
) != def
)
3105 h
->is_weakalias
= 0;
3109 while (h
->root
.type
== bfd_link_hash_indirect
)
3110 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3111 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3112 || h
->root
.type
== bfd_link_hash_defweak
);
3113 BFD_ASSERT (def
->def_dynamic
);
3114 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
3121 /* Make the backend pick a good value for a dynamic symbol. This is
3122 called via elf_link_hash_traverse, and also calls itself
3126 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
3128 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3129 struct elf_link_hash_table
*htab
;
3130 const struct elf_backend_data
*bed
;
3132 if (! is_elf_hash_table (eif
->info
->hash
))
3135 /* Ignore indirect symbols. These are added by the versioning code. */
3136 if (h
->root
.type
== bfd_link_hash_indirect
)
3139 /* Fix the symbol flags. */
3140 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
3143 htab
= elf_hash_table (eif
->info
);
3144 bed
= get_elf_backend_data (htab
->dynobj
);
3146 if (h
->root
.type
== bfd_link_hash_undefweak
)
3148 if (eif
->info
->dynamic_undefined_weak
== 0)
3149 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3150 else if (eif
->info
->dynamic_undefined_weak
> 0
3152 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3153 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
3154 h
->root
.root
.string
))
3156 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3164 /* If this symbol does not require a PLT entry, and it is not
3165 defined by a dynamic object, or is not referenced by a regular
3166 object, ignore it. We do have to handle a weak defined symbol,
3167 even if no regular object refers to it, if we decided to add it
3168 to the dynamic symbol table. FIXME: Do we normally need to worry
3169 about symbols which are defined by one dynamic object and
3170 referenced by another one? */
3172 && h
->type
!= STT_GNU_IFUNC
3176 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3178 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3182 /* If we've already adjusted this symbol, don't do it again. This
3183 can happen via a recursive call. */
3184 if (h
->dynamic_adjusted
)
3187 /* Don't look at this symbol again. Note that we must set this
3188 after checking the above conditions, because we may look at a
3189 symbol once, decide not to do anything, and then get called
3190 recursively later after REF_REGULAR is set below. */
3191 h
->dynamic_adjusted
= 1;
3193 /* If this is a weak definition, and we know a real definition, and
3194 the real symbol is not itself defined by a regular object file,
3195 then get a good value for the real definition. We handle the
3196 real symbol first, for the convenience of the backend routine.
3198 Note that there is a confusing case here. If the real definition
3199 is defined by a regular object file, we don't get the real symbol
3200 from the dynamic object, but we do get the weak symbol. If the
3201 processor backend uses a COPY reloc, then if some routine in the
3202 dynamic object changes the real symbol, we will not see that
3203 change in the corresponding weak symbol. This is the way other
3204 ELF linkers work as well, and seems to be a result of the shared
3207 I will clarify this issue. Most SVR4 shared libraries define the
3208 variable _timezone and define timezone as a weak synonym. The
3209 tzset call changes _timezone. If you write
3210 extern int timezone;
3212 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3213 you might expect that, since timezone is a synonym for _timezone,
3214 the same number will print both times. However, if the processor
3215 backend uses a COPY reloc, then actually timezone will be copied
3216 into your process image, and, since you define _timezone
3217 yourself, _timezone will not. Thus timezone and _timezone will
3218 wind up at different memory locations. The tzset call will set
3219 _timezone, leaving timezone unchanged. */
3221 if (h
->is_weakalias
)
3223 struct elf_link_hash_entry
*def
= weakdef (h
);
3225 /* If we get to this point, there is an implicit reference to
3226 the alias by a regular object file via the weak symbol H. */
3227 def
->ref_regular
= 1;
3229 /* Ensure that the backend adjust_dynamic_symbol function sees
3230 the strong alias before H by recursively calling ourselves. */
3231 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3235 /* If a symbol has no type and no size and does not require a PLT
3236 entry, then we are probably about to do the wrong thing here: we
3237 are probably going to create a COPY reloc for an empty object.
3238 This case can arise when a shared object is built with assembly
3239 code, and the assembly code fails to set the symbol type. */
3241 && h
->type
== STT_NOTYPE
3244 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3245 h
->root
.root
.string
);
3247 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3256 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3260 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3261 struct elf_link_hash_entry
*h
,
3264 unsigned int power_of_two
;
3266 asection
*sec
= h
->root
.u
.def
.section
;
3268 /* The section alignment of the definition is the maximum alignment
3269 requirement of symbols defined in the section. Since we don't
3270 know the symbol alignment requirement, we start with the
3271 maximum alignment and check low bits of the symbol address
3272 for the minimum alignment. */
3273 power_of_two
= bfd_section_alignment (sec
);
3274 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3275 while ((h
->root
.u
.def
.value
& mask
) != 0)
3281 if (power_of_two
> bfd_section_alignment (dynbss
))
3283 /* Adjust the section alignment if needed. */
3284 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3288 /* We make sure that the symbol will be aligned properly. */
3289 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3291 /* Define the symbol as being at this point in DYNBSS. */
3292 h
->root
.u
.def
.section
= dynbss
;
3293 h
->root
.u
.def
.value
= dynbss
->size
;
3295 /* Increment the size of DYNBSS to make room for the symbol. */
3296 dynbss
->size
+= h
->size
;
3298 /* No error if extern_protected_data is true. */
3299 if (h
->protected_def
3300 && (!info
->extern_protected_data
3301 || (info
->extern_protected_data
< 0
3302 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3303 info
->callbacks
->einfo
3304 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3305 h
->root
.root
.string
);
3310 /* Adjust all external symbols pointing into SEC_MERGE sections
3311 to reflect the object merging within the sections. */
3314 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3318 if ((h
->root
.type
== bfd_link_hash_defined
3319 || h
->root
.type
== bfd_link_hash_defweak
)
3320 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3321 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3323 bfd
*output_bfd
= (bfd
*) data
;
3325 h
->root
.u
.def
.value
=
3326 _bfd_merged_section_offset (output_bfd
,
3327 &h
->root
.u
.def
.section
,
3328 elf_section_data (sec
)->sec_info
,
3329 h
->root
.u
.def
.value
);
3335 /* Returns false if the symbol referred to by H should be considered
3336 to resolve local to the current module, and true if it should be
3337 considered to bind dynamically. */
3340 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3341 struct bfd_link_info
*info
,
3342 bool not_local_protected
)
3344 bool binding_stays_local_p
;
3345 const struct elf_backend_data
*bed
;
3346 struct elf_link_hash_table
*hash_table
;
3351 while (h
->root
.type
== bfd_link_hash_indirect
3352 || h
->root
.type
== bfd_link_hash_warning
)
3353 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3355 /* If it was forced local, then clearly it's not dynamic. */
3356 if (h
->dynindx
== -1)
3358 if (h
->forced_local
)
3361 /* Identify the cases where name binding rules say that a
3362 visible symbol resolves locally. */
3363 binding_stays_local_p
= (bfd_link_executable (info
)
3364 || SYMBOLIC_BIND (info
, h
));
3366 switch (ELF_ST_VISIBILITY (h
->other
))
3373 hash_table
= elf_hash_table (info
);
3374 if (!is_elf_hash_table (&hash_table
->root
))
3377 bed
= get_elf_backend_data (hash_table
->dynobj
);
3379 /* Proper resolution for function pointer equality may require
3380 that these symbols perhaps be resolved dynamically, even though
3381 we should be resolving them to the current module. */
3382 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3383 binding_stays_local_p
= true;
3390 /* If it isn't defined locally, then clearly it's dynamic. */
3391 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3394 /* Otherwise, the symbol is dynamic if binding rules don't tell
3395 us that it remains local. */
3396 return !binding_stays_local_p
;
3399 /* Return true if the symbol referred to by H should be considered
3400 to resolve local to the current module, and false otherwise. Differs
3401 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3402 undefined symbols. The two functions are virtually identical except
3403 for the place where dynindx == -1 is tested. If that test is true,
3404 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3405 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3407 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3408 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3409 treatment of undefined weak symbols. For those that do not make
3410 undefined weak symbols dynamic, both functions may return false. */
3413 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3414 struct bfd_link_info
*info
,
3415 bool local_protected
)
3417 const struct elf_backend_data
*bed
;
3418 struct elf_link_hash_table
*hash_table
;
3420 /* If it's a local sym, of course we resolve locally. */
3424 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3425 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3426 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3429 /* Forced local symbols resolve locally. */
3430 if (h
->forced_local
)
3433 /* Common symbols that become definitions don't get the DEF_REGULAR
3434 flag set, so test it first, and don't bail out. */
3435 if (ELF_COMMON_DEF_P (h
))
3437 /* If we don't have a definition in a regular file, then we can't
3438 resolve locally. The sym is either undefined or dynamic. */
3439 else if (!h
->def_regular
)
3442 /* Non-dynamic symbols resolve locally. */
3443 if (h
->dynindx
== -1)
3446 /* At this point, we know the symbol is defined and dynamic. In an
3447 executable it must resolve locally, likewise when building symbolic
3448 shared libraries. */
3449 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3452 /* Now deal with defined dynamic symbols in shared libraries. Ones
3453 with default visibility might not resolve locally. */
3454 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3457 hash_table
= elf_hash_table (info
);
3458 if (!is_elf_hash_table (&hash_table
->root
))
3461 /* STV_PROTECTED symbols with indirect external access are local. */
3462 if (info
->indirect_extern_access
> 0)
3465 bed
= get_elf_backend_data (hash_table
->dynobj
);
3467 /* If extern_protected_data is false, STV_PROTECTED non-function
3468 symbols are local. */
3469 if ((!info
->extern_protected_data
3470 || (info
->extern_protected_data
< 0
3471 && !bed
->extern_protected_data
))
3472 && !bed
->is_function_type (h
->type
))
3475 /* Function pointer equality tests may require that STV_PROTECTED
3476 symbols be treated as dynamic symbols. If the address of a
3477 function not defined in an executable is set to that function's
3478 plt entry in the executable, then the address of the function in
3479 a shared library must also be the plt entry in the executable. */
3480 return local_protected
;
3483 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3484 aligned. Returns the first TLS output section. */
3486 struct bfd_section
*
3487 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3489 struct bfd_section
*sec
, *tls
;
3490 unsigned int align
= 0;
3492 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3493 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3497 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3498 if (sec
->alignment_power
> align
)
3499 align
= sec
->alignment_power
;
3501 elf_hash_table (info
)->tls_sec
= tls
;
3503 /* Ensure the alignment of the first section (usually .tdata) is the largest
3504 alignment, so that the tls segment starts aligned. */
3506 tls
->alignment_power
= align
;
3511 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3513 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3514 Elf_Internal_Sym
*sym
)
3516 const struct elf_backend_data
*bed
;
3518 /* Local symbols do not count, but target specific ones might. */
3519 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3520 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3523 bed
= get_elf_backend_data (abfd
);
3524 /* Function symbols do not count. */
3525 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3528 /* If the section is undefined, then so is the symbol. */
3529 if (sym
->st_shndx
== SHN_UNDEF
)
3532 /* If the symbol is defined in the common section, then
3533 it is a common definition and so does not count. */
3534 if (bed
->common_definition (sym
))
3537 /* If the symbol is in a target specific section then we
3538 must rely upon the backend to tell us what it is. */
3539 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3540 /* FIXME - this function is not coded yet:
3542 return _bfd_is_global_symbol_definition (abfd, sym);
3544 Instead for now assume that the definition is not global,
3545 Even if this is wrong, at least the linker will behave
3546 in the same way that it used to do. */
3552 /* Search the symbol table of the archive element of the archive ABFD
3553 whose archive map contains a mention of SYMDEF, and determine if
3554 the symbol is defined in this element. */
3556 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3558 Elf_Internal_Shdr
* hdr
;
3562 Elf_Internal_Sym
*isymbuf
;
3563 Elf_Internal_Sym
*isym
;
3564 Elf_Internal_Sym
*isymend
;
3567 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
, NULL
);
3571 if (! bfd_check_format (abfd
, bfd_object
))
3574 if (elf_use_dt_symtab_p (abfd
))
3576 bfd_set_error (bfd_error_wrong_format
);
3580 /* Select the appropriate symbol table. If we don't know if the
3581 object file is an IR object, give linker LTO plugin a chance to
3582 get the correct symbol table. */
3583 if (abfd
->plugin_format
== bfd_plugin_yes
3584 #if BFD_SUPPORTS_PLUGINS
3585 || (abfd
->plugin_format
== bfd_plugin_unknown
3586 && bfd_link_plugin_object_p (abfd
))
3590 /* Use the IR symbol table if the object has been claimed by
3592 abfd
= abfd
->plugin_dummy_bfd
;
3593 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3595 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3596 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3598 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3600 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3602 /* The sh_info field of the symtab header tells us where the
3603 external symbols start. We don't care about the local symbols. */
3604 if (elf_bad_symtab (abfd
))
3606 extsymcount
= symcount
;
3611 extsymcount
= symcount
- hdr
->sh_info
;
3612 extsymoff
= hdr
->sh_info
;
3615 if (extsymcount
== 0)
3618 /* Read in the symbol table. */
3619 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3621 if (isymbuf
== NULL
)
3624 /* Scan the symbol table looking for SYMDEF. */
3626 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3630 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3635 if (strcmp (name
, symdef
->name
) == 0)
3637 result
= is_global_data_symbol_definition (abfd
, isym
);
3647 /* Add an entry to the .dynamic table. */
3650 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3654 struct elf_link_hash_table
*hash_table
;
3655 const struct elf_backend_data
*bed
;
3657 bfd_size_type newsize
;
3658 bfd_byte
*newcontents
;
3659 Elf_Internal_Dyn dyn
;
3661 hash_table
= elf_hash_table (info
);
3662 if (! is_elf_hash_table (&hash_table
->root
))
3665 if (tag
== DT_RELA
|| tag
== DT_REL
)
3666 hash_table
->dynamic_relocs
= true;
3668 bed
= get_elf_backend_data (hash_table
->dynobj
);
3669 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3670 BFD_ASSERT (s
!= NULL
);
3672 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3673 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3674 if (newcontents
== NULL
)
3678 dyn
.d_un
.d_val
= val
;
3679 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3682 s
->contents
= newcontents
;
3687 /* Strip zero-sized dynamic sections. */
3690 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3692 struct elf_link_hash_table
*hash_table
;
3693 const struct elf_backend_data
*bed
;
3694 asection
*s
, *sdynamic
, **pp
;
3695 asection
*rela_dyn
, *rel_dyn
;
3696 Elf_Internal_Dyn dyn
;
3697 bfd_byte
*extdyn
, *next
;
3698 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3699 bool strip_zero_sized
;
3700 bool strip_zero_sized_plt
;
3702 if (bfd_link_relocatable (info
))
3705 hash_table
= elf_hash_table (info
);
3706 if (!is_elf_hash_table (&hash_table
->root
))
3709 if (!hash_table
->dynobj
)
3712 sdynamic
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3716 bed
= get_elf_backend_data (hash_table
->dynobj
);
3717 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3719 strip_zero_sized
= false;
3720 strip_zero_sized_plt
= false;
3722 /* Strip zero-sized dynamic sections. */
3723 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3724 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3725 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3729 || s
== hash_table
->srelplt
->output_section
3730 || s
== hash_table
->splt
->output_section
))
3733 info
->output_bfd
->section_count
--;
3734 strip_zero_sized
= true;
3739 else if (s
== hash_table
->splt
->output_section
)
3741 s
= hash_table
->splt
;
3742 strip_zero_sized_plt
= true;
3745 s
= hash_table
->srelplt
;
3746 s
->flags
|= SEC_EXCLUDE
;
3747 s
->output_section
= bfd_abs_section_ptr
;
3752 if (strip_zero_sized_plt
&& sdynamic
->size
!= 0)
3753 for (extdyn
= sdynamic
->contents
;
3754 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3757 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3758 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3766 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3767 the procedure linkage table (the .plt section) has been
3769 memmove (extdyn
, next
,
3770 sdynamic
->size
- (next
- sdynamic
->contents
));
3775 if (strip_zero_sized
)
3777 /* Regenerate program headers. */
3778 elf_seg_map (info
->output_bfd
) = NULL
;
3779 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
,
3786 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3787 1 if a DT_NEEDED tag already exists, and 0 on success. */
3790 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3792 struct elf_link_hash_table
*hash_table
;
3796 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3799 hash_table
= elf_hash_table (info
);
3800 soname
= elf_dt_name (abfd
);
3801 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, false);
3802 if (strindex
== (size_t) -1)
3805 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3808 const struct elf_backend_data
*bed
;
3811 bed
= get_elf_backend_data (hash_table
->dynobj
);
3812 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3813 if (sdyn
!= NULL
&& sdyn
->size
!= 0)
3814 for (extdyn
= sdyn
->contents
;
3815 extdyn
< sdyn
->contents
+ sdyn
->size
;
3816 extdyn
+= bed
->s
->sizeof_dyn
)
3818 Elf_Internal_Dyn dyn
;
3820 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3821 if (dyn
.d_tag
== DT_NEEDED
3822 && dyn
.d_un
.d_val
== strindex
)
3824 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3830 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3833 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3839 /* Return true if SONAME is on the needed list between NEEDED and STOP
3840 (or the end of list if STOP is NULL), and needed by a library that
3844 on_needed_list (const char *soname
,
3845 struct bfd_link_needed_list
*needed
,
3846 struct bfd_link_needed_list
*stop
)
3848 struct bfd_link_needed_list
*look
;
3849 for (look
= needed
; look
!= stop
; look
= look
->next
)
3850 if (strcmp (soname
, look
->name
) == 0
3851 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3852 /* If needed by a library that itself is not directly
3853 needed, recursively check whether that library is
3854 indirectly needed. Since we add DT_NEEDED entries to
3855 the end of the list, library dependencies appear after
3856 the library. Therefore search prior to the current
3857 LOOK, preventing possible infinite recursion. */
3858 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3864 /* Sort symbol by value, section, size, and type. */
3866 elf_sort_symbol (const void *arg1
, const void *arg2
)
3868 const struct elf_link_hash_entry
*h1
;
3869 const struct elf_link_hash_entry
*h2
;
3870 bfd_signed_vma vdiff
;
3875 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3876 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3877 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3879 return vdiff
> 0 ? 1 : -1;
3881 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3885 /* Sort so that sized symbols are selected over zero size symbols. */
3886 vdiff
= h1
->size
- h2
->size
;
3888 return vdiff
> 0 ? 1 : -1;
3890 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3891 if (h1
->type
!= h2
->type
)
3892 return h1
->type
- h2
->type
;
3894 /* If symbols are properly sized and typed, and multiple strong
3895 aliases are not defined in a shared library by the user we
3896 shouldn't get here. Unfortunately linker script symbols like
3897 __bss_start sometimes match a user symbol defined at the start of
3898 .bss without proper size and type. We'd like to preference the
3899 user symbol over reserved system symbols. Sort on leading
3901 n1
= h1
->root
.root
.string
;
3902 n2
= h2
->root
.root
.string
;
3915 /* Final sort on name selects user symbols like '_u' over reserved
3916 system symbols like '_Z' and also will avoid qsort instability. */
3920 /* This function is used to adjust offsets into .dynstr for
3921 dynamic symbols. This is called via elf_link_hash_traverse. */
3924 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3926 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3928 if (h
->dynindx
!= -1)
3929 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3933 /* Assign string offsets in .dynstr, update all structures referencing
3937 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3939 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3940 struct elf_link_local_dynamic_entry
*entry
;
3941 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3942 bfd
*dynobj
= hash_table
->dynobj
;
3945 const struct elf_backend_data
*bed
;
3948 _bfd_elf_strtab_finalize (dynstr
);
3949 size
= _bfd_elf_strtab_size (dynstr
);
3951 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3953 if (info
->callbacks
->examine_strtab
)
3954 info
->callbacks
->examine_strtab (dynstr
);
3956 bed
= get_elf_backend_data (dynobj
);
3957 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3958 BFD_ASSERT (sdyn
!= NULL
);
3960 /* Update all .dynamic entries referencing .dynstr strings. */
3961 for (extdyn
= sdyn
->contents
;
3962 extdyn
< PTR_ADD (sdyn
->contents
, sdyn
->size
);
3963 extdyn
+= bed
->s
->sizeof_dyn
)
3965 Elf_Internal_Dyn dyn
;
3967 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3971 dyn
.d_un
.d_val
= size
;
3981 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3986 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3989 /* Now update local dynamic symbols. */
3990 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3991 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3992 entry
->isym
.st_name
);
3994 /* And the rest of dynamic symbols. */
3995 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3997 /* Adjust version definitions. */
3998 if (elf_tdata (output_bfd
)->cverdefs
)
4003 Elf_Internal_Verdef def
;
4004 Elf_Internal_Verdaux defaux
;
4006 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
4010 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
4012 p
+= sizeof (Elf_External_Verdef
);
4013 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
4015 for (i
= 0; i
< def
.vd_cnt
; ++i
)
4017 _bfd_elf_swap_verdaux_in (output_bfd
,
4018 (Elf_External_Verdaux
*) p
, &defaux
);
4019 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
4021 _bfd_elf_swap_verdaux_out (output_bfd
,
4022 &defaux
, (Elf_External_Verdaux
*) p
);
4023 p
+= sizeof (Elf_External_Verdaux
);
4026 while (def
.vd_next
);
4029 /* Adjust version references. */
4030 if (elf_tdata (output_bfd
)->verref
)
4035 Elf_Internal_Verneed need
;
4036 Elf_Internal_Vernaux needaux
;
4038 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
4042 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
4044 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
4045 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
4046 (Elf_External_Verneed
*) p
);
4047 p
+= sizeof (Elf_External_Verneed
);
4048 for (i
= 0; i
< need
.vn_cnt
; ++i
)
4050 _bfd_elf_swap_vernaux_in (output_bfd
,
4051 (Elf_External_Vernaux
*) p
, &needaux
);
4052 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
4054 _bfd_elf_swap_vernaux_out (output_bfd
,
4056 (Elf_External_Vernaux
*) p
);
4057 p
+= sizeof (Elf_External_Vernaux
);
4060 while (need
.vn_next
);
4066 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4067 The default is to only match when the INPUT and OUTPUT are exactly
4071 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
4072 const bfd_target
*output
)
4074 return input
== output
;
4077 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4078 This version is used when different targets for the same architecture
4079 are virtually identical. */
4082 _bfd_elf_relocs_compatible (const bfd_target
*input
,
4083 const bfd_target
*output
)
4085 const struct elf_backend_data
*obed
, *ibed
;
4087 if (input
== output
)
4090 ibed
= xvec_get_elf_backend_data (input
);
4091 obed
= xvec_get_elf_backend_data (output
);
4093 if (ibed
->arch
!= obed
->arch
)
4096 /* If both backends are using this function, deem them compatible. */
4097 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
4100 /* Make a special call to the linker "notice" function to tell it that
4101 we are about to handle an as-needed lib, or have finished
4102 processing the lib. */
4105 _bfd_elf_notice_as_needed (bfd
*ibfd
,
4106 struct bfd_link_info
*info
,
4107 enum notice_asneeded_action act
)
4109 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
4112 /* Call ACTION on each relocation in an ELF object file. */
4115 _bfd_elf_link_iterate_on_relocs
4116 (bfd
*abfd
, struct bfd_link_info
*info
,
4117 bool (*action
) (bfd
*, struct bfd_link_info
*, asection
*,
4118 const Elf_Internal_Rela
*))
4120 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4121 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4123 /* If this object is the same format as the output object, and it is
4124 not a shared library, then let the backend look through the
4127 This is required to build global offset table entries and to
4128 arrange for dynamic relocs. It is not required for the
4129 particular common case of linking non PIC code, even when linking
4130 against shared libraries, but unfortunately there is no way of
4131 knowing whether an object file has been compiled PIC or not.
4132 Looking through the relocs is not particularly time consuming.
4133 The problem is that we must either (1) keep the relocs in memory,
4134 which causes the linker to require additional runtime memory or
4135 (2) read the relocs twice from the input file, which wastes time.
4136 This would be a good case for using mmap.
4138 I have no idea how to handle linking PIC code into a file of a
4139 different format. It probably can't be done. */
4140 if ((abfd
->flags
& DYNAMIC
) == 0
4141 && is_elf_hash_table (&htab
->root
)
4142 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4143 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4147 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4149 Elf_Internal_Rela
*internal_relocs
;
4152 /* Don't check relocations in excluded sections. Don't do
4153 anything special with non-loaded, non-alloced sections.
4154 In particular, any relocs in such sections should not
4155 affect GOT and PLT reference counting (ie. we don't
4156 allow them to create GOT or PLT entries), there's no
4157 possibility or desire to optimize TLS relocs, and
4158 there's not much point in propagating relocs to shared
4159 libs that the dynamic linker won't relocate. */
4160 if ((o
->flags
& SEC_ALLOC
) == 0
4161 || (o
->flags
& SEC_RELOC
) == 0
4162 || (o
->flags
& SEC_EXCLUDE
) != 0
4163 || o
->reloc_count
== 0
4164 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4165 && (o
->flags
& SEC_DEBUGGING
) != 0)
4166 || bfd_is_abs_section (o
->output_section
))
4169 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
4172 _bfd_link_keep_memory (info
));
4173 if (internal_relocs
== NULL
)
4176 ok
= action (abfd
, info
, o
, internal_relocs
);
4178 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4179 free (internal_relocs
);
4189 /* Check relocations in an ELF object file. This is called after
4190 all input files have been opened. */
4193 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
4195 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4196 if (bed
->check_relocs
!= NULL
)
4197 return _bfd_elf_link_iterate_on_relocs (abfd
, info
,
4202 /* Add symbols from an ELF object file to the linker hash table. */
4205 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4207 Elf_Internal_Ehdr
*ehdr
;
4208 Elf_Internal_Shdr
*hdr
;
4212 struct elf_link_hash_entry
**sym_hash
;
4214 Elf_External_Versym
*extversym
= NULL
;
4215 Elf_External_Versym
*extversym_end
= NULL
;
4216 Elf_External_Versym
*ever
;
4217 struct elf_link_hash_entry
*weaks
;
4218 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4219 size_t nondeflt_vers_cnt
= 0;
4220 Elf_Internal_Sym
*isymbuf
= NULL
;
4221 Elf_Internal_Sym
*isym
;
4222 Elf_Internal_Sym
*isymend
;
4223 const struct elf_backend_data
*bed
;
4225 struct elf_link_hash_table
*htab
;
4226 void *alloc_mark
= NULL
;
4227 struct bfd_hash_entry
**old_table
= NULL
;
4228 unsigned int old_size
= 0;
4229 unsigned int old_count
= 0;
4230 void *old_tab
= NULL
;
4232 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4233 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4234 void *old_strtab
= NULL
;
4239 htab
= elf_hash_table (info
);
4240 bed
= get_elf_backend_data (abfd
);
4242 if (elf_use_dt_symtab_p (abfd
))
4244 bfd_set_error (bfd_error_wrong_format
);
4248 if ((abfd
->flags
& DYNAMIC
) == 0)
4254 /* You can't use -r against a dynamic object. Also, there's no
4255 hope of using a dynamic object which does not exactly match
4256 the format of the output file. */
4257 if (bfd_link_relocatable (info
)
4258 || !is_elf_hash_table (&htab
->root
)
4259 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4261 if (bfd_link_relocatable (info
))
4262 bfd_set_error (bfd_error_invalid_operation
);
4264 bfd_set_error (bfd_error_wrong_format
);
4269 ehdr
= elf_elfheader (abfd
);
4270 if (info
->warn_alternate_em
4271 && bed
->elf_machine_code
!= ehdr
->e_machine
4272 && ((bed
->elf_machine_alt1
!= 0
4273 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4274 || (bed
->elf_machine_alt2
!= 0
4275 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4277 /* xgettext:c-format */
4278 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4279 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4281 /* As a GNU extension, any input sections which are named
4282 .gnu.warning.SYMBOL are treated as warning symbols for the given
4283 symbol. This differs from .gnu.warning sections, which generate
4284 warnings when they are included in an output file. */
4285 /* PR 12761: Also generate this warning when building shared libraries. */
4286 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4290 name
= bfd_section_name (s
);
4291 if (startswith (name
, ".gnu.warning."))
4296 name
+= sizeof ".gnu.warning." - 1;
4298 /* If this is a shared object, then look up the symbol
4299 in the hash table. If it is there, and it is already
4300 been defined, then we will not be using the entry
4301 from this shared object, so we don't need to warn.
4302 FIXME: If we see the definition in a regular object
4303 later on, we will warn, but we shouldn't. The only
4304 fix is to keep track of what warnings we are supposed
4305 to emit, and then handle them all at the end of the
4309 struct elf_link_hash_entry
*h
;
4311 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
4313 /* FIXME: What about bfd_link_hash_common? */
4315 && (h
->root
.type
== bfd_link_hash_defined
4316 || h
->root
.type
== bfd_link_hash_defweak
))
4321 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4325 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4330 if (! (_bfd_generic_link_add_one_symbol
4331 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4332 false, bed
->collect
, NULL
)))
4335 if (bfd_link_executable (info
))
4337 /* Clobber the section size so that the warning does
4338 not get copied into the output file. */
4341 /* Also set SEC_EXCLUDE, so that symbols defined in
4342 the warning section don't get copied to the output. */
4343 s
->flags
|= SEC_EXCLUDE
;
4348 just_syms
= ((s
= abfd
->sections
) != NULL
4349 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4354 /* If we are creating a shared library, create all the dynamic
4355 sections immediately. We need to attach them to something,
4356 so we attach them to this BFD, provided it is the right
4357 format and is not from ld --just-symbols. Always create the
4358 dynamic sections for -E/--dynamic-list. FIXME: If there
4359 are no input BFD's of the same format as the output, we can't
4360 make a shared library. */
4362 && (bfd_link_pic (info
)
4363 || (!bfd_link_relocatable (info
)
4365 && (info
->export_dynamic
|| info
->dynamic
)))
4366 && is_elf_hash_table (&htab
->root
)
4367 && info
->output_bfd
->xvec
== abfd
->xvec
4368 && !htab
->dynamic_sections_created
)
4370 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4374 else if (!is_elf_hash_table (&htab
->root
))
4378 const char *soname
= NULL
;
4380 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4381 const Elf_Internal_Phdr
*phdr
;
4382 struct elf_link_loaded_list
*loaded_lib
;
4384 /* ld --just-symbols and dynamic objects don't mix very well.
4385 ld shouldn't allow it. */
4389 /* If this dynamic lib was specified on the command line with
4390 --as-needed in effect, then we don't want to add a DT_NEEDED
4391 tag unless the lib is actually used. Similary for libs brought
4392 in by another lib's DT_NEEDED. When --no-add-needed is used
4393 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4394 any dynamic library in DT_NEEDED tags in the dynamic lib at
4396 add_needed
= (elf_dyn_lib_class (abfd
)
4397 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4398 | DYN_NO_NEEDED
)) == 0;
4400 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4401 if (s
!= NULL
&& s
->size
!= 0 && (s
->flags
& SEC_HAS_CONTENTS
) != 0)
4405 unsigned int elfsec
;
4406 unsigned long shlink
;
4408 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4415 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4416 if (elfsec
== SHN_BAD
)
4417 goto error_free_dyn
;
4418 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4420 for (extdyn
= dynbuf
;
4421 (size_t) (dynbuf
+ s
->size
- extdyn
) >= bed
->s
->sizeof_dyn
;
4422 extdyn
+= bed
->s
->sizeof_dyn
)
4424 Elf_Internal_Dyn dyn
;
4426 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4427 if (dyn
.d_tag
== DT_SONAME
)
4429 unsigned int tagv
= dyn
.d_un
.d_val
;
4430 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4432 goto error_free_dyn
;
4434 if (dyn
.d_tag
== DT_NEEDED
)
4436 struct bfd_link_needed_list
*n
, **pn
;
4438 unsigned int tagv
= dyn
.d_un
.d_val
;
4439 size_t amt
= sizeof (struct bfd_link_needed_list
);
4441 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4442 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4443 if (n
== NULL
|| fnm
== NULL
)
4444 goto error_free_dyn
;
4445 amt
= strlen (fnm
) + 1;
4446 anm
= (char *) bfd_alloc (abfd
, amt
);
4448 goto error_free_dyn
;
4449 memcpy (anm
, fnm
, amt
);
4453 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4457 if (dyn
.d_tag
== DT_RUNPATH
)
4459 struct bfd_link_needed_list
*n
, **pn
;
4461 unsigned int tagv
= dyn
.d_un
.d_val
;
4462 size_t amt
= sizeof (struct bfd_link_needed_list
);
4464 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4465 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4466 if (n
== NULL
|| fnm
== NULL
)
4467 goto error_free_dyn
;
4468 amt
= strlen (fnm
) + 1;
4469 anm
= (char *) bfd_alloc (abfd
, amt
);
4471 goto error_free_dyn
;
4472 memcpy (anm
, fnm
, amt
);
4476 for (pn
= & runpath
;
4482 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4483 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4485 struct bfd_link_needed_list
*n
, **pn
;
4487 unsigned int tagv
= dyn
.d_un
.d_val
;
4488 size_t amt
= sizeof (struct bfd_link_needed_list
);
4490 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4491 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4492 if (n
== NULL
|| fnm
== NULL
)
4493 goto error_free_dyn
;
4494 amt
= strlen (fnm
) + 1;
4495 anm
= (char *) bfd_alloc (abfd
, amt
);
4497 goto error_free_dyn
;
4498 memcpy (anm
, fnm
, amt
);
4508 if (dyn
.d_tag
== DT_AUDIT
)
4510 unsigned int tagv
= dyn
.d_un
.d_val
;
4511 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4513 if (dyn
.d_tag
== DT_FLAGS_1
)
4514 elf_tdata (abfd
)->is_pie
= (dyn
.d_un
.d_val
& DF_1_PIE
) != 0;
4520 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4521 frees all more recently bfd_alloc'd blocks as well. */
4527 struct bfd_link_needed_list
**pn
;
4528 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4533 /* If we have a PT_GNU_RELRO program header, mark as read-only
4534 all sections contained fully therein. This makes relro
4535 shared library sections appear as they will at run-time. */
4536 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4537 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4538 if (phdr
->p_type
== PT_GNU_RELRO
)
4540 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4542 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4544 if ((s
->flags
& SEC_ALLOC
) != 0
4545 && s
->vma
* opb
>= phdr
->p_vaddr
4546 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4547 s
->flags
|= SEC_READONLY
;
4552 /* We do not want to include any of the sections in a dynamic
4553 object in the output file. We hack by simply clobbering the
4554 list of sections in the BFD. This could be handled more
4555 cleanly by, say, a new section flag; the existing
4556 SEC_NEVER_LOAD flag is not the one we want, because that one
4557 still implies that the section takes up space in the output
4559 bfd_section_list_clear (abfd
);
4561 /* Find the name to use in a DT_NEEDED entry that refers to this
4562 object. If the object has a DT_SONAME entry, we use it.
4563 Otherwise, if the generic linker stuck something in
4564 elf_dt_name, we use that. Otherwise, we just use the file
4566 if (soname
== NULL
|| *soname
== '\0')
4568 soname
= elf_dt_name (abfd
);
4569 if (soname
== NULL
|| *soname
== '\0')
4570 soname
= bfd_get_filename (abfd
);
4573 /* Save the SONAME because sometimes the linker emulation code
4574 will need to know it. */
4575 elf_dt_name (abfd
) = soname
;
4577 /* If we have already included this dynamic object in the
4578 link, just ignore it. There is no reason to include a
4579 particular dynamic object more than once. */
4580 for (loaded_lib
= htab
->dyn_loaded
;
4582 loaded_lib
= loaded_lib
->next
)
4584 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4588 /* Create dynamic sections for backends that require that be done
4589 before setup_gnu_properties. */
4591 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4594 /* Save the DT_AUDIT entry for the linker emulation code. */
4595 elf_dt_audit (abfd
) = audit
;
4598 /* If this is a dynamic object, we always link against the .dynsym
4599 symbol table, not the .symtab symbol table. The dynamic linker
4600 will only see the .dynsym symbol table, so there is no reason to
4601 look at .symtab for a dynamic object. */
4603 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4604 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4606 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4608 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4610 /* The sh_info field of the symtab header tells us where the
4611 external symbols start. We don't care about the local symbols at
4613 if (elf_bad_symtab (abfd
))
4615 extsymcount
= symcount
;
4620 extsymcount
= symcount
- hdr
->sh_info
;
4621 extsymoff
= hdr
->sh_info
;
4624 sym_hash
= elf_sym_hashes (abfd
);
4625 if (extsymcount
!= 0)
4627 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4629 if (isymbuf
== NULL
)
4632 if (sym_hash
== NULL
)
4634 /* We store a pointer to the hash table entry for each
4636 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4637 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4638 if (sym_hash
== NULL
)
4639 goto error_free_sym
;
4640 elf_sym_hashes (abfd
) = sym_hash
;
4646 /* Read in any version definitions. */
4647 if (!_bfd_elf_slurp_version_tables (abfd
,
4648 info
->default_imported_symver
))
4649 goto error_free_sym
;
4651 /* Read in the symbol versions, but don't bother to convert them
4652 to internal format. */
4653 if (elf_dynversym (abfd
) != 0)
4655 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4656 bfd_size_type amt
= versymhdr
->sh_size
;
4658 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4659 goto error_free_sym
;
4660 extversym
= (Elf_External_Versym
*)
4661 _bfd_malloc_and_read (abfd
, amt
, amt
);
4662 if (extversym
== NULL
)
4663 goto error_free_sym
;
4664 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4668 /* If we are loading an as-needed shared lib, save the symbol table
4669 state before we start adding symbols. If the lib turns out
4670 to be unneeded, restore the state. */
4671 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4676 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4678 struct bfd_hash_entry
*p
;
4679 struct elf_link_hash_entry
*h
;
4681 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4683 h
= (struct elf_link_hash_entry
*) p
;
4684 entsize
+= htab
->root
.table
.entsize
;
4685 if (h
->root
.type
== bfd_link_hash_warning
)
4687 entsize
+= htab
->root
.table
.entsize
;
4688 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4690 if (h
->root
.type
== bfd_link_hash_common
)
4691 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4695 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4696 old_tab
= bfd_malloc (tabsize
+ entsize
);
4697 if (old_tab
== NULL
)
4698 goto error_free_vers
;
4700 /* Remember the current objalloc pointer, so that all mem for
4701 symbols added can later be reclaimed. */
4702 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4703 if (alloc_mark
== NULL
)
4704 goto error_free_vers
;
4706 /* Make a special call to the linker "notice" function to
4707 tell it that we are about to handle an as-needed lib. */
4708 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4709 goto error_free_vers
;
4711 /* Clone the symbol table. Remember some pointers into the
4712 symbol table, and dynamic symbol count. */
4713 old_ent
= (char *) old_tab
+ tabsize
;
4714 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4715 old_undefs
= htab
->root
.undefs
;
4716 old_undefs_tail
= htab
->root
.undefs_tail
;
4717 old_table
= htab
->root
.table
.table
;
4718 old_size
= htab
->root
.table
.size
;
4719 old_count
= htab
->root
.table
.count
;
4721 if (htab
->dynstr
!= NULL
)
4723 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4724 if (old_strtab
== NULL
)
4725 goto error_free_vers
;
4728 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4730 struct bfd_hash_entry
*p
;
4731 struct elf_link_hash_entry
*h
;
4733 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4735 h
= (struct elf_link_hash_entry
*) p
;
4736 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4737 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4738 if (h
->root
.type
== bfd_link_hash_warning
)
4740 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4741 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4742 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4744 if (h
->root
.type
== bfd_link_hash_common
)
4746 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4747 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4754 if (extversym
== NULL
)
4756 else if (extversym
+ extsymoff
< extversym_end
)
4757 ever
= extversym
+ extsymoff
;
4760 /* xgettext:c-format */
4761 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4762 abfd
, (long) extsymoff
,
4763 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4764 bfd_set_error (bfd_error_bad_value
);
4765 goto error_free_vers
;
4768 if (!bfd_link_relocatable (info
)
4769 && abfd
->lto_slim_object
)
4772 (_("%pB: plugin needed to handle lto object"), abfd
);
4775 for (isym
= isymbuf
, isymend
= PTR_ADD (isymbuf
, extsymcount
);
4777 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4781 asection
*sec
, *new_sec
;
4784 struct elf_link_hash_entry
*h
;
4785 struct elf_link_hash_entry
*hi
;
4787 bool size_change_ok
;
4788 bool type_change_ok
;
4794 unsigned int old_alignment
;
4795 unsigned int shindex
;
4801 flags
= BSF_NO_FLAGS
;
4803 value
= isym
->st_value
;
4804 common
= bed
->common_definition (isym
);
4805 if (common
&& info
->inhibit_common_definition
)
4807 /* Treat common symbol as undefined for --no-define-common. */
4808 isym
->st_shndx
= SHN_UNDEF
;
4813 bind
= ELF_ST_BIND (isym
->st_info
);
4817 /* This should be impossible, since ELF requires that all
4818 global symbols follow all local symbols, and that sh_info
4819 point to the first global symbol. Unfortunately, Irix 5
4821 if (elf_bad_symtab (abfd
))
4824 /* If we aren't prepared to handle locals within the globals
4825 then we'll likely segfault on a NULL symbol hash if the
4826 symbol is ever referenced in relocations. */
4827 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4828 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4829 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4830 " (>= sh_info of %lu)"),
4831 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4834 /* Dynamic object relocations are not processed by ld, so
4835 ld won't run into the problem mentioned above. */
4838 bfd_set_error (bfd_error_bad_value
);
4839 goto error_free_vers
;
4842 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4850 case STB_GNU_UNIQUE
:
4851 flags
= BSF_GNU_UNIQUE
;
4855 /* Leave it up to the processor backend. */
4859 if (isym
->st_shndx
== SHN_UNDEF
)
4860 sec
= bfd_und_section_ptr
;
4861 else if (isym
->st_shndx
== SHN_ABS
)
4862 sec
= bfd_abs_section_ptr
;
4863 else if (isym
->st_shndx
== SHN_COMMON
)
4865 sec
= bfd_com_section_ptr
;
4866 /* What ELF calls the size we call the value. What ELF
4867 calls the value we call the alignment. */
4868 value
= isym
->st_size
;
4872 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4874 sec
= bfd_abs_section_ptr
;
4875 else if (discarded_section (sec
))
4877 /* Symbols from discarded section are undefined. We keep
4879 sec
= bfd_und_section_ptr
;
4881 isym
->st_shndx
= SHN_UNDEF
;
4883 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4887 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4890 goto error_free_vers
;
4892 if (isym
->st_shndx
== SHN_COMMON
4893 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4895 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4899 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4901 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4903 goto error_free_vers
;
4907 else if (isym
->st_shndx
== SHN_COMMON
4908 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4909 && !bfd_link_relocatable (info
))
4911 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4915 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4916 | SEC_LINKER_CREATED
);
4917 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4919 goto error_free_vers
;
4923 else if (bed
->elf_add_symbol_hook
)
4925 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4927 goto error_free_vers
;
4929 /* The hook function sets the name to NULL if this symbol
4930 should be skipped for some reason. */
4935 /* Sanity check that all possibilities were handled. */
4939 /* Silently discard TLS symbols from --just-syms. There's
4940 no way to combine a static TLS block with a new TLS block
4941 for this executable. */
4942 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4943 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4946 if (bfd_is_und_section (sec
)
4947 || bfd_is_com_section (sec
))
4952 size_change_ok
= false;
4953 type_change_ok
= bed
->type_change_ok
;
4960 if (is_elf_hash_table (&htab
->root
))
4962 Elf_Internal_Versym iver
;
4963 unsigned int vernum
= 0;
4968 if (info
->default_imported_symver
)
4969 /* Use the default symbol version created earlier. */
4970 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4974 else if (ever
>= extversym_end
)
4976 /* xgettext:c-format */
4977 _bfd_error_handler (_("%pB: not enough version information"),
4979 bfd_set_error (bfd_error_bad_value
);
4980 goto error_free_vers
;
4983 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4985 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4987 /* If this is a hidden symbol, or if it is not version
4988 1, we append the version name to the symbol name.
4989 However, we do not modify a non-hidden absolute symbol
4990 if it is not a function, because it might be the version
4991 symbol itself. FIXME: What if it isn't? */
4992 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4994 && (!bfd_is_abs_section (sec
)
4995 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4998 size_t namelen
, verlen
, newlen
;
5001 if (isym
->st_shndx
!= SHN_UNDEF
)
5003 if (vernum
> elf_tdata (abfd
)->cverdefs
)
5005 else if (vernum
> 1)
5007 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
5014 /* xgettext:c-format */
5015 (_("%pB: %s: invalid version %u (max %d)"),
5017 elf_tdata (abfd
)->cverdefs
);
5018 bfd_set_error (bfd_error_bad_value
);
5019 goto error_free_vers
;
5024 /* We cannot simply test for the number of
5025 entries in the VERNEED section since the
5026 numbers for the needed versions do not start
5028 Elf_Internal_Verneed
*t
;
5031 for (t
= elf_tdata (abfd
)->verref
;
5035 Elf_Internal_Vernaux
*a
;
5037 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5039 if (a
->vna_other
== vernum
)
5041 verstr
= a
->vna_nodename
;
5051 /* xgettext:c-format */
5052 (_("%pB: %s: invalid needed version %d"),
5053 abfd
, name
, vernum
);
5054 bfd_set_error (bfd_error_bad_value
);
5055 goto error_free_vers
;
5059 namelen
= strlen (name
);
5060 verlen
= strlen (verstr
);
5061 newlen
= namelen
+ verlen
+ 2;
5062 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5063 && isym
->st_shndx
!= SHN_UNDEF
)
5066 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
5067 if (newname
== NULL
)
5068 goto error_free_vers
;
5069 memcpy (newname
, name
, namelen
);
5070 p
= newname
+ namelen
;
5072 /* If this is a defined non-hidden version symbol,
5073 we add another @ to the name. This indicates the
5074 default version of the symbol. */
5075 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5076 && isym
->st_shndx
!= SHN_UNDEF
)
5078 memcpy (p
, verstr
, verlen
+ 1);
5083 /* If this symbol has default visibility and the user has
5084 requested we not re-export it, then mark it as hidden. */
5085 if (!bfd_is_und_section (sec
)
5088 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
5089 isym
->st_other
= (STV_HIDDEN
5090 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
5092 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
5093 sym_hash
, &old_bfd
, &old_weak
,
5094 &old_alignment
, &skip
, &override
,
5095 &type_change_ok
, &size_change_ok
,
5097 goto error_free_vers
;
5102 /* Override a definition only if the new symbol matches the
5104 if (override
&& matched
)
5108 while (h
->root
.type
== bfd_link_hash_indirect
5109 || h
->root
.type
== bfd_link_hash_warning
)
5110 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5112 if (h
->versioned
!= unversioned
5113 && elf_tdata (abfd
)->verdef
!= NULL
5116 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
5119 if (! (_bfd_generic_link_add_one_symbol
5120 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
5121 NULL
, false, bed
->collect
,
5122 (struct bfd_link_hash_entry
**) sym_hash
)))
5123 goto error_free_vers
;
5126 /* We need to make sure that indirect symbol dynamic flags are
5129 while (h
->root
.type
== bfd_link_hash_indirect
5130 || h
->root
.type
== bfd_link_hash_warning
)
5131 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5135 /* Setting the index to -3 tells elf_link_output_extsym that
5136 this symbol is defined in a discarded section. */
5137 if (discarded
&& is_elf_hash_table (&htab
->root
))
5140 new_weak
= (flags
& BSF_WEAK
) != 0;
5144 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
5145 && is_elf_hash_table (&htab
->root
)
5146 && h
->u
.alias
== NULL
)
5148 /* Keep a list of all weak defined non function symbols from
5149 a dynamic object, using the alias field. Later in this
5150 function we will set the alias field to the correct
5151 value. We only put non-function symbols from dynamic
5152 objects on this list, because that happens to be the only
5153 time we need to know the normal symbol corresponding to a
5154 weak symbol, and the information is time consuming to
5155 figure out. If the alias field is not already NULL,
5156 then this symbol was already defined by some previous
5157 dynamic object, and we will be using that previous
5158 definition anyhow. */
5164 /* Set the alignment of a common symbol. */
5165 if ((common
|| bfd_is_com_section (sec
))
5166 && h
->root
.type
== bfd_link_hash_common
)
5171 align
= bfd_log2 (isym
->st_value
);
5174 /* The new symbol is a common symbol in a shared object.
5175 We need to get the alignment from the section. */
5176 align
= new_sec
->alignment_power
;
5178 if (align
> old_alignment
)
5179 h
->root
.u
.c
.p
->alignment_power
= align
;
5181 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5184 if (is_elf_hash_table (&htab
->root
))
5186 /* Set a flag in the hash table entry indicating the type of
5187 reference or definition we just found. A dynamic symbol
5188 is one which is referenced or defined by both a regular
5189 object and a shared object. */
5190 bool dynsym
= false;
5192 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5193 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5195 /* Except for this flag to track nonweak references. */
5197 && bind
!= STB_WEAK
)
5198 h
->ref_ir_nonweak
= 1;
5205 if (bind
!= STB_WEAK
)
5206 h
->ref_regular_nonweak
= 1;
5223 hi
->ref_dynamic
= 1;
5228 hi
->def_dynamic
= 1;
5232 /* If an indirect symbol has been forced local, don't
5233 make the real symbol dynamic. */
5234 if (h
!= hi
&& hi
->forced_local
)
5238 if (bfd_link_dll (info
)
5248 && weakdef (h
)->dynindx
!= -1))
5252 /* Check to see if we need to add an indirect symbol for
5253 the default name. */
5255 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5257 && hi
->versioned
== versioned_hidden
))
5258 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5259 sec
, value
, &old_bfd
, &dynsym
))
5260 goto error_free_vers
;
5262 /* Check the alignment when a common symbol is involved. This
5263 can change when a common symbol is overridden by a normal
5264 definition or a common symbol is ignored due to the old
5265 normal definition. We need to make sure the maximum
5266 alignment is maintained. */
5267 if ((old_alignment
|| common
)
5268 && h
->root
.type
!= bfd_link_hash_common
)
5270 unsigned int common_align
;
5271 unsigned int normal_align
;
5272 unsigned int symbol_align
;
5276 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5277 || h
->root
.type
== bfd_link_hash_defweak
);
5279 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5280 if (h
->root
.u
.def
.section
->owner
!= NULL
5281 && (h
->root
.u
.def
.section
->owner
->flags
5282 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5284 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5285 if (normal_align
> symbol_align
)
5286 normal_align
= symbol_align
;
5289 normal_align
= symbol_align
;
5293 common_align
= old_alignment
;
5294 common_bfd
= old_bfd
;
5299 common_align
= bfd_log2 (isym
->st_value
);
5301 normal_bfd
= old_bfd
;
5304 if (normal_align
< common_align
)
5306 /* PR binutils/2735 */
5307 if (normal_bfd
== NULL
)
5309 /* xgettext:c-format */
5310 (_("warning: alignment %u of common symbol `%s' in %pB is"
5311 " greater than the alignment (%u) of its section %pA"),
5312 1 << common_align
, name
, common_bfd
,
5313 1 << normal_align
, h
->root
.u
.def
.section
);
5316 /* xgettext:c-format */
5317 (_("warning: alignment %u of normal symbol `%s' in %pB"
5318 " is smaller than %u used by the common definition in %pB"),
5319 1 << normal_align
, name
, normal_bfd
,
5320 1 << common_align
, common_bfd
);
5322 /* PR 30499: make sure that users understand that this warning is serious. */
5324 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5328 /* Remember the symbol size if it isn't undefined. */
5329 if (isym
->st_size
!= 0
5330 && isym
->st_shndx
!= SHN_UNDEF
5331 && (definition
|| h
->size
== 0))
5334 && h
->size
!= isym
->st_size
5335 && ! size_change_ok
)
5338 /* xgettext:c-format */
5339 (_("warning: size of symbol `%s' changed"
5340 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5341 name
, (uint64_t) h
->size
, old_bfd
,
5342 (uint64_t) isym
->st_size
, abfd
);
5344 /* PR 30499: make sure that users understand that this warning is serious. */
5346 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5349 h
->size
= isym
->st_size
;
5352 /* If this is a common symbol, then we always want H->SIZE
5353 to be the size of the common symbol. The code just above
5354 won't fix the size if a common symbol becomes larger. We
5355 don't warn about a size change here, because that is
5356 covered by --warn-common. Allow changes between different
5358 if (h
->root
.type
== bfd_link_hash_common
)
5359 h
->size
= h
->root
.u
.c
.size
;
5361 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5362 && ((definition
&& !new_weak
)
5363 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5364 || h
->type
== STT_NOTYPE
))
5366 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5368 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5370 if (type
== STT_GNU_IFUNC
5371 && (abfd
->flags
& DYNAMIC
) != 0)
5374 if (h
->type
!= type
)
5376 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5377 /* xgettext:c-format */
5379 (_("warning: type of symbol `%s' changed"
5380 " from %d to %d in %pB"),
5381 name
, h
->type
, type
, abfd
);
5387 /* Merge st_other field. */
5388 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5389 definition
, dynamic
);
5391 /* We don't want to make debug symbol dynamic. */
5393 && (sec
->flags
& SEC_DEBUGGING
)
5394 && !bfd_link_relocatable (info
))
5397 /* Nor should we make plugin symbols dynamic. */
5398 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5403 h
->target_internal
= isym
->st_target_internal
;
5404 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5407 /* Don't add indirect symbols for .symver x, x@FOO aliases
5408 in IR. Since all data or text symbols in IR have the
5409 same type, value and section, we can't tell if a symbol
5410 is an alias of another symbol by their types, values and
5414 && (abfd
->flags
& BFD_PLUGIN
) == 0)
5416 char *p
= strchr (name
, ELF_VER_CHR
);
5417 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5419 /* Queue non-default versions so that .symver x, x@FOO
5420 aliases can be checked. */
5423 size_t amt
= ((isymend
- isym
+ 1)
5424 * sizeof (struct elf_link_hash_entry
*));
5426 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5428 goto error_free_vers
;
5430 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5434 if (dynsym
&& h
->dynindx
== -1)
5436 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5437 goto error_free_vers
;
5439 && weakdef (h
)->dynindx
== -1)
5441 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5442 goto error_free_vers
;
5445 else if (h
->dynindx
!= -1)
5446 /* If the symbol already has a dynamic index, but
5447 visibility says it should not be visible, turn it into
5449 switch (ELF_ST_VISIBILITY (h
->other
))
5453 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
5461 && h
->root
.type
!= bfd_link_hash_indirect
5463 && h
->ref_regular_nonweak
)
5465 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5466 && h
->ref_ir_nonweak
5467 && !info
->lto_all_symbols_read
)
5468 || (h
->ref_dynamic_nonweak
5469 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5470 && !on_needed_list (elf_dt_name (abfd
),
5471 htab
->needed
, NULL
))))
5473 const char *soname
= elf_dt_name (abfd
);
5475 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5476 h
->root
.root
.string
);
5478 /* A symbol from a library loaded via DT_NEEDED of some
5479 other library is referenced by a regular object.
5480 Add a DT_NEEDED entry for it. Issue an error if
5481 --no-add-needed is used and the reference was not
5484 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5487 /* xgettext:c-format */
5488 (_("%pB: undefined reference to symbol '%s'"),
5490 bfd_set_error (bfd_error_missing_dso
);
5491 goto error_free_vers
;
5494 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5495 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5497 /* Create dynamic sections for backends that require
5498 that be done before setup_gnu_properties. */
5499 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5506 if (info
->lto_plugin_active
5507 && !bfd_link_relocatable (info
)
5508 && (abfd
->flags
& BFD_PLUGIN
) == 0
5514 if (bed
->s
->arch_size
== 32)
5519 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5520 referenced in regular objects so that linker plugin will get
5521 the correct symbol resolution. */
5523 sym_hash
= elf_sym_hashes (abfd
);
5524 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5526 Elf_Internal_Rela
*internal_relocs
;
5527 Elf_Internal_Rela
*rel
, *relend
;
5529 /* Don't check relocations in excluded sections. */
5530 if ((s
->flags
& SEC_RELOC
) == 0
5531 || s
->reloc_count
== 0
5532 || (s
->flags
& SEC_EXCLUDE
) != 0
5533 || ((info
->strip
== strip_all
5534 || info
->strip
== strip_debugger
)
5535 && (s
->flags
& SEC_DEBUGGING
) != 0))
5538 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
5541 _bfd_link_keep_memory (info
));
5542 if (internal_relocs
== NULL
)
5543 goto error_free_vers
;
5545 rel
= internal_relocs
;
5546 relend
= rel
+ s
->reloc_count
;
5547 for ( ; rel
< relend
; rel
++)
5549 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5550 struct elf_link_hash_entry
*h
;
5552 /* Skip local symbols. */
5553 if (r_symndx
< extsymoff
)
5556 h
= sym_hash
[r_symndx
- extsymoff
];
5558 h
->root
.non_ir_ref_regular
= 1;
5561 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5562 free (internal_relocs
);
5571 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5575 /* Restore the symbol table. */
5576 old_ent
= (char *) old_tab
+ tabsize
;
5577 memset (elf_sym_hashes (abfd
), 0,
5578 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5579 htab
->root
.table
.table
= old_table
;
5580 htab
->root
.table
.size
= old_size
;
5581 htab
->root
.table
.count
= old_count
;
5582 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5583 htab
->root
.undefs
= old_undefs
;
5584 htab
->root
.undefs_tail
= old_undefs_tail
;
5585 if (htab
->dynstr
!= NULL
)
5586 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5589 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5591 struct bfd_hash_entry
*p
;
5592 struct elf_link_hash_entry
*h
;
5593 unsigned int non_ir_ref_dynamic
;
5595 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5597 /* Preserve non_ir_ref_dynamic so that this symbol
5598 will be exported when the dynamic lib becomes needed
5599 in the second pass. */
5600 h
= (struct elf_link_hash_entry
*) p
;
5601 if (h
->root
.type
== bfd_link_hash_warning
)
5602 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5603 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5605 h
= (struct elf_link_hash_entry
*) p
;
5606 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5607 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5608 if (h
->root
.type
== bfd_link_hash_warning
)
5610 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5611 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5612 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5614 if (h
->root
.type
== bfd_link_hash_common
)
5616 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5617 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5619 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5623 /* Make a special call to the linker "notice" function to
5624 tell it that symbols added for crefs may need to be removed. */
5625 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5626 goto error_free_vers
;
5629 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5631 free (nondeflt_vers
);
5635 if (old_tab
!= NULL
)
5637 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5638 goto error_free_vers
;
5643 /* Now that all the symbols from this input file are created, if
5644 not performing a relocatable link, handle .symver foo, foo@BAR
5645 such that any relocs against foo become foo@BAR. */
5646 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5650 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5652 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5653 char *shortname
, *p
;
5656 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5658 || (h
->root
.type
!= bfd_link_hash_defined
5659 && h
->root
.type
!= bfd_link_hash_defweak
))
5662 amt
= p
- h
->root
.root
.string
;
5663 shortname
= (char *) bfd_malloc (amt
+ 1);
5665 goto error_free_vers
;
5666 memcpy (shortname
, h
->root
.root
.string
, amt
);
5667 shortname
[amt
] = '\0';
5669 hi
= (struct elf_link_hash_entry
*)
5670 bfd_link_hash_lookup (&htab
->root
, shortname
,
5671 false, false, false);
5673 && hi
->root
.type
== h
->root
.type
5674 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5675 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5677 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
5678 hi
->root
.type
= bfd_link_hash_indirect
;
5679 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5680 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5681 sym_hash
= elf_sym_hashes (abfd
);
5683 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5684 if (sym_hash
[symidx
] == hi
)
5686 sym_hash
[symidx
] = h
;
5692 free (nondeflt_vers
);
5693 nondeflt_vers
= NULL
;
5696 /* Now set the alias field correctly for all the weak defined
5697 symbols we found. The only way to do this is to search all the
5698 symbols. Since we only need the information for non functions in
5699 dynamic objects, that's the only time we actually put anything on
5700 the list WEAKS. We need this information so that if a regular
5701 object refers to a symbol defined weakly in a dynamic object, the
5702 real symbol in the dynamic object is also put in the dynamic
5703 symbols; we also must arrange for both symbols to point to the
5704 same memory location. We could handle the general case of symbol
5705 aliasing, but a general symbol alias can only be generated in
5706 assembler code, handling it correctly would be very time
5707 consuming, and other ELF linkers don't handle general aliasing
5711 struct elf_link_hash_entry
**hpp
;
5712 struct elf_link_hash_entry
**hppend
;
5713 struct elf_link_hash_entry
**sorted_sym_hash
;
5714 struct elf_link_hash_entry
*h
;
5715 size_t sym_count
, amt
;
5717 /* Since we have to search the whole symbol list for each weak
5718 defined symbol, search time for N weak defined symbols will be
5719 O(N^2). Binary search will cut it down to O(NlogN). */
5720 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5721 sorted_sym_hash
= bfd_malloc (amt
);
5722 if (sorted_sym_hash
== NULL
)
5724 sym_hash
= sorted_sym_hash
;
5725 hpp
= elf_sym_hashes (abfd
);
5726 hppend
= hpp
+ extsymcount
;
5728 for (; hpp
< hppend
; hpp
++)
5732 && h
->root
.type
== bfd_link_hash_defined
5733 && !bed
->is_function_type (h
->type
))
5741 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5744 while (weaks
!= NULL
)
5746 struct elf_link_hash_entry
*hlook
;
5749 size_t i
, j
, idx
= 0;
5752 weaks
= hlook
->u
.alias
;
5753 hlook
->u
.alias
= NULL
;
5755 if (hlook
->root
.type
!= bfd_link_hash_defined
5756 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5759 slook
= hlook
->root
.u
.def
.section
;
5760 vlook
= hlook
->root
.u
.def
.value
;
5766 bfd_signed_vma vdiff
;
5768 h
= sorted_sym_hash
[idx
];
5769 vdiff
= vlook
- h
->root
.u
.def
.value
;
5776 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5786 /* We didn't find a value/section match. */
5790 /* With multiple aliases, or when the weak symbol is already
5791 strongly defined, we have multiple matching symbols and
5792 the binary search above may land on any of them. Step
5793 one past the matching symbol(s). */
5796 h
= sorted_sym_hash
[idx
];
5797 if (h
->root
.u
.def
.section
!= slook
5798 || h
->root
.u
.def
.value
!= vlook
)
5802 /* Now look back over the aliases. Since we sorted by size
5803 as well as value and section, we'll choose the one with
5804 the largest size. */
5807 h
= sorted_sym_hash
[idx
];
5809 /* Stop if value or section doesn't match. */
5810 if (h
->root
.u
.def
.section
!= slook
5811 || h
->root
.u
.def
.value
!= vlook
)
5813 else if (h
!= hlook
)
5815 struct elf_link_hash_entry
*t
;
5818 hlook
->is_weakalias
= 1;
5820 if (t
->u
.alias
!= NULL
)
5821 while (t
->u
.alias
!= h
)
5825 /* If the weak definition is in the list of dynamic
5826 symbols, make sure the real definition is put
5828 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5830 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5833 free (sorted_sym_hash
);
5838 /* If the real definition is in the list of dynamic
5839 symbols, make sure the weak definition is put
5840 there as well. If we don't do this, then the
5841 dynamic loader might not merge the entries for the
5842 real definition and the weak definition. */
5843 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5845 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5846 goto err_free_sym_hash
;
5853 free (sorted_sym_hash
);
5856 if (bed
->check_directives
5857 && !(*bed
->check_directives
) (abfd
, info
))
5860 /* If this is a non-traditional link, try to optimize the handling
5861 of the .stab/.stabstr sections. */
5863 && ! info
->traditional_format
5864 && is_elf_hash_table (&htab
->root
)
5865 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5869 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5870 if (stabstr
!= NULL
)
5872 bfd_size_type string_offset
= 0;
5875 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5876 if (startswith (stab
->name
, ".stab")
5877 && (!stab
->name
[5] ||
5878 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5879 && (stab
->flags
& SEC_MERGE
) == 0
5880 && !bfd_is_abs_section (stab
->output_section
))
5882 struct bfd_elf_section_data
*secdata
;
5884 secdata
= elf_section_data (stab
);
5885 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5886 stabstr
, &secdata
->sec_info
,
5889 if (secdata
->sec_info
)
5890 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5895 if (dynamic
&& add_needed
)
5897 /* Add this bfd to the loaded list. */
5898 struct elf_link_loaded_list
*n
;
5900 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5904 n
->next
= htab
->dyn_loaded
;
5905 htab
->dyn_loaded
= n
;
5907 if (dynamic
&& !add_needed
5908 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5909 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5916 free (nondeflt_vers
);
5924 /* Return the linker hash table entry of a symbol that might be
5925 satisfied by an archive symbol. Return -1 on error. */
5927 struct bfd_link_hash_entry
*
5928 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5929 struct bfd_link_info
*info
,
5932 struct bfd_link_hash_entry
*h
;
5936 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
5940 /* If this is a default version (the name contains @@), look up the
5941 symbol again with only one `@' as well as without the version.
5942 The effect is that references to the symbol with and without the
5943 version will be matched by the default symbol in the archive. */
5945 p
= strchr (name
, ELF_VER_CHR
);
5946 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5949 /* First check with only one `@'. */
5950 len
= strlen (name
);
5951 copy
= (char *) bfd_alloc (abfd
, len
);
5953 return (struct bfd_link_hash_entry
*) -1;
5955 first
= p
- name
+ 1;
5956 memcpy (copy
, name
, first
);
5957 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5959 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5962 /* We also need to check references to the symbol without the
5964 copy
[first
- 1] = '\0';
5965 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5968 bfd_release (abfd
, copy
);
5972 /* Add symbols from an ELF archive file to the linker hash table. We
5973 don't use _bfd_generic_link_add_archive_symbols because we need to
5974 handle versioned symbols.
5976 Fortunately, ELF archive handling is simpler than that done by
5977 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5978 oddities. In ELF, if we find a symbol in the archive map, and the
5979 symbol is currently undefined, we know that we must pull in that
5982 Unfortunately, we do have to make multiple passes over the symbol
5983 table until nothing further is resolved. */
5986 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5989 unsigned char *included
= NULL
;
5993 const struct elf_backend_data
*bed
;
5994 struct bfd_link_hash_entry
* (*archive_symbol_lookup
)
5995 (bfd
*, struct bfd_link_info
*, const char *);
5997 if (! bfd_has_map (abfd
))
5999 /* An empty archive is a special case. */
6000 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
6002 bfd_set_error (bfd_error_no_armap
);
6006 /* Keep track of all symbols we know to be already defined, and all
6007 files we know to be already included. This is to speed up the
6008 second and subsequent passes. */
6009 c
= bfd_ardata (abfd
)->symdef_count
;
6012 amt
= c
* sizeof (*included
);
6013 included
= (unsigned char *) bfd_zmalloc (amt
);
6014 if (included
== NULL
)
6017 symdefs
= bfd_ardata (abfd
)->symdefs
;
6018 bed
= get_elf_backend_data (abfd
);
6019 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
6032 symdefend
= symdef
+ c
;
6033 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
6035 struct bfd_link_hash_entry
*h
;
6037 struct bfd_link_hash_entry
*undefs_tail
;
6042 if (symdef
->file_offset
== last
)
6048 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
6049 if (h
== (struct bfd_link_hash_entry
*) -1)
6055 if (h
->type
== bfd_link_hash_undefined
)
6057 /* If the archive element has already been loaded then one
6058 of the symbols defined by that element might have been
6059 made undefined due to being in a discarded section. */
6060 if (is_elf_hash_table (info
->hash
)
6061 && ((struct elf_link_hash_entry
*) h
)->indx
== -3)
6064 else if (h
->type
== bfd_link_hash_common
)
6066 /* We currently have a common symbol. The archive map contains
6067 a reference to this symbol, so we may want to include it. We
6068 only want to include it however, if this archive element
6069 contains a definition of the symbol, not just another common
6072 Unfortunately some archivers (including GNU ar) will put
6073 declarations of common symbols into their archive maps, as
6074 well as real definitions, so we cannot just go by the archive
6075 map alone. Instead we must read in the element's symbol
6076 table and check that to see what kind of symbol definition
6078 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
6083 if (h
->type
!= bfd_link_hash_undefweak
)
6084 /* Symbol must be defined. Don't check it again. */
6089 /* We need to include this archive member. */
6090 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
,
6092 if (element
== NULL
)
6095 if (! bfd_check_format (element
, bfd_object
))
6098 undefs_tail
= info
->hash
->undefs_tail
;
6100 if (!(*info
->callbacks
6101 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
6103 if (!bfd_link_add_symbols (element
, info
))
6106 /* If there are any new undefined symbols, we need to make
6107 another pass through the archive in order to see whether
6108 they can be defined. FIXME: This isn't perfect, because
6109 common symbols wind up on undefs_tail and because an
6110 undefined symbol which is defined later on in this pass
6111 does not require another pass. This isn't a bug, but it
6112 does make the code less efficient than it could be. */
6113 if (undefs_tail
!= info
->hash
->undefs_tail
)
6116 /* Look backward to mark all symbols from this object file
6117 which we have already seen in this pass. */
6121 included
[mark
] = true;
6126 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
6128 /* We mark subsequent symbols from this object file as we go
6129 on through the loop. */
6130 last
= symdef
->file_offset
;
6143 /* Given an ELF BFD, add symbols to the global hash table as
6147 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6149 switch (bfd_get_format (abfd
))
6152 return elf_link_add_object_symbols (abfd
, info
);
6154 return elf_link_add_archive_symbols (abfd
, info
);
6156 bfd_set_error (bfd_error_wrong_format
);
6161 struct hash_codes_info
6163 unsigned long *hashcodes
;
6167 /* This function will be called though elf_link_hash_traverse to store
6168 all hash value of the exported symbols in an array. */
6171 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6173 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
6178 /* Ignore indirect symbols. These are added by the versioning code. */
6179 if (h
->dynindx
== -1)
6182 name
= h
->root
.root
.string
;
6183 if (h
->versioned
>= versioned
)
6185 char *p
= strchr (name
, ELF_VER_CHR
);
6188 alc
= (char *) bfd_malloc (p
- name
+ 1);
6194 memcpy (alc
, name
, p
- name
);
6195 alc
[p
- name
] = '\0';
6200 /* Compute the hash value. */
6201 ha
= bfd_elf_hash (name
);
6203 /* Store the found hash value in the array given as the argument. */
6204 *(inf
->hashcodes
)++ = ha
;
6206 /* And store it in the struct so that we can put it in the hash table
6208 h
->u
.elf_hash_value
= ha
;
6214 struct collect_gnu_hash_codes
6217 const struct elf_backend_data
*bed
;
6218 unsigned long int nsyms
;
6219 unsigned long int maskbits
;
6220 unsigned long int *hashcodes
;
6221 unsigned long int *hashval
;
6222 unsigned long int *indx
;
6223 unsigned long int *counts
;
6227 long int min_dynindx
;
6228 unsigned long int bucketcount
;
6229 unsigned long int symindx
;
6230 long int local_indx
;
6231 long int shift1
, shift2
;
6232 unsigned long int mask
;
6236 /* This function will be called though elf_link_hash_traverse to store
6237 all hash value of the exported symbols in an array. */
6240 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6242 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6247 /* Ignore indirect symbols. These are added by the versioning code. */
6248 if (h
->dynindx
== -1)
6251 /* Ignore also local symbols and undefined symbols. */
6252 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6255 name
= h
->root
.root
.string
;
6256 if (h
->versioned
>= versioned
)
6258 char *p
= strchr (name
, ELF_VER_CHR
);
6261 alc
= (char *) bfd_malloc (p
- name
+ 1);
6267 memcpy (alc
, name
, p
- name
);
6268 alc
[p
- name
] = '\0';
6273 /* Compute the hash value. */
6274 ha
= bfd_elf_gnu_hash (name
);
6276 /* Store the found hash value in the array for compute_bucket_count,
6277 and also for .dynsym reordering purposes. */
6278 s
->hashcodes
[s
->nsyms
] = ha
;
6279 s
->hashval
[h
->dynindx
] = ha
;
6281 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6282 s
->min_dynindx
= h
->dynindx
;
6288 /* This function will be called though elf_link_hash_traverse to do
6289 final dynamic symbol renumbering in case of .gnu.hash.
6290 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6291 to the translation table. */
6294 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6296 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6297 unsigned long int bucket
;
6298 unsigned long int val
;
6300 /* Ignore indirect symbols. */
6301 if (h
->dynindx
== -1)
6304 /* Ignore also local symbols and undefined symbols. */
6305 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6307 if (h
->dynindx
>= s
->min_dynindx
)
6309 if (s
->bed
->record_xhash_symbol
!= NULL
)
6311 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6315 h
->dynindx
= s
->local_indx
++;
6320 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6321 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6322 & ((s
->maskbits
>> s
->shift1
) - 1);
6323 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6325 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6326 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6327 if (s
->counts
[bucket
] == 1)
6328 /* Last element terminates the chain. */
6330 bfd_put_32 (s
->output_bfd
, val
,
6331 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6332 --s
->counts
[bucket
];
6333 if (s
->bed
->record_xhash_symbol
!= NULL
)
6335 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6337 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6340 h
->dynindx
= s
->indx
[bucket
]++;
6344 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6347 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6349 return !(h
->forced_local
6350 || h
->root
.type
== bfd_link_hash_undefined
6351 || h
->root
.type
== bfd_link_hash_undefweak
6352 || ((h
->root
.type
== bfd_link_hash_defined
6353 || h
->root
.type
== bfd_link_hash_defweak
)
6354 && h
->root
.u
.def
.section
->output_section
== NULL
));
6357 /* Array used to determine the number of hash table buckets to use
6358 based on the number of symbols there are. If there are fewer than
6359 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6360 fewer than 37 we use 17 buckets, and so forth. We never use more
6361 than 32771 buckets. */
6363 static const size_t elf_buckets
[] =
6365 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6369 /* Compute bucket count for hashing table. We do not use a static set
6370 of possible tables sizes anymore. Instead we determine for all
6371 possible reasonable sizes of the table the outcome (i.e., the
6372 number of collisions etc) and choose the best solution. The
6373 weighting functions are not too simple to allow the table to grow
6374 without bounds. Instead one of the weighting factors is the size.
6375 Therefore the result is always a good payoff between few collisions
6376 (= short chain lengths) and table size. */
6378 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6379 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6380 unsigned long int nsyms
,
6383 size_t best_size
= 0;
6384 unsigned long int i
;
6390 uint64_t best_chlen
= ~((uint64_t) 0);
6391 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6392 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6393 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6394 unsigned long int *counts
;
6396 unsigned int no_improvement_count
= 0;
6398 /* Possible optimization parameters: if we have NSYMS symbols we say
6399 that the hashing table must at least have NSYMS/4 and at most
6401 minsize
= nsyms
/ 4;
6404 best_size
= maxsize
= nsyms
* 2;
6409 if ((best_size
& 31) == 0)
6413 /* Create array where we count the collisions in. We must use bfd_malloc
6414 since the size could be large. */
6416 amt
*= sizeof (unsigned long int);
6417 counts
= (unsigned long int *) bfd_malloc (amt
);
6421 /* Compute the "optimal" size for the hash table. The criteria is a
6422 minimal chain length. The minor criteria is (of course) the size
6424 for (i
= minsize
; i
< maxsize
; ++i
)
6426 /* Walk through the array of hashcodes and count the collisions. */
6428 unsigned long int j
;
6429 unsigned long int fact
;
6431 if (gnu_hash
&& (i
& 31) == 0)
6434 memset (counts
, '\0', i
* sizeof (unsigned long int));
6436 /* Determine how often each hash bucket is used. */
6437 for (j
= 0; j
< nsyms
; ++j
)
6438 ++counts
[hashcodes
[j
] % i
];
6440 /* For the weight function we need some information about the
6441 pagesize on the target. This is information need not be 100%
6442 accurate. Since this information is not available (so far) we
6443 define it here to a reasonable default value. If it is crucial
6444 to have a better value some day simply define this value. */
6445 # ifndef BFD_TARGET_PAGESIZE
6446 # define BFD_TARGET_PAGESIZE (4096)
6449 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6451 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6454 /* Variant 1: optimize for short chains. We add the squares
6455 of all the chain lengths (which favors many small chain
6456 over a few long chains). */
6457 for (j
= 0; j
< i
; ++j
)
6458 max
+= counts
[j
] * counts
[j
];
6460 /* This adds penalties for the overall size of the table. */
6461 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6464 /* Variant 2: Optimize a lot more for small table. Here we
6465 also add squares of the size but we also add penalties for
6466 empty slots (the +1 term). */
6467 for (j
= 0; j
< i
; ++j
)
6468 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6470 /* The overall size of the table is considered, but not as
6471 strong as in variant 1, where it is squared. */
6472 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6476 /* Compare with current best results. */
6477 if (max
< best_chlen
)
6481 no_improvement_count
= 0;
6483 /* PR 11843: Avoid futile long searches for the best bucket size
6484 when there are a large number of symbols. */
6485 else if (++no_improvement_count
== 100)
6493 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6495 best_size
= elf_buckets
[i
];
6496 if (nsyms
< elf_buckets
[i
+ 1])
6499 if (gnu_hash
&& best_size
< 2)
6506 /* Size any SHT_GROUP section for ld -r. */
6509 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6514 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6515 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6516 && (s
= ibfd
->sections
) != NULL
6517 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6518 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6523 /* Set a default stack segment size. The value in INFO wins. If it
6524 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6525 undefined it is initialized. */
6528 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6529 struct bfd_link_info
*info
,
6530 const char *legacy_symbol
,
6531 bfd_vma default_size
)
6533 struct elf_link_hash_entry
*h
= NULL
;
6535 /* Look for legacy symbol. */
6537 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6538 false, false, false);
6539 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6540 || h
->root
.type
== bfd_link_hash_defweak
)
6542 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6544 /* The symbol has no type if specified on the command line. */
6545 h
->type
= STT_OBJECT
;
6546 if (info
->stacksize
)
6547 /* xgettext:c-format */
6548 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6549 output_bfd
, legacy_symbol
);
6550 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6551 /* xgettext:c-format */
6552 _bfd_error_handler (_("%pB: %s not absolute"),
6553 output_bfd
, legacy_symbol
);
6555 info
->stacksize
= h
->root
.u
.def
.value
;
6558 if (!info
->stacksize
)
6559 /* If the user didn't set a size, or explicitly inhibit the
6560 size, set it now. */
6561 info
->stacksize
= default_size
;
6563 /* Provide the legacy symbol, if it is referenced. */
6564 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6565 || h
->root
.type
== bfd_link_hash_undefweak
))
6567 struct bfd_link_hash_entry
*bh
= NULL
;
6569 if (!(_bfd_generic_link_add_one_symbol
6570 (info
, output_bfd
, legacy_symbol
,
6571 BSF_GLOBAL
, bfd_abs_section_ptr
,
6572 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6573 NULL
, false, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6576 h
= (struct elf_link_hash_entry
*) bh
;
6578 h
->type
= STT_OBJECT
;
6584 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6586 struct elf_gc_sweep_symbol_info
6588 struct bfd_link_info
*info
;
6589 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6594 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6597 && (((h
->root
.type
== bfd_link_hash_defined
6598 || h
->root
.type
== bfd_link_hash_defweak
)
6599 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6600 && h
->root
.u
.def
.section
->gc_mark
))
6601 || h
->root
.type
== bfd_link_hash_undefined
6602 || h
->root
.type
== bfd_link_hash_undefweak
))
6604 struct elf_gc_sweep_symbol_info
*inf
;
6606 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6607 (*inf
->hide_symbol
) (inf
->info
, h
, true);
6610 h
->ref_regular_nonweak
= 0;
6616 /* Set up the sizes and contents of the ELF dynamic sections. This is
6617 called by the ELF linker emulation before_allocation routine. We
6618 must set the sizes of the sections before the linker sets the
6619 addresses of the various sections. */
6622 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6625 const char *filter_shlib
,
6627 const char *depaudit
,
6628 const char * const *auxiliary_filters
,
6629 struct bfd_link_info
*info
,
6630 asection
**sinterpptr
)
6633 const struct elf_backend_data
*bed
;
6637 if (!is_elf_hash_table (info
->hash
))
6640 /* Any syms created from now on start with -1 in
6641 got.refcount/offset and plt.refcount/offset. */
6642 elf_hash_table (info
)->init_got_refcount
6643 = elf_hash_table (info
)->init_got_offset
;
6644 elf_hash_table (info
)->init_plt_refcount
6645 = elf_hash_table (info
)->init_plt_offset
;
6647 bed
= get_elf_backend_data (output_bfd
);
6649 /* The backend may have to create some sections regardless of whether
6650 we're dynamic or not. */
6651 if (bed
->elf_backend_always_size_sections
6652 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6655 dynobj
= elf_hash_table (info
)->dynobj
;
6657 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6659 struct bfd_elf_version_tree
*verdefs
;
6660 struct elf_info_failed asvinfo
;
6661 struct bfd_elf_version_tree
*t
;
6662 struct bfd_elf_version_expr
*d
;
6666 /* If we are supposed to export all symbols into the dynamic symbol
6667 table (this is not the normal case), then do so. */
6668 if (info
->export_dynamic
6669 || (bfd_link_executable (info
) && info
->dynamic
))
6671 struct elf_info_failed eif
;
6675 elf_link_hash_traverse (elf_hash_table (info
),
6676 _bfd_elf_export_symbol
,
6684 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6686 if (soname_indx
== (size_t) -1
6687 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6691 soname_indx
= (size_t) -1;
6693 /* Make all global versions with definition. */
6694 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6695 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6696 if (!d
->symver
&& d
->literal
)
6698 const char *verstr
, *name
;
6699 size_t namelen
, verlen
, newlen
;
6700 char *newname
, *p
, leading_char
;
6701 struct elf_link_hash_entry
*newh
;
6703 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6705 namelen
= strlen (name
) + (leading_char
!= '\0');
6707 verlen
= strlen (verstr
);
6708 newlen
= namelen
+ verlen
+ 3;
6710 newname
= (char *) bfd_malloc (newlen
);
6711 if (newname
== NULL
)
6713 newname
[0] = leading_char
;
6714 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6716 /* Check the hidden versioned definition. */
6717 p
= newname
+ namelen
;
6719 memcpy (p
, verstr
, verlen
+ 1);
6720 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6721 newname
, false, false,
6724 || (newh
->root
.type
!= bfd_link_hash_defined
6725 && newh
->root
.type
!= bfd_link_hash_defweak
))
6727 /* Check the default versioned definition. */
6729 memcpy (p
, verstr
, verlen
+ 1);
6730 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6731 newname
, false, false,
6736 /* Mark this version if there is a definition and it is
6737 not defined in a shared object. */
6739 && !newh
->def_dynamic
6740 && (newh
->root
.type
== bfd_link_hash_defined
6741 || newh
->root
.type
== bfd_link_hash_defweak
))
6745 /* Attach all the symbols to their version information. */
6746 asvinfo
.info
= info
;
6747 asvinfo
.failed
= false;
6749 elf_link_hash_traverse (elf_hash_table (info
),
6750 _bfd_elf_link_assign_sym_version
,
6755 if (!info
->allow_undefined_version
)
6757 /* Check if all global versions have a definition. */
6758 bool all_defined
= true;
6759 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6760 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6761 if (d
->literal
&& !d
->symver
&& !d
->script
)
6764 (_("%s: undefined version: %s"),
6765 d
->pattern
, t
->name
);
6766 all_defined
= false;
6771 bfd_set_error (bfd_error_bad_value
);
6776 /* Set up the version definition section. */
6777 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6778 BFD_ASSERT (s
!= NULL
);
6780 /* We may have created additional version definitions if we are
6781 just linking a regular application. */
6782 verdefs
= info
->version_info
;
6784 /* Skip anonymous version tag. */
6785 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6786 verdefs
= verdefs
->next
;
6788 if (verdefs
== NULL
&& !info
->create_default_symver
)
6789 s
->flags
|= SEC_EXCLUDE
;
6795 Elf_Internal_Verdef def
;
6796 Elf_Internal_Verdaux defaux
;
6797 struct bfd_link_hash_entry
*bh
;
6798 struct elf_link_hash_entry
*h
;
6804 /* Make space for the base version. */
6805 size
+= sizeof (Elf_External_Verdef
);
6806 size
+= sizeof (Elf_External_Verdaux
);
6809 /* Make space for the default version. */
6810 if (info
->create_default_symver
)
6812 size
+= sizeof (Elf_External_Verdef
);
6816 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6818 struct bfd_elf_version_deps
*n
;
6820 /* Don't emit base version twice. */
6824 size
+= sizeof (Elf_External_Verdef
);
6825 size
+= sizeof (Elf_External_Verdaux
);
6828 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6829 size
+= sizeof (Elf_External_Verdaux
);
6833 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6834 if (s
->contents
== NULL
&& s
->size
!= 0)
6837 /* Fill in the version definition section. */
6841 def
.vd_version
= VER_DEF_CURRENT
;
6842 def
.vd_flags
= VER_FLG_BASE
;
6845 if (info
->create_default_symver
)
6847 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6848 def
.vd_next
= sizeof (Elf_External_Verdef
);
6852 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6853 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6854 + sizeof (Elf_External_Verdaux
));
6857 if (soname_indx
!= (size_t) -1)
6859 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6861 def
.vd_hash
= bfd_elf_hash (soname
);
6862 defaux
.vda_name
= soname_indx
;
6869 name
= lbasename (bfd_get_filename (output_bfd
));
6870 def
.vd_hash
= bfd_elf_hash (name
);
6871 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6873 if (indx
== (size_t) -1)
6875 defaux
.vda_name
= indx
;
6877 defaux
.vda_next
= 0;
6879 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6880 (Elf_External_Verdef
*) p
);
6881 p
+= sizeof (Elf_External_Verdef
);
6882 if (info
->create_default_symver
)
6884 /* Add a symbol representing this version. */
6886 if (! (_bfd_generic_link_add_one_symbol
6887 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6889 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6891 h
= (struct elf_link_hash_entry
*) bh
;
6894 h
->type
= STT_OBJECT
;
6895 h
->verinfo
.vertree
= NULL
;
6897 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6900 /* Create a duplicate of the base version with the same
6901 aux block, but different flags. */
6904 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6906 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6907 + sizeof (Elf_External_Verdaux
));
6910 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6911 (Elf_External_Verdef
*) p
);
6912 p
+= sizeof (Elf_External_Verdef
);
6914 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6915 (Elf_External_Verdaux
*) p
);
6916 p
+= sizeof (Elf_External_Verdaux
);
6918 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6921 struct bfd_elf_version_deps
*n
;
6923 /* Don't emit the base version twice. */
6928 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6931 /* Add a symbol representing this version. */
6933 if (! (_bfd_generic_link_add_one_symbol
6934 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6936 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6938 h
= (struct elf_link_hash_entry
*) bh
;
6941 h
->type
= STT_OBJECT
;
6942 h
->verinfo
.vertree
= t
;
6944 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6947 def
.vd_version
= VER_DEF_CURRENT
;
6949 if (t
->globals
.list
== NULL
6950 && t
->locals
.list
== NULL
6952 def
.vd_flags
|= VER_FLG_WEAK
;
6953 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6954 def
.vd_cnt
= cdeps
+ 1;
6955 def
.vd_hash
= bfd_elf_hash (t
->name
);
6956 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6959 /* If a basever node is next, it *must* be the last node in
6960 the chain, otherwise Verdef construction breaks. */
6961 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6962 BFD_ASSERT (t
->next
->next
== NULL
);
6964 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6965 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6966 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6968 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6969 (Elf_External_Verdef
*) p
);
6970 p
+= sizeof (Elf_External_Verdef
);
6972 defaux
.vda_name
= h
->dynstr_index
;
6973 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6975 defaux
.vda_next
= 0;
6976 if (t
->deps
!= NULL
)
6977 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6978 t
->name_indx
= defaux
.vda_name
;
6980 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6981 (Elf_External_Verdaux
*) p
);
6982 p
+= sizeof (Elf_External_Verdaux
);
6984 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6986 if (n
->version_needed
== NULL
)
6988 /* This can happen if there was an error in the
6990 defaux
.vda_name
= 0;
6994 defaux
.vda_name
= n
->version_needed
->name_indx
;
6995 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6998 if (n
->next
== NULL
)
6999 defaux
.vda_next
= 0;
7001 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
7003 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7004 (Elf_External_Verdaux
*) p
);
7005 p
+= sizeof (Elf_External_Verdaux
);
7009 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
7013 if (info
->gc_sections
&& bed
->can_gc_sections
)
7015 struct elf_gc_sweep_symbol_info sweep_info
;
7017 /* Remove the symbols that were in the swept sections from the
7018 dynamic symbol table. */
7019 sweep_info
.info
= info
;
7020 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
7021 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
7025 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7028 struct elf_find_verdep_info sinfo
;
7030 /* Work out the size of the version reference section. */
7032 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
7033 BFD_ASSERT (s
!= NULL
);
7036 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
7037 if (sinfo
.vers
== 0)
7039 sinfo
.failed
= false;
7041 elf_link_hash_traverse (elf_hash_table (info
),
7042 _bfd_elf_link_find_version_dependencies
,
7047 if (info
->enable_dt_relr
)
7049 elf_link_add_dt_relr_dependency (&sinfo
);
7054 if (elf_tdata (output_bfd
)->verref
== NULL
)
7055 s
->flags
|= SEC_EXCLUDE
;
7058 Elf_Internal_Verneed
*vn
;
7063 /* Build the version dependency section. */
7066 for (vn
= elf_tdata (output_bfd
)->verref
;
7068 vn
= vn
->vn_nextref
)
7070 Elf_Internal_Vernaux
*a
;
7072 size
+= sizeof (Elf_External_Verneed
);
7074 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7075 size
+= sizeof (Elf_External_Vernaux
);
7079 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7080 if (s
->contents
== NULL
)
7084 for (vn
= elf_tdata (output_bfd
)->verref
;
7086 vn
= vn
->vn_nextref
)
7089 Elf_Internal_Vernaux
*a
;
7093 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7096 vn
->vn_version
= VER_NEED_CURRENT
;
7098 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7099 elf_dt_name (vn
->vn_bfd
) != NULL
7100 ? elf_dt_name (vn
->vn_bfd
)
7101 : lbasename (bfd_get_filename
7104 if (indx
== (size_t) -1)
7107 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
7108 if (vn
->vn_nextref
== NULL
)
7111 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
7112 + caux
* sizeof (Elf_External_Vernaux
));
7114 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
7115 (Elf_External_Verneed
*) p
);
7116 p
+= sizeof (Elf_External_Verneed
);
7118 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7120 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
7121 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7122 a
->vna_nodename
, false);
7123 if (indx
== (size_t) -1)
7126 if (a
->vna_nextptr
== NULL
)
7129 a
->vna_next
= sizeof (Elf_External_Vernaux
);
7131 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
7132 (Elf_External_Vernaux
*) p
);
7133 p
+= sizeof (Elf_External_Vernaux
);
7137 elf_tdata (output_bfd
)->cverrefs
= crefs
;
7141 if (bfd_link_relocatable (info
)
7142 && !_bfd_elf_size_group_sections (info
))
7145 /* Determine any GNU_STACK segment requirements, after the backend
7146 has had a chance to set a default segment size. */
7147 if (info
->execstack
)
7149 /* If the user has explicitly requested warnings, then generate one even
7150 though the choice is the result of another command line option. */
7151 if (info
->warn_execstack
== 1)
7154 warning: enabling an executable stack because of -z execstack command line option"));
7155 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
7157 else if (info
->noexecstack
)
7158 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
7162 asection
*notesec
= NULL
;
7163 bfd
*noteobj
= NULL
;
7164 bfd
*emptyobj
= NULL
;
7167 for (inputobj
= info
->input_bfds
;
7169 inputobj
= inputobj
->link
.next
)
7174 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
7176 s
= inputobj
->sections
;
7177 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
7180 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
7184 if (s
->flags
& SEC_CODE
)
7188 /* There is no point in scanning the remaining bfds. */
7192 else if (bed
->default_execstack
&& info
->default_execstack
)
7195 emptyobj
= inputobj
;
7199 if (notesec
|| info
->stacksize
> 0)
7203 if (info
->warn_execstack
!= 0)
7205 /* PR 29072: Because an executable stack is a serious
7206 security risk, make sure that the user knows that it is
7207 being enabled despite the fact that it was not requested
7208 on the command line. */
7210 _bfd_error_handler (_("\
7211 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7212 bfd_get_filename (noteobj
));
7215 _bfd_error_handler (_("\
7216 warning: %s: missing .note.GNU-stack section implies executable stack"),
7217 bfd_get_filename (emptyobj
));
7218 _bfd_error_handler (_("\
7219 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7223 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
7226 if (notesec
&& exec
&& bfd_link_relocatable (info
)
7227 && notesec
->output_section
!= bfd_abs_section_ptr
)
7228 notesec
->output_section
->flags
|= SEC_CODE
;
7231 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7233 struct elf_info_failed eif
;
7234 struct elf_link_hash_entry
*h
;
7238 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7239 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7243 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7245 info
->flags
|= DF_SYMBOLIC
;
7253 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7255 if (indx
== (size_t) -1)
7258 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7259 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7263 if (filter_shlib
!= NULL
)
7267 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7268 filter_shlib
, true);
7269 if (indx
== (size_t) -1
7270 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7274 if (auxiliary_filters
!= NULL
)
7276 const char * const *p
;
7278 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7282 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7284 if (indx
== (size_t) -1
7285 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7294 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7296 if (indx
== (size_t) -1
7297 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7301 if (depaudit
!= NULL
)
7305 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7307 if (indx
== (size_t) -1
7308 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7315 /* Find all symbols which were defined in a dynamic object and make
7316 the backend pick a reasonable value for them. */
7317 elf_link_hash_traverse (elf_hash_table (info
),
7318 _bfd_elf_adjust_dynamic_symbol
,
7323 /* Add some entries to the .dynamic section. We fill in some of the
7324 values later, in bfd_elf_final_link, but we must add the entries
7325 now so that we know the final size of the .dynamic section. */
7327 /* If there are initialization and/or finalization functions to
7328 call then add the corresponding DT_INIT/DT_FINI entries. */
7329 h
= (info
->init_function
7330 ? elf_link_hash_lookup (elf_hash_table (info
),
7331 info
->init_function
, false,
7338 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7341 h
= (info
->fini_function
7342 ? elf_link_hash_lookup (elf_hash_table (info
),
7343 info
->fini_function
, false,
7350 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7354 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7355 if (s
!= NULL
&& s
->linker_has_input
)
7357 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7358 if (! bfd_link_executable (info
))
7363 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7364 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7365 && (o
= sub
->sections
) != NULL
7366 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7367 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7368 if (elf_section_data (o
)->this_hdr
.sh_type
7369 == SHT_PREINIT_ARRAY
)
7372 (_("%pB: .preinit_array section is not allowed in DSO"),
7377 bfd_set_error (bfd_error_nonrepresentable_section
);
7381 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7382 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7385 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7386 if (s
!= NULL
&& s
->linker_has_input
)
7388 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7389 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7392 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7393 if (s
!= NULL
&& s
->linker_has_input
)
7395 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7396 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7400 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7401 /* If .dynstr is excluded from the link, we don't want any of
7402 these tags. Strictly, we should be checking each section
7403 individually; This quick check covers for the case where
7404 someone does a /DISCARD/ : { *(*) }. */
7405 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7407 bfd_size_type strsize
;
7409 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7410 if ((info
->emit_hash
7411 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7412 || (info
->emit_gnu_hash
7413 && (bed
->record_xhash_symbol
== NULL
7414 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7415 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7416 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7417 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7418 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7420 || (info
->gnu_flags_1
7421 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_FLAGS_1
,
7422 info
->gnu_flags_1
)))
7427 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7430 /* The backend must work out the sizes of all the other dynamic
7433 && bed
->elf_backend_size_dynamic_sections
!= NULL
7434 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7437 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7439 if (elf_tdata (output_bfd
)->cverdefs
)
7441 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7443 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7444 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7448 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7450 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7453 else if (info
->flags
& DF_BIND_NOW
)
7455 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7461 if (bfd_link_executable (info
))
7462 info
->flags_1
&= ~ (DF_1_INITFIRST
7465 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7469 if (elf_tdata (output_bfd
)->cverrefs
)
7471 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7473 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7474 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7478 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7479 && elf_tdata (output_bfd
)->cverdefs
== 0)
7480 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7484 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7485 s
->flags
|= SEC_EXCLUDE
;
7491 /* Find the first non-excluded output section. We'll use its
7492 section symbol for some emitted relocs. */
7494 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7497 asection
*found
= NULL
;
7499 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7500 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7501 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7504 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7507 elf_hash_table (info
)->text_index_section
= found
;
7510 /* Find two non-excluded output sections, one for code, one for data.
7511 We'll use their section symbols for some emitted relocs. */
7513 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7516 asection
*found
= NULL
;
7518 /* Data first, since setting text_index_section changes
7519 _bfd_elf_omit_section_dynsym_default. */
7520 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7521 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7522 && !(s
->flags
& SEC_READONLY
)
7523 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7526 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7529 elf_hash_table (info
)->data_index_section
= found
;
7531 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7532 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7533 && (s
->flags
& SEC_READONLY
)
7534 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7539 elf_hash_table (info
)->text_index_section
= found
;
7542 #define GNU_HASH_SECTION_NAME(bed) \
7543 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7546 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7548 const struct elf_backend_data
*bed
;
7549 unsigned long section_sym_count
;
7550 bfd_size_type dynsymcount
= 0;
7552 if (!is_elf_hash_table (info
->hash
))
7555 bed
= get_elf_backend_data (output_bfd
);
7556 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7558 /* Assign dynsym indices. In a shared library we generate a section
7559 symbol for each output section, which come first. Next come all
7560 of the back-end allocated local dynamic syms, followed by the rest
7561 of the global symbols.
7563 This is usually not needed for static binaries, however backends
7564 can request to always do it, e.g. the MIPS backend uses dynamic
7565 symbol counts to lay out GOT, which will be produced in the
7566 presence of GOT relocations even in static binaries (holding fixed
7567 data in that case, to satisfy those relocations). */
7569 if (elf_hash_table (info
)->dynamic_sections_created
7570 || bed
->always_renumber_dynsyms
)
7571 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7572 §ion_sym_count
);
7574 if (elf_hash_table (info
)->dynamic_sections_created
)
7578 unsigned int dtagcount
;
7580 dynobj
= elf_hash_table (info
)->dynobj
;
7582 /* Work out the size of the symbol version section. */
7583 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7584 BFD_ASSERT (s
!= NULL
);
7585 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7587 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7588 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7589 if (s
->contents
== NULL
)
7592 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7596 /* Set the size of the .dynsym and .hash sections. We counted
7597 the number of dynamic symbols in elf_link_add_object_symbols.
7598 We will build the contents of .dynsym and .hash when we build
7599 the final symbol table, because until then we do not know the
7600 correct value to give the symbols. We built the .dynstr
7601 section as we went along in elf_link_add_object_symbols. */
7602 s
= elf_hash_table (info
)->dynsym
;
7603 BFD_ASSERT (s
!= NULL
);
7604 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7606 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7607 if (s
->contents
== NULL
)
7610 /* The first entry in .dynsym is a dummy symbol. Clear all the
7611 section syms, in case we don't output them all. */
7612 ++section_sym_count
;
7613 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7615 elf_hash_table (info
)->bucketcount
= 0;
7617 /* Compute the size of the hashing table. As a side effect this
7618 computes the hash values for all the names we export. */
7619 if (info
->emit_hash
)
7621 unsigned long int *hashcodes
;
7622 struct hash_codes_info hashinf
;
7624 unsigned long int nsyms
;
7626 size_t hash_entry_size
;
7628 /* Compute the hash values for all exported symbols. At the same
7629 time store the values in an array so that we could use them for
7631 amt
= dynsymcount
* sizeof (unsigned long int);
7632 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7633 if (hashcodes
== NULL
)
7635 hashinf
.hashcodes
= hashcodes
;
7636 hashinf
.error
= false;
7638 /* Put all hash values in HASHCODES. */
7639 elf_link_hash_traverse (elf_hash_table (info
),
7640 elf_collect_hash_codes
, &hashinf
);
7647 nsyms
= hashinf
.hashcodes
- hashcodes
;
7649 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7652 if (bucketcount
== 0 && nsyms
> 0)
7655 elf_hash_table (info
)->bucketcount
= bucketcount
;
7657 s
= bfd_get_linker_section (dynobj
, ".hash");
7658 BFD_ASSERT (s
!= NULL
);
7659 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7660 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7661 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7662 if (s
->contents
== NULL
)
7665 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7666 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7667 s
->contents
+ hash_entry_size
);
7670 if (info
->emit_gnu_hash
)
7673 unsigned char *contents
;
7674 struct collect_gnu_hash_codes cinfo
;
7678 memset (&cinfo
, 0, sizeof (cinfo
));
7680 /* Compute the hash values for all exported symbols. At the same
7681 time store the values in an array so that we could use them for
7683 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7684 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7685 if (cinfo
.hashcodes
== NULL
)
7688 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7689 cinfo
.min_dynindx
= -1;
7690 cinfo
.output_bfd
= output_bfd
;
7693 /* Put all hash values in HASHCODES. */
7694 elf_link_hash_traverse (elf_hash_table (info
),
7695 elf_collect_gnu_hash_codes
, &cinfo
);
7698 free (cinfo
.hashcodes
);
7703 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7705 if (bucketcount
== 0)
7707 free (cinfo
.hashcodes
);
7711 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7712 BFD_ASSERT (s
!= NULL
);
7714 if (cinfo
.nsyms
== 0)
7716 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7717 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7718 free (cinfo
.hashcodes
);
7719 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7720 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7721 if (contents
== NULL
)
7723 s
->contents
= contents
;
7724 /* 1 empty bucket. */
7725 bfd_put_32 (output_bfd
, 1, contents
);
7726 /* SYMIDX above the special symbol 0. */
7727 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7728 /* Just one word for bitmask. */
7729 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7730 /* Only hash fn bloom filter. */
7731 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7732 /* No hashes are valid - empty bitmask. */
7733 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7734 /* No hashes in the only bucket. */
7735 bfd_put_32 (output_bfd
, 0,
7736 contents
+ 16 + bed
->s
->arch_size
/ 8);
7740 unsigned long int maskwords
, maskbitslog2
, x
;
7741 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7745 while ((x
>>= 1) != 0)
7747 if (maskbitslog2
< 3)
7749 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7750 maskbitslog2
= maskbitslog2
+ 3;
7752 maskbitslog2
= maskbitslog2
+ 2;
7753 if (bed
->s
->arch_size
== 64)
7755 if (maskbitslog2
== 5)
7761 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7762 cinfo
.shift2
= maskbitslog2
;
7763 cinfo
.maskbits
= 1 << maskbitslog2
;
7764 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7765 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7766 amt
+= maskwords
* sizeof (bfd_vma
);
7767 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7768 if (cinfo
.bitmask
== NULL
)
7770 free (cinfo
.hashcodes
);
7774 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7775 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7776 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7777 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7779 /* Determine how often each hash bucket is used. */
7780 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7781 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7782 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7784 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7785 if (cinfo
.counts
[i
] != 0)
7787 cinfo
.indx
[i
] = cnt
;
7788 cnt
+= cinfo
.counts
[i
];
7790 BFD_ASSERT (cnt
== dynsymcount
);
7791 cinfo
.bucketcount
= bucketcount
;
7792 cinfo
.local_indx
= cinfo
.min_dynindx
;
7794 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7795 s
->size
+= cinfo
.maskbits
/ 8;
7796 if (bed
->record_xhash_symbol
!= NULL
)
7797 s
->size
+= cinfo
.nsyms
* 4;
7798 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7799 if (contents
== NULL
)
7801 free (cinfo
.bitmask
);
7802 free (cinfo
.hashcodes
);
7806 s
->contents
= contents
;
7807 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7808 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7809 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7810 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7811 contents
+= 16 + cinfo
.maskbits
/ 8;
7813 for (i
= 0; i
< bucketcount
; ++i
)
7815 if (cinfo
.counts
[i
] == 0)
7816 bfd_put_32 (output_bfd
, 0, contents
);
7818 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7822 cinfo
.contents
= contents
;
7824 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7825 /* Renumber dynamic symbols, if populating .gnu.hash section.
7826 If using .MIPS.xhash, populate the translation table. */
7827 elf_link_hash_traverse (elf_hash_table (info
),
7828 elf_gnu_hash_process_symidx
, &cinfo
);
7830 contents
= s
->contents
+ 16;
7831 for (i
= 0; i
< maskwords
; ++i
)
7833 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7835 contents
+= bed
->s
->arch_size
/ 8;
7838 free (cinfo
.bitmask
);
7839 free (cinfo
.hashcodes
);
7843 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7844 BFD_ASSERT (s
!= NULL
);
7846 elf_finalize_dynstr (output_bfd
, info
);
7848 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7850 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7851 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7858 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7861 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7864 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7865 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7868 /* Finish SHF_MERGE section merging. */
7871 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7876 if (!is_elf_hash_table (info
->hash
))
7879 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7880 if ((ibfd
->flags
& DYNAMIC
) == 0
7881 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7882 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7883 == get_elf_backend_data (obfd
)->s
->elfclass
))
7884 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7885 if ((sec
->flags
& SEC_MERGE
) != 0
7886 && !bfd_is_abs_section (sec
->output_section
))
7888 struct bfd_elf_section_data
*secdata
;
7890 secdata
= elf_section_data (sec
);
7891 if (! _bfd_add_merge_section (obfd
,
7892 &elf_hash_table (info
)->merge_info
,
7893 sec
, &secdata
->sec_info
))
7895 else if (secdata
->sec_info
)
7896 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7899 if (elf_hash_table (info
)->merge_info
!= NULL
)
7900 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7901 merge_sections_remove_hook
);
7905 /* Create an entry in an ELF linker hash table. */
7907 struct bfd_hash_entry
*
7908 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7909 struct bfd_hash_table
*table
,
7912 /* Allocate the structure if it has not already been allocated by a
7916 entry
= (struct bfd_hash_entry
*)
7917 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7922 /* Call the allocation method of the superclass. */
7923 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7926 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7927 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7929 /* Set local fields. */
7932 ret
->got
= htab
->init_got_refcount
;
7933 ret
->plt
= htab
->init_plt_refcount
;
7934 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7935 - offsetof (struct elf_link_hash_entry
, size
)));
7936 /* Assume that we have been called by a non-ELF symbol reader.
7937 This flag is then reset by the code which reads an ELF input
7938 file. This ensures that a symbol created by a non-ELF symbol
7939 reader will have the flag set correctly. */
7946 /* Copy data from an indirect symbol to its direct symbol, hiding the
7947 old indirect symbol. Also used for copying flags to a weakdef. */
7950 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7951 struct elf_link_hash_entry
*dir
,
7952 struct elf_link_hash_entry
*ind
)
7954 struct elf_link_hash_table
*htab
;
7956 if (ind
->dyn_relocs
!= NULL
)
7958 if (dir
->dyn_relocs
!= NULL
)
7960 struct elf_dyn_relocs
**pp
;
7961 struct elf_dyn_relocs
*p
;
7963 /* Add reloc counts against the indirect sym to the direct sym
7964 list. Merge any entries against the same section. */
7965 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
7967 struct elf_dyn_relocs
*q
;
7969 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
7970 if (q
->sec
== p
->sec
)
7972 q
->pc_count
+= p
->pc_count
;
7973 q
->count
+= p
->count
;
7980 *pp
= dir
->dyn_relocs
;
7983 dir
->dyn_relocs
= ind
->dyn_relocs
;
7984 ind
->dyn_relocs
= NULL
;
7987 /* Copy down any references that we may have already seen to the
7988 symbol which just became indirect. */
7990 if (dir
->versioned
!= versioned_hidden
)
7991 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7992 dir
->ref_regular
|= ind
->ref_regular
;
7993 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7994 dir
->non_got_ref
|= ind
->non_got_ref
;
7995 dir
->needs_plt
|= ind
->needs_plt
;
7996 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7998 if (ind
->root
.type
!= bfd_link_hash_indirect
)
8001 /* Copy over the global and procedure linkage table refcount entries.
8002 These may have been already set up by a check_relocs routine. */
8003 htab
= elf_hash_table (info
);
8004 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
8006 if (dir
->got
.refcount
< 0)
8007 dir
->got
.refcount
= 0;
8008 dir
->got
.refcount
+= ind
->got
.refcount
;
8009 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
8012 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
8014 if (dir
->plt
.refcount
< 0)
8015 dir
->plt
.refcount
= 0;
8016 dir
->plt
.refcount
+= ind
->plt
.refcount
;
8017 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
8020 if (ind
->dynindx
!= -1)
8022 if (dir
->dynindx
!= -1)
8023 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
8024 dir
->dynindx
= ind
->dynindx
;
8025 dir
->dynstr_index
= ind
->dynstr_index
;
8027 ind
->dynstr_index
= 0;
8032 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
8033 struct elf_link_hash_entry
*h
,
8036 /* STT_GNU_IFUNC symbol must go through PLT. */
8037 if (h
->type
!= STT_GNU_IFUNC
)
8039 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
8044 h
->forced_local
= 1;
8045 if (h
->dynindx
!= -1)
8047 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
8050 h
->dynstr_index
= 0;
8055 /* Hide a symbol. */
8058 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
8059 struct bfd_link_info
*info
,
8060 struct bfd_link_hash_entry
*h
)
8062 if (is_elf_hash_table (info
->hash
))
8064 const struct elf_backend_data
*bed
8065 = get_elf_backend_data (output_bfd
);
8066 struct elf_link_hash_entry
*eh
8067 = (struct elf_link_hash_entry
*) h
;
8068 bed
->elf_backend_hide_symbol (info
, eh
, true);
8069 eh
->def_dynamic
= 0;
8070 eh
->ref_dynamic
= 0;
8071 eh
->dynamic_def
= 0;
8075 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8079 _bfd_elf_link_hash_table_init
8080 (struct elf_link_hash_table
*table
,
8082 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
8083 struct bfd_hash_table
*,
8085 unsigned int entsize
,
8086 enum elf_target_id target_id
)
8089 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
8091 table
->init_got_refcount
.refcount
= can_refcount
- 1;
8092 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
8093 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
8094 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
8095 /* The first dynamic symbol is a dummy. */
8096 table
->dynsymcount
= 1;
8098 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
8100 table
->root
.type
= bfd_link_elf_hash_table
;
8101 table
->hash_table_id
= target_id
;
8102 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
8107 /* Create an ELF linker hash table. */
8109 struct bfd_link_hash_table
*
8110 _bfd_elf_link_hash_table_create (bfd
*abfd
)
8112 struct elf_link_hash_table
*ret
;
8113 size_t amt
= sizeof (struct elf_link_hash_table
);
8115 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
8119 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
8120 sizeof (struct elf_link_hash_entry
),
8126 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
8131 /* Destroy an ELF linker hash table. */
8134 _bfd_elf_link_hash_table_free (bfd
*obfd
)
8136 struct elf_link_hash_table
*htab
;
8138 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
8139 if (htab
->dynstr
!= NULL
)
8140 _bfd_elf_strtab_free (htab
->dynstr
);
8141 _bfd_merge_sections_free (htab
->merge_info
);
8142 _bfd_generic_link_hash_table_free (obfd
);
8145 /* This is a hook for the ELF emulation code in the generic linker to
8146 tell the backend linker what file name to use for the DT_NEEDED
8147 entry for a dynamic object. */
8150 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
8152 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8153 && bfd_get_format (abfd
) == bfd_object
)
8154 elf_dt_name (abfd
) = name
;
8158 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
8161 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8162 && bfd_get_format (abfd
) == bfd_object
)
8163 lib_class
= elf_dyn_lib_class (abfd
);
8170 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
8172 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8173 && bfd_get_format (abfd
) == bfd_object
)
8174 elf_dyn_lib_class (abfd
) = lib_class
;
8177 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8178 the linker ELF emulation code. */
8180 struct bfd_link_needed_list
*
8181 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8182 struct bfd_link_info
*info
)
8184 if (! is_elf_hash_table (info
->hash
))
8186 return elf_hash_table (info
)->needed
;
8189 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8190 hook for the linker ELF emulation code. */
8192 struct bfd_link_needed_list
*
8193 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8194 struct bfd_link_info
*info
)
8196 if (! is_elf_hash_table (info
->hash
))
8198 return elf_hash_table (info
)->runpath
;
8201 /* Get the name actually used for a dynamic object for a link. This
8202 is the SONAME entry if there is one. Otherwise, it is the string
8203 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8206 bfd_elf_get_dt_soname (bfd
*abfd
)
8208 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8209 && bfd_get_format (abfd
) == bfd_object
)
8210 return elf_dt_name (abfd
);
8214 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8215 the ELF linker emulation code. */
8218 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
8219 struct bfd_link_needed_list
**pneeded
)
8222 bfd_byte
*dynbuf
= NULL
;
8223 unsigned int elfsec
;
8224 unsigned long shlink
;
8225 bfd_byte
*extdyn
, *extdynend
;
8227 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
8231 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
8232 || bfd_get_format (abfd
) != bfd_object
)
8235 s
= bfd_get_section_by_name (abfd
, ".dynamic");
8236 if (s
== NULL
|| s
->size
== 0 || (s
->flags
& SEC_HAS_CONTENTS
) == 0)
8239 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
8242 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8243 if (elfsec
== SHN_BAD
)
8246 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8248 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8249 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8251 for (extdyn
= dynbuf
, extdynend
= dynbuf
+ s
->size
;
8252 (size_t) (extdynend
- extdyn
) >= extdynsize
;
8253 extdyn
+= extdynsize
)
8255 Elf_Internal_Dyn dyn
;
8257 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8259 if (dyn
.d_tag
== DT_NULL
)
8262 if (dyn
.d_tag
== DT_NEEDED
)
8265 struct bfd_link_needed_list
*l
;
8266 unsigned int tagv
= dyn
.d_un
.d_val
;
8269 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8274 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8294 struct elf_symbuf_symbol
8296 unsigned long st_name
; /* Symbol name, index in string tbl */
8297 unsigned char st_info
; /* Type and binding attributes */
8298 unsigned char st_other
; /* Visibilty, and target specific */
8301 struct elf_symbuf_head
8303 struct elf_symbuf_symbol
*ssym
;
8305 unsigned int st_shndx
;
8312 Elf_Internal_Sym
*isym
;
8313 struct elf_symbuf_symbol
*ssym
;
8319 /* Sort references to symbols by ascending section number. */
8322 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8324 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8325 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8327 if (s1
->st_shndx
!= s2
->st_shndx
)
8328 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8329 /* Final sort by the address of the sym in the symbuf ensures
8332 return s1
> s2
? 1 : -1;
8337 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8339 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8340 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8341 int ret
= strcmp (s1
->name
, s2
->name
);
8344 if (s1
->u
.p
!= s2
->u
.p
)
8345 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8349 static struct elf_symbuf_head
*
8350 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8352 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8353 struct elf_symbuf_symbol
*ssym
;
8354 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8355 size_t i
, shndx_count
, total_size
, amt
;
8357 amt
= symcount
* sizeof (*indbuf
);
8358 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8362 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8363 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8364 *ind
++ = &isymbuf
[i
];
8367 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8368 elf_sort_elf_symbol
);
8371 if (indbufend
> indbuf
)
8372 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8373 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8376 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8377 + (indbufend
- indbuf
) * sizeof (*ssym
));
8378 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8379 if (ssymbuf
== NULL
)
8385 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8386 ssymbuf
->ssym
= NULL
;
8387 ssymbuf
->count
= shndx_count
;
8388 ssymbuf
->st_shndx
= 0;
8389 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8391 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8394 ssymhead
->ssym
= ssym
;
8395 ssymhead
->count
= 0;
8396 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8398 ssym
->st_name
= (*ind
)->st_name
;
8399 ssym
->st_info
= (*ind
)->st_info
;
8400 ssym
->st_other
= (*ind
)->st_other
;
8403 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8404 && (uintptr_t) ssym
- (uintptr_t) ssymbuf
== total_size
);
8410 /* Check if 2 sections define the same set of local and global
8414 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8415 struct bfd_link_info
*info
)
8418 const struct elf_backend_data
*bed1
, *bed2
;
8419 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8420 size_t symcount1
, symcount2
;
8421 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8422 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8423 Elf_Internal_Sym
*isym
, *isymend
;
8424 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8425 size_t count1
, count2
, sec_count1
, sec_count2
, i
;
8426 unsigned int shndx1
, shndx2
;
8428 bool ignore_section_symbol_p
;
8433 /* Both sections have to be in ELF. */
8434 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8435 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8438 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8441 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8442 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8443 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8446 bed1
= get_elf_backend_data (bfd1
);
8447 bed2
= get_elf_backend_data (bfd2
);
8448 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8449 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8450 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8451 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8453 if (symcount1
== 0 || symcount2
== 0)
8459 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8460 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8462 /* Ignore section symbols only when matching non-debugging sections
8463 or linkonce section with comdat section. */
8464 ignore_section_symbol_p
8465 = ((sec1
->flags
& SEC_DEBUGGING
) == 0
8466 || ((elf_section_flags (sec1
) & SHF_GROUP
)
8467 != (elf_section_flags (sec2
) & SHF_GROUP
)));
8469 if (ssymbuf1
== NULL
)
8471 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8473 if (isymbuf1
== NULL
)
8476 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8478 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8479 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8483 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8485 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8487 if (isymbuf2
== NULL
)
8490 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8492 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8493 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8497 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8499 /* Optimized faster version. */
8501 struct elf_symbol
*symp
;
8502 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8505 hi
= ssymbuf1
->count
;
8511 mid
= (lo
+ hi
) / 2;
8512 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8514 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8518 count1
= ssymbuf1
[mid
].count
;
8523 if (ignore_section_symbol_p
)
8525 for (i
= 0; i
< count1
; i
++)
8526 if (ELF_ST_TYPE (ssymbuf1
->ssym
[i
].st_info
) == STT_SECTION
)
8528 count1
-= sec_count1
;
8532 hi
= ssymbuf2
->count
;
8538 mid
= (lo
+ hi
) / 2;
8539 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8541 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8545 count2
= ssymbuf2
[mid
].count
;
8550 if (ignore_section_symbol_p
)
8552 for (i
= 0; i
< count2
; i
++)
8553 if (ELF_ST_TYPE (ssymbuf2
->ssym
[i
].st_info
) == STT_SECTION
)
8555 count2
-= sec_count2
;
8558 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8562 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8564 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8565 if (symtable1
== NULL
|| symtable2
== NULL
)
8569 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
+ sec_count1
;
8570 ssym
< ssymend
; ssym
++)
8572 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8574 symp
->u
.ssym
= ssym
;
8575 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8582 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
+ sec_count2
;
8583 ssym
< ssymend
; ssym
++)
8585 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8587 symp
->u
.ssym
= ssym
;
8588 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8594 /* Sort symbol by name. */
8595 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8596 elf_sym_name_compare
);
8597 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8598 elf_sym_name_compare
);
8600 for (i
= 0; i
< count1
; i
++)
8601 /* Two symbols must have the same binding, type and name. */
8602 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8603 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8604 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8611 symtable1
= (struct elf_symbol
*)
8612 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8613 symtable2
= (struct elf_symbol
*)
8614 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8615 if (symtable1
== NULL
|| symtable2
== NULL
)
8618 /* Count definitions in the section. */
8620 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8621 if (isym
->st_shndx
== shndx1
8622 && (!ignore_section_symbol_p
8623 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8624 symtable1
[count1
++].u
.isym
= isym
;
8627 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8628 if (isym
->st_shndx
== shndx2
8629 && (!ignore_section_symbol_p
8630 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8631 symtable2
[count2
++].u
.isym
= isym
;
8633 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8636 for (i
= 0; i
< count1
; i
++)
8638 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8639 symtable1
[i
].u
.isym
->st_name
);
8641 for (i
= 0; i
< count2
; i
++)
8643 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8644 symtable2
[i
].u
.isym
->st_name
);
8646 /* Sort symbol by name. */
8647 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8648 elf_sym_name_compare
);
8649 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8650 elf_sym_name_compare
);
8652 for (i
= 0; i
< count1
; i
++)
8653 /* Two symbols must have the same binding, type and name. */
8654 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8655 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8656 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8670 /* Return TRUE if 2 section types are compatible. */
8673 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8674 bfd
*bbfd
, const asection
*bsec
)
8678 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8679 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8682 return elf_section_type (asec
) == elf_section_type (bsec
);
8685 /* Final phase of ELF linker. */
8687 /* A structure we use to avoid passing large numbers of arguments. */
8689 struct elf_final_link_info
8691 /* General link information. */
8692 struct bfd_link_info
*info
;
8695 /* Symbol string table. */
8696 struct elf_strtab_hash
*symstrtab
;
8697 /* .hash section. */
8699 /* symbol version section (.gnu.version). */
8700 asection
*symver_sec
;
8701 /* Buffer large enough to hold contents of any section. */
8703 /* Buffer large enough to hold external relocs of any section. */
8704 void *external_relocs
;
8705 /* Buffer large enough to hold internal relocs of any section. */
8706 Elf_Internal_Rela
*internal_relocs
;
8707 /* Buffer large enough to hold external local symbols of any input
8709 bfd_byte
*external_syms
;
8710 /* And a buffer for symbol section indices. */
8711 Elf_External_Sym_Shndx
*locsym_shndx
;
8712 /* Buffer large enough to hold internal local symbols of any input
8714 Elf_Internal_Sym
*internal_syms
;
8715 /* Array large enough to hold a symbol index for each local symbol
8716 of any input BFD. */
8718 /* Array large enough to hold a section pointer for each local
8719 symbol of any input BFD. */
8720 asection
**sections
;
8721 /* Buffer for SHT_SYMTAB_SHNDX section. */
8722 Elf_External_Sym_Shndx
*symshndxbuf
;
8723 /* Number of STT_FILE syms seen. */
8724 size_t filesym_count
;
8725 /* Local symbol hash table. */
8726 struct bfd_hash_table local_hash_table
;
8729 struct local_hash_entry
8731 /* Base hash table entry structure. */
8732 struct bfd_hash_entry root
;
8733 /* Size of the local symbol name. */
8735 /* Number of the duplicated local symbol names. */
8739 /* Create an entry in the local symbol hash table. */
8741 static struct bfd_hash_entry
*
8742 local_hash_newfunc (struct bfd_hash_entry
*entry
,
8743 struct bfd_hash_table
*table
,
8747 /* Allocate the structure if it has not already been allocated by a
8751 entry
= bfd_hash_allocate (table
,
8752 sizeof (struct local_hash_entry
));
8757 /* Call the allocation method of the superclass. */
8758 entry
= bfd_hash_newfunc (entry
, table
, string
);
8761 ((struct local_hash_entry
*) entry
)->count
= 0;
8762 ((struct local_hash_entry
*) entry
)->size
= 0;
8768 /* This struct is used to pass information to elf_link_output_extsym. */
8770 struct elf_outext_info
8775 struct elf_final_link_info
*flinfo
;
8779 /* Support for evaluating a complex relocation.
8781 Complex relocations are generalized, self-describing relocations. The
8782 implementation of them consists of two parts: complex symbols, and the
8783 relocations themselves.
8785 The relocations use a reserved elf-wide relocation type code (R_RELC
8786 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8787 information (start bit, end bit, word width, etc) into the addend. This
8788 information is extracted from CGEN-generated operand tables within gas.
8790 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8791 internal) representing prefix-notation expressions, including but not
8792 limited to those sorts of expressions normally encoded as addends in the
8793 addend field. The symbol mangling format is:
8796 | <unary-operator> ':' <node>
8797 | <binary-operator> ':' <node> ':' <node>
8800 <literal> := 's' <digits=N> ':' <N character symbol name>
8801 | 'S' <digits=N> ':' <N character section name>
8805 <binary-operator> := as in C
8806 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8809 set_symbol_value (bfd
*bfd_with_globals
,
8810 Elf_Internal_Sym
*isymbuf
,
8815 struct elf_link_hash_entry
**sym_hashes
;
8816 struct elf_link_hash_entry
*h
;
8817 size_t extsymoff
= locsymcount
;
8819 if (symidx
< locsymcount
)
8821 Elf_Internal_Sym
*sym
;
8823 sym
= isymbuf
+ symidx
;
8824 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8826 /* It is a local symbol: move it to the
8827 "absolute" section and give it a value. */
8828 sym
->st_shndx
= SHN_ABS
;
8829 sym
->st_value
= val
;
8832 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8836 /* It is a global symbol: set its link type
8837 to "defined" and give it a value. */
8839 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8840 h
= sym_hashes
[symidx
- extsymoff
];
8841 while (h
->root
.type
== bfd_link_hash_indirect
8842 || h
->root
.type
== bfd_link_hash_warning
)
8843 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8844 h
->root
.type
= bfd_link_hash_defined
;
8845 h
->root
.u
.def
.value
= val
;
8846 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8850 resolve_symbol (const char *name
,
8852 struct elf_final_link_info
*flinfo
,
8854 Elf_Internal_Sym
*isymbuf
,
8857 Elf_Internal_Sym
*sym
;
8858 struct bfd_link_hash_entry
*global_entry
;
8859 const char *candidate
= NULL
;
8860 Elf_Internal_Shdr
*symtab_hdr
;
8863 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8865 for (i
= 0; i
< locsymcount
; ++ i
)
8869 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8872 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8873 symtab_hdr
->sh_link
,
8876 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8877 name
, candidate
, (unsigned long) sym
->st_value
);
8879 if (candidate
&& strcmp (candidate
, name
) == 0)
8881 asection
*sec
= flinfo
->sections
[i
];
8883 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8884 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8886 printf ("Found symbol with value %8.8lx\n",
8887 (unsigned long) *result
);
8893 /* Hmm, haven't found it yet. perhaps it is a global. */
8894 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8895 false, false, true);
8899 if (global_entry
->type
== bfd_link_hash_defined
8900 || global_entry
->type
== bfd_link_hash_defweak
)
8902 *result
= (global_entry
->u
.def
.value
8903 + global_entry
->u
.def
.section
->output_section
->vma
8904 + global_entry
->u
.def
.section
->output_offset
);
8906 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8907 global_entry
->root
.string
, (unsigned long) *result
);
8915 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8916 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8917 names like "foo.end" which is the end address of section "foo". */
8920 resolve_section (const char *name
,
8928 for (curr
= sections
; curr
; curr
= curr
->next
)
8929 if (strcmp (curr
->name
, name
) == 0)
8931 *result
= curr
->vma
;
8935 /* Hmm. still haven't found it. try pseudo-section names. */
8936 /* FIXME: This could be coded more efficiently... */
8937 for (curr
= sections
; curr
; curr
= curr
->next
)
8939 len
= strlen (curr
->name
);
8940 if (len
> strlen (name
))
8943 if (strncmp (curr
->name
, name
, len
) == 0)
8945 if (startswith (name
+ len
, ".end"))
8947 *result
= (curr
->vma
8948 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
8952 /* Insert more pseudo-section names here, if you like. */
8960 undefined_reference (const char *reftype
, const char *name
)
8962 /* xgettext:c-format */
8963 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8965 bfd_set_error (bfd_error_bad_value
);
8969 eval_symbol (bfd_vma
*result
,
8972 struct elf_final_link_info
*flinfo
,
8974 Elf_Internal_Sym
*isymbuf
,
8983 const char *sym
= *symp
;
8985 bool symbol_is_section
= false;
8990 if (len
< 1 || len
> sizeof (symbuf
))
8992 bfd_set_error (bfd_error_invalid_operation
);
9005 *result
= strtoul (sym
, (char **) symp
, 16);
9009 symbol_is_section
= true;
9013 symlen
= strtol (sym
, (char **) symp
, 10);
9014 sym
= *symp
+ 1; /* Skip the trailing ':'. */
9016 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
9018 bfd_set_error (bfd_error_invalid_operation
);
9022 memcpy (symbuf
, sym
, symlen
);
9023 symbuf
[symlen
] = '\0';
9024 *symp
= sym
+ symlen
;
9026 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9027 the symbol as a section, or vice-versa. so we're pretty liberal in our
9028 interpretation here; section means "try section first", not "must be a
9029 section", and likewise with symbol. */
9031 if (symbol_is_section
)
9033 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
9034 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
9035 isymbuf
, locsymcount
))
9037 undefined_reference ("section", symbuf
);
9043 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
9044 isymbuf
, locsymcount
)
9045 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
9048 undefined_reference ("symbol", symbuf
);
9055 /* All that remains are operators. */
9057 #define UNARY_OP(op) \
9058 if (startswith (sym, #op)) \
9060 sym += strlen (#op); \
9064 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9065 isymbuf, locsymcount, signed_p)) \
9068 *result = op ((bfd_signed_vma) a); \
9074 #define BINARY_OP_HEAD(op) \
9075 if (startswith (sym, #op)) \
9077 sym += strlen (#op); \
9081 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9082 isymbuf, locsymcount, signed_p)) \
9085 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9086 isymbuf, locsymcount, signed_p)) \
9088 #define BINARY_OP_TAIL(op) \
9090 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9095 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9099 BINARY_OP_HEAD (<<);
9100 if (b
>= sizeof (a
) * CHAR_BIT
)
9106 BINARY_OP_TAIL (<<);
9107 BINARY_OP_HEAD (>>);
9108 if (b
>= sizeof (a
) * CHAR_BIT
)
9110 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
9113 BINARY_OP_TAIL (>>);
9126 _bfd_error_handler (_("division by zero"));
9127 bfd_set_error (bfd_error_bad_value
);
9134 _bfd_error_handler (_("division by zero"));
9135 bfd_set_error (bfd_error_bad_value
);
9148 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
9149 bfd_set_error (bfd_error_invalid_operation
);
9155 put_value (bfd_vma size
,
9156 unsigned long chunksz
,
9161 location
+= (size
- chunksz
);
9163 for (; size
; size
-= chunksz
, location
-= chunksz
)
9168 bfd_put_8 (input_bfd
, x
, location
);
9172 bfd_put_16 (input_bfd
, x
, location
);
9176 bfd_put_32 (input_bfd
, x
, location
);
9177 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9183 bfd_put_64 (input_bfd
, x
, location
);
9184 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9197 get_value (bfd_vma size
,
9198 unsigned long chunksz
,
9205 /* Sanity checks. */
9206 BFD_ASSERT (chunksz
<= sizeof (x
)
9209 && (size
% chunksz
) == 0
9210 && input_bfd
!= NULL
9211 && location
!= NULL
);
9213 if (chunksz
== sizeof (x
))
9215 BFD_ASSERT (size
== chunksz
);
9217 /* Make sure that we do not perform an undefined shift operation.
9218 We know that size == chunksz so there will only be one iteration
9219 of the loop below. */
9223 shift
= 8 * chunksz
;
9225 for (; size
; size
-= chunksz
, location
+= chunksz
)
9230 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
9233 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
9236 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
9240 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
9251 decode_complex_addend (unsigned long *start
, /* in bits */
9252 unsigned long *oplen
, /* in bits */
9253 unsigned long *len
, /* in bits */
9254 unsigned long *wordsz
, /* in bytes */
9255 unsigned long *chunksz
, /* in bytes */
9256 unsigned long *lsb0_p
,
9257 unsigned long *signed_p
,
9258 unsigned long *trunc_p
,
9259 unsigned long encoded
)
9261 * start
= encoded
& 0x3F;
9262 * len
= (encoded
>> 6) & 0x3F;
9263 * oplen
= (encoded
>> 12) & 0x3F;
9264 * wordsz
= (encoded
>> 18) & 0xF;
9265 * chunksz
= (encoded
>> 22) & 0xF;
9266 * lsb0_p
= (encoded
>> 27) & 1;
9267 * signed_p
= (encoded
>> 28) & 1;
9268 * trunc_p
= (encoded
>> 29) & 1;
9271 bfd_reloc_status_type
9272 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
9273 asection
*input_section
,
9275 Elf_Internal_Rela
*rel
,
9278 bfd_vma shift
, x
, mask
;
9279 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9280 bfd_reloc_status_type r
;
9281 bfd_size_type octets
;
9283 /* Perform this reloc, since it is complex.
9284 (this is not to say that it necessarily refers to a complex
9285 symbol; merely that it is a self-describing CGEN based reloc.
9286 i.e. the addend has the complete reloc information (bit start, end,
9287 word size, etc) encoded within it.). */
9289 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9290 &chunksz
, &lsb0_p
, &signed_p
,
9291 &trunc_p
, rel
->r_addend
);
9293 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9296 shift
= (start
+ 1) - len
;
9298 shift
= (8 * wordsz
) - (start
+ len
);
9300 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9301 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9304 printf ("Doing complex reloc: "
9305 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9306 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9307 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9308 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9309 oplen
, (unsigned long) x
, (unsigned long) mask
,
9310 (unsigned long) relocation
);
9315 /* Now do an overflow check. */
9316 r
= bfd_check_overflow ((signed_p
9317 ? complain_overflow_signed
9318 : complain_overflow_unsigned
),
9319 len
, 0, (8 * wordsz
),
9323 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9326 printf (" relocation: %8.8lx\n"
9327 " shifted mask: %8.8lx\n"
9328 " shifted/masked reloc: %8.8lx\n"
9329 " result: %8.8lx\n",
9330 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9331 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9333 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9337 /* Functions to read r_offset from external (target order) reloc
9338 entry. Faster than bfd_getl32 et al, because we let the compiler
9339 know the value is aligned. */
9342 ext32l_r_offset (const void *p
)
9349 const union aligned32
*a
9350 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9352 uint32_t aval
= ( (uint32_t) a
->c
[0]
9353 | (uint32_t) a
->c
[1] << 8
9354 | (uint32_t) a
->c
[2] << 16
9355 | (uint32_t) a
->c
[3] << 24);
9360 ext32b_r_offset (const void *p
)
9367 const union aligned32
*a
9368 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9370 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9371 | (uint32_t) a
->c
[1] << 16
9372 | (uint32_t) a
->c
[2] << 8
9373 | (uint32_t) a
->c
[3]);
9378 ext64l_r_offset (const void *p
)
9385 const union aligned64
*a
9386 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9388 uint64_t aval
= ( (uint64_t) a
->c
[0]
9389 | (uint64_t) a
->c
[1] << 8
9390 | (uint64_t) a
->c
[2] << 16
9391 | (uint64_t) a
->c
[3] << 24
9392 | (uint64_t) a
->c
[4] << 32
9393 | (uint64_t) a
->c
[5] << 40
9394 | (uint64_t) a
->c
[6] << 48
9395 | (uint64_t) a
->c
[7] << 56);
9400 ext64b_r_offset (const void *p
)
9407 const union aligned64
*a
9408 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9410 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9411 | (uint64_t) a
->c
[1] << 48
9412 | (uint64_t) a
->c
[2] << 40
9413 | (uint64_t) a
->c
[3] << 32
9414 | (uint64_t) a
->c
[4] << 24
9415 | (uint64_t) a
->c
[5] << 16
9416 | (uint64_t) a
->c
[6] << 8
9417 | (uint64_t) a
->c
[7]);
9421 /* When performing a relocatable link, the input relocations are
9422 preserved. But, if they reference global symbols, the indices
9423 referenced must be updated. Update all the relocations found in
9427 elf_link_adjust_relocs (bfd
*abfd
,
9429 struct bfd_elf_section_reloc_data
*reldata
,
9431 struct bfd_link_info
*info
)
9434 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9436 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9437 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9438 bfd_vma r_type_mask
;
9440 unsigned int count
= reldata
->count
;
9441 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9443 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9445 swap_in
= bed
->s
->swap_reloc_in
;
9446 swap_out
= bed
->s
->swap_reloc_out
;
9448 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9450 swap_in
= bed
->s
->swap_reloca_in
;
9451 swap_out
= bed
->s
->swap_reloca_out
;
9456 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9459 if (bed
->s
->arch_size
== 32)
9466 r_type_mask
= 0xffffffff;
9470 erela
= reldata
->hdr
->contents
;
9471 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9473 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9476 if (*rel_hash
== NULL
)
9479 if ((*rel_hash
)->indx
== -2
9480 && info
->gc_sections
9481 && ! info
->gc_keep_exported
)
9483 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9484 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9486 (*rel_hash
)->root
.root
.string
);
9487 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9489 bfd_set_error (bfd_error_invalid_operation
);
9492 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9494 (*swap_in
) (abfd
, erela
, irela
);
9495 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9496 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9497 | (irela
[j
].r_info
& r_type_mask
));
9498 (*swap_out
) (abfd
, irela
, erela
);
9501 if (bed
->elf_backend_update_relocs
)
9502 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9504 if (sort
&& count
!= 0)
9506 bfd_vma (*ext_r_off
) (const void *);
9509 bfd_byte
*base
, *end
, *p
, *loc
;
9510 bfd_byte
*buf
= NULL
;
9512 if (bed
->s
->arch_size
== 32)
9514 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9515 ext_r_off
= ext32l_r_offset
;
9516 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9517 ext_r_off
= ext32b_r_offset
;
9523 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9524 ext_r_off
= ext64l_r_offset
;
9525 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9526 ext_r_off
= ext64b_r_offset
;
9531 /* Must use a stable sort here. A modified insertion sort,
9532 since the relocs are mostly sorted already. */
9533 elt_size
= reldata
->hdr
->sh_entsize
;
9534 base
= reldata
->hdr
->contents
;
9535 end
= base
+ count
* elt_size
;
9536 if (elt_size
> sizeof (Elf64_External_Rela
))
9539 /* Ensure the first element is lowest. This acts as a sentinel,
9540 speeding the main loop below. */
9541 r_off
= (*ext_r_off
) (base
);
9542 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9544 bfd_vma r_off2
= (*ext_r_off
) (p
);
9553 /* Don't just swap *base and *loc as that changes the order
9554 of the original base[0] and base[1] if they happen to
9555 have the same r_offset. */
9556 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9557 memcpy (onebuf
, loc
, elt_size
);
9558 memmove (base
+ elt_size
, base
, loc
- base
);
9559 memcpy (base
, onebuf
, elt_size
);
9562 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9564 /* base to p is sorted, *p is next to insert. */
9565 r_off
= (*ext_r_off
) (p
);
9566 /* Search the sorted region for location to insert. */
9568 while (r_off
< (*ext_r_off
) (loc
))
9573 /* Chances are there is a run of relocs to insert here,
9574 from one of more input files. Files are not always
9575 linked in order due to the way elf_link_input_bfd is
9576 called. See pr17666. */
9577 size_t sortlen
= p
- loc
;
9578 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9579 size_t runlen
= elt_size
;
9580 bfd_vma r_off_runend
= r_off
;
9581 bfd_vma r_off_runend_next
;
9582 size_t buf_size
= 96 * 1024;
9583 while (p
+ runlen
< end
9584 && (sortlen
<= buf_size
9585 || runlen
+ elt_size
<= buf_size
)
9586 /* run must not break the ordering of base..loc+1 */
9587 && r_off2
> (r_off_runend_next
= (*ext_r_off
) (p
+ runlen
))
9588 /* run must be already sorted */
9589 && r_off_runend_next
>= r_off_runend
)
9592 r_off_runend
= r_off_runend_next
;
9596 buf
= bfd_malloc (buf_size
);
9600 if (runlen
< sortlen
)
9602 memcpy (buf
, p
, runlen
);
9603 memmove (loc
+ runlen
, loc
, sortlen
);
9604 memcpy (loc
, buf
, runlen
);
9608 memcpy (buf
, loc
, sortlen
);
9609 memmove (loc
, p
, runlen
);
9610 memcpy (loc
+ runlen
, buf
, sortlen
);
9612 p
+= runlen
- elt_size
;
9615 /* Hashes are no longer valid. */
9616 free (reldata
->hashes
);
9617 reldata
->hashes
= NULL
;
9623 struct elf_link_sort_rela
9629 enum elf_reloc_type_class type
;
9630 /* We use this as an array of size int_rels_per_ext_rel. */
9631 Elf_Internal_Rela rela
[1];
9634 /* qsort stability here and for cmp2 is only an issue if multiple
9635 dynamic relocations are emitted at the same address. But targets
9636 that apply a series of dynamic relocations each operating on the
9637 result of the prior relocation can't use -z combreloc as
9638 implemented anyway. Such schemes tend to be broken by sorting on
9639 symbol index. That leaves dynamic NONE relocs as the only other
9640 case where ld might emit multiple relocs at the same address, and
9641 those are only emitted due to target bugs. */
9644 elf_link_sort_cmp1 (const void *A
, const void *B
)
9646 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9647 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9648 int relativea
, relativeb
;
9650 relativea
= a
->type
== reloc_class_relative
;
9651 relativeb
= b
->type
== reloc_class_relative
;
9653 if (relativea
< relativeb
)
9655 if (relativea
> relativeb
)
9657 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9659 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9661 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9663 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9669 elf_link_sort_cmp2 (const void *A
, const void *B
)
9671 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9672 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9674 if (a
->type
< b
->type
)
9676 if (a
->type
> b
->type
)
9678 if (a
->u
.offset
< b
->u
.offset
)
9680 if (a
->u
.offset
> b
->u
.offset
)
9682 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9684 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9690 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9692 asection
*dynamic_relocs
;
9695 bfd_size_type count
, size
;
9696 size_t i
, ret
, sort_elt
, ext_size
;
9697 bfd_byte
*sort
, *s_non_relative
, *p
;
9698 struct elf_link_sort_rela
*sq
;
9699 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9700 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9701 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9702 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9703 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9704 struct bfd_link_order
*lo
;
9708 /* Find a dynamic reloc section. */
9709 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9710 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9711 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9712 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9714 bool use_rela_initialised
= false;
9716 /* This is just here to stop gcc from complaining.
9717 Its initialization checking code is not perfect. */
9720 /* Both sections are present. Examine the sizes
9721 of the indirect sections to help us choose. */
9722 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9723 if (lo
->type
== bfd_indirect_link_order
)
9725 asection
*o
= lo
->u
.indirect
.section
;
9727 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9729 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9730 /* Section size is divisible by both rel and rela sizes.
9731 It is of no help to us. */
9735 /* Section size is only divisible by rela. */
9736 if (use_rela_initialised
&& !use_rela
)
9738 _bfd_error_handler (_("%pB: unable to sort relocs - "
9739 "they are in more than one size"),
9741 bfd_set_error (bfd_error_invalid_operation
);
9747 use_rela_initialised
= true;
9751 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9753 /* Section size is only divisible by rel. */
9754 if (use_rela_initialised
&& use_rela
)
9756 _bfd_error_handler (_("%pB: unable to sort relocs - "
9757 "they are in more than one size"),
9759 bfd_set_error (bfd_error_invalid_operation
);
9765 use_rela_initialised
= true;
9770 /* The section size is not divisible by either -
9771 something is wrong. */
9772 _bfd_error_handler (_("%pB: unable to sort relocs - "
9773 "they are of an unknown size"), abfd
);
9774 bfd_set_error (bfd_error_invalid_operation
);
9779 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9780 if (lo
->type
== bfd_indirect_link_order
)
9782 asection
*o
= lo
->u
.indirect
.section
;
9784 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9786 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9787 /* Section size is divisible by both rel and rela sizes.
9788 It is of no help to us. */
9792 /* Section size is only divisible by rela. */
9793 if (use_rela_initialised
&& !use_rela
)
9795 _bfd_error_handler (_("%pB: unable to sort relocs - "
9796 "they are in more than one size"),
9798 bfd_set_error (bfd_error_invalid_operation
);
9804 use_rela_initialised
= true;
9808 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9810 /* Section size is only divisible by rel. */
9811 if (use_rela_initialised
&& use_rela
)
9813 _bfd_error_handler (_("%pB: unable to sort relocs - "
9814 "they are in more than one size"),
9816 bfd_set_error (bfd_error_invalid_operation
);
9822 use_rela_initialised
= true;
9827 /* The section size is not divisible by either -
9828 something is wrong. */
9829 _bfd_error_handler (_("%pB: unable to sort relocs - "
9830 "they are of an unknown size"), abfd
);
9831 bfd_set_error (bfd_error_invalid_operation
);
9836 if (! use_rela_initialised
)
9840 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9842 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9849 dynamic_relocs
= rela_dyn
;
9850 ext_size
= bed
->s
->sizeof_rela
;
9851 swap_in
= bed
->s
->swap_reloca_in
;
9852 swap_out
= bed
->s
->swap_reloca_out
;
9856 dynamic_relocs
= rel_dyn
;
9857 ext_size
= bed
->s
->sizeof_rel
;
9858 swap_in
= bed
->s
->swap_reloc_in
;
9859 swap_out
= bed
->s
->swap_reloc_out
;
9863 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9864 if (lo
->type
== bfd_indirect_link_order
)
9865 size
+= lo
->u
.indirect
.section
->size
;
9867 if (size
!= dynamic_relocs
->size
)
9870 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9871 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9873 count
= dynamic_relocs
->size
/ ext_size
;
9876 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9880 (*info
->callbacks
->warning
)
9881 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9885 if (bed
->s
->arch_size
== 32)
9886 r_sym_mask
= ~(bfd_vma
) 0xff;
9888 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9890 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9891 if (lo
->type
== bfd_indirect_link_order
)
9893 bfd_byte
*erel
, *erelend
;
9894 asection
*o
= lo
->u
.indirect
.section
;
9896 if (o
->contents
== NULL
&& o
->size
!= 0)
9898 /* This is a reloc section that is being handled as a normal
9899 section. See bfd_section_from_shdr. We can't combine
9900 relocs in this case. */
9905 erelend
= o
->contents
+ o
->size
;
9906 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9908 while (erel
< erelend
)
9910 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9912 (*swap_in
) (abfd
, erel
, s
->rela
);
9913 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9914 s
->u
.sym_mask
= r_sym_mask
;
9920 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9922 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9924 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9925 if (s
->type
!= reloc_class_relative
)
9931 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9932 for (; i
< count
; i
++, p
+= sort_elt
)
9934 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9935 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9937 sp
->u
.offset
= sq
->rela
->r_offset
;
9940 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9942 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9943 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9945 /* We have plt relocs in .rela.dyn. */
9946 sq
= (struct elf_link_sort_rela
*) sort
;
9947 for (i
= 0; i
< count
; i
++)
9948 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9950 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9952 struct bfd_link_order
**plo
;
9953 /* Put srelplt link_order last. This is so the output_offset
9954 set in the next loop is correct for DT_JMPREL. */
9955 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9956 if ((*plo
)->type
== bfd_indirect_link_order
9957 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9963 plo
= &(*plo
)->next
;
9966 dynamic_relocs
->map_tail
.link_order
= lo
;
9971 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9972 if (lo
->type
== bfd_indirect_link_order
)
9974 bfd_byte
*erel
, *erelend
;
9975 asection
*o
= lo
->u
.indirect
.section
;
9978 erelend
= o
->contents
+ o
->size
;
9979 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9980 while (erel
< erelend
)
9982 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9983 (*swap_out
) (abfd
, s
->rela
, erel
);
9990 *psec
= dynamic_relocs
;
9994 /* Add a symbol to the output symbol string table. */
9997 elf_link_output_symstrtab (void *finf
,
9999 Elf_Internal_Sym
*elfsym
,
10000 asection
*input_sec
,
10001 struct elf_link_hash_entry
*h
)
10003 struct elf_final_link_info
*flinfo
= finf
;
10004 int (*output_symbol_hook
)
10005 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
10006 struct elf_link_hash_entry
*);
10007 struct elf_link_hash_table
*hash_table
;
10008 const struct elf_backend_data
*bed
;
10009 bfd_size_type strtabsize
;
10011 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10013 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10014 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
10015 if (output_symbol_hook
!= NULL
)
10017 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
10022 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
10023 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
10024 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
10025 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
10027 if (name
== NULL
|| *name
== '\0')
10028 elfsym
->st_name
= (unsigned long) -1;
10031 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10032 to get the final offset for st_name. */
10033 char *versioned_name
= (char *) name
;
10036 if (h
->versioned
== versioned
&& h
->def_dynamic
)
10038 /* Keep only one '@' for versioned symbols defined in
10040 char *version
= strrchr (name
, ELF_VER_CHR
);
10041 char *base_end
= strchr (name
, ELF_VER_CHR
);
10042 if (version
!= base_end
)
10045 size_t len
= strlen (name
);
10046 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
10047 if (versioned_name
== NULL
)
10049 base_len
= base_end
- name
;
10050 memcpy (versioned_name
, name
, base_len
);
10051 memcpy (versioned_name
+ base_len
, version
,
10056 else if (flinfo
->info
->unique_symbol
10057 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
10059 struct local_hash_entry
*lh
;
10063 switch (ELF_ST_TYPE (elfsym
->st_info
))
10069 lh
= (struct local_hash_entry
*) bfd_hash_lookup
10070 (&flinfo
->local_hash_table
, name
, true, false);
10073 /* Always append ".COUNT" to local symbols to avoid
10074 potential conflicts with local symbol "XXX.COUNT". */
10075 sprintf (buf
, "%lx", lh
->count
);
10076 base_len
= lh
->size
;
10079 base_len
= strlen (name
);
10080 lh
->size
= base_len
;
10082 count_len
= strlen (buf
);
10083 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
10084 base_len
+ count_len
+ 2);
10085 if (versioned_name
== NULL
)
10087 memcpy (versioned_name
, name
, base_len
);
10088 versioned_name
[base_len
] = '.';
10089 memcpy (versioned_name
+ base_len
+ 1, buf
,
10096 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
10097 versioned_name
, false);
10098 if (elfsym
->st_name
== (unsigned long) -1)
10102 hash_table
= elf_hash_table (flinfo
->info
);
10103 strtabsize
= hash_table
->strtabsize
;
10104 if (strtabsize
<= flinfo
->output_bfd
->symcount
)
10106 strtabsize
+= strtabsize
;
10107 hash_table
->strtabsize
= strtabsize
;
10108 strtabsize
*= sizeof (*hash_table
->strtab
);
10110 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
10112 if (hash_table
->strtab
== NULL
)
10115 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].sym
= *elfsym
;
10116 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].dest_index
10117 = flinfo
->output_bfd
->symcount
;
10118 flinfo
->output_bfd
->symcount
+= 1;
10123 /* Swap symbols out to the symbol table and flush the output symbols to
10127 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
10129 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
10132 const struct elf_backend_data
*bed
;
10134 Elf_Internal_Shdr
*hdr
;
10138 if (flinfo
->output_bfd
->symcount
== 0)
10141 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10143 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10145 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10146 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
10147 if (symbuf
== NULL
)
10150 if (flinfo
->symshndxbuf
)
10152 amt
= sizeof (Elf_External_Sym_Shndx
);
10153 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
10154 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
10155 if (flinfo
->symshndxbuf
== NULL
)
10162 /* Now swap out the symbols. */
10163 for (i
= 0; i
< flinfo
->output_bfd
->symcount
; i
++)
10165 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
10166 if (elfsym
->sym
.st_name
== (unsigned long) -1)
10167 elfsym
->sym
.st_name
= 0;
10169 elfsym
->sym
.st_name
10170 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
10171 elfsym
->sym
.st_name
);
10173 /* Inform the linker of the addition of this symbol. */
10175 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
10176 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
10179 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
10180 ((bfd_byte
*) symbuf
10181 + (elfsym
->dest_index
10182 * bed
->s
->sizeof_sym
)),
10183 NPTR_ADD (flinfo
->symshndxbuf
,
10184 elfsym
->dest_index
));
10187 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
10188 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
10189 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10190 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
10191 && bfd_write (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
10193 hdr
->sh_size
+= amt
;
10201 free (hash_table
->strtab
);
10202 hash_table
->strtab
= NULL
;
10207 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10210 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
10212 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
10213 && sym
->st_shndx
< SHN_LORESERVE
)
10215 /* The gABI doesn't support dynamic symbols in output sections
10218 /* xgettext:c-format */
10219 (_("%pB: too many sections: %d (>= %d)"),
10220 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
10221 bfd_set_error (bfd_error_nonrepresentable_section
);
10227 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10228 allowing an unsatisfied unversioned symbol in the DSO to match a
10229 versioned symbol that would normally require an explicit version.
10230 We also handle the case that a DSO references a hidden symbol
10231 which may be satisfied by a versioned symbol in another DSO. */
10234 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
10235 const struct elf_backend_data
*bed
,
10236 struct elf_link_hash_entry
*h
)
10239 struct elf_link_loaded_list
*loaded
;
10241 if (!is_elf_hash_table (info
->hash
))
10244 /* Check indirect symbol. */
10245 while (h
->root
.type
== bfd_link_hash_indirect
)
10246 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10248 switch (h
->root
.type
)
10254 case bfd_link_hash_undefined
:
10255 case bfd_link_hash_undefweak
:
10256 abfd
= h
->root
.u
.undef
.abfd
;
10258 || (abfd
->flags
& DYNAMIC
) == 0
10259 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
10263 case bfd_link_hash_defined
:
10264 case bfd_link_hash_defweak
:
10265 abfd
= h
->root
.u
.def
.section
->owner
;
10268 case bfd_link_hash_common
:
10269 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10272 BFD_ASSERT (abfd
!= NULL
);
10274 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10276 loaded
= loaded
->next
)
10279 Elf_Internal_Shdr
*hdr
;
10281 size_t extsymcount
;
10283 Elf_Internal_Shdr
*versymhdr
;
10284 Elf_Internal_Sym
*isym
;
10285 Elf_Internal_Sym
*isymend
;
10286 Elf_Internal_Sym
*isymbuf
;
10287 Elf_External_Versym
*ever
;
10288 Elf_External_Versym
*extversym
;
10290 input
= loaded
->abfd
;
10292 /* We check each DSO for a possible hidden versioned definition. */
10294 || elf_dynversym (input
) == 0)
10297 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10299 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10300 if (elf_bad_symtab (input
))
10302 extsymcount
= symcount
;
10307 extsymcount
= symcount
- hdr
->sh_info
;
10308 extsymoff
= hdr
->sh_info
;
10311 if (extsymcount
== 0)
10314 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10316 if (isymbuf
== NULL
)
10319 /* Read in any version definitions. */
10320 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10321 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10322 || (extversym
= (Elf_External_Versym
*)
10323 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10324 versymhdr
->sh_size
)) == NULL
)
10330 ever
= extversym
+ extsymoff
;
10331 isymend
= isymbuf
+ extsymcount
;
10332 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10335 Elf_Internal_Versym iver
;
10336 unsigned short version_index
;
10338 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10339 || isym
->st_shndx
== SHN_UNDEF
)
10342 name
= bfd_elf_string_from_elf_section (input
,
10345 if (strcmp (name
, h
->root
.root
.string
) != 0)
10348 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10350 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10351 && !(h
->def_regular
10352 && h
->forced_local
))
10354 /* If we have a non-hidden versioned sym, then it should
10355 have provided a definition for the undefined sym unless
10356 it is defined in a non-shared object and forced local.
10361 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10362 if (version_index
== 1 || version_index
== 2)
10364 /* This is the base or first version. We can use it. */
10378 /* Convert ELF common symbol TYPE. */
10381 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10383 /* Commom symbol can only appear in relocatable link. */
10384 if (!bfd_link_relocatable (info
))
10386 switch (info
->elf_stt_common
)
10390 case elf_stt_common
:
10393 case no_elf_stt_common
:
10400 /* Add an external symbol to the symbol table. This is called from
10401 the hash table traversal routine. When generating a shared object,
10402 we go through the symbol table twice. The first time we output
10403 anything that might have been forced to local scope in a version
10404 script. The second time we output the symbols that are still
10408 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10410 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10411 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10412 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10414 Elf_Internal_Sym sym
;
10415 asection
*input_sec
;
10416 const struct elf_backend_data
*bed
;
10421 if (h
->root
.type
== bfd_link_hash_warning
)
10423 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10424 if (h
->root
.type
== bfd_link_hash_new
)
10428 /* Decide whether to output this symbol in this pass. */
10429 if (eoinfo
->localsyms
)
10431 if (!h
->forced_local
)
10436 if (h
->forced_local
)
10440 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10442 if (h
->root
.type
== bfd_link_hash_undefined
)
10444 /* If we have an undefined symbol reference here then it must have
10445 come from a shared library that is being linked in. (Undefined
10446 references in regular files have already been handled unless
10447 they are in unreferenced sections which are removed by garbage
10449 bool ignore_undef
= false;
10451 /* Some symbols may be special in that the fact that they're
10452 undefined can be safely ignored - let backend determine that. */
10453 if (bed
->elf_backend_ignore_undef_symbol
)
10454 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10456 /* If we are reporting errors for this situation then do so now. */
10458 && h
->ref_dynamic_nonweak
10459 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10460 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10461 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10463 flinfo
->info
->callbacks
->undefined_symbol
10464 (flinfo
->info
, h
->root
.root
.string
,
10465 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10466 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10467 && !flinfo
->info
->warn_unresolved_syms
);
10470 /* Strip a global symbol defined in a discarded section. */
10475 /* We should also warn if a forced local symbol is referenced from
10476 shared libraries. */
10477 if (bfd_link_executable (flinfo
->info
)
10482 && h
->ref_dynamic_nonweak
10483 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10487 struct elf_link_hash_entry
*hi
= h
;
10489 /* Check indirect symbol. */
10490 while (hi
->root
.type
== bfd_link_hash_indirect
)
10491 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10493 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10494 /* xgettext:c-format */
10495 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10496 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10497 /* xgettext:c-format */
10498 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10500 /* xgettext:c-format */
10501 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10502 def_bfd
= flinfo
->output_bfd
;
10503 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10504 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10505 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10506 h
->root
.root
.string
, def_bfd
);
10507 bfd_set_error (bfd_error_bad_value
);
10508 eoinfo
->failed
= true;
10512 /* We don't want to output symbols that have never been mentioned by
10513 a regular file, or that we have been told to strip. However, if
10514 h->indx is set to -2, the symbol is used by a reloc and we must
10519 else if ((h
->def_dynamic
10521 || h
->root
.type
== bfd_link_hash_new
)
10523 && !h
->ref_regular
)
10525 else if (flinfo
->info
->strip
== strip_all
)
10527 else if (flinfo
->info
->strip
== strip_some
10528 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10529 h
->root
.root
.string
, false, false) == NULL
)
10531 else if ((h
->root
.type
== bfd_link_hash_defined
10532 || h
->root
.type
== bfd_link_hash_defweak
)
10533 && ((flinfo
->info
->strip_discarded
10534 && discarded_section (h
->root
.u
.def
.section
))
10535 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10536 && h
->root
.u
.def
.section
->owner
!= NULL
10537 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10539 else if ((h
->root
.type
== bfd_link_hash_undefined
10540 || h
->root
.type
== bfd_link_hash_undefweak
)
10541 && h
->root
.u
.undef
.abfd
!= NULL
10542 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10547 /* If we're stripping it, and it's not a dynamic symbol, there's
10548 nothing else to do. However, if it is a forced local symbol or
10549 an ifunc symbol we need to give the backend finish_dynamic_symbol
10550 function a chance to make it dynamic. */
10552 && h
->dynindx
== -1
10553 && type
!= STT_GNU_IFUNC
10554 && !h
->forced_local
)
10558 sym
.st_size
= h
->size
;
10559 sym
.st_other
= h
->other
;
10560 switch (h
->root
.type
)
10563 case bfd_link_hash_new
:
10564 case bfd_link_hash_warning
:
10568 case bfd_link_hash_undefined
:
10569 case bfd_link_hash_undefweak
:
10570 input_sec
= bfd_und_section_ptr
;
10571 sym
.st_shndx
= SHN_UNDEF
;
10574 case bfd_link_hash_defined
:
10575 case bfd_link_hash_defweak
:
10577 input_sec
= h
->root
.u
.def
.section
;
10578 if (input_sec
->output_section
!= NULL
)
10581 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10582 input_sec
->output_section
);
10583 if (sym
.st_shndx
== SHN_BAD
)
10586 /* xgettext:c-format */
10587 (_("%pB: could not find output section %pA for input section %pA"),
10588 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10589 bfd_set_error (bfd_error_nonrepresentable_section
);
10590 eoinfo
->failed
= true;
10594 /* ELF symbols in relocatable files are section relative,
10595 but in nonrelocatable files they are virtual
10597 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10598 if (!bfd_link_relocatable (flinfo
->info
))
10600 sym
.st_value
+= input_sec
->output_section
->vma
;
10601 if (h
->type
== STT_TLS
)
10603 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10604 if (tls_sec
!= NULL
)
10605 sym
.st_value
-= tls_sec
->vma
;
10611 BFD_ASSERT (input_sec
->owner
== NULL
10612 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10613 sym
.st_shndx
= SHN_UNDEF
;
10614 input_sec
= bfd_und_section_ptr
;
10619 case bfd_link_hash_common
:
10620 input_sec
= h
->root
.u
.c
.p
->section
;
10621 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10622 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10625 case bfd_link_hash_indirect
:
10626 /* These symbols are created by symbol versioning. They point
10627 to the decorated version of the name. For example, if the
10628 symbol foo@@GNU_1.2 is the default, which should be used when
10629 foo is used with no version, then we add an indirect symbol
10630 foo which points to foo@@GNU_1.2. We ignore these symbols,
10631 since the indirected symbol is already in the hash table. */
10635 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10636 switch (h
->root
.type
)
10638 case bfd_link_hash_common
:
10639 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10641 case bfd_link_hash_defined
:
10642 case bfd_link_hash_defweak
:
10643 if (bed
->common_definition (&sym
))
10644 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10648 case bfd_link_hash_undefined
:
10649 case bfd_link_hash_undefweak
:
10655 if (h
->forced_local
)
10657 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10658 /* Turn off visibility on local symbol. */
10659 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10661 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10662 else if (h
->unique_global
&& h
->def_regular
)
10663 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10664 else if (h
->root
.type
== bfd_link_hash_undefweak
10665 || h
->root
.type
== bfd_link_hash_defweak
)
10666 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10668 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10669 sym
.st_target_internal
= h
->target_internal
;
10671 /* Give the processor backend a chance to tweak the symbol value,
10672 and also to finish up anything that needs to be done for this
10673 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10674 forced local syms when non-shared is due to a historical quirk.
10675 STT_GNU_IFUNC symbol must go through PLT. */
10676 if ((h
->type
== STT_GNU_IFUNC
10678 && !bfd_link_relocatable (flinfo
->info
))
10679 || ((h
->dynindx
!= -1
10680 || h
->forced_local
)
10681 && ((bfd_link_pic (flinfo
->info
)
10682 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10683 || h
->root
.type
!= bfd_link_hash_undefweak
))
10684 || !h
->forced_local
)
10685 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10687 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10688 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10690 eoinfo
->failed
= true;
10695 /* If we are marking the symbol as undefined, and there are no
10696 non-weak references to this symbol from a regular object, then
10697 mark the symbol as weak undefined; if there are non-weak
10698 references, mark the symbol as strong. We can't do this earlier,
10699 because it might not be marked as undefined until the
10700 finish_dynamic_symbol routine gets through with it. */
10701 if (sym
.st_shndx
== SHN_UNDEF
10703 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10704 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10707 type
= ELF_ST_TYPE (sym
.st_info
);
10709 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10710 if (type
== STT_GNU_IFUNC
)
10713 if (h
->ref_regular_nonweak
)
10714 bindtype
= STB_GLOBAL
;
10716 bindtype
= STB_WEAK
;
10717 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10720 /* If this is a symbol defined in a dynamic library, don't use the
10721 symbol size from the dynamic library. Relinking an executable
10722 against a new library may introduce gratuitous changes in the
10723 executable's symbols if we keep the size. */
10724 if (sym
.st_shndx
== SHN_UNDEF
10729 /* If a non-weak symbol with non-default visibility is not defined
10730 locally, it is a fatal error. */
10731 if (!bfd_link_relocatable (flinfo
->info
)
10732 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10733 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10734 && h
->root
.type
== bfd_link_hash_undefined
10735 && !h
->def_regular
)
10739 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10740 /* xgettext:c-format */
10741 msg
= _("%pB: protected symbol `%s' isn't defined");
10742 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10743 /* xgettext:c-format */
10744 msg
= _("%pB: internal symbol `%s' isn't defined");
10746 /* xgettext:c-format */
10747 msg
= _("%pB: hidden symbol `%s' isn't defined");
10748 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10749 bfd_set_error (bfd_error_bad_value
);
10750 eoinfo
->failed
= true;
10754 /* If this symbol should be put in the .dynsym section, then put it
10755 there now. We already know the symbol index. We also fill in
10756 the entry in the .hash section. */
10757 if (h
->dynindx
!= -1
10758 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10759 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10760 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10764 /* Since there is no version information in the dynamic string,
10765 if there is no version info in symbol version section, we will
10766 have a run-time problem if not linking executable, referenced
10767 by shared library, or not bound locally. */
10768 if (h
->verinfo
.verdef
== NULL
10769 && (!bfd_link_executable (flinfo
->info
)
10771 || !h
->def_regular
))
10773 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10775 if (p
&& p
[1] != '\0')
10778 /* xgettext:c-format */
10779 (_("%pB: no symbol version section for versioned symbol `%s'"),
10780 flinfo
->output_bfd
, h
->root
.root
.string
);
10781 eoinfo
->failed
= true;
10786 sym
.st_name
= h
->dynstr_index
;
10787 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10788 + h
->dynindx
* bed
->s
->sizeof_sym
);
10789 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10791 eoinfo
->failed
= true;
10795 /* Inform the linker of the addition of this symbol. */
10797 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
10798 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
10800 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10802 if (flinfo
->hash_sec
!= NULL
)
10804 size_t hash_entry_size
;
10805 bfd_byte
*bucketpos
;
10807 size_t bucketcount
;
10810 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10811 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10814 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10815 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10816 + (bucket
+ 2) * hash_entry_size
);
10817 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10818 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10820 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10821 ((bfd_byte
*) flinfo
->hash_sec
->contents
10822 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10825 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10827 Elf_Internal_Versym iversym
;
10828 Elf_External_Versym
*eversym
;
10830 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10832 if (h
->verinfo
.verdef
== NULL
10833 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10834 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10835 iversym
.vs_vers
= 1;
10837 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10841 if (h
->verinfo
.vertree
== NULL
)
10842 iversym
.vs_vers
= 1;
10844 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10845 if (flinfo
->info
->create_default_symver
)
10849 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10850 defined locally. */
10851 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10852 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10854 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10855 eversym
+= h
->dynindx
;
10856 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10860 /* If the symbol is undefined, and we didn't output it to .dynsym,
10861 strip it from .symtab too. Obviously we can't do this for
10862 relocatable output or when needed for --emit-relocs. */
10863 else if (input_sec
== bfd_und_section_ptr
10865 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10866 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10867 && !bfd_link_relocatable (flinfo
->info
))
10870 /* Also strip others that we couldn't earlier due to dynamic symbol
10874 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10877 /* Output a FILE symbol so that following locals are not associated
10878 with the wrong input file. We need one for forced local symbols
10879 if we've seen more than one FILE symbol or when we have exactly
10880 one FILE symbol but global symbols are present in a file other
10881 than the one with the FILE symbol. We also need one if linker
10882 defined symbols are present. In practice these conditions are
10883 always met, so just emit the FILE symbol unconditionally. */
10884 if (eoinfo
->localsyms
10885 && !eoinfo
->file_sym_done
10886 && eoinfo
->flinfo
->filesym_count
!= 0)
10888 Elf_Internal_Sym fsym
;
10890 memset (&fsym
, 0, sizeof (fsym
));
10891 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10892 fsym
.st_shndx
= SHN_ABS
;
10893 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10894 bfd_und_section_ptr
, NULL
))
10897 eoinfo
->file_sym_done
= true;
10900 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10901 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10905 eoinfo
->failed
= true;
10910 else if (h
->indx
== -2)
10916 /* Return TRUE if special handling is done for relocs in SEC against
10917 symbols defined in discarded sections. */
10920 elf_section_ignore_discarded_relocs (asection
*sec
)
10922 const struct elf_backend_data
*bed
;
10924 switch (sec
->sec_info_type
)
10926 case SEC_INFO_TYPE_STABS
:
10927 case SEC_INFO_TYPE_EH_FRAME
:
10928 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10929 case SEC_INFO_TYPE_SFRAME
:
10935 bed
= get_elf_backend_data (sec
->owner
);
10936 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10937 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10943 /* Return a mask saying how ld should treat relocations in SEC against
10944 symbols defined in discarded sections. If this function returns
10945 COMPLAIN set, ld will issue a warning message. If this function
10946 returns PRETEND set, and the discarded section was link-once and the
10947 same size as the kept link-once section, ld will pretend that the
10948 symbol was actually defined in the kept section. Otherwise ld will
10949 zero the reloc (at least that is the intent, but some cooperation by
10950 the target dependent code is needed, particularly for REL targets). */
10953 _bfd_elf_default_action_discarded (asection
*sec
)
10955 const struct elf_backend_data
*bed
;
10956 bed
= get_elf_backend_data (sec
->owner
);
10958 if (sec
->flags
& SEC_DEBUGGING
)
10961 if (strcmp (".eh_frame", sec
->name
) == 0)
10964 if (bed
->elf_backend_can_make_multiple_eh_frame
10965 && strncmp (sec
->name
, ".eh_frame.", 10) == 0)
10968 if (strcmp (".sframe", sec
->name
) == 0)
10971 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10974 return COMPLAIN
| PRETEND
;
10977 /* Find a match between a section and a member of a section group. */
10980 match_group_member (asection
*sec
, asection
*group
,
10981 struct bfd_link_info
*info
)
10983 asection
*first
= elf_next_in_group (group
);
10984 asection
*s
= first
;
10988 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10991 s
= elf_next_in_group (s
);
10999 /* Check if the kept section of a discarded section SEC can be used
11000 to replace it. Return the replacement if it is OK. Otherwise return
11004 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
11008 kept
= sec
->kept_section
;
11011 if ((kept
->flags
& SEC_GROUP
) != 0)
11012 kept
= match_group_member (sec
, kept
, info
);
11015 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
11016 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
11020 /* Get the real kept section. */
11022 for (next
= kept
->kept_section
;
11024 next
= next
->kept_section
)
11028 sec
->kept_section
= kept
;
11033 /* Link an input file into the linker output file. This function
11034 handles all the sections and relocations of the input file at once.
11035 This is so that we only have to read the local symbols once, and
11036 don't have to keep them in memory. */
11039 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
11041 int (*relocate_section
)
11042 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
11043 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
11045 Elf_Internal_Shdr
*symtab_hdr
;
11046 size_t locsymcount
;
11048 Elf_Internal_Sym
*isymbuf
;
11049 Elf_Internal_Sym
*isym
;
11050 Elf_Internal_Sym
*isymend
;
11052 asection
**ppsection
;
11054 const struct elf_backend_data
*bed
;
11055 struct elf_link_hash_entry
**sym_hashes
;
11056 bfd_size_type address_size
;
11057 bfd_vma r_type_mask
;
11059 bool have_file_sym
= false;
11061 output_bfd
= flinfo
->output_bfd
;
11062 bed
= get_elf_backend_data (output_bfd
);
11063 relocate_section
= bed
->elf_backend_relocate_section
;
11065 /* If this is a dynamic object, we don't want to do anything here:
11066 we don't want the local symbols, and we don't want the section
11068 if ((input_bfd
->flags
& DYNAMIC
) != 0)
11071 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
11072 if (elf_bad_symtab (input_bfd
))
11074 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11079 locsymcount
= symtab_hdr
->sh_info
;
11080 extsymoff
= symtab_hdr
->sh_info
;
11083 /* Enable GNU OSABI features in the output BFD that are used in the input
11085 if (bed
->elf_osabi
== ELFOSABI_NONE
11086 || bed
->elf_osabi
== ELFOSABI_GNU
11087 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
11088 elf_tdata (output_bfd
)->has_gnu_osabi
11089 |= (elf_tdata (input_bfd
)->has_gnu_osabi
11090 & (bfd_link_relocatable (flinfo
->info
)
11091 ? -1 : ~elf_gnu_osabi_retain
));
11093 /* Read the local symbols. */
11094 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11095 if (isymbuf
== NULL
&& locsymcount
!= 0)
11097 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
11098 flinfo
->internal_syms
,
11099 flinfo
->external_syms
,
11100 flinfo
->locsym_shndx
);
11101 if (isymbuf
== NULL
)
11105 /* Find local symbol sections and adjust values of symbols in
11106 SEC_MERGE sections. Write out those local symbols we know are
11107 going into the output file. */
11108 isymend
= PTR_ADD (isymbuf
, locsymcount
);
11109 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
11111 isym
++, pindex
++, ppsection
++)
11115 Elf_Internal_Sym osym
;
11121 if (elf_bad_symtab (input_bfd
))
11123 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
11130 if (isym
->st_shndx
== SHN_UNDEF
)
11131 isec
= bfd_und_section_ptr
;
11132 else if (isym
->st_shndx
== SHN_ABS
)
11133 isec
= bfd_abs_section_ptr
;
11134 else if (isym
->st_shndx
== SHN_COMMON
)
11135 isec
= bfd_com_section_ptr
;
11138 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
11141 /* Don't attempt to output symbols with st_shnx in the
11142 reserved range other than SHN_ABS and SHN_COMMON. */
11143 isec
= bfd_und_section_ptr
;
11145 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
11146 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
11148 _bfd_merged_section_offset (output_bfd
, &isec
,
11149 elf_section_data (isec
)->sec_info
,
11155 /* Don't output the first, undefined, symbol. In fact, don't
11156 output any undefined local symbol. */
11157 if (isec
== bfd_und_section_ptr
)
11160 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
11162 /* We never output section symbols. Instead, we use the
11163 section symbol of the corresponding section in the output
11168 /* If we are stripping all symbols, we don't want to output this
11170 if (flinfo
->info
->strip
== strip_all
)
11173 /* If we are discarding all local symbols, we don't want to
11174 output this one. If we are generating a relocatable output
11175 file, then some of the local symbols may be required by
11176 relocs; we output them below as we discover that they are
11178 if (flinfo
->info
->discard
== discard_all
)
11181 /* If this symbol is defined in a section which we are
11182 discarding, we don't need to keep it. */
11183 if (isym
->st_shndx
< SHN_LORESERVE
11184 && (isec
->output_section
== NULL
11185 || bfd_section_removed_from_list (output_bfd
,
11186 isec
->output_section
)))
11189 /* Get the name of the symbol. */
11190 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
11195 /* See if we are discarding symbols with this name. */
11196 if ((flinfo
->info
->strip
== strip_some
11197 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, false, false)
11199 || (((flinfo
->info
->discard
== discard_sec_merge
11200 && (isec
->flags
& SEC_MERGE
)
11201 && !bfd_link_relocatable (flinfo
->info
))
11202 || flinfo
->info
->discard
== discard_l
)
11203 && bfd_is_local_label_name (input_bfd
, name
)))
11206 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
11208 if (input_bfd
->lto_output
)
11209 /* -flto puts a temp file name here. This means builds
11210 are not reproducible. Discard the symbol. */
11212 have_file_sym
= true;
11213 flinfo
->filesym_count
+= 1;
11215 if (!have_file_sym
)
11217 /* In the absence of debug info, bfd_find_nearest_line uses
11218 FILE symbols to determine the source file for local
11219 function symbols. Provide a FILE symbol here if input
11220 files lack such, so that their symbols won't be
11221 associated with a previous input file. It's not the
11222 source file, but the best we can do. */
11223 const char *filename
;
11224 have_file_sym
= true;
11225 flinfo
->filesym_count
+= 1;
11226 memset (&osym
, 0, sizeof (osym
));
11227 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11228 osym
.st_shndx
= SHN_ABS
;
11229 if (input_bfd
->lto_output
)
11232 filename
= lbasename (bfd_get_filename (input_bfd
));
11233 if (!elf_link_output_symstrtab (flinfo
, filename
, &osym
,
11234 bfd_abs_section_ptr
, NULL
))
11240 /* Adjust the section index for the output file. */
11241 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11242 isec
->output_section
);
11243 if (osym
.st_shndx
== SHN_BAD
)
11246 /* ELF symbols in relocatable files are section relative, but
11247 in executable files they are virtual addresses. Note that
11248 this code assumes that all ELF sections have an associated
11249 BFD section with a reasonable value for output_offset; below
11250 we assume that they also have a reasonable value for
11251 output_section. Any special sections must be set up to meet
11252 these requirements. */
11253 osym
.st_value
+= isec
->output_offset
;
11254 if (!bfd_link_relocatable (flinfo
->info
))
11256 osym
.st_value
+= isec
->output_section
->vma
;
11257 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
11259 /* STT_TLS symbols are relative to PT_TLS segment base. */
11260 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
11261 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
11263 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
11268 indx
= bfd_get_symcount (output_bfd
);
11269 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11276 if (bed
->s
->arch_size
== 32)
11278 r_type_mask
= 0xff;
11284 r_type_mask
= 0xffffffff;
11289 /* Relocate the contents of each section. */
11290 sym_hashes
= elf_sym_hashes (input_bfd
);
11291 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11293 bfd_byte
*contents
;
11295 if (! o
->linker_mark
)
11297 /* This section was omitted from the link. */
11301 if (!flinfo
->info
->resolve_section_groups
11302 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11304 /* Deal with the group signature symbol. */
11305 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11306 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11307 asection
*osec
= o
->output_section
;
11309 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11310 if (symndx
>= locsymcount
11311 || (elf_bad_symtab (input_bfd
)
11312 && flinfo
->sections
[symndx
] == NULL
))
11314 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
11315 while (h
->root
.type
== bfd_link_hash_indirect
11316 || h
->root
.type
== bfd_link_hash_warning
)
11317 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11318 /* Arrange for symbol to be output. */
11320 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11322 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11324 /* We'll use the output section target_index. */
11325 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11326 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11330 if (flinfo
->indices
[symndx
] == -1)
11332 /* Otherwise output the local symbol now. */
11333 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11334 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11339 name
= bfd_elf_string_from_elf_section (input_bfd
,
11340 symtab_hdr
->sh_link
,
11345 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11347 if (sym
.st_shndx
== SHN_BAD
)
11350 sym
.st_value
+= o
->output_offset
;
11352 indx
= bfd_get_symcount (output_bfd
);
11353 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11358 flinfo
->indices
[symndx
] = indx
;
11362 elf_section_data (osec
)->this_hdr
.sh_info
11363 = flinfo
->indices
[symndx
];
11367 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11368 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11371 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11373 /* Section was created by _bfd_elf_link_create_dynamic_sections
11378 /* Get the contents of the section. They have been cached by a
11379 relaxation routine. Note that o is a section in an input
11380 file, so the contents field will not have been set by any of
11381 the routines which work on output files. */
11382 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11384 contents
= elf_section_data (o
)->this_hdr
.contents
;
11385 if (bed
->caches_rawsize
11387 && o
->rawsize
< o
->size
)
11389 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11390 contents
= flinfo
->contents
;
11393 else if (!(o
->flags
& SEC_RELOC
)
11394 && !bed
->elf_backend_write_section
11395 && o
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
11396 /* A MERGE section that has no relocations doesn't need the
11397 contents anymore, they have been recorded earlier. Except
11398 if the backend has special provisions for writing sections. */
11402 contents
= flinfo
->contents
;
11403 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
11407 if ((o
->flags
& SEC_RELOC
) != 0)
11409 Elf_Internal_Rela
*internal_relocs
;
11410 Elf_Internal_Rela
*rel
, *relend
;
11411 int action_discarded
;
11414 /* Get the swapped relocs. */
11416 = _bfd_elf_link_info_read_relocs (input_bfd
, flinfo
->info
, o
,
11417 flinfo
->external_relocs
,
11418 flinfo
->internal_relocs
,
11420 if (internal_relocs
== NULL
11421 && o
->reloc_count
> 0)
11424 action_discarded
= -1;
11425 if (!elf_section_ignore_discarded_relocs (o
))
11426 action_discarded
= (*bed
->action_discarded
) (o
);
11428 /* Run through the relocs evaluating complex reloc symbols and
11429 looking for relocs against symbols from discarded sections
11430 or section symbols from removed link-once sections.
11431 Complain about relocs against discarded sections. Zero
11432 relocs against removed link-once sections. */
11434 rel
= internal_relocs
;
11435 relend
= rel
+ o
->reloc_count
;
11436 for ( ; rel
< relend
; rel
++)
11438 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11439 unsigned int s_type
;
11440 asection
**ps
, *sec
;
11441 struct elf_link_hash_entry
*h
= NULL
;
11442 const char *sym_name
;
11444 if (r_symndx
== STN_UNDEF
)
11447 if (r_symndx
>= locsymcount
11448 || (elf_bad_symtab (input_bfd
)
11449 && flinfo
->sections
[r_symndx
] == NULL
))
11451 h
= sym_hashes
[r_symndx
- extsymoff
];
11453 /* Badly formatted input files can contain relocs that
11454 reference non-existant symbols. Check here so that
11455 we do not seg fault. */
11459 /* xgettext:c-format */
11460 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11461 "that references a non-existent global symbol"),
11462 input_bfd
, (uint64_t) rel
->r_info
, o
);
11463 bfd_set_error (bfd_error_bad_value
);
11467 while (h
->root
.type
== bfd_link_hash_indirect
11468 || h
->root
.type
== bfd_link_hash_warning
)
11469 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11473 /* If a plugin symbol is referenced from a non-IR file,
11474 mark the symbol as undefined. Note that the
11475 linker may attach linker created dynamic sections
11476 to the plugin bfd. Symbols defined in linker
11477 created sections are not plugin symbols. */
11478 if ((h
->root
.non_ir_ref_regular
11479 || h
->root
.non_ir_ref_dynamic
)
11480 && (h
->root
.type
== bfd_link_hash_defined
11481 || h
->root
.type
== bfd_link_hash_defweak
)
11482 && (h
->root
.u
.def
.section
->flags
11483 & SEC_LINKER_CREATED
) == 0
11484 && h
->root
.u
.def
.section
->owner
!= NULL
11485 && (h
->root
.u
.def
.section
->owner
->flags
11486 & BFD_PLUGIN
) != 0)
11488 h
->root
.type
= bfd_link_hash_undefined
;
11489 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11493 if (h
->root
.type
== bfd_link_hash_defined
11494 || h
->root
.type
== bfd_link_hash_defweak
)
11495 ps
= &h
->root
.u
.def
.section
;
11497 sym_name
= h
->root
.root
.string
;
11501 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11503 s_type
= ELF_ST_TYPE (sym
->st_info
);
11504 ps
= &flinfo
->sections
[r_symndx
];
11505 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11509 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11510 && !bfd_link_relocatable (flinfo
->info
))
11513 bfd_vma dot
= (rel
->r_offset
11514 + o
->output_offset
+ o
->output_section
->vma
);
11516 printf ("Encountered a complex symbol!");
11517 printf (" (input_bfd %s, section %s, reloc %ld\n",
11518 bfd_get_filename (input_bfd
), o
->name
,
11519 (long) (rel
- internal_relocs
));
11520 printf (" symbol: idx %8.8lx, name %s\n",
11521 r_symndx
, sym_name
);
11522 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11523 (unsigned long) rel
->r_info
,
11524 (unsigned long) rel
->r_offset
);
11526 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11527 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11530 /* Symbol evaluated OK. Update to absolute value. */
11531 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11536 if (action_discarded
!= -1 && ps
!= NULL
)
11538 /* Complain if the definition comes from a
11539 discarded section. */
11540 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11542 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11543 if (action_discarded
& COMPLAIN
)
11544 (*flinfo
->info
->callbacks
->einfo
)
11545 /* xgettext:c-format */
11546 (_("%X`%s' referenced in section `%pA' of %pB: "
11547 "defined in discarded section `%pA' of %pB\n"),
11548 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11550 /* Try to do the best we can to support buggy old
11551 versions of gcc. Pretend that the symbol is
11552 really defined in the kept linkonce section.
11553 FIXME: This is quite broken. Modifying the
11554 symbol here means we will be changing all later
11555 uses of the symbol, not just in this section. */
11556 if (action_discarded
& PRETEND
)
11560 kept
= _bfd_elf_check_kept_section (sec
,
11572 /* Relocate the section by invoking a back end routine.
11574 The back end routine is responsible for adjusting the
11575 section contents as necessary, and (if using Rela relocs
11576 and generating a relocatable output file) adjusting the
11577 reloc addend as necessary.
11579 The back end routine does not have to worry about setting
11580 the reloc address or the reloc symbol index.
11582 The back end routine is given a pointer to the swapped in
11583 internal symbols, and can access the hash table entries
11584 for the external symbols via elf_sym_hashes (input_bfd).
11586 When generating relocatable output, the back end routine
11587 must handle STB_LOCAL/STT_SECTION symbols specially. The
11588 output symbol is going to be a section symbol
11589 corresponding to the output section, which will require
11590 the addend to be adjusted. */
11592 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11593 input_bfd
, o
, contents
,
11601 || bfd_link_relocatable (flinfo
->info
)
11602 || flinfo
->info
->emitrelocations
)
11604 Elf_Internal_Rela
*irela
;
11605 Elf_Internal_Rela
*irelaend
, *irelamid
;
11606 bfd_vma last_offset
;
11607 struct elf_link_hash_entry
**rel_hash
;
11608 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11609 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11610 unsigned int next_erel
;
11612 struct bfd_elf_section_data
*esdi
, *esdo
;
11614 esdi
= elf_section_data (o
);
11615 esdo
= elf_section_data (o
->output_section
);
11616 rela_normal
= false;
11618 /* Adjust the reloc addresses and symbol indices. */
11620 irela
= internal_relocs
;
11621 irelaend
= irela
+ o
->reloc_count
;
11622 rel_hash
= PTR_ADD (esdo
->rel
.hashes
, esdo
->rel
.count
);
11623 /* We start processing the REL relocs, if any. When we reach
11624 IRELAMID in the loop, we switch to the RELA relocs. */
11626 if (esdi
->rel
.hdr
!= NULL
)
11627 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11628 * bed
->s
->int_rels_per_ext_rel
);
11629 rel_hash_list
= rel_hash
;
11630 rela_hash_list
= NULL
;
11631 last_offset
= o
->output_offset
;
11632 if (!bfd_link_relocatable (flinfo
->info
))
11633 last_offset
+= o
->output_section
->vma
;
11634 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11636 unsigned long r_symndx
;
11638 Elf_Internal_Sym sym
;
11640 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11646 if (irela
== irelamid
)
11648 rel_hash
= PTR_ADD (esdo
->rela
.hashes
, esdo
->rela
.count
);
11649 rela_hash_list
= rel_hash
;
11650 rela_normal
= bed
->rela_normal
;
11653 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11656 if (irela
->r_offset
>= (bfd_vma
) -2)
11658 /* This is a reloc for a deleted entry or somesuch.
11659 Turn it into an R_*_NONE reloc, at the same
11660 offset as the last reloc. elf_eh_frame.c and
11661 bfd_elf_discard_info rely on reloc offsets
11663 irela
->r_offset
= last_offset
;
11665 irela
->r_addend
= 0;
11669 irela
->r_offset
+= o
->output_offset
;
11671 /* Relocs in an executable have to be virtual addresses. */
11672 if (!bfd_link_relocatable (flinfo
->info
))
11673 irela
->r_offset
+= o
->output_section
->vma
;
11675 last_offset
= irela
->r_offset
;
11677 r_symndx
= irela
->r_info
>> r_sym_shift
;
11678 if (r_symndx
== STN_UNDEF
)
11681 if (r_symndx
>= locsymcount
11682 || (elf_bad_symtab (input_bfd
)
11683 && flinfo
->sections
[r_symndx
] == NULL
))
11685 struct elf_link_hash_entry
*rh
;
11686 unsigned long indx
;
11688 /* This is a reloc against a global symbol. We
11689 have not yet output all the local symbols, so
11690 we do not know the symbol index of any global
11691 symbol. We set the rel_hash entry for this
11692 reloc to point to the global hash table entry
11693 for this symbol. The symbol index is then
11694 set at the end of bfd_elf_final_link. */
11695 indx
= r_symndx
- extsymoff
;
11696 rh
= elf_sym_hashes (input_bfd
)[indx
];
11697 while (rh
->root
.type
== bfd_link_hash_indirect
11698 || rh
->root
.type
== bfd_link_hash_warning
)
11699 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11701 /* Setting the index to -2 tells
11702 elf_link_output_extsym that this symbol is
11703 used by a reloc. */
11704 BFD_ASSERT (rh
->indx
< 0);
11711 /* This is a reloc against a local symbol. */
11714 sym
= isymbuf
[r_symndx
];
11715 sec
= flinfo
->sections
[r_symndx
];
11716 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11718 /* I suppose the backend ought to fill in the
11719 section of any STT_SECTION symbol against a
11720 processor specific section. */
11721 r_symndx
= STN_UNDEF
;
11722 if (bfd_is_abs_section (sec
))
11724 else if (sec
== NULL
|| sec
->owner
== NULL
)
11726 bfd_set_error (bfd_error_bad_value
);
11731 asection
*osec
= sec
->output_section
;
11733 /* If we have discarded a section, the output
11734 section will be the absolute section. In
11735 case of discarded SEC_MERGE sections, use
11736 the kept section. relocate_section should
11737 have already handled discarded linkonce
11739 if (bfd_is_abs_section (osec
)
11740 && sec
->kept_section
!= NULL
11741 && sec
->kept_section
->output_section
!= NULL
)
11743 osec
= sec
->kept_section
->output_section
;
11744 irela
->r_addend
-= osec
->vma
;
11747 if (!bfd_is_abs_section (osec
))
11749 r_symndx
= osec
->target_index
;
11750 if (r_symndx
== STN_UNDEF
)
11752 irela
->r_addend
+= osec
->vma
;
11753 osec
= _bfd_nearby_section (output_bfd
, osec
,
11755 irela
->r_addend
-= osec
->vma
;
11756 r_symndx
= osec
->target_index
;
11761 /* Adjust the addend according to where the
11762 section winds up in the output section. */
11764 irela
->r_addend
+= sec
->output_offset
;
11768 if (flinfo
->indices
[r_symndx
] == -1)
11770 unsigned long shlink
;
11775 if (flinfo
->info
->strip
== strip_all
)
11777 /* You can't do ld -r -s. */
11778 bfd_set_error (bfd_error_invalid_operation
);
11782 /* This symbol was skipped earlier, but
11783 since it is needed by a reloc, we
11784 must output it now. */
11785 shlink
= symtab_hdr
->sh_link
;
11786 name
= (bfd_elf_string_from_elf_section
11787 (input_bfd
, shlink
, sym
.st_name
));
11791 osec
= sec
->output_section
;
11793 _bfd_elf_section_from_bfd_section (output_bfd
,
11795 if (sym
.st_shndx
== SHN_BAD
)
11798 sym
.st_value
+= sec
->output_offset
;
11799 if (!bfd_link_relocatable (flinfo
->info
))
11801 sym
.st_value
+= osec
->vma
;
11802 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11804 struct elf_link_hash_table
*htab
11805 = elf_hash_table (flinfo
->info
);
11807 /* STT_TLS symbols are relative to PT_TLS
11809 if (htab
->tls_sec
!= NULL
)
11810 sym
.st_value
-= htab
->tls_sec
->vma
;
11813 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11818 indx
= bfd_get_symcount (output_bfd
);
11819 ret
= elf_link_output_symstrtab (flinfo
, name
,
11825 flinfo
->indices
[r_symndx
] = indx
;
11830 r_symndx
= flinfo
->indices
[r_symndx
];
11833 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11834 | (irela
->r_info
& r_type_mask
));
11837 /* Swap out the relocs. */
11838 input_rel_hdr
= esdi
->rel
.hdr
;
11839 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11841 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11846 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11847 * bed
->s
->int_rels_per_ext_rel
);
11848 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11851 input_rela_hdr
= esdi
->rela
.hdr
;
11852 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11854 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11863 /* Write out the modified section contents. */
11864 if (bed
->elf_backend_write_section
11865 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11868 /* Section written out. */
11870 else switch (o
->sec_info_type
)
11872 case SEC_INFO_TYPE_STABS
:
11873 if (! (_bfd_write_section_stabs
11875 &elf_hash_table (flinfo
->info
)->stab_info
,
11876 o
, &elf_section_data (o
)->sec_info
, contents
)))
11879 case SEC_INFO_TYPE_MERGE
:
11880 if (! _bfd_write_merged_section (output_bfd
, o
,
11881 elf_section_data (o
)->sec_info
))
11884 case SEC_INFO_TYPE_EH_FRAME
:
11886 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11891 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11893 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11899 case SEC_INFO_TYPE_SFRAME
:
11901 /* Merge .sframe sections into the ctf frame encoder
11902 context of the output_bfd's section. The final .sframe
11903 output section will be written out later. */
11904 if (!_bfd_elf_merge_section_sframe (output_bfd
, flinfo
->info
,
11911 if (! (o
->flags
& SEC_EXCLUDE
))
11913 file_ptr offset
= (file_ptr
) o
->output_offset
;
11914 bfd_size_type todo
= o
->size
;
11916 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11918 if ((o
->flags
& SEC_ELF_REVERSE_COPY
)
11919 && o
->size
> address_size
)
11921 /* Reverse-copy input section to output. */
11923 if ((o
->size
& (address_size
- 1)) != 0
11924 || (o
->reloc_count
!= 0
11925 && (o
->size
* bed
->s
->int_rels_per_ext_rel
11926 != o
->reloc_count
* address_size
)))
11929 /* xgettext:c-format */
11930 (_("error: %pB: size of section %pA is not "
11931 "multiple of address size"),
11933 bfd_set_error (bfd_error_bad_value
);
11939 todo
-= address_size
;
11940 if (! bfd_set_section_contents (output_bfd
,
11948 offset
+= address_size
;
11952 else if (! bfd_set_section_contents (output_bfd
,
11966 /* Generate a reloc when linking an ELF file. This is a reloc
11967 requested by the linker, and does not come from any input file. This
11968 is used to build constructor and destructor tables when linking
11972 elf_reloc_link_order (bfd
*output_bfd
,
11973 struct bfd_link_info
*info
,
11974 asection
*output_section
,
11975 struct bfd_link_order
*link_order
)
11977 reloc_howto_type
*howto
;
11981 struct bfd_elf_section_reloc_data
*reldata
;
11982 struct elf_link_hash_entry
**rel_hash_ptr
;
11983 Elf_Internal_Shdr
*rel_hdr
;
11984 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11985 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11988 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11990 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11993 bfd_set_error (bfd_error_bad_value
);
11997 addend
= link_order
->u
.reloc
.p
->addend
;
12000 reldata
= &esdo
->rel
;
12001 else if (esdo
->rela
.hdr
)
12002 reldata
= &esdo
->rela
;
12009 /* Figure out the symbol index. */
12010 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
12011 if (link_order
->type
== bfd_section_reloc_link_order
)
12013 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
12014 BFD_ASSERT (indx
!= 0);
12015 *rel_hash_ptr
= NULL
;
12019 struct elf_link_hash_entry
*h
;
12021 /* Treat a reloc against a defined symbol as though it were
12022 actually against the section. */
12023 h
= ((struct elf_link_hash_entry
*)
12024 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
12025 link_order
->u
.reloc
.p
->u
.name
,
12026 false, false, true));
12028 && (h
->root
.type
== bfd_link_hash_defined
12029 || h
->root
.type
== bfd_link_hash_defweak
))
12033 section
= h
->root
.u
.def
.section
;
12034 indx
= section
->output_section
->target_index
;
12035 *rel_hash_ptr
= NULL
;
12036 /* It seems that we ought to add the symbol value to the
12037 addend here, but in practice it has already been added
12038 because it was passed to constructor_callback. */
12039 addend
+= section
->output_section
->vma
+ section
->output_offset
;
12041 else if (h
!= NULL
)
12043 /* Setting the index to -2 tells elf_link_output_extsym that
12044 this symbol is used by a reloc. */
12051 (*info
->callbacks
->unattached_reloc
)
12052 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
12057 /* If this is an inplace reloc, we must write the addend into the
12059 if (howto
->partial_inplace
&& addend
!= 0)
12061 bfd_size_type size
;
12062 bfd_reloc_status_type rstat
;
12065 const char *sym_name
;
12066 bfd_size_type octets
;
12068 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
12069 buf
= (bfd_byte
*) bfd_zmalloc (size
);
12070 if (buf
== NULL
&& size
!= 0)
12072 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
12079 case bfd_reloc_outofrange
:
12082 case bfd_reloc_overflow
:
12083 if (link_order
->type
== bfd_section_reloc_link_order
)
12084 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
12086 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
12087 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
12088 howto
->name
, addend
, NULL
, NULL
,
12093 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
12095 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
12102 /* The address of a reloc is relative to the section in a
12103 relocatable file, and is a virtual address in an executable
12105 offset
= link_order
->offset
;
12106 if (! bfd_link_relocatable (info
))
12107 offset
+= output_section
->vma
;
12109 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
12111 irel
[i
].r_offset
= offset
;
12112 irel
[i
].r_info
= 0;
12113 irel
[i
].r_addend
= 0;
12115 if (bed
->s
->arch_size
== 32)
12116 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
12118 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
12120 rel_hdr
= reldata
->hdr
;
12121 erel
= rel_hdr
->contents
;
12122 if (rel_hdr
->sh_type
== SHT_REL
)
12124 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
12125 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
12129 irel
[0].r_addend
= addend
;
12130 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
12131 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
12139 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12140 Returns TRUE upon success, FALSE otherwise. */
12143 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
12147 const struct elf_backend_data
*bed
;
12149 enum bfd_architecture arch
;
12151 asymbol
**sympp
= NULL
;
12155 elf_symbol_type
*osymbuf
;
12158 implib_bfd
= info
->out_implib_bfd
;
12159 bed
= get_elf_backend_data (abfd
);
12161 if (!bfd_set_format (implib_bfd
, bfd_object
))
12164 /* Use flag from executable but make it a relocatable object. */
12165 flags
= bfd_get_file_flags (abfd
);
12166 flags
&= ~HAS_RELOC
;
12167 if (!bfd_set_start_address (implib_bfd
, 0)
12168 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
12171 /* Copy architecture of output file to import library file. */
12172 arch
= bfd_get_arch (abfd
);
12173 mach
= bfd_get_mach (abfd
);
12174 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
12175 && (abfd
->target_defaulted
12176 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
12179 /* Get symbol table size. */
12180 symsize
= bfd_get_symtab_upper_bound (abfd
);
12184 /* Read in the symbol table. */
12185 sympp
= (asymbol
**) bfd_malloc (symsize
);
12189 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
12193 /* Allow the BFD backend to copy any private header data it
12194 understands from the output BFD to the import library BFD. */
12195 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
12198 /* Filter symbols to appear in the import library. */
12199 if (bed
->elf_backend_filter_implib_symbols
)
12200 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
12203 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
12206 bfd_set_error (bfd_error_no_symbols
);
12207 _bfd_error_handler (_("%pB: no symbol found for import library"),
12213 /* Make symbols absolute. */
12214 amt
= symcount
* sizeof (*osymbuf
);
12215 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
12216 if (osymbuf
== NULL
)
12219 for (src_count
= 0; src_count
< symcount
; src_count
++)
12221 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
12222 sizeof (*osymbuf
));
12223 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
12224 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
12225 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
12226 osymbuf
[src_count
].internal_elf_sym
.st_value
=
12227 osymbuf
[src_count
].symbol
.value
;
12228 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
12231 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
12233 /* Allow the BFD backend to copy any private data it understands
12234 from the output BFD to the import library BFD. This is done last
12235 to permit the routine to look at the filtered symbol table. */
12236 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
12239 if (!bfd_close (implib_bfd
))
12250 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
12254 if (flinfo
->symstrtab
!= NULL
)
12255 _bfd_elf_strtab_free (flinfo
->symstrtab
);
12256 free (flinfo
->contents
);
12257 free (flinfo
->external_relocs
);
12258 free (flinfo
->internal_relocs
);
12259 free (flinfo
->external_syms
);
12260 free (flinfo
->locsym_shndx
);
12261 free (flinfo
->internal_syms
);
12262 free (flinfo
->indices
);
12263 free (flinfo
->sections
);
12264 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12265 free (flinfo
->symshndxbuf
);
12266 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12268 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12269 free (esdo
->rel
.hashes
);
12270 free (esdo
->rela
.hashes
);
12274 /* Do the final step of an ELF link. */
12277 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12282 struct elf_final_link_info flinfo
;
12284 struct bfd_link_order
*p
;
12286 bfd_size_type max_contents_size
;
12287 bfd_size_type max_external_reloc_size
;
12288 bfd_size_type max_internal_reloc_count
;
12289 bfd_size_type max_sym_count
;
12290 bfd_size_type max_sym_shndx_count
;
12291 Elf_Internal_Sym elfsym
;
12293 Elf_Internal_Shdr
*symtab_hdr
;
12294 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12295 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12296 struct elf_outext_info eoinfo
;
12298 size_t relativecount
;
12299 size_t relr_entsize
;
12300 asection
*reldyn
= 0;
12302 asection
*attr_section
= NULL
;
12303 bfd_vma attr_size
= 0;
12304 const char *std_attrs_section
;
12305 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12306 bool sections_removed
;
12309 if (!is_elf_hash_table (&htab
->root
))
12312 if (bfd_link_pic (info
))
12313 abfd
->flags
|= DYNAMIC
;
12315 dynamic
= htab
->dynamic_sections_created
;
12316 dynobj
= htab
->dynobj
;
12318 emit_relocs
= (bfd_link_relocatable (info
)
12319 || info
->emitrelocations
);
12321 memset (&flinfo
, 0, sizeof (flinfo
));
12322 flinfo
.info
= info
;
12323 flinfo
.output_bfd
= abfd
;
12324 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12325 if (flinfo
.symstrtab
== NULL
)
12330 flinfo
.hash_sec
= NULL
;
12331 flinfo
.symver_sec
= NULL
;
12335 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12336 /* Note that dynsym_sec can be NULL (on VMS). */
12337 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12338 /* Note that it is OK if symver_sec is NULL. */
12341 if (info
->unique_symbol
12342 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12343 local_hash_newfunc
,
12344 sizeof (struct local_hash_entry
)))
12347 /* The object attributes have been merged. Remove the input
12348 sections from the link, and set the contents of the output
12350 sections_removed
= false;
12351 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12352 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12354 bool remove_section
= false;
12356 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12357 || strcmp (o
->name
, ".gnu.attributes") == 0)
12359 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12361 asection
*input_section
;
12363 if (p
->type
!= bfd_indirect_link_order
)
12365 input_section
= p
->u
.indirect
.section
;
12366 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12367 elf_link_input_bfd ignores this section. */
12368 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12371 attr_size
= bfd_elf_obj_attr_size (abfd
);
12372 bfd_set_section_size (o
, attr_size
);
12373 /* Skip this section later on. */
12374 o
->map_head
.link_order
= NULL
;
12378 remove_section
= true;
12380 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12382 /* Remove empty group section from linker output. */
12383 remove_section
= true;
12385 if (remove_section
)
12387 o
->flags
|= SEC_EXCLUDE
;
12388 bfd_section_list_remove (abfd
, o
);
12389 abfd
->section_count
--;
12390 sections_removed
= true;
12393 if (sections_removed
)
12394 _bfd_fix_excluded_sec_syms (abfd
, info
);
12396 /* Count up the number of relocations we will output for each output
12397 section, so that we know the sizes of the reloc sections. We
12398 also figure out some maximum sizes. */
12399 max_contents_size
= 0;
12400 max_external_reloc_size
= 0;
12401 max_internal_reloc_count
= 0;
12403 max_sym_shndx_count
= 0;
12405 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12407 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12408 o
->reloc_count
= 0;
12410 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12412 unsigned int reloc_count
= 0;
12413 unsigned int additional_reloc_count
= 0;
12414 struct bfd_elf_section_data
*esdi
= NULL
;
12416 if (p
->type
== bfd_section_reloc_link_order
12417 || p
->type
== bfd_symbol_reloc_link_order
)
12419 else if (p
->type
== bfd_indirect_link_order
)
12423 sec
= p
->u
.indirect
.section
;
12425 /* Mark all sections which are to be included in the
12426 link. This will normally be every section. We need
12427 to do this so that we can identify any sections which
12428 the linker has decided to not include. */
12429 sec
->linker_mark
= true;
12431 if (sec
->flags
& SEC_MERGE
)
12434 if (sec
->rawsize
> max_contents_size
)
12435 max_contents_size
= sec
->rawsize
;
12436 if (sec
->size
> max_contents_size
)
12437 max_contents_size
= sec
->size
;
12439 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12440 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12444 /* We are interested in just local symbols, not all
12446 if (elf_bad_symtab (sec
->owner
))
12447 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12448 / bed
->s
->sizeof_sym
);
12450 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12452 if (sym_count
> max_sym_count
)
12453 max_sym_count
= sym_count
;
12455 if (sym_count
> max_sym_shndx_count
12456 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12457 max_sym_shndx_count
= sym_count
;
12459 esdi
= elf_section_data (sec
);
12461 if (esdi
->this_hdr
.sh_type
== SHT_REL
12462 || esdi
->this_hdr
.sh_type
== SHT_RELA
)
12463 /* Some backends use reloc_count in relocation sections
12464 to count particular types of relocs. Of course,
12465 reloc sections themselves can't have relocations. */
12467 else if (emit_relocs
)
12469 reloc_count
= sec
->reloc_count
;
12470 if (bed
->elf_backend_count_additional_relocs
)
12473 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12474 additional_reloc_count
+= c
;
12477 else if (bed
->elf_backend_count_relocs
)
12478 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12480 if ((sec
->flags
& SEC_RELOC
) != 0)
12482 size_t ext_size
= 0;
12484 if (esdi
->rel
.hdr
!= NULL
)
12485 ext_size
= esdi
->rel
.hdr
->sh_size
;
12486 if (esdi
->rela
.hdr
!= NULL
)
12487 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12489 if (ext_size
> max_external_reloc_size
)
12490 max_external_reloc_size
= ext_size
;
12491 if (sec
->reloc_count
> max_internal_reloc_count
)
12492 max_internal_reloc_count
= sec
->reloc_count
;
12497 if (reloc_count
== 0)
12500 reloc_count
+= additional_reloc_count
;
12501 o
->reloc_count
+= reloc_count
;
12503 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12507 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12508 esdo
->rel
.count
+= additional_reloc_count
;
12510 if (esdi
->rela
.hdr
)
12512 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12513 esdo
->rela
.count
+= additional_reloc_count
;
12519 esdo
->rela
.count
+= reloc_count
;
12521 esdo
->rel
.count
+= reloc_count
;
12525 if (o
->reloc_count
> 0)
12526 o
->flags
|= SEC_RELOC
;
12529 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12530 set it (this is probably a bug) and if it is set
12531 assign_section_numbers will create a reloc section. */
12532 o
->flags
&=~ SEC_RELOC
;
12535 /* If the SEC_ALLOC flag is not set, force the section VMA to
12536 zero. This is done in elf_fake_sections as well, but forcing
12537 the VMA to 0 here will ensure that relocs against these
12538 sections are handled correctly. */
12539 if ((o
->flags
& SEC_ALLOC
) == 0
12540 && ! o
->user_set_vma
)
12544 if (! bfd_link_relocatable (info
) && merged
)
12545 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12547 /* Figure out the file positions for everything but the symbol table
12548 and the relocs. We set symcount to force assign_section_numbers
12549 to create a symbol table. */
12550 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12551 BFD_ASSERT (! abfd
->output_has_begun
);
12552 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12555 /* Set sizes, and assign file positions for reloc sections. */
12556 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12558 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12559 if ((o
->flags
& SEC_RELOC
) != 0)
12562 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12566 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12570 /* _bfd_elf_compute_section_file_positions makes temporary use
12571 of target_index. Reset it. */
12572 o
->target_index
= 0;
12574 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12575 to count upwards while actually outputting the relocations. */
12576 esdo
->rel
.count
= 0;
12577 esdo
->rela
.count
= 0;
12579 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12580 && !bfd_section_is_ctf (o
))
12582 /* Cache the section contents so that they can be compressed
12583 later. Use bfd_malloc since it will be freed by
12584 bfd_compress_section_contents. */
12585 unsigned char *contents
= esdo
->this_hdr
.contents
;
12586 if (contents
!= NULL
)
12589 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12590 if (contents
== NULL
)
12592 esdo
->this_hdr
.contents
= contents
;
12596 /* We have now assigned file positions for all the sections except .symtab,
12597 .strtab, and non-loaded reloc and compressed debugging sections. We start
12598 the .symtab section at the current file position, and write directly to it.
12599 We build the .strtab section in memory. */
12600 abfd
->symcount
= 0;
12601 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12602 /* sh_name is set in prep_headers. */
12603 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12604 /* sh_flags, sh_addr and sh_size all start off zero. */
12605 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12606 /* sh_link is set in assign_section_numbers. */
12607 /* sh_info is set below. */
12608 /* sh_offset is set just below. */
12609 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12611 if (max_sym_count
< 20)
12612 max_sym_count
= 20;
12613 htab
->strtabsize
= max_sym_count
;
12614 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12615 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12616 if (htab
->strtab
== NULL
)
12618 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12620 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12621 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12623 if (info
->strip
!= strip_all
|| emit_relocs
)
12625 file_ptr off
= elf_next_file_pos (abfd
);
12627 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
12629 /* Note that at this point elf_next_file_pos (abfd) is
12630 incorrect. We do not yet know the size of the .symtab section.
12631 We correct next_file_pos below, after we do know the size. */
12633 /* Start writing out the symbol table. The first symbol is always a
12635 elfsym
.st_value
= 0;
12636 elfsym
.st_size
= 0;
12637 elfsym
.st_info
= 0;
12638 elfsym
.st_other
= 0;
12639 elfsym
.st_shndx
= SHN_UNDEF
;
12640 elfsym
.st_target_internal
= 0;
12641 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12642 bfd_und_section_ptr
, NULL
) != 1)
12645 /* Output a symbol for each section if asked or they are used for
12646 relocs. These symbols usually have no names. We store the
12647 index of each one in the index field of the section, so that
12648 we can find it again when outputting relocs. */
12650 if (bfd_keep_unused_section_symbols (abfd
) || emit_relocs
)
12652 bool name_local_sections
12653 = (bed
->elf_backend_name_local_section_symbols
12654 && bed
->elf_backend_name_local_section_symbols (abfd
));
12655 const char *name
= NULL
;
12657 elfsym
.st_size
= 0;
12658 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12659 elfsym
.st_other
= 0;
12660 elfsym
.st_value
= 0;
12661 elfsym
.st_target_internal
= 0;
12662 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12664 o
= bfd_section_from_elf_index (abfd
, i
);
12667 o
->target_index
= bfd_get_symcount (abfd
);
12668 elfsym
.st_shndx
= i
;
12669 if (!bfd_link_relocatable (info
))
12670 elfsym
.st_value
= o
->vma
;
12671 if (name_local_sections
)
12673 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12681 /* On some targets like Irix 5 the symbol split between local and global
12682 ones recorded in the sh_info field needs to be done between section
12683 and all other symbols. */
12684 if (bed
->elf_backend_elfsym_local_is_section
12685 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12686 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12688 /* Allocate some memory to hold information read in from the input
12690 if (max_contents_size
!= 0)
12692 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12693 if (flinfo
.contents
== NULL
)
12697 if (max_external_reloc_size
!= 0)
12699 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12700 if (flinfo
.external_relocs
== NULL
)
12704 if (max_internal_reloc_count
!= 0)
12706 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12707 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12708 if (flinfo
.internal_relocs
== NULL
)
12712 if (max_sym_count
!= 0)
12714 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12715 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12716 if (flinfo
.external_syms
== NULL
)
12719 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12720 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12721 if (flinfo
.internal_syms
== NULL
)
12724 amt
= max_sym_count
* sizeof (long);
12725 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12726 if (flinfo
.indices
== NULL
)
12729 amt
= max_sym_count
* sizeof (asection
*);
12730 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12731 if (flinfo
.sections
== NULL
)
12735 if (max_sym_shndx_count
!= 0)
12737 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12738 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12739 if (flinfo
.locsym_shndx
== NULL
)
12745 bfd_vma base
, end
= 0; /* Both bytes. */
12748 for (sec
= htab
->tls_sec
;
12749 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12752 bfd_size_type size
= sec
->size
;
12753 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12756 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12758 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12761 size
= ord
->offset
* opb
+ ord
->size
;
12763 end
= sec
->vma
+ size
/ opb
;
12765 base
= htab
->tls_sec
->vma
;
12766 /* Only align end of TLS section if static TLS doesn't have special
12767 alignment requirements. */
12768 if (bed
->static_tls_alignment
== 1)
12769 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12770 htab
->tls_size
= end
- base
;
12773 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12776 /* Finish relative relocations here after regular symbol processing
12777 is finished if DT_RELR is enabled. */
12778 if (info
->enable_dt_relr
12779 && bed
->finish_relative_relocs
12780 && !bed
->finish_relative_relocs (info
))
12781 info
->callbacks
->einfo
12782 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd
);
12784 /* Since ELF permits relocations to be against local symbols, we
12785 must have the local symbols available when we do the relocations.
12786 Since we would rather only read the local symbols once, and we
12787 would rather not keep them in memory, we handle all the
12788 relocations for a single input file at the same time.
12790 Unfortunately, there is no way to know the total number of local
12791 symbols until we have seen all of them, and the local symbol
12792 indices precede the global symbol indices. This means that when
12793 we are generating relocatable output, and we see a reloc against
12794 a global symbol, we can not know the symbol index until we have
12795 finished examining all the local symbols to see which ones we are
12796 going to output. To deal with this, we keep the relocations in
12797 memory, and don't output them until the end of the link. This is
12798 an unfortunate waste of memory, but I don't see a good way around
12799 it. Fortunately, it only happens when performing a relocatable
12800 link, which is not the common case. FIXME: If keep_memory is set
12801 we could write the relocs out and then read them again; I don't
12802 know how bad the memory loss will be. */
12804 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12805 sub
->output_has_begun
= false;
12806 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12808 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12810 if (p
->type
== bfd_indirect_link_order
12811 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12812 == bfd_target_elf_flavour
)
12813 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12815 if (! sub
->output_has_begun
)
12817 if (! elf_link_input_bfd (&flinfo
, sub
))
12819 sub
->output_has_begun
= true;
12822 else if (p
->type
== bfd_section_reloc_link_order
12823 || p
->type
== bfd_symbol_reloc_link_order
)
12825 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12830 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12832 if (p
->type
== bfd_indirect_link_order
12833 && (bfd_get_flavour (sub
)
12834 == bfd_target_elf_flavour
)
12835 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12836 != bed
->s
->elfclass
))
12838 const char *iclass
, *oclass
;
12840 switch (bed
->s
->elfclass
)
12842 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12843 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12844 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12848 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12850 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12851 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12852 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12856 bfd_set_error (bfd_error_wrong_format
);
12858 /* xgettext:c-format */
12859 (_("%pB: file class %s incompatible with %s"),
12860 sub
, iclass
, oclass
);
12869 /* Free symbol buffer if needed. */
12870 if (!info
->reduce_memory_overheads
)
12872 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12873 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
12875 free (elf_tdata (sub
)->symbuf
);
12876 elf_tdata (sub
)->symbuf
= NULL
;
12882 /* Output any global symbols that got converted to local in a
12883 version script or due to symbol visibility. We do this in a
12884 separate step since ELF requires all local symbols to appear
12885 prior to any global symbols. FIXME: We should only do this if
12886 some global symbols were, in fact, converted to become local.
12887 FIXME: Will this work correctly with the Irix 5 linker? */
12888 eoinfo
.failed
= false;
12889 eoinfo
.flinfo
= &flinfo
;
12890 eoinfo
.localsyms
= true;
12891 eoinfo
.file_sym_done
= false;
12892 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12896 goto return_local_hash_table
;
12899 /* If backend needs to output some local symbols not present in the hash
12900 table, do it now. */
12901 if (bed
->elf_backend_output_arch_local_syms
)
12903 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12904 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
12907 goto return_local_hash_table
;
12911 /* That wrote out all the local symbols. Finish up the symbol table
12912 with the global symbols. Even if we want to strip everything we
12913 can, we still need to deal with those global symbols that got
12914 converted to local in a version script. */
12916 /* The sh_info field records the index of the first non local symbol. */
12917 if (!symtab_hdr
->sh_info
)
12918 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12921 && htab
->dynsym
!= NULL
12922 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12924 Elf_Internal_Sym sym
;
12925 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12927 o
= htab
->dynsym
->output_section
;
12928 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12930 /* Write out the section symbols for the output sections. */
12931 if (bfd_link_pic (info
)
12932 || htab
->is_relocatable_executable
)
12938 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12940 sym
.st_target_internal
= 0;
12942 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12948 dynindx
= elf_section_data (s
)->dynindx
;
12951 indx
= elf_section_data (s
)->this_idx
;
12952 BFD_ASSERT (indx
> 0);
12953 sym
.st_shndx
= indx
;
12954 if (! check_dynsym (abfd
, &sym
))
12957 goto return_local_hash_table
;
12959 sym
.st_value
= s
->vma
;
12960 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12962 /* Inform the linker of the addition of this symbol. */
12964 if (info
->callbacks
->ctf_new_dynsym
)
12965 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
12967 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12971 /* Write out the local dynsyms. */
12972 if (htab
->dynlocal
)
12974 struct elf_link_local_dynamic_entry
*e
;
12975 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12980 /* Copy the internal symbol and turn off visibility.
12981 Note that we saved a word of storage and overwrote
12982 the original st_name with the dynstr_index. */
12984 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12985 sym
.st_shndx
= SHN_UNDEF
;
12987 s
= bfd_section_from_elf_index (e
->input_bfd
,
12990 && s
->output_section
!= NULL
12991 && elf_section_data (s
->output_section
) != NULL
)
12994 elf_section_data (s
->output_section
)->this_idx
;
12995 if (! check_dynsym (abfd
, &sym
))
12998 goto return_local_hash_table
;
13000 sym
.st_value
= (s
->output_section
->vma
13002 + e
->isym
.st_value
);
13005 /* Inform the linker of the addition of this symbol. */
13007 if (info
->callbacks
->ctf_new_dynsym
)
13008 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
13010 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
13011 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
13016 /* We get the global symbols from the hash table. */
13017 eoinfo
.failed
= false;
13018 eoinfo
.localsyms
= false;
13019 eoinfo
.flinfo
= &flinfo
;
13020 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
13024 goto return_local_hash_table
;
13027 /* If backend needs to output some symbols not present in the hash
13028 table, do it now. */
13029 if (bed
->elf_backend_output_arch_syms
13030 && (info
->strip
!= strip_all
|| emit_relocs
))
13032 if (! ((*bed
->elf_backend_output_arch_syms
)
13033 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
13036 goto return_local_hash_table
;
13040 /* Finalize the .strtab section. */
13041 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
13043 /* Swap out the .strtab section. */
13044 if (!elf_link_swap_symbols_out (&flinfo
))
13047 goto return_local_hash_table
;
13050 /* Now we know the size of the symtab section. */
13051 if (bfd_get_symcount (abfd
) > 0)
13053 /* Finish up and write out the symbol string table (.strtab)
13055 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
13056 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
13058 if (elf_symtab_shndx_list (abfd
))
13060 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
13062 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
13064 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
13065 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
13066 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
13067 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
13068 symtab_shndx_hdr
->sh_size
= amt
;
13070 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
13073 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
13074 || (bfd_write (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
13077 goto return_local_hash_table
;
13082 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
13083 /* sh_name was set in prep_headers. */
13084 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
13085 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
13086 symstrtab_hdr
->sh_addr
= 0;
13087 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
13088 symstrtab_hdr
->sh_entsize
= 0;
13089 symstrtab_hdr
->sh_link
= 0;
13090 symstrtab_hdr
->sh_info
= 0;
13091 /* sh_offset is set just below. */
13092 symstrtab_hdr
->sh_addralign
= 1;
13094 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
13096 elf_next_file_pos (abfd
) = off
;
13098 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
13099 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
13102 goto return_local_hash_table
;
13106 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
13108 _bfd_error_handler (_("%pB: failed to generate import library"),
13109 info
->out_implib_bfd
);
13111 goto return_local_hash_table
;
13114 /* Adjust the relocs to have the correct symbol indices. */
13115 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
13117 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
13120 if ((o
->flags
& SEC_RELOC
) == 0)
13123 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
13124 if (esdo
->rel
.hdr
!= NULL
13125 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
13128 goto return_local_hash_table
;
13130 if (esdo
->rela
.hdr
!= NULL
13131 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
13134 goto return_local_hash_table
;
13137 /* Set the reloc_count field to 0 to prevent write_relocs from
13138 trying to swap the relocs out itself. */
13139 o
->reloc_count
= 0;
13143 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
13144 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
13147 if (htab
->srelrdyn
!= NULL
13148 && htab
->srelrdyn
->output_section
!= NULL
13149 && htab
->srelrdyn
->size
!= 0)
13151 asection
*s
= htab
->srelrdyn
->output_section
;
13152 relr_entsize
= elf_section_data (s
)->this_hdr
.sh_entsize
;
13153 if (relr_entsize
== 0)
13155 relr_entsize
= bed
->s
->arch_size
/ 8;
13156 elf_section_data (s
)->this_hdr
.sh_entsize
= relr_entsize
;
13160 /* If we are linking against a dynamic object, or generating a
13161 shared library, finish up the dynamic linking information. */
13164 bfd_byte
*dyncon
, *dynconend
;
13166 /* Fix up .dynamic entries. */
13167 o
= bfd_get_linker_section (dynobj
, ".dynamic");
13168 BFD_ASSERT (o
!= NULL
);
13170 dyncon
= o
->contents
;
13171 dynconend
= PTR_ADD (o
->contents
, o
->size
);
13172 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13174 Elf_Internal_Dyn dyn
;
13177 bfd_size_type sh_size
;
13180 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13187 if (relativecount
!= 0)
13189 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
13191 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
13192 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
13194 if (dyn
.d_tag
!= DT_NULL
13195 && dynconend
- dyncon
>= bed
->s
->sizeof_dyn
)
13197 dyn
.d_un
.d_val
= relativecount
;
13203 if (relr_entsize
!= 0)
13205 if (dynconend
- dyncon
>= 3 * bed
->s
->sizeof_dyn
)
13207 asection
*s
= htab
->srelrdyn
;
13208 dyn
.d_tag
= DT_RELR
;
13210 = s
->output_section
->vma
+ s
->output_offset
;
13211 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13212 dyncon
+= bed
->s
->sizeof_dyn
;
13214 dyn
.d_tag
= DT_RELRSZ
;
13215 dyn
.d_un
.d_val
= s
->size
;
13216 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13217 dyncon
+= bed
->s
->sizeof_dyn
;
13219 dyn
.d_tag
= DT_RELRENT
;
13220 dyn
.d_un
.d_val
= relr_entsize
;
13229 name
= info
->init_function
;
13232 name
= info
->fini_function
;
13235 struct elf_link_hash_entry
*h
;
13237 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
13239 && (h
->root
.type
== bfd_link_hash_defined
13240 || h
->root
.type
== bfd_link_hash_defweak
))
13242 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
13243 o
= h
->root
.u
.def
.section
;
13244 if (o
->output_section
!= NULL
)
13245 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
13246 + o
->output_offset
);
13249 /* The symbol is imported from another shared
13250 library and does not apply to this one. */
13251 dyn
.d_un
.d_ptr
= 0;
13258 case DT_PREINIT_ARRAYSZ
:
13259 name
= ".preinit_array";
13261 case DT_INIT_ARRAYSZ
:
13262 name
= ".init_array";
13264 case DT_FINI_ARRAYSZ
:
13265 name
= ".fini_array";
13267 o
= bfd_get_section_by_name (abfd
, name
);
13271 (_("could not find section %s"), name
);
13276 (_("warning: %s section has zero size"), name
);
13277 dyn
.d_un
.d_val
= o
->size
;
13280 case DT_PREINIT_ARRAY
:
13281 name
= ".preinit_array";
13283 case DT_INIT_ARRAY
:
13284 name
= ".init_array";
13286 case DT_FINI_ARRAY
:
13287 name
= ".fini_array";
13289 o
= bfd_get_section_by_name (abfd
, name
);
13296 name
= ".gnu.hash";
13305 name
= ".gnu.version_d";
13308 name
= ".gnu.version_r";
13311 name
= ".gnu.version";
13313 o
= bfd_get_linker_section (dynobj
, name
);
13315 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13318 (_("could not find section %s"), name
);
13321 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13324 (_("warning: section '%s' is being made into a note"), name
);
13325 bfd_set_error (bfd_error_nonrepresentable_section
);
13328 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13335 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13341 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13343 Elf_Internal_Shdr
*hdr
;
13345 hdr
= elf_elfsections (abfd
)[i
];
13346 if (hdr
->sh_type
== type
13347 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13349 sh_size
+= hdr
->sh_size
;
13351 || sh_addr
> hdr
->sh_addr
)
13352 sh_addr
= hdr
->sh_addr
;
13356 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13358 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13360 /* Don't count procedure linkage table relocs in the
13361 overall reloc count. */
13362 sh_size
-= htab
->srelplt
->size
;
13364 /* If the size is zero, make the address zero too.
13365 This is to avoid a glibc bug. If the backend
13366 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13367 zero, then we'll put DT_RELA at the end of
13368 DT_JMPREL. glibc will interpret the end of
13369 DT_RELA matching the end of DT_JMPREL as the
13370 case where DT_RELA includes DT_JMPREL, and for
13371 LD_BIND_NOW will decide that processing DT_RELA
13372 will process the PLT relocs too. Net result:
13373 No PLT relocs applied. */
13376 /* If .rela.plt is the first .rela section, exclude
13377 it from DT_RELA. */
13378 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13379 + htab
->srelplt
->output_offset
) * opb
)
13380 sh_addr
+= htab
->srelplt
->size
;
13383 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13384 dyn
.d_un
.d_val
= sh_size
;
13386 dyn
.d_un
.d_ptr
= sh_addr
;
13389 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13393 /* If we have created any dynamic sections, then output them. */
13394 if (dynobj
!= NULL
)
13396 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13399 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13400 if (bfd_link_textrel_check (info
)
13401 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
13404 bfd_byte
*dyncon
, *dynconend
;
13406 dyncon
= o
->contents
;
13407 dynconend
= o
->contents
+ o
->size
;
13408 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13410 Elf_Internal_Dyn dyn
;
13412 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13414 if (dyn
.d_tag
== DT_TEXTREL
)
13416 if (info
->textrel_check
== textrel_check_error
)
13417 info
->callbacks
->einfo
13418 (_("%P%X: read-only segment has dynamic relocations\n"));
13419 else if (bfd_link_dll (info
))
13420 info
->callbacks
->einfo
13421 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13422 else if (bfd_link_pde (info
))
13423 info
->callbacks
->einfo
13424 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13426 info
->callbacks
->einfo
13427 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13433 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13435 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13437 || o
->output_section
== bfd_abs_section_ptr
)
13439 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13441 /* At this point, we are only interested in sections
13442 created by _bfd_elf_link_create_dynamic_sections. */
13445 if (htab
->stab_info
.stabstr
== o
)
13447 if (htab
->eh_info
.hdr_sec
== o
)
13449 if (strcmp (o
->name
, ".dynstr") != 0)
13451 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13452 * bfd_octets_per_byte (abfd
, o
));
13453 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13454 o
->contents
, octets
, o
->size
))
13459 /* The contents of the .dynstr section are actually in a
13463 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13464 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13465 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13471 if (!info
->resolve_section_groups
)
13473 bool failed
= false;
13475 BFD_ASSERT (bfd_link_relocatable (info
));
13476 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13481 /* If we have optimized stabs strings, output them. */
13482 if (htab
->stab_info
.stabstr
!= NULL
)
13484 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13488 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13491 if (! _bfd_elf_write_section_sframe (abfd
, info
))
13494 if (info
->callbacks
->emit_ctf
)
13495 info
->callbacks
->emit_ctf ();
13497 elf_final_link_free (abfd
, &flinfo
);
13501 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13502 if (contents
== NULL
)
13504 /* Bail out and fail. */
13506 goto return_local_hash_table
;
13508 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13509 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13513 return_local_hash_table
:
13514 if (info
->unique_symbol
)
13515 bfd_hash_table_free (&flinfo
.local_hash_table
);
13519 elf_final_link_free (abfd
, &flinfo
);
13521 goto return_local_hash_table
;
13524 /* Initialize COOKIE for input bfd ABFD. */
13527 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13528 struct bfd_link_info
*info
, bfd
*abfd
)
13530 Elf_Internal_Shdr
*symtab_hdr
;
13531 const struct elf_backend_data
*bed
;
13533 bed
= get_elf_backend_data (abfd
);
13534 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13536 cookie
->abfd
= abfd
;
13537 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13538 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13539 if (cookie
->bad_symtab
)
13541 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13542 cookie
->extsymoff
= 0;
13546 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13547 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13550 if (bed
->s
->arch_size
== 32)
13551 cookie
->r_sym_shift
= 8;
13553 cookie
->r_sym_shift
= 32;
13555 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13556 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13558 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13559 cookie
->locsymcount
, 0,
13561 if (cookie
->locsyms
== NULL
)
13563 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13566 if (_bfd_link_keep_memory (info
) )
13568 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13569 info
->cache_size
+= (cookie
->locsymcount
13570 * sizeof (Elf_External_Sym_Shndx
));
13576 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13579 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13581 Elf_Internal_Shdr
*symtab_hdr
;
13583 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13584 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13585 free (cookie
->locsyms
);
13588 /* Initialize the relocation information in COOKIE for input section SEC
13589 of input bfd ABFD. */
13592 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13593 struct bfd_link_info
*info
, bfd
*abfd
,
13596 if (sec
->reloc_count
== 0)
13598 cookie
->rels
= NULL
;
13599 cookie
->relend
= NULL
;
13603 cookie
->rels
= _bfd_elf_link_info_read_relocs (abfd
, info
, sec
,
13605 _bfd_link_keep_memory (info
));
13606 if (cookie
->rels
== NULL
)
13608 cookie
->rel
= cookie
->rels
;
13609 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13611 cookie
->rel
= cookie
->rels
;
13615 /* Free the memory allocated by init_reloc_cookie_rels,
13619 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13622 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13623 free (cookie
->rels
);
13626 /* Initialize the whole of COOKIE for input section SEC. */
13629 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13630 struct bfd_link_info
*info
,
13633 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13635 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13640 fini_reloc_cookie (cookie
, sec
->owner
);
13645 /* Free the memory allocated by init_reloc_cookie_for_section,
13649 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13652 fini_reloc_cookie_rels (cookie
, sec
);
13653 fini_reloc_cookie (cookie
, sec
->owner
);
13656 /* Garbage collect unused sections. */
13658 /* Default gc_mark_hook. */
13661 _bfd_elf_gc_mark_hook (asection
*sec
,
13662 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13663 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13664 struct elf_link_hash_entry
*h
,
13665 Elf_Internal_Sym
*sym
)
13669 switch (h
->root
.type
)
13671 case bfd_link_hash_defined
:
13672 case bfd_link_hash_defweak
:
13673 return h
->root
.u
.def
.section
;
13675 case bfd_link_hash_common
:
13676 return h
->root
.u
.c
.p
->section
;
13683 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13688 /* Return the debug definition section. */
13691 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13692 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13693 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13694 struct elf_link_hash_entry
*h
,
13695 Elf_Internal_Sym
*sym
)
13699 /* Return the global debug definition section. */
13700 if ((h
->root
.type
== bfd_link_hash_defined
13701 || h
->root
.type
== bfd_link_hash_defweak
)
13702 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13703 return h
->root
.u
.def
.section
;
13707 /* Return the local debug definition section. */
13708 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13710 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
13717 /* COOKIE->rel describes a relocation against section SEC, which is
13718 a section we've decided to keep. Return the section that contains
13719 the relocation symbol, or NULL if no section contains it. */
13722 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13723 elf_gc_mark_hook_fn gc_mark_hook
,
13724 struct elf_reloc_cookie
*cookie
,
13727 unsigned long r_symndx
;
13728 struct elf_link_hash_entry
*h
, *hw
;
13730 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13731 if (r_symndx
== STN_UNDEF
)
13734 if (r_symndx
>= cookie
->locsymcount
13735 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13739 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13742 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13746 while (h
->root
.type
== bfd_link_hash_indirect
13747 || h
->root
.type
== bfd_link_hash_warning
)
13748 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13750 was_marked
= h
->mark
;
13752 /* Keep all aliases of the symbol too. If an object symbol
13753 needs to be copied into .dynbss then all of its aliases
13754 should be present as dynamic symbols, not just the one used
13755 on the copy relocation. */
13757 while (hw
->is_weakalias
)
13763 if (!was_marked
&& h
->start_stop
&& !h
->root
.ldscript_def
)
13765 if (info
->start_stop_gc
)
13768 /* To work around a glibc bug, mark XXX input sections
13769 when there is a reference to __start_XXX or __stop_XXX
13771 else if (start_stop
!= NULL
)
13773 asection
*s
= h
->u2
.start_stop_section
;
13774 *start_stop
= true;
13779 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13782 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13783 &cookie
->locsyms
[r_symndx
]);
13786 /* COOKIE->rel describes a relocation against section SEC, which is
13787 a section we've decided to keep. Mark the section that contains
13788 the relocation symbol. */
13791 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13793 elf_gc_mark_hook_fn gc_mark_hook
,
13794 struct elf_reloc_cookie
*cookie
)
13797 bool start_stop
= false;
13799 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13800 while (rsec
!= NULL
)
13802 if (!rsec
->gc_mark
)
13804 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13805 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13807 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13812 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13817 /* The mark phase of garbage collection. For a given section, mark
13818 it and any sections in this section's group, and all the sections
13819 which define symbols to which it refers. */
13822 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13824 elf_gc_mark_hook_fn gc_mark_hook
)
13827 asection
*group_sec
, *eh_frame
;
13831 /* Mark all the sections in the group. */
13832 group_sec
= elf_section_data (sec
)->next_in_group
;
13833 if (group_sec
&& !group_sec
->gc_mark
)
13834 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13837 /* Look through the section relocs. */
13839 eh_frame
= elf_eh_frame_section (sec
->owner
);
13840 if ((sec
->flags
& SEC_RELOC
) != 0
13841 && sec
->reloc_count
> 0
13842 && sec
!= eh_frame
)
13844 struct elf_reloc_cookie cookie
;
13846 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13850 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13851 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13856 fini_reloc_cookie_for_section (&cookie
, sec
);
13860 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13862 struct elf_reloc_cookie cookie
;
13864 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13868 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13869 gc_mark_hook
, &cookie
))
13871 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13875 eh_frame
= elf_section_eh_frame_entry (sec
);
13876 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13877 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13883 /* Scan and mark sections in a special or debug section group. */
13886 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13888 /* Point to first section of section group. */
13890 /* Used to iterate the section group. */
13893 bool is_special_grp
= true;
13894 bool is_debug_grp
= true;
13896 /* First scan to see if group contains any section other than debug
13897 and special section. */
13898 ssec
= msec
= elf_next_in_group (grp
);
13901 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13902 is_debug_grp
= false;
13904 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13905 is_special_grp
= false;
13907 msec
= elf_next_in_group (msec
);
13909 while (msec
!= ssec
);
13911 /* If this is a pure debug section group or pure special section group,
13912 keep all sections in this group. */
13913 if (is_debug_grp
|| is_special_grp
)
13918 msec
= elf_next_in_group (msec
);
13920 while (msec
!= ssec
);
13924 /* Keep debug and special sections. */
13927 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13928 elf_gc_mark_hook_fn mark_hook
)
13932 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13936 bool debug_frag_seen
;
13937 bool has_kept_debug_info
;
13939 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13941 isec
= ibfd
->sections
;
13942 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13945 /* Ensure all linker created sections are kept,
13946 see if any other section is already marked,
13947 and note if we have any fragmented debug sections. */
13948 debug_frag_seen
= some_kept
= has_kept_debug_info
= false;
13949 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13951 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13953 else if (isec
->gc_mark
13954 && (isec
->flags
& SEC_ALLOC
) != 0
13955 && elf_section_type (isec
) != SHT_NOTE
)
13959 /* Since all sections, except for backend specific ones,
13960 have been garbage collected, call mark_hook on this
13961 section if any of its linked-to sections is marked. */
13962 asection
*linked_to_sec
;
13963 for (linked_to_sec
= elf_linked_to_section (isec
);
13964 linked_to_sec
!= NULL
&& !linked_to_sec
->linker_mark
;
13965 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13967 if (linked_to_sec
->gc_mark
)
13969 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
13973 linked_to_sec
->linker_mark
= 1;
13975 for (linked_to_sec
= elf_linked_to_section (isec
);
13976 linked_to_sec
!= NULL
&& linked_to_sec
->linker_mark
;
13977 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13978 linked_to_sec
->linker_mark
= 0;
13981 if (!debug_frag_seen
13982 && (isec
->flags
& SEC_DEBUGGING
)
13983 && startswith (isec
->name
, ".debug_line."))
13984 debug_frag_seen
= true;
13985 else if (strcmp (bfd_section_name (isec
),
13986 "__patchable_function_entries") == 0
13987 && elf_linked_to_section (isec
) == NULL
)
13988 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
13989 "need linked-to section "
13990 "for --gc-sections\n"),
13991 isec
->owner
, isec
);
13994 /* If no non-note alloc section in this file will be kept, then
13995 we can toss out the debug and special sections. */
13999 /* Keep debug and special sections like .comment when they are
14000 not part of a group. Also keep section groups that contain
14001 just debug sections or special sections. NB: Sections with
14002 linked-to section has been handled above. */
14003 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14005 if ((isec
->flags
& SEC_GROUP
) != 0)
14006 _bfd_elf_gc_mark_debug_special_section_group (isec
);
14007 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
14008 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
14009 && elf_next_in_group (isec
) == NULL
14010 && elf_linked_to_section (isec
) == NULL
)
14012 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
14013 has_kept_debug_info
= true;
14016 /* Look for CODE sections which are going to be discarded,
14017 and find and discard any fragmented debug sections which
14018 are associated with that code section. */
14019 if (debug_frag_seen
)
14020 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14021 if ((isec
->flags
& SEC_CODE
) != 0
14022 && isec
->gc_mark
== 0)
14027 ilen
= strlen (isec
->name
);
14029 /* Association is determined by the name of the debug
14030 section containing the name of the code section as
14031 a suffix. For example .debug_line.text.foo is a
14032 debug section associated with .text.foo. */
14033 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
14037 if (dsec
->gc_mark
== 0
14038 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
14041 dlen
= strlen (dsec
->name
);
14044 && strncmp (dsec
->name
+ (dlen
- ilen
),
14045 isec
->name
, ilen
) == 0)
14050 /* Mark debug sections referenced by kept debug sections. */
14051 if (has_kept_debug_info
)
14052 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14054 && (isec
->flags
& SEC_DEBUGGING
) != 0)
14055 if (!_bfd_elf_gc_mark (info
, isec
,
14056 elf_gc_mark_debug_section
))
14063 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
14066 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14068 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14072 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14073 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
14074 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14077 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14080 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14082 /* When any section in a section group is kept, we keep all
14083 sections in the section group. If the first member of
14084 the section group is excluded, we will also exclude the
14086 if (o
->flags
& SEC_GROUP
)
14088 asection
*first
= elf_next_in_group (o
);
14089 o
->gc_mark
= first
->gc_mark
;
14095 /* Skip sweeping sections already excluded. */
14096 if (o
->flags
& SEC_EXCLUDE
)
14099 /* Since this is early in the link process, it is simple
14100 to remove a section from the output. */
14101 o
->flags
|= SEC_EXCLUDE
;
14103 if (info
->print_gc_sections
&& o
->size
!= 0)
14104 /* xgettext:c-format */
14105 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14113 /* Propagate collected vtable information. This is called through
14114 elf_link_hash_traverse. */
14117 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
14119 /* Those that are not vtables. */
14121 || h
->u2
.vtable
== NULL
14122 || h
->u2
.vtable
->parent
== NULL
)
14125 /* Those vtables that do not have parents, we cannot merge. */
14126 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
14129 /* If we've already been done, exit. */
14130 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
14133 /* Make sure the parent's table is up to date. */
14134 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
14136 if (h
->u2
.vtable
->used
== NULL
)
14138 /* None of this table's entries were referenced. Re-use the
14140 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14141 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
14148 /* Or the parent's entries into ours. */
14149 cu
= h
->u2
.vtable
->used
;
14151 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14154 const struct elf_backend_data
*bed
;
14155 unsigned int log_file_align
;
14157 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
14158 log_file_align
= bed
->s
->log_file_align
;
14159 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
14173 struct link_info_ok
14175 struct bfd_link_info
*info
;
14180 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
,
14184 bfd_vma hstart
, hend
;
14185 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
14186 const struct elf_backend_data
*bed
;
14187 unsigned int log_file_align
;
14188 struct link_info_ok
*info
= (struct link_info_ok
*) ptr
;
14190 /* Take care of both those symbols that do not describe vtables as
14191 well as those that are not loaded. */
14193 || h
->u2
.vtable
== NULL
14194 || h
->u2
.vtable
->parent
== NULL
)
14197 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
14198 || h
->root
.type
== bfd_link_hash_defweak
);
14200 sec
= h
->root
.u
.def
.section
;
14201 hstart
= h
->root
.u
.def
.value
;
14202 hend
= hstart
+ h
->size
;
14204 relstart
= _bfd_elf_link_info_read_relocs (sec
->owner
, info
->info
,
14205 sec
, NULL
, NULL
, true);
14207 return info
->ok
= false;
14208 bed
= get_elf_backend_data (sec
->owner
);
14209 log_file_align
= bed
->s
->log_file_align
;
14211 relend
= relstart
+ sec
->reloc_count
;
14213 for (rel
= relstart
; rel
< relend
; ++rel
)
14214 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
14216 /* If the entry is in use, do nothing. */
14217 if (h
->u2
.vtable
->used
14218 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
14220 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
14221 if (h
->u2
.vtable
->used
[entry
])
14224 /* Otherwise, kill it. */
14225 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
14231 /* Mark sections containing dynamically referenced symbols. When
14232 building shared libraries, we must assume that any visible symbol is
14236 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
14238 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
14239 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
14241 if ((h
->root
.type
== bfd_link_hash_defined
14242 || h
->root
.type
== bfd_link_hash_defweak
)
14244 || h
->root
.ldscript_def
14245 || !info
->start_stop_gc
)
14246 && ((h
->ref_dynamic
&& !h
->forced_local
)
14247 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
14248 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
14249 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
14250 && (!bfd_link_executable (info
)
14251 || info
->gc_keep_exported
14252 || info
->export_dynamic
14255 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
14256 && (h
->versioned
>= versioned
14257 || !bfd_hide_sym_by_version (info
->version_info
,
14258 h
->root
.root
.string
)))))
14259 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14264 /* Keep all sections containing symbols undefined on the command-line,
14265 and the section containing the entry symbol. */
14268 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
14270 struct bfd_sym_chain
*sym
;
14272 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
14274 struct elf_link_hash_entry
*h
;
14276 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
14277 false, false, false);
14280 && (h
->root
.type
== bfd_link_hash_defined
14281 || h
->root
.type
== bfd_link_hash_defweak
)
14282 && !bfd_is_const_section (h
->root
.u
.def
.section
))
14283 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14288 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
14289 struct bfd_link_info
*info
)
14291 bfd
*ibfd
= info
->input_bfds
;
14293 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14296 struct elf_reloc_cookie cookie
;
14298 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14300 sec
= ibfd
->sections
;
14301 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14304 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
14307 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
14309 if (startswith (bfd_section_name (sec
), ".eh_frame_entry")
14310 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
14312 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
14313 fini_reloc_cookie_rels (&cookie
, sec
);
14320 /* Do mark and sweep of unused sections. */
14323 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14327 elf_gc_mark_hook_fn gc_mark_hook
;
14328 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14329 struct elf_link_hash_table
*htab
;
14330 struct link_info_ok info_ok
;
14332 if (!bed
->can_gc_sections
14333 || !is_elf_hash_table (info
->hash
))
14335 _bfd_error_handler(_("warning: gc-sections option ignored"));
14339 bed
->gc_keep (info
);
14340 htab
= elf_hash_table (info
);
14342 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14343 at the .eh_frame section if we can mark the FDEs individually. */
14344 for (sub
= info
->input_bfds
;
14345 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14346 sub
= sub
->link
.next
)
14349 struct elf_reloc_cookie cookie
;
14351 sec
= sub
->sections
;
14352 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14354 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14355 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
14357 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14358 if (elf_section_data (sec
)->sec_info
14359 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14360 elf_eh_frame_section (sub
) = sec
;
14361 fini_reloc_cookie_for_section (&cookie
, sec
);
14362 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14366 /* Apply transitive closure to the vtable entry usage info. */
14367 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14371 /* Kill the vtable relocations that were not used. */
14372 info_ok
.info
= info
;
14374 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &info_ok
);
14378 /* Mark dynamically referenced symbols. */
14379 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14380 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14382 /* Grovel through relocs to find out who stays ... */
14383 gc_mark_hook
= bed
->gc_mark_hook
;
14384 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14388 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14389 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14390 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14394 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14397 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14398 Also treat note sections as a root, if the section is not part
14399 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14400 well as FINI_ARRAY sections for ld -r. */
14401 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14403 && (o
->flags
& SEC_EXCLUDE
) == 0
14404 && ((o
->flags
& SEC_KEEP
) != 0
14405 || (bfd_link_relocatable (info
)
14406 && ((elf_section_data (o
)->this_hdr
.sh_type
14407 == SHT_PREINIT_ARRAY
)
14408 || (elf_section_data (o
)->this_hdr
.sh_type
14410 || (elf_section_data (o
)->this_hdr
.sh_type
14411 == SHT_FINI_ARRAY
)))
14412 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14413 && elf_next_in_group (o
) == NULL
14414 && elf_linked_to_section (o
) == NULL
)
14415 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14416 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14418 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14423 /* Allow the backend to mark additional target specific sections. */
14424 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14426 /* ... and mark SEC_EXCLUDE for those that go. */
14427 return elf_gc_sweep (abfd
, info
);
14430 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14433 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14435 struct elf_link_hash_entry
*h
,
14438 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14439 struct elf_link_hash_entry
**search
, *child
;
14440 size_t extsymcount
;
14441 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14443 /* The sh_info field of the symtab header tells us where the
14444 external symbols start. We don't care about the local symbols at
14446 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14447 if (!elf_bad_symtab (abfd
))
14448 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14450 sym_hashes
= elf_sym_hashes (abfd
);
14451 sym_hashes_end
= PTR_ADD (sym_hashes
, extsymcount
);
14453 /* Hunt down the child symbol, which is in this section at the same
14454 offset as the relocation. */
14455 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14457 if ((child
= *search
) != NULL
14458 && (child
->root
.type
== bfd_link_hash_defined
14459 || child
->root
.type
== bfd_link_hash_defweak
)
14460 && child
->root
.u
.def
.section
== sec
14461 && child
->root
.u
.def
.value
== offset
)
14465 /* xgettext:c-format */
14466 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14467 abfd
, sec
, (uint64_t) offset
);
14468 bfd_set_error (bfd_error_invalid_operation
);
14472 if (!child
->u2
.vtable
)
14474 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14475 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14476 if (!child
->u2
.vtable
)
14481 /* This *should* only be the absolute section. It could potentially
14482 be that someone has defined a non-global vtable though, which
14483 would be bad. It isn't worth paging in the local symbols to be
14484 sure though; that case should simply be handled by the assembler. */
14486 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14489 child
->u2
.vtable
->parent
= h
;
14494 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14497 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14498 struct elf_link_hash_entry
*h
,
14501 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14502 unsigned int log_file_align
= bed
->s
->log_file_align
;
14506 /* xgettext:c-format */
14507 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14509 bfd_set_error (bfd_error_bad_value
);
14515 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14516 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14521 if (addend
>= h
->u2
.vtable
->size
)
14523 size_t size
, bytes
, file_align
;
14524 bool *ptr
= h
->u2
.vtable
->used
;
14526 /* While the symbol is undefined, we have to be prepared to handle
14528 file_align
= 1 << log_file_align
;
14529 if (h
->root
.type
== bfd_link_hash_undefined
)
14530 size
= addend
+ file_align
;
14534 if (addend
>= size
)
14536 /* Oops! We've got a reference past the defined end of
14537 the table. This is probably a bug -- shall we warn? */
14538 size
= addend
+ file_align
;
14541 size
= (size
+ file_align
- 1) & -file_align
;
14543 /* Allocate one extra entry for use as a "done" flag for the
14544 consolidation pass. */
14545 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bool);
14549 ptr
= (bool *) bfd_realloc (ptr
- 1, bytes
);
14555 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14557 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14561 ptr
= (bool *) bfd_zmalloc (bytes
);
14566 /* And arrange for that done flag to be at index -1. */
14567 h
->u2
.vtable
->used
= ptr
+ 1;
14568 h
->u2
.vtable
->size
= size
;
14571 h
->u2
.vtable
->used
[addend
>> log_file_align
] = true;
14576 /* Map an ELF section header flag to its corresponding string. */
14580 flagword flag_value
;
14581 } elf_flags_to_name_table
;
14583 static const elf_flags_to_name_table elf_flags_to_names
[] =
14585 { "SHF_WRITE", SHF_WRITE
},
14586 { "SHF_ALLOC", SHF_ALLOC
},
14587 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14588 { "SHF_MERGE", SHF_MERGE
},
14589 { "SHF_STRINGS", SHF_STRINGS
},
14590 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14591 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14592 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14593 { "SHF_GROUP", SHF_GROUP
},
14594 { "SHF_TLS", SHF_TLS
},
14595 { "SHF_MASKOS", SHF_MASKOS
},
14596 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14599 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14601 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14602 struct flag_info
*flaginfo
,
14605 const bfd_vma sh_flags
= elf_section_flags (section
);
14607 if (!flaginfo
->flags_initialized
)
14609 bfd
*obfd
= info
->output_bfd
;
14610 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14611 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14613 int without_hex
= 0;
14615 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14618 flagword (*lookup
) (char *);
14620 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14621 if (lookup
!= NULL
)
14623 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14627 if (tf
->with
== with_flags
)
14628 with_hex
|= hexval
;
14629 else if (tf
->with
== without_flags
)
14630 without_hex
|= hexval
;
14635 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14637 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14639 if (tf
->with
== with_flags
)
14640 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14641 else if (tf
->with
== without_flags
)
14642 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14649 info
->callbacks
->einfo
14650 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14654 flaginfo
->flags_initialized
= true;
14655 flaginfo
->only_with_flags
|= with_hex
;
14656 flaginfo
->not_with_flags
|= without_hex
;
14659 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14662 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14668 struct alloc_got_off_arg
{
14670 struct bfd_link_info
*info
;
14673 /* We need a special top-level link routine to convert got reference counts
14674 to real got offsets. */
14677 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14679 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14680 bfd
*obfd
= gofarg
->info
->output_bfd
;
14681 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14683 if (h
->got
.refcount
> 0)
14685 h
->got
.offset
= gofarg
->gotoff
;
14686 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14689 h
->got
.offset
= (bfd_vma
) -1;
14694 /* And an accompanying bit to work out final got entry offsets once
14695 we're done. Should be called from final_link. */
14698 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14699 struct bfd_link_info
*info
)
14702 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14704 struct alloc_got_off_arg gofarg
;
14706 BFD_ASSERT (abfd
== info
->output_bfd
);
14708 if (! is_elf_hash_table (info
->hash
))
14711 /* The GOT offset is relative to the .got section, but the GOT header is
14712 put into the .got.plt section, if the backend uses it. */
14713 if (bed
->want_got_plt
)
14716 gotoff
= bed
->got_header_size
;
14718 /* Do the local .got entries first. */
14719 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14721 bfd_signed_vma
*local_got
;
14722 size_t j
, locsymcount
;
14723 Elf_Internal_Shdr
*symtab_hdr
;
14725 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14728 local_got
= elf_local_got_refcounts (i
);
14732 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14733 if (elf_bad_symtab (i
))
14734 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14736 locsymcount
= symtab_hdr
->sh_info
;
14738 for (j
= 0; j
< locsymcount
; ++j
)
14740 if (local_got
[j
] > 0)
14742 local_got
[j
] = gotoff
;
14743 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14746 local_got
[j
] = (bfd_vma
) -1;
14750 /* Then the global .got entries. .plt refcounts are handled by
14751 adjust_dynamic_symbol */
14752 gofarg
.gotoff
= gotoff
;
14753 gofarg
.info
= info
;
14754 elf_link_hash_traverse (elf_hash_table (info
),
14755 elf_gc_allocate_got_offsets
,
14760 /* Many folk need no more in the way of final link than this, once
14761 got entry reference counting is enabled. */
14764 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14766 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14769 /* Invoke the regular ELF backend linker to do all the work. */
14770 return bfd_elf_final_link (abfd
, info
);
14774 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14776 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14778 if (rcookie
->bad_symtab
)
14779 rcookie
->rel
= rcookie
->rels
;
14781 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14783 unsigned long r_symndx
;
14785 if (! rcookie
->bad_symtab
)
14786 if (rcookie
->rel
->r_offset
> offset
)
14788 if (rcookie
->rel
->r_offset
!= offset
)
14791 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14792 if (r_symndx
== STN_UNDEF
)
14795 if (r_symndx
>= rcookie
->locsymcount
14796 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14798 struct elf_link_hash_entry
*h
;
14800 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14802 while (h
->root
.type
== bfd_link_hash_indirect
14803 || h
->root
.type
== bfd_link_hash_warning
)
14804 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14806 if ((h
->root
.type
== bfd_link_hash_defined
14807 || h
->root
.type
== bfd_link_hash_defweak
)
14808 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14809 || h
->root
.u
.def
.section
->kept_section
!= NULL
14810 || discarded_section (h
->root
.u
.def
.section
)))
14815 /* It's not a relocation against a global symbol,
14816 but it could be a relocation against a local
14817 symbol for a discarded section. */
14819 Elf_Internal_Sym
*isym
;
14821 /* Need to: get the symbol; get the section. */
14822 isym
= &rcookie
->locsyms
[r_symndx
];
14823 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14825 && (isec
->kept_section
!= NULL
14826 || discarded_section (isec
)))
14834 /* Discard unneeded references to discarded sections.
14835 Returns -1 on error, 1 if any section's size was changed, 0 if
14836 nothing changed. This function assumes that the relocations are in
14837 sorted order, which is true for all known assemblers. */
14840 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14842 struct elf_reloc_cookie cookie
;
14847 if (info
->traditional_format
14848 || !is_elf_hash_table (info
->hash
))
14851 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14856 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14859 || i
->reloc_count
== 0
14860 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14864 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14867 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14870 if (_bfd_discard_section_stabs (abfd
, i
,
14871 elf_section_data (i
)->sec_info
,
14872 bfd_elf_reloc_symbol_deleted_p
,
14876 fini_reloc_cookie_for_section (&cookie
, i
);
14881 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14882 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14886 int eh_changed
= 0;
14887 unsigned int eh_alignment
; /* Octets. */
14889 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14895 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14898 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14901 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14902 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14903 bfd_elf_reloc_symbol_deleted_p
,
14907 if (i
->size
!= i
->rawsize
)
14911 fini_reloc_cookie_for_section (&cookie
, i
);
14914 eh_alignment
= ((1 << o
->alignment_power
)
14915 * bfd_octets_per_byte (output_bfd
, o
));
14916 /* Skip over zero terminator, and prevent empty sections from
14917 adding alignment padding at the end. */
14918 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14920 i
->flags
|= SEC_EXCLUDE
;
14921 else if (i
->size
> 4)
14923 /* The last non-empty eh_frame section doesn't need padding. */
14926 /* Any prior sections must pad the last FDE out to the output
14927 section alignment. Otherwise we might have zero padding
14928 between sections, which would be seen as a terminator. */
14929 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14931 /* All but the last zero terminator should have been removed. */
14936 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14937 if (i
->size
!= size
)
14945 elf_link_hash_traverse (elf_hash_table (info
),
14946 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14949 o
= bfd_get_section_by_name (output_bfd
, ".sframe");
14954 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14960 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14963 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14966 if (_bfd_elf_parse_sframe (abfd
, info
, i
, &cookie
))
14968 if (_bfd_elf_discard_section_sframe (i
,
14969 bfd_elf_reloc_symbol_deleted_p
,
14972 if (i
->size
!= i
->rawsize
)
14976 fini_reloc_cookie_for_section (&cookie
, i
);
14978 /* Update the reference to the output .sframe section. Used to
14979 determine later if PT_GNU_SFRAME segment is to be generated. */
14980 if (!_bfd_elf_set_section_sframe (output_bfd
, info
))
14984 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14986 const struct elf_backend_data
*bed
;
14989 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14991 s
= abfd
->sections
;
14992 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14995 bed
= get_elf_backend_data (abfd
);
14997 if (bed
->elf_backend_discard_info
!= NULL
)
14999 if (!init_reloc_cookie (&cookie
, info
, abfd
))
15002 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
15005 fini_reloc_cookie (&cookie
, abfd
);
15009 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
15010 _bfd_elf_end_eh_frame_parsing (info
);
15012 if (info
->eh_frame_hdr_type
15013 && !bfd_link_relocatable (info
)
15014 && _bfd_elf_discard_section_eh_frame_hdr (info
))
15021 _bfd_elf_section_already_linked (bfd
*abfd
,
15023 struct bfd_link_info
*info
)
15026 const char *name
, *key
;
15027 struct bfd_section_already_linked
*l
;
15028 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
15030 if (sec
->output_section
== bfd_abs_section_ptr
)
15033 flags
= sec
->flags
;
15035 /* Return if it isn't a linkonce section. A comdat group section
15036 also has SEC_LINK_ONCE set. */
15037 if ((flags
& SEC_LINK_ONCE
) == 0)
15040 /* Don't put group member sections on our list of already linked
15041 sections. They are handled as a group via their group section. */
15042 if (elf_sec_group (sec
) != NULL
)
15045 /* For a SHT_GROUP section, use the group signature as the key. */
15047 if ((flags
& SEC_GROUP
) != 0
15048 && elf_next_in_group (sec
) != NULL
15049 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
15050 key
= elf_group_name (elf_next_in_group (sec
));
15053 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15054 if (startswith (name
, ".gnu.linkonce.")
15055 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
15058 /* Must be a user linkonce section that doesn't follow gcc's
15059 naming convention. In this case we won't be matching
15060 single member groups. */
15064 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
15066 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15068 /* We may have 2 different types of sections on the list: group
15069 sections with a signature of <key> (<key> is some string),
15070 and linkonce sections named .gnu.linkonce.<type>.<key>.
15071 Match like sections. LTO plugin sections are an exception.
15072 They are always named .gnu.linkonce.t.<key> and match either
15073 type of section. */
15074 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
15075 && ((flags
& SEC_GROUP
) != 0
15076 || strcmp (name
, l
->sec
->name
) == 0))
15077 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
15078 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
15080 /* The section has already been linked. See if we should
15081 issue a warning. */
15082 if (!_bfd_handle_already_linked (sec
, l
, info
))
15085 if (flags
& SEC_GROUP
)
15087 asection
*first
= elf_next_in_group (sec
);
15088 asection
*s
= first
;
15092 s
->output_section
= bfd_abs_section_ptr
;
15093 /* Record which group discards it. */
15094 s
->kept_section
= l
->sec
;
15095 s
= elf_next_in_group (s
);
15096 /* These lists are circular. */
15106 /* A single member comdat group section may be discarded by a
15107 linkonce section and vice versa. */
15108 if ((flags
& SEC_GROUP
) != 0)
15110 asection
*first
= elf_next_in_group (sec
);
15112 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
15113 /* Check this single member group against linkonce sections. */
15114 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15115 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15116 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
15118 first
->output_section
= bfd_abs_section_ptr
;
15119 first
->kept_section
= l
->sec
;
15120 sec
->output_section
= bfd_abs_section_ptr
;
15125 /* Check this linkonce section against single member groups. */
15126 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15127 if (l
->sec
->flags
& SEC_GROUP
)
15129 asection
*first
= elf_next_in_group (l
->sec
);
15132 && elf_next_in_group (first
) == first
15133 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
15135 sec
->output_section
= bfd_abs_section_ptr
;
15136 sec
->kept_section
= first
;
15141 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15142 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15143 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15144 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15145 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15146 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15147 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15148 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15149 The reverse order cannot happen as there is never a bfd with only the
15150 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15151 matter as here were are looking only for cross-bfd sections. */
15153 if ((flags
& SEC_GROUP
) == 0 && startswith (name
, ".gnu.linkonce.r."))
15154 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15155 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15156 && startswith (l
->sec
->name
, ".gnu.linkonce.t."))
15158 if (abfd
!= l
->sec
->owner
)
15159 sec
->output_section
= bfd_abs_section_ptr
;
15163 /* This is the first section with this name. Record it. */
15164 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
15165 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
15166 return sec
->output_section
== bfd_abs_section_ptr
;
15170 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
15172 return sym
->st_shndx
== SHN_COMMON
;
15176 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
15182 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
15184 return bfd_com_section_ptr
;
15188 _bfd_elf_default_got_elt_size (bfd
*abfd
,
15189 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
15190 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
15191 bfd
*ibfd ATTRIBUTE_UNUSED
,
15192 unsigned long symndx ATTRIBUTE_UNUSED
)
15194 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15195 return bed
->s
->arch_size
/ 8;
15198 /* Routines to support the creation of dynamic relocs. */
15200 /* Returns the name of the dynamic reloc section associated with SEC. */
15202 static const char *
15203 get_dynamic_reloc_section_name (bfd
* abfd
,
15208 const char *old_name
= bfd_section_name (sec
);
15209 const char *prefix
= is_rela
? ".rela" : ".rel";
15211 if (old_name
== NULL
)
15214 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
15215 sprintf (name
, "%s%s", prefix
, old_name
);
15220 /* Returns the dynamic reloc section associated with SEC.
15221 If necessary compute the name of the dynamic reloc section based
15222 on SEC's name (looked up in ABFD's string table) and the setting
15226 _bfd_elf_get_dynamic_reloc_section (bfd
*abfd
,
15230 asection
*reloc_sec
= elf_section_data (sec
)->sreloc
;
15232 if (reloc_sec
== NULL
)
15234 const char *name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15238 reloc_sec
= bfd_get_linker_section (abfd
, name
);
15240 if (reloc_sec
!= NULL
)
15241 elf_section_data (sec
)->sreloc
= reloc_sec
;
15248 /* Returns the dynamic reloc section associated with SEC. If the
15249 section does not exist it is created and attached to the DYNOBJ
15250 bfd and stored in the SRELOC field of SEC's elf_section_data
15253 ALIGNMENT is the alignment for the newly created section and
15254 IS_RELA defines whether the name should be .rela.<SEC's name>
15255 or .rel.<SEC's name>. The section name is looked up in the
15256 string table associated with ABFD. */
15259 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
15261 unsigned int alignment
,
15265 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
15267 if (reloc_sec
== NULL
)
15269 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15274 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
15276 if (reloc_sec
== NULL
)
15278 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
15279 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
15280 if ((sec
->flags
& SEC_ALLOC
) != 0)
15281 flags
|= SEC_ALLOC
| SEC_LOAD
;
15283 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
15284 if (reloc_sec
!= NULL
)
15286 /* _bfd_elf_get_sec_type_attr chooses a section type by
15287 name. Override as it may be wrong, eg. for a user
15288 section named "auto" we'll get ".relauto" which is
15289 seen to be a .rela section. */
15290 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
15291 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
15296 elf_section_data (sec
)->sreloc
= reloc_sec
;
15302 /* Copy the ELF symbol type and other attributes for a linker script
15303 assignment from HSRC to HDEST. Generally this should be treated as
15304 if we found a strong non-dynamic definition for HDEST (except that
15305 ld ignores multiple definition errors). */
15307 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
15308 struct bfd_link_hash_entry
*hdest
,
15309 struct bfd_link_hash_entry
*hsrc
)
15311 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
15312 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
15313 Elf_Internal_Sym isym
;
15315 ehdest
->type
= ehsrc
->type
;
15316 ehdest
->target_internal
= ehsrc
->target_internal
;
15318 isym
.st_other
= ehsrc
->other
;
15319 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, true, false);
15322 /* Append a RELA relocation REL to section S in BFD. */
15325 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15327 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15328 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
15329 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
15330 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
15333 /* Append a REL relocation REL to section S in BFD. */
15336 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15338 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15339 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
15340 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
15341 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
15344 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15346 struct bfd_link_hash_entry
*
15347 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
15348 const char *symbol
, asection
*sec
)
15350 struct elf_link_hash_entry
*h
;
15352 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
15353 false, false, true);
15354 /* NB: Common symbols will be turned into definition later. */
15356 && !h
->root
.ldscript_def
15357 && (h
->root
.type
== bfd_link_hash_undefined
15358 || h
->root
.type
== bfd_link_hash_undefweak
15359 || ((h
->ref_regular
|| h
->def_dynamic
)
15361 && h
->root
.type
!= bfd_link_hash_common
)))
15363 bool was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15364 h
->verinfo
.verdef
= NULL
;
15365 h
->root
.type
= bfd_link_hash_defined
;
15366 h
->root
.u
.def
.section
= sec
;
15367 h
->root
.u
.def
.value
= 0;
15368 h
->def_regular
= 1;
15369 h
->def_dynamic
= 0;
15371 h
->u2
.start_stop_section
= sec
;
15372 if (symbol
[0] == '.')
15374 /* .startof. and .sizeof. symbols are local. */
15375 const struct elf_backend_data
*bed
;
15376 bed
= get_elf_backend_data (info
->output_bfd
);
15377 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
15381 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15382 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15383 | info
->start_stop_visibility
);
15385 bfd_elf_link_record_dynamic_symbol (info
, h
);
15392 /* Find dynamic relocs for H that apply to read-only sections. */
15395 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15397 struct elf_dyn_relocs
*p
;
15399 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15401 asection
*s
= p
->sec
->output_section
;
15403 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15409 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15410 read-only sections. */
15413 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15417 if (h
->root
.type
== bfd_link_hash_indirect
)
15420 sec
= _bfd_elf_readonly_dynrelocs (h
);
15423 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15425 info
->flags
|= DF_TEXTREL
;
15426 /* xgettext:c-format */
15427 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15428 "in read-only section `%pA'\n"),
15429 sec
->owner
, h
->root
.root
.string
, sec
);
15431 if (bfd_link_textrel_check (info
))
15432 /* xgettext:c-format */
15433 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15434 "in read-only section `%pA'\n"),
15435 sec
->owner
, h
->root
.root
.string
, sec
);
15437 /* Not an error, just cut short the traversal. */
15443 /* Add dynamic tags. */
15446 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15447 bool need_dynamic_reloc
)
15449 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15451 if (htab
->dynamic_sections_created
)
15453 /* Add some entries to the .dynamic section. We fill in the
15454 values later, in finish_dynamic_sections, but we must add
15455 the entries now so that we get the correct size for the
15456 .dynamic section. The DT_DEBUG entry is filled in by the
15457 dynamic linker and used by the debugger. */
15458 #define add_dynamic_entry(TAG, VAL) \
15459 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15461 const struct elf_backend_data
*bed
15462 = get_elf_backend_data (output_bfd
);
15464 if (bfd_link_executable (info
))
15466 if (!add_dynamic_entry (DT_DEBUG
, 0))
15470 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15472 /* DT_PLTGOT is used by prelink even if there is no PLT
15474 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15478 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15480 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15481 || !add_dynamic_entry (DT_PLTREL
,
15482 (bed
->rela_plts_and_copies_p
15483 ? DT_RELA
: DT_REL
))
15484 || !add_dynamic_entry (DT_JMPREL
, 0))
15488 if (htab
->tlsdesc_plt
15489 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15490 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15493 if (need_dynamic_reloc
)
15495 if (bed
->rela_plts_and_copies_p
)
15497 if (!add_dynamic_entry (DT_RELA
, 0)
15498 || !add_dynamic_entry (DT_RELASZ
, 0)
15499 || !add_dynamic_entry (DT_RELAENT
,
15500 bed
->s
->sizeof_rela
))
15505 if (!add_dynamic_entry (DT_REL
, 0)
15506 || !add_dynamic_entry (DT_RELSZ
, 0)
15507 || !add_dynamic_entry (DT_RELENT
,
15508 bed
->s
->sizeof_rel
))
15512 /* If any dynamic relocs apply to a read-only section,
15513 then we need a DT_TEXTREL entry. */
15514 if ((info
->flags
& DF_TEXTREL
) == 0)
15515 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15518 if ((info
->flags
& DF_TEXTREL
) != 0)
15520 if (htab
->ifunc_resolvers
)
15521 info
->callbacks
->einfo
15522 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15523 "may result in a segfault at runtime; recompile with %s\n"),
15524 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15526 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15531 #undef add_dynamic_entry