1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2022 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
43 struct elf_info_failed
45 struct bfd_link_info
*info
;
49 /* This structure is used to pass information to
50 _bfd_elf_link_find_version_dependencies. */
52 struct elf_find_verdep_info
54 /* General link information. */
55 struct bfd_link_info
*info
;
56 /* The number of dependencies. */
58 /* Whether we had a failure. */
62 static bool _bfd_elf_fix_symbol_flags
63 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
66 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
67 unsigned long r_symndx
,
70 if (r_symndx
>= cookie
->locsymcount
71 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
73 struct elf_link_hash_entry
*h
;
75 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
77 while (h
->root
.type
== bfd_link_hash_indirect
78 || h
->root
.type
== bfd_link_hash_warning
)
79 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
81 if ((h
->root
.type
== bfd_link_hash_defined
82 || h
->root
.type
== bfd_link_hash_defweak
)
83 && discarded_section (h
->root
.u
.def
.section
))
84 return h
->root
.u
.def
.section
;
90 /* It's not a relocation against a global symbol,
91 but it could be a relocation against a local
92 symbol for a discarded section. */
94 Elf_Internal_Sym
*isym
;
96 /* Need to: get the symbol; get the section. */
97 isym
= &cookie
->locsyms
[r_symndx
];
98 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
100 && discard
? discarded_section (isec
) : 1)
106 /* Define a symbol in a dynamic linkage section. */
108 struct elf_link_hash_entry
*
109 _bfd_elf_define_linkage_sym (bfd
*abfd
,
110 struct bfd_link_info
*info
,
114 struct elf_link_hash_entry
*h
;
115 struct bfd_link_hash_entry
*bh
;
116 const struct elf_backend_data
*bed
;
118 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, false, false, false);
121 /* Zap symbol defined in an as-needed lib that wasn't linked.
122 This is a symptom of a larger problem: Absolute symbols
123 defined in shared libraries can't be overridden, because we
124 lose the link to the bfd which is via the symbol section. */
125 h
->root
.type
= bfd_link_hash_new
;
131 bed
= get_elf_backend_data (abfd
);
132 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
133 sec
, 0, NULL
, false, bed
->collect
,
136 h
= (struct elf_link_hash_entry
*) bh
;
137 BFD_ASSERT (h
!= NULL
);
140 h
->root
.linker_def
= 1;
141 h
->type
= STT_OBJECT
;
142 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
143 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
145 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
150 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
154 struct elf_link_hash_entry
*h
;
155 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
156 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
158 /* This function may be called more than once. */
159 if (htab
->sgot
!= NULL
)
162 flags
= bed
->dynamic_sec_flags
;
164 s
= bfd_make_section_anyway_with_flags (abfd
,
165 (bed
->rela_plts_and_copies_p
166 ? ".rela.got" : ".rel.got"),
167 (bed
->dynamic_sec_flags
170 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
174 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
176 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
180 if (bed
->want_got_plt
)
182 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
184 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
189 /* The first bit of the global offset table is the header. */
190 s
->size
+= bed
->got_header_size
;
192 if (bed
->want_got_sym
)
194 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
195 (or .got.plt) section. We don't do this in the linker script
196 because we don't want to define the symbol if we are not creating
197 a global offset table. */
198 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
199 "_GLOBAL_OFFSET_TABLE_");
200 elf_hash_table (info
)->hgot
= h
;
208 /* Create a strtab to hold the dynamic symbol names. */
210 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
212 struct elf_link_hash_table
*hash_table
;
214 hash_table
= elf_hash_table (info
);
215 if (hash_table
->dynobj
== NULL
)
217 /* We may not set dynobj, an input file holding linker created
218 dynamic sections to abfd, which may be a dynamic object with
219 its own dynamic sections. We need to find a normal input file
220 to hold linker created sections if possible. */
221 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
225 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
227 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0
228 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
229 && elf_object_id (ibfd
) == elf_hash_table_id (hash_table
)
230 && !((s
= ibfd
->sections
) != NULL
231 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
))
237 hash_table
->dynobj
= abfd
;
240 if (hash_table
->dynstr
== NULL
)
242 hash_table
->dynstr
= _bfd_elf_strtab_init ();
243 if (hash_table
->dynstr
== NULL
)
249 /* Create some sections which will be filled in with dynamic linking
250 information. ABFD is an input file which requires dynamic sections
251 to be created. The dynamic sections take up virtual memory space
252 when the final executable is run, so we need to create them before
253 addresses are assigned to the output sections. We work out the
254 actual contents and size of these sections later. */
257 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
261 const struct elf_backend_data
*bed
;
262 struct elf_link_hash_entry
*h
;
264 if (! is_elf_hash_table (info
->hash
))
267 if (elf_hash_table (info
)->dynamic_sections_created
)
270 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
273 abfd
= elf_hash_table (info
)->dynobj
;
274 bed
= get_elf_backend_data (abfd
);
276 flags
= bed
->dynamic_sec_flags
;
278 /* A dynamically linked executable has a .interp section, but a
279 shared library does not. */
280 if (bfd_link_executable (info
) && !info
->nointerp
)
282 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
283 flags
| SEC_READONLY
);
288 /* Create sections to hold version informations. These are removed
289 if they are not needed. */
290 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
291 flags
| SEC_READONLY
);
293 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
296 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
297 flags
| SEC_READONLY
);
299 || !bfd_set_section_alignment (s
, 1))
302 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
303 flags
| SEC_READONLY
);
305 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
308 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
309 flags
| SEC_READONLY
);
311 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
313 elf_hash_table (info
)->dynsym
= s
;
315 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
316 flags
| SEC_READONLY
);
320 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
322 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
325 /* The special symbol _DYNAMIC is always set to the start of the
326 .dynamic section. We could set _DYNAMIC in a linker script, but we
327 only want to define it if we are, in fact, creating a .dynamic
328 section. We don't want to define it if there is no .dynamic
329 section, since on some ELF platforms the start up code examines it
330 to decide how to initialize the process. */
331 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
332 elf_hash_table (info
)->hdynamic
= h
;
338 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
339 flags
| SEC_READONLY
);
341 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
343 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
346 if (info
->emit_gnu_hash
&& bed
->record_xhash_symbol
== NULL
)
348 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
349 flags
| SEC_READONLY
);
351 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
353 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
354 4 32-bit words followed by variable count of 64-bit words, then
355 variable count of 32-bit words. */
356 if (bed
->s
->arch_size
== 64)
357 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
359 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
362 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_bread (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 /* Select the appropriate symbol table. If we don't know if the
3575 object file is an IR object, give linker LTO plugin a chance to
3576 get the correct symbol table. */
3577 if (abfd
->plugin_format
== bfd_plugin_yes
3578 #if BFD_SUPPORTS_PLUGINS
3579 || (abfd
->plugin_format
== bfd_plugin_unknown
3580 && bfd_link_plugin_object_p (abfd
))
3584 /* Use the IR symbol table if the object has been claimed by
3586 abfd
= abfd
->plugin_dummy_bfd
;
3587 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3589 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3590 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3592 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3594 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3596 /* The sh_info field of the symtab header tells us where the
3597 external symbols start. We don't care about the local symbols. */
3598 if (elf_bad_symtab (abfd
))
3600 extsymcount
= symcount
;
3605 extsymcount
= symcount
- hdr
->sh_info
;
3606 extsymoff
= hdr
->sh_info
;
3609 if (extsymcount
== 0)
3612 /* Read in the symbol table. */
3613 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3615 if (isymbuf
== NULL
)
3618 /* Scan the symbol table looking for SYMDEF. */
3620 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3624 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3629 if (strcmp (name
, symdef
->name
) == 0)
3631 result
= is_global_data_symbol_definition (abfd
, isym
);
3641 /* Add an entry to the .dynamic table. */
3644 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3648 struct elf_link_hash_table
*hash_table
;
3649 const struct elf_backend_data
*bed
;
3651 bfd_size_type newsize
;
3652 bfd_byte
*newcontents
;
3653 Elf_Internal_Dyn dyn
;
3655 hash_table
= elf_hash_table (info
);
3656 if (! is_elf_hash_table (&hash_table
->root
))
3659 if (tag
== DT_RELA
|| tag
== DT_REL
)
3660 hash_table
->dynamic_relocs
= true;
3662 bed
= get_elf_backend_data (hash_table
->dynobj
);
3663 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3664 BFD_ASSERT (s
!= NULL
);
3666 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3667 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3668 if (newcontents
== NULL
)
3672 dyn
.d_un
.d_val
= val
;
3673 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3676 s
->contents
= newcontents
;
3681 /* Strip zero-sized dynamic sections. */
3684 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3686 struct elf_link_hash_table
*hash_table
;
3687 const struct elf_backend_data
*bed
;
3688 asection
*s
, *sdynamic
, **pp
;
3689 asection
*rela_dyn
, *rel_dyn
;
3690 Elf_Internal_Dyn dyn
;
3691 bfd_byte
*extdyn
, *next
;
3692 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3693 bool strip_zero_sized
;
3694 bool strip_zero_sized_plt
;
3696 if (bfd_link_relocatable (info
))
3699 hash_table
= elf_hash_table (info
);
3700 if (!is_elf_hash_table (&hash_table
->root
))
3703 if (!hash_table
->dynobj
)
3706 sdynamic
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3710 bed
= get_elf_backend_data (hash_table
->dynobj
);
3711 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3713 strip_zero_sized
= false;
3714 strip_zero_sized_plt
= false;
3716 /* Strip zero-sized dynamic sections. */
3717 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3718 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3719 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3723 || s
== hash_table
->srelplt
->output_section
3724 || s
== hash_table
->splt
->output_section
))
3727 info
->output_bfd
->section_count
--;
3728 strip_zero_sized
= true;
3733 else if (s
== hash_table
->splt
->output_section
)
3735 s
= hash_table
->splt
;
3736 strip_zero_sized_plt
= true;
3739 s
= hash_table
->srelplt
;
3740 s
->flags
|= SEC_EXCLUDE
;
3741 s
->output_section
= bfd_abs_section_ptr
;
3746 if (strip_zero_sized_plt
&& sdynamic
->size
!= 0)
3747 for (extdyn
= sdynamic
->contents
;
3748 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3751 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3752 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3760 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3761 the procedure linkage table (the .plt section) has been
3763 memmove (extdyn
, next
,
3764 sdynamic
->size
- (next
- sdynamic
->contents
));
3769 if (strip_zero_sized
)
3771 /* Regenerate program headers. */
3772 elf_seg_map (info
->output_bfd
) = NULL
;
3773 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
,
3780 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3781 1 if a DT_NEEDED tag already exists, and 0 on success. */
3784 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3786 struct elf_link_hash_table
*hash_table
;
3790 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3793 hash_table
= elf_hash_table (info
);
3794 soname
= elf_dt_name (abfd
);
3795 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, false);
3796 if (strindex
== (size_t) -1)
3799 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3802 const struct elf_backend_data
*bed
;
3805 bed
= get_elf_backend_data (hash_table
->dynobj
);
3806 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3807 if (sdyn
!= NULL
&& sdyn
->size
!= 0)
3808 for (extdyn
= sdyn
->contents
;
3809 extdyn
< sdyn
->contents
+ sdyn
->size
;
3810 extdyn
+= bed
->s
->sizeof_dyn
)
3812 Elf_Internal_Dyn dyn
;
3814 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3815 if (dyn
.d_tag
== DT_NEEDED
3816 && dyn
.d_un
.d_val
== strindex
)
3818 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3824 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3827 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3833 /* Return true if SONAME is on the needed list between NEEDED and STOP
3834 (or the end of list if STOP is NULL), and needed by a library that
3838 on_needed_list (const char *soname
,
3839 struct bfd_link_needed_list
*needed
,
3840 struct bfd_link_needed_list
*stop
)
3842 struct bfd_link_needed_list
*look
;
3843 for (look
= needed
; look
!= stop
; look
= look
->next
)
3844 if (strcmp (soname
, look
->name
) == 0
3845 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3846 /* If needed by a library that itself is not directly
3847 needed, recursively check whether that library is
3848 indirectly needed. Since we add DT_NEEDED entries to
3849 the end of the list, library dependencies appear after
3850 the library. Therefore search prior to the current
3851 LOOK, preventing possible infinite recursion. */
3852 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3858 /* Sort symbol by value, section, size, and type. */
3860 elf_sort_symbol (const void *arg1
, const void *arg2
)
3862 const struct elf_link_hash_entry
*h1
;
3863 const struct elf_link_hash_entry
*h2
;
3864 bfd_signed_vma vdiff
;
3869 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3870 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3871 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3873 return vdiff
> 0 ? 1 : -1;
3875 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3879 /* Sort so that sized symbols are selected over zero size symbols. */
3880 vdiff
= h1
->size
- h2
->size
;
3882 return vdiff
> 0 ? 1 : -1;
3884 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3885 if (h1
->type
!= h2
->type
)
3886 return h1
->type
- h2
->type
;
3888 /* If symbols are properly sized and typed, and multiple strong
3889 aliases are not defined in a shared library by the user we
3890 shouldn't get here. Unfortunately linker script symbols like
3891 __bss_start sometimes match a user symbol defined at the start of
3892 .bss without proper size and type. We'd like to preference the
3893 user symbol over reserved system symbols. Sort on leading
3895 n1
= h1
->root
.root
.string
;
3896 n2
= h2
->root
.root
.string
;
3909 /* Final sort on name selects user symbols like '_u' over reserved
3910 system symbols like '_Z' and also will avoid qsort instability. */
3914 /* This function is used to adjust offsets into .dynstr for
3915 dynamic symbols. This is called via elf_link_hash_traverse. */
3918 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3920 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3922 if (h
->dynindx
!= -1)
3923 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3927 /* Assign string offsets in .dynstr, update all structures referencing
3931 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3933 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3934 struct elf_link_local_dynamic_entry
*entry
;
3935 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3936 bfd
*dynobj
= hash_table
->dynobj
;
3939 const struct elf_backend_data
*bed
;
3942 _bfd_elf_strtab_finalize (dynstr
);
3943 size
= _bfd_elf_strtab_size (dynstr
);
3945 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3947 if (info
->callbacks
->examine_strtab
)
3948 info
->callbacks
->examine_strtab (dynstr
);
3950 bed
= get_elf_backend_data (dynobj
);
3951 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3952 BFD_ASSERT (sdyn
!= NULL
);
3954 /* Update all .dynamic entries referencing .dynstr strings. */
3955 for (extdyn
= sdyn
->contents
;
3956 extdyn
< PTR_ADD (sdyn
->contents
, sdyn
->size
);
3957 extdyn
+= bed
->s
->sizeof_dyn
)
3959 Elf_Internal_Dyn dyn
;
3961 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3965 dyn
.d_un
.d_val
= size
;
3975 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3980 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3983 /* Now update local dynamic symbols. */
3984 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3985 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3986 entry
->isym
.st_name
);
3988 /* And the rest of dynamic symbols. */
3989 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3991 /* Adjust version definitions. */
3992 if (elf_tdata (output_bfd
)->cverdefs
)
3997 Elf_Internal_Verdef def
;
3998 Elf_Internal_Verdaux defaux
;
4000 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
4004 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
4006 p
+= sizeof (Elf_External_Verdef
);
4007 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
4009 for (i
= 0; i
< def
.vd_cnt
; ++i
)
4011 _bfd_elf_swap_verdaux_in (output_bfd
,
4012 (Elf_External_Verdaux
*) p
, &defaux
);
4013 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
4015 _bfd_elf_swap_verdaux_out (output_bfd
,
4016 &defaux
, (Elf_External_Verdaux
*) p
);
4017 p
+= sizeof (Elf_External_Verdaux
);
4020 while (def
.vd_next
);
4023 /* Adjust version references. */
4024 if (elf_tdata (output_bfd
)->verref
)
4029 Elf_Internal_Verneed need
;
4030 Elf_Internal_Vernaux needaux
;
4032 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
4036 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
4038 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
4039 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
4040 (Elf_External_Verneed
*) p
);
4041 p
+= sizeof (Elf_External_Verneed
);
4042 for (i
= 0; i
< need
.vn_cnt
; ++i
)
4044 _bfd_elf_swap_vernaux_in (output_bfd
,
4045 (Elf_External_Vernaux
*) p
, &needaux
);
4046 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
4048 _bfd_elf_swap_vernaux_out (output_bfd
,
4050 (Elf_External_Vernaux
*) p
);
4051 p
+= sizeof (Elf_External_Vernaux
);
4054 while (need
.vn_next
);
4060 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4061 The default is to only match when the INPUT and OUTPUT are exactly
4065 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
4066 const bfd_target
*output
)
4068 return input
== output
;
4071 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4072 This version is used when different targets for the same architecture
4073 are virtually identical. */
4076 _bfd_elf_relocs_compatible (const bfd_target
*input
,
4077 const bfd_target
*output
)
4079 const struct elf_backend_data
*obed
, *ibed
;
4081 if (input
== output
)
4084 ibed
= xvec_get_elf_backend_data (input
);
4085 obed
= xvec_get_elf_backend_data (output
);
4087 if (ibed
->arch
!= obed
->arch
)
4090 /* If both backends are using this function, deem them compatible. */
4091 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
4094 /* Make a special call to the linker "notice" function to tell it that
4095 we are about to handle an as-needed lib, or have finished
4096 processing the lib. */
4099 _bfd_elf_notice_as_needed (bfd
*ibfd
,
4100 struct bfd_link_info
*info
,
4101 enum notice_asneeded_action act
)
4103 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
4106 /* Call ACTION on each relocation in an ELF object file. */
4109 _bfd_elf_link_iterate_on_relocs
4110 (bfd
*abfd
, struct bfd_link_info
*info
,
4111 bool (*action
) (bfd
*, struct bfd_link_info
*, asection
*,
4112 const Elf_Internal_Rela
*))
4114 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4115 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4117 /* If this object is the same format as the output object, and it is
4118 not a shared library, then let the backend look through the
4121 This is required to build global offset table entries and to
4122 arrange for dynamic relocs. It is not required for the
4123 particular common case of linking non PIC code, even when linking
4124 against shared libraries, but unfortunately there is no way of
4125 knowing whether an object file has been compiled PIC or not.
4126 Looking through the relocs is not particularly time consuming.
4127 The problem is that we must either (1) keep the relocs in memory,
4128 which causes the linker to require additional runtime memory or
4129 (2) read the relocs twice from the input file, which wastes time.
4130 This would be a good case for using mmap.
4132 I have no idea how to handle linking PIC code into a file of a
4133 different format. It probably can't be done. */
4134 if ((abfd
->flags
& DYNAMIC
) == 0
4135 && is_elf_hash_table (&htab
->root
)
4136 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4137 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4141 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4143 Elf_Internal_Rela
*internal_relocs
;
4146 /* Don't check relocations in excluded sections. Don't do
4147 anything special with non-loaded, non-alloced sections.
4148 In particular, any relocs in such sections should not
4149 affect GOT and PLT reference counting (ie. we don't
4150 allow them to create GOT or PLT entries), there's no
4151 possibility or desire to optimize TLS relocs, and
4152 there's not much point in propagating relocs to shared
4153 libs that the dynamic linker won't relocate. */
4154 if ((o
->flags
& SEC_ALLOC
) == 0
4155 || (o
->flags
& SEC_RELOC
) == 0
4156 || (o
->flags
& SEC_EXCLUDE
) != 0
4157 || o
->reloc_count
== 0
4158 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4159 && (o
->flags
& SEC_DEBUGGING
) != 0)
4160 || bfd_is_abs_section (o
->output_section
))
4163 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
4166 _bfd_link_keep_memory (info
));
4167 if (internal_relocs
== NULL
)
4170 ok
= action (abfd
, info
, o
, internal_relocs
);
4172 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4173 free (internal_relocs
);
4183 /* Check relocations in an ELF object file. This is called after
4184 all input files have been opened. */
4187 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
4189 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4190 if (bed
->check_relocs
!= NULL
)
4191 return _bfd_elf_link_iterate_on_relocs (abfd
, info
,
4196 /* Add symbols from an ELF object file to the linker hash table. */
4199 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4201 Elf_Internal_Ehdr
*ehdr
;
4202 Elf_Internal_Shdr
*hdr
;
4206 struct elf_link_hash_entry
**sym_hash
;
4208 Elf_External_Versym
*extversym
= NULL
;
4209 Elf_External_Versym
*extversym_end
= NULL
;
4210 Elf_External_Versym
*ever
;
4211 struct elf_link_hash_entry
*weaks
;
4212 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4213 size_t nondeflt_vers_cnt
= 0;
4214 Elf_Internal_Sym
*isymbuf
= NULL
;
4215 Elf_Internal_Sym
*isym
;
4216 Elf_Internal_Sym
*isymend
;
4217 const struct elf_backend_data
*bed
;
4219 struct elf_link_hash_table
*htab
;
4220 void *alloc_mark
= NULL
;
4221 struct bfd_hash_entry
**old_table
= NULL
;
4222 unsigned int old_size
= 0;
4223 unsigned int old_count
= 0;
4224 void *old_tab
= NULL
;
4226 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4227 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4228 void *old_strtab
= NULL
;
4233 htab
= elf_hash_table (info
);
4234 bed
= get_elf_backend_data (abfd
);
4236 if ((abfd
->flags
& DYNAMIC
) == 0)
4242 /* You can't use -r against a dynamic object. Also, there's no
4243 hope of using a dynamic object which does not exactly match
4244 the format of the output file. */
4245 if (bfd_link_relocatable (info
)
4246 || !is_elf_hash_table (&htab
->root
)
4247 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4249 if (bfd_link_relocatable (info
))
4250 bfd_set_error (bfd_error_invalid_operation
);
4252 bfd_set_error (bfd_error_wrong_format
);
4257 ehdr
= elf_elfheader (abfd
);
4258 if (info
->warn_alternate_em
4259 && bed
->elf_machine_code
!= ehdr
->e_machine
4260 && ((bed
->elf_machine_alt1
!= 0
4261 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4262 || (bed
->elf_machine_alt2
!= 0
4263 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4265 /* xgettext:c-format */
4266 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4267 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4269 /* As a GNU extension, any input sections which are named
4270 .gnu.warning.SYMBOL are treated as warning symbols for the given
4271 symbol. This differs from .gnu.warning sections, which generate
4272 warnings when they are included in an output file. */
4273 /* PR 12761: Also generate this warning when building shared libraries. */
4274 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4278 name
= bfd_section_name (s
);
4279 if (startswith (name
, ".gnu.warning."))
4284 name
+= sizeof ".gnu.warning." - 1;
4286 /* If this is a shared object, then look up the symbol
4287 in the hash table. If it is there, and it is already
4288 been defined, then we will not be using the entry
4289 from this shared object, so we don't need to warn.
4290 FIXME: If we see the definition in a regular object
4291 later on, we will warn, but we shouldn't. The only
4292 fix is to keep track of what warnings we are supposed
4293 to emit, and then handle them all at the end of the
4297 struct elf_link_hash_entry
*h
;
4299 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
4301 /* FIXME: What about bfd_link_hash_common? */
4303 && (h
->root
.type
== bfd_link_hash_defined
4304 || h
->root
.type
== bfd_link_hash_defweak
))
4309 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4313 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4318 if (! (_bfd_generic_link_add_one_symbol
4319 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4320 false, bed
->collect
, NULL
)))
4323 if (bfd_link_executable (info
))
4325 /* Clobber the section size so that the warning does
4326 not get copied into the output file. */
4329 /* Also set SEC_EXCLUDE, so that symbols defined in
4330 the warning section don't get copied to the output. */
4331 s
->flags
|= SEC_EXCLUDE
;
4336 just_syms
= ((s
= abfd
->sections
) != NULL
4337 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4342 /* If we are creating a shared library, create all the dynamic
4343 sections immediately. We need to attach them to something,
4344 so we attach them to this BFD, provided it is the right
4345 format and is not from ld --just-symbols. Always create the
4346 dynamic sections for -E/--dynamic-list. FIXME: If there
4347 are no input BFD's of the same format as the output, we can't
4348 make a shared library. */
4350 && (bfd_link_pic (info
)
4351 || (!bfd_link_relocatable (info
)
4353 && (info
->export_dynamic
|| info
->dynamic
)))
4354 && is_elf_hash_table (&htab
->root
)
4355 && info
->output_bfd
->xvec
== abfd
->xvec
4356 && !htab
->dynamic_sections_created
)
4358 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4362 else if (!is_elf_hash_table (&htab
->root
))
4366 const char *soname
= NULL
;
4368 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4369 const Elf_Internal_Phdr
*phdr
;
4370 struct elf_link_loaded_list
*loaded_lib
;
4372 /* ld --just-symbols and dynamic objects don't mix very well.
4373 ld shouldn't allow it. */
4377 /* If this dynamic lib was specified on the command line with
4378 --as-needed in effect, then we don't want to add a DT_NEEDED
4379 tag unless the lib is actually used. Similary for libs brought
4380 in by another lib's DT_NEEDED. When --no-add-needed is used
4381 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4382 any dynamic library in DT_NEEDED tags in the dynamic lib at
4384 add_needed
= (elf_dyn_lib_class (abfd
)
4385 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4386 | DYN_NO_NEEDED
)) == 0;
4388 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4389 if (s
!= NULL
&& s
->size
!= 0)
4393 unsigned int elfsec
;
4394 unsigned long shlink
;
4396 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4403 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4404 if (elfsec
== SHN_BAD
)
4405 goto error_free_dyn
;
4406 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4408 for (extdyn
= dynbuf
;
4409 extdyn
<= dynbuf
+ s
->size
- bed
->s
->sizeof_dyn
;
4410 extdyn
+= bed
->s
->sizeof_dyn
)
4412 Elf_Internal_Dyn dyn
;
4414 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4415 if (dyn
.d_tag
== DT_SONAME
)
4417 unsigned int tagv
= dyn
.d_un
.d_val
;
4418 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4420 goto error_free_dyn
;
4422 if (dyn
.d_tag
== DT_NEEDED
)
4424 struct bfd_link_needed_list
*n
, **pn
;
4426 unsigned int tagv
= dyn
.d_un
.d_val
;
4427 size_t amt
= sizeof (struct bfd_link_needed_list
);
4429 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4430 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4431 if (n
== NULL
|| fnm
== NULL
)
4432 goto error_free_dyn
;
4433 amt
= strlen (fnm
) + 1;
4434 anm
= (char *) bfd_alloc (abfd
, amt
);
4436 goto error_free_dyn
;
4437 memcpy (anm
, fnm
, amt
);
4441 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4445 if (dyn
.d_tag
== DT_RUNPATH
)
4447 struct bfd_link_needed_list
*n
, **pn
;
4449 unsigned int tagv
= dyn
.d_un
.d_val
;
4450 size_t amt
= sizeof (struct bfd_link_needed_list
);
4452 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4453 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4454 if (n
== NULL
|| fnm
== NULL
)
4455 goto error_free_dyn
;
4456 amt
= strlen (fnm
) + 1;
4457 anm
= (char *) bfd_alloc (abfd
, amt
);
4459 goto error_free_dyn
;
4460 memcpy (anm
, fnm
, amt
);
4464 for (pn
= & runpath
;
4470 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4471 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4473 struct bfd_link_needed_list
*n
, **pn
;
4475 unsigned int tagv
= dyn
.d_un
.d_val
;
4476 size_t amt
= sizeof (struct bfd_link_needed_list
);
4478 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4479 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4480 if (n
== NULL
|| fnm
== NULL
)
4481 goto error_free_dyn
;
4482 amt
= strlen (fnm
) + 1;
4483 anm
= (char *) bfd_alloc (abfd
, amt
);
4485 goto error_free_dyn
;
4486 memcpy (anm
, fnm
, amt
);
4496 if (dyn
.d_tag
== DT_AUDIT
)
4498 unsigned int tagv
= dyn
.d_un
.d_val
;
4499 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4501 if (dyn
.d_tag
== DT_FLAGS_1
)
4502 elf_tdata (abfd
)->is_pie
= (dyn
.d_un
.d_val
& DF_1_PIE
) != 0;
4508 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4509 frees all more recently bfd_alloc'd blocks as well. */
4515 struct bfd_link_needed_list
**pn
;
4516 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4521 /* If we have a PT_GNU_RELRO program header, mark as read-only
4522 all sections contained fully therein. This makes relro
4523 shared library sections appear as they will at run-time. */
4524 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4525 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4526 if (phdr
->p_type
== PT_GNU_RELRO
)
4528 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4530 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4532 if ((s
->flags
& SEC_ALLOC
) != 0
4533 && s
->vma
* opb
>= phdr
->p_vaddr
4534 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4535 s
->flags
|= SEC_READONLY
;
4540 /* We do not want to include any of the sections in a dynamic
4541 object in the output file. We hack by simply clobbering the
4542 list of sections in the BFD. This could be handled more
4543 cleanly by, say, a new section flag; the existing
4544 SEC_NEVER_LOAD flag is not the one we want, because that one
4545 still implies that the section takes up space in the output
4547 bfd_section_list_clear (abfd
);
4549 /* Find the name to use in a DT_NEEDED entry that refers to this
4550 object. If the object has a DT_SONAME entry, we use it.
4551 Otherwise, if the generic linker stuck something in
4552 elf_dt_name, we use that. Otherwise, we just use the file
4554 if (soname
== NULL
|| *soname
== '\0')
4556 soname
= elf_dt_name (abfd
);
4557 if (soname
== NULL
|| *soname
== '\0')
4558 soname
= bfd_get_filename (abfd
);
4561 /* Save the SONAME because sometimes the linker emulation code
4562 will need to know it. */
4563 elf_dt_name (abfd
) = soname
;
4565 /* If we have already included this dynamic object in the
4566 link, just ignore it. There is no reason to include a
4567 particular dynamic object more than once. */
4568 for (loaded_lib
= htab
->dyn_loaded
;
4570 loaded_lib
= loaded_lib
->next
)
4572 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4576 /* Create dynamic sections for backends that require that be done
4577 before setup_gnu_properties. */
4579 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4582 /* Save the DT_AUDIT entry for the linker emulation code. */
4583 elf_dt_audit (abfd
) = audit
;
4586 /* If this is a dynamic object, we always link against the .dynsym
4587 symbol table, not the .symtab symbol table. The dynamic linker
4588 will only see the .dynsym symbol table, so there is no reason to
4589 look at .symtab for a dynamic object. */
4591 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4592 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4594 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4596 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4598 /* The sh_info field of the symtab header tells us where the
4599 external symbols start. We don't care about the local symbols at
4601 if (elf_bad_symtab (abfd
))
4603 extsymcount
= symcount
;
4608 extsymcount
= symcount
- hdr
->sh_info
;
4609 extsymoff
= hdr
->sh_info
;
4612 sym_hash
= elf_sym_hashes (abfd
);
4613 if (extsymcount
!= 0)
4615 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4617 if (isymbuf
== NULL
)
4620 if (sym_hash
== NULL
)
4622 /* We store a pointer to the hash table entry for each
4624 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4625 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4626 if (sym_hash
== NULL
)
4627 goto error_free_sym
;
4628 elf_sym_hashes (abfd
) = sym_hash
;
4634 /* Read in any version definitions. */
4635 if (!_bfd_elf_slurp_version_tables (abfd
,
4636 info
->default_imported_symver
))
4637 goto error_free_sym
;
4639 /* Read in the symbol versions, but don't bother to convert them
4640 to internal format. */
4641 if (elf_dynversym (abfd
) != 0)
4643 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4644 bfd_size_type amt
= versymhdr
->sh_size
;
4646 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4647 goto error_free_sym
;
4648 extversym
= (Elf_External_Versym
*)
4649 _bfd_malloc_and_read (abfd
, amt
, amt
);
4650 if (extversym
== NULL
)
4651 goto error_free_sym
;
4652 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4656 /* If we are loading an as-needed shared lib, save the symbol table
4657 state before we start adding symbols. If the lib turns out
4658 to be unneeded, restore the state. */
4659 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4664 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4666 struct bfd_hash_entry
*p
;
4667 struct elf_link_hash_entry
*h
;
4669 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4671 h
= (struct elf_link_hash_entry
*) p
;
4672 entsize
+= htab
->root
.table
.entsize
;
4673 if (h
->root
.type
== bfd_link_hash_warning
)
4675 entsize
+= htab
->root
.table
.entsize
;
4676 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4678 if (h
->root
.type
== bfd_link_hash_common
)
4679 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4683 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4684 old_tab
= bfd_malloc (tabsize
+ entsize
);
4685 if (old_tab
== NULL
)
4686 goto error_free_vers
;
4688 /* Remember the current objalloc pointer, so that all mem for
4689 symbols added can later be reclaimed. */
4690 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4691 if (alloc_mark
== NULL
)
4692 goto error_free_vers
;
4694 /* Make a special call to the linker "notice" function to
4695 tell it that we are about to handle an as-needed lib. */
4696 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4697 goto error_free_vers
;
4699 /* Clone the symbol table. Remember some pointers into the
4700 symbol table, and dynamic symbol count. */
4701 old_ent
= (char *) old_tab
+ tabsize
;
4702 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4703 old_undefs
= htab
->root
.undefs
;
4704 old_undefs_tail
= htab
->root
.undefs_tail
;
4705 old_table
= htab
->root
.table
.table
;
4706 old_size
= htab
->root
.table
.size
;
4707 old_count
= htab
->root
.table
.count
;
4709 if (htab
->dynstr
!= NULL
)
4711 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4712 if (old_strtab
== NULL
)
4713 goto error_free_vers
;
4716 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4718 struct bfd_hash_entry
*p
;
4719 struct elf_link_hash_entry
*h
;
4721 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4723 h
= (struct elf_link_hash_entry
*) p
;
4724 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4725 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4726 if (h
->root
.type
== bfd_link_hash_warning
)
4728 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4729 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4730 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4732 if (h
->root
.type
== bfd_link_hash_common
)
4734 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4735 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4742 if (extversym
== NULL
)
4744 else if (extversym
+ extsymoff
< extversym_end
)
4745 ever
= extversym
+ extsymoff
;
4748 /* xgettext:c-format */
4749 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4750 abfd
, (long) extsymoff
,
4751 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4752 bfd_set_error (bfd_error_bad_value
);
4753 goto error_free_vers
;
4756 if (!bfd_link_relocatable (info
)
4757 && abfd
->lto_slim_object
)
4760 (_("%pB: plugin needed to handle lto object"), abfd
);
4763 for (isym
= isymbuf
, isymend
= PTR_ADD (isymbuf
, extsymcount
);
4765 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4769 asection
*sec
, *new_sec
;
4772 struct elf_link_hash_entry
*h
;
4773 struct elf_link_hash_entry
*hi
;
4775 bool size_change_ok
;
4776 bool type_change_ok
;
4782 unsigned int old_alignment
;
4783 unsigned int shindex
;
4789 flags
= BSF_NO_FLAGS
;
4791 value
= isym
->st_value
;
4792 common
= bed
->common_definition (isym
);
4793 if (common
&& info
->inhibit_common_definition
)
4795 /* Treat common symbol as undefined for --no-define-common. */
4796 isym
->st_shndx
= SHN_UNDEF
;
4801 bind
= ELF_ST_BIND (isym
->st_info
);
4805 /* This should be impossible, since ELF requires that all
4806 global symbols follow all local symbols, and that sh_info
4807 point to the first global symbol. Unfortunately, Irix 5
4809 if (elf_bad_symtab (abfd
))
4812 /* If we aren't prepared to handle locals within the globals
4813 then we'll likely segfault on a NULL symbol hash if the
4814 symbol is ever referenced in relocations. */
4815 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4816 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4817 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4818 " (>= sh_info of %lu)"),
4819 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4822 /* Dynamic object relocations are not processed by ld, so
4823 ld won't run into the problem mentioned above. */
4826 bfd_set_error (bfd_error_bad_value
);
4827 goto error_free_vers
;
4830 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4838 case STB_GNU_UNIQUE
:
4839 flags
= BSF_GNU_UNIQUE
;
4843 /* Leave it up to the processor backend. */
4847 if (isym
->st_shndx
== SHN_UNDEF
)
4848 sec
= bfd_und_section_ptr
;
4849 else if (isym
->st_shndx
== SHN_ABS
)
4850 sec
= bfd_abs_section_ptr
;
4851 else if (isym
->st_shndx
== SHN_COMMON
)
4853 sec
= bfd_com_section_ptr
;
4854 /* What ELF calls the size we call the value. What ELF
4855 calls the value we call the alignment. */
4856 value
= isym
->st_size
;
4860 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4862 sec
= bfd_abs_section_ptr
;
4863 else if (discarded_section (sec
))
4865 /* Symbols from discarded section are undefined. We keep
4867 sec
= bfd_und_section_ptr
;
4869 isym
->st_shndx
= SHN_UNDEF
;
4871 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4875 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4878 goto error_free_vers
;
4880 if (isym
->st_shndx
== SHN_COMMON
4881 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4883 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4887 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4889 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4891 goto error_free_vers
;
4895 else if (isym
->st_shndx
== SHN_COMMON
4896 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4897 && !bfd_link_relocatable (info
))
4899 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4903 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4904 | SEC_LINKER_CREATED
);
4905 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4907 goto error_free_vers
;
4911 else if (bed
->elf_add_symbol_hook
)
4913 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4915 goto error_free_vers
;
4917 /* The hook function sets the name to NULL if this symbol
4918 should be skipped for some reason. */
4923 /* Sanity check that all possibilities were handled. */
4927 /* Silently discard TLS symbols from --just-syms. There's
4928 no way to combine a static TLS block with a new TLS block
4929 for this executable. */
4930 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4931 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4934 if (bfd_is_und_section (sec
)
4935 || bfd_is_com_section (sec
))
4940 size_change_ok
= false;
4941 type_change_ok
= bed
->type_change_ok
;
4948 if (is_elf_hash_table (&htab
->root
))
4950 Elf_Internal_Versym iver
;
4951 unsigned int vernum
= 0;
4956 if (info
->default_imported_symver
)
4957 /* Use the default symbol version created earlier. */
4958 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4962 else if (ever
>= extversym_end
)
4964 /* xgettext:c-format */
4965 _bfd_error_handler (_("%pB: not enough version information"),
4967 bfd_set_error (bfd_error_bad_value
);
4968 goto error_free_vers
;
4971 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4973 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4975 /* If this is a hidden symbol, or if it is not version
4976 1, we append the version name to the symbol name.
4977 However, we do not modify a non-hidden absolute symbol
4978 if it is not a function, because it might be the version
4979 symbol itself. FIXME: What if it isn't? */
4980 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4982 && (!bfd_is_abs_section (sec
)
4983 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4986 size_t namelen
, verlen
, newlen
;
4989 if (isym
->st_shndx
!= SHN_UNDEF
)
4991 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4993 else if (vernum
> 1)
4995 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
5002 /* xgettext:c-format */
5003 (_("%pB: %s: invalid version %u (max %d)"),
5005 elf_tdata (abfd
)->cverdefs
);
5006 bfd_set_error (bfd_error_bad_value
);
5007 goto error_free_vers
;
5012 /* We cannot simply test for the number of
5013 entries in the VERNEED section since the
5014 numbers for the needed versions do not start
5016 Elf_Internal_Verneed
*t
;
5019 for (t
= elf_tdata (abfd
)->verref
;
5023 Elf_Internal_Vernaux
*a
;
5025 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5027 if (a
->vna_other
== vernum
)
5029 verstr
= a
->vna_nodename
;
5039 /* xgettext:c-format */
5040 (_("%pB: %s: invalid needed version %d"),
5041 abfd
, name
, vernum
);
5042 bfd_set_error (bfd_error_bad_value
);
5043 goto error_free_vers
;
5047 namelen
= strlen (name
);
5048 verlen
= strlen (verstr
);
5049 newlen
= namelen
+ verlen
+ 2;
5050 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5051 && isym
->st_shndx
!= SHN_UNDEF
)
5054 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
5055 if (newname
== NULL
)
5056 goto error_free_vers
;
5057 memcpy (newname
, name
, namelen
);
5058 p
= newname
+ namelen
;
5060 /* If this is a defined non-hidden version symbol,
5061 we add another @ to the name. This indicates the
5062 default version of the symbol. */
5063 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5064 && isym
->st_shndx
!= SHN_UNDEF
)
5066 memcpy (p
, verstr
, verlen
+ 1);
5071 /* If this symbol has default visibility and the user has
5072 requested we not re-export it, then mark it as hidden. */
5073 if (!bfd_is_und_section (sec
)
5076 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
5077 isym
->st_other
= (STV_HIDDEN
5078 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
5080 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
5081 sym_hash
, &old_bfd
, &old_weak
,
5082 &old_alignment
, &skip
, &override
,
5083 &type_change_ok
, &size_change_ok
,
5085 goto error_free_vers
;
5090 /* Override a definition only if the new symbol matches the
5092 if (override
&& matched
)
5096 while (h
->root
.type
== bfd_link_hash_indirect
5097 || h
->root
.type
== bfd_link_hash_warning
)
5098 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5100 if (h
->versioned
!= unversioned
5101 && elf_tdata (abfd
)->verdef
!= NULL
5104 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
5107 if (! (_bfd_generic_link_add_one_symbol
5108 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
5109 NULL
, false, bed
->collect
,
5110 (struct bfd_link_hash_entry
**) sym_hash
)))
5111 goto error_free_vers
;
5114 /* We need to make sure that indirect symbol dynamic flags are
5117 while (h
->root
.type
== bfd_link_hash_indirect
5118 || h
->root
.type
== bfd_link_hash_warning
)
5119 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5123 /* Setting the index to -3 tells elf_link_output_extsym that
5124 this symbol is defined in a discarded section. */
5125 if (discarded
&& is_elf_hash_table (&htab
->root
))
5128 new_weak
= (flags
& BSF_WEAK
) != 0;
5132 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
5133 && is_elf_hash_table (&htab
->root
)
5134 && h
->u
.alias
== NULL
)
5136 /* Keep a list of all weak defined non function symbols from
5137 a dynamic object, using the alias field. Later in this
5138 function we will set the alias field to the correct
5139 value. We only put non-function symbols from dynamic
5140 objects on this list, because that happens to be the only
5141 time we need to know the normal symbol corresponding to a
5142 weak symbol, and the information is time consuming to
5143 figure out. If the alias field is not already NULL,
5144 then this symbol was already defined by some previous
5145 dynamic object, and we will be using that previous
5146 definition anyhow. */
5152 /* Set the alignment of a common symbol. */
5153 if ((common
|| bfd_is_com_section (sec
))
5154 && h
->root
.type
== bfd_link_hash_common
)
5159 align
= bfd_log2 (isym
->st_value
);
5162 /* The new symbol is a common symbol in a shared object.
5163 We need to get the alignment from the section. */
5164 align
= new_sec
->alignment_power
;
5166 if (align
> old_alignment
)
5167 h
->root
.u
.c
.p
->alignment_power
= align
;
5169 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5172 if (is_elf_hash_table (&htab
->root
))
5174 /* Set a flag in the hash table entry indicating the type of
5175 reference or definition we just found. A dynamic symbol
5176 is one which is referenced or defined by both a regular
5177 object and a shared object. */
5178 bool dynsym
= false;
5180 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5181 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5183 /* Except for this flag to track nonweak references. */
5185 && bind
!= STB_WEAK
)
5186 h
->ref_ir_nonweak
= 1;
5193 if (bind
!= STB_WEAK
)
5194 h
->ref_regular_nonweak
= 1;
5211 hi
->ref_dynamic
= 1;
5216 hi
->def_dynamic
= 1;
5220 /* If an indirect symbol has been forced local, don't
5221 make the real symbol dynamic. */
5222 if (h
!= hi
&& hi
->forced_local
)
5226 if (bfd_link_dll (info
)
5236 && weakdef (h
)->dynindx
!= -1))
5240 /* Check to see if we need to add an indirect symbol for
5241 the default name. */
5243 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5245 && hi
->versioned
== versioned_hidden
))
5246 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5247 sec
, value
, &old_bfd
, &dynsym
))
5248 goto error_free_vers
;
5250 /* Check the alignment when a common symbol is involved. This
5251 can change when a common symbol is overridden by a normal
5252 definition or a common symbol is ignored due to the old
5253 normal definition. We need to make sure the maximum
5254 alignment is maintained. */
5255 if ((old_alignment
|| common
)
5256 && h
->root
.type
!= bfd_link_hash_common
)
5258 unsigned int common_align
;
5259 unsigned int normal_align
;
5260 unsigned int symbol_align
;
5264 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5265 || h
->root
.type
== bfd_link_hash_defweak
);
5267 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5268 if (h
->root
.u
.def
.section
->owner
!= NULL
5269 && (h
->root
.u
.def
.section
->owner
->flags
5270 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5272 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5273 if (normal_align
> symbol_align
)
5274 normal_align
= symbol_align
;
5277 normal_align
= symbol_align
;
5281 common_align
= old_alignment
;
5282 common_bfd
= old_bfd
;
5287 common_align
= bfd_log2 (isym
->st_value
);
5289 normal_bfd
= old_bfd
;
5292 if (normal_align
< common_align
)
5294 /* PR binutils/2735 */
5295 if (normal_bfd
== NULL
)
5297 /* xgettext:c-format */
5298 (_("warning: alignment %u of common symbol `%s' in %pB is"
5299 " greater than the alignment (%u) of its section %pA"),
5300 1 << common_align
, name
, common_bfd
,
5301 1 << normal_align
, h
->root
.u
.def
.section
);
5304 /* xgettext:c-format */
5305 (_("warning: alignment %u of symbol `%s' in %pB"
5306 " is smaller than %u in %pB"),
5307 1 << normal_align
, name
, normal_bfd
,
5308 1 << common_align
, common_bfd
);
5312 /* Remember the symbol size if it isn't undefined. */
5313 if (isym
->st_size
!= 0
5314 && isym
->st_shndx
!= SHN_UNDEF
5315 && (definition
|| h
->size
== 0))
5318 && h
->size
!= isym
->st_size
5319 && ! size_change_ok
)
5321 /* xgettext:c-format */
5322 (_("warning: size of symbol `%s' changed"
5323 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5324 name
, (uint64_t) h
->size
, old_bfd
,
5325 (uint64_t) isym
->st_size
, abfd
);
5327 h
->size
= isym
->st_size
;
5330 /* If this is a common symbol, then we always want H->SIZE
5331 to be the size of the common symbol. The code just above
5332 won't fix the size if a common symbol becomes larger. We
5333 don't warn about a size change here, because that is
5334 covered by --warn-common. Allow changes between different
5336 if (h
->root
.type
== bfd_link_hash_common
)
5337 h
->size
= h
->root
.u
.c
.size
;
5339 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5340 && ((definition
&& !new_weak
)
5341 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5342 || h
->type
== STT_NOTYPE
))
5344 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5346 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5348 if (type
== STT_GNU_IFUNC
5349 && (abfd
->flags
& DYNAMIC
) != 0)
5352 if (h
->type
!= type
)
5354 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5355 /* xgettext:c-format */
5357 (_("warning: type of symbol `%s' changed"
5358 " from %d to %d in %pB"),
5359 name
, h
->type
, type
, abfd
);
5365 /* Merge st_other field. */
5366 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5367 definition
, dynamic
);
5369 /* We don't want to make debug symbol dynamic. */
5371 && (sec
->flags
& SEC_DEBUGGING
)
5372 && !bfd_link_relocatable (info
))
5375 /* Nor should we make plugin symbols dynamic. */
5376 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5381 h
->target_internal
= isym
->st_target_internal
;
5382 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5385 if (definition
&& !dynamic
)
5387 char *p
= strchr (name
, ELF_VER_CHR
);
5388 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5390 /* Queue non-default versions so that .symver x, x@FOO
5391 aliases can be checked. */
5394 size_t amt
= ((isymend
- isym
+ 1)
5395 * sizeof (struct elf_link_hash_entry
*));
5397 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5399 goto error_free_vers
;
5401 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5405 if (dynsym
&& h
->dynindx
== -1)
5407 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5408 goto error_free_vers
;
5410 && weakdef (h
)->dynindx
== -1)
5412 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5413 goto error_free_vers
;
5416 else if (h
->dynindx
!= -1)
5417 /* If the symbol already has a dynamic index, but
5418 visibility says it should not be visible, turn it into
5420 switch (ELF_ST_VISIBILITY (h
->other
))
5424 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
5432 && h
->root
.type
!= bfd_link_hash_indirect
5434 && h
->ref_regular_nonweak
)
5436 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5437 && h
->ref_ir_nonweak
5438 && !info
->lto_all_symbols_read
)
5439 || (h
->ref_dynamic_nonweak
5440 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5441 && !on_needed_list (elf_dt_name (abfd
),
5442 htab
->needed
, NULL
))))
5444 const char *soname
= elf_dt_name (abfd
);
5446 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5447 h
->root
.root
.string
);
5449 /* A symbol from a library loaded via DT_NEEDED of some
5450 other library is referenced by a regular object.
5451 Add a DT_NEEDED entry for it. Issue an error if
5452 --no-add-needed is used and the reference was not
5455 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5458 /* xgettext:c-format */
5459 (_("%pB: undefined reference to symbol '%s'"),
5461 bfd_set_error (bfd_error_missing_dso
);
5462 goto error_free_vers
;
5465 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5466 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5468 /* Create dynamic sections for backends that require
5469 that be done before setup_gnu_properties. */
5470 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5477 if (info
->lto_plugin_active
5478 && !bfd_link_relocatable (info
)
5479 && (abfd
->flags
& BFD_PLUGIN
) == 0
5485 if (bed
->s
->arch_size
== 32)
5490 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5491 referenced in regular objects so that linker plugin will get
5492 the correct symbol resolution. */
5494 sym_hash
= elf_sym_hashes (abfd
);
5495 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5497 Elf_Internal_Rela
*internal_relocs
;
5498 Elf_Internal_Rela
*rel
, *relend
;
5500 /* Don't check relocations in excluded sections. */
5501 if ((s
->flags
& SEC_RELOC
) == 0
5502 || s
->reloc_count
== 0
5503 || (s
->flags
& SEC_EXCLUDE
) != 0
5504 || ((info
->strip
== strip_all
5505 || info
->strip
== strip_debugger
)
5506 && (s
->flags
& SEC_DEBUGGING
) != 0))
5509 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
5512 _bfd_link_keep_memory (info
));
5513 if (internal_relocs
== NULL
)
5514 goto error_free_vers
;
5516 rel
= internal_relocs
;
5517 relend
= rel
+ s
->reloc_count
;
5518 for ( ; rel
< relend
; rel
++)
5520 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5521 struct elf_link_hash_entry
*h
;
5523 /* Skip local symbols. */
5524 if (r_symndx
< extsymoff
)
5527 h
= sym_hash
[r_symndx
- extsymoff
];
5529 h
->root
.non_ir_ref_regular
= 1;
5532 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5533 free (internal_relocs
);
5542 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5546 /* Restore the symbol table. */
5547 old_ent
= (char *) old_tab
+ tabsize
;
5548 memset (elf_sym_hashes (abfd
), 0,
5549 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5550 htab
->root
.table
.table
= old_table
;
5551 htab
->root
.table
.size
= old_size
;
5552 htab
->root
.table
.count
= old_count
;
5553 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5554 htab
->root
.undefs
= old_undefs
;
5555 htab
->root
.undefs_tail
= old_undefs_tail
;
5556 if (htab
->dynstr
!= NULL
)
5557 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5560 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5562 struct bfd_hash_entry
*p
;
5563 struct elf_link_hash_entry
*h
;
5564 unsigned int non_ir_ref_dynamic
;
5566 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5568 /* Preserve non_ir_ref_dynamic so that this symbol
5569 will be exported when the dynamic lib becomes needed
5570 in the second pass. */
5571 h
= (struct elf_link_hash_entry
*) p
;
5572 if (h
->root
.type
== bfd_link_hash_warning
)
5573 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5574 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5576 h
= (struct elf_link_hash_entry
*) p
;
5577 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5578 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5579 if (h
->root
.type
== bfd_link_hash_warning
)
5581 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5582 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5583 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5585 if (h
->root
.type
== bfd_link_hash_common
)
5587 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5588 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5590 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5594 /* Make a special call to the linker "notice" function to
5595 tell it that symbols added for crefs may need to be removed. */
5596 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5597 goto error_free_vers
;
5600 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5602 free (nondeflt_vers
);
5606 if (old_tab
!= NULL
)
5608 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5609 goto error_free_vers
;
5614 /* Now that all the symbols from this input file are created, if
5615 not performing a relocatable link, handle .symver foo, foo@BAR
5616 such that any relocs against foo become foo@BAR. */
5617 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5621 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5623 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5624 char *shortname
, *p
;
5627 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5629 || (h
->root
.type
!= bfd_link_hash_defined
5630 && h
->root
.type
!= bfd_link_hash_defweak
))
5633 amt
= p
- h
->root
.root
.string
;
5634 shortname
= (char *) bfd_malloc (amt
+ 1);
5636 goto error_free_vers
;
5637 memcpy (shortname
, h
->root
.root
.string
, amt
);
5638 shortname
[amt
] = '\0';
5640 hi
= (struct elf_link_hash_entry
*)
5641 bfd_link_hash_lookup (&htab
->root
, shortname
,
5642 false, false, false);
5644 && hi
->root
.type
== h
->root
.type
5645 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5646 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5648 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
5649 hi
->root
.type
= bfd_link_hash_indirect
;
5650 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5651 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5652 sym_hash
= elf_sym_hashes (abfd
);
5654 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5655 if (sym_hash
[symidx
] == hi
)
5657 sym_hash
[symidx
] = h
;
5663 free (nondeflt_vers
);
5664 nondeflt_vers
= NULL
;
5667 /* Now set the alias field correctly for all the weak defined
5668 symbols we found. The only way to do this is to search all the
5669 symbols. Since we only need the information for non functions in
5670 dynamic objects, that's the only time we actually put anything on
5671 the list WEAKS. We need this information so that if a regular
5672 object refers to a symbol defined weakly in a dynamic object, the
5673 real symbol in the dynamic object is also put in the dynamic
5674 symbols; we also must arrange for both symbols to point to the
5675 same memory location. We could handle the general case of symbol
5676 aliasing, but a general symbol alias can only be generated in
5677 assembler code, handling it correctly would be very time
5678 consuming, and other ELF linkers don't handle general aliasing
5682 struct elf_link_hash_entry
**hpp
;
5683 struct elf_link_hash_entry
**hppend
;
5684 struct elf_link_hash_entry
**sorted_sym_hash
;
5685 struct elf_link_hash_entry
*h
;
5686 size_t sym_count
, amt
;
5688 /* Since we have to search the whole symbol list for each weak
5689 defined symbol, search time for N weak defined symbols will be
5690 O(N^2). Binary search will cut it down to O(NlogN). */
5691 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5692 sorted_sym_hash
= bfd_malloc (amt
);
5693 if (sorted_sym_hash
== NULL
)
5695 sym_hash
= sorted_sym_hash
;
5696 hpp
= elf_sym_hashes (abfd
);
5697 hppend
= hpp
+ extsymcount
;
5699 for (; hpp
< hppend
; hpp
++)
5703 && h
->root
.type
== bfd_link_hash_defined
5704 && !bed
->is_function_type (h
->type
))
5712 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5715 while (weaks
!= NULL
)
5717 struct elf_link_hash_entry
*hlook
;
5720 size_t i
, j
, idx
= 0;
5723 weaks
= hlook
->u
.alias
;
5724 hlook
->u
.alias
= NULL
;
5726 if (hlook
->root
.type
!= bfd_link_hash_defined
5727 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5730 slook
= hlook
->root
.u
.def
.section
;
5731 vlook
= hlook
->root
.u
.def
.value
;
5737 bfd_signed_vma vdiff
;
5739 h
= sorted_sym_hash
[idx
];
5740 vdiff
= vlook
- h
->root
.u
.def
.value
;
5747 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5757 /* We didn't find a value/section match. */
5761 /* With multiple aliases, or when the weak symbol is already
5762 strongly defined, we have multiple matching symbols and
5763 the binary search above may land on any of them. Step
5764 one past the matching symbol(s). */
5767 h
= sorted_sym_hash
[idx
];
5768 if (h
->root
.u
.def
.section
!= slook
5769 || h
->root
.u
.def
.value
!= vlook
)
5773 /* Now look back over the aliases. Since we sorted by size
5774 as well as value and section, we'll choose the one with
5775 the largest size. */
5778 h
= sorted_sym_hash
[idx
];
5780 /* Stop if value or section doesn't match. */
5781 if (h
->root
.u
.def
.section
!= slook
5782 || h
->root
.u
.def
.value
!= vlook
)
5784 else if (h
!= hlook
)
5786 struct elf_link_hash_entry
*t
;
5789 hlook
->is_weakalias
= 1;
5791 if (t
->u
.alias
!= NULL
)
5792 while (t
->u
.alias
!= h
)
5796 /* If the weak definition is in the list of dynamic
5797 symbols, make sure the real definition is put
5799 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5801 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5804 free (sorted_sym_hash
);
5809 /* If the real definition is in the list of dynamic
5810 symbols, make sure the weak definition is put
5811 there as well. If we don't do this, then the
5812 dynamic loader might not merge the entries for the
5813 real definition and the weak definition. */
5814 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5816 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5817 goto err_free_sym_hash
;
5824 free (sorted_sym_hash
);
5827 if (bed
->check_directives
5828 && !(*bed
->check_directives
) (abfd
, info
))
5831 /* If this is a non-traditional link, try to optimize the handling
5832 of the .stab/.stabstr sections. */
5834 && ! info
->traditional_format
5835 && is_elf_hash_table (&htab
->root
)
5836 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5840 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5841 if (stabstr
!= NULL
)
5843 bfd_size_type string_offset
= 0;
5846 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5847 if (startswith (stab
->name
, ".stab")
5848 && (!stab
->name
[5] ||
5849 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5850 && (stab
->flags
& SEC_MERGE
) == 0
5851 && !bfd_is_abs_section (stab
->output_section
))
5853 struct bfd_elf_section_data
*secdata
;
5855 secdata
= elf_section_data (stab
);
5856 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5857 stabstr
, &secdata
->sec_info
,
5860 if (secdata
->sec_info
)
5861 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5866 if (dynamic
&& add_needed
)
5868 /* Add this bfd to the loaded list. */
5869 struct elf_link_loaded_list
*n
;
5871 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5875 n
->next
= htab
->dyn_loaded
;
5876 htab
->dyn_loaded
= n
;
5878 if (dynamic
&& !add_needed
5879 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5880 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5887 free (nondeflt_vers
);
5895 /* Return the linker hash table entry of a symbol that might be
5896 satisfied by an archive symbol. Return -1 on error. */
5898 struct bfd_link_hash_entry
*
5899 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5900 struct bfd_link_info
*info
,
5903 struct bfd_link_hash_entry
*h
;
5907 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
5911 /* If this is a default version (the name contains @@), look up the
5912 symbol again with only one `@' as well as without the version.
5913 The effect is that references to the symbol with and without the
5914 version will be matched by the default symbol in the archive. */
5916 p
= strchr (name
, ELF_VER_CHR
);
5917 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5920 /* First check with only one `@'. */
5921 len
= strlen (name
);
5922 copy
= (char *) bfd_alloc (abfd
, len
);
5924 return (struct bfd_link_hash_entry
*) -1;
5926 first
= p
- name
+ 1;
5927 memcpy (copy
, name
, first
);
5928 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5930 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5933 /* We also need to check references to the symbol without the
5935 copy
[first
- 1] = '\0';
5936 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5939 bfd_release (abfd
, copy
);
5943 /* Add symbols from an ELF archive file to the linker hash table. We
5944 don't use _bfd_generic_link_add_archive_symbols because we need to
5945 handle versioned symbols.
5947 Fortunately, ELF archive handling is simpler than that done by
5948 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5949 oddities. In ELF, if we find a symbol in the archive map, and the
5950 symbol is currently undefined, we know that we must pull in that
5953 Unfortunately, we do have to make multiple passes over the symbol
5954 table until nothing further is resolved. */
5957 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5960 unsigned char *included
= NULL
;
5964 const struct elf_backend_data
*bed
;
5965 struct bfd_link_hash_entry
* (*archive_symbol_lookup
)
5966 (bfd
*, struct bfd_link_info
*, const char *);
5968 if (! bfd_has_map (abfd
))
5970 /* An empty archive is a special case. */
5971 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5973 bfd_set_error (bfd_error_no_armap
);
5977 /* Keep track of all symbols we know to be already defined, and all
5978 files we know to be already included. This is to speed up the
5979 second and subsequent passes. */
5980 c
= bfd_ardata (abfd
)->symdef_count
;
5983 amt
= c
* sizeof (*included
);
5984 included
= (unsigned char *) bfd_zmalloc (amt
);
5985 if (included
== NULL
)
5988 symdefs
= bfd_ardata (abfd
)->symdefs
;
5989 bed
= get_elf_backend_data (abfd
);
5990 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
6003 symdefend
= symdef
+ c
;
6004 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
6006 struct bfd_link_hash_entry
*h
;
6008 struct bfd_link_hash_entry
*undefs_tail
;
6013 if (symdef
->file_offset
== last
)
6019 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
6020 if (h
== (struct bfd_link_hash_entry
*) -1)
6026 if (h
->type
== bfd_link_hash_undefined
)
6028 /* If the archive element has already been loaded then one
6029 of the symbols defined by that element might have been
6030 made undefined due to being in a discarded section. */
6031 if (is_elf_hash_table (info
->hash
)
6032 && ((struct elf_link_hash_entry
*) h
)->indx
== -3)
6035 else if (h
->type
== bfd_link_hash_common
)
6037 /* We currently have a common symbol. The archive map contains
6038 a reference to this symbol, so we may want to include it. We
6039 only want to include it however, if this archive element
6040 contains a definition of the symbol, not just another common
6043 Unfortunately some archivers (including GNU ar) will put
6044 declarations of common symbols into their archive maps, as
6045 well as real definitions, so we cannot just go by the archive
6046 map alone. Instead we must read in the element's symbol
6047 table and check that to see what kind of symbol definition
6049 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
6054 if (h
->type
!= bfd_link_hash_undefweak
)
6055 /* Symbol must be defined. Don't check it again. */
6060 /* We need to include this archive member. */
6061 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
,
6063 if (element
== NULL
)
6066 if (! bfd_check_format (element
, bfd_object
))
6069 undefs_tail
= info
->hash
->undefs_tail
;
6071 if (!(*info
->callbacks
6072 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
6074 if (!bfd_link_add_symbols (element
, info
))
6077 /* If there are any new undefined symbols, we need to make
6078 another pass through the archive in order to see whether
6079 they can be defined. FIXME: This isn't perfect, because
6080 common symbols wind up on undefs_tail and because an
6081 undefined symbol which is defined later on in this pass
6082 does not require another pass. This isn't a bug, but it
6083 does make the code less efficient than it could be. */
6084 if (undefs_tail
!= info
->hash
->undefs_tail
)
6087 /* Look backward to mark all symbols from this object file
6088 which we have already seen in this pass. */
6092 included
[mark
] = true;
6097 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
6099 /* We mark subsequent symbols from this object file as we go
6100 on through the loop. */
6101 last
= symdef
->file_offset
;
6114 /* Given an ELF BFD, add symbols to the global hash table as
6118 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6120 switch (bfd_get_format (abfd
))
6123 return elf_link_add_object_symbols (abfd
, info
);
6125 return elf_link_add_archive_symbols (abfd
, info
);
6127 bfd_set_error (bfd_error_wrong_format
);
6132 struct hash_codes_info
6134 unsigned long *hashcodes
;
6138 /* This function will be called though elf_link_hash_traverse to store
6139 all hash value of the exported symbols in an array. */
6142 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6144 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
6149 /* Ignore indirect symbols. These are added by the versioning code. */
6150 if (h
->dynindx
== -1)
6153 name
= h
->root
.root
.string
;
6154 if (h
->versioned
>= versioned
)
6156 char *p
= strchr (name
, ELF_VER_CHR
);
6159 alc
= (char *) bfd_malloc (p
- name
+ 1);
6165 memcpy (alc
, name
, p
- name
);
6166 alc
[p
- name
] = '\0';
6171 /* Compute the hash value. */
6172 ha
= bfd_elf_hash (name
);
6174 /* Store the found hash value in the array given as the argument. */
6175 *(inf
->hashcodes
)++ = ha
;
6177 /* And store it in the struct so that we can put it in the hash table
6179 h
->u
.elf_hash_value
= ha
;
6185 struct collect_gnu_hash_codes
6188 const struct elf_backend_data
*bed
;
6189 unsigned long int nsyms
;
6190 unsigned long int maskbits
;
6191 unsigned long int *hashcodes
;
6192 unsigned long int *hashval
;
6193 unsigned long int *indx
;
6194 unsigned long int *counts
;
6198 long int min_dynindx
;
6199 unsigned long int bucketcount
;
6200 unsigned long int symindx
;
6201 long int local_indx
;
6202 long int shift1
, shift2
;
6203 unsigned long int mask
;
6207 /* This function will be called though elf_link_hash_traverse to store
6208 all hash value of the exported symbols in an array. */
6211 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6213 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6218 /* Ignore indirect symbols. These are added by the versioning code. */
6219 if (h
->dynindx
== -1)
6222 /* Ignore also local symbols and undefined symbols. */
6223 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6226 name
= h
->root
.root
.string
;
6227 if (h
->versioned
>= versioned
)
6229 char *p
= strchr (name
, ELF_VER_CHR
);
6232 alc
= (char *) bfd_malloc (p
- name
+ 1);
6238 memcpy (alc
, name
, p
- name
);
6239 alc
[p
- name
] = '\0';
6244 /* Compute the hash value. */
6245 ha
= bfd_elf_gnu_hash (name
);
6247 /* Store the found hash value in the array for compute_bucket_count,
6248 and also for .dynsym reordering purposes. */
6249 s
->hashcodes
[s
->nsyms
] = ha
;
6250 s
->hashval
[h
->dynindx
] = ha
;
6252 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6253 s
->min_dynindx
= h
->dynindx
;
6259 /* This function will be called though elf_link_hash_traverse to do
6260 final dynamic symbol renumbering in case of .gnu.hash.
6261 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6262 to the translation table. */
6265 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6267 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6268 unsigned long int bucket
;
6269 unsigned long int val
;
6271 /* Ignore indirect symbols. */
6272 if (h
->dynindx
== -1)
6275 /* Ignore also local symbols and undefined symbols. */
6276 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6278 if (h
->dynindx
>= s
->min_dynindx
)
6280 if (s
->bed
->record_xhash_symbol
!= NULL
)
6282 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6286 h
->dynindx
= s
->local_indx
++;
6291 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6292 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6293 & ((s
->maskbits
>> s
->shift1
) - 1);
6294 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6296 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6297 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6298 if (s
->counts
[bucket
] == 1)
6299 /* Last element terminates the chain. */
6301 bfd_put_32 (s
->output_bfd
, val
,
6302 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6303 --s
->counts
[bucket
];
6304 if (s
->bed
->record_xhash_symbol
!= NULL
)
6306 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6308 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6311 h
->dynindx
= s
->indx
[bucket
]++;
6315 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6318 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6320 return !(h
->forced_local
6321 || h
->root
.type
== bfd_link_hash_undefined
6322 || h
->root
.type
== bfd_link_hash_undefweak
6323 || ((h
->root
.type
== bfd_link_hash_defined
6324 || h
->root
.type
== bfd_link_hash_defweak
)
6325 && h
->root
.u
.def
.section
->output_section
== NULL
));
6328 /* Array used to determine the number of hash table buckets to use
6329 based on the number of symbols there are. If there are fewer than
6330 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6331 fewer than 37 we use 17 buckets, and so forth. We never use more
6332 than 32771 buckets. */
6334 static const size_t elf_buckets
[] =
6336 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6340 /* Compute bucket count for hashing table. We do not use a static set
6341 of possible tables sizes anymore. Instead we determine for all
6342 possible reasonable sizes of the table the outcome (i.e., the
6343 number of collisions etc) and choose the best solution. The
6344 weighting functions are not too simple to allow the table to grow
6345 without bounds. Instead one of the weighting factors is the size.
6346 Therefore the result is always a good payoff between few collisions
6347 (= short chain lengths) and table size. */
6349 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6350 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6351 unsigned long int nsyms
,
6354 size_t best_size
= 0;
6355 unsigned long int i
;
6357 /* We have a problem here. The following code to optimize the table
6358 size requires an integer type with more the 32 bits. If
6359 BFD_HOST_U_64_BIT is set we know about such a type. */
6360 #ifdef BFD_HOST_U_64_BIT
6365 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
6366 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6367 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6368 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6369 unsigned long int *counts
;
6371 unsigned int no_improvement_count
= 0;
6373 /* Possible optimization parameters: if we have NSYMS symbols we say
6374 that the hashing table must at least have NSYMS/4 and at most
6376 minsize
= nsyms
/ 4;
6379 best_size
= maxsize
= nsyms
* 2;
6384 if ((best_size
& 31) == 0)
6388 /* Create array where we count the collisions in. We must use bfd_malloc
6389 since the size could be large. */
6391 amt
*= sizeof (unsigned long int);
6392 counts
= (unsigned long int *) bfd_malloc (amt
);
6396 /* Compute the "optimal" size for the hash table. The criteria is a
6397 minimal chain length. The minor criteria is (of course) the size
6399 for (i
= minsize
; i
< maxsize
; ++i
)
6401 /* Walk through the array of hashcodes and count the collisions. */
6402 BFD_HOST_U_64_BIT max
;
6403 unsigned long int j
;
6404 unsigned long int fact
;
6406 if (gnu_hash
&& (i
& 31) == 0)
6409 memset (counts
, '\0', i
* sizeof (unsigned long int));
6411 /* Determine how often each hash bucket is used. */
6412 for (j
= 0; j
< nsyms
; ++j
)
6413 ++counts
[hashcodes
[j
] % i
];
6415 /* For the weight function we need some information about the
6416 pagesize on the target. This is information need not be 100%
6417 accurate. Since this information is not available (so far) we
6418 define it here to a reasonable default value. If it is crucial
6419 to have a better value some day simply define this value. */
6420 # ifndef BFD_TARGET_PAGESIZE
6421 # define BFD_TARGET_PAGESIZE (4096)
6424 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6426 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6429 /* Variant 1: optimize for short chains. We add the squares
6430 of all the chain lengths (which favors many small chain
6431 over a few long chains). */
6432 for (j
= 0; j
< i
; ++j
)
6433 max
+= counts
[j
] * counts
[j
];
6435 /* This adds penalties for the overall size of the table. */
6436 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6439 /* Variant 2: Optimize a lot more for small table. Here we
6440 also add squares of the size but we also add penalties for
6441 empty slots (the +1 term). */
6442 for (j
= 0; j
< i
; ++j
)
6443 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6445 /* The overall size of the table is considered, but not as
6446 strong as in variant 1, where it is squared. */
6447 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6451 /* Compare with current best results. */
6452 if (max
< best_chlen
)
6456 no_improvement_count
= 0;
6458 /* PR 11843: Avoid futile long searches for the best bucket size
6459 when there are a large number of symbols. */
6460 else if (++no_improvement_count
== 100)
6467 #endif /* defined (BFD_HOST_U_64_BIT) */
6469 /* This is the fallback solution if no 64bit type is available or if we
6470 are not supposed to spend much time on optimizations. We select the
6471 bucket count using a fixed set of numbers. */
6472 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6474 best_size
= elf_buckets
[i
];
6475 if (nsyms
< elf_buckets
[i
+ 1])
6478 if (gnu_hash
&& best_size
< 2)
6485 /* Size any SHT_GROUP section for ld -r. */
6488 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6493 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6494 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6495 && (s
= ibfd
->sections
) != NULL
6496 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6497 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6502 /* Set a default stack segment size. The value in INFO wins. If it
6503 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6504 undefined it is initialized. */
6507 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6508 struct bfd_link_info
*info
,
6509 const char *legacy_symbol
,
6510 bfd_vma default_size
)
6512 struct elf_link_hash_entry
*h
= NULL
;
6514 /* Look for legacy symbol. */
6516 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6517 false, false, false);
6518 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6519 || h
->root
.type
== bfd_link_hash_defweak
)
6521 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6523 /* The symbol has no type if specified on the command line. */
6524 h
->type
= STT_OBJECT
;
6525 if (info
->stacksize
)
6526 /* xgettext:c-format */
6527 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6528 output_bfd
, legacy_symbol
);
6529 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6530 /* xgettext:c-format */
6531 _bfd_error_handler (_("%pB: %s not absolute"),
6532 output_bfd
, legacy_symbol
);
6534 info
->stacksize
= h
->root
.u
.def
.value
;
6537 if (!info
->stacksize
)
6538 /* If the user didn't set a size, or explicitly inhibit the
6539 size, set it now. */
6540 info
->stacksize
= default_size
;
6542 /* Provide the legacy symbol, if it is referenced. */
6543 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6544 || h
->root
.type
== bfd_link_hash_undefweak
))
6546 struct bfd_link_hash_entry
*bh
= NULL
;
6548 if (!(_bfd_generic_link_add_one_symbol
6549 (info
, output_bfd
, legacy_symbol
,
6550 BSF_GLOBAL
, bfd_abs_section_ptr
,
6551 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6552 NULL
, false, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6555 h
= (struct elf_link_hash_entry
*) bh
;
6557 h
->type
= STT_OBJECT
;
6563 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6565 struct elf_gc_sweep_symbol_info
6567 struct bfd_link_info
*info
;
6568 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6573 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6576 && (((h
->root
.type
== bfd_link_hash_defined
6577 || h
->root
.type
== bfd_link_hash_defweak
)
6578 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6579 && h
->root
.u
.def
.section
->gc_mark
))
6580 || h
->root
.type
== bfd_link_hash_undefined
6581 || h
->root
.type
== bfd_link_hash_undefweak
))
6583 struct elf_gc_sweep_symbol_info
*inf
;
6585 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6586 (*inf
->hide_symbol
) (inf
->info
, h
, true);
6589 h
->ref_regular_nonweak
= 0;
6595 /* Set up the sizes and contents of the ELF dynamic sections. This is
6596 called by the ELF linker emulation before_allocation routine. We
6597 must set the sizes of the sections before the linker sets the
6598 addresses of the various sections. */
6601 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6604 const char *filter_shlib
,
6606 const char *depaudit
,
6607 const char * const *auxiliary_filters
,
6608 struct bfd_link_info
*info
,
6609 asection
**sinterpptr
)
6612 const struct elf_backend_data
*bed
;
6616 if (!is_elf_hash_table (info
->hash
))
6619 /* Any syms created from now on start with -1 in
6620 got.refcount/offset and plt.refcount/offset. */
6621 elf_hash_table (info
)->init_got_refcount
6622 = elf_hash_table (info
)->init_got_offset
;
6623 elf_hash_table (info
)->init_plt_refcount
6624 = elf_hash_table (info
)->init_plt_offset
;
6626 bed
= get_elf_backend_data (output_bfd
);
6628 /* The backend may have to create some sections regardless of whether
6629 we're dynamic or not. */
6630 if (bed
->elf_backend_always_size_sections
6631 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6634 dynobj
= elf_hash_table (info
)->dynobj
;
6636 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6638 struct bfd_elf_version_tree
*verdefs
;
6639 struct elf_info_failed asvinfo
;
6640 struct bfd_elf_version_tree
*t
;
6641 struct bfd_elf_version_expr
*d
;
6645 /* If we are supposed to export all symbols into the dynamic symbol
6646 table (this is not the normal case), then do so. */
6647 if (info
->export_dynamic
6648 || (bfd_link_executable (info
) && info
->dynamic
))
6650 struct elf_info_failed eif
;
6654 elf_link_hash_traverse (elf_hash_table (info
),
6655 _bfd_elf_export_symbol
,
6663 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6665 if (soname_indx
== (size_t) -1
6666 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6670 soname_indx
= (size_t) -1;
6672 /* Make all global versions with definition. */
6673 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6674 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6675 if (!d
->symver
&& d
->literal
)
6677 const char *verstr
, *name
;
6678 size_t namelen
, verlen
, newlen
;
6679 char *newname
, *p
, leading_char
;
6680 struct elf_link_hash_entry
*newh
;
6682 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6684 namelen
= strlen (name
) + (leading_char
!= '\0');
6686 verlen
= strlen (verstr
);
6687 newlen
= namelen
+ verlen
+ 3;
6689 newname
= (char *) bfd_malloc (newlen
);
6690 if (newname
== NULL
)
6692 newname
[0] = leading_char
;
6693 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6695 /* Check the hidden versioned definition. */
6696 p
= newname
+ namelen
;
6698 memcpy (p
, verstr
, verlen
+ 1);
6699 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6700 newname
, false, false,
6703 || (newh
->root
.type
!= bfd_link_hash_defined
6704 && newh
->root
.type
!= bfd_link_hash_defweak
))
6706 /* Check the default versioned definition. */
6708 memcpy (p
, verstr
, verlen
+ 1);
6709 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6710 newname
, false, false,
6715 /* Mark this version if there is a definition and it is
6716 not defined in a shared object. */
6718 && !newh
->def_dynamic
6719 && (newh
->root
.type
== bfd_link_hash_defined
6720 || newh
->root
.type
== bfd_link_hash_defweak
))
6724 /* Attach all the symbols to their version information. */
6725 asvinfo
.info
= info
;
6726 asvinfo
.failed
= false;
6728 elf_link_hash_traverse (elf_hash_table (info
),
6729 _bfd_elf_link_assign_sym_version
,
6734 if (!info
->allow_undefined_version
)
6736 /* Check if all global versions have a definition. */
6737 bool all_defined
= true;
6738 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6739 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6740 if (d
->literal
&& !d
->symver
&& !d
->script
)
6743 (_("%s: undefined version: %s"),
6744 d
->pattern
, t
->name
);
6745 all_defined
= false;
6750 bfd_set_error (bfd_error_bad_value
);
6755 /* Set up the version definition section. */
6756 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6757 BFD_ASSERT (s
!= NULL
);
6759 /* We may have created additional version definitions if we are
6760 just linking a regular application. */
6761 verdefs
= info
->version_info
;
6763 /* Skip anonymous version tag. */
6764 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6765 verdefs
= verdefs
->next
;
6767 if (verdefs
== NULL
&& !info
->create_default_symver
)
6768 s
->flags
|= SEC_EXCLUDE
;
6774 Elf_Internal_Verdef def
;
6775 Elf_Internal_Verdaux defaux
;
6776 struct bfd_link_hash_entry
*bh
;
6777 struct elf_link_hash_entry
*h
;
6783 /* Make space for the base version. */
6784 size
+= sizeof (Elf_External_Verdef
);
6785 size
+= sizeof (Elf_External_Verdaux
);
6788 /* Make space for the default version. */
6789 if (info
->create_default_symver
)
6791 size
+= sizeof (Elf_External_Verdef
);
6795 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6797 struct bfd_elf_version_deps
*n
;
6799 /* Don't emit base version twice. */
6803 size
+= sizeof (Elf_External_Verdef
);
6804 size
+= sizeof (Elf_External_Verdaux
);
6807 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6808 size
+= sizeof (Elf_External_Verdaux
);
6812 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6813 if (s
->contents
== NULL
&& s
->size
!= 0)
6816 /* Fill in the version definition section. */
6820 def
.vd_version
= VER_DEF_CURRENT
;
6821 def
.vd_flags
= VER_FLG_BASE
;
6824 if (info
->create_default_symver
)
6826 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6827 def
.vd_next
= sizeof (Elf_External_Verdef
);
6831 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6832 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6833 + sizeof (Elf_External_Verdaux
));
6836 if (soname_indx
!= (size_t) -1)
6838 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6840 def
.vd_hash
= bfd_elf_hash (soname
);
6841 defaux
.vda_name
= soname_indx
;
6848 name
= lbasename (bfd_get_filename (output_bfd
));
6849 def
.vd_hash
= bfd_elf_hash (name
);
6850 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6852 if (indx
== (size_t) -1)
6854 defaux
.vda_name
= indx
;
6856 defaux
.vda_next
= 0;
6858 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6859 (Elf_External_Verdef
*) p
);
6860 p
+= sizeof (Elf_External_Verdef
);
6861 if (info
->create_default_symver
)
6863 /* Add a symbol representing this version. */
6865 if (! (_bfd_generic_link_add_one_symbol
6866 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6868 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6870 h
= (struct elf_link_hash_entry
*) bh
;
6873 h
->type
= STT_OBJECT
;
6874 h
->verinfo
.vertree
= NULL
;
6876 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6879 /* Create a duplicate of the base version with the same
6880 aux block, but different flags. */
6883 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6885 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6886 + sizeof (Elf_External_Verdaux
));
6889 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6890 (Elf_External_Verdef
*) p
);
6891 p
+= sizeof (Elf_External_Verdef
);
6893 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6894 (Elf_External_Verdaux
*) p
);
6895 p
+= sizeof (Elf_External_Verdaux
);
6897 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6900 struct bfd_elf_version_deps
*n
;
6902 /* Don't emit the base version twice. */
6907 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6910 /* Add a symbol representing this version. */
6912 if (! (_bfd_generic_link_add_one_symbol
6913 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6915 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6917 h
= (struct elf_link_hash_entry
*) bh
;
6920 h
->type
= STT_OBJECT
;
6921 h
->verinfo
.vertree
= t
;
6923 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6926 def
.vd_version
= VER_DEF_CURRENT
;
6928 if (t
->globals
.list
== NULL
6929 && t
->locals
.list
== NULL
6931 def
.vd_flags
|= VER_FLG_WEAK
;
6932 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6933 def
.vd_cnt
= cdeps
+ 1;
6934 def
.vd_hash
= bfd_elf_hash (t
->name
);
6935 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6938 /* If a basever node is next, it *must* be the last node in
6939 the chain, otherwise Verdef construction breaks. */
6940 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6941 BFD_ASSERT (t
->next
->next
== NULL
);
6943 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6944 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6945 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6947 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6948 (Elf_External_Verdef
*) p
);
6949 p
+= sizeof (Elf_External_Verdef
);
6951 defaux
.vda_name
= h
->dynstr_index
;
6952 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6954 defaux
.vda_next
= 0;
6955 if (t
->deps
!= NULL
)
6956 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6957 t
->name_indx
= defaux
.vda_name
;
6959 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6960 (Elf_External_Verdaux
*) p
);
6961 p
+= sizeof (Elf_External_Verdaux
);
6963 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6965 if (n
->version_needed
== NULL
)
6967 /* This can happen if there was an error in the
6969 defaux
.vda_name
= 0;
6973 defaux
.vda_name
= n
->version_needed
->name_indx
;
6974 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6977 if (n
->next
== NULL
)
6978 defaux
.vda_next
= 0;
6980 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6982 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6983 (Elf_External_Verdaux
*) p
);
6984 p
+= sizeof (Elf_External_Verdaux
);
6988 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6992 if (info
->gc_sections
&& bed
->can_gc_sections
)
6994 struct elf_gc_sweep_symbol_info sweep_info
;
6996 /* Remove the symbols that were in the swept sections from the
6997 dynamic symbol table. */
6998 sweep_info
.info
= info
;
6999 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
7000 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
7004 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7007 struct elf_find_verdep_info sinfo
;
7009 /* Work out the size of the version reference section. */
7011 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
7012 BFD_ASSERT (s
!= NULL
);
7015 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
7016 if (sinfo
.vers
== 0)
7018 sinfo
.failed
= false;
7020 elf_link_hash_traverse (elf_hash_table (info
),
7021 _bfd_elf_link_find_version_dependencies
,
7026 if (info
->enable_dt_relr
)
7028 elf_link_add_dt_relr_dependency (&sinfo
);
7033 if (elf_tdata (output_bfd
)->verref
== NULL
)
7034 s
->flags
|= SEC_EXCLUDE
;
7037 Elf_Internal_Verneed
*vn
;
7042 /* Build the version dependency section. */
7045 for (vn
= elf_tdata (output_bfd
)->verref
;
7047 vn
= vn
->vn_nextref
)
7049 Elf_Internal_Vernaux
*a
;
7051 size
+= sizeof (Elf_External_Verneed
);
7053 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7054 size
+= sizeof (Elf_External_Vernaux
);
7058 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7059 if (s
->contents
== NULL
)
7063 for (vn
= elf_tdata (output_bfd
)->verref
;
7065 vn
= vn
->vn_nextref
)
7068 Elf_Internal_Vernaux
*a
;
7072 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7075 vn
->vn_version
= VER_NEED_CURRENT
;
7077 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7078 elf_dt_name (vn
->vn_bfd
) != NULL
7079 ? elf_dt_name (vn
->vn_bfd
)
7080 : lbasename (bfd_get_filename
7083 if (indx
== (size_t) -1)
7086 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
7087 if (vn
->vn_nextref
== NULL
)
7090 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
7091 + caux
* sizeof (Elf_External_Vernaux
));
7093 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
7094 (Elf_External_Verneed
*) p
);
7095 p
+= sizeof (Elf_External_Verneed
);
7097 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7099 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
7100 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7101 a
->vna_nodename
, false);
7102 if (indx
== (size_t) -1)
7105 if (a
->vna_nextptr
== NULL
)
7108 a
->vna_next
= sizeof (Elf_External_Vernaux
);
7110 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
7111 (Elf_External_Vernaux
*) p
);
7112 p
+= sizeof (Elf_External_Vernaux
);
7116 elf_tdata (output_bfd
)->cverrefs
= crefs
;
7120 if (bfd_link_relocatable (info
)
7121 && !_bfd_elf_size_group_sections (info
))
7124 /* Determine any GNU_STACK segment requirements, after the backend
7125 has had a chance to set a default segment size. */
7126 if (info
->execstack
)
7127 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
7128 else if (info
->noexecstack
)
7129 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
7133 asection
*notesec
= NULL
;
7136 for (inputobj
= info
->input_bfds
;
7138 inputobj
= inputobj
->link
.next
)
7143 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
7145 s
= inputobj
->sections
;
7146 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
7149 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
7152 if (s
->flags
& SEC_CODE
)
7156 else if (bed
->default_execstack
)
7159 if (notesec
|| info
->stacksize
> 0)
7160 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
7161 if (notesec
&& exec
&& bfd_link_relocatable (info
)
7162 && notesec
->output_section
!= bfd_abs_section_ptr
)
7163 notesec
->output_section
->flags
|= SEC_CODE
;
7166 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7168 struct elf_info_failed eif
;
7169 struct elf_link_hash_entry
*h
;
7173 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7174 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7178 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7180 info
->flags
|= DF_SYMBOLIC
;
7188 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7190 if (indx
== (size_t) -1)
7193 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7194 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7198 if (filter_shlib
!= NULL
)
7202 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7203 filter_shlib
, true);
7204 if (indx
== (size_t) -1
7205 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7209 if (auxiliary_filters
!= NULL
)
7211 const char * const *p
;
7213 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7217 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7219 if (indx
== (size_t) -1
7220 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7229 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7231 if (indx
== (size_t) -1
7232 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7236 if (depaudit
!= NULL
)
7240 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7242 if (indx
== (size_t) -1
7243 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7250 /* Find all symbols which were defined in a dynamic object and make
7251 the backend pick a reasonable value for them. */
7252 elf_link_hash_traverse (elf_hash_table (info
),
7253 _bfd_elf_adjust_dynamic_symbol
,
7258 /* Add some entries to the .dynamic section. We fill in some of the
7259 values later, in bfd_elf_final_link, but we must add the entries
7260 now so that we know the final size of the .dynamic section. */
7262 /* If there are initialization and/or finalization functions to
7263 call then add the corresponding DT_INIT/DT_FINI entries. */
7264 h
= (info
->init_function
7265 ? elf_link_hash_lookup (elf_hash_table (info
),
7266 info
->init_function
, false,
7273 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7276 h
= (info
->fini_function
7277 ? elf_link_hash_lookup (elf_hash_table (info
),
7278 info
->fini_function
, false,
7285 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7289 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7290 if (s
!= NULL
&& s
->linker_has_input
)
7292 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7293 if (! bfd_link_executable (info
))
7298 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7299 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7300 && (o
= sub
->sections
) != NULL
7301 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7302 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7303 if (elf_section_data (o
)->this_hdr
.sh_type
7304 == SHT_PREINIT_ARRAY
)
7307 (_("%pB: .preinit_array section is not allowed in DSO"),
7312 bfd_set_error (bfd_error_nonrepresentable_section
);
7316 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7317 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7320 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7321 if (s
!= NULL
&& s
->linker_has_input
)
7323 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7324 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7327 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7328 if (s
!= NULL
&& s
->linker_has_input
)
7330 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7331 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7335 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7336 /* If .dynstr is excluded from the link, we don't want any of
7337 these tags. Strictly, we should be checking each section
7338 individually; This quick check covers for the case where
7339 someone does a /DISCARD/ : { *(*) }. */
7340 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7342 bfd_size_type strsize
;
7344 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7345 if ((info
->emit_hash
7346 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7347 || (info
->emit_gnu_hash
7348 && (bed
->record_xhash_symbol
== NULL
7349 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7350 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7351 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7352 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7353 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7355 || (info
->gnu_flags_1
7356 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_FLAGS_1
,
7357 info
->gnu_flags_1
)))
7362 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7365 /* The backend must work out the sizes of all the other dynamic
7368 && bed
->elf_backend_size_dynamic_sections
!= NULL
7369 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7372 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7374 if (elf_tdata (output_bfd
)->cverdefs
)
7376 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7378 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7379 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7383 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7385 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7388 else if (info
->flags
& DF_BIND_NOW
)
7390 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7396 if (bfd_link_executable (info
))
7397 info
->flags_1
&= ~ (DF_1_INITFIRST
7400 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7404 if (elf_tdata (output_bfd
)->cverrefs
)
7406 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7408 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7409 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7413 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7414 && elf_tdata (output_bfd
)->cverdefs
== 0)
7415 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7419 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7420 s
->flags
|= SEC_EXCLUDE
;
7426 /* Find the first non-excluded output section. We'll use its
7427 section symbol for some emitted relocs. */
7429 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7432 asection
*found
= NULL
;
7434 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7435 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7436 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7439 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7442 elf_hash_table (info
)->text_index_section
= found
;
7445 /* Find two non-excluded output sections, one for code, one for data.
7446 We'll use their section symbols for some emitted relocs. */
7448 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7451 asection
*found
= NULL
;
7453 /* Data first, since setting text_index_section changes
7454 _bfd_elf_omit_section_dynsym_default. */
7455 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7456 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7457 && !(s
->flags
& SEC_READONLY
)
7458 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7461 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7464 elf_hash_table (info
)->data_index_section
= found
;
7466 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7467 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7468 && (s
->flags
& SEC_READONLY
)
7469 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7474 elf_hash_table (info
)->text_index_section
= found
;
7477 #define GNU_HASH_SECTION_NAME(bed) \
7478 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7481 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7483 const struct elf_backend_data
*bed
;
7484 unsigned long section_sym_count
;
7485 bfd_size_type dynsymcount
= 0;
7487 if (!is_elf_hash_table (info
->hash
))
7490 bed
= get_elf_backend_data (output_bfd
);
7491 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7493 /* Assign dynsym indices. In a shared library we generate a section
7494 symbol for each output section, which come first. Next come all
7495 of the back-end allocated local dynamic syms, followed by the rest
7496 of the global symbols.
7498 This is usually not needed for static binaries, however backends
7499 can request to always do it, e.g. the MIPS backend uses dynamic
7500 symbol counts to lay out GOT, which will be produced in the
7501 presence of GOT relocations even in static binaries (holding fixed
7502 data in that case, to satisfy those relocations). */
7504 if (elf_hash_table (info
)->dynamic_sections_created
7505 || bed
->always_renumber_dynsyms
)
7506 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7507 §ion_sym_count
);
7509 if (elf_hash_table (info
)->dynamic_sections_created
)
7513 unsigned int dtagcount
;
7515 dynobj
= elf_hash_table (info
)->dynobj
;
7517 /* Work out the size of the symbol version section. */
7518 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7519 BFD_ASSERT (s
!= NULL
);
7520 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7522 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7523 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7524 if (s
->contents
== NULL
)
7527 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7531 /* Set the size of the .dynsym and .hash sections. We counted
7532 the number of dynamic symbols in elf_link_add_object_symbols.
7533 We will build the contents of .dynsym and .hash when we build
7534 the final symbol table, because until then we do not know the
7535 correct value to give the symbols. We built the .dynstr
7536 section as we went along in elf_link_add_object_symbols. */
7537 s
= elf_hash_table (info
)->dynsym
;
7538 BFD_ASSERT (s
!= NULL
);
7539 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7541 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7542 if (s
->contents
== NULL
)
7545 /* The first entry in .dynsym is a dummy symbol. Clear all the
7546 section syms, in case we don't output them all. */
7547 ++section_sym_count
;
7548 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7550 elf_hash_table (info
)->bucketcount
= 0;
7552 /* Compute the size of the hashing table. As a side effect this
7553 computes the hash values for all the names we export. */
7554 if (info
->emit_hash
)
7556 unsigned long int *hashcodes
;
7557 struct hash_codes_info hashinf
;
7559 unsigned long int nsyms
;
7561 size_t hash_entry_size
;
7563 /* Compute the hash values for all exported symbols. At the same
7564 time store the values in an array so that we could use them for
7566 amt
= dynsymcount
* sizeof (unsigned long int);
7567 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7568 if (hashcodes
== NULL
)
7570 hashinf
.hashcodes
= hashcodes
;
7571 hashinf
.error
= false;
7573 /* Put all hash values in HASHCODES. */
7574 elf_link_hash_traverse (elf_hash_table (info
),
7575 elf_collect_hash_codes
, &hashinf
);
7582 nsyms
= hashinf
.hashcodes
- hashcodes
;
7584 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7587 if (bucketcount
== 0 && nsyms
> 0)
7590 elf_hash_table (info
)->bucketcount
= bucketcount
;
7592 s
= bfd_get_linker_section (dynobj
, ".hash");
7593 BFD_ASSERT (s
!= NULL
);
7594 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7595 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7596 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7597 if (s
->contents
== NULL
)
7600 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7601 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7602 s
->contents
+ hash_entry_size
);
7605 if (info
->emit_gnu_hash
)
7608 unsigned char *contents
;
7609 struct collect_gnu_hash_codes cinfo
;
7613 memset (&cinfo
, 0, sizeof (cinfo
));
7615 /* Compute the hash values for all exported symbols. At the same
7616 time store the values in an array so that we could use them for
7618 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7619 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7620 if (cinfo
.hashcodes
== NULL
)
7623 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7624 cinfo
.min_dynindx
= -1;
7625 cinfo
.output_bfd
= output_bfd
;
7628 /* Put all hash values in HASHCODES. */
7629 elf_link_hash_traverse (elf_hash_table (info
),
7630 elf_collect_gnu_hash_codes
, &cinfo
);
7633 free (cinfo
.hashcodes
);
7638 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7640 if (bucketcount
== 0)
7642 free (cinfo
.hashcodes
);
7646 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7647 BFD_ASSERT (s
!= NULL
);
7649 if (cinfo
.nsyms
== 0)
7651 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7652 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7653 free (cinfo
.hashcodes
);
7654 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7655 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7656 if (contents
== NULL
)
7658 s
->contents
= contents
;
7659 /* 1 empty bucket. */
7660 bfd_put_32 (output_bfd
, 1, contents
);
7661 /* SYMIDX above the special symbol 0. */
7662 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7663 /* Just one word for bitmask. */
7664 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7665 /* Only hash fn bloom filter. */
7666 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7667 /* No hashes are valid - empty bitmask. */
7668 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7669 /* No hashes in the only bucket. */
7670 bfd_put_32 (output_bfd
, 0,
7671 contents
+ 16 + bed
->s
->arch_size
/ 8);
7675 unsigned long int maskwords
, maskbitslog2
, x
;
7676 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7680 while ((x
>>= 1) != 0)
7682 if (maskbitslog2
< 3)
7684 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7685 maskbitslog2
= maskbitslog2
+ 3;
7687 maskbitslog2
= maskbitslog2
+ 2;
7688 if (bed
->s
->arch_size
== 64)
7690 if (maskbitslog2
== 5)
7696 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7697 cinfo
.shift2
= maskbitslog2
;
7698 cinfo
.maskbits
= 1 << maskbitslog2
;
7699 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7700 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7701 amt
+= maskwords
* sizeof (bfd_vma
);
7702 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7703 if (cinfo
.bitmask
== NULL
)
7705 free (cinfo
.hashcodes
);
7709 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7710 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7711 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7712 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7714 /* Determine how often each hash bucket is used. */
7715 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7716 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7717 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7719 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7720 if (cinfo
.counts
[i
] != 0)
7722 cinfo
.indx
[i
] = cnt
;
7723 cnt
+= cinfo
.counts
[i
];
7725 BFD_ASSERT (cnt
== dynsymcount
);
7726 cinfo
.bucketcount
= bucketcount
;
7727 cinfo
.local_indx
= cinfo
.min_dynindx
;
7729 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7730 s
->size
+= cinfo
.maskbits
/ 8;
7731 if (bed
->record_xhash_symbol
!= NULL
)
7732 s
->size
+= cinfo
.nsyms
* 4;
7733 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7734 if (contents
== NULL
)
7736 free (cinfo
.bitmask
);
7737 free (cinfo
.hashcodes
);
7741 s
->contents
= contents
;
7742 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7743 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7744 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7745 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7746 contents
+= 16 + cinfo
.maskbits
/ 8;
7748 for (i
= 0; i
< bucketcount
; ++i
)
7750 if (cinfo
.counts
[i
] == 0)
7751 bfd_put_32 (output_bfd
, 0, contents
);
7753 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7757 cinfo
.contents
= contents
;
7759 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7760 /* Renumber dynamic symbols, if populating .gnu.hash section.
7761 If using .MIPS.xhash, populate the translation table. */
7762 elf_link_hash_traverse (elf_hash_table (info
),
7763 elf_gnu_hash_process_symidx
, &cinfo
);
7765 contents
= s
->contents
+ 16;
7766 for (i
= 0; i
< maskwords
; ++i
)
7768 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7770 contents
+= bed
->s
->arch_size
/ 8;
7773 free (cinfo
.bitmask
);
7774 free (cinfo
.hashcodes
);
7778 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7779 BFD_ASSERT (s
!= NULL
);
7781 elf_finalize_dynstr (output_bfd
, info
);
7783 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7785 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7786 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7793 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7796 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7799 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7800 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7803 /* Finish SHF_MERGE section merging. */
7806 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7811 if (!is_elf_hash_table (info
->hash
))
7814 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7815 if ((ibfd
->flags
& DYNAMIC
) == 0
7816 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7817 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7818 == get_elf_backend_data (obfd
)->s
->elfclass
))
7819 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7820 if ((sec
->flags
& SEC_MERGE
) != 0
7821 && !bfd_is_abs_section (sec
->output_section
))
7823 struct bfd_elf_section_data
*secdata
;
7825 secdata
= elf_section_data (sec
);
7826 if (! _bfd_add_merge_section (obfd
,
7827 &elf_hash_table (info
)->merge_info
,
7828 sec
, &secdata
->sec_info
))
7830 else if (secdata
->sec_info
)
7831 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7834 if (elf_hash_table (info
)->merge_info
!= NULL
)
7835 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7836 merge_sections_remove_hook
);
7840 /* Create an entry in an ELF linker hash table. */
7842 struct bfd_hash_entry
*
7843 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7844 struct bfd_hash_table
*table
,
7847 /* Allocate the structure if it has not already been allocated by a
7851 entry
= (struct bfd_hash_entry
*)
7852 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7857 /* Call the allocation method of the superclass. */
7858 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7861 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7862 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7864 /* Set local fields. */
7867 ret
->got
= htab
->init_got_refcount
;
7868 ret
->plt
= htab
->init_plt_refcount
;
7869 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7870 - offsetof (struct elf_link_hash_entry
, size
)));
7871 /* Assume that we have been called by a non-ELF symbol reader.
7872 This flag is then reset by the code which reads an ELF input
7873 file. This ensures that a symbol created by a non-ELF symbol
7874 reader will have the flag set correctly. */
7881 /* Copy data from an indirect symbol to its direct symbol, hiding the
7882 old indirect symbol. Also used for copying flags to a weakdef. */
7885 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7886 struct elf_link_hash_entry
*dir
,
7887 struct elf_link_hash_entry
*ind
)
7889 struct elf_link_hash_table
*htab
;
7891 if (ind
->dyn_relocs
!= NULL
)
7893 if (dir
->dyn_relocs
!= NULL
)
7895 struct elf_dyn_relocs
**pp
;
7896 struct elf_dyn_relocs
*p
;
7898 /* Add reloc counts against the indirect sym to the direct sym
7899 list. Merge any entries against the same section. */
7900 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
7902 struct elf_dyn_relocs
*q
;
7904 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
7905 if (q
->sec
== p
->sec
)
7907 q
->pc_count
+= p
->pc_count
;
7908 q
->count
+= p
->count
;
7915 *pp
= dir
->dyn_relocs
;
7918 dir
->dyn_relocs
= ind
->dyn_relocs
;
7919 ind
->dyn_relocs
= NULL
;
7922 /* Copy down any references that we may have already seen to the
7923 symbol which just became indirect. */
7925 if (dir
->versioned
!= versioned_hidden
)
7926 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7927 dir
->ref_regular
|= ind
->ref_regular
;
7928 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7929 dir
->non_got_ref
|= ind
->non_got_ref
;
7930 dir
->needs_plt
|= ind
->needs_plt
;
7931 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7933 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7936 /* Copy over the global and procedure linkage table refcount entries.
7937 These may have been already set up by a check_relocs routine. */
7938 htab
= elf_hash_table (info
);
7939 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7941 if (dir
->got
.refcount
< 0)
7942 dir
->got
.refcount
= 0;
7943 dir
->got
.refcount
+= ind
->got
.refcount
;
7944 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7947 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7949 if (dir
->plt
.refcount
< 0)
7950 dir
->plt
.refcount
= 0;
7951 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7952 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7955 if (ind
->dynindx
!= -1)
7957 if (dir
->dynindx
!= -1)
7958 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7959 dir
->dynindx
= ind
->dynindx
;
7960 dir
->dynstr_index
= ind
->dynstr_index
;
7962 ind
->dynstr_index
= 0;
7967 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7968 struct elf_link_hash_entry
*h
,
7971 /* STT_GNU_IFUNC symbol must go through PLT. */
7972 if (h
->type
!= STT_GNU_IFUNC
)
7974 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7979 h
->forced_local
= 1;
7980 if (h
->dynindx
!= -1)
7982 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7985 h
->dynstr_index
= 0;
7990 /* Hide a symbol. */
7993 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
7994 struct bfd_link_info
*info
,
7995 struct bfd_link_hash_entry
*h
)
7997 if (is_elf_hash_table (info
->hash
))
7999 const struct elf_backend_data
*bed
8000 = get_elf_backend_data (output_bfd
);
8001 struct elf_link_hash_entry
*eh
8002 = (struct elf_link_hash_entry
*) h
;
8003 bed
->elf_backend_hide_symbol (info
, eh
, true);
8004 eh
->def_dynamic
= 0;
8005 eh
->ref_dynamic
= 0;
8006 eh
->dynamic_def
= 0;
8010 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8014 _bfd_elf_link_hash_table_init
8015 (struct elf_link_hash_table
*table
,
8017 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
8018 struct bfd_hash_table
*,
8020 unsigned int entsize
,
8021 enum elf_target_id target_id
)
8024 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
8026 table
->init_got_refcount
.refcount
= can_refcount
- 1;
8027 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
8028 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
8029 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
8030 /* The first dynamic symbol is a dummy. */
8031 table
->dynsymcount
= 1;
8033 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
8035 table
->root
.type
= bfd_link_elf_hash_table
;
8036 table
->hash_table_id
= target_id
;
8037 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
8042 /* Create an ELF linker hash table. */
8044 struct bfd_link_hash_table
*
8045 _bfd_elf_link_hash_table_create (bfd
*abfd
)
8047 struct elf_link_hash_table
*ret
;
8048 size_t amt
= sizeof (struct elf_link_hash_table
);
8050 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
8054 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
8055 sizeof (struct elf_link_hash_entry
),
8061 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
8066 /* Destroy an ELF linker hash table. */
8069 _bfd_elf_link_hash_table_free (bfd
*obfd
)
8071 struct elf_link_hash_table
*htab
;
8073 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
8074 if (htab
->dynstr
!= NULL
)
8075 _bfd_elf_strtab_free (htab
->dynstr
);
8076 _bfd_merge_sections_free (htab
->merge_info
);
8077 _bfd_generic_link_hash_table_free (obfd
);
8080 /* This is a hook for the ELF emulation code in the generic linker to
8081 tell the backend linker what file name to use for the DT_NEEDED
8082 entry for a dynamic object. */
8085 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
8087 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8088 && bfd_get_format (abfd
) == bfd_object
)
8089 elf_dt_name (abfd
) = name
;
8093 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
8096 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8097 && bfd_get_format (abfd
) == bfd_object
)
8098 lib_class
= elf_dyn_lib_class (abfd
);
8105 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
8107 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8108 && bfd_get_format (abfd
) == bfd_object
)
8109 elf_dyn_lib_class (abfd
) = lib_class
;
8112 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8113 the linker ELF emulation code. */
8115 struct bfd_link_needed_list
*
8116 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8117 struct bfd_link_info
*info
)
8119 if (! is_elf_hash_table (info
->hash
))
8121 return elf_hash_table (info
)->needed
;
8124 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8125 hook for the linker ELF emulation code. */
8127 struct bfd_link_needed_list
*
8128 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8129 struct bfd_link_info
*info
)
8131 if (! is_elf_hash_table (info
->hash
))
8133 return elf_hash_table (info
)->runpath
;
8136 /* Get the name actually used for a dynamic object for a link. This
8137 is the SONAME entry if there is one. Otherwise, it is the string
8138 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8141 bfd_elf_get_dt_soname (bfd
*abfd
)
8143 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8144 && bfd_get_format (abfd
) == bfd_object
)
8145 return elf_dt_name (abfd
);
8149 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8150 the ELF linker emulation code. */
8153 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
8154 struct bfd_link_needed_list
**pneeded
)
8157 bfd_byte
*dynbuf
= NULL
;
8158 unsigned int elfsec
;
8159 unsigned long shlink
;
8160 bfd_byte
*extdyn
, *extdynend
;
8162 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
8166 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
8167 || bfd_get_format (abfd
) != bfd_object
)
8170 s
= bfd_get_section_by_name (abfd
, ".dynamic");
8171 if (s
== NULL
|| s
->size
== 0)
8174 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
8177 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8178 if (elfsec
== SHN_BAD
)
8181 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8183 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8184 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8187 extdynend
= extdyn
+ s
->size
;
8188 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
8190 Elf_Internal_Dyn dyn
;
8192 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8194 if (dyn
.d_tag
== DT_NULL
)
8197 if (dyn
.d_tag
== DT_NEEDED
)
8200 struct bfd_link_needed_list
*l
;
8201 unsigned int tagv
= dyn
.d_un
.d_val
;
8204 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8209 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8229 struct elf_symbuf_symbol
8231 unsigned long st_name
; /* Symbol name, index in string tbl */
8232 unsigned char st_info
; /* Type and binding attributes */
8233 unsigned char st_other
; /* Visibilty, and target specific */
8236 struct elf_symbuf_head
8238 struct elf_symbuf_symbol
*ssym
;
8240 unsigned int st_shndx
;
8247 Elf_Internal_Sym
*isym
;
8248 struct elf_symbuf_symbol
*ssym
;
8254 /* Sort references to symbols by ascending section number. */
8257 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8259 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8260 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8262 if (s1
->st_shndx
!= s2
->st_shndx
)
8263 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8264 /* Final sort by the address of the sym in the symbuf ensures
8267 return s1
> s2
? 1 : -1;
8272 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8274 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8275 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8276 int ret
= strcmp (s1
->name
, s2
->name
);
8279 if (s1
->u
.p
!= s2
->u
.p
)
8280 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8284 static struct elf_symbuf_head
*
8285 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8287 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8288 struct elf_symbuf_symbol
*ssym
;
8289 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8290 size_t i
, shndx_count
, total_size
, amt
;
8292 amt
= symcount
* sizeof (*indbuf
);
8293 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8297 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8298 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8299 *ind
++ = &isymbuf
[i
];
8302 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8303 elf_sort_elf_symbol
);
8306 if (indbufend
> indbuf
)
8307 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8308 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8311 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8312 + (indbufend
- indbuf
) * sizeof (*ssym
));
8313 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8314 if (ssymbuf
== NULL
)
8320 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8321 ssymbuf
->ssym
= NULL
;
8322 ssymbuf
->count
= shndx_count
;
8323 ssymbuf
->st_shndx
= 0;
8324 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8326 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8329 ssymhead
->ssym
= ssym
;
8330 ssymhead
->count
= 0;
8331 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8333 ssym
->st_name
= (*ind
)->st_name
;
8334 ssym
->st_info
= (*ind
)->st_info
;
8335 ssym
->st_other
= (*ind
)->st_other
;
8338 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8339 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
8346 /* Check if 2 sections define the same set of local and global
8350 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8351 struct bfd_link_info
*info
)
8354 const struct elf_backend_data
*bed1
, *bed2
;
8355 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8356 size_t symcount1
, symcount2
;
8357 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8358 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8359 Elf_Internal_Sym
*isym
, *isymend
;
8360 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8361 size_t count1
, count2
, sec_count1
, sec_count2
, i
;
8362 unsigned int shndx1
, shndx2
;
8364 bool ignore_section_symbol_p
;
8369 /* Both sections have to be in ELF. */
8370 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8371 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8374 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8377 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8378 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8379 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8382 bed1
= get_elf_backend_data (bfd1
);
8383 bed2
= get_elf_backend_data (bfd2
);
8384 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8385 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8386 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8387 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8389 if (symcount1
== 0 || symcount2
== 0)
8395 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8396 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8398 /* Ignore section symbols only when matching non-debugging sections
8399 or linkonce section with comdat section. */
8400 ignore_section_symbol_p
8401 = ((sec1
->flags
& SEC_DEBUGGING
) == 0
8402 || ((elf_section_flags (sec1
) & SHF_GROUP
)
8403 != (elf_section_flags (sec2
) & SHF_GROUP
)));
8405 if (ssymbuf1
== NULL
)
8407 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8409 if (isymbuf1
== NULL
)
8412 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8414 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8415 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8419 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8421 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8423 if (isymbuf2
== NULL
)
8426 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8428 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8429 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8433 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8435 /* Optimized faster version. */
8437 struct elf_symbol
*symp
;
8438 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8441 hi
= ssymbuf1
->count
;
8447 mid
= (lo
+ hi
) / 2;
8448 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8450 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8454 count1
= ssymbuf1
[mid
].count
;
8459 if (ignore_section_symbol_p
)
8461 for (i
= 0; i
< count1
; i
++)
8462 if (ELF_ST_TYPE (ssymbuf1
->ssym
[i
].st_info
) == STT_SECTION
)
8464 count1
-= sec_count1
;
8468 hi
= ssymbuf2
->count
;
8474 mid
= (lo
+ hi
) / 2;
8475 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8477 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8481 count2
= ssymbuf2
[mid
].count
;
8486 if (ignore_section_symbol_p
)
8488 for (i
= 0; i
< count2
; i
++)
8489 if (ELF_ST_TYPE (ssymbuf2
->ssym
[i
].st_info
) == STT_SECTION
)
8491 count2
-= sec_count2
;
8494 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8498 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8500 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8501 if (symtable1
== NULL
|| symtable2
== NULL
)
8505 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
+ sec_count1
;
8506 ssym
< ssymend
; ssym
++)
8508 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8510 symp
->u
.ssym
= ssym
;
8511 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8518 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
+ sec_count2
;
8519 ssym
< ssymend
; ssym
++)
8521 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8523 symp
->u
.ssym
= ssym
;
8524 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8530 /* Sort symbol by name. */
8531 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8532 elf_sym_name_compare
);
8533 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8534 elf_sym_name_compare
);
8536 for (i
= 0; i
< count1
; i
++)
8537 /* Two symbols must have the same binding, type and name. */
8538 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8539 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8540 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8547 symtable1
= (struct elf_symbol
*)
8548 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8549 symtable2
= (struct elf_symbol
*)
8550 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8551 if (symtable1
== NULL
|| symtable2
== NULL
)
8554 /* Count definitions in the section. */
8556 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8557 if (isym
->st_shndx
== shndx1
8558 && (!ignore_section_symbol_p
8559 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8560 symtable1
[count1
++].u
.isym
= isym
;
8563 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8564 if (isym
->st_shndx
== shndx2
8565 && (!ignore_section_symbol_p
8566 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8567 symtable2
[count2
++].u
.isym
= isym
;
8569 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8572 for (i
= 0; i
< count1
; i
++)
8574 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8575 symtable1
[i
].u
.isym
->st_name
);
8577 for (i
= 0; i
< count2
; i
++)
8579 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8580 symtable2
[i
].u
.isym
->st_name
);
8582 /* Sort symbol by name. */
8583 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8584 elf_sym_name_compare
);
8585 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8586 elf_sym_name_compare
);
8588 for (i
= 0; i
< count1
; i
++)
8589 /* Two symbols must have the same binding, type and name. */
8590 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8591 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8592 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8606 /* Return TRUE if 2 section types are compatible. */
8609 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8610 bfd
*bbfd
, const asection
*bsec
)
8614 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8615 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8618 return elf_section_type (asec
) == elf_section_type (bsec
);
8621 /* Final phase of ELF linker. */
8623 /* A structure we use to avoid passing large numbers of arguments. */
8625 struct elf_final_link_info
8627 /* General link information. */
8628 struct bfd_link_info
*info
;
8631 /* Symbol string table. */
8632 struct elf_strtab_hash
*symstrtab
;
8633 /* .hash section. */
8635 /* symbol version section (.gnu.version). */
8636 asection
*symver_sec
;
8637 /* Buffer large enough to hold contents of any section. */
8639 /* Buffer large enough to hold external relocs of any section. */
8640 void *external_relocs
;
8641 /* Buffer large enough to hold internal relocs of any section. */
8642 Elf_Internal_Rela
*internal_relocs
;
8643 /* Buffer large enough to hold external local symbols of any input
8645 bfd_byte
*external_syms
;
8646 /* And a buffer for symbol section indices. */
8647 Elf_External_Sym_Shndx
*locsym_shndx
;
8648 /* Buffer large enough to hold internal local symbols of any input
8650 Elf_Internal_Sym
*internal_syms
;
8651 /* Array large enough to hold a symbol index for each local symbol
8652 of any input BFD. */
8654 /* Array large enough to hold a section pointer for each local
8655 symbol of any input BFD. */
8656 asection
**sections
;
8657 /* Buffer for SHT_SYMTAB_SHNDX section. */
8658 Elf_External_Sym_Shndx
*symshndxbuf
;
8659 /* Number of STT_FILE syms seen. */
8660 size_t filesym_count
;
8661 /* Local symbol hash table. */
8662 struct bfd_hash_table local_hash_table
;
8665 struct local_hash_entry
8667 /* Base hash table entry structure. */
8668 struct bfd_hash_entry root
;
8669 /* Size of the local symbol name. */
8671 /* Number of the duplicated local symbol names. */
8675 /* Create an entry in the local symbol hash table. */
8677 static struct bfd_hash_entry
*
8678 local_hash_newfunc (struct bfd_hash_entry
*entry
,
8679 struct bfd_hash_table
*table
,
8683 /* Allocate the structure if it has not already been allocated by a
8687 entry
= bfd_hash_allocate (table
,
8688 sizeof (struct local_hash_entry
));
8693 /* Call the allocation method of the superclass. */
8694 entry
= bfd_hash_newfunc (entry
, table
, string
);
8697 ((struct local_hash_entry
*) entry
)->count
= 0;
8698 ((struct local_hash_entry
*) entry
)->size
= 0;
8704 /* This struct is used to pass information to elf_link_output_extsym. */
8706 struct elf_outext_info
8711 struct elf_final_link_info
*flinfo
;
8715 /* Support for evaluating a complex relocation.
8717 Complex relocations are generalized, self-describing relocations. The
8718 implementation of them consists of two parts: complex symbols, and the
8719 relocations themselves.
8721 The relocations use a reserved elf-wide relocation type code (R_RELC
8722 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8723 information (start bit, end bit, word width, etc) into the addend. This
8724 information is extracted from CGEN-generated operand tables within gas.
8726 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8727 internal) representing prefix-notation expressions, including but not
8728 limited to those sorts of expressions normally encoded as addends in the
8729 addend field. The symbol mangling format is:
8732 | <unary-operator> ':' <node>
8733 | <binary-operator> ':' <node> ':' <node>
8736 <literal> := 's' <digits=N> ':' <N character symbol name>
8737 | 'S' <digits=N> ':' <N character section name>
8741 <binary-operator> := as in C
8742 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8745 set_symbol_value (bfd
*bfd_with_globals
,
8746 Elf_Internal_Sym
*isymbuf
,
8751 struct elf_link_hash_entry
**sym_hashes
;
8752 struct elf_link_hash_entry
*h
;
8753 size_t extsymoff
= locsymcount
;
8755 if (symidx
< locsymcount
)
8757 Elf_Internal_Sym
*sym
;
8759 sym
= isymbuf
+ symidx
;
8760 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8762 /* It is a local symbol: move it to the
8763 "absolute" section and give it a value. */
8764 sym
->st_shndx
= SHN_ABS
;
8765 sym
->st_value
= val
;
8768 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8772 /* It is a global symbol: set its link type
8773 to "defined" and give it a value. */
8775 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8776 h
= sym_hashes
[symidx
- extsymoff
];
8777 while (h
->root
.type
== bfd_link_hash_indirect
8778 || h
->root
.type
== bfd_link_hash_warning
)
8779 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8780 h
->root
.type
= bfd_link_hash_defined
;
8781 h
->root
.u
.def
.value
= val
;
8782 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8786 resolve_symbol (const char *name
,
8788 struct elf_final_link_info
*flinfo
,
8790 Elf_Internal_Sym
*isymbuf
,
8793 Elf_Internal_Sym
*sym
;
8794 struct bfd_link_hash_entry
*global_entry
;
8795 const char *candidate
= NULL
;
8796 Elf_Internal_Shdr
*symtab_hdr
;
8799 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8801 for (i
= 0; i
< locsymcount
; ++ i
)
8805 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8808 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8809 symtab_hdr
->sh_link
,
8812 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8813 name
, candidate
, (unsigned long) sym
->st_value
);
8815 if (candidate
&& strcmp (candidate
, name
) == 0)
8817 asection
*sec
= flinfo
->sections
[i
];
8819 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8820 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8822 printf ("Found symbol with value %8.8lx\n",
8823 (unsigned long) *result
);
8829 /* Hmm, haven't found it yet. perhaps it is a global. */
8830 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8831 false, false, true);
8835 if (global_entry
->type
== bfd_link_hash_defined
8836 || global_entry
->type
== bfd_link_hash_defweak
)
8838 *result
= (global_entry
->u
.def
.value
8839 + global_entry
->u
.def
.section
->output_section
->vma
8840 + global_entry
->u
.def
.section
->output_offset
);
8842 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8843 global_entry
->root
.string
, (unsigned long) *result
);
8851 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8852 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8853 names like "foo.end" which is the end address of section "foo". */
8856 resolve_section (const char *name
,
8864 for (curr
= sections
; curr
; curr
= curr
->next
)
8865 if (strcmp (curr
->name
, name
) == 0)
8867 *result
= curr
->vma
;
8871 /* Hmm. still haven't found it. try pseudo-section names. */
8872 /* FIXME: This could be coded more efficiently... */
8873 for (curr
= sections
; curr
; curr
= curr
->next
)
8875 len
= strlen (curr
->name
);
8876 if (len
> strlen (name
))
8879 if (strncmp (curr
->name
, name
, len
) == 0)
8881 if (startswith (name
+ len
, ".end"))
8883 *result
= (curr
->vma
8884 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
8888 /* Insert more pseudo-section names here, if you like. */
8896 undefined_reference (const char *reftype
, const char *name
)
8898 /* xgettext:c-format */
8899 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8901 bfd_set_error (bfd_error_bad_value
);
8905 eval_symbol (bfd_vma
*result
,
8908 struct elf_final_link_info
*flinfo
,
8910 Elf_Internal_Sym
*isymbuf
,
8919 const char *sym
= *symp
;
8921 bool symbol_is_section
= false;
8926 if (len
< 1 || len
> sizeof (symbuf
))
8928 bfd_set_error (bfd_error_invalid_operation
);
8941 *result
= strtoul (sym
, (char **) symp
, 16);
8945 symbol_is_section
= true;
8949 symlen
= strtol (sym
, (char **) symp
, 10);
8950 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8952 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8954 bfd_set_error (bfd_error_invalid_operation
);
8958 memcpy (symbuf
, sym
, symlen
);
8959 symbuf
[symlen
] = '\0';
8960 *symp
= sym
+ symlen
;
8962 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8963 the symbol as a section, or vice-versa. so we're pretty liberal in our
8964 interpretation here; section means "try section first", not "must be a
8965 section", and likewise with symbol. */
8967 if (symbol_is_section
)
8969 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8970 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8971 isymbuf
, locsymcount
))
8973 undefined_reference ("section", symbuf
);
8979 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8980 isymbuf
, locsymcount
)
8981 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8984 undefined_reference ("symbol", symbuf
);
8991 /* All that remains are operators. */
8993 #define UNARY_OP(op) \
8994 if (startswith (sym, #op)) \
8996 sym += strlen (#op); \
9000 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9001 isymbuf, locsymcount, signed_p)) \
9004 *result = op ((bfd_signed_vma) a); \
9010 #define BINARY_OP_HEAD(op) \
9011 if (startswith (sym, #op)) \
9013 sym += strlen (#op); \
9017 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9018 isymbuf, locsymcount, signed_p)) \
9021 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9022 isymbuf, locsymcount, signed_p)) \
9024 #define BINARY_OP_TAIL(op) \
9026 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9031 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9035 BINARY_OP_HEAD (<<);
9036 if (b
>= sizeof (a
) * CHAR_BIT
)
9042 BINARY_OP_TAIL (<<);
9043 BINARY_OP_HEAD (>>);
9044 if (b
>= sizeof (a
) * CHAR_BIT
)
9046 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
9049 BINARY_OP_TAIL (>>);
9062 _bfd_error_handler (_("division by zero"));
9063 bfd_set_error (bfd_error_bad_value
);
9070 _bfd_error_handler (_("division by zero"));
9071 bfd_set_error (bfd_error_bad_value
);
9084 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
9085 bfd_set_error (bfd_error_invalid_operation
);
9091 put_value (bfd_vma size
,
9092 unsigned long chunksz
,
9097 location
+= (size
- chunksz
);
9099 for (; size
; size
-= chunksz
, location
-= chunksz
)
9104 bfd_put_8 (input_bfd
, x
, location
);
9108 bfd_put_16 (input_bfd
, x
, location
);
9112 bfd_put_32 (input_bfd
, x
, location
);
9113 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9119 bfd_put_64 (input_bfd
, x
, location
);
9120 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9133 get_value (bfd_vma size
,
9134 unsigned long chunksz
,
9141 /* Sanity checks. */
9142 BFD_ASSERT (chunksz
<= sizeof (x
)
9145 && (size
% chunksz
) == 0
9146 && input_bfd
!= NULL
9147 && location
!= NULL
);
9149 if (chunksz
== sizeof (x
))
9151 BFD_ASSERT (size
== chunksz
);
9153 /* Make sure that we do not perform an undefined shift operation.
9154 We know that size == chunksz so there will only be one iteration
9155 of the loop below. */
9159 shift
= 8 * chunksz
;
9161 for (; size
; size
-= chunksz
, location
+= chunksz
)
9166 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
9169 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
9172 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
9176 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
9187 decode_complex_addend (unsigned long *start
, /* in bits */
9188 unsigned long *oplen
, /* in bits */
9189 unsigned long *len
, /* in bits */
9190 unsigned long *wordsz
, /* in bytes */
9191 unsigned long *chunksz
, /* in bytes */
9192 unsigned long *lsb0_p
,
9193 unsigned long *signed_p
,
9194 unsigned long *trunc_p
,
9195 unsigned long encoded
)
9197 * start
= encoded
& 0x3F;
9198 * len
= (encoded
>> 6) & 0x3F;
9199 * oplen
= (encoded
>> 12) & 0x3F;
9200 * wordsz
= (encoded
>> 18) & 0xF;
9201 * chunksz
= (encoded
>> 22) & 0xF;
9202 * lsb0_p
= (encoded
>> 27) & 1;
9203 * signed_p
= (encoded
>> 28) & 1;
9204 * trunc_p
= (encoded
>> 29) & 1;
9207 bfd_reloc_status_type
9208 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
9209 asection
*input_section
,
9211 Elf_Internal_Rela
*rel
,
9214 bfd_vma shift
, x
, mask
;
9215 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9216 bfd_reloc_status_type r
;
9217 bfd_size_type octets
;
9219 /* Perform this reloc, since it is complex.
9220 (this is not to say that it necessarily refers to a complex
9221 symbol; merely that it is a self-describing CGEN based reloc.
9222 i.e. the addend has the complete reloc information (bit start, end,
9223 word size, etc) encoded within it.). */
9225 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9226 &chunksz
, &lsb0_p
, &signed_p
,
9227 &trunc_p
, rel
->r_addend
);
9229 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9232 shift
= (start
+ 1) - len
;
9234 shift
= (8 * wordsz
) - (start
+ len
);
9236 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9237 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9240 printf ("Doing complex reloc: "
9241 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9242 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9243 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9244 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9245 oplen
, (unsigned long) x
, (unsigned long) mask
,
9246 (unsigned long) relocation
);
9251 /* Now do an overflow check. */
9252 r
= bfd_check_overflow ((signed_p
9253 ? complain_overflow_signed
9254 : complain_overflow_unsigned
),
9255 len
, 0, (8 * wordsz
),
9259 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9262 printf (" relocation: %8.8lx\n"
9263 " shifted mask: %8.8lx\n"
9264 " shifted/masked reloc: %8.8lx\n"
9265 " result: %8.8lx\n",
9266 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9267 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9269 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9273 /* Functions to read r_offset from external (target order) reloc
9274 entry. Faster than bfd_getl32 et al, because we let the compiler
9275 know the value is aligned. */
9278 ext32l_r_offset (const void *p
)
9285 const union aligned32
*a
9286 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9288 uint32_t aval
= ( (uint32_t) a
->c
[0]
9289 | (uint32_t) a
->c
[1] << 8
9290 | (uint32_t) a
->c
[2] << 16
9291 | (uint32_t) a
->c
[3] << 24);
9296 ext32b_r_offset (const void *p
)
9303 const union aligned32
*a
9304 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9306 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9307 | (uint32_t) a
->c
[1] << 16
9308 | (uint32_t) a
->c
[2] << 8
9309 | (uint32_t) a
->c
[3]);
9313 #ifdef BFD_HOST_64_BIT
9315 ext64l_r_offset (const void *p
)
9322 const union aligned64
*a
9323 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9325 uint64_t aval
= ( (uint64_t) a
->c
[0]
9326 | (uint64_t) a
->c
[1] << 8
9327 | (uint64_t) a
->c
[2] << 16
9328 | (uint64_t) a
->c
[3] << 24
9329 | (uint64_t) a
->c
[4] << 32
9330 | (uint64_t) a
->c
[5] << 40
9331 | (uint64_t) a
->c
[6] << 48
9332 | (uint64_t) a
->c
[7] << 56);
9337 ext64b_r_offset (const void *p
)
9344 const union aligned64
*a
9345 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9347 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9348 | (uint64_t) a
->c
[1] << 48
9349 | (uint64_t) a
->c
[2] << 40
9350 | (uint64_t) a
->c
[3] << 32
9351 | (uint64_t) a
->c
[4] << 24
9352 | (uint64_t) a
->c
[5] << 16
9353 | (uint64_t) a
->c
[6] << 8
9354 | (uint64_t) a
->c
[7]);
9359 /* When performing a relocatable link, the input relocations are
9360 preserved. But, if they reference global symbols, the indices
9361 referenced must be updated. Update all the relocations found in
9365 elf_link_adjust_relocs (bfd
*abfd
,
9367 struct bfd_elf_section_reloc_data
*reldata
,
9369 struct bfd_link_info
*info
)
9372 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9374 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9375 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9376 bfd_vma r_type_mask
;
9378 unsigned int count
= reldata
->count
;
9379 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9381 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9383 swap_in
= bed
->s
->swap_reloc_in
;
9384 swap_out
= bed
->s
->swap_reloc_out
;
9386 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9388 swap_in
= bed
->s
->swap_reloca_in
;
9389 swap_out
= bed
->s
->swap_reloca_out
;
9394 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9397 if (bed
->s
->arch_size
== 32)
9404 r_type_mask
= 0xffffffff;
9408 erela
= reldata
->hdr
->contents
;
9409 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9411 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9414 if (*rel_hash
== NULL
)
9417 if ((*rel_hash
)->indx
== -2
9418 && info
->gc_sections
9419 && ! info
->gc_keep_exported
)
9421 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9422 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9424 (*rel_hash
)->root
.root
.string
);
9425 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9427 bfd_set_error (bfd_error_invalid_operation
);
9430 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9432 (*swap_in
) (abfd
, erela
, irela
);
9433 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9434 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9435 | (irela
[j
].r_info
& r_type_mask
));
9436 (*swap_out
) (abfd
, irela
, erela
);
9439 if (bed
->elf_backend_update_relocs
)
9440 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9442 if (sort
&& count
!= 0)
9444 bfd_vma (*ext_r_off
) (const void *);
9447 bfd_byte
*base
, *end
, *p
, *loc
;
9448 bfd_byte
*buf
= NULL
;
9450 if (bed
->s
->arch_size
== 32)
9452 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9453 ext_r_off
= ext32l_r_offset
;
9454 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9455 ext_r_off
= ext32b_r_offset
;
9461 #ifdef BFD_HOST_64_BIT
9462 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9463 ext_r_off
= ext64l_r_offset
;
9464 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9465 ext_r_off
= ext64b_r_offset
;
9471 /* Must use a stable sort here. A modified insertion sort,
9472 since the relocs are mostly sorted already. */
9473 elt_size
= reldata
->hdr
->sh_entsize
;
9474 base
= reldata
->hdr
->contents
;
9475 end
= base
+ count
* elt_size
;
9476 if (elt_size
> sizeof (Elf64_External_Rela
))
9479 /* Ensure the first element is lowest. This acts as a sentinel,
9480 speeding the main loop below. */
9481 r_off
= (*ext_r_off
) (base
);
9482 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9484 bfd_vma r_off2
= (*ext_r_off
) (p
);
9493 /* Don't just swap *base and *loc as that changes the order
9494 of the original base[0] and base[1] if they happen to
9495 have the same r_offset. */
9496 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9497 memcpy (onebuf
, loc
, elt_size
);
9498 memmove (base
+ elt_size
, base
, loc
- base
);
9499 memcpy (base
, onebuf
, elt_size
);
9502 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9504 /* base to p is sorted, *p is next to insert. */
9505 r_off
= (*ext_r_off
) (p
);
9506 /* Search the sorted region for location to insert. */
9508 while (r_off
< (*ext_r_off
) (loc
))
9513 /* Chances are there is a run of relocs to insert here,
9514 from one of more input files. Files are not always
9515 linked in order due to the way elf_link_input_bfd is
9516 called. See pr17666. */
9517 size_t sortlen
= p
- loc
;
9518 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9519 size_t runlen
= elt_size
;
9520 size_t buf_size
= 96 * 1024;
9521 while (p
+ runlen
< end
9522 && (sortlen
<= buf_size
9523 || runlen
+ elt_size
<= buf_size
)
9524 && r_off2
> (*ext_r_off
) (p
+ runlen
))
9528 buf
= bfd_malloc (buf_size
);
9532 if (runlen
< sortlen
)
9534 memcpy (buf
, p
, runlen
);
9535 memmove (loc
+ runlen
, loc
, sortlen
);
9536 memcpy (loc
, buf
, runlen
);
9540 memcpy (buf
, loc
, sortlen
);
9541 memmove (loc
, p
, runlen
);
9542 memcpy (loc
+ runlen
, buf
, sortlen
);
9544 p
+= runlen
- elt_size
;
9547 /* Hashes are no longer valid. */
9548 free (reldata
->hashes
);
9549 reldata
->hashes
= NULL
;
9555 struct elf_link_sort_rela
9561 enum elf_reloc_type_class type
;
9562 /* We use this as an array of size int_rels_per_ext_rel. */
9563 Elf_Internal_Rela rela
[1];
9566 /* qsort stability here and for cmp2 is only an issue if multiple
9567 dynamic relocations are emitted at the same address. But targets
9568 that apply a series of dynamic relocations each operating on the
9569 result of the prior relocation can't use -z combreloc as
9570 implemented anyway. Such schemes tend to be broken by sorting on
9571 symbol index. That leaves dynamic NONE relocs as the only other
9572 case where ld might emit multiple relocs at the same address, and
9573 those are only emitted due to target bugs. */
9576 elf_link_sort_cmp1 (const void *A
, const void *B
)
9578 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9579 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9580 int relativea
, relativeb
;
9582 relativea
= a
->type
== reloc_class_relative
;
9583 relativeb
= b
->type
== reloc_class_relative
;
9585 if (relativea
< relativeb
)
9587 if (relativea
> relativeb
)
9589 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9591 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9593 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9595 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9601 elf_link_sort_cmp2 (const void *A
, const void *B
)
9603 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9604 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9606 if (a
->type
< b
->type
)
9608 if (a
->type
> b
->type
)
9610 if (a
->u
.offset
< b
->u
.offset
)
9612 if (a
->u
.offset
> b
->u
.offset
)
9614 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9616 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9622 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9624 asection
*dynamic_relocs
;
9627 bfd_size_type count
, size
;
9628 size_t i
, ret
, sort_elt
, ext_size
;
9629 bfd_byte
*sort
, *s_non_relative
, *p
;
9630 struct elf_link_sort_rela
*sq
;
9631 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9632 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9633 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9634 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9635 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9636 struct bfd_link_order
*lo
;
9640 /* Find a dynamic reloc section. */
9641 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9642 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9643 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9644 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9646 bool use_rela_initialised
= false;
9648 /* This is just here to stop gcc from complaining.
9649 Its initialization checking code is not perfect. */
9652 /* Both sections are present. Examine the sizes
9653 of the indirect sections to help us choose. */
9654 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9655 if (lo
->type
== bfd_indirect_link_order
)
9657 asection
*o
= lo
->u
.indirect
.section
;
9659 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9661 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9662 /* Section size is divisible by both rel and rela sizes.
9663 It is of no help to us. */
9667 /* Section size is only divisible by rela. */
9668 if (use_rela_initialised
&& !use_rela
)
9670 _bfd_error_handler (_("%pB: unable to sort relocs - "
9671 "they are in more than one size"),
9673 bfd_set_error (bfd_error_invalid_operation
);
9679 use_rela_initialised
= true;
9683 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9685 /* Section size is only divisible by rel. */
9686 if (use_rela_initialised
&& use_rela
)
9688 _bfd_error_handler (_("%pB: unable to sort relocs - "
9689 "they are in more than one size"),
9691 bfd_set_error (bfd_error_invalid_operation
);
9697 use_rela_initialised
= true;
9702 /* The section size is not divisible by either -
9703 something is wrong. */
9704 _bfd_error_handler (_("%pB: unable to sort relocs - "
9705 "they are of an unknown size"), abfd
);
9706 bfd_set_error (bfd_error_invalid_operation
);
9711 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9712 if (lo
->type
== bfd_indirect_link_order
)
9714 asection
*o
= lo
->u
.indirect
.section
;
9716 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9718 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9719 /* Section size is divisible by both rel and rela sizes.
9720 It is of no help to us. */
9724 /* Section size is only divisible by rela. */
9725 if (use_rela_initialised
&& !use_rela
)
9727 _bfd_error_handler (_("%pB: unable to sort relocs - "
9728 "they are in more than one size"),
9730 bfd_set_error (bfd_error_invalid_operation
);
9736 use_rela_initialised
= true;
9740 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9742 /* Section size is only divisible by rel. */
9743 if (use_rela_initialised
&& use_rela
)
9745 _bfd_error_handler (_("%pB: unable to sort relocs - "
9746 "they are in more than one size"),
9748 bfd_set_error (bfd_error_invalid_operation
);
9754 use_rela_initialised
= true;
9759 /* The section size is not divisible by either -
9760 something is wrong. */
9761 _bfd_error_handler (_("%pB: unable to sort relocs - "
9762 "they are of an unknown size"), abfd
);
9763 bfd_set_error (bfd_error_invalid_operation
);
9768 if (! use_rela_initialised
)
9772 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9774 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9781 dynamic_relocs
= rela_dyn
;
9782 ext_size
= bed
->s
->sizeof_rela
;
9783 swap_in
= bed
->s
->swap_reloca_in
;
9784 swap_out
= bed
->s
->swap_reloca_out
;
9788 dynamic_relocs
= rel_dyn
;
9789 ext_size
= bed
->s
->sizeof_rel
;
9790 swap_in
= bed
->s
->swap_reloc_in
;
9791 swap_out
= bed
->s
->swap_reloc_out
;
9795 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9796 if (lo
->type
== bfd_indirect_link_order
)
9797 size
+= lo
->u
.indirect
.section
->size
;
9799 if (size
!= dynamic_relocs
->size
)
9802 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9803 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9805 count
= dynamic_relocs
->size
/ ext_size
;
9808 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9812 (*info
->callbacks
->warning
)
9813 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9817 if (bed
->s
->arch_size
== 32)
9818 r_sym_mask
= ~(bfd_vma
) 0xff;
9820 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9822 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9823 if (lo
->type
== bfd_indirect_link_order
)
9825 bfd_byte
*erel
, *erelend
;
9826 asection
*o
= lo
->u
.indirect
.section
;
9828 if (o
->contents
== NULL
&& o
->size
!= 0)
9830 /* This is a reloc section that is being handled as a normal
9831 section. See bfd_section_from_shdr. We can't combine
9832 relocs in this case. */
9837 erelend
= o
->contents
+ o
->size
;
9838 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9840 while (erel
< erelend
)
9842 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9844 (*swap_in
) (abfd
, erel
, s
->rela
);
9845 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9846 s
->u
.sym_mask
= r_sym_mask
;
9852 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9854 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9856 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9857 if (s
->type
!= reloc_class_relative
)
9863 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9864 for (; i
< count
; i
++, p
+= sort_elt
)
9866 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9867 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9869 sp
->u
.offset
= sq
->rela
->r_offset
;
9872 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9874 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9875 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9877 /* We have plt relocs in .rela.dyn. */
9878 sq
= (struct elf_link_sort_rela
*) sort
;
9879 for (i
= 0; i
< count
; i
++)
9880 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9882 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9884 struct bfd_link_order
**plo
;
9885 /* Put srelplt link_order last. This is so the output_offset
9886 set in the next loop is correct for DT_JMPREL. */
9887 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9888 if ((*plo
)->type
== bfd_indirect_link_order
9889 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9895 plo
= &(*plo
)->next
;
9898 dynamic_relocs
->map_tail
.link_order
= lo
;
9903 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9904 if (lo
->type
== bfd_indirect_link_order
)
9906 bfd_byte
*erel
, *erelend
;
9907 asection
*o
= lo
->u
.indirect
.section
;
9910 erelend
= o
->contents
+ o
->size
;
9911 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9912 while (erel
< erelend
)
9914 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9915 (*swap_out
) (abfd
, s
->rela
, erel
);
9922 *psec
= dynamic_relocs
;
9926 /* Add a symbol to the output symbol string table. */
9929 elf_link_output_symstrtab (void *finf
,
9931 Elf_Internal_Sym
*elfsym
,
9932 asection
*input_sec
,
9933 struct elf_link_hash_entry
*h
)
9935 struct elf_final_link_info
*flinfo
= finf
;
9936 int (*output_symbol_hook
)
9937 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9938 struct elf_link_hash_entry
*);
9939 struct elf_link_hash_table
*hash_table
;
9940 const struct elf_backend_data
*bed
;
9941 bfd_size_type strtabsize
;
9943 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9945 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9946 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9947 if (output_symbol_hook
!= NULL
)
9949 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9954 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
9955 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
9956 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
9957 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
9961 || (input_sec
->flags
& SEC_EXCLUDE
))
9962 elfsym
->st_name
= (unsigned long) -1;
9965 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9966 to get the final offset for st_name. */
9967 char *versioned_name
= (char *) name
;
9970 if (h
->versioned
== versioned
&& h
->def_dynamic
)
9972 /* Keep only one '@' for versioned symbols defined in
9974 char *version
= strrchr (name
, ELF_VER_CHR
);
9975 char *base_end
= strchr (name
, ELF_VER_CHR
);
9976 if (version
!= base_end
)
9979 size_t len
= strlen (name
);
9980 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
9981 if (versioned_name
== NULL
)
9983 base_len
= base_end
- name
;
9984 memcpy (versioned_name
, name
, base_len
);
9985 memcpy (versioned_name
+ base_len
, version
,
9990 else if (flinfo
->info
->unique_symbol
9991 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
9993 struct local_hash_entry
*lh
;
9997 switch (ELF_ST_TYPE (elfsym
->st_info
))
10003 lh
= (struct local_hash_entry
*) bfd_hash_lookup
10004 (&flinfo
->local_hash_table
, name
, true, false);
10007 /* Always append ".COUNT" to local symbols to avoid
10008 potential conflicts with local symbol "XXX.COUNT". */
10009 sprintf (buf
, "%lx", lh
->count
);
10010 base_len
= lh
->size
;
10013 base_len
= strlen (name
);
10014 lh
->size
= base_len
;
10016 count_len
= strlen (buf
);
10017 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
10018 base_len
+ count_len
+ 2);
10019 if (versioned_name
== NULL
)
10021 memcpy (versioned_name
, name
, base_len
);
10022 versioned_name
[base_len
] = '.';
10023 memcpy (versioned_name
+ base_len
+ 1, buf
,
10030 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
10031 versioned_name
, false);
10032 if (elfsym
->st_name
== (unsigned long) -1)
10036 hash_table
= elf_hash_table (flinfo
->info
);
10037 strtabsize
= hash_table
->strtabsize
;
10038 if (strtabsize
<= flinfo
->output_bfd
->symcount
)
10040 strtabsize
+= strtabsize
;
10041 hash_table
->strtabsize
= strtabsize
;
10042 strtabsize
*= sizeof (*hash_table
->strtab
);
10044 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
10046 if (hash_table
->strtab
== NULL
)
10049 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].sym
= *elfsym
;
10050 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].dest_index
10051 = flinfo
->output_bfd
->symcount
;
10052 flinfo
->output_bfd
->symcount
+= 1;
10057 /* Swap symbols out to the symbol table and flush the output symbols to
10061 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
10063 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
10066 const struct elf_backend_data
*bed
;
10068 Elf_Internal_Shdr
*hdr
;
10072 if (flinfo
->output_bfd
->symcount
== 0)
10075 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10077 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10079 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10080 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
10081 if (symbuf
== NULL
)
10084 if (flinfo
->symshndxbuf
)
10086 amt
= sizeof (Elf_External_Sym_Shndx
);
10087 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
10088 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
10089 if (flinfo
->symshndxbuf
== NULL
)
10096 /* Now swap out the symbols. */
10097 for (i
= 0; i
< flinfo
->output_bfd
->symcount
; i
++)
10099 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
10100 if (elfsym
->sym
.st_name
== (unsigned long) -1)
10101 elfsym
->sym
.st_name
= 0;
10103 elfsym
->sym
.st_name
10104 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
10105 elfsym
->sym
.st_name
);
10107 /* Inform the linker of the addition of this symbol. */
10109 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
10110 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
10113 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
10114 ((bfd_byte
*) symbuf
10115 + (elfsym
->dest_index
10116 * bed
->s
->sizeof_sym
)),
10117 NPTR_ADD (flinfo
->symshndxbuf
,
10118 elfsym
->dest_index
));
10121 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
10122 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
10123 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10124 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
10125 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
10127 hdr
->sh_size
+= amt
;
10135 free (hash_table
->strtab
);
10136 hash_table
->strtab
= NULL
;
10141 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10144 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
10146 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
10147 && sym
->st_shndx
< SHN_LORESERVE
)
10149 /* The gABI doesn't support dynamic symbols in output sections
10152 /* xgettext:c-format */
10153 (_("%pB: too many sections: %d (>= %d)"),
10154 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
10155 bfd_set_error (bfd_error_nonrepresentable_section
);
10161 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10162 allowing an unsatisfied unversioned symbol in the DSO to match a
10163 versioned symbol that would normally require an explicit version.
10164 We also handle the case that a DSO references a hidden symbol
10165 which may be satisfied by a versioned symbol in another DSO. */
10168 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
10169 const struct elf_backend_data
*bed
,
10170 struct elf_link_hash_entry
*h
)
10173 struct elf_link_loaded_list
*loaded
;
10175 if (!is_elf_hash_table (info
->hash
))
10178 /* Check indirect symbol. */
10179 while (h
->root
.type
== bfd_link_hash_indirect
)
10180 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10182 switch (h
->root
.type
)
10188 case bfd_link_hash_undefined
:
10189 case bfd_link_hash_undefweak
:
10190 abfd
= h
->root
.u
.undef
.abfd
;
10192 || (abfd
->flags
& DYNAMIC
) == 0
10193 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
10197 case bfd_link_hash_defined
:
10198 case bfd_link_hash_defweak
:
10199 abfd
= h
->root
.u
.def
.section
->owner
;
10202 case bfd_link_hash_common
:
10203 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10206 BFD_ASSERT (abfd
!= NULL
);
10208 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10210 loaded
= loaded
->next
)
10213 Elf_Internal_Shdr
*hdr
;
10215 size_t extsymcount
;
10217 Elf_Internal_Shdr
*versymhdr
;
10218 Elf_Internal_Sym
*isym
;
10219 Elf_Internal_Sym
*isymend
;
10220 Elf_Internal_Sym
*isymbuf
;
10221 Elf_External_Versym
*ever
;
10222 Elf_External_Versym
*extversym
;
10224 input
= loaded
->abfd
;
10226 /* We check each DSO for a possible hidden versioned definition. */
10228 || elf_dynversym (input
) == 0)
10231 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10233 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10234 if (elf_bad_symtab (input
))
10236 extsymcount
= symcount
;
10241 extsymcount
= symcount
- hdr
->sh_info
;
10242 extsymoff
= hdr
->sh_info
;
10245 if (extsymcount
== 0)
10248 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10250 if (isymbuf
== NULL
)
10253 /* Read in any version definitions. */
10254 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10255 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10256 || (extversym
= (Elf_External_Versym
*)
10257 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10258 versymhdr
->sh_size
)) == NULL
)
10264 ever
= extversym
+ extsymoff
;
10265 isymend
= isymbuf
+ extsymcount
;
10266 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10269 Elf_Internal_Versym iver
;
10270 unsigned short version_index
;
10272 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10273 || isym
->st_shndx
== SHN_UNDEF
)
10276 name
= bfd_elf_string_from_elf_section (input
,
10279 if (strcmp (name
, h
->root
.root
.string
) != 0)
10282 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10284 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10285 && !(h
->def_regular
10286 && h
->forced_local
))
10288 /* If we have a non-hidden versioned sym, then it should
10289 have provided a definition for the undefined sym unless
10290 it is defined in a non-shared object and forced local.
10295 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10296 if (version_index
== 1 || version_index
== 2)
10298 /* This is the base or first version. We can use it. */
10312 /* Convert ELF common symbol TYPE. */
10315 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10317 /* Commom symbol can only appear in relocatable link. */
10318 if (!bfd_link_relocatable (info
))
10320 switch (info
->elf_stt_common
)
10324 case elf_stt_common
:
10327 case no_elf_stt_common
:
10334 /* Add an external symbol to the symbol table. This is called from
10335 the hash table traversal routine. When generating a shared object,
10336 we go through the symbol table twice. The first time we output
10337 anything that might have been forced to local scope in a version
10338 script. The second time we output the symbols that are still
10342 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10344 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10345 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10346 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10348 Elf_Internal_Sym sym
;
10349 asection
*input_sec
;
10350 const struct elf_backend_data
*bed
;
10355 if (h
->root
.type
== bfd_link_hash_warning
)
10357 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10358 if (h
->root
.type
== bfd_link_hash_new
)
10362 /* Decide whether to output this symbol in this pass. */
10363 if (eoinfo
->localsyms
)
10365 if (!h
->forced_local
)
10370 if (h
->forced_local
)
10374 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10376 if (h
->root
.type
== bfd_link_hash_undefined
)
10378 /* If we have an undefined symbol reference here then it must have
10379 come from a shared library that is being linked in. (Undefined
10380 references in regular files have already been handled unless
10381 they are in unreferenced sections which are removed by garbage
10383 bool ignore_undef
= false;
10385 /* Some symbols may be special in that the fact that they're
10386 undefined can be safely ignored - let backend determine that. */
10387 if (bed
->elf_backend_ignore_undef_symbol
)
10388 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10390 /* If we are reporting errors for this situation then do so now. */
10392 && h
->ref_dynamic_nonweak
10393 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10394 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10395 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10397 flinfo
->info
->callbacks
->undefined_symbol
10398 (flinfo
->info
, h
->root
.root
.string
,
10399 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10400 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10401 && !flinfo
->info
->warn_unresolved_syms
);
10404 /* Strip a global symbol defined in a discarded section. */
10409 /* We should also warn if a forced local symbol is referenced from
10410 shared libraries. */
10411 if (bfd_link_executable (flinfo
->info
)
10416 && h
->ref_dynamic_nonweak
10417 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10421 struct elf_link_hash_entry
*hi
= h
;
10423 /* Check indirect symbol. */
10424 while (hi
->root
.type
== bfd_link_hash_indirect
)
10425 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10427 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10428 /* xgettext:c-format */
10429 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10430 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10431 /* xgettext:c-format */
10432 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10434 /* xgettext:c-format */
10435 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10436 def_bfd
= flinfo
->output_bfd
;
10437 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10438 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10439 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10440 h
->root
.root
.string
, def_bfd
);
10441 bfd_set_error (bfd_error_bad_value
);
10442 eoinfo
->failed
= true;
10446 /* We don't want to output symbols that have never been mentioned by
10447 a regular file, or that we have been told to strip. However, if
10448 h->indx is set to -2, the symbol is used by a reloc and we must
10453 else if ((h
->def_dynamic
10455 || h
->root
.type
== bfd_link_hash_new
)
10457 && !h
->ref_regular
)
10459 else if (flinfo
->info
->strip
== strip_all
)
10461 else if (flinfo
->info
->strip
== strip_some
10462 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10463 h
->root
.root
.string
, false, false) == NULL
)
10465 else if ((h
->root
.type
== bfd_link_hash_defined
10466 || h
->root
.type
== bfd_link_hash_defweak
)
10467 && ((flinfo
->info
->strip_discarded
10468 && discarded_section (h
->root
.u
.def
.section
))
10469 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10470 && h
->root
.u
.def
.section
->owner
!= NULL
10471 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10473 else if ((h
->root
.type
== bfd_link_hash_undefined
10474 || h
->root
.type
== bfd_link_hash_undefweak
)
10475 && h
->root
.u
.undef
.abfd
!= NULL
10476 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10481 /* If we're stripping it, and it's not a dynamic symbol, there's
10482 nothing else to do. However, if it is a forced local symbol or
10483 an ifunc symbol we need to give the backend finish_dynamic_symbol
10484 function a chance to make it dynamic. */
10486 && h
->dynindx
== -1
10487 && type
!= STT_GNU_IFUNC
10488 && !h
->forced_local
)
10492 sym
.st_size
= h
->size
;
10493 sym
.st_other
= h
->other
;
10494 switch (h
->root
.type
)
10497 case bfd_link_hash_new
:
10498 case bfd_link_hash_warning
:
10502 case bfd_link_hash_undefined
:
10503 case bfd_link_hash_undefweak
:
10504 input_sec
= bfd_und_section_ptr
;
10505 sym
.st_shndx
= SHN_UNDEF
;
10508 case bfd_link_hash_defined
:
10509 case bfd_link_hash_defweak
:
10511 input_sec
= h
->root
.u
.def
.section
;
10512 if (input_sec
->output_section
!= NULL
)
10515 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10516 input_sec
->output_section
);
10517 if (sym
.st_shndx
== SHN_BAD
)
10520 /* xgettext:c-format */
10521 (_("%pB: could not find output section %pA for input section %pA"),
10522 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10523 bfd_set_error (bfd_error_nonrepresentable_section
);
10524 eoinfo
->failed
= true;
10528 /* ELF symbols in relocatable files are section relative,
10529 but in nonrelocatable files they are virtual
10531 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10532 if (!bfd_link_relocatable (flinfo
->info
))
10534 sym
.st_value
+= input_sec
->output_section
->vma
;
10535 if (h
->type
== STT_TLS
)
10537 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10538 if (tls_sec
!= NULL
)
10539 sym
.st_value
-= tls_sec
->vma
;
10545 BFD_ASSERT (input_sec
->owner
== NULL
10546 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10547 sym
.st_shndx
= SHN_UNDEF
;
10548 input_sec
= bfd_und_section_ptr
;
10553 case bfd_link_hash_common
:
10554 input_sec
= h
->root
.u
.c
.p
->section
;
10555 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10556 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10559 case bfd_link_hash_indirect
:
10560 /* These symbols are created by symbol versioning. They point
10561 to the decorated version of the name. For example, if the
10562 symbol foo@@GNU_1.2 is the default, which should be used when
10563 foo is used with no version, then we add an indirect symbol
10564 foo which points to foo@@GNU_1.2. We ignore these symbols,
10565 since the indirected symbol is already in the hash table. */
10569 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10570 switch (h
->root
.type
)
10572 case bfd_link_hash_common
:
10573 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10575 case bfd_link_hash_defined
:
10576 case bfd_link_hash_defweak
:
10577 if (bed
->common_definition (&sym
))
10578 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10582 case bfd_link_hash_undefined
:
10583 case bfd_link_hash_undefweak
:
10589 if (h
->forced_local
)
10591 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10592 /* Turn off visibility on local symbol. */
10593 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10595 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10596 else if (h
->unique_global
&& h
->def_regular
)
10597 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10598 else if (h
->root
.type
== bfd_link_hash_undefweak
10599 || h
->root
.type
== bfd_link_hash_defweak
)
10600 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10602 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10603 sym
.st_target_internal
= h
->target_internal
;
10605 /* Give the processor backend a chance to tweak the symbol value,
10606 and also to finish up anything that needs to be done for this
10607 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10608 forced local syms when non-shared is due to a historical quirk.
10609 STT_GNU_IFUNC symbol must go through PLT. */
10610 if ((h
->type
== STT_GNU_IFUNC
10612 && !bfd_link_relocatable (flinfo
->info
))
10613 || ((h
->dynindx
!= -1
10614 || h
->forced_local
)
10615 && ((bfd_link_pic (flinfo
->info
)
10616 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10617 || h
->root
.type
!= bfd_link_hash_undefweak
))
10618 || !h
->forced_local
)
10619 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10621 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10622 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10624 eoinfo
->failed
= true;
10629 /* If we are marking the symbol as undefined, and there are no
10630 non-weak references to this symbol from a regular object, then
10631 mark the symbol as weak undefined; if there are non-weak
10632 references, mark the symbol as strong. We can't do this earlier,
10633 because it might not be marked as undefined until the
10634 finish_dynamic_symbol routine gets through with it. */
10635 if (sym
.st_shndx
== SHN_UNDEF
10637 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10638 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10641 type
= ELF_ST_TYPE (sym
.st_info
);
10643 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10644 if (type
== STT_GNU_IFUNC
)
10647 if (h
->ref_regular_nonweak
)
10648 bindtype
= STB_GLOBAL
;
10650 bindtype
= STB_WEAK
;
10651 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10654 /* If this is a symbol defined in a dynamic library, don't use the
10655 symbol size from the dynamic library. Relinking an executable
10656 against a new library may introduce gratuitous changes in the
10657 executable's symbols if we keep the size. */
10658 if (sym
.st_shndx
== SHN_UNDEF
10663 /* If a non-weak symbol with non-default visibility is not defined
10664 locally, it is a fatal error. */
10665 if (!bfd_link_relocatable (flinfo
->info
)
10666 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10667 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10668 && h
->root
.type
== bfd_link_hash_undefined
10669 && !h
->def_regular
)
10673 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10674 /* xgettext:c-format */
10675 msg
= _("%pB: protected symbol `%s' isn't defined");
10676 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10677 /* xgettext:c-format */
10678 msg
= _("%pB: internal symbol `%s' isn't defined");
10680 /* xgettext:c-format */
10681 msg
= _("%pB: hidden symbol `%s' isn't defined");
10682 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10683 bfd_set_error (bfd_error_bad_value
);
10684 eoinfo
->failed
= true;
10688 /* If this symbol should be put in the .dynsym section, then put it
10689 there now. We already know the symbol index. We also fill in
10690 the entry in the .hash section. */
10691 if (h
->dynindx
!= -1
10692 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10693 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10694 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10698 /* Since there is no version information in the dynamic string,
10699 if there is no version info in symbol version section, we will
10700 have a run-time problem if not linking executable, referenced
10701 by shared library, or not bound locally. */
10702 if (h
->verinfo
.verdef
== NULL
10703 && (!bfd_link_executable (flinfo
->info
)
10705 || !h
->def_regular
))
10707 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10709 if (p
&& p
[1] != '\0')
10712 /* xgettext:c-format */
10713 (_("%pB: no symbol version section for versioned symbol `%s'"),
10714 flinfo
->output_bfd
, h
->root
.root
.string
);
10715 eoinfo
->failed
= true;
10720 sym
.st_name
= h
->dynstr_index
;
10721 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10722 + h
->dynindx
* bed
->s
->sizeof_sym
);
10723 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10725 eoinfo
->failed
= true;
10729 /* Inform the linker of the addition of this symbol. */
10731 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
10732 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
10734 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10736 if (flinfo
->hash_sec
!= NULL
)
10738 size_t hash_entry_size
;
10739 bfd_byte
*bucketpos
;
10741 size_t bucketcount
;
10744 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10745 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10748 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10749 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10750 + (bucket
+ 2) * hash_entry_size
);
10751 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10752 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10754 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10755 ((bfd_byte
*) flinfo
->hash_sec
->contents
10756 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10759 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10761 Elf_Internal_Versym iversym
;
10762 Elf_External_Versym
*eversym
;
10764 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10766 if (h
->verinfo
.verdef
== NULL
10767 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10768 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10769 iversym
.vs_vers
= 1;
10771 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10775 if (h
->verinfo
.vertree
== NULL
)
10776 iversym
.vs_vers
= 1;
10778 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10779 if (flinfo
->info
->create_default_symver
)
10783 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10784 defined locally. */
10785 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10786 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10788 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10789 eversym
+= h
->dynindx
;
10790 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10794 /* If the symbol is undefined, and we didn't output it to .dynsym,
10795 strip it from .symtab too. Obviously we can't do this for
10796 relocatable output or when needed for --emit-relocs. */
10797 else if (input_sec
== bfd_und_section_ptr
10799 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10800 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10801 && !bfd_link_relocatable (flinfo
->info
))
10804 /* Also strip others that we couldn't earlier due to dynamic symbol
10808 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10811 /* Output a FILE symbol so that following locals are not associated
10812 with the wrong input file. We need one for forced local symbols
10813 if we've seen more than one FILE symbol or when we have exactly
10814 one FILE symbol but global symbols are present in a file other
10815 than the one with the FILE symbol. We also need one if linker
10816 defined symbols are present. In practice these conditions are
10817 always met, so just emit the FILE symbol unconditionally. */
10818 if (eoinfo
->localsyms
10819 && !eoinfo
->file_sym_done
10820 && eoinfo
->flinfo
->filesym_count
!= 0)
10822 Elf_Internal_Sym fsym
;
10824 memset (&fsym
, 0, sizeof (fsym
));
10825 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10826 fsym
.st_shndx
= SHN_ABS
;
10827 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10828 bfd_und_section_ptr
, NULL
))
10831 eoinfo
->file_sym_done
= true;
10834 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10835 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10839 eoinfo
->failed
= true;
10844 else if (h
->indx
== -2)
10850 /* Return TRUE if special handling is done for relocs in SEC against
10851 symbols defined in discarded sections. */
10854 elf_section_ignore_discarded_relocs (asection
*sec
)
10856 const struct elf_backend_data
*bed
;
10858 switch (sec
->sec_info_type
)
10860 case SEC_INFO_TYPE_STABS
:
10861 case SEC_INFO_TYPE_EH_FRAME
:
10862 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10868 bed
= get_elf_backend_data (sec
->owner
);
10869 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10870 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10876 /* Return a mask saying how ld should treat relocations in SEC against
10877 symbols defined in discarded sections. If this function returns
10878 COMPLAIN set, ld will issue a warning message. If this function
10879 returns PRETEND set, and the discarded section was link-once and the
10880 same size as the kept link-once section, ld will pretend that the
10881 symbol was actually defined in the kept section. Otherwise ld will
10882 zero the reloc (at least that is the intent, but some cooperation by
10883 the target dependent code is needed, particularly for REL targets). */
10886 _bfd_elf_default_action_discarded (asection
*sec
)
10888 if (sec
->flags
& SEC_DEBUGGING
)
10891 if (strcmp (".eh_frame", sec
->name
) == 0)
10894 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10897 return COMPLAIN
| PRETEND
;
10900 /* Find a match between a section and a member of a section group. */
10903 match_group_member (asection
*sec
, asection
*group
,
10904 struct bfd_link_info
*info
)
10906 asection
*first
= elf_next_in_group (group
);
10907 asection
*s
= first
;
10911 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10914 s
= elf_next_in_group (s
);
10922 /* Check if the kept section of a discarded section SEC can be used
10923 to replace it. Return the replacement if it is OK. Otherwise return
10927 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10931 kept
= sec
->kept_section
;
10934 if ((kept
->flags
& SEC_GROUP
) != 0)
10935 kept
= match_group_member (sec
, kept
, info
);
10938 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10939 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
10943 /* Get the real kept section. */
10945 for (next
= kept
->kept_section
;
10947 next
= next
->kept_section
)
10951 sec
->kept_section
= kept
;
10956 /* Link an input file into the linker output file. This function
10957 handles all the sections and relocations of the input file at once.
10958 This is so that we only have to read the local symbols once, and
10959 don't have to keep them in memory. */
10962 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10964 int (*relocate_section
)
10965 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10966 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10968 Elf_Internal_Shdr
*symtab_hdr
;
10969 size_t locsymcount
;
10971 Elf_Internal_Sym
*isymbuf
;
10972 Elf_Internal_Sym
*isym
;
10973 Elf_Internal_Sym
*isymend
;
10975 asection
**ppsection
;
10977 const struct elf_backend_data
*bed
;
10978 struct elf_link_hash_entry
**sym_hashes
;
10979 bfd_size_type address_size
;
10980 bfd_vma r_type_mask
;
10982 bool have_file_sym
= false;
10984 output_bfd
= flinfo
->output_bfd
;
10985 bed
= get_elf_backend_data (output_bfd
);
10986 relocate_section
= bed
->elf_backend_relocate_section
;
10988 /* If this is a dynamic object, we don't want to do anything here:
10989 we don't want the local symbols, and we don't want the section
10991 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10994 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10995 if (elf_bad_symtab (input_bfd
))
10997 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11002 locsymcount
= symtab_hdr
->sh_info
;
11003 extsymoff
= symtab_hdr
->sh_info
;
11006 /* Enable GNU OSABI features in the output BFD that are used in the input
11008 if (bed
->elf_osabi
== ELFOSABI_NONE
11009 || bed
->elf_osabi
== ELFOSABI_GNU
11010 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
11011 elf_tdata (output_bfd
)->has_gnu_osabi
11012 |= (elf_tdata (input_bfd
)->has_gnu_osabi
11013 & (bfd_link_relocatable (flinfo
->info
)
11014 ? -1 : ~elf_gnu_osabi_retain
));
11016 /* Read the local symbols. */
11017 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11018 if (isymbuf
== NULL
&& locsymcount
!= 0)
11020 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
11021 flinfo
->internal_syms
,
11022 flinfo
->external_syms
,
11023 flinfo
->locsym_shndx
);
11024 if (isymbuf
== NULL
)
11028 /* Find local symbol sections and adjust values of symbols in
11029 SEC_MERGE sections. Write out those local symbols we know are
11030 going into the output file. */
11031 isymend
= PTR_ADD (isymbuf
, locsymcount
);
11032 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
11034 isym
++, pindex
++, ppsection
++)
11038 Elf_Internal_Sym osym
;
11044 if (elf_bad_symtab (input_bfd
))
11046 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
11053 if (isym
->st_shndx
== SHN_UNDEF
)
11054 isec
= bfd_und_section_ptr
;
11055 else if (isym
->st_shndx
== SHN_ABS
)
11056 isec
= bfd_abs_section_ptr
;
11057 else if (isym
->st_shndx
== SHN_COMMON
)
11058 isec
= bfd_com_section_ptr
;
11061 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
11064 /* Don't attempt to output symbols with st_shnx in the
11065 reserved range other than SHN_ABS and SHN_COMMON. */
11066 isec
= bfd_und_section_ptr
;
11068 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
11069 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
11071 _bfd_merged_section_offset (output_bfd
, &isec
,
11072 elf_section_data (isec
)->sec_info
,
11078 /* Don't output the first, undefined, symbol. In fact, don't
11079 output any undefined local symbol. */
11080 if (isec
== bfd_und_section_ptr
)
11083 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
11085 /* We never output section symbols. Instead, we use the
11086 section symbol of the corresponding section in the output
11091 /* If we are stripping all symbols, we don't want to output this
11093 if (flinfo
->info
->strip
== strip_all
)
11096 /* If we are discarding all local symbols, we don't want to
11097 output this one. If we are generating a relocatable output
11098 file, then some of the local symbols may be required by
11099 relocs; we output them below as we discover that they are
11101 if (flinfo
->info
->discard
== discard_all
)
11104 /* If this symbol is defined in a section which we are
11105 discarding, we don't need to keep it. */
11106 if (isym
->st_shndx
!= SHN_UNDEF
11107 && isym
->st_shndx
< SHN_LORESERVE
11108 && isec
->output_section
== NULL
11109 && flinfo
->info
->non_contiguous_regions
11110 && flinfo
->info
->non_contiguous_regions_warnings
)
11112 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
11113 "discards section `%s' from '%s'\n"),
11114 isec
->name
, bfd_get_filename (isec
->owner
));
11118 if (isym
->st_shndx
!= SHN_UNDEF
11119 && isym
->st_shndx
< SHN_LORESERVE
11120 && bfd_section_removed_from_list (output_bfd
,
11121 isec
->output_section
))
11124 /* Get the name of the symbol. */
11125 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
11130 /* See if we are discarding symbols with this name. */
11131 if ((flinfo
->info
->strip
== strip_some
11132 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, false, false)
11134 || (((flinfo
->info
->discard
== discard_sec_merge
11135 && (isec
->flags
& SEC_MERGE
)
11136 && !bfd_link_relocatable (flinfo
->info
))
11137 || flinfo
->info
->discard
== discard_l
)
11138 && bfd_is_local_label_name (input_bfd
, name
)))
11141 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
11143 if (input_bfd
->lto_output
)
11144 /* -flto puts a temp file name here. This means builds
11145 are not reproducible. Discard the symbol. */
11147 have_file_sym
= true;
11148 flinfo
->filesym_count
+= 1;
11150 if (!have_file_sym
)
11152 /* In the absence of debug info, bfd_find_nearest_line uses
11153 FILE symbols to determine the source file for local
11154 function symbols. Provide a FILE symbol here if input
11155 files lack such, so that their symbols won't be
11156 associated with a previous input file. It's not the
11157 source file, but the best we can do. */
11158 const char *filename
;
11159 have_file_sym
= true;
11160 flinfo
->filesym_count
+= 1;
11161 memset (&osym
, 0, sizeof (osym
));
11162 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11163 osym
.st_shndx
= SHN_ABS
;
11164 if (input_bfd
->lto_output
)
11167 filename
= lbasename (bfd_get_filename (input_bfd
));
11168 if (!elf_link_output_symstrtab (flinfo
, filename
, &osym
,
11169 bfd_abs_section_ptr
, NULL
))
11175 /* Adjust the section index for the output file. */
11176 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11177 isec
->output_section
);
11178 if (osym
.st_shndx
== SHN_BAD
)
11181 /* ELF symbols in relocatable files are section relative, but
11182 in executable files they are virtual addresses. Note that
11183 this code assumes that all ELF sections have an associated
11184 BFD section with a reasonable value for output_offset; below
11185 we assume that they also have a reasonable value for
11186 output_section. Any special sections must be set up to meet
11187 these requirements. */
11188 osym
.st_value
+= isec
->output_offset
;
11189 if (!bfd_link_relocatable (flinfo
->info
))
11191 osym
.st_value
+= isec
->output_section
->vma
;
11192 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
11194 /* STT_TLS symbols are relative to PT_TLS segment base. */
11195 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
11196 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
11198 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
11203 indx
= bfd_get_symcount (output_bfd
);
11204 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11211 if (bed
->s
->arch_size
== 32)
11213 r_type_mask
= 0xff;
11219 r_type_mask
= 0xffffffff;
11224 /* Relocate the contents of each section. */
11225 sym_hashes
= elf_sym_hashes (input_bfd
);
11226 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11228 bfd_byte
*contents
;
11230 if (! o
->linker_mark
)
11232 /* This section was omitted from the link. */
11236 if (!flinfo
->info
->resolve_section_groups
11237 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11239 /* Deal with the group signature symbol. */
11240 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11241 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11242 asection
*osec
= o
->output_section
;
11244 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11245 if (symndx
>= locsymcount
11246 || (elf_bad_symtab (input_bfd
)
11247 && flinfo
->sections
[symndx
] == NULL
))
11249 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
11250 while (h
->root
.type
== bfd_link_hash_indirect
11251 || h
->root
.type
== bfd_link_hash_warning
)
11252 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11253 /* Arrange for symbol to be output. */
11255 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11257 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11259 /* We'll use the output section target_index. */
11260 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11261 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11265 if (flinfo
->indices
[symndx
] == -1)
11267 /* Otherwise output the local symbol now. */
11268 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11269 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11274 name
= bfd_elf_string_from_elf_section (input_bfd
,
11275 symtab_hdr
->sh_link
,
11280 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11282 if (sym
.st_shndx
== SHN_BAD
)
11285 sym
.st_value
+= o
->output_offset
;
11287 indx
= bfd_get_symcount (output_bfd
);
11288 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11293 flinfo
->indices
[symndx
] = indx
;
11297 elf_section_data (osec
)->this_hdr
.sh_info
11298 = flinfo
->indices
[symndx
];
11302 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11303 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11306 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11308 /* Section was created by _bfd_elf_link_create_dynamic_sections
11313 /* Get the contents of the section. They have been cached by a
11314 relaxation routine. Note that o is a section in an input
11315 file, so the contents field will not have been set by any of
11316 the routines which work on output files. */
11317 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11319 contents
= elf_section_data (o
)->this_hdr
.contents
;
11320 if (bed
->caches_rawsize
11322 && o
->rawsize
< o
->size
)
11324 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11325 contents
= flinfo
->contents
;
11330 contents
= flinfo
->contents
;
11331 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
11335 if ((o
->flags
& SEC_RELOC
) != 0)
11337 Elf_Internal_Rela
*internal_relocs
;
11338 Elf_Internal_Rela
*rel
, *relend
;
11339 int action_discarded
;
11342 /* Get the swapped relocs. */
11344 = _bfd_elf_link_info_read_relocs (input_bfd
, flinfo
->info
, o
,
11345 flinfo
->external_relocs
,
11346 flinfo
->internal_relocs
,
11348 if (internal_relocs
== NULL
11349 && o
->reloc_count
> 0)
11352 action_discarded
= -1;
11353 if (!elf_section_ignore_discarded_relocs (o
))
11354 action_discarded
= (*bed
->action_discarded
) (o
);
11356 /* Run through the relocs evaluating complex reloc symbols and
11357 looking for relocs against symbols from discarded sections
11358 or section symbols from removed link-once sections.
11359 Complain about relocs against discarded sections. Zero
11360 relocs against removed link-once sections. */
11362 rel
= internal_relocs
;
11363 relend
= rel
+ o
->reloc_count
;
11364 for ( ; rel
< relend
; rel
++)
11366 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11367 unsigned int s_type
;
11368 asection
**ps
, *sec
;
11369 struct elf_link_hash_entry
*h
= NULL
;
11370 const char *sym_name
;
11372 if (r_symndx
== STN_UNDEF
)
11375 if (r_symndx
>= locsymcount
11376 || (elf_bad_symtab (input_bfd
)
11377 && flinfo
->sections
[r_symndx
] == NULL
))
11379 h
= sym_hashes
[r_symndx
- extsymoff
];
11381 /* Badly formatted input files can contain relocs that
11382 reference non-existant symbols. Check here so that
11383 we do not seg fault. */
11387 /* xgettext:c-format */
11388 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11389 "that references a non-existent global symbol"),
11390 input_bfd
, (uint64_t) rel
->r_info
, o
);
11391 bfd_set_error (bfd_error_bad_value
);
11395 while (h
->root
.type
== bfd_link_hash_indirect
11396 || h
->root
.type
== bfd_link_hash_warning
)
11397 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11401 /* If a plugin symbol is referenced from a non-IR file,
11402 mark the symbol as undefined. Note that the
11403 linker may attach linker created dynamic sections
11404 to the plugin bfd. Symbols defined in linker
11405 created sections are not plugin symbols. */
11406 if ((h
->root
.non_ir_ref_regular
11407 || h
->root
.non_ir_ref_dynamic
)
11408 && (h
->root
.type
== bfd_link_hash_defined
11409 || h
->root
.type
== bfd_link_hash_defweak
)
11410 && (h
->root
.u
.def
.section
->flags
11411 & SEC_LINKER_CREATED
) == 0
11412 && h
->root
.u
.def
.section
->owner
!= NULL
11413 && (h
->root
.u
.def
.section
->owner
->flags
11414 & BFD_PLUGIN
) != 0)
11416 h
->root
.type
= bfd_link_hash_undefined
;
11417 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11421 if (h
->root
.type
== bfd_link_hash_defined
11422 || h
->root
.type
== bfd_link_hash_defweak
)
11423 ps
= &h
->root
.u
.def
.section
;
11425 sym_name
= h
->root
.root
.string
;
11429 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11431 s_type
= ELF_ST_TYPE (sym
->st_info
);
11432 ps
= &flinfo
->sections
[r_symndx
];
11433 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11437 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11438 && !bfd_link_relocatable (flinfo
->info
))
11441 bfd_vma dot
= (rel
->r_offset
11442 + o
->output_offset
+ o
->output_section
->vma
);
11444 printf ("Encountered a complex symbol!");
11445 printf (" (input_bfd %s, section %s, reloc %ld\n",
11446 bfd_get_filename (input_bfd
), o
->name
,
11447 (long) (rel
- internal_relocs
));
11448 printf (" symbol: idx %8.8lx, name %s\n",
11449 r_symndx
, sym_name
);
11450 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11451 (unsigned long) rel
->r_info
,
11452 (unsigned long) rel
->r_offset
);
11454 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11455 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11458 /* Symbol evaluated OK. Update to absolute value. */
11459 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11464 if (action_discarded
!= -1 && ps
!= NULL
)
11466 /* Complain if the definition comes from a
11467 discarded section. */
11468 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11470 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11471 if (action_discarded
& COMPLAIN
)
11472 (*flinfo
->info
->callbacks
->einfo
)
11473 /* xgettext:c-format */
11474 (_("%X`%s' referenced in section `%pA' of %pB: "
11475 "defined in discarded section `%pA' of %pB\n"),
11476 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11478 /* Try to do the best we can to support buggy old
11479 versions of gcc. Pretend that the symbol is
11480 really defined in the kept linkonce section.
11481 FIXME: This is quite broken. Modifying the
11482 symbol here means we will be changing all later
11483 uses of the symbol, not just in this section. */
11484 if (action_discarded
& PRETEND
)
11488 kept
= _bfd_elf_check_kept_section (sec
,
11500 /* Relocate the section by invoking a back end routine.
11502 The back end routine is responsible for adjusting the
11503 section contents as necessary, and (if using Rela relocs
11504 and generating a relocatable output file) adjusting the
11505 reloc addend as necessary.
11507 The back end routine does not have to worry about setting
11508 the reloc address or the reloc symbol index.
11510 The back end routine is given a pointer to the swapped in
11511 internal symbols, and can access the hash table entries
11512 for the external symbols via elf_sym_hashes (input_bfd).
11514 When generating relocatable output, the back end routine
11515 must handle STB_LOCAL/STT_SECTION symbols specially. The
11516 output symbol is going to be a section symbol
11517 corresponding to the output section, which will require
11518 the addend to be adjusted. */
11520 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11521 input_bfd
, o
, contents
,
11529 || bfd_link_relocatable (flinfo
->info
)
11530 || flinfo
->info
->emitrelocations
)
11532 Elf_Internal_Rela
*irela
;
11533 Elf_Internal_Rela
*irelaend
, *irelamid
;
11534 bfd_vma last_offset
;
11535 struct elf_link_hash_entry
**rel_hash
;
11536 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11537 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11538 unsigned int next_erel
;
11540 struct bfd_elf_section_data
*esdi
, *esdo
;
11542 esdi
= elf_section_data (o
);
11543 esdo
= elf_section_data (o
->output_section
);
11544 rela_normal
= false;
11546 /* Adjust the reloc addresses and symbol indices. */
11548 irela
= internal_relocs
;
11549 irelaend
= irela
+ o
->reloc_count
;
11550 rel_hash
= PTR_ADD (esdo
->rel
.hashes
, esdo
->rel
.count
);
11551 /* We start processing the REL relocs, if any. When we reach
11552 IRELAMID in the loop, we switch to the RELA relocs. */
11554 if (esdi
->rel
.hdr
!= NULL
)
11555 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11556 * bed
->s
->int_rels_per_ext_rel
);
11557 rel_hash_list
= rel_hash
;
11558 rela_hash_list
= NULL
;
11559 last_offset
= o
->output_offset
;
11560 if (!bfd_link_relocatable (flinfo
->info
))
11561 last_offset
+= o
->output_section
->vma
;
11562 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11564 unsigned long r_symndx
;
11566 Elf_Internal_Sym sym
;
11568 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11574 if (irela
== irelamid
)
11576 rel_hash
= PTR_ADD (esdo
->rela
.hashes
, esdo
->rela
.count
);
11577 rela_hash_list
= rel_hash
;
11578 rela_normal
= bed
->rela_normal
;
11581 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11584 if (irela
->r_offset
>= (bfd_vma
) -2)
11586 /* This is a reloc for a deleted entry or somesuch.
11587 Turn it into an R_*_NONE reloc, at the same
11588 offset as the last reloc. elf_eh_frame.c and
11589 bfd_elf_discard_info rely on reloc offsets
11591 irela
->r_offset
= last_offset
;
11593 irela
->r_addend
= 0;
11597 irela
->r_offset
+= o
->output_offset
;
11599 /* Relocs in an executable have to be virtual addresses. */
11600 if (!bfd_link_relocatable (flinfo
->info
))
11601 irela
->r_offset
+= o
->output_section
->vma
;
11603 last_offset
= irela
->r_offset
;
11605 r_symndx
= irela
->r_info
>> r_sym_shift
;
11606 if (r_symndx
== STN_UNDEF
)
11609 if (r_symndx
>= locsymcount
11610 || (elf_bad_symtab (input_bfd
)
11611 && flinfo
->sections
[r_symndx
] == NULL
))
11613 struct elf_link_hash_entry
*rh
;
11614 unsigned long indx
;
11616 /* This is a reloc against a global symbol. We
11617 have not yet output all the local symbols, so
11618 we do not know the symbol index of any global
11619 symbol. We set the rel_hash entry for this
11620 reloc to point to the global hash table entry
11621 for this symbol. The symbol index is then
11622 set at the end of bfd_elf_final_link. */
11623 indx
= r_symndx
- extsymoff
;
11624 rh
= elf_sym_hashes (input_bfd
)[indx
];
11625 while (rh
->root
.type
== bfd_link_hash_indirect
11626 || rh
->root
.type
== bfd_link_hash_warning
)
11627 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11629 /* Setting the index to -2 tells
11630 elf_link_output_extsym that this symbol is
11631 used by a reloc. */
11632 BFD_ASSERT (rh
->indx
< 0);
11639 /* This is a reloc against a local symbol. */
11642 sym
= isymbuf
[r_symndx
];
11643 sec
= flinfo
->sections
[r_symndx
];
11644 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11646 /* I suppose the backend ought to fill in the
11647 section of any STT_SECTION symbol against a
11648 processor specific section. */
11649 r_symndx
= STN_UNDEF
;
11650 if (bfd_is_abs_section (sec
))
11652 else if (sec
== NULL
|| sec
->owner
== NULL
)
11654 bfd_set_error (bfd_error_bad_value
);
11659 asection
*osec
= sec
->output_section
;
11661 /* If we have discarded a section, the output
11662 section will be the absolute section. In
11663 case of discarded SEC_MERGE sections, use
11664 the kept section. relocate_section should
11665 have already handled discarded linkonce
11667 if (bfd_is_abs_section (osec
)
11668 && sec
->kept_section
!= NULL
11669 && sec
->kept_section
->output_section
!= NULL
)
11671 osec
= sec
->kept_section
->output_section
;
11672 irela
->r_addend
-= osec
->vma
;
11675 if (!bfd_is_abs_section (osec
))
11677 r_symndx
= osec
->target_index
;
11678 if (r_symndx
== STN_UNDEF
)
11680 irela
->r_addend
+= osec
->vma
;
11681 osec
= _bfd_nearby_section (output_bfd
, osec
,
11683 irela
->r_addend
-= osec
->vma
;
11684 r_symndx
= osec
->target_index
;
11689 /* Adjust the addend according to where the
11690 section winds up in the output section. */
11692 irela
->r_addend
+= sec
->output_offset
;
11696 if (flinfo
->indices
[r_symndx
] == -1)
11698 unsigned long shlink
;
11703 if (flinfo
->info
->strip
== strip_all
)
11705 /* You can't do ld -r -s. */
11706 bfd_set_error (bfd_error_invalid_operation
);
11710 /* This symbol was skipped earlier, but
11711 since it is needed by a reloc, we
11712 must output it now. */
11713 shlink
= symtab_hdr
->sh_link
;
11714 name
= (bfd_elf_string_from_elf_section
11715 (input_bfd
, shlink
, sym
.st_name
));
11719 osec
= sec
->output_section
;
11721 _bfd_elf_section_from_bfd_section (output_bfd
,
11723 if (sym
.st_shndx
== SHN_BAD
)
11726 sym
.st_value
+= sec
->output_offset
;
11727 if (!bfd_link_relocatable (flinfo
->info
))
11729 sym
.st_value
+= osec
->vma
;
11730 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11732 struct elf_link_hash_table
*htab
11733 = elf_hash_table (flinfo
->info
);
11735 /* STT_TLS symbols are relative to PT_TLS
11737 if (htab
->tls_sec
!= NULL
)
11738 sym
.st_value
-= htab
->tls_sec
->vma
;
11741 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11746 indx
= bfd_get_symcount (output_bfd
);
11747 ret
= elf_link_output_symstrtab (flinfo
, name
,
11753 flinfo
->indices
[r_symndx
] = indx
;
11758 r_symndx
= flinfo
->indices
[r_symndx
];
11761 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11762 | (irela
->r_info
& r_type_mask
));
11765 /* Swap out the relocs. */
11766 input_rel_hdr
= esdi
->rel
.hdr
;
11767 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11769 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11774 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11775 * bed
->s
->int_rels_per_ext_rel
);
11776 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11779 input_rela_hdr
= esdi
->rela
.hdr
;
11780 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11782 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11791 /* Write out the modified section contents. */
11792 if (bed
->elf_backend_write_section
11793 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11796 /* Section written out. */
11798 else switch (o
->sec_info_type
)
11800 case SEC_INFO_TYPE_STABS
:
11801 if (! (_bfd_write_section_stabs
11803 &elf_hash_table (flinfo
->info
)->stab_info
,
11804 o
, &elf_section_data (o
)->sec_info
, contents
)))
11807 case SEC_INFO_TYPE_MERGE
:
11808 if (! _bfd_write_merged_section (output_bfd
, o
,
11809 elf_section_data (o
)->sec_info
))
11812 case SEC_INFO_TYPE_EH_FRAME
:
11814 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11819 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11821 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11829 if (! (o
->flags
& SEC_EXCLUDE
))
11831 file_ptr offset
= (file_ptr
) o
->output_offset
;
11832 bfd_size_type todo
= o
->size
;
11834 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11836 if ((o
->flags
& SEC_ELF_REVERSE_COPY
)
11837 && o
->size
> address_size
)
11839 /* Reverse-copy input section to output. */
11841 if ((o
->size
& (address_size
- 1)) != 0
11842 || (o
->reloc_count
!= 0
11843 && (o
->size
* bed
->s
->int_rels_per_ext_rel
11844 != o
->reloc_count
* address_size
)))
11847 /* xgettext:c-format */
11848 (_("error: %pB: size of section %pA is not "
11849 "multiple of address size"),
11851 bfd_set_error (bfd_error_bad_value
);
11857 todo
-= address_size
;
11858 if (! bfd_set_section_contents (output_bfd
,
11866 offset
+= address_size
;
11870 else if (! bfd_set_section_contents (output_bfd
,
11884 /* Generate a reloc when linking an ELF file. This is a reloc
11885 requested by the linker, and does not come from any input file. This
11886 is used to build constructor and destructor tables when linking
11890 elf_reloc_link_order (bfd
*output_bfd
,
11891 struct bfd_link_info
*info
,
11892 asection
*output_section
,
11893 struct bfd_link_order
*link_order
)
11895 reloc_howto_type
*howto
;
11899 struct bfd_elf_section_reloc_data
*reldata
;
11900 struct elf_link_hash_entry
**rel_hash_ptr
;
11901 Elf_Internal_Shdr
*rel_hdr
;
11902 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11903 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11906 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11908 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11911 bfd_set_error (bfd_error_bad_value
);
11915 addend
= link_order
->u
.reloc
.p
->addend
;
11918 reldata
= &esdo
->rel
;
11919 else if (esdo
->rela
.hdr
)
11920 reldata
= &esdo
->rela
;
11927 /* Figure out the symbol index. */
11928 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11929 if (link_order
->type
== bfd_section_reloc_link_order
)
11931 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11932 BFD_ASSERT (indx
!= 0);
11933 *rel_hash_ptr
= NULL
;
11937 struct elf_link_hash_entry
*h
;
11939 /* Treat a reloc against a defined symbol as though it were
11940 actually against the section. */
11941 h
= ((struct elf_link_hash_entry
*)
11942 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11943 link_order
->u
.reloc
.p
->u
.name
,
11944 false, false, true));
11946 && (h
->root
.type
== bfd_link_hash_defined
11947 || h
->root
.type
== bfd_link_hash_defweak
))
11951 section
= h
->root
.u
.def
.section
;
11952 indx
= section
->output_section
->target_index
;
11953 *rel_hash_ptr
= NULL
;
11954 /* It seems that we ought to add the symbol value to the
11955 addend here, but in practice it has already been added
11956 because it was passed to constructor_callback. */
11957 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11959 else if (h
!= NULL
)
11961 /* Setting the index to -2 tells elf_link_output_extsym that
11962 this symbol is used by a reloc. */
11969 (*info
->callbacks
->unattached_reloc
)
11970 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11975 /* If this is an inplace reloc, we must write the addend into the
11977 if (howto
->partial_inplace
&& addend
!= 0)
11979 bfd_size_type size
;
11980 bfd_reloc_status_type rstat
;
11983 const char *sym_name
;
11984 bfd_size_type octets
;
11986 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11987 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11988 if (buf
== NULL
&& size
!= 0)
11990 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11997 case bfd_reloc_outofrange
:
12000 case bfd_reloc_overflow
:
12001 if (link_order
->type
== bfd_section_reloc_link_order
)
12002 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
12004 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
12005 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
12006 howto
->name
, addend
, NULL
, NULL
,
12011 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
12013 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
12020 /* The address of a reloc is relative to the section in a
12021 relocatable file, and is a virtual address in an executable
12023 offset
= link_order
->offset
;
12024 if (! bfd_link_relocatable (info
))
12025 offset
+= output_section
->vma
;
12027 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
12029 irel
[i
].r_offset
= offset
;
12030 irel
[i
].r_info
= 0;
12031 irel
[i
].r_addend
= 0;
12033 if (bed
->s
->arch_size
== 32)
12034 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
12036 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
12038 rel_hdr
= reldata
->hdr
;
12039 erel
= rel_hdr
->contents
;
12040 if (rel_hdr
->sh_type
== SHT_REL
)
12042 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
12043 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
12047 irel
[0].r_addend
= addend
;
12048 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
12049 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
12057 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12058 Returns TRUE upon success, FALSE otherwise. */
12061 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
12065 const struct elf_backend_data
*bed
;
12067 enum bfd_architecture arch
;
12069 asymbol
**sympp
= NULL
;
12073 elf_symbol_type
*osymbuf
;
12076 implib_bfd
= info
->out_implib_bfd
;
12077 bed
= get_elf_backend_data (abfd
);
12079 if (!bfd_set_format (implib_bfd
, bfd_object
))
12082 /* Use flag from executable but make it a relocatable object. */
12083 flags
= bfd_get_file_flags (abfd
);
12084 flags
&= ~HAS_RELOC
;
12085 if (!bfd_set_start_address (implib_bfd
, 0)
12086 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
12089 /* Copy architecture of output file to import library file. */
12090 arch
= bfd_get_arch (abfd
);
12091 mach
= bfd_get_mach (abfd
);
12092 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
12093 && (abfd
->target_defaulted
12094 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
12097 /* Get symbol table size. */
12098 symsize
= bfd_get_symtab_upper_bound (abfd
);
12102 /* Read in the symbol table. */
12103 sympp
= (asymbol
**) bfd_malloc (symsize
);
12107 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
12111 /* Allow the BFD backend to copy any private header data it
12112 understands from the output BFD to the import library BFD. */
12113 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
12116 /* Filter symbols to appear in the import library. */
12117 if (bed
->elf_backend_filter_implib_symbols
)
12118 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
12121 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
12124 bfd_set_error (bfd_error_no_symbols
);
12125 _bfd_error_handler (_("%pB: no symbol found for import library"),
12131 /* Make symbols absolute. */
12132 amt
= symcount
* sizeof (*osymbuf
);
12133 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
12134 if (osymbuf
== NULL
)
12137 for (src_count
= 0; src_count
< symcount
; src_count
++)
12139 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
12140 sizeof (*osymbuf
));
12141 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
12142 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
12143 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
12144 osymbuf
[src_count
].internal_elf_sym
.st_value
=
12145 osymbuf
[src_count
].symbol
.value
;
12146 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
12149 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
12151 /* Allow the BFD backend to copy any private data it understands
12152 from the output BFD to the import library BFD. This is done last
12153 to permit the routine to look at the filtered symbol table. */
12154 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
12157 if (!bfd_close (implib_bfd
))
12168 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
12172 if (flinfo
->symstrtab
!= NULL
)
12173 _bfd_elf_strtab_free (flinfo
->symstrtab
);
12174 free (flinfo
->contents
);
12175 free (flinfo
->external_relocs
);
12176 free (flinfo
->internal_relocs
);
12177 free (flinfo
->external_syms
);
12178 free (flinfo
->locsym_shndx
);
12179 free (flinfo
->internal_syms
);
12180 free (flinfo
->indices
);
12181 free (flinfo
->sections
);
12182 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12183 free (flinfo
->symshndxbuf
);
12184 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12186 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12187 free (esdo
->rel
.hashes
);
12188 free (esdo
->rela
.hashes
);
12192 /* Do the final step of an ELF link. */
12195 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12200 struct elf_final_link_info flinfo
;
12202 struct bfd_link_order
*p
;
12204 bfd_size_type max_contents_size
;
12205 bfd_size_type max_external_reloc_size
;
12206 bfd_size_type max_internal_reloc_count
;
12207 bfd_size_type max_sym_count
;
12208 bfd_size_type max_sym_shndx_count
;
12209 Elf_Internal_Sym elfsym
;
12211 Elf_Internal_Shdr
*symtab_hdr
;
12212 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12213 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12214 struct elf_outext_info eoinfo
;
12216 size_t relativecount
;
12217 size_t relr_entsize
;
12218 asection
*reldyn
= 0;
12220 asection
*attr_section
= NULL
;
12221 bfd_vma attr_size
= 0;
12222 const char *std_attrs_section
;
12223 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12224 bool sections_removed
;
12227 if (!is_elf_hash_table (&htab
->root
))
12230 if (bfd_link_pic (info
))
12231 abfd
->flags
|= DYNAMIC
;
12233 dynamic
= htab
->dynamic_sections_created
;
12234 dynobj
= htab
->dynobj
;
12236 emit_relocs
= (bfd_link_relocatable (info
)
12237 || info
->emitrelocations
);
12239 memset (&flinfo
, 0, sizeof (flinfo
));
12240 flinfo
.info
= info
;
12241 flinfo
.output_bfd
= abfd
;
12242 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12243 if (flinfo
.symstrtab
== NULL
)
12248 flinfo
.hash_sec
= NULL
;
12249 flinfo
.symver_sec
= NULL
;
12253 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12254 /* Note that dynsym_sec can be NULL (on VMS). */
12255 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12256 /* Note that it is OK if symver_sec is NULL. */
12259 if (info
->unique_symbol
12260 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12261 local_hash_newfunc
,
12262 sizeof (struct local_hash_entry
)))
12265 /* The object attributes have been merged. Remove the input
12266 sections from the link, and set the contents of the output
12268 sections_removed
= false;
12269 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12270 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12272 bool remove_section
= false;
12274 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12275 || strcmp (o
->name
, ".gnu.attributes") == 0)
12277 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12279 asection
*input_section
;
12281 if (p
->type
!= bfd_indirect_link_order
)
12283 input_section
= p
->u
.indirect
.section
;
12284 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12285 elf_link_input_bfd ignores this section. */
12286 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12289 attr_size
= bfd_elf_obj_attr_size (abfd
);
12290 bfd_set_section_size (o
, attr_size
);
12291 /* Skip this section later on. */
12292 o
->map_head
.link_order
= NULL
;
12296 remove_section
= true;
12298 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12300 /* Remove empty group section from linker output. */
12301 remove_section
= true;
12303 if (remove_section
)
12305 o
->flags
|= SEC_EXCLUDE
;
12306 bfd_section_list_remove (abfd
, o
);
12307 abfd
->section_count
--;
12308 sections_removed
= true;
12311 if (sections_removed
)
12312 _bfd_fix_excluded_sec_syms (abfd
, info
);
12314 /* Count up the number of relocations we will output for each output
12315 section, so that we know the sizes of the reloc sections. We
12316 also figure out some maximum sizes. */
12317 max_contents_size
= 0;
12318 max_external_reloc_size
= 0;
12319 max_internal_reloc_count
= 0;
12321 max_sym_shndx_count
= 0;
12323 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12325 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12326 o
->reloc_count
= 0;
12328 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12330 unsigned int reloc_count
= 0;
12331 unsigned int additional_reloc_count
= 0;
12332 struct bfd_elf_section_data
*esdi
= NULL
;
12334 if (p
->type
== bfd_section_reloc_link_order
12335 || p
->type
== bfd_symbol_reloc_link_order
)
12337 else if (p
->type
== bfd_indirect_link_order
)
12341 sec
= p
->u
.indirect
.section
;
12343 /* Mark all sections which are to be included in the
12344 link. This will normally be every section. We need
12345 to do this so that we can identify any sections which
12346 the linker has decided to not include. */
12347 sec
->linker_mark
= true;
12349 if (sec
->flags
& SEC_MERGE
)
12352 if (sec
->rawsize
> max_contents_size
)
12353 max_contents_size
= sec
->rawsize
;
12354 if (sec
->size
> max_contents_size
)
12355 max_contents_size
= sec
->size
;
12357 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12358 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12362 /* We are interested in just local symbols, not all
12364 if (elf_bad_symtab (sec
->owner
))
12365 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12366 / bed
->s
->sizeof_sym
);
12368 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12370 if (sym_count
> max_sym_count
)
12371 max_sym_count
= sym_count
;
12373 if (sym_count
> max_sym_shndx_count
12374 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12375 max_sym_shndx_count
= sym_count
;
12377 if (esdo
->this_hdr
.sh_type
== SHT_REL
12378 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
12379 /* Some backends use reloc_count in relocation sections
12380 to count particular types of relocs. Of course,
12381 reloc sections themselves can't have relocations. */
12383 else if (emit_relocs
)
12385 reloc_count
= sec
->reloc_count
;
12386 if (bed
->elf_backend_count_additional_relocs
)
12389 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12390 additional_reloc_count
+= c
;
12393 else if (bed
->elf_backend_count_relocs
)
12394 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12396 esdi
= elf_section_data (sec
);
12398 if ((sec
->flags
& SEC_RELOC
) != 0)
12400 size_t ext_size
= 0;
12402 if (esdi
->rel
.hdr
!= NULL
)
12403 ext_size
= esdi
->rel
.hdr
->sh_size
;
12404 if (esdi
->rela
.hdr
!= NULL
)
12405 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12407 if (ext_size
> max_external_reloc_size
)
12408 max_external_reloc_size
= ext_size
;
12409 if (sec
->reloc_count
> max_internal_reloc_count
)
12410 max_internal_reloc_count
= sec
->reloc_count
;
12415 if (reloc_count
== 0)
12418 reloc_count
+= additional_reloc_count
;
12419 o
->reloc_count
+= reloc_count
;
12421 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12425 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12426 esdo
->rel
.count
+= additional_reloc_count
;
12428 if (esdi
->rela
.hdr
)
12430 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12431 esdo
->rela
.count
+= additional_reloc_count
;
12437 esdo
->rela
.count
+= reloc_count
;
12439 esdo
->rel
.count
+= reloc_count
;
12443 if (o
->reloc_count
> 0)
12444 o
->flags
|= SEC_RELOC
;
12447 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12448 set it (this is probably a bug) and if it is set
12449 assign_section_numbers will create a reloc section. */
12450 o
->flags
&=~ SEC_RELOC
;
12453 /* If the SEC_ALLOC flag is not set, force the section VMA to
12454 zero. This is done in elf_fake_sections as well, but forcing
12455 the VMA to 0 here will ensure that relocs against these
12456 sections are handled correctly. */
12457 if ((o
->flags
& SEC_ALLOC
) == 0
12458 && ! o
->user_set_vma
)
12462 if (! bfd_link_relocatable (info
) && merged
)
12463 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12465 /* Figure out the file positions for everything but the symbol table
12466 and the relocs. We set symcount to force assign_section_numbers
12467 to create a symbol table. */
12468 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12469 BFD_ASSERT (! abfd
->output_has_begun
);
12470 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12473 /* Set sizes, and assign file positions for reloc sections. */
12474 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12476 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12477 if ((o
->flags
& SEC_RELOC
) != 0)
12480 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12484 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12488 /* _bfd_elf_compute_section_file_positions makes temporary use
12489 of target_index. Reset it. */
12490 o
->target_index
= 0;
12492 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12493 to count upwards while actually outputting the relocations. */
12494 esdo
->rel
.count
= 0;
12495 esdo
->rela
.count
= 0;
12497 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12498 && !bfd_section_is_ctf (o
))
12500 /* Cache the section contents so that they can be compressed
12501 later. Use bfd_malloc since it will be freed by
12502 bfd_compress_section_contents. */
12503 unsigned char *contents
= esdo
->this_hdr
.contents
;
12504 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
12507 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12508 if (contents
== NULL
)
12510 esdo
->this_hdr
.contents
= contents
;
12514 /* We have now assigned file positions for all the sections except .symtab,
12515 .strtab, and non-loaded reloc and compressed debugging sections. We start
12516 the .symtab section at the current file position, and write directly to it.
12517 We build the .strtab section in memory. */
12518 abfd
->symcount
= 0;
12519 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12520 /* sh_name is set in prep_headers. */
12521 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12522 /* sh_flags, sh_addr and sh_size all start off zero. */
12523 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12524 /* sh_link is set in assign_section_numbers. */
12525 /* sh_info is set below. */
12526 /* sh_offset is set just below. */
12527 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12529 if (max_sym_count
< 20)
12530 max_sym_count
= 20;
12531 htab
->strtabsize
= max_sym_count
;
12532 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12533 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12534 if (htab
->strtab
== NULL
)
12536 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12538 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12539 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12541 if (info
->strip
!= strip_all
|| emit_relocs
)
12543 file_ptr off
= elf_next_file_pos (abfd
);
12545 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
12547 /* Note that at this point elf_next_file_pos (abfd) is
12548 incorrect. We do not yet know the size of the .symtab section.
12549 We correct next_file_pos below, after we do know the size. */
12551 /* Start writing out the symbol table. The first symbol is always a
12553 elfsym
.st_value
= 0;
12554 elfsym
.st_size
= 0;
12555 elfsym
.st_info
= 0;
12556 elfsym
.st_other
= 0;
12557 elfsym
.st_shndx
= SHN_UNDEF
;
12558 elfsym
.st_target_internal
= 0;
12559 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12560 bfd_und_section_ptr
, NULL
) != 1)
12563 /* Output a symbol for each section if asked or they are used for
12564 relocs. These symbols usually have no names. We store the
12565 index of each one in the index field of the section, so that
12566 we can find it again when outputting relocs. */
12568 if (bfd_keep_unused_section_symbols (abfd
) || emit_relocs
)
12570 bool name_local_sections
12571 = (bed
->elf_backend_name_local_section_symbols
12572 && bed
->elf_backend_name_local_section_symbols (abfd
));
12573 const char *name
= NULL
;
12575 elfsym
.st_size
= 0;
12576 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12577 elfsym
.st_other
= 0;
12578 elfsym
.st_value
= 0;
12579 elfsym
.st_target_internal
= 0;
12580 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12582 o
= bfd_section_from_elf_index (abfd
, i
);
12585 o
->target_index
= bfd_get_symcount (abfd
);
12586 elfsym
.st_shndx
= i
;
12587 if (!bfd_link_relocatable (info
))
12588 elfsym
.st_value
= o
->vma
;
12589 if (name_local_sections
)
12591 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12599 /* On some targets like Irix 5 the symbol split between local and global
12600 ones recorded in the sh_info field needs to be done between section
12601 and all other symbols. */
12602 if (bed
->elf_backend_elfsym_local_is_section
12603 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12604 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12606 /* Allocate some memory to hold information read in from the input
12608 if (max_contents_size
!= 0)
12610 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12611 if (flinfo
.contents
== NULL
)
12615 if (max_external_reloc_size
!= 0)
12617 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12618 if (flinfo
.external_relocs
== NULL
)
12622 if (max_internal_reloc_count
!= 0)
12624 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12625 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12626 if (flinfo
.internal_relocs
== NULL
)
12630 if (max_sym_count
!= 0)
12632 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12633 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12634 if (flinfo
.external_syms
== NULL
)
12637 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12638 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12639 if (flinfo
.internal_syms
== NULL
)
12642 amt
= max_sym_count
* sizeof (long);
12643 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12644 if (flinfo
.indices
== NULL
)
12647 amt
= max_sym_count
* sizeof (asection
*);
12648 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12649 if (flinfo
.sections
== NULL
)
12653 if (max_sym_shndx_count
!= 0)
12655 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12656 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12657 if (flinfo
.locsym_shndx
== NULL
)
12663 bfd_vma base
, end
= 0; /* Both bytes. */
12666 for (sec
= htab
->tls_sec
;
12667 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12670 bfd_size_type size
= sec
->size
;
12671 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12674 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12676 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12679 size
= ord
->offset
* opb
+ ord
->size
;
12681 end
= sec
->vma
+ size
/ opb
;
12683 base
= htab
->tls_sec
->vma
;
12684 /* Only align end of TLS section if static TLS doesn't have special
12685 alignment requirements. */
12686 if (bed
->static_tls_alignment
== 1)
12687 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12688 htab
->tls_size
= end
- base
;
12691 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12694 /* Finish relative relocations here after regular symbol processing
12695 is finished if DT_RELR is enabled. */
12696 if (info
->enable_dt_relr
12697 && bed
->finish_relative_relocs
12698 && !bed
->finish_relative_relocs (info
))
12699 info
->callbacks
->einfo
12700 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd
);
12702 /* Since ELF permits relocations to be against local symbols, we
12703 must have the local symbols available when we do the relocations.
12704 Since we would rather only read the local symbols once, and we
12705 would rather not keep them in memory, we handle all the
12706 relocations for a single input file at the same time.
12708 Unfortunately, there is no way to know the total number of local
12709 symbols until we have seen all of them, and the local symbol
12710 indices precede the global symbol indices. This means that when
12711 we are generating relocatable output, and we see a reloc against
12712 a global symbol, we can not know the symbol index until we have
12713 finished examining all the local symbols to see which ones we are
12714 going to output. To deal with this, we keep the relocations in
12715 memory, and don't output them until the end of the link. This is
12716 an unfortunate waste of memory, but I don't see a good way around
12717 it. Fortunately, it only happens when performing a relocatable
12718 link, which is not the common case. FIXME: If keep_memory is set
12719 we could write the relocs out and then read them again; I don't
12720 know how bad the memory loss will be. */
12722 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12723 sub
->output_has_begun
= false;
12724 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12726 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12728 if (p
->type
== bfd_indirect_link_order
12729 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12730 == bfd_target_elf_flavour
)
12731 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12733 if (! sub
->output_has_begun
)
12735 if (! elf_link_input_bfd (&flinfo
, sub
))
12737 sub
->output_has_begun
= true;
12740 else if (p
->type
== bfd_section_reloc_link_order
12741 || p
->type
== bfd_symbol_reloc_link_order
)
12743 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12748 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12750 if (p
->type
== bfd_indirect_link_order
12751 && (bfd_get_flavour (sub
)
12752 == bfd_target_elf_flavour
)
12753 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12754 != bed
->s
->elfclass
))
12756 const char *iclass
, *oclass
;
12758 switch (bed
->s
->elfclass
)
12760 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12761 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12762 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12766 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12768 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12769 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12770 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12774 bfd_set_error (bfd_error_wrong_format
);
12776 /* xgettext:c-format */
12777 (_("%pB: file class %s incompatible with %s"),
12778 sub
, iclass
, oclass
);
12787 /* Free symbol buffer if needed. */
12788 if (!info
->reduce_memory_overheads
)
12790 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12791 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
12793 free (elf_tdata (sub
)->symbuf
);
12794 elf_tdata (sub
)->symbuf
= NULL
;
12800 /* Output any global symbols that got converted to local in a
12801 version script or due to symbol visibility. We do this in a
12802 separate step since ELF requires all local symbols to appear
12803 prior to any global symbols. FIXME: We should only do this if
12804 some global symbols were, in fact, converted to become local.
12805 FIXME: Will this work correctly with the Irix 5 linker? */
12806 eoinfo
.failed
= false;
12807 eoinfo
.flinfo
= &flinfo
;
12808 eoinfo
.localsyms
= true;
12809 eoinfo
.file_sym_done
= false;
12810 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12814 goto return_local_hash_table
;
12817 /* If backend needs to output some local symbols not present in the hash
12818 table, do it now. */
12819 if (bed
->elf_backend_output_arch_local_syms
12820 && (info
->strip
!= strip_all
|| emit_relocs
))
12822 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12823 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
12826 goto return_local_hash_table
;
12830 /* That wrote out all the local symbols. Finish up the symbol table
12831 with the global symbols. Even if we want to strip everything we
12832 can, we still need to deal with those global symbols that got
12833 converted to local in a version script. */
12835 /* The sh_info field records the index of the first non local symbol. */
12836 if (!symtab_hdr
->sh_info
)
12837 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12840 && htab
->dynsym
!= NULL
12841 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12843 Elf_Internal_Sym sym
;
12844 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12846 o
= htab
->dynsym
->output_section
;
12847 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12849 /* Write out the section symbols for the output sections. */
12850 if (bfd_link_pic (info
)
12851 || htab
->is_relocatable_executable
)
12857 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12859 sym
.st_target_internal
= 0;
12861 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12867 dynindx
= elf_section_data (s
)->dynindx
;
12870 indx
= elf_section_data (s
)->this_idx
;
12871 BFD_ASSERT (indx
> 0);
12872 sym
.st_shndx
= indx
;
12873 if (! check_dynsym (abfd
, &sym
))
12876 goto return_local_hash_table
;
12878 sym
.st_value
= s
->vma
;
12879 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12881 /* Inform the linker of the addition of this symbol. */
12883 if (info
->callbacks
->ctf_new_dynsym
)
12884 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
12886 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12890 /* Write out the local dynsyms. */
12891 if (htab
->dynlocal
)
12893 struct elf_link_local_dynamic_entry
*e
;
12894 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12899 /* Copy the internal symbol and turn off visibility.
12900 Note that we saved a word of storage and overwrote
12901 the original st_name with the dynstr_index. */
12903 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12904 sym
.st_shndx
= SHN_UNDEF
;
12906 s
= bfd_section_from_elf_index (e
->input_bfd
,
12909 && s
->output_section
!= NULL
12910 && elf_section_data (s
->output_section
) != NULL
)
12913 elf_section_data (s
->output_section
)->this_idx
;
12914 if (! check_dynsym (abfd
, &sym
))
12917 goto return_local_hash_table
;
12919 sym
.st_value
= (s
->output_section
->vma
12921 + e
->isym
.st_value
);
12924 /* Inform the linker of the addition of this symbol. */
12926 if (info
->callbacks
->ctf_new_dynsym
)
12927 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
12929 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12930 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12935 /* We get the global symbols from the hash table. */
12936 eoinfo
.failed
= false;
12937 eoinfo
.localsyms
= false;
12938 eoinfo
.flinfo
= &flinfo
;
12939 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12943 goto return_local_hash_table
;
12946 /* If backend needs to output some symbols not present in the hash
12947 table, do it now. */
12948 if (bed
->elf_backend_output_arch_syms
12949 && (info
->strip
!= strip_all
|| emit_relocs
))
12951 if (! ((*bed
->elf_backend_output_arch_syms
)
12952 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
12955 goto return_local_hash_table
;
12959 /* Finalize the .strtab section. */
12960 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12962 /* Swap out the .strtab section. */
12963 if (!elf_link_swap_symbols_out (&flinfo
))
12966 goto return_local_hash_table
;
12969 /* Now we know the size of the symtab section. */
12970 if (bfd_get_symcount (abfd
) > 0)
12972 /* Finish up and write out the symbol string table (.strtab)
12974 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12975 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12977 if (elf_symtab_shndx_list (abfd
))
12979 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12981 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12983 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12984 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12985 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12986 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12987 symtab_shndx_hdr
->sh_size
= amt
;
12989 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12992 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12993 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12996 goto return_local_hash_table
;
13001 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
13002 /* sh_name was set in prep_headers. */
13003 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
13004 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
13005 symstrtab_hdr
->sh_addr
= 0;
13006 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
13007 symstrtab_hdr
->sh_entsize
= 0;
13008 symstrtab_hdr
->sh_link
= 0;
13009 symstrtab_hdr
->sh_info
= 0;
13010 /* sh_offset is set just below. */
13011 symstrtab_hdr
->sh_addralign
= 1;
13013 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
13015 elf_next_file_pos (abfd
) = off
;
13017 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
13018 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
13021 goto return_local_hash_table
;
13025 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
13027 _bfd_error_handler (_("%pB: failed to generate import library"),
13028 info
->out_implib_bfd
);
13030 goto return_local_hash_table
;
13033 /* Adjust the relocs to have the correct symbol indices. */
13034 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
13036 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
13039 if ((o
->flags
& SEC_RELOC
) == 0)
13042 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
13043 if (esdo
->rel
.hdr
!= NULL
13044 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
13047 goto return_local_hash_table
;
13049 if (esdo
->rela
.hdr
!= NULL
13050 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
13053 goto return_local_hash_table
;
13056 /* Set the reloc_count field to 0 to prevent write_relocs from
13057 trying to swap the relocs out itself. */
13058 o
->reloc_count
= 0;
13062 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
13063 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
13066 if (htab
->srelrdyn
!= NULL
13067 && htab
->srelrdyn
->output_section
!= NULL
13068 && htab
->srelrdyn
->size
!= 0)
13070 asection
*s
= htab
->srelrdyn
->output_section
;
13071 relr_entsize
= elf_section_data (s
)->this_hdr
.sh_entsize
;
13072 if (relr_entsize
== 0)
13074 relr_entsize
= bed
->s
->arch_size
/ 8;
13075 elf_section_data (s
)->this_hdr
.sh_entsize
= relr_entsize
;
13079 /* If we are linking against a dynamic object, or generating a
13080 shared library, finish up the dynamic linking information. */
13083 bfd_byte
*dyncon
, *dynconend
;
13085 /* Fix up .dynamic entries. */
13086 o
= bfd_get_linker_section (dynobj
, ".dynamic");
13087 BFD_ASSERT (o
!= NULL
);
13089 dyncon
= o
->contents
;
13090 dynconend
= PTR_ADD (o
->contents
, o
->size
);
13091 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13093 Elf_Internal_Dyn dyn
;
13096 bfd_size_type sh_size
;
13099 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13106 if (relativecount
!= 0)
13108 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
13110 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
13111 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
13113 if (dyn
.d_tag
!= DT_NULL
13114 && dynconend
- dyncon
>= bed
->s
->sizeof_dyn
)
13116 dyn
.d_un
.d_val
= relativecount
;
13122 if (relr_entsize
!= 0)
13124 if (dynconend
- dyncon
>= 3 * bed
->s
->sizeof_dyn
)
13126 asection
*s
= htab
->srelrdyn
;
13127 dyn
.d_tag
= DT_RELR
;
13129 = s
->output_section
->vma
+ s
->output_offset
;
13130 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13131 dyncon
+= bed
->s
->sizeof_dyn
;
13133 dyn
.d_tag
= DT_RELRSZ
;
13134 dyn
.d_un
.d_val
= s
->size
;
13135 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13136 dyncon
+= bed
->s
->sizeof_dyn
;
13138 dyn
.d_tag
= DT_RELRENT
;
13139 dyn
.d_un
.d_val
= relr_entsize
;
13148 name
= info
->init_function
;
13151 name
= info
->fini_function
;
13154 struct elf_link_hash_entry
*h
;
13156 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
13158 && (h
->root
.type
== bfd_link_hash_defined
13159 || h
->root
.type
== bfd_link_hash_defweak
))
13161 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
13162 o
= h
->root
.u
.def
.section
;
13163 if (o
->output_section
!= NULL
)
13164 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
13165 + o
->output_offset
);
13168 /* The symbol is imported from another shared
13169 library and does not apply to this one. */
13170 dyn
.d_un
.d_ptr
= 0;
13177 case DT_PREINIT_ARRAYSZ
:
13178 name
= ".preinit_array";
13180 case DT_INIT_ARRAYSZ
:
13181 name
= ".init_array";
13183 case DT_FINI_ARRAYSZ
:
13184 name
= ".fini_array";
13186 o
= bfd_get_section_by_name (abfd
, name
);
13190 (_("could not find section %s"), name
);
13195 (_("warning: %s section has zero size"), name
);
13196 dyn
.d_un
.d_val
= o
->size
;
13199 case DT_PREINIT_ARRAY
:
13200 name
= ".preinit_array";
13202 case DT_INIT_ARRAY
:
13203 name
= ".init_array";
13205 case DT_FINI_ARRAY
:
13206 name
= ".fini_array";
13208 o
= bfd_get_section_by_name (abfd
, name
);
13215 name
= ".gnu.hash";
13224 name
= ".gnu.version_d";
13227 name
= ".gnu.version_r";
13230 name
= ".gnu.version";
13232 o
= bfd_get_linker_section (dynobj
, name
);
13234 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13237 (_("could not find section %s"), name
);
13240 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13243 (_("warning: section '%s' is being made into a note"), name
);
13244 bfd_set_error (bfd_error_nonrepresentable_section
);
13247 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13254 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13260 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13262 Elf_Internal_Shdr
*hdr
;
13264 hdr
= elf_elfsections (abfd
)[i
];
13265 if (hdr
->sh_type
== type
13266 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13268 sh_size
+= hdr
->sh_size
;
13270 || sh_addr
> hdr
->sh_addr
)
13271 sh_addr
= hdr
->sh_addr
;
13275 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13277 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13279 /* Don't count procedure linkage table relocs in the
13280 overall reloc count. */
13281 sh_size
-= htab
->srelplt
->size
;
13283 /* If the size is zero, make the address zero too.
13284 This is to avoid a glibc bug. If the backend
13285 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13286 zero, then we'll put DT_RELA at the end of
13287 DT_JMPREL. glibc will interpret the end of
13288 DT_RELA matching the end of DT_JMPREL as the
13289 case where DT_RELA includes DT_JMPREL, and for
13290 LD_BIND_NOW will decide that processing DT_RELA
13291 will process the PLT relocs too. Net result:
13292 No PLT relocs applied. */
13295 /* If .rela.plt is the first .rela section, exclude
13296 it from DT_RELA. */
13297 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13298 + htab
->srelplt
->output_offset
) * opb
)
13299 sh_addr
+= htab
->srelplt
->size
;
13302 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13303 dyn
.d_un
.d_val
= sh_size
;
13305 dyn
.d_un
.d_ptr
= sh_addr
;
13308 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13312 /* If we have created any dynamic sections, then output them. */
13313 if (dynobj
!= NULL
)
13315 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13318 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13319 if (bfd_link_textrel_check (info
)
13320 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
13323 bfd_byte
*dyncon
, *dynconend
;
13325 dyncon
= o
->contents
;
13326 dynconend
= o
->contents
+ o
->size
;
13327 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13329 Elf_Internal_Dyn dyn
;
13331 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13333 if (dyn
.d_tag
== DT_TEXTREL
)
13335 if (info
->textrel_check
== textrel_check_error
)
13336 info
->callbacks
->einfo
13337 (_("%P%X: read-only segment has dynamic relocations\n"));
13338 else if (bfd_link_dll (info
))
13339 info
->callbacks
->einfo
13340 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13341 else if (bfd_link_pde (info
))
13342 info
->callbacks
->einfo
13343 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13345 info
->callbacks
->einfo
13346 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13352 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13354 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13356 || o
->output_section
== bfd_abs_section_ptr
)
13358 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13360 /* At this point, we are only interested in sections
13361 created by _bfd_elf_link_create_dynamic_sections. */
13364 if (htab
->stab_info
.stabstr
== o
)
13366 if (htab
->eh_info
.hdr_sec
== o
)
13368 if (strcmp (o
->name
, ".dynstr") != 0)
13370 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13371 * bfd_octets_per_byte (abfd
, o
));
13372 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13373 o
->contents
, octets
, o
->size
))
13378 /* The contents of the .dynstr section are actually in a
13382 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13383 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13384 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13390 if (!info
->resolve_section_groups
)
13392 bool failed
= false;
13394 BFD_ASSERT (bfd_link_relocatable (info
));
13395 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13400 /* If we have optimized stabs strings, output them. */
13401 if (htab
->stab_info
.stabstr
!= NULL
)
13403 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13407 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13410 if (info
->callbacks
->emit_ctf
)
13411 info
->callbacks
->emit_ctf ();
13413 elf_final_link_free (abfd
, &flinfo
);
13417 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13418 if (contents
== NULL
)
13420 /* Bail out and fail. */
13422 goto return_local_hash_table
;
13424 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13425 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13429 return_local_hash_table
:
13430 if (info
->unique_symbol
)
13431 bfd_hash_table_free (&flinfo
.local_hash_table
);
13435 elf_final_link_free (abfd
, &flinfo
);
13437 goto return_local_hash_table
;
13440 /* Initialize COOKIE for input bfd ABFD. */
13443 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13444 struct bfd_link_info
*info
, bfd
*abfd
)
13446 Elf_Internal_Shdr
*symtab_hdr
;
13447 const struct elf_backend_data
*bed
;
13449 bed
= get_elf_backend_data (abfd
);
13450 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13452 cookie
->abfd
= abfd
;
13453 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13454 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13455 if (cookie
->bad_symtab
)
13457 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13458 cookie
->extsymoff
= 0;
13462 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13463 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13466 if (bed
->s
->arch_size
== 32)
13467 cookie
->r_sym_shift
= 8;
13469 cookie
->r_sym_shift
= 32;
13471 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13472 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13474 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13475 cookie
->locsymcount
, 0,
13477 if (cookie
->locsyms
== NULL
)
13479 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13482 if (_bfd_link_keep_memory (info
) )
13484 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13485 info
->cache_size
+= (cookie
->locsymcount
13486 * sizeof (Elf_External_Sym_Shndx
));
13492 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13495 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13497 Elf_Internal_Shdr
*symtab_hdr
;
13499 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13500 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13501 free (cookie
->locsyms
);
13504 /* Initialize the relocation information in COOKIE for input section SEC
13505 of input bfd ABFD. */
13508 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13509 struct bfd_link_info
*info
, bfd
*abfd
,
13512 if (sec
->reloc_count
== 0)
13514 cookie
->rels
= NULL
;
13515 cookie
->relend
= NULL
;
13519 cookie
->rels
= _bfd_elf_link_info_read_relocs (abfd
, info
, sec
,
13521 _bfd_link_keep_memory (info
));
13522 if (cookie
->rels
== NULL
)
13524 cookie
->rel
= cookie
->rels
;
13525 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13527 cookie
->rel
= cookie
->rels
;
13531 /* Free the memory allocated by init_reloc_cookie_rels,
13535 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13538 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13539 free (cookie
->rels
);
13542 /* Initialize the whole of COOKIE for input section SEC. */
13545 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13546 struct bfd_link_info
*info
,
13549 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13551 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13556 fini_reloc_cookie (cookie
, sec
->owner
);
13561 /* Free the memory allocated by init_reloc_cookie_for_section,
13565 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13568 fini_reloc_cookie_rels (cookie
, sec
);
13569 fini_reloc_cookie (cookie
, sec
->owner
);
13572 /* Garbage collect unused sections. */
13574 /* Default gc_mark_hook. */
13577 _bfd_elf_gc_mark_hook (asection
*sec
,
13578 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13579 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13580 struct elf_link_hash_entry
*h
,
13581 Elf_Internal_Sym
*sym
)
13585 switch (h
->root
.type
)
13587 case bfd_link_hash_defined
:
13588 case bfd_link_hash_defweak
:
13589 return h
->root
.u
.def
.section
;
13591 case bfd_link_hash_common
:
13592 return h
->root
.u
.c
.p
->section
;
13599 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13604 /* Return the debug definition section. */
13607 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13608 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13609 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13610 struct elf_link_hash_entry
*h
,
13611 Elf_Internal_Sym
*sym
)
13615 /* Return the global debug definition section. */
13616 if ((h
->root
.type
== bfd_link_hash_defined
13617 || h
->root
.type
== bfd_link_hash_defweak
)
13618 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13619 return h
->root
.u
.def
.section
;
13623 /* Return the local debug definition section. */
13624 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13626 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
13633 /* COOKIE->rel describes a relocation against section SEC, which is
13634 a section we've decided to keep. Return the section that contains
13635 the relocation symbol, or NULL if no section contains it. */
13638 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13639 elf_gc_mark_hook_fn gc_mark_hook
,
13640 struct elf_reloc_cookie
*cookie
,
13643 unsigned long r_symndx
;
13644 struct elf_link_hash_entry
*h
, *hw
;
13646 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13647 if (r_symndx
== STN_UNDEF
)
13650 if (r_symndx
>= cookie
->locsymcount
13651 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13655 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13658 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13662 while (h
->root
.type
== bfd_link_hash_indirect
13663 || h
->root
.type
== bfd_link_hash_warning
)
13664 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13666 was_marked
= h
->mark
;
13668 /* Keep all aliases of the symbol too. If an object symbol
13669 needs to be copied into .dynbss then all of its aliases
13670 should be present as dynamic symbols, not just the one used
13671 on the copy relocation. */
13673 while (hw
->is_weakalias
)
13679 if (!was_marked
&& h
->start_stop
&& !h
->root
.ldscript_def
)
13681 if (info
->start_stop_gc
)
13684 /* To work around a glibc bug, mark XXX input sections
13685 when there is a reference to __start_XXX or __stop_XXX
13687 else if (start_stop
!= NULL
)
13689 asection
*s
= h
->u2
.start_stop_section
;
13690 *start_stop
= true;
13695 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13698 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13699 &cookie
->locsyms
[r_symndx
]);
13702 /* COOKIE->rel describes a relocation against section SEC, which is
13703 a section we've decided to keep. Mark the section that contains
13704 the relocation symbol. */
13707 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13709 elf_gc_mark_hook_fn gc_mark_hook
,
13710 struct elf_reloc_cookie
*cookie
)
13713 bool start_stop
= false;
13715 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13716 while (rsec
!= NULL
)
13718 if (!rsec
->gc_mark
)
13720 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13721 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13723 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13728 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13733 /* The mark phase of garbage collection. For a given section, mark
13734 it and any sections in this section's group, and all the sections
13735 which define symbols to which it refers. */
13738 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13740 elf_gc_mark_hook_fn gc_mark_hook
)
13743 asection
*group_sec
, *eh_frame
;
13747 /* Mark all the sections in the group. */
13748 group_sec
= elf_section_data (sec
)->next_in_group
;
13749 if (group_sec
&& !group_sec
->gc_mark
)
13750 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13753 /* Look through the section relocs. */
13755 eh_frame
= elf_eh_frame_section (sec
->owner
);
13756 if ((sec
->flags
& SEC_RELOC
) != 0
13757 && sec
->reloc_count
> 0
13758 && sec
!= eh_frame
)
13760 struct elf_reloc_cookie cookie
;
13762 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13766 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13767 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13772 fini_reloc_cookie_for_section (&cookie
, sec
);
13776 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13778 struct elf_reloc_cookie cookie
;
13780 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13784 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13785 gc_mark_hook
, &cookie
))
13787 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13791 eh_frame
= elf_section_eh_frame_entry (sec
);
13792 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13793 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13799 /* Scan and mark sections in a special or debug section group. */
13802 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13804 /* Point to first section of section group. */
13806 /* Used to iterate the section group. */
13809 bool is_special_grp
= true;
13810 bool is_debug_grp
= true;
13812 /* First scan to see if group contains any section other than debug
13813 and special section. */
13814 ssec
= msec
= elf_next_in_group (grp
);
13817 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13818 is_debug_grp
= false;
13820 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13821 is_special_grp
= false;
13823 msec
= elf_next_in_group (msec
);
13825 while (msec
!= ssec
);
13827 /* If this is a pure debug section group or pure special section group,
13828 keep all sections in this group. */
13829 if (is_debug_grp
|| is_special_grp
)
13834 msec
= elf_next_in_group (msec
);
13836 while (msec
!= ssec
);
13840 /* Keep debug and special sections. */
13843 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13844 elf_gc_mark_hook_fn mark_hook
)
13848 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13852 bool debug_frag_seen
;
13853 bool has_kept_debug_info
;
13855 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13857 isec
= ibfd
->sections
;
13858 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13861 /* Ensure all linker created sections are kept,
13862 see if any other section is already marked,
13863 and note if we have any fragmented debug sections. */
13864 debug_frag_seen
= some_kept
= has_kept_debug_info
= false;
13865 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13867 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13869 else if (isec
->gc_mark
13870 && (isec
->flags
& SEC_ALLOC
) != 0
13871 && elf_section_type (isec
) != SHT_NOTE
)
13875 /* Since all sections, except for backend specific ones,
13876 have been garbage collected, call mark_hook on this
13877 section if any of its linked-to sections is marked. */
13878 asection
*linked_to_sec
;
13879 for (linked_to_sec
= elf_linked_to_section (isec
);
13880 linked_to_sec
!= NULL
&& !linked_to_sec
->linker_mark
;
13881 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13883 if (linked_to_sec
->gc_mark
)
13885 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
13889 linked_to_sec
->linker_mark
= 1;
13891 for (linked_to_sec
= elf_linked_to_section (isec
);
13892 linked_to_sec
!= NULL
&& linked_to_sec
->linker_mark
;
13893 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13894 linked_to_sec
->linker_mark
= 0;
13897 if (!debug_frag_seen
13898 && (isec
->flags
& SEC_DEBUGGING
)
13899 && startswith (isec
->name
, ".debug_line."))
13900 debug_frag_seen
= true;
13901 else if (strcmp (bfd_section_name (isec
),
13902 "__patchable_function_entries") == 0
13903 && elf_linked_to_section (isec
) == NULL
)
13904 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
13905 "need linked-to section "
13906 "for --gc-sections\n"),
13907 isec
->owner
, isec
);
13910 /* If no non-note alloc section in this file will be kept, then
13911 we can toss out the debug and special sections. */
13915 /* Keep debug and special sections like .comment when they are
13916 not part of a group. Also keep section groups that contain
13917 just debug sections or special sections. NB: Sections with
13918 linked-to section has been handled above. */
13919 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13921 if ((isec
->flags
& SEC_GROUP
) != 0)
13922 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13923 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13924 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13925 && elf_next_in_group (isec
) == NULL
13926 && elf_linked_to_section (isec
) == NULL
)
13928 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13929 has_kept_debug_info
= true;
13932 /* Look for CODE sections which are going to be discarded,
13933 and find and discard any fragmented debug sections which
13934 are associated with that code section. */
13935 if (debug_frag_seen
)
13936 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13937 if ((isec
->flags
& SEC_CODE
) != 0
13938 && isec
->gc_mark
== 0)
13943 ilen
= strlen (isec
->name
);
13945 /* Association is determined by the name of the debug
13946 section containing the name of the code section as
13947 a suffix. For example .debug_line.text.foo is a
13948 debug section associated with .text.foo. */
13949 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13953 if (dsec
->gc_mark
== 0
13954 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13957 dlen
= strlen (dsec
->name
);
13960 && strncmp (dsec
->name
+ (dlen
- ilen
),
13961 isec
->name
, ilen
) == 0)
13966 /* Mark debug sections referenced by kept debug sections. */
13967 if (has_kept_debug_info
)
13968 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13970 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13971 if (!_bfd_elf_gc_mark (info
, isec
,
13972 elf_gc_mark_debug_section
))
13979 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13982 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13984 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13988 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13989 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13990 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13993 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13996 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13998 /* When any section in a section group is kept, we keep all
13999 sections in the section group. If the first member of
14000 the section group is excluded, we will also exclude the
14002 if (o
->flags
& SEC_GROUP
)
14004 asection
*first
= elf_next_in_group (o
);
14005 o
->gc_mark
= first
->gc_mark
;
14011 /* Skip sweeping sections already excluded. */
14012 if (o
->flags
& SEC_EXCLUDE
)
14015 /* Since this is early in the link process, it is simple
14016 to remove a section from the output. */
14017 o
->flags
|= SEC_EXCLUDE
;
14019 if (info
->print_gc_sections
&& o
->size
!= 0)
14020 /* xgettext:c-format */
14021 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14029 /* Propagate collected vtable information. This is called through
14030 elf_link_hash_traverse. */
14033 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
14035 /* Those that are not vtables. */
14037 || h
->u2
.vtable
== NULL
14038 || h
->u2
.vtable
->parent
== NULL
)
14041 /* Those vtables that do not have parents, we cannot merge. */
14042 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
14045 /* If we've already been done, exit. */
14046 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
14049 /* Make sure the parent's table is up to date. */
14050 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
14052 if (h
->u2
.vtable
->used
== NULL
)
14054 /* None of this table's entries were referenced. Re-use the
14056 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14057 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
14064 /* Or the parent's entries into ours. */
14065 cu
= h
->u2
.vtable
->used
;
14067 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14070 const struct elf_backend_data
*bed
;
14071 unsigned int log_file_align
;
14073 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
14074 log_file_align
= bed
->s
->log_file_align
;
14075 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
14089 struct link_info_ok
14091 struct bfd_link_info
*info
;
14096 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
,
14100 bfd_vma hstart
, hend
;
14101 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
14102 const struct elf_backend_data
*bed
;
14103 unsigned int log_file_align
;
14104 struct link_info_ok
*info
= (struct link_info_ok
*) ptr
;
14106 /* Take care of both those symbols that do not describe vtables as
14107 well as those that are not loaded. */
14109 || h
->u2
.vtable
== NULL
14110 || h
->u2
.vtable
->parent
== NULL
)
14113 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
14114 || h
->root
.type
== bfd_link_hash_defweak
);
14116 sec
= h
->root
.u
.def
.section
;
14117 hstart
= h
->root
.u
.def
.value
;
14118 hend
= hstart
+ h
->size
;
14120 relstart
= _bfd_elf_link_info_read_relocs (sec
->owner
, info
->info
,
14121 sec
, NULL
, NULL
, true);
14123 return info
->ok
= false;
14124 bed
= get_elf_backend_data (sec
->owner
);
14125 log_file_align
= bed
->s
->log_file_align
;
14127 relend
= relstart
+ sec
->reloc_count
;
14129 for (rel
= relstart
; rel
< relend
; ++rel
)
14130 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
14132 /* If the entry is in use, do nothing. */
14133 if (h
->u2
.vtable
->used
14134 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
14136 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
14137 if (h
->u2
.vtable
->used
[entry
])
14140 /* Otherwise, kill it. */
14141 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
14147 /* Mark sections containing dynamically referenced symbols. When
14148 building shared libraries, we must assume that any visible symbol is
14152 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
14154 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
14155 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
14157 if ((h
->root
.type
== bfd_link_hash_defined
14158 || h
->root
.type
== bfd_link_hash_defweak
)
14160 || h
->root
.ldscript_def
14161 || !info
->start_stop_gc
)
14162 && ((h
->ref_dynamic
&& !h
->forced_local
)
14163 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
14164 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
14165 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
14166 && (!bfd_link_executable (info
)
14167 || info
->gc_keep_exported
14168 || info
->export_dynamic
14171 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
14172 && (h
->versioned
>= versioned
14173 || !bfd_hide_sym_by_version (info
->version_info
,
14174 h
->root
.root
.string
)))))
14175 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14180 /* Keep all sections containing symbols undefined on the command-line,
14181 and the section containing the entry symbol. */
14184 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
14186 struct bfd_sym_chain
*sym
;
14188 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
14190 struct elf_link_hash_entry
*h
;
14192 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
14193 false, false, false);
14196 && (h
->root
.type
== bfd_link_hash_defined
14197 || h
->root
.type
== bfd_link_hash_defweak
)
14198 && !bfd_is_const_section (h
->root
.u
.def
.section
))
14199 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14204 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
14205 struct bfd_link_info
*info
)
14207 bfd
*ibfd
= info
->input_bfds
;
14209 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14212 struct elf_reloc_cookie cookie
;
14214 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14216 sec
= ibfd
->sections
;
14217 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14220 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
14223 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
14225 if (startswith (bfd_section_name (sec
), ".eh_frame_entry")
14226 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
14228 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
14229 fini_reloc_cookie_rels (&cookie
, sec
);
14236 /* Do mark and sweep of unused sections. */
14239 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14243 elf_gc_mark_hook_fn gc_mark_hook
;
14244 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14245 struct elf_link_hash_table
*htab
;
14246 struct link_info_ok info_ok
;
14248 if (!bed
->can_gc_sections
14249 || !is_elf_hash_table (info
->hash
))
14251 _bfd_error_handler(_("warning: gc-sections option ignored"));
14255 bed
->gc_keep (info
);
14256 htab
= elf_hash_table (info
);
14258 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14259 at the .eh_frame section if we can mark the FDEs individually. */
14260 for (sub
= info
->input_bfds
;
14261 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14262 sub
= sub
->link
.next
)
14265 struct elf_reloc_cookie cookie
;
14267 sec
= sub
->sections
;
14268 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14270 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14271 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
14273 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14274 if (elf_section_data (sec
)->sec_info
14275 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14276 elf_eh_frame_section (sub
) = sec
;
14277 fini_reloc_cookie_for_section (&cookie
, sec
);
14278 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14282 /* Apply transitive closure to the vtable entry usage info. */
14283 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14287 /* Kill the vtable relocations that were not used. */
14288 info_ok
.info
= info
;
14290 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &info_ok
);
14294 /* Mark dynamically referenced symbols. */
14295 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14296 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14298 /* Grovel through relocs to find out who stays ... */
14299 gc_mark_hook
= bed
->gc_mark_hook
;
14300 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14304 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14305 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14306 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14310 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14313 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14314 Also treat note sections as a root, if the section is not part
14315 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14316 well as FINI_ARRAY sections for ld -r. */
14317 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14319 && (o
->flags
& SEC_EXCLUDE
) == 0
14320 && ((o
->flags
& SEC_KEEP
) != 0
14321 || (bfd_link_relocatable (info
)
14322 && ((elf_section_data (o
)->this_hdr
.sh_type
14323 == SHT_PREINIT_ARRAY
)
14324 || (elf_section_data (o
)->this_hdr
.sh_type
14326 || (elf_section_data (o
)->this_hdr
.sh_type
14327 == SHT_FINI_ARRAY
)))
14328 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14329 && elf_next_in_group (o
) == NULL
14330 && elf_linked_to_section (o
) == NULL
)
14331 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14332 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14334 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14339 /* Allow the backend to mark additional target specific sections. */
14340 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14342 /* ... and mark SEC_EXCLUDE for those that go. */
14343 return elf_gc_sweep (abfd
, info
);
14346 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14349 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14351 struct elf_link_hash_entry
*h
,
14354 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14355 struct elf_link_hash_entry
**search
, *child
;
14356 size_t extsymcount
;
14357 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14359 /* The sh_info field of the symtab header tells us where the
14360 external symbols start. We don't care about the local symbols at
14362 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14363 if (!elf_bad_symtab (abfd
))
14364 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14366 sym_hashes
= elf_sym_hashes (abfd
);
14367 sym_hashes_end
= PTR_ADD (sym_hashes
, extsymcount
);
14369 /* Hunt down the child symbol, which is in this section at the same
14370 offset as the relocation. */
14371 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14373 if ((child
= *search
) != NULL
14374 && (child
->root
.type
== bfd_link_hash_defined
14375 || child
->root
.type
== bfd_link_hash_defweak
)
14376 && child
->root
.u
.def
.section
== sec
14377 && child
->root
.u
.def
.value
== offset
)
14381 /* xgettext:c-format */
14382 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14383 abfd
, sec
, (uint64_t) offset
);
14384 bfd_set_error (bfd_error_invalid_operation
);
14388 if (!child
->u2
.vtable
)
14390 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14391 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14392 if (!child
->u2
.vtable
)
14397 /* This *should* only be the absolute section. It could potentially
14398 be that someone has defined a non-global vtable though, which
14399 would be bad. It isn't worth paging in the local symbols to be
14400 sure though; that case should simply be handled by the assembler. */
14402 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14405 child
->u2
.vtable
->parent
= h
;
14410 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14413 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14414 struct elf_link_hash_entry
*h
,
14417 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14418 unsigned int log_file_align
= bed
->s
->log_file_align
;
14422 /* xgettext:c-format */
14423 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14425 bfd_set_error (bfd_error_bad_value
);
14431 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14432 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14437 if (addend
>= h
->u2
.vtable
->size
)
14439 size_t size
, bytes
, file_align
;
14440 bool *ptr
= h
->u2
.vtable
->used
;
14442 /* While the symbol is undefined, we have to be prepared to handle
14444 file_align
= 1 << log_file_align
;
14445 if (h
->root
.type
== bfd_link_hash_undefined
)
14446 size
= addend
+ file_align
;
14450 if (addend
>= size
)
14452 /* Oops! We've got a reference past the defined end of
14453 the table. This is probably a bug -- shall we warn? */
14454 size
= addend
+ file_align
;
14457 size
= (size
+ file_align
- 1) & -file_align
;
14459 /* Allocate one extra entry for use as a "done" flag for the
14460 consolidation pass. */
14461 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bool);
14465 ptr
= (bool *) bfd_realloc (ptr
- 1, bytes
);
14471 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14473 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14477 ptr
= (bool *) bfd_zmalloc (bytes
);
14482 /* And arrange for that done flag to be at index -1. */
14483 h
->u2
.vtable
->used
= ptr
+ 1;
14484 h
->u2
.vtable
->size
= size
;
14487 h
->u2
.vtable
->used
[addend
>> log_file_align
] = true;
14492 /* Map an ELF section header flag to its corresponding string. */
14496 flagword flag_value
;
14497 } elf_flags_to_name_table
;
14499 static const elf_flags_to_name_table elf_flags_to_names
[] =
14501 { "SHF_WRITE", SHF_WRITE
},
14502 { "SHF_ALLOC", SHF_ALLOC
},
14503 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14504 { "SHF_MERGE", SHF_MERGE
},
14505 { "SHF_STRINGS", SHF_STRINGS
},
14506 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14507 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14508 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14509 { "SHF_GROUP", SHF_GROUP
},
14510 { "SHF_TLS", SHF_TLS
},
14511 { "SHF_MASKOS", SHF_MASKOS
},
14512 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14515 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14517 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14518 struct flag_info
*flaginfo
,
14521 const bfd_vma sh_flags
= elf_section_flags (section
);
14523 if (!flaginfo
->flags_initialized
)
14525 bfd
*obfd
= info
->output_bfd
;
14526 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14527 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14529 int without_hex
= 0;
14531 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14534 flagword (*lookup
) (char *);
14536 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14537 if (lookup
!= NULL
)
14539 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14543 if (tf
->with
== with_flags
)
14544 with_hex
|= hexval
;
14545 else if (tf
->with
== without_flags
)
14546 without_hex
|= hexval
;
14551 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14553 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14555 if (tf
->with
== with_flags
)
14556 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14557 else if (tf
->with
== without_flags
)
14558 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14565 info
->callbacks
->einfo
14566 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14570 flaginfo
->flags_initialized
= true;
14571 flaginfo
->only_with_flags
|= with_hex
;
14572 flaginfo
->not_with_flags
|= without_hex
;
14575 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14578 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14584 struct alloc_got_off_arg
{
14586 struct bfd_link_info
*info
;
14589 /* We need a special top-level link routine to convert got reference counts
14590 to real got offsets. */
14593 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14595 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14596 bfd
*obfd
= gofarg
->info
->output_bfd
;
14597 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14599 if (h
->got
.refcount
> 0)
14601 h
->got
.offset
= gofarg
->gotoff
;
14602 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14605 h
->got
.offset
= (bfd_vma
) -1;
14610 /* And an accompanying bit to work out final got entry offsets once
14611 we're done. Should be called from final_link. */
14614 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14615 struct bfd_link_info
*info
)
14618 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14620 struct alloc_got_off_arg gofarg
;
14622 BFD_ASSERT (abfd
== info
->output_bfd
);
14624 if (! is_elf_hash_table (info
->hash
))
14627 /* The GOT offset is relative to the .got section, but the GOT header is
14628 put into the .got.plt section, if the backend uses it. */
14629 if (bed
->want_got_plt
)
14632 gotoff
= bed
->got_header_size
;
14634 /* Do the local .got entries first. */
14635 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14637 bfd_signed_vma
*local_got
;
14638 size_t j
, locsymcount
;
14639 Elf_Internal_Shdr
*symtab_hdr
;
14641 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14644 local_got
= elf_local_got_refcounts (i
);
14648 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14649 if (elf_bad_symtab (i
))
14650 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14652 locsymcount
= symtab_hdr
->sh_info
;
14654 for (j
= 0; j
< locsymcount
; ++j
)
14656 if (local_got
[j
] > 0)
14658 local_got
[j
] = gotoff
;
14659 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14662 local_got
[j
] = (bfd_vma
) -1;
14666 /* Then the global .got entries. .plt refcounts are handled by
14667 adjust_dynamic_symbol */
14668 gofarg
.gotoff
= gotoff
;
14669 gofarg
.info
= info
;
14670 elf_link_hash_traverse (elf_hash_table (info
),
14671 elf_gc_allocate_got_offsets
,
14676 /* Many folk need no more in the way of final link than this, once
14677 got entry reference counting is enabled. */
14680 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14682 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14685 /* Invoke the regular ELF backend linker to do all the work. */
14686 return bfd_elf_final_link (abfd
, info
);
14690 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14692 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14694 if (rcookie
->bad_symtab
)
14695 rcookie
->rel
= rcookie
->rels
;
14697 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14699 unsigned long r_symndx
;
14701 if (! rcookie
->bad_symtab
)
14702 if (rcookie
->rel
->r_offset
> offset
)
14704 if (rcookie
->rel
->r_offset
!= offset
)
14707 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14708 if (r_symndx
== STN_UNDEF
)
14711 if (r_symndx
>= rcookie
->locsymcount
14712 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14714 struct elf_link_hash_entry
*h
;
14716 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14718 while (h
->root
.type
== bfd_link_hash_indirect
14719 || h
->root
.type
== bfd_link_hash_warning
)
14720 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14722 if ((h
->root
.type
== bfd_link_hash_defined
14723 || h
->root
.type
== bfd_link_hash_defweak
)
14724 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14725 || h
->root
.u
.def
.section
->kept_section
!= NULL
14726 || discarded_section (h
->root
.u
.def
.section
)))
14731 /* It's not a relocation against a global symbol,
14732 but it could be a relocation against a local
14733 symbol for a discarded section. */
14735 Elf_Internal_Sym
*isym
;
14737 /* Need to: get the symbol; get the section. */
14738 isym
= &rcookie
->locsyms
[r_symndx
];
14739 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14741 && (isec
->kept_section
!= NULL
14742 || discarded_section (isec
)))
14750 /* Discard unneeded references to discarded sections.
14751 Returns -1 on error, 1 if any section's size was changed, 0 if
14752 nothing changed. This function assumes that the relocations are in
14753 sorted order, which is true for all known assemblers. */
14756 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14758 struct elf_reloc_cookie cookie
;
14763 if (info
->traditional_format
14764 || !is_elf_hash_table (info
->hash
))
14767 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14772 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14775 || i
->reloc_count
== 0
14776 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14780 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14783 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14786 if (_bfd_discard_section_stabs (abfd
, i
,
14787 elf_section_data (i
)->sec_info
,
14788 bfd_elf_reloc_symbol_deleted_p
,
14792 fini_reloc_cookie_for_section (&cookie
, i
);
14797 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14798 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14802 int eh_changed
= 0;
14803 unsigned int eh_alignment
; /* Octets. */
14805 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14811 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14814 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14817 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14818 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14819 bfd_elf_reloc_symbol_deleted_p
,
14823 if (i
->size
!= i
->rawsize
)
14827 fini_reloc_cookie_for_section (&cookie
, i
);
14830 eh_alignment
= ((1 << o
->alignment_power
)
14831 * bfd_octets_per_byte (output_bfd
, o
));
14832 /* Skip over zero terminator, and prevent empty sections from
14833 adding alignment padding at the end. */
14834 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14836 i
->flags
|= SEC_EXCLUDE
;
14837 else if (i
->size
> 4)
14839 /* The last non-empty eh_frame section doesn't need padding. */
14842 /* Any prior sections must pad the last FDE out to the output
14843 section alignment. Otherwise we might have zero padding
14844 between sections, which would be seen as a terminator. */
14845 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14847 /* All but the last zero terminator should have been removed. */
14852 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14853 if (i
->size
!= size
)
14861 elf_link_hash_traverse (elf_hash_table (info
),
14862 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14865 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14867 const struct elf_backend_data
*bed
;
14870 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14872 s
= abfd
->sections
;
14873 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14876 bed
= get_elf_backend_data (abfd
);
14878 if (bed
->elf_backend_discard_info
!= NULL
)
14880 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14883 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14886 fini_reloc_cookie (&cookie
, abfd
);
14890 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14891 _bfd_elf_end_eh_frame_parsing (info
);
14893 if (info
->eh_frame_hdr_type
14894 && !bfd_link_relocatable (info
)
14895 && _bfd_elf_discard_section_eh_frame_hdr (info
))
14902 _bfd_elf_section_already_linked (bfd
*abfd
,
14904 struct bfd_link_info
*info
)
14907 const char *name
, *key
;
14908 struct bfd_section_already_linked
*l
;
14909 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14911 if (sec
->output_section
== bfd_abs_section_ptr
)
14914 flags
= sec
->flags
;
14916 /* Return if it isn't a linkonce section. A comdat group section
14917 also has SEC_LINK_ONCE set. */
14918 if ((flags
& SEC_LINK_ONCE
) == 0)
14921 /* Don't put group member sections on our list of already linked
14922 sections. They are handled as a group via their group section. */
14923 if (elf_sec_group (sec
) != NULL
)
14926 /* For a SHT_GROUP section, use the group signature as the key. */
14928 if ((flags
& SEC_GROUP
) != 0
14929 && elf_next_in_group (sec
) != NULL
14930 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14931 key
= elf_group_name (elf_next_in_group (sec
));
14934 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14935 if (startswith (name
, ".gnu.linkonce.")
14936 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14939 /* Must be a user linkonce section that doesn't follow gcc's
14940 naming convention. In this case we won't be matching
14941 single member groups. */
14945 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14947 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14949 /* We may have 2 different types of sections on the list: group
14950 sections with a signature of <key> (<key> is some string),
14951 and linkonce sections named .gnu.linkonce.<type>.<key>.
14952 Match like sections. LTO plugin sections are an exception.
14953 They are always named .gnu.linkonce.t.<key> and match either
14954 type of section. */
14955 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14956 && ((flags
& SEC_GROUP
) != 0
14957 || strcmp (name
, l
->sec
->name
) == 0))
14958 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
14959 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14961 /* The section has already been linked. See if we should
14962 issue a warning. */
14963 if (!_bfd_handle_already_linked (sec
, l
, info
))
14966 if (flags
& SEC_GROUP
)
14968 asection
*first
= elf_next_in_group (sec
);
14969 asection
*s
= first
;
14973 s
->output_section
= bfd_abs_section_ptr
;
14974 /* Record which group discards it. */
14975 s
->kept_section
= l
->sec
;
14976 s
= elf_next_in_group (s
);
14977 /* These lists are circular. */
14987 /* A single member comdat group section may be discarded by a
14988 linkonce section and vice versa. */
14989 if ((flags
& SEC_GROUP
) != 0)
14991 asection
*first
= elf_next_in_group (sec
);
14993 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14994 /* Check this single member group against linkonce sections. */
14995 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14996 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14997 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14999 first
->output_section
= bfd_abs_section_ptr
;
15000 first
->kept_section
= l
->sec
;
15001 sec
->output_section
= bfd_abs_section_ptr
;
15006 /* Check this linkonce section against single member groups. */
15007 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15008 if (l
->sec
->flags
& SEC_GROUP
)
15010 asection
*first
= elf_next_in_group (l
->sec
);
15013 && elf_next_in_group (first
) == first
15014 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
15016 sec
->output_section
= bfd_abs_section_ptr
;
15017 sec
->kept_section
= first
;
15022 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15023 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15024 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15025 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15026 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15027 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15028 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15029 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15030 The reverse order cannot happen as there is never a bfd with only the
15031 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15032 matter as here were are looking only for cross-bfd sections. */
15034 if ((flags
& SEC_GROUP
) == 0 && startswith (name
, ".gnu.linkonce.r."))
15035 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15036 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15037 && startswith (l
->sec
->name
, ".gnu.linkonce.t."))
15039 if (abfd
!= l
->sec
->owner
)
15040 sec
->output_section
= bfd_abs_section_ptr
;
15044 /* This is the first section with this name. Record it. */
15045 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
15046 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
15047 return sec
->output_section
== bfd_abs_section_ptr
;
15051 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
15053 return sym
->st_shndx
== SHN_COMMON
;
15057 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
15063 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
15065 return bfd_com_section_ptr
;
15069 _bfd_elf_default_got_elt_size (bfd
*abfd
,
15070 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
15071 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
15072 bfd
*ibfd ATTRIBUTE_UNUSED
,
15073 unsigned long symndx ATTRIBUTE_UNUSED
)
15075 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15076 return bed
->s
->arch_size
/ 8;
15079 /* Routines to support the creation of dynamic relocs. */
15081 /* Returns the name of the dynamic reloc section associated with SEC. */
15083 static const char *
15084 get_dynamic_reloc_section_name (bfd
* abfd
,
15089 const char *old_name
= bfd_section_name (sec
);
15090 const char *prefix
= is_rela
? ".rela" : ".rel";
15092 if (old_name
== NULL
)
15095 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
15096 sprintf (name
, "%s%s", prefix
, old_name
);
15101 /* Returns the dynamic reloc section associated with SEC.
15102 If necessary compute the name of the dynamic reloc section based
15103 on SEC's name (looked up in ABFD's string table) and the setting
15107 _bfd_elf_get_dynamic_reloc_section (bfd
*abfd
,
15111 asection
*reloc_sec
= elf_section_data (sec
)->sreloc
;
15113 if (reloc_sec
== NULL
)
15115 const char *name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15119 reloc_sec
= bfd_get_linker_section (abfd
, name
);
15121 if (reloc_sec
!= NULL
)
15122 elf_section_data (sec
)->sreloc
= reloc_sec
;
15129 /* Returns the dynamic reloc section associated with SEC. If the
15130 section does not exist it is created and attached to the DYNOBJ
15131 bfd and stored in the SRELOC field of SEC's elf_section_data
15134 ALIGNMENT is the alignment for the newly created section and
15135 IS_RELA defines whether the name should be .rela.<SEC's name>
15136 or .rel.<SEC's name>. The section name is looked up in the
15137 string table associated with ABFD. */
15140 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
15142 unsigned int alignment
,
15146 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
15148 if (reloc_sec
== NULL
)
15150 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15155 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
15157 if (reloc_sec
== NULL
)
15159 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
15160 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
15161 if ((sec
->flags
& SEC_ALLOC
) != 0)
15162 flags
|= SEC_ALLOC
| SEC_LOAD
;
15164 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
15165 if (reloc_sec
!= NULL
)
15167 /* _bfd_elf_get_sec_type_attr chooses a section type by
15168 name. Override as it may be wrong, eg. for a user
15169 section named "auto" we'll get ".relauto" which is
15170 seen to be a .rela section. */
15171 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
15172 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
15177 elf_section_data (sec
)->sreloc
= reloc_sec
;
15183 /* Copy the ELF symbol type and other attributes for a linker script
15184 assignment from HSRC to HDEST. Generally this should be treated as
15185 if we found a strong non-dynamic definition for HDEST (except that
15186 ld ignores multiple definition errors). */
15188 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
15189 struct bfd_link_hash_entry
*hdest
,
15190 struct bfd_link_hash_entry
*hsrc
)
15192 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
15193 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
15194 Elf_Internal_Sym isym
;
15196 ehdest
->type
= ehsrc
->type
;
15197 ehdest
->target_internal
= ehsrc
->target_internal
;
15199 isym
.st_other
= ehsrc
->other
;
15200 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, true, false);
15203 /* Append a RELA relocation REL to section S in BFD. */
15206 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15208 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15209 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
15210 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
15211 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
15214 /* Append a REL relocation REL to section S in BFD. */
15217 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15219 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15220 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
15221 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
15222 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
15225 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15227 struct bfd_link_hash_entry
*
15228 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
15229 const char *symbol
, asection
*sec
)
15231 struct elf_link_hash_entry
*h
;
15233 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
15234 false, false, true);
15235 /* NB: Common symbols will be turned into definition later. */
15237 && !h
->root
.ldscript_def
15238 && (h
->root
.type
== bfd_link_hash_undefined
15239 || h
->root
.type
== bfd_link_hash_undefweak
15240 || ((h
->ref_regular
|| h
->def_dynamic
)
15242 && h
->root
.type
!= bfd_link_hash_common
)))
15244 bool was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15245 h
->verinfo
.verdef
= NULL
;
15246 h
->root
.type
= bfd_link_hash_defined
;
15247 h
->root
.u
.def
.section
= sec
;
15248 h
->root
.u
.def
.value
= 0;
15249 h
->def_regular
= 1;
15250 h
->def_dynamic
= 0;
15252 h
->u2
.start_stop_section
= sec
;
15253 if (symbol
[0] == '.')
15255 /* .startof. and .sizeof. symbols are local. */
15256 const struct elf_backend_data
*bed
;
15257 bed
= get_elf_backend_data (info
->output_bfd
);
15258 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
15262 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15263 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15264 | info
->start_stop_visibility
);
15266 bfd_elf_link_record_dynamic_symbol (info
, h
);
15273 /* Find dynamic relocs for H that apply to read-only sections. */
15276 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15278 struct elf_dyn_relocs
*p
;
15280 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15282 asection
*s
= p
->sec
->output_section
;
15284 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15290 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15291 read-only sections. */
15294 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15298 if (h
->root
.type
== bfd_link_hash_indirect
)
15301 sec
= _bfd_elf_readonly_dynrelocs (h
);
15304 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15306 info
->flags
|= DF_TEXTREL
;
15307 /* xgettext:c-format */
15308 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15309 "in read-only section `%pA'\n"),
15310 sec
->owner
, h
->root
.root
.string
, sec
);
15312 if (bfd_link_textrel_check (info
))
15313 /* xgettext:c-format */
15314 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15315 "in read-only section `%pA'\n"),
15316 sec
->owner
, h
->root
.root
.string
, sec
);
15318 /* Not an error, just cut short the traversal. */
15324 /* Add dynamic tags. */
15327 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15328 bool need_dynamic_reloc
)
15330 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15332 if (htab
->dynamic_sections_created
)
15334 /* Add some entries to the .dynamic section. We fill in the
15335 values later, in finish_dynamic_sections, but we must add
15336 the entries now so that we get the correct size for the
15337 .dynamic section. The DT_DEBUG entry is filled in by the
15338 dynamic linker and used by the debugger. */
15339 #define add_dynamic_entry(TAG, VAL) \
15340 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15342 const struct elf_backend_data
*bed
15343 = get_elf_backend_data (output_bfd
);
15345 if (bfd_link_executable (info
))
15347 if (!add_dynamic_entry (DT_DEBUG
, 0))
15351 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15353 /* DT_PLTGOT is used by prelink even if there is no PLT
15355 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15359 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15361 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15362 || !add_dynamic_entry (DT_PLTREL
,
15363 (bed
->rela_plts_and_copies_p
15364 ? DT_RELA
: DT_REL
))
15365 || !add_dynamic_entry (DT_JMPREL
, 0))
15369 if (htab
->tlsdesc_plt
15370 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15371 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15374 if (need_dynamic_reloc
)
15376 if (bed
->rela_plts_and_copies_p
)
15378 if (!add_dynamic_entry (DT_RELA
, 0)
15379 || !add_dynamic_entry (DT_RELASZ
, 0)
15380 || !add_dynamic_entry (DT_RELAENT
,
15381 bed
->s
->sizeof_rela
))
15386 if (!add_dynamic_entry (DT_REL
, 0)
15387 || !add_dynamic_entry (DT_RELSZ
, 0)
15388 || !add_dynamic_entry (DT_RELENT
,
15389 bed
->s
->sizeof_rel
))
15393 /* If any dynamic relocs apply to a read-only section,
15394 then we need a DT_TEXTREL entry. */
15395 if ((info
->flags
& DF_TEXTREL
) == 0)
15396 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15399 if ((info
->flags
& DF_TEXTREL
) != 0)
15401 if (htab
->ifunc_resolvers
)
15402 info
->callbacks
->einfo
15403 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15404 "may result in a segfault at runtime; recompile with %s\n"),
15405 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15407 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15412 #undef add_dynamic_entry