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
;
1098 bind
= ELF_ST_BIND (sym
->st_info
);
1100 if (! bfd_is_und_section (sec
))
1101 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
1103 h
= ((struct elf_link_hash_entry
*)
1104 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
1109 bed
= get_elf_backend_data (abfd
);
1111 /* NEW_VERSION is the symbol version of the new symbol. */
1112 if (h
->versioned
!= unversioned
)
1114 /* Symbol version is unknown or versioned. */
1115 new_version
= strrchr (name
, ELF_VER_CHR
);
1118 if (h
->versioned
== unknown
)
1120 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1121 h
->versioned
= versioned_hidden
;
1123 h
->versioned
= versioned
;
1126 if (new_version
[0] == '\0')
1130 h
->versioned
= unversioned
;
1135 /* For merging, we only care about real symbols. But we need to make
1136 sure that indirect symbol dynamic flags are updated. */
1138 while (h
->root
.type
== bfd_link_hash_indirect
1139 || h
->root
.type
== bfd_link_hash_warning
)
1140 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1144 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1148 /* OLD_HIDDEN is true if the existing symbol is only visible
1149 to the symbol with the same symbol version. NEW_HIDDEN is
1150 true if the new symbol is only visible to the symbol with
1151 the same symbol version. */
1152 bool old_hidden
= h
->versioned
== versioned_hidden
;
1153 bool new_hidden
= hi
->versioned
== versioned_hidden
;
1154 if (!old_hidden
&& !new_hidden
)
1155 /* The new symbol matches the existing symbol if both
1160 /* OLD_VERSION is the symbol version of the existing
1164 if (h
->versioned
>= versioned
)
1165 old_version
= strrchr (h
->root
.root
.string
,
1170 /* The new symbol matches the existing symbol if they
1171 have the same symbol version. */
1172 *matched
= (old_version
== new_version
1173 || (old_version
!= NULL
1174 && new_version
!= NULL
1175 && strcmp (old_version
, new_version
) == 0));
1180 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1185 switch (h
->root
.type
)
1190 case bfd_link_hash_undefined
:
1191 case bfd_link_hash_undefweak
:
1192 oldbfd
= h
->root
.u
.undef
.abfd
;
1195 case bfd_link_hash_defined
:
1196 case bfd_link_hash_defweak
:
1197 oldbfd
= h
->root
.u
.def
.section
->owner
;
1198 oldsec
= h
->root
.u
.def
.section
;
1201 case bfd_link_hash_common
:
1202 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1203 oldsec
= h
->root
.u
.c
.p
->section
;
1205 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1208 if (poldbfd
&& *poldbfd
== NULL
)
1211 /* Differentiate strong and weak symbols. */
1212 newweak
= bind
== STB_WEAK
;
1213 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1214 || h
->root
.type
== bfd_link_hash_undefweak
);
1216 *pold_weak
= oldweak
;
1218 /* We have to check it for every instance since the first few may be
1219 references and not all compilers emit symbol type for undefined
1221 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1223 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1224 respectively, is from a dynamic object. */
1226 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1228 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1229 syms and defined syms in dynamic libraries respectively.
1230 ref_dynamic on the other hand can be set for a symbol defined in
1231 a dynamic library, and def_dynamic may not be set; When the
1232 definition in a dynamic lib is overridden by a definition in the
1233 executable use of the symbol in the dynamic lib becomes a
1234 reference to the executable symbol. */
1237 if (bfd_is_und_section (sec
))
1239 if (bind
!= STB_WEAK
)
1241 h
->ref_dynamic_nonweak
= 1;
1242 hi
->ref_dynamic_nonweak
= 1;
1247 /* Update the existing symbol only if they match. */
1250 hi
->dynamic_def
= 1;
1254 /* If we just created the symbol, mark it as being an ELF symbol.
1255 Other than that, there is nothing to do--there is no merge issue
1256 with a newly defined symbol--so we just return. */
1258 if (h
->root
.type
== bfd_link_hash_new
)
1264 /* In cases involving weak versioned symbols, we may wind up trying
1265 to merge a symbol with itself. Catch that here, to avoid the
1266 confusion that results if we try to override a symbol with
1267 itself. The additional tests catch cases like
1268 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1269 dynamic object, which we do want to handle here. */
1271 && (newweak
|| oldweak
)
1272 && ((abfd
->flags
& DYNAMIC
) == 0
1273 || !h
->def_regular
))
1278 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1279 else if (oldsec
!= NULL
)
1281 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1282 indices used by MIPS ELF. */
1283 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1287 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
))
1289 if (newdyn
!= olddyn
)
1291 /* Handle a case where plugin_notice won't be called and thus
1292 won't set the non_ir_ref flags on the first pass over
1294 h
->root
.non_ir_ref_dynamic
= true;
1295 hi
->root
.non_ir_ref_dynamic
= true;
1298 if ((oldbfd
->flags
& BFD_PLUGIN
) != 0
1299 && hi
->root
.type
== bfd_link_hash_indirect
)
1301 /* Change indirect symbol from IR to undefined. */
1302 hi
->root
.type
= bfd_link_hash_undefined
;
1303 hi
->root
.u
.undef
.abfd
= oldbfd
;
1307 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1308 respectively, appear to be a definition rather than reference. */
1310 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1312 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1313 && h
->root
.type
!= bfd_link_hash_undefweak
1314 && h
->root
.type
!= bfd_link_hash_common
);
1316 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1317 respectively, appear to be a function. */
1319 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1320 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1322 oldfunc
= (h
->type
!= STT_NOTYPE
1323 && bed
->is_function_type (h
->type
));
1325 if (!(newfunc
&& oldfunc
)
1326 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1327 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1328 && h
->type
!= STT_NOTYPE
1329 && (newdef
|| bfd_is_com_section (sec
))
1330 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1332 /* If creating a default indirect symbol ("foo" or "foo@") from
1333 a dynamic versioned definition ("foo@@") skip doing so if
1334 there is an existing regular definition with a different
1335 type. We don't want, for example, a "time" variable in the
1336 executable overriding a "time" function in a shared library. */
1344 /* When adding a symbol from a regular object file after we have
1345 created indirect symbols, undo the indirection and any
1352 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1353 h
->forced_local
= 0;
1357 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1359 h
->root
.type
= bfd_link_hash_undefined
;
1360 h
->root
.u
.undef
.abfd
= abfd
;
1364 h
->root
.type
= bfd_link_hash_new
;
1365 h
->root
.u
.undef
.abfd
= NULL
;
1371 /* Check TLS symbols. We don't check undefined symbols introduced
1372 by "ld -u" which have no type (and oldbfd NULL), and we don't
1373 check symbols from plugins because they also have no type. */
1375 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1376 && (abfd
->flags
& BFD_PLUGIN
) == 0
1377 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1378 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1382 asection
*ntsec
, *tsec
;
1384 if (h
->type
== STT_TLS
)
1405 /* xgettext:c-format */
1406 (_("%s: TLS definition in %pB section %pA "
1407 "mismatches non-TLS definition in %pB section %pA"),
1408 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1409 else if (!tdef
&& !ntdef
)
1411 /* xgettext:c-format */
1412 (_("%s: TLS reference in %pB "
1413 "mismatches non-TLS reference in %pB"),
1414 h
->root
.root
.string
, tbfd
, ntbfd
);
1417 /* xgettext:c-format */
1418 (_("%s: TLS definition in %pB section %pA "
1419 "mismatches non-TLS reference in %pB"),
1420 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1423 /* xgettext:c-format */
1424 (_("%s: TLS reference in %pB "
1425 "mismatches non-TLS definition in %pB section %pA"),
1426 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1428 bfd_set_error (bfd_error_bad_value
);
1432 /* If the old symbol has non-default visibility, we ignore the new
1433 definition from a dynamic object. */
1435 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1436 && !bfd_is_und_section (sec
))
1439 /* Make sure this symbol is dynamic. */
1441 hi
->ref_dynamic
= 1;
1442 /* A protected symbol has external availability. Make sure it is
1443 recorded as dynamic.
1445 FIXME: Should we check type and size for protected symbol? */
1446 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1447 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1452 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1455 /* If the new symbol with non-default visibility comes from a
1456 relocatable file and the old definition comes from a dynamic
1457 object, we remove the old definition. */
1458 if (hi
->root
.type
== bfd_link_hash_indirect
)
1460 /* Handle the case where the old dynamic definition is
1461 default versioned. We need to copy the symbol info from
1462 the symbol with default version to the normal one if it
1463 was referenced before. */
1466 hi
->root
.type
= h
->root
.type
;
1467 h
->root
.type
= bfd_link_hash_indirect
;
1468 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1470 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1471 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1473 /* If the new symbol is hidden or internal, completely undo
1474 any dynamic link state. */
1475 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1476 h
->forced_local
= 0;
1483 /* FIXME: Should we check type and size for protected symbol? */
1493 /* If the old symbol was undefined before, then it will still be
1494 on the undefs list. If the new symbol is undefined or
1495 common, we can't make it bfd_link_hash_new here, because new
1496 undefined or common symbols will be added to the undefs list
1497 by _bfd_generic_link_add_one_symbol. Symbols may not be
1498 added twice to the undefs list. Also, if the new symbol is
1499 undefweak then we don't want to lose the strong undef. */
1500 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1502 h
->root
.type
= bfd_link_hash_undefined
;
1503 h
->root
.u
.undef
.abfd
= abfd
;
1507 h
->root
.type
= bfd_link_hash_new
;
1508 h
->root
.u
.undef
.abfd
= NULL
;
1511 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1513 /* If the new symbol is hidden or internal, completely undo
1514 any dynamic link state. */
1515 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1516 h
->forced_local
= 0;
1522 /* FIXME: Should we check type and size for protected symbol? */
1528 /* If a new weak symbol definition comes from a regular file and the
1529 old symbol comes from a dynamic library, we treat the new one as
1530 strong. Similarly, an old weak symbol definition from a regular
1531 file is treated as strong when the new symbol comes from a dynamic
1532 library. Further, an old weak symbol from a dynamic library is
1533 treated as strong if the new symbol is from a dynamic library.
1534 This reflects the way glibc's ld.so works.
1536 Also allow a weak symbol to override a linker script symbol
1537 defined by an early pass over the script. This is done so the
1538 linker knows the symbol is defined in an object file, for the
1539 DEFINED script function.
1541 Do this before setting *type_change_ok or *size_change_ok so that
1542 we warn properly when dynamic library symbols are overridden. */
1544 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1546 if (olddef
&& newdyn
)
1549 /* Allow changes between different types of function symbol. */
1550 if (newfunc
&& oldfunc
)
1551 *type_change_ok
= true;
1553 /* It's OK to change the type if either the existing symbol or the
1554 new symbol is weak. A type change is also OK if the old symbol
1555 is undefined and the new symbol is defined. */
1560 && h
->root
.type
== bfd_link_hash_undefined
))
1561 *type_change_ok
= true;
1563 /* It's OK to change the size if either the existing symbol or the
1564 new symbol is weak, or if the old symbol is undefined. */
1567 || h
->root
.type
== bfd_link_hash_undefined
)
1568 *size_change_ok
= true;
1570 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1571 symbol, respectively, appears to be a common symbol in a dynamic
1572 object. If a symbol appears in an uninitialized section, and is
1573 not weak, and is not a function, then it may be a common symbol
1574 which was resolved when the dynamic object was created. We want
1575 to treat such symbols specially, because they raise special
1576 considerations when setting the symbol size: if the symbol
1577 appears as a common symbol in a regular object, and the size in
1578 the regular object is larger, we must make sure that we use the
1579 larger size. This problematic case can always be avoided in C,
1580 but it must be handled correctly when using Fortran shared
1583 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1584 likewise for OLDDYNCOMMON and OLDDEF.
1586 Note that this test is just a heuristic, and that it is quite
1587 possible to have an uninitialized symbol in a shared object which
1588 is really a definition, rather than a common symbol. This could
1589 lead to some minor confusion when the symbol really is a common
1590 symbol in some regular object. However, I think it will be
1596 && (sec
->flags
& SEC_ALLOC
) != 0
1597 && (sec
->flags
& SEC_LOAD
) == 0
1600 newdyncommon
= true;
1602 newdyncommon
= false;
1606 && h
->root
.type
== bfd_link_hash_defined
1608 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1609 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1612 olddyncommon
= true;
1614 olddyncommon
= false;
1616 /* We now know everything about the old and new symbols. We ask the
1617 backend to check if we can merge them. */
1618 if (bed
->merge_symbol
!= NULL
)
1620 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1625 /* There are multiple definitions of a normal symbol. Skip the
1626 default symbol as well as definition from an IR object. */
1627 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1628 && !default_sym
&& h
->def_regular
1630 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1631 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1633 /* Handle a multiple definition. */
1634 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1635 abfd
, sec
, *pvalue
);
1640 /* If both the old and the new symbols look like common symbols in a
1641 dynamic object, set the size of the symbol to the larger of the
1646 && sym
->st_size
!= h
->size
)
1648 /* Since we think we have two common symbols, issue a multiple
1649 common warning if desired. Note that we only warn if the
1650 size is different. If the size is the same, we simply let
1651 the old symbol override the new one as normally happens with
1652 symbols defined in dynamic objects. */
1654 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1655 bfd_link_hash_common
, sym
->st_size
);
1656 if (sym
->st_size
> h
->size
)
1657 h
->size
= sym
->st_size
;
1659 *size_change_ok
= true;
1662 /* If we are looking at a dynamic object, and we have found a
1663 definition, we need to see if the symbol was already defined by
1664 some other object. If so, we want to use the existing
1665 definition, and we do not want to report a multiple symbol
1666 definition error; we do this by clobbering *PSEC to be
1667 bfd_und_section_ptr.
1669 We treat a common symbol as a definition if the symbol in the
1670 shared library is a function, since common symbols always
1671 represent variables; this can cause confusion in principle, but
1672 any such confusion would seem to indicate an erroneous program or
1673 shared library. We also permit a common symbol in a regular
1674 object to override a weak symbol in a shared object. */
1679 || (h
->root
.type
== bfd_link_hash_common
1680 && (newweak
|| newfunc
))))
1684 newdyncommon
= false;
1686 *psec
= sec
= bfd_und_section_ptr
;
1687 *size_change_ok
= true;
1689 /* If we get here when the old symbol is a common symbol, then
1690 we are explicitly letting it override a weak symbol or
1691 function in a dynamic object, and we don't want to warn about
1692 a type change. If the old symbol is a defined symbol, a type
1693 change warning may still be appropriate. */
1695 if (h
->root
.type
== bfd_link_hash_common
)
1696 *type_change_ok
= true;
1699 /* Handle the special case of an old common symbol merging with a
1700 new symbol which looks like a common symbol in a shared object.
1701 We change *PSEC and *PVALUE to make the new symbol look like a
1702 common symbol, and let _bfd_generic_link_add_one_symbol do the
1706 && h
->root
.type
== bfd_link_hash_common
)
1710 newdyncommon
= false;
1711 *pvalue
= sym
->st_size
;
1712 *psec
= sec
= bed
->common_section (oldsec
);
1713 *size_change_ok
= true;
1716 /* Skip weak definitions of symbols that are already defined. */
1717 if (newdef
&& olddef
&& newweak
)
1719 /* Don't skip new non-IR weak syms. */
1720 if (!(oldbfd
!= NULL
1721 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1722 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1728 /* Merge st_other. If the symbol already has a dynamic index,
1729 but visibility says it should not be visible, turn it into a
1731 elf_merge_st_other (abfd
, h
, sym
->st_other
, sec
, newdef
, newdyn
);
1732 if (h
->dynindx
!= -1)
1733 switch (ELF_ST_VISIBILITY (h
->other
))
1737 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1742 /* If the old symbol is from a dynamic object, and the new symbol is
1743 a definition which is not from a dynamic object, then the new
1744 symbol overrides the old symbol. Symbols from regular files
1745 always take precedence over symbols from dynamic objects, even if
1746 they are defined after the dynamic object in the link.
1748 As above, we again permit a common symbol in a regular object to
1749 override a definition in a shared object if the shared object
1750 symbol is a function or is weak. */
1755 || (bfd_is_com_section (sec
)
1756 && (oldweak
|| oldfunc
)))
1761 /* Change the hash table entry to undefined, and let
1762 _bfd_generic_link_add_one_symbol do the right thing with the
1765 h
->root
.type
= bfd_link_hash_undefined
;
1766 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1767 *size_change_ok
= true;
1770 olddyncommon
= false;
1772 /* We again permit a type change when a common symbol may be
1773 overriding a function. */
1775 if (bfd_is_com_section (sec
))
1779 /* If a common symbol overrides a function, make sure
1780 that it isn't defined dynamically nor has type
1783 h
->type
= STT_NOTYPE
;
1785 *type_change_ok
= true;
1788 if (hi
->root
.type
== bfd_link_hash_indirect
)
1791 /* This union may have been set to be non-NULL when this symbol
1792 was seen in a dynamic object. We must force the union to be
1793 NULL, so that it is correct for a regular symbol. */
1794 h
->verinfo
.vertree
= NULL
;
1797 /* Handle the special case of a new common symbol merging with an
1798 old symbol that looks like it might be a common symbol defined in
1799 a shared object. Note that we have already handled the case in
1800 which a new common symbol should simply override the definition
1801 in the shared library. */
1804 && bfd_is_com_section (sec
)
1807 /* It would be best if we could set the hash table entry to a
1808 common symbol, but we don't know what to use for the section
1809 or the alignment. */
1810 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1811 bfd_link_hash_common
, sym
->st_size
);
1813 /* If the presumed common symbol in the dynamic object is
1814 larger, pretend that the new symbol has its size. */
1816 if (h
->size
> *pvalue
)
1819 /* We need to remember the alignment required by the symbol
1820 in the dynamic object. */
1821 BFD_ASSERT (pold_alignment
);
1822 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1825 olddyncommon
= false;
1827 h
->root
.type
= bfd_link_hash_undefined
;
1828 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1830 *size_change_ok
= true;
1831 *type_change_ok
= true;
1833 if (hi
->root
.type
== bfd_link_hash_indirect
)
1836 h
->verinfo
.vertree
= NULL
;
1841 /* Handle the case where we had a versioned symbol in a dynamic
1842 library and now find a definition in a normal object. In this
1843 case, we make the versioned symbol point to the normal one. */
1844 flip
->root
.type
= h
->root
.type
;
1845 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1846 h
->root
.type
= bfd_link_hash_indirect
;
1847 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1848 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1852 flip
->ref_dynamic
= 1;
1859 /* This function is called to create an indirect symbol from the
1860 default for the symbol with the default version if needed. The
1861 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1862 set DYNSYM if the new indirect symbol is dynamic. */
1865 _bfd_elf_add_default_symbol (bfd
*abfd
,
1866 struct bfd_link_info
*info
,
1867 struct elf_link_hash_entry
*h
,
1869 Elf_Internal_Sym
*sym
,
1875 bool type_change_ok
;
1876 bool size_change_ok
;
1879 struct elf_link_hash_entry
*hi
;
1880 struct bfd_link_hash_entry
*bh
;
1881 const struct elf_backend_data
*bed
;
1886 size_t len
, shortlen
;
1890 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1893 /* If this symbol has a version, and it is the default version, we
1894 create an indirect symbol from the default name to the fully
1895 decorated name. This will cause external references which do not
1896 specify a version to be bound to this version of the symbol. */
1897 p
= strchr (name
, ELF_VER_CHR
);
1898 if (h
->versioned
== unknown
)
1902 h
->versioned
= unversioned
;
1907 if (p
[1] != ELF_VER_CHR
)
1909 h
->versioned
= versioned_hidden
;
1913 h
->versioned
= versioned
;
1918 /* PR ld/19073: We may see an unversioned definition after the
1924 bed
= get_elf_backend_data (abfd
);
1925 collect
= bed
->collect
;
1926 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1928 shortlen
= p
- name
;
1929 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1930 if (shortname
== NULL
)
1932 memcpy (shortname
, name
, shortlen
);
1933 shortname
[shortlen
] = '\0';
1935 /* We are going to create a new symbol. Merge it with any existing
1936 symbol with this name. For the purposes of the merge, act as
1937 though we were defining the symbol we just defined, although we
1938 actually going to define an indirect symbol. */
1939 type_change_ok
= false;
1940 size_change_ok
= false;
1943 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1944 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1945 &type_change_ok
, &size_change_ok
, &matched
))
1951 if (hi
->def_regular
|| ELF_COMMON_DEF_P (hi
))
1953 /* If the undecorated symbol will have a version added by a
1954 script different to H, then don't indirect to/from the
1955 undecorated symbol. This isn't ideal because we may not yet
1956 have seen symbol versions, if given by a script on the
1957 command line rather than via --version-script. */
1958 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1963 = bfd_find_version_for_sym (info
->version_info
,
1964 hi
->root
.root
.string
, &hide
);
1965 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1967 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
1971 if (hi
->verinfo
.vertree
!= NULL
1972 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1978 /* Add the default symbol if not performing a relocatable link. */
1979 if (! bfd_link_relocatable (info
))
1982 if (bh
->type
== bfd_link_hash_defined
1983 && bh
->u
.def
.section
->owner
!= NULL
1984 && (bh
->u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
1986 /* Mark the previous definition from IR object as
1987 undefined so that the generic linker will override
1989 bh
->type
= bfd_link_hash_undefined
;
1990 bh
->u
.undef
.abfd
= bh
->u
.def
.section
->owner
;
1992 if (! (_bfd_generic_link_add_one_symbol
1993 (info
, abfd
, shortname
, BSF_INDIRECT
,
1994 bfd_ind_section_ptr
,
1995 0, name
, false, collect
, &bh
)))
1997 hi
= (struct elf_link_hash_entry
*) bh
;
2002 /* In this case the symbol named SHORTNAME is overriding the
2003 indirect symbol we want to add. We were planning on making
2004 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2005 is the name without a version. NAME is the fully versioned
2006 name, and it is the default version.
2008 Overriding means that we already saw a definition for the
2009 symbol SHORTNAME in a regular object, and it is overriding
2010 the symbol defined in the dynamic object.
2012 When this happens, we actually want to change NAME, the
2013 symbol we just added, to refer to SHORTNAME. This will cause
2014 references to NAME in the shared object to become references
2015 to SHORTNAME in the regular object. This is what we expect
2016 when we override a function in a shared object: that the
2017 references in the shared object will be mapped to the
2018 definition in the regular object. */
2020 while (hi
->root
.type
== bfd_link_hash_indirect
2021 || hi
->root
.type
== bfd_link_hash_warning
)
2022 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2024 h
->root
.type
= bfd_link_hash_indirect
;
2025 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
2029 hi
->ref_dynamic
= 1;
2033 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
2038 /* Now set HI to H, so that the following code will set the
2039 other fields correctly. */
2043 /* Check if HI is a warning symbol. */
2044 if (hi
->root
.type
== bfd_link_hash_warning
)
2045 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2047 /* If there is a duplicate definition somewhere, then HI may not
2048 point to an indirect symbol. We will have reported an error to
2049 the user in that case. */
2051 if (hi
->root
.type
== bfd_link_hash_indirect
)
2053 struct elf_link_hash_entry
*ht
;
2055 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2056 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2058 /* If we first saw a reference to SHORTNAME with non-default
2059 visibility, merge that visibility to the @@VER symbol. */
2060 elf_merge_st_other (abfd
, ht
, hi
->other
, sec
, true, dynamic
);
2062 /* A reference to the SHORTNAME symbol from a dynamic library
2063 will be satisfied by the versioned symbol at runtime. In
2064 effect, we have a reference to the versioned symbol. */
2065 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2066 hi
->dynamic_def
|= ht
->dynamic_def
;
2068 /* See if the new flags lead us to realize that the symbol must
2074 if (! bfd_link_executable (info
)
2081 if (hi
->ref_regular
)
2087 /* We also need to define an indirection from the nondefault version
2091 len
= strlen (name
);
2092 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2093 if (shortname
== NULL
)
2095 memcpy (shortname
, name
, shortlen
);
2096 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2098 /* Once again, merge with any existing symbol. */
2099 type_change_ok
= false;
2100 size_change_ok
= false;
2102 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2103 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2104 &type_change_ok
, &size_change_ok
, &matched
))
2110 && h
->root
.type
== bfd_link_hash_defweak
2111 && hi
->root
.type
== bfd_link_hash_defined
)
2113 /* We are handling a weak sym@@ver and attempting to define
2114 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2115 new weak sym@ver because there is already a strong sym@ver.
2116 However, sym@ver and sym@@ver are really the same symbol.
2117 The existing strong sym@ver ought to override sym@@ver. */
2118 h
->root
.type
= bfd_link_hash_defined
;
2119 h
->root
.u
.def
.section
= hi
->root
.u
.def
.section
;
2120 h
->root
.u
.def
.value
= hi
->root
.u
.def
.value
;
2121 hi
->root
.type
= bfd_link_hash_indirect
;
2122 hi
->root
.u
.i
.link
= &h
->root
;
2129 /* Here SHORTNAME is a versioned name, so we don't expect to see
2130 the type of override we do in the case above unless it is
2131 overridden by a versioned definition. */
2132 if (hi
->root
.type
!= bfd_link_hash_defined
2133 && hi
->root
.type
!= bfd_link_hash_defweak
)
2135 /* xgettext:c-format */
2136 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2143 if (! (_bfd_generic_link_add_one_symbol
2144 (info
, abfd
, shortname
, BSF_INDIRECT
,
2145 bfd_ind_section_ptr
, 0, name
, false, collect
, &bh
)))
2147 hi
= (struct elf_link_hash_entry
*) bh
;
2150 /* If there is a duplicate definition somewhere, then HI may not
2151 point to an indirect symbol. We will have reported an error
2152 to the user in that case. */
2153 if (hi
->root
.type
== bfd_link_hash_indirect
)
2155 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2156 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2157 hi
->dynamic_def
|= h
->dynamic_def
;
2159 /* If we first saw a reference to @VER symbol with
2160 non-default visibility, merge that visibility to the
2162 elf_merge_st_other (abfd
, h
, hi
->other
, sec
, true, dynamic
);
2164 /* See if the new flags lead us to realize that the symbol
2170 if (! bfd_link_executable (info
)
2176 if (hi
->ref_regular
)
2185 /* This routine is used to export all defined symbols into the dynamic
2186 symbol table. It is called via elf_link_hash_traverse. */
2189 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2191 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2193 /* Ignore indirect symbols. These are added by the versioning code. */
2194 if (h
->root
.type
== bfd_link_hash_indirect
)
2197 /* Ignore this if we won't export it. */
2198 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2201 if (h
->dynindx
== -1
2202 && (h
->def_regular
|| h
->ref_regular
)
2203 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2204 h
->root
.root
.string
))
2206 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2216 /* Return true if GLIBC_ABI_DT_RELR is added to the list of version
2217 dependencies successfully. GLIBC_ABI_DT_RELR will be put into the
2218 .gnu.version_r section. */
2221 elf_link_add_dt_relr_dependency (struct elf_find_verdep_info
*rinfo
)
2223 bfd
*glibc_bfd
= NULL
;
2224 Elf_Internal_Verneed
*t
;
2225 Elf_Internal_Vernaux
*a
;
2227 const char *relr
= "GLIBC_ABI_DT_RELR";
2229 /* See if we already know about GLIBC_PRIVATE_DT_RELR. */
2230 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2234 const char *soname
= bfd_elf_get_dt_soname (t
->vn_bfd
);
2235 /* Skip the shared library if it isn't libc.so. */
2236 if (!soname
|| !startswith (soname
, "libc.so."))
2239 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2241 /* Return if GLIBC_PRIVATE_DT_RELR dependency has been
2243 if (a
->vna_nodename
== relr
2244 || strcmp (a
->vna_nodename
, relr
) == 0)
2247 /* Check if libc.so provides GLIBC_2.XX version. */
2248 if (!glibc_bfd
&& startswith (a
->vna_nodename
, "GLIBC_2."))
2249 glibc_bfd
= t
->vn_bfd
;
2255 /* Skip if it isn't linked against glibc. */
2256 if (glibc_bfd
== NULL
)
2259 /* This is a new version. Add it to tree we are building. */
2263 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
,
2267 rinfo
->failed
= true;
2271 t
->vn_bfd
= glibc_bfd
;
2272 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2273 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2277 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2280 rinfo
->failed
= true;
2284 a
->vna_nodename
= relr
;
2286 a
->vna_nextptr
= t
->vn_auxptr
;
2287 a
->vna_other
= rinfo
->vers
+ 1;
2295 /* Look through the symbols which are defined in other shared
2296 libraries and referenced here. Update the list of version
2297 dependencies. This will be put into the .gnu.version_r section.
2298 This function is called via elf_link_hash_traverse. */
2301 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2304 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2305 Elf_Internal_Verneed
*t
;
2306 Elf_Internal_Vernaux
*a
;
2309 /* We only care about symbols defined in shared objects with version
2314 || h
->verinfo
.verdef
== NULL
2315 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2316 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2319 /* See if we already know about this version. */
2320 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2324 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2327 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2328 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2334 /* This is a new version. Add it to tree we are building. */
2339 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2342 rinfo
->failed
= true;
2346 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2347 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2348 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2352 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2355 rinfo
->failed
= true;
2359 /* Note that we are copying a string pointer here, and testing it
2360 above. If bfd_elf_string_from_elf_section is ever changed to
2361 discard the string data when low in memory, this will have to be
2363 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2365 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2366 a
->vna_nextptr
= t
->vn_auxptr
;
2368 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2371 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2378 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2379 hidden. Set *T_P to NULL if there is no match. */
2382 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2383 struct elf_link_hash_entry
*h
,
2384 const char *version_p
,
2385 struct bfd_elf_version_tree
**t_p
,
2388 struct bfd_elf_version_tree
*t
;
2390 /* Look for the version. If we find it, it is no longer weak. */
2391 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2393 if (strcmp (t
->name
, version_p
) == 0)
2397 struct bfd_elf_version_expr
*d
;
2399 len
= version_p
- h
->root
.root
.string
;
2400 alc
= (char *) bfd_malloc (len
);
2403 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2404 alc
[len
- 1] = '\0';
2405 if (alc
[len
- 2] == ELF_VER_CHR
)
2406 alc
[len
- 2] = '\0';
2408 h
->verinfo
.vertree
= t
;
2412 if (t
->globals
.list
!= NULL
)
2413 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2415 /* See if there is anything to force this symbol to
2417 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2419 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2422 && ! info
->export_dynamic
)
2436 /* Return TRUE if the symbol H is hidden by version script. */
2439 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2440 struct elf_link_hash_entry
*h
)
2444 const struct elf_backend_data
*bed
2445 = get_elf_backend_data (info
->output_bfd
);
2447 /* Version script only hides symbols defined in regular objects. */
2448 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2451 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2452 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2454 struct bfd_elf_version_tree
*t
;
2457 if (*p
== ELF_VER_CHR
)
2461 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2465 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2470 /* If we don't have a version for this symbol, see if we can find
2472 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2475 = bfd_find_version_for_sym (info
->version_info
,
2476 h
->root
.root
.string
, &hide
);
2477 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2479 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2487 /* Figure out appropriate versions for all the symbols. We may not
2488 have the version number script until we have read all of the input
2489 files, so until that point we don't know which symbols should be
2490 local. This function is called via elf_link_hash_traverse. */
2493 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2495 struct elf_info_failed
*sinfo
;
2496 struct bfd_link_info
*info
;
2497 const struct elf_backend_data
*bed
;
2498 struct elf_info_failed eif
;
2502 sinfo
= (struct elf_info_failed
*) data
;
2505 /* Fix the symbol flags. */
2508 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2511 sinfo
->failed
= true;
2515 bed
= get_elf_backend_data (info
->output_bfd
);
2517 /* We only need version numbers for symbols defined in regular
2519 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2521 /* Hide symbols defined in discarded input sections. */
2522 if ((h
->root
.type
== bfd_link_hash_defined
2523 || h
->root
.type
== bfd_link_hash_defweak
)
2524 && discarded_section (h
->root
.u
.def
.section
))
2525 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2530 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2531 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2533 struct bfd_elf_version_tree
*t
;
2536 if (*p
== ELF_VER_CHR
)
2539 /* If there is no version string, we can just return out. */
2543 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2545 sinfo
->failed
= true;
2550 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2552 /* If we are building an application, we need to create a
2553 version node for this version. */
2554 if (t
== NULL
&& bfd_link_executable (info
))
2556 struct bfd_elf_version_tree
**pp
;
2559 /* If we aren't going to export this symbol, we don't need
2560 to worry about it. */
2561 if (h
->dynindx
== -1)
2564 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2568 sinfo
->failed
= true;
2573 t
->name_indx
= (unsigned int) -1;
2577 /* Don't count anonymous version tag. */
2578 if (sinfo
->info
->version_info
!= NULL
2579 && sinfo
->info
->version_info
->vernum
== 0)
2581 for (pp
= &sinfo
->info
->version_info
;
2585 t
->vernum
= version_index
;
2589 h
->verinfo
.vertree
= t
;
2593 /* We could not find the version for a symbol when
2594 generating a shared archive. Return an error. */
2596 /* xgettext:c-format */
2597 (_("%pB: version node not found for symbol %s"),
2598 info
->output_bfd
, h
->root
.root
.string
);
2599 bfd_set_error (bfd_error_bad_value
);
2600 sinfo
->failed
= true;
2605 /* If we don't have a version for this symbol, see if we can find
2608 && h
->verinfo
.vertree
== NULL
2609 && sinfo
->info
->version_info
!= NULL
)
2612 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2613 h
->root
.root
.string
, &hide
);
2614 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2615 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2621 /* Read and swap the relocs from the section indicated by SHDR. This
2622 may be either a REL or a RELA section. The relocations are
2623 translated into RELA relocations and stored in INTERNAL_RELOCS,
2624 which should have already been allocated to contain enough space.
2625 The EXTERNAL_RELOCS are a buffer where the external form of the
2626 relocations should be stored.
2628 Returns FALSE if something goes wrong. */
2631 elf_link_read_relocs_from_section (bfd
*abfd
,
2633 Elf_Internal_Shdr
*shdr
,
2634 void *external_relocs
,
2635 Elf_Internal_Rela
*internal_relocs
)
2637 const struct elf_backend_data
*bed
;
2638 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2639 const bfd_byte
*erela
;
2640 const bfd_byte
*erelaend
;
2641 Elf_Internal_Rela
*irela
;
2642 Elf_Internal_Shdr
*symtab_hdr
;
2645 /* Position ourselves at the start of the section. */
2646 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2649 /* Read the relocations. */
2650 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2653 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2654 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2656 bed
= get_elf_backend_data (abfd
);
2658 /* Convert the external relocations to the internal format. */
2659 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2660 swap_in
= bed
->s
->swap_reloc_in
;
2661 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2662 swap_in
= bed
->s
->swap_reloca_in
;
2665 bfd_set_error (bfd_error_wrong_format
);
2669 erela
= (const bfd_byte
*) external_relocs
;
2670 /* Setting erelaend like this and comparing with <= handles case of
2671 a fuzzed object with sh_size not a multiple of sh_entsize. */
2672 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2673 irela
= internal_relocs
;
2674 while (erela
<= erelaend
)
2678 (*swap_in
) (abfd
, erela
, irela
);
2679 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2680 if (bed
->s
->arch_size
== 64)
2684 if ((size_t) r_symndx
>= nsyms
)
2687 /* xgettext:c-format */
2688 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2689 " for offset %#" PRIx64
" in section `%pA'"),
2690 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2691 (uint64_t) irela
->r_offset
, sec
);
2692 bfd_set_error (bfd_error_bad_value
);
2696 else if (r_symndx
!= STN_UNDEF
)
2699 /* xgettext:c-format */
2700 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2701 " for offset %#" PRIx64
" in section `%pA'"
2702 " when the object file has no symbol table"),
2703 abfd
, (uint64_t) r_symndx
,
2704 (uint64_t) irela
->r_offset
, sec
);
2705 bfd_set_error (bfd_error_bad_value
);
2708 irela
+= bed
->s
->int_rels_per_ext_rel
;
2709 erela
+= shdr
->sh_entsize
;
2715 /* Read and swap the relocs for a section O. They may have been
2716 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2717 not NULL, they are used as buffers to read into. They are known to
2718 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2719 the return value is allocated using either malloc or bfd_alloc,
2720 according to the KEEP_MEMORY argument. If O has two relocation
2721 sections (both REL and RELA relocations), then the REL_HDR
2722 relocations will appear first in INTERNAL_RELOCS, followed by the
2723 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2724 update cache_size. */
2727 _bfd_elf_link_info_read_relocs (bfd
*abfd
,
2728 struct bfd_link_info
*info
,
2730 void *external_relocs
,
2731 Elf_Internal_Rela
*internal_relocs
,
2734 void *alloc1
= NULL
;
2735 Elf_Internal_Rela
*alloc2
= NULL
;
2736 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2737 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2738 Elf_Internal_Rela
*internal_rela_relocs
;
2740 if (esdo
->relocs
!= NULL
)
2741 return esdo
->relocs
;
2743 if (o
->reloc_count
== 0)
2746 if (internal_relocs
== NULL
)
2750 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2753 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2755 info
->cache_size
+= size
;
2758 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2759 if (internal_relocs
== NULL
)
2763 if (external_relocs
== NULL
)
2765 bfd_size_type size
= 0;
2768 size
+= esdo
->rel
.hdr
->sh_size
;
2770 size
+= esdo
->rela
.hdr
->sh_size
;
2772 alloc1
= bfd_malloc (size
);
2775 external_relocs
= alloc1
;
2778 internal_rela_relocs
= internal_relocs
;
2781 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2785 external_relocs
= (((bfd_byte
*) external_relocs
)
2786 + esdo
->rel
.hdr
->sh_size
);
2787 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2788 * bed
->s
->int_rels_per_ext_rel
);
2792 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2794 internal_rela_relocs
)))
2797 /* Cache the results for next time, if we can. */
2799 esdo
->relocs
= internal_relocs
;
2803 /* Don't free alloc2, since if it was allocated we are passing it
2804 back (under the name of internal_relocs). */
2806 return internal_relocs
;
2813 bfd_release (abfd
, alloc2
);
2820 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2821 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2822 struct bfd_link_info. */
2825 _bfd_elf_link_read_relocs (bfd
*abfd
,
2827 void *external_relocs
,
2828 Elf_Internal_Rela
*internal_relocs
,
2831 return _bfd_elf_link_info_read_relocs (abfd
, NULL
, o
, external_relocs
,
2832 internal_relocs
, keep_memory
);
2836 /* Compute the size of, and allocate space for, REL_HDR which is the
2837 section header for a section containing relocations for O. */
2840 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2841 struct bfd_elf_section_reloc_data
*reldata
)
2843 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2845 /* That allows us to calculate the size of the section. */
2846 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2848 /* The contents field must last into write_object_contents, so we
2849 allocate it with bfd_alloc rather than malloc. Also since we
2850 cannot be sure that the contents will actually be filled in,
2851 we zero the allocated space. */
2852 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2853 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2856 if (reldata
->hashes
== NULL
&& reldata
->count
)
2858 struct elf_link_hash_entry
**p
;
2860 p
= ((struct elf_link_hash_entry
**)
2861 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2865 reldata
->hashes
= p
;
2871 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2872 originated from the section given by INPUT_REL_HDR) to the
2876 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2877 asection
*input_section
,
2878 Elf_Internal_Shdr
*input_rel_hdr
,
2879 Elf_Internal_Rela
*internal_relocs
,
2880 struct elf_link_hash_entry
**rel_hash
2883 Elf_Internal_Rela
*irela
;
2884 Elf_Internal_Rela
*irelaend
;
2886 struct bfd_elf_section_reloc_data
*output_reldata
;
2887 asection
*output_section
;
2888 const struct elf_backend_data
*bed
;
2889 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2890 struct bfd_elf_section_data
*esdo
;
2892 output_section
= input_section
->output_section
;
2894 bed
= get_elf_backend_data (output_bfd
);
2895 esdo
= elf_section_data (output_section
);
2896 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2898 output_reldata
= &esdo
->rel
;
2899 swap_out
= bed
->s
->swap_reloc_out
;
2901 else if (esdo
->rela
.hdr
2902 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2904 output_reldata
= &esdo
->rela
;
2905 swap_out
= bed
->s
->swap_reloca_out
;
2910 /* xgettext:c-format */
2911 (_("%pB: relocation size mismatch in %pB section %pA"),
2912 output_bfd
, input_section
->owner
, input_section
);
2913 bfd_set_error (bfd_error_wrong_format
);
2917 erel
= output_reldata
->hdr
->contents
;
2918 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2919 irela
= internal_relocs
;
2920 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2921 * bed
->s
->int_rels_per_ext_rel
);
2922 while (irela
< irelaend
)
2924 (*swap_out
) (output_bfd
, irela
, erel
);
2925 irela
+= bed
->s
->int_rels_per_ext_rel
;
2926 erel
+= input_rel_hdr
->sh_entsize
;
2929 /* Bump the counter, so that we know where to add the next set of
2931 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2936 /* Make weak undefined symbols in PIE dynamic. */
2939 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2940 struct elf_link_hash_entry
*h
)
2942 if (bfd_link_pie (info
)
2944 && h
->root
.type
== bfd_link_hash_undefweak
)
2945 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2950 /* Fix up the flags for a symbol. This handles various cases which
2951 can only be fixed after all the input files are seen. This is
2952 currently called by both adjust_dynamic_symbol and
2953 assign_sym_version, which is unnecessary but perhaps more robust in
2954 the face of future changes. */
2957 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2958 struct elf_info_failed
*eif
)
2960 const struct elf_backend_data
*bed
;
2962 /* If this symbol was mentioned in a non-ELF file, try to set
2963 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2964 permit a non-ELF file to correctly refer to a symbol defined in
2965 an ELF dynamic object. */
2968 while (h
->root
.type
== bfd_link_hash_indirect
)
2969 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2971 if (h
->root
.type
!= bfd_link_hash_defined
2972 && h
->root
.type
!= bfd_link_hash_defweak
)
2975 h
->ref_regular_nonweak
= 1;
2979 if (h
->root
.u
.def
.section
->owner
!= NULL
2980 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2981 == bfd_target_elf_flavour
))
2984 h
->ref_regular_nonweak
= 1;
2990 if (h
->dynindx
== -1
2994 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3003 /* Unfortunately, NON_ELF is only correct if the symbol
3004 was first seen in a non-ELF file. Fortunately, if the symbol
3005 was first seen in an ELF file, we're probably OK unless the
3006 symbol was defined in a non-ELF file. Catch that case here.
3007 FIXME: We're still in trouble if the symbol was first seen in
3008 a dynamic object, and then later in a non-ELF regular object. */
3009 if ((h
->root
.type
== bfd_link_hash_defined
3010 || h
->root
.type
== bfd_link_hash_defweak
)
3012 && (h
->root
.u
.def
.section
->owner
!= NULL
3013 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3014 != bfd_target_elf_flavour
)
3015 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3016 && !h
->def_dynamic
)))
3020 /* Backend specific symbol fixup. */
3021 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3022 if (bed
->elf_backend_fixup_symbol
3023 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
3026 /* If this is a final link, and the symbol was defined as a common
3027 symbol in a regular object file, and there was no definition in
3028 any dynamic object, then the linker will have allocated space for
3029 the symbol in a common section but the DEF_REGULAR
3030 flag will not have been set. */
3031 if (h
->root
.type
== bfd_link_hash_defined
3035 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
3038 /* Symbols defined in discarded sections shouldn't be dynamic. */
3039 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
3040 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3042 /* If a weak undefined symbol has non-default visibility, we also
3043 hide it from the dynamic linker. */
3044 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
3045 && h
->root
.type
== bfd_link_hash_undefweak
)
3046 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3048 /* A hidden versioned symbol in executable should be forced local if
3049 it is is locally defined, not referenced by shared library and not
3051 else if (bfd_link_executable (eif
->info
)
3052 && h
->versioned
== versioned_hidden
3053 && !eif
->info
->export_dynamic
3057 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3059 /* If -Bsymbolic was used (which means to bind references to global
3060 symbols to the definition within the shared object), and this
3061 symbol was defined in a regular object, then it actually doesn't
3062 need a PLT entry. Likewise, if the symbol has non-default
3063 visibility. If the symbol has hidden or internal visibility, we
3064 will force it local. */
3065 else if (h
->needs_plt
3066 && bfd_link_pic (eif
->info
)
3067 && is_elf_hash_table (eif
->info
->hash
)
3068 && (SYMBOLIC_BIND (eif
->info
, h
)
3069 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
3074 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3075 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
3076 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3079 /* If this is a weak defined symbol in a dynamic object, and we know
3080 the real definition in the dynamic object, copy interesting flags
3081 over to the real definition. */
3082 if (h
->is_weakalias
)
3084 struct elf_link_hash_entry
*def
= weakdef (h
);
3086 /* If the real definition is defined by a regular object file,
3087 don't do anything special. See the longer description in
3088 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3089 bfd_link_hash_defined as it was when put on the alias list
3090 then it must have originally been a versioned symbol (for
3091 which a non-versioned indirect symbol is created) and later
3092 a definition for the non-versioned symbol is found. In that
3093 case the indirection is flipped with the versioned symbol
3094 becoming an indirect pointing at the non-versioned symbol.
3095 Thus, not an alias any more. */
3096 if (def
->def_regular
3097 || def
->root
.type
!= bfd_link_hash_defined
)
3100 while ((h
= h
->u
.alias
) != def
)
3101 h
->is_weakalias
= 0;
3105 while (h
->root
.type
== bfd_link_hash_indirect
)
3106 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3107 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3108 || h
->root
.type
== bfd_link_hash_defweak
);
3109 BFD_ASSERT (def
->def_dynamic
);
3110 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
3117 /* Make the backend pick a good value for a dynamic symbol. This is
3118 called via elf_link_hash_traverse, and also calls itself
3122 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
3124 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3125 struct elf_link_hash_table
*htab
;
3126 const struct elf_backend_data
*bed
;
3128 if (! is_elf_hash_table (eif
->info
->hash
))
3131 /* Ignore indirect symbols. These are added by the versioning code. */
3132 if (h
->root
.type
== bfd_link_hash_indirect
)
3135 /* Fix the symbol flags. */
3136 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
3139 htab
= elf_hash_table (eif
->info
);
3140 bed
= get_elf_backend_data (htab
->dynobj
);
3142 if (h
->root
.type
== bfd_link_hash_undefweak
)
3144 if (eif
->info
->dynamic_undefined_weak
== 0)
3145 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3146 else if (eif
->info
->dynamic_undefined_weak
> 0
3148 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3149 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
3150 h
->root
.root
.string
))
3152 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3160 /* If this symbol does not require a PLT entry, and it is not
3161 defined by a dynamic object, or is not referenced by a regular
3162 object, ignore it. We do have to handle a weak defined symbol,
3163 even if no regular object refers to it, if we decided to add it
3164 to the dynamic symbol table. FIXME: Do we normally need to worry
3165 about symbols which are defined by one dynamic object and
3166 referenced by another one? */
3168 && h
->type
!= STT_GNU_IFUNC
3172 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3174 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3178 /* If we've already adjusted this symbol, don't do it again. This
3179 can happen via a recursive call. */
3180 if (h
->dynamic_adjusted
)
3183 /* Don't look at this symbol again. Note that we must set this
3184 after checking the above conditions, because we may look at a
3185 symbol once, decide not to do anything, and then get called
3186 recursively later after REF_REGULAR is set below. */
3187 h
->dynamic_adjusted
= 1;
3189 /* If this is a weak definition, and we know a real definition, and
3190 the real symbol is not itself defined by a regular object file,
3191 then get a good value for the real definition. We handle the
3192 real symbol first, for the convenience of the backend routine.
3194 Note that there is a confusing case here. If the real definition
3195 is defined by a regular object file, we don't get the real symbol
3196 from the dynamic object, but we do get the weak symbol. If the
3197 processor backend uses a COPY reloc, then if some routine in the
3198 dynamic object changes the real symbol, we will not see that
3199 change in the corresponding weak symbol. This is the way other
3200 ELF linkers work as well, and seems to be a result of the shared
3203 I will clarify this issue. Most SVR4 shared libraries define the
3204 variable _timezone and define timezone as a weak synonym. The
3205 tzset call changes _timezone. If you write
3206 extern int timezone;
3208 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3209 you might expect that, since timezone is a synonym for _timezone,
3210 the same number will print both times. However, if the processor
3211 backend uses a COPY reloc, then actually timezone will be copied
3212 into your process image, and, since you define _timezone
3213 yourself, _timezone will not. Thus timezone and _timezone will
3214 wind up at different memory locations. The tzset call will set
3215 _timezone, leaving timezone unchanged. */
3217 if (h
->is_weakalias
)
3219 struct elf_link_hash_entry
*def
= weakdef (h
);
3221 /* If we get to this point, there is an implicit reference to
3222 the alias by a regular object file via the weak symbol H. */
3223 def
->ref_regular
= 1;
3225 /* Ensure that the backend adjust_dynamic_symbol function sees
3226 the strong alias before H by recursively calling ourselves. */
3227 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3231 /* If a symbol has no type and no size and does not require a PLT
3232 entry, then we are probably about to do the wrong thing here: we
3233 are probably going to create a COPY reloc for an empty object.
3234 This case can arise when a shared object is built with assembly
3235 code, and the assembly code fails to set the symbol type. */
3237 && h
->type
== STT_NOTYPE
3240 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3241 h
->root
.root
.string
);
3243 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3252 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3256 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3257 struct elf_link_hash_entry
*h
,
3260 unsigned int power_of_two
;
3262 asection
*sec
= h
->root
.u
.def
.section
;
3264 /* The section alignment of the definition is the maximum alignment
3265 requirement of symbols defined in the section. Since we don't
3266 know the symbol alignment requirement, we start with the
3267 maximum alignment and check low bits of the symbol address
3268 for the minimum alignment. */
3269 power_of_two
= bfd_section_alignment (sec
);
3270 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3271 while ((h
->root
.u
.def
.value
& mask
) != 0)
3277 if (power_of_two
> bfd_section_alignment (dynbss
))
3279 /* Adjust the section alignment if needed. */
3280 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3284 /* We make sure that the symbol will be aligned properly. */
3285 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3287 /* Define the symbol as being at this point in DYNBSS. */
3288 h
->root
.u
.def
.section
= dynbss
;
3289 h
->root
.u
.def
.value
= dynbss
->size
;
3291 /* Increment the size of DYNBSS to make room for the symbol. */
3292 dynbss
->size
+= h
->size
;
3294 /* No error if extern_protected_data is true. */
3295 if (h
->protected_def
3296 && (!info
->extern_protected_data
3297 || (info
->extern_protected_data
< 0
3298 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3299 info
->callbacks
->einfo
3300 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3301 h
->root
.root
.string
);
3306 /* Adjust all external symbols pointing into SEC_MERGE sections
3307 to reflect the object merging within the sections. */
3310 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3314 if ((h
->root
.type
== bfd_link_hash_defined
3315 || h
->root
.type
== bfd_link_hash_defweak
)
3316 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3317 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3319 bfd
*output_bfd
= (bfd
*) data
;
3321 h
->root
.u
.def
.value
=
3322 _bfd_merged_section_offset (output_bfd
,
3323 &h
->root
.u
.def
.section
,
3324 elf_section_data (sec
)->sec_info
,
3325 h
->root
.u
.def
.value
);
3331 /* Returns false if the symbol referred to by H should be considered
3332 to resolve local to the current module, and true if it should be
3333 considered to bind dynamically. */
3336 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3337 struct bfd_link_info
*info
,
3338 bool not_local_protected
)
3340 bool binding_stays_local_p
;
3341 const struct elf_backend_data
*bed
;
3342 struct elf_link_hash_table
*hash_table
;
3347 while (h
->root
.type
== bfd_link_hash_indirect
3348 || h
->root
.type
== bfd_link_hash_warning
)
3349 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3351 /* If it was forced local, then clearly it's not dynamic. */
3352 if (h
->dynindx
== -1)
3354 if (h
->forced_local
)
3357 /* Identify the cases where name binding rules say that a
3358 visible symbol resolves locally. */
3359 binding_stays_local_p
= (bfd_link_executable (info
)
3360 || SYMBOLIC_BIND (info
, h
));
3362 switch (ELF_ST_VISIBILITY (h
->other
))
3369 hash_table
= elf_hash_table (info
);
3370 if (!is_elf_hash_table (&hash_table
->root
))
3373 bed
= get_elf_backend_data (hash_table
->dynobj
);
3375 /* Proper resolution for function pointer equality may require
3376 that these symbols perhaps be resolved dynamically, even though
3377 we should be resolving them to the current module. */
3378 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3379 binding_stays_local_p
= true;
3386 /* If it isn't defined locally, then clearly it's dynamic. */
3387 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3390 /* Otherwise, the symbol is dynamic if binding rules don't tell
3391 us that it remains local. */
3392 return !binding_stays_local_p
;
3395 /* Return true if the symbol referred to by H should be considered
3396 to resolve local to the current module, and false otherwise. Differs
3397 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3398 undefined symbols. The two functions are virtually identical except
3399 for the place where dynindx == -1 is tested. If that test is true,
3400 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3401 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3403 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3404 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3405 treatment of undefined weak symbols. For those that do not make
3406 undefined weak symbols dynamic, both functions may return false. */
3409 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3410 struct bfd_link_info
*info
,
3411 bool local_protected
)
3413 const struct elf_backend_data
*bed
;
3414 struct elf_link_hash_table
*hash_table
;
3416 /* If it's a local sym, of course we resolve locally. */
3420 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3421 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3422 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3425 /* Forced local symbols resolve locally. */
3426 if (h
->forced_local
)
3429 /* Common symbols that become definitions don't get the DEF_REGULAR
3430 flag set, so test it first, and don't bail out. */
3431 if (ELF_COMMON_DEF_P (h
))
3433 /* If we don't have a definition in a regular file, then we can't
3434 resolve locally. The sym is either undefined or dynamic. */
3435 else if (!h
->def_regular
)
3438 /* Non-dynamic symbols resolve locally. */
3439 if (h
->dynindx
== -1)
3442 /* At this point, we know the symbol is defined and dynamic. In an
3443 executable it must resolve locally, likewise when building symbolic
3444 shared libraries. */
3445 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3448 /* Now deal with defined dynamic symbols in shared libraries. Ones
3449 with default visibility might not resolve locally. */
3450 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3453 hash_table
= elf_hash_table (info
);
3454 if (!is_elf_hash_table (&hash_table
->root
))
3457 /* STV_PROTECTED symbols with indirect external access are local. */
3458 if (info
->indirect_extern_access
> 0)
3461 bed
= get_elf_backend_data (hash_table
->dynobj
);
3463 /* If extern_protected_data is false, STV_PROTECTED non-function
3464 symbols are local. */
3465 if ((!info
->extern_protected_data
3466 || (info
->extern_protected_data
< 0
3467 && !bed
->extern_protected_data
))
3468 && !bed
->is_function_type (h
->type
))
3471 /* Function pointer equality tests may require that STV_PROTECTED
3472 symbols be treated as dynamic symbols. If the address of a
3473 function not defined in an executable is set to that function's
3474 plt entry in the executable, then the address of the function in
3475 a shared library must also be the plt entry in the executable. */
3476 return local_protected
;
3479 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3480 aligned. Returns the first TLS output section. */
3482 struct bfd_section
*
3483 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3485 struct bfd_section
*sec
, *tls
;
3486 unsigned int align
= 0;
3488 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3489 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3493 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3494 if (sec
->alignment_power
> align
)
3495 align
= sec
->alignment_power
;
3497 elf_hash_table (info
)->tls_sec
= tls
;
3499 /* Ensure the alignment of the first section (usually .tdata) is the largest
3500 alignment, so that the tls segment starts aligned. */
3502 tls
->alignment_power
= align
;
3507 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3509 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3510 Elf_Internal_Sym
*sym
)
3512 const struct elf_backend_data
*bed
;
3514 /* Local symbols do not count, but target specific ones might. */
3515 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3516 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3519 bed
= get_elf_backend_data (abfd
);
3520 /* Function symbols do not count. */
3521 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3524 /* If the section is undefined, then so is the symbol. */
3525 if (sym
->st_shndx
== SHN_UNDEF
)
3528 /* If the symbol is defined in the common section, then
3529 it is a common definition and so does not count. */
3530 if (bed
->common_definition (sym
))
3533 /* If the symbol is in a target specific section then we
3534 must rely upon the backend to tell us what it is. */
3535 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3536 /* FIXME - this function is not coded yet:
3538 return _bfd_is_global_symbol_definition (abfd, sym);
3540 Instead for now assume that the definition is not global,
3541 Even if this is wrong, at least the linker will behave
3542 in the same way that it used to do. */
3548 /* Search the symbol table of the archive element of the archive ABFD
3549 whose archive map contains a mention of SYMDEF, and determine if
3550 the symbol is defined in this element. */
3552 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3554 Elf_Internal_Shdr
* hdr
;
3558 Elf_Internal_Sym
*isymbuf
;
3559 Elf_Internal_Sym
*isym
;
3560 Elf_Internal_Sym
*isymend
;
3563 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
, NULL
);
3567 if (! bfd_check_format (abfd
, bfd_object
))
3570 /* Select the appropriate symbol table. If we don't know if the
3571 object file is an IR object, give linker LTO plugin a chance to
3572 get the correct symbol table. */
3573 if (abfd
->plugin_format
== bfd_plugin_yes
3574 #if BFD_SUPPORTS_PLUGINS
3575 || (abfd
->plugin_format
== bfd_plugin_unknown
3576 && bfd_link_plugin_object_p (abfd
))
3580 /* Use the IR symbol table if the object has been claimed by
3582 abfd
= abfd
->plugin_dummy_bfd
;
3583 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3585 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3586 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3588 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3590 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3592 /* The sh_info field of the symtab header tells us where the
3593 external symbols start. We don't care about the local symbols. */
3594 if (elf_bad_symtab (abfd
))
3596 extsymcount
= symcount
;
3601 extsymcount
= symcount
- hdr
->sh_info
;
3602 extsymoff
= hdr
->sh_info
;
3605 if (extsymcount
== 0)
3608 /* Read in the symbol table. */
3609 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3611 if (isymbuf
== NULL
)
3614 /* Scan the symbol table looking for SYMDEF. */
3616 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3620 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3625 if (strcmp (name
, symdef
->name
) == 0)
3627 result
= is_global_data_symbol_definition (abfd
, isym
);
3637 /* Add an entry to the .dynamic table. */
3640 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3644 struct elf_link_hash_table
*hash_table
;
3645 const struct elf_backend_data
*bed
;
3647 bfd_size_type newsize
;
3648 bfd_byte
*newcontents
;
3649 Elf_Internal_Dyn dyn
;
3651 hash_table
= elf_hash_table (info
);
3652 if (! is_elf_hash_table (&hash_table
->root
))
3655 if (tag
== DT_RELA
|| tag
== DT_REL
)
3656 hash_table
->dynamic_relocs
= true;
3658 bed
= get_elf_backend_data (hash_table
->dynobj
);
3659 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3660 BFD_ASSERT (s
!= NULL
);
3662 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3663 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3664 if (newcontents
== NULL
)
3668 dyn
.d_un
.d_val
= val
;
3669 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3672 s
->contents
= newcontents
;
3677 /* Strip zero-sized dynamic sections. */
3680 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3682 struct elf_link_hash_table
*hash_table
;
3683 const struct elf_backend_data
*bed
;
3684 asection
*s
, *sdynamic
, **pp
;
3685 asection
*rela_dyn
, *rel_dyn
;
3686 Elf_Internal_Dyn dyn
;
3687 bfd_byte
*extdyn
, *next
;
3688 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3689 bool strip_zero_sized
;
3690 bool strip_zero_sized_plt
;
3692 if (bfd_link_relocatable (info
))
3695 hash_table
= elf_hash_table (info
);
3696 if (!is_elf_hash_table (&hash_table
->root
))
3699 if (!hash_table
->dynobj
)
3702 sdynamic
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3706 bed
= get_elf_backend_data (hash_table
->dynobj
);
3707 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3709 strip_zero_sized
= false;
3710 strip_zero_sized_plt
= false;
3712 /* Strip zero-sized dynamic sections. */
3713 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3714 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3715 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3719 || s
== hash_table
->srelplt
->output_section
3720 || s
== hash_table
->splt
->output_section
))
3723 info
->output_bfd
->section_count
--;
3724 strip_zero_sized
= true;
3729 else if (s
== hash_table
->splt
->output_section
)
3731 s
= hash_table
->splt
;
3732 strip_zero_sized_plt
= true;
3735 s
= hash_table
->srelplt
;
3736 s
->flags
|= SEC_EXCLUDE
;
3737 s
->output_section
= bfd_abs_section_ptr
;
3742 if (strip_zero_sized_plt
&& sdynamic
->size
!= 0)
3743 for (extdyn
= sdynamic
->contents
;
3744 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3747 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3748 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3756 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3757 the procedure linkage table (the .plt section) has been
3759 memmove (extdyn
, next
,
3760 sdynamic
->size
- (next
- sdynamic
->contents
));
3765 if (strip_zero_sized
)
3767 /* Regenerate program headers. */
3768 elf_seg_map (info
->output_bfd
) = NULL
;
3769 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
,
3776 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3777 1 if a DT_NEEDED tag already exists, and 0 on success. */
3780 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3782 struct elf_link_hash_table
*hash_table
;
3786 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3789 hash_table
= elf_hash_table (info
);
3790 soname
= elf_dt_name (abfd
);
3791 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, false);
3792 if (strindex
== (size_t) -1)
3795 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3798 const struct elf_backend_data
*bed
;
3801 bed
= get_elf_backend_data (hash_table
->dynobj
);
3802 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3803 if (sdyn
!= NULL
&& sdyn
->size
!= 0)
3804 for (extdyn
= sdyn
->contents
;
3805 extdyn
< sdyn
->contents
+ sdyn
->size
;
3806 extdyn
+= bed
->s
->sizeof_dyn
)
3808 Elf_Internal_Dyn dyn
;
3810 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3811 if (dyn
.d_tag
== DT_NEEDED
3812 && dyn
.d_un
.d_val
== strindex
)
3814 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3820 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3823 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3829 /* Return true if SONAME is on the needed list between NEEDED and STOP
3830 (or the end of list if STOP is NULL), and needed by a library that
3834 on_needed_list (const char *soname
,
3835 struct bfd_link_needed_list
*needed
,
3836 struct bfd_link_needed_list
*stop
)
3838 struct bfd_link_needed_list
*look
;
3839 for (look
= needed
; look
!= stop
; look
= look
->next
)
3840 if (strcmp (soname
, look
->name
) == 0
3841 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3842 /* If needed by a library that itself is not directly
3843 needed, recursively check whether that library is
3844 indirectly needed. Since we add DT_NEEDED entries to
3845 the end of the list, library dependencies appear after
3846 the library. Therefore search prior to the current
3847 LOOK, preventing possible infinite recursion. */
3848 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3854 /* Sort symbol by value, section, size, and type. */
3856 elf_sort_symbol (const void *arg1
, const void *arg2
)
3858 const struct elf_link_hash_entry
*h1
;
3859 const struct elf_link_hash_entry
*h2
;
3860 bfd_signed_vma vdiff
;
3865 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3866 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3867 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3869 return vdiff
> 0 ? 1 : -1;
3871 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3875 /* Sort so that sized symbols are selected over zero size symbols. */
3876 vdiff
= h1
->size
- h2
->size
;
3878 return vdiff
> 0 ? 1 : -1;
3880 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3881 if (h1
->type
!= h2
->type
)
3882 return h1
->type
- h2
->type
;
3884 /* If symbols are properly sized and typed, and multiple strong
3885 aliases are not defined in a shared library by the user we
3886 shouldn't get here. Unfortunately linker script symbols like
3887 __bss_start sometimes match a user symbol defined at the start of
3888 .bss without proper size and type. We'd like to preference the
3889 user symbol over reserved system symbols. Sort on leading
3891 n1
= h1
->root
.root
.string
;
3892 n2
= h2
->root
.root
.string
;
3905 /* Final sort on name selects user symbols like '_u' over reserved
3906 system symbols like '_Z' and also will avoid qsort instability. */
3910 /* This function is used to adjust offsets into .dynstr for
3911 dynamic symbols. This is called via elf_link_hash_traverse. */
3914 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3916 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3918 if (h
->dynindx
!= -1)
3919 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3923 /* Assign string offsets in .dynstr, update all structures referencing
3927 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3929 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3930 struct elf_link_local_dynamic_entry
*entry
;
3931 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3932 bfd
*dynobj
= hash_table
->dynobj
;
3935 const struct elf_backend_data
*bed
;
3938 _bfd_elf_strtab_finalize (dynstr
);
3939 size
= _bfd_elf_strtab_size (dynstr
);
3941 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3943 if (info
->callbacks
->examine_strtab
)
3944 info
->callbacks
->examine_strtab (dynstr
);
3946 bed
= get_elf_backend_data (dynobj
);
3947 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3948 BFD_ASSERT (sdyn
!= NULL
);
3950 /* Update all .dynamic entries referencing .dynstr strings. */
3951 for (extdyn
= sdyn
->contents
;
3952 extdyn
< PTR_ADD (sdyn
->contents
, sdyn
->size
);
3953 extdyn
+= bed
->s
->sizeof_dyn
)
3955 Elf_Internal_Dyn dyn
;
3957 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3961 dyn
.d_un
.d_val
= size
;
3971 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3976 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3979 /* Now update local dynamic symbols. */
3980 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3981 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3982 entry
->isym
.st_name
);
3984 /* And the rest of dynamic symbols. */
3985 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3987 /* Adjust version definitions. */
3988 if (elf_tdata (output_bfd
)->cverdefs
)
3993 Elf_Internal_Verdef def
;
3994 Elf_Internal_Verdaux defaux
;
3996 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
4000 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
4002 p
+= sizeof (Elf_External_Verdef
);
4003 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
4005 for (i
= 0; i
< def
.vd_cnt
; ++i
)
4007 _bfd_elf_swap_verdaux_in (output_bfd
,
4008 (Elf_External_Verdaux
*) p
, &defaux
);
4009 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
4011 _bfd_elf_swap_verdaux_out (output_bfd
,
4012 &defaux
, (Elf_External_Verdaux
*) p
);
4013 p
+= sizeof (Elf_External_Verdaux
);
4016 while (def
.vd_next
);
4019 /* Adjust version references. */
4020 if (elf_tdata (output_bfd
)->verref
)
4025 Elf_Internal_Verneed need
;
4026 Elf_Internal_Vernaux needaux
;
4028 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
4032 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
4034 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
4035 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
4036 (Elf_External_Verneed
*) p
);
4037 p
+= sizeof (Elf_External_Verneed
);
4038 for (i
= 0; i
< need
.vn_cnt
; ++i
)
4040 _bfd_elf_swap_vernaux_in (output_bfd
,
4041 (Elf_External_Vernaux
*) p
, &needaux
);
4042 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
4044 _bfd_elf_swap_vernaux_out (output_bfd
,
4046 (Elf_External_Vernaux
*) p
);
4047 p
+= sizeof (Elf_External_Vernaux
);
4050 while (need
.vn_next
);
4056 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4057 The default is to only match when the INPUT and OUTPUT are exactly
4061 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
4062 const bfd_target
*output
)
4064 return input
== output
;
4067 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4068 This version is used when different targets for the same architecture
4069 are virtually identical. */
4072 _bfd_elf_relocs_compatible (const bfd_target
*input
,
4073 const bfd_target
*output
)
4075 const struct elf_backend_data
*obed
, *ibed
;
4077 if (input
== output
)
4080 ibed
= xvec_get_elf_backend_data (input
);
4081 obed
= xvec_get_elf_backend_data (output
);
4083 if (ibed
->arch
!= obed
->arch
)
4086 /* If both backends are using this function, deem them compatible. */
4087 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
4090 /* Make a special call to the linker "notice" function to tell it that
4091 we are about to handle an as-needed lib, or have finished
4092 processing the lib. */
4095 _bfd_elf_notice_as_needed (bfd
*ibfd
,
4096 struct bfd_link_info
*info
,
4097 enum notice_asneeded_action act
)
4099 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
4102 /* Call ACTION on each relocation in an ELF object file. */
4105 _bfd_elf_link_iterate_on_relocs
4106 (bfd
*abfd
, struct bfd_link_info
*info
,
4107 bool (*action
) (bfd
*, struct bfd_link_info
*, asection
*,
4108 const Elf_Internal_Rela
*))
4110 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4111 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4113 /* If this object is the same format as the output object, and it is
4114 not a shared library, then let the backend look through the
4117 This is required to build global offset table entries and to
4118 arrange for dynamic relocs. It is not required for the
4119 particular common case of linking non PIC code, even when linking
4120 against shared libraries, but unfortunately there is no way of
4121 knowing whether an object file has been compiled PIC or not.
4122 Looking through the relocs is not particularly time consuming.
4123 The problem is that we must either (1) keep the relocs in memory,
4124 which causes the linker to require additional runtime memory or
4125 (2) read the relocs twice from the input file, which wastes time.
4126 This would be a good case for using mmap.
4128 I have no idea how to handle linking PIC code into a file of a
4129 different format. It probably can't be done. */
4130 if ((abfd
->flags
& DYNAMIC
) == 0
4131 && is_elf_hash_table (&htab
->root
)
4132 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4133 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4137 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4139 Elf_Internal_Rela
*internal_relocs
;
4142 /* Don't check relocations in excluded sections. Don't do
4143 anything special with non-loaded, non-alloced sections.
4144 In particular, any relocs in such sections should not
4145 affect GOT and PLT reference counting (ie. we don't
4146 allow them to create GOT or PLT entries), there's no
4147 possibility or desire to optimize TLS relocs, and
4148 there's not much point in propagating relocs to shared
4149 libs that the dynamic linker won't relocate. */
4150 if ((o
->flags
& SEC_ALLOC
) == 0
4151 || (o
->flags
& SEC_RELOC
) == 0
4152 || (o
->flags
& SEC_EXCLUDE
) != 0
4153 || o
->reloc_count
== 0
4154 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4155 && (o
->flags
& SEC_DEBUGGING
) != 0)
4156 || bfd_is_abs_section (o
->output_section
))
4159 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
4162 _bfd_link_keep_memory (info
));
4163 if (internal_relocs
== NULL
)
4166 ok
= action (abfd
, info
, o
, internal_relocs
);
4168 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4169 free (internal_relocs
);
4179 /* Check relocations in an ELF object file. This is called after
4180 all input files have been opened. */
4183 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
4185 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4186 if (bed
->check_relocs
!= NULL
)
4187 return _bfd_elf_link_iterate_on_relocs (abfd
, info
,
4192 /* Add symbols from an ELF object file to the linker hash table. */
4195 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4197 Elf_Internal_Ehdr
*ehdr
;
4198 Elf_Internal_Shdr
*hdr
;
4202 struct elf_link_hash_entry
**sym_hash
;
4204 Elf_External_Versym
*extversym
= NULL
;
4205 Elf_External_Versym
*extversym_end
= NULL
;
4206 Elf_External_Versym
*ever
;
4207 struct elf_link_hash_entry
*weaks
;
4208 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4209 size_t nondeflt_vers_cnt
= 0;
4210 Elf_Internal_Sym
*isymbuf
= NULL
;
4211 Elf_Internal_Sym
*isym
;
4212 Elf_Internal_Sym
*isymend
;
4213 const struct elf_backend_data
*bed
;
4215 struct elf_link_hash_table
*htab
;
4216 void *alloc_mark
= NULL
;
4217 struct bfd_hash_entry
**old_table
= NULL
;
4218 unsigned int old_size
= 0;
4219 unsigned int old_count
= 0;
4220 void *old_tab
= NULL
;
4222 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4223 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4224 void *old_strtab
= NULL
;
4229 htab
= elf_hash_table (info
);
4230 bed
= get_elf_backend_data (abfd
);
4232 if ((abfd
->flags
& DYNAMIC
) == 0)
4238 /* You can't use -r against a dynamic object. Also, there's no
4239 hope of using a dynamic object which does not exactly match
4240 the format of the output file. */
4241 if (bfd_link_relocatable (info
)
4242 || !is_elf_hash_table (&htab
->root
)
4243 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4245 if (bfd_link_relocatable (info
))
4246 bfd_set_error (bfd_error_invalid_operation
);
4248 bfd_set_error (bfd_error_wrong_format
);
4253 ehdr
= elf_elfheader (abfd
);
4254 if (info
->warn_alternate_em
4255 && bed
->elf_machine_code
!= ehdr
->e_machine
4256 && ((bed
->elf_machine_alt1
!= 0
4257 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4258 || (bed
->elf_machine_alt2
!= 0
4259 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4261 /* xgettext:c-format */
4262 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4263 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4265 /* As a GNU extension, any input sections which are named
4266 .gnu.warning.SYMBOL are treated as warning symbols for the given
4267 symbol. This differs from .gnu.warning sections, which generate
4268 warnings when they are included in an output file. */
4269 /* PR 12761: Also generate this warning when building shared libraries. */
4270 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4274 name
= bfd_section_name (s
);
4275 if (startswith (name
, ".gnu.warning."))
4280 name
+= sizeof ".gnu.warning." - 1;
4282 /* If this is a shared object, then look up the symbol
4283 in the hash table. If it is there, and it is already
4284 been defined, then we will not be using the entry
4285 from this shared object, so we don't need to warn.
4286 FIXME: If we see the definition in a regular object
4287 later on, we will warn, but we shouldn't. The only
4288 fix is to keep track of what warnings we are supposed
4289 to emit, and then handle them all at the end of the
4293 struct elf_link_hash_entry
*h
;
4295 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
4297 /* FIXME: What about bfd_link_hash_common? */
4299 && (h
->root
.type
== bfd_link_hash_defined
4300 || h
->root
.type
== bfd_link_hash_defweak
))
4305 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4309 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4314 if (! (_bfd_generic_link_add_one_symbol
4315 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4316 false, bed
->collect
, NULL
)))
4319 if (bfd_link_executable (info
))
4321 /* Clobber the section size so that the warning does
4322 not get copied into the output file. */
4325 /* Also set SEC_EXCLUDE, so that symbols defined in
4326 the warning section don't get copied to the output. */
4327 s
->flags
|= SEC_EXCLUDE
;
4332 just_syms
= ((s
= abfd
->sections
) != NULL
4333 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4338 /* If we are creating a shared library, create all the dynamic
4339 sections immediately. We need to attach them to something,
4340 so we attach them to this BFD, provided it is the right
4341 format and is not from ld --just-symbols. Always create the
4342 dynamic sections for -E/--dynamic-list. FIXME: If there
4343 are no input BFD's of the same format as the output, we can't
4344 make a shared library. */
4346 && (bfd_link_pic (info
)
4347 || (!bfd_link_relocatable (info
)
4349 && (info
->export_dynamic
|| info
->dynamic
)))
4350 && is_elf_hash_table (&htab
->root
)
4351 && info
->output_bfd
->xvec
== abfd
->xvec
4352 && !htab
->dynamic_sections_created
)
4354 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4358 else if (!is_elf_hash_table (&htab
->root
))
4362 const char *soname
= NULL
;
4364 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4365 const Elf_Internal_Phdr
*phdr
;
4366 struct elf_link_loaded_list
*loaded_lib
;
4368 /* ld --just-symbols and dynamic objects don't mix very well.
4369 ld shouldn't allow it. */
4373 /* If this dynamic lib was specified on the command line with
4374 --as-needed in effect, then we don't want to add a DT_NEEDED
4375 tag unless the lib is actually used. Similary for libs brought
4376 in by another lib's DT_NEEDED. When --no-add-needed is used
4377 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4378 any dynamic library in DT_NEEDED tags in the dynamic lib at
4380 add_needed
= (elf_dyn_lib_class (abfd
)
4381 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4382 | DYN_NO_NEEDED
)) == 0;
4384 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4385 if (s
!= NULL
&& s
->size
!= 0)
4389 unsigned int elfsec
;
4390 unsigned long shlink
;
4392 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4399 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4400 if (elfsec
== SHN_BAD
)
4401 goto error_free_dyn
;
4402 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4404 for (extdyn
= dynbuf
;
4405 extdyn
<= dynbuf
+ s
->size
- bed
->s
->sizeof_dyn
;
4406 extdyn
+= bed
->s
->sizeof_dyn
)
4408 Elf_Internal_Dyn dyn
;
4410 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4411 if (dyn
.d_tag
== DT_SONAME
)
4413 unsigned int tagv
= dyn
.d_un
.d_val
;
4414 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4416 goto error_free_dyn
;
4418 if (dyn
.d_tag
== DT_NEEDED
)
4420 struct bfd_link_needed_list
*n
, **pn
;
4422 unsigned int tagv
= dyn
.d_un
.d_val
;
4423 size_t amt
= sizeof (struct bfd_link_needed_list
);
4425 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4426 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4427 if (n
== NULL
|| fnm
== NULL
)
4428 goto error_free_dyn
;
4429 amt
= strlen (fnm
) + 1;
4430 anm
= (char *) bfd_alloc (abfd
, amt
);
4432 goto error_free_dyn
;
4433 memcpy (anm
, fnm
, amt
);
4437 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4441 if (dyn
.d_tag
== DT_RUNPATH
)
4443 struct bfd_link_needed_list
*n
, **pn
;
4445 unsigned int tagv
= dyn
.d_un
.d_val
;
4446 size_t amt
= sizeof (struct bfd_link_needed_list
);
4448 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4449 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4450 if (n
== NULL
|| fnm
== NULL
)
4451 goto error_free_dyn
;
4452 amt
= strlen (fnm
) + 1;
4453 anm
= (char *) bfd_alloc (abfd
, amt
);
4455 goto error_free_dyn
;
4456 memcpy (anm
, fnm
, amt
);
4460 for (pn
= & runpath
;
4466 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4467 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4469 struct bfd_link_needed_list
*n
, **pn
;
4471 unsigned int tagv
= dyn
.d_un
.d_val
;
4472 size_t amt
= sizeof (struct bfd_link_needed_list
);
4474 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4475 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4476 if (n
== NULL
|| fnm
== NULL
)
4477 goto error_free_dyn
;
4478 amt
= strlen (fnm
) + 1;
4479 anm
= (char *) bfd_alloc (abfd
, amt
);
4481 goto error_free_dyn
;
4482 memcpy (anm
, fnm
, amt
);
4492 if (dyn
.d_tag
== DT_AUDIT
)
4494 unsigned int tagv
= dyn
.d_un
.d_val
;
4495 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4497 if (dyn
.d_tag
== DT_FLAGS_1
)
4498 elf_tdata (abfd
)->is_pie
= (dyn
.d_un
.d_val
& DF_1_PIE
) != 0;
4504 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4505 frees all more recently bfd_alloc'd blocks as well. */
4511 struct bfd_link_needed_list
**pn
;
4512 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4517 /* If we have a PT_GNU_RELRO program header, mark as read-only
4518 all sections contained fully therein. This makes relro
4519 shared library sections appear as they will at run-time. */
4520 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4521 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4522 if (phdr
->p_type
== PT_GNU_RELRO
)
4524 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4526 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4528 if ((s
->flags
& SEC_ALLOC
) != 0
4529 && s
->vma
* opb
>= phdr
->p_vaddr
4530 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4531 s
->flags
|= SEC_READONLY
;
4536 /* We do not want to include any of the sections in a dynamic
4537 object in the output file. We hack by simply clobbering the
4538 list of sections in the BFD. This could be handled more
4539 cleanly by, say, a new section flag; the existing
4540 SEC_NEVER_LOAD flag is not the one we want, because that one
4541 still implies that the section takes up space in the output
4543 bfd_section_list_clear (abfd
);
4545 /* Find the name to use in a DT_NEEDED entry that refers to this
4546 object. If the object has a DT_SONAME entry, we use it.
4547 Otherwise, if the generic linker stuck something in
4548 elf_dt_name, we use that. Otherwise, we just use the file
4550 if (soname
== NULL
|| *soname
== '\0')
4552 soname
= elf_dt_name (abfd
);
4553 if (soname
== NULL
|| *soname
== '\0')
4554 soname
= bfd_get_filename (abfd
);
4557 /* Save the SONAME because sometimes the linker emulation code
4558 will need to know it. */
4559 elf_dt_name (abfd
) = soname
;
4561 /* If we have already included this dynamic object in the
4562 link, just ignore it. There is no reason to include a
4563 particular dynamic object more than once. */
4564 for (loaded_lib
= htab
->dyn_loaded
;
4566 loaded_lib
= loaded_lib
->next
)
4568 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4572 /* Create dynamic sections for backends that require that be done
4573 before setup_gnu_properties. */
4575 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4578 /* Save the DT_AUDIT entry for the linker emulation code. */
4579 elf_dt_audit (abfd
) = audit
;
4582 /* If this is a dynamic object, we always link against the .dynsym
4583 symbol table, not the .symtab symbol table. The dynamic linker
4584 will only see the .dynsym symbol table, so there is no reason to
4585 look at .symtab for a dynamic object. */
4587 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4588 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4590 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4592 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4594 /* The sh_info field of the symtab header tells us where the
4595 external symbols start. We don't care about the local symbols at
4597 if (elf_bad_symtab (abfd
))
4599 extsymcount
= symcount
;
4604 extsymcount
= symcount
- hdr
->sh_info
;
4605 extsymoff
= hdr
->sh_info
;
4608 sym_hash
= elf_sym_hashes (abfd
);
4609 if (extsymcount
!= 0)
4611 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4613 if (isymbuf
== NULL
)
4616 if (sym_hash
== NULL
)
4618 /* We store a pointer to the hash table entry for each
4620 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4621 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4622 if (sym_hash
== NULL
)
4623 goto error_free_sym
;
4624 elf_sym_hashes (abfd
) = sym_hash
;
4630 /* Read in any version definitions. */
4631 if (!_bfd_elf_slurp_version_tables (abfd
,
4632 info
->default_imported_symver
))
4633 goto error_free_sym
;
4635 /* Read in the symbol versions, but don't bother to convert them
4636 to internal format. */
4637 if (elf_dynversym (abfd
) != 0)
4639 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4640 bfd_size_type amt
= versymhdr
->sh_size
;
4642 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4643 goto error_free_sym
;
4644 extversym
= (Elf_External_Versym
*)
4645 _bfd_malloc_and_read (abfd
, amt
, amt
);
4646 if (extversym
== NULL
)
4647 goto error_free_sym
;
4648 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4652 /* If we are loading an as-needed shared lib, save the symbol table
4653 state before we start adding symbols. If the lib turns out
4654 to be unneeded, restore the state. */
4655 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4660 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4662 struct bfd_hash_entry
*p
;
4663 struct elf_link_hash_entry
*h
;
4665 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4667 h
= (struct elf_link_hash_entry
*) p
;
4668 entsize
+= htab
->root
.table
.entsize
;
4669 if (h
->root
.type
== bfd_link_hash_warning
)
4671 entsize
+= htab
->root
.table
.entsize
;
4672 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4674 if (h
->root
.type
== bfd_link_hash_common
)
4675 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4679 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4680 old_tab
= bfd_malloc (tabsize
+ entsize
);
4681 if (old_tab
== NULL
)
4682 goto error_free_vers
;
4684 /* Remember the current objalloc pointer, so that all mem for
4685 symbols added can later be reclaimed. */
4686 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4687 if (alloc_mark
== NULL
)
4688 goto error_free_vers
;
4690 /* Make a special call to the linker "notice" function to
4691 tell it that we are about to handle an as-needed lib. */
4692 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4693 goto error_free_vers
;
4695 /* Clone the symbol table. Remember some pointers into the
4696 symbol table, and dynamic symbol count. */
4697 old_ent
= (char *) old_tab
+ tabsize
;
4698 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4699 old_undefs
= htab
->root
.undefs
;
4700 old_undefs_tail
= htab
->root
.undefs_tail
;
4701 old_table
= htab
->root
.table
.table
;
4702 old_size
= htab
->root
.table
.size
;
4703 old_count
= htab
->root
.table
.count
;
4705 if (htab
->dynstr
!= NULL
)
4707 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4708 if (old_strtab
== NULL
)
4709 goto error_free_vers
;
4712 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4714 struct bfd_hash_entry
*p
;
4715 struct elf_link_hash_entry
*h
;
4717 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4719 h
= (struct elf_link_hash_entry
*) p
;
4720 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4721 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4722 if (h
->root
.type
== bfd_link_hash_warning
)
4724 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4725 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4726 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4728 if (h
->root
.type
== bfd_link_hash_common
)
4730 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4731 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4738 if (extversym
== NULL
)
4740 else if (extversym
+ extsymoff
< extversym_end
)
4741 ever
= extversym
+ extsymoff
;
4744 /* xgettext:c-format */
4745 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4746 abfd
, (long) extsymoff
,
4747 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4748 bfd_set_error (bfd_error_bad_value
);
4749 goto error_free_vers
;
4752 if (!bfd_link_relocatable (info
)
4753 && abfd
->lto_slim_object
)
4756 (_("%pB: plugin needed to handle lto object"), abfd
);
4759 for (isym
= isymbuf
, isymend
= PTR_ADD (isymbuf
, extsymcount
);
4761 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4765 asection
*sec
, *new_sec
;
4768 struct elf_link_hash_entry
*h
;
4769 struct elf_link_hash_entry
*hi
;
4771 bool size_change_ok
;
4772 bool type_change_ok
;
4778 unsigned int old_alignment
;
4779 unsigned int shindex
;
4785 flags
= BSF_NO_FLAGS
;
4787 value
= isym
->st_value
;
4788 common
= bed
->common_definition (isym
);
4789 if (common
&& info
->inhibit_common_definition
)
4791 /* Treat common symbol as undefined for --no-define-common. */
4792 isym
->st_shndx
= SHN_UNDEF
;
4797 bind
= ELF_ST_BIND (isym
->st_info
);
4801 /* This should be impossible, since ELF requires that all
4802 global symbols follow all local symbols, and that sh_info
4803 point to the first global symbol. Unfortunately, Irix 5
4805 if (elf_bad_symtab (abfd
))
4808 /* If we aren't prepared to handle locals within the globals
4809 then we'll likely segfault on a NULL symbol hash if the
4810 symbol is ever referenced in relocations. */
4811 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4812 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4813 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4814 " (>= sh_info of %lu)"),
4815 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4818 /* Dynamic object relocations are not processed by ld, so
4819 ld won't run into the problem mentioned above. */
4822 bfd_set_error (bfd_error_bad_value
);
4823 goto error_free_vers
;
4826 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4834 case STB_GNU_UNIQUE
:
4835 flags
= BSF_GNU_UNIQUE
;
4839 /* Leave it up to the processor backend. */
4843 if (isym
->st_shndx
== SHN_UNDEF
)
4844 sec
= bfd_und_section_ptr
;
4845 else if (isym
->st_shndx
== SHN_ABS
)
4846 sec
= bfd_abs_section_ptr
;
4847 else if (isym
->st_shndx
== SHN_COMMON
)
4849 sec
= bfd_com_section_ptr
;
4850 /* What ELF calls the size we call the value. What ELF
4851 calls the value we call the alignment. */
4852 value
= isym
->st_size
;
4856 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4858 sec
= bfd_abs_section_ptr
;
4859 else if (discarded_section (sec
))
4861 /* Symbols from discarded section are undefined. We keep
4863 sec
= bfd_und_section_ptr
;
4865 isym
->st_shndx
= SHN_UNDEF
;
4867 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4871 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4874 goto error_free_vers
;
4876 if (isym
->st_shndx
== SHN_COMMON
4877 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4879 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4883 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4885 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4887 goto error_free_vers
;
4891 else if (isym
->st_shndx
== SHN_COMMON
4892 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4893 && !bfd_link_relocatable (info
))
4895 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4899 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4900 | SEC_LINKER_CREATED
);
4901 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4903 goto error_free_vers
;
4907 else if (bed
->elf_add_symbol_hook
)
4909 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4911 goto error_free_vers
;
4913 /* The hook function sets the name to NULL if this symbol
4914 should be skipped for some reason. */
4919 /* Sanity check that all possibilities were handled. */
4923 /* Silently discard TLS symbols from --just-syms. There's
4924 no way to combine a static TLS block with a new TLS block
4925 for this executable. */
4926 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4927 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4930 if (bfd_is_und_section (sec
)
4931 || bfd_is_com_section (sec
))
4936 size_change_ok
= false;
4937 type_change_ok
= bed
->type_change_ok
;
4944 if (is_elf_hash_table (&htab
->root
))
4946 Elf_Internal_Versym iver
;
4947 unsigned int vernum
= 0;
4952 if (info
->default_imported_symver
)
4953 /* Use the default symbol version created earlier. */
4954 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4958 else if (ever
>= extversym_end
)
4960 /* xgettext:c-format */
4961 _bfd_error_handler (_("%pB: not enough version information"),
4963 bfd_set_error (bfd_error_bad_value
);
4964 goto error_free_vers
;
4967 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4969 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4971 /* If this is a hidden symbol, or if it is not version
4972 1, we append the version name to the symbol name.
4973 However, we do not modify a non-hidden absolute symbol
4974 if it is not a function, because it might be the version
4975 symbol itself. FIXME: What if it isn't? */
4976 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4978 && (!bfd_is_abs_section (sec
)
4979 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4982 size_t namelen
, verlen
, newlen
;
4985 if (isym
->st_shndx
!= SHN_UNDEF
)
4987 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4989 else if (vernum
> 1)
4991 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4998 /* xgettext:c-format */
4999 (_("%pB: %s: invalid version %u (max %d)"),
5001 elf_tdata (abfd
)->cverdefs
);
5002 bfd_set_error (bfd_error_bad_value
);
5003 goto error_free_vers
;
5008 /* We cannot simply test for the number of
5009 entries in the VERNEED section since the
5010 numbers for the needed versions do not start
5012 Elf_Internal_Verneed
*t
;
5015 for (t
= elf_tdata (abfd
)->verref
;
5019 Elf_Internal_Vernaux
*a
;
5021 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5023 if (a
->vna_other
== vernum
)
5025 verstr
= a
->vna_nodename
;
5035 /* xgettext:c-format */
5036 (_("%pB: %s: invalid needed version %d"),
5037 abfd
, name
, vernum
);
5038 bfd_set_error (bfd_error_bad_value
);
5039 goto error_free_vers
;
5043 namelen
= strlen (name
);
5044 verlen
= strlen (verstr
);
5045 newlen
= namelen
+ verlen
+ 2;
5046 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5047 && isym
->st_shndx
!= SHN_UNDEF
)
5050 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
5051 if (newname
== NULL
)
5052 goto error_free_vers
;
5053 memcpy (newname
, name
, namelen
);
5054 p
= newname
+ namelen
;
5056 /* If this is a defined non-hidden version symbol,
5057 we add another @ to the name. This indicates the
5058 default version of the symbol. */
5059 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5060 && isym
->st_shndx
!= SHN_UNDEF
)
5062 memcpy (p
, verstr
, verlen
+ 1);
5067 /* If this symbol has default visibility and the user has
5068 requested we not re-export it, then mark it as hidden. */
5069 if (!bfd_is_und_section (sec
)
5072 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
5073 isym
->st_other
= (STV_HIDDEN
5074 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
5076 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
5077 sym_hash
, &old_bfd
, &old_weak
,
5078 &old_alignment
, &skip
, &override
,
5079 &type_change_ok
, &size_change_ok
,
5081 goto error_free_vers
;
5086 /* Override a definition only if the new symbol matches the
5088 if (override
&& matched
)
5092 while (h
->root
.type
== bfd_link_hash_indirect
5093 || h
->root
.type
== bfd_link_hash_warning
)
5094 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5096 if (h
->versioned
!= unversioned
5097 && elf_tdata (abfd
)->verdef
!= NULL
5100 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
5103 if (! (_bfd_generic_link_add_one_symbol
5104 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
5105 NULL
, false, bed
->collect
,
5106 (struct bfd_link_hash_entry
**) sym_hash
)))
5107 goto error_free_vers
;
5110 /* We need to make sure that indirect symbol dynamic flags are
5113 while (h
->root
.type
== bfd_link_hash_indirect
5114 || h
->root
.type
== bfd_link_hash_warning
)
5115 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5119 /* Setting the index to -3 tells elf_link_output_extsym that
5120 this symbol is defined in a discarded section. */
5121 if (discarded
&& is_elf_hash_table (&htab
->root
))
5124 new_weak
= (flags
& BSF_WEAK
) != 0;
5128 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
5129 && is_elf_hash_table (&htab
->root
)
5130 && h
->u
.alias
== NULL
)
5132 /* Keep a list of all weak defined non function symbols from
5133 a dynamic object, using the alias field. Later in this
5134 function we will set the alias field to the correct
5135 value. We only put non-function symbols from dynamic
5136 objects on this list, because that happens to be the only
5137 time we need to know the normal symbol corresponding to a
5138 weak symbol, and the information is time consuming to
5139 figure out. If the alias field is not already NULL,
5140 then this symbol was already defined by some previous
5141 dynamic object, and we will be using that previous
5142 definition anyhow. */
5148 /* Set the alignment of a common symbol. */
5149 if ((common
|| bfd_is_com_section (sec
))
5150 && h
->root
.type
== bfd_link_hash_common
)
5155 align
= bfd_log2 (isym
->st_value
);
5158 /* The new symbol is a common symbol in a shared object.
5159 We need to get the alignment from the section. */
5160 align
= new_sec
->alignment_power
;
5162 if (align
> old_alignment
)
5163 h
->root
.u
.c
.p
->alignment_power
= align
;
5165 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5168 if (is_elf_hash_table (&htab
->root
))
5170 /* Set a flag in the hash table entry indicating the type of
5171 reference or definition we just found. A dynamic symbol
5172 is one which is referenced or defined by both a regular
5173 object and a shared object. */
5174 bool dynsym
= false;
5176 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5177 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5179 /* Except for this flag to track nonweak references. */
5181 && bind
!= STB_WEAK
)
5182 h
->ref_ir_nonweak
= 1;
5189 if (bind
!= STB_WEAK
)
5190 h
->ref_regular_nonweak
= 1;
5207 hi
->ref_dynamic
= 1;
5212 hi
->def_dynamic
= 1;
5216 /* If an indirect symbol has been forced local, don't
5217 make the real symbol dynamic. */
5218 if (h
!= hi
&& hi
->forced_local
)
5222 if (bfd_link_dll (info
)
5232 && weakdef (h
)->dynindx
!= -1))
5236 /* Check to see if we need to add an indirect symbol for
5237 the default name. */
5239 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5241 && hi
->versioned
== versioned_hidden
))
5242 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5243 sec
, value
, &old_bfd
, &dynsym
))
5244 goto error_free_vers
;
5246 /* Check the alignment when a common symbol is involved. This
5247 can change when a common symbol is overridden by a normal
5248 definition or a common symbol is ignored due to the old
5249 normal definition. We need to make sure the maximum
5250 alignment is maintained. */
5251 if ((old_alignment
|| common
)
5252 && h
->root
.type
!= bfd_link_hash_common
)
5254 unsigned int common_align
;
5255 unsigned int normal_align
;
5256 unsigned int symbol_align
;
5260 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5261 || h
->root
.type
== bfd_link_hash_defweak
);
5263 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5264 if (h
->root
.u
.def
.section
->owner
!= NULL
5265 && (h
->root
.u
.def
.section
->owner
->flags
5266 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5268 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5269 if (normal_align
> symbol_align
)
5270 normal_align
= symbol_align
;
5273 normal_align
= symbol_align
;
5277 common_align
= old_alignment
;
5278 common_bfd
= old_bfd
;
5283 common_align
= bfd_log2 (isym
->st_value
);
5285 normal_bfd
= old_bfd
;
5288 if (normal_align
< common_align
)
5290 /* PR binutils/2735 */
5291 if (normal_bfd
== NULL
)
5293 /* xgettext:c-format */
5294 (_("warning: alignment %u of common symbol `%s' in %pB is"
5295 " greater than the alignment (%u) of its section %pA"),
5296 1 << common_align
, name
, common_bfd
,
5297 1 << normal_align
, h
->root
.u
.def
.section
);
5300 /* xgettext:c-format */
5301 (_("warning: alignment %u of symbol `%s' in %pB"
5302 " is smaller than %u in %pB"),
5303 1 << normal_align
, name
, normal_bfd
,
5304 1 << common_align
, common_bfd
);
5308 /* Remember the symbol size if it isn't undefined. */
5309 if (isym
->st_size
!= 0
5310 && isym
->st_shndx
!= SHN_UNDEF
5311 && (definition
|| h
->size
== 0))
5314 && h
->size
!= isym
->st_size
5315 && ! size_change_ok
)
5317 /* xgettext:c-format */
5318 (_("warning: size of symbol `%s' changed"
5319 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5320 name
, (uint64_t) h
->size
, old_bfd
,
5321 (uint64_t) isym
->st_size
, abfd
);
5323 h
->size
= isym
->st_size
;
5326 /* If this is a common symbol, then we always want H->SIZE
5327 to be the size of the common symbol. The code just above
5328 won't fix the size if a common symbol becomes larger. We
5329 don't warn about a size change here, because that is
5330 covered by --warn-common. Allow changes between different
5332 if (h
->root
.type
== bfd_link_hash_common
)
5333 h
->size
= h
->root
.u
.c
.size
;
5335 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5336 && ((definition
&& !new_weak
)
5337 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5338 || h
->type
== STT_NOTYPE
))
5340 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5342 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5344 if (type
== STT_GNU_IFUNC
5345 && (abfd
->flags
& DYNAMIC
) != 0)
5348 if (h
->type
!= type
)
5350 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5351 /* xgettext:c-format */
5353 (_("warning: type of symbol `%s' changed"
5354 " from %d to %d in %pB"),
5355 name
, h
->type
, type
, abfd
);
5361 /* Merge st_other field. */
5362 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5363 definition
, dynamic
);
5365 /* We don't want to make debug symbol dynamic. */
5367 && (sec
->flags
& SEC_DEBUGGING
)
5368 && !bfd_link_relocatable (info
))
5371 /* Nor should we make plugin symbols dynamic. */
5372 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5377 h
->target_internal
= isym
->st_target_internal
;
5378 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5381 if (definition
&& !dynamic
)
5383 char *p
= strchr (name
, ELF_VER_CHR
);
5384 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5386 /* Queue non-default versions so that .symver x, x@FOO
5387 aliases can be checked. */
5390 size_t amt
= ((isymend
- isym
+ 1)
5391 * sizeof (struct elf_link_hash_entry
*));
5393 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5395 goto error_free_vers
;
5397 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5401 if (dynsym
&& h
->dynindx
== -1)
5403 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5404 goto error_free_vers
;
5406 && weakdef (h
)->dynindx
== -1)
5408 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5409 goto error_free_vers
;
5412 else if (h
->dynindx
!= -1)
5413 /* If the symbol already has a dynamic index, but
5414 visibility says it should not be visible, turn it into
5416 switch (ELF_ST_VISIBILITY (h
->other
))
5420 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
5428 && h
->root
.type
!= bfd_link_hash_indirect
5430 && h
->ref_regular_nonweak
)
5432 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5433 && h
->ref_ir_nonweak
5434 && !info
->lto_all_symbols_read
)
5435 || (h
->ref_dynamic_nonweak
5436 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5437 && !on_needed_list (elf_dt_name (abfd
),
5438 htab
->needed
, NULL
))))
5440 const char *soname
= elf_dt_name (abfd
);
5442 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5443 h
->root
.root
.string
);
5445 /* A symbol from a library loaded via DT_NEEDED of some
5446 other library is referenced by a regular object.
5447 Add a DT_NEEDED entry for it. Issue an error if
5448 --no-add-needed is used and the reference was not
5451 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5454 /* xgettext:c-format */
5455 (_("%pB: undefined reference to symbol '%s'"),
5457 bfd_set_error (bfd_error_missing_dso
);
5458 goto error_free_vers
;
5461 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5462 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5464 /* Create dynamic sections for backends that require
5465 that be done before setup_gnu_properties. */
5466 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5473 if (info
->lto_plugin_active
5474 && !bfd_link_relocatable (info
)
5475 && (abfd
->flags
& BFD_PLUGIN
) == 0
5481 if (bed
->s
->arch_size
== 32)
5486 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5487 referenced in regular objects so that linker plugin will get
5488 the correct symbol resolution. */
5490 sym_hash
= elf_sym_hashes (abfd
);
5491 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5493 Elf_Internal_Rela
*internal_relocs
;
5494 Elf_Internal_Rela
*rel
, *relend
;
5496 /* Don't check relocations in excluded sections. */
5497 if ((s
->flags
& SEC_RELOC
) == 0
5498 || s
->reloc_count
== 0
5499 || (s
->flags
& SEC_EXCLUDE
) != 0
5500 || ((info
->strip
== strip_all
5501 || info
->strip
== strip_debugger
)
5502 && (s
->flags
& SEC_DEBUGGING
) != 0))
5505 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
5508 _bfd_link_keep_memory (info
));
5509 if (internal_relocs
== NULL
)
5510 goto error_free_vers
;
5512 rel
= internal_relocs
;
5513 relend
= rel
+ s
->reloc_count
;
5514 for ( ; rel
< relend
; rel
++)
5516 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5517 struct elf_link_hash_entry
*h
;
5519 /* Skip local symbols. */
5520 if (r_symndx
< extsymoff
)
5523 h
= sym_hash
[r_symndx
- extsymoff
];
5525 h
->root
.non_ir_ref_regular
= 1;
5528 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5529 free (internal_relocs
);
5538 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5542 /* Restore the symbol table. */
5543 old_ent
= (char *) old_tab
+ tabsize
;
5544 memset (elf_sym_hashes (abfd
), 0,
5545 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5546 htab
->root
.table
.table
= old_table
;
5547 htab
->root
.table
.size
= old_size
;
5548 htab
->root
.table
.count
= old_count
;
5549 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5550 htab
->root
.undefs
= old_undefs
;
5551 htab
->root
.undefs_tail
= old_undefs_tail
;
5552 if (htab
->dynstr
!= NULL
)
5553 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5556 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5558 struct bfd_hash_entry
*p
;
5559 struct elf_link_hash_entry
*h
;
5560 unsigned int non_ir_ref_dynamic
;
5562 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5564 /* Preserve non_ir_ref_dynamic so that this symbol
5565 will be exported when the dynamic lib becomes needed
5566 in the second pass. */
5567 h
= (struct elf_link_hash_entry
*) p
;
5568 if (h
->root
.type
== bfd_link_hash_warning
)
5569 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5570 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5572 h
= (struct elf_link_hash_entry
*) p
;
5573 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5574 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5575 if (h
->root
.type
== bfd_link_hash_warning
)
5577 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5578 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5579 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5581 if (h
->root
.type
== bfd_link_hash_common
)
5583 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5584 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5586 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5590 /* Make a special call to the linker "notice" function to
5591 tell it that symbols added for crefs may need to be removed. */
5592 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5593 goto error_free_vers
;
5596 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5598 free (nondeflt_vers
);
5602 if (old_tab
!= NULL
)
5604 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5605 goto error_free_vers
;
5610 /* Now that all the symbols from this input file are created, if
5611 not performing a relocatable link, handle .symver foo, foo@BAR
5612 such that any relocs against foo become foo@BAR. */
5613 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5617 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5619 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5620 char *shortname
, *p
;
5623 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5625 || (h
->root
.type
!= bfd_link_hash_defined
5626 && h
->root
.type
!= bfd_link_hash_defweak
))
5629 amt
= p
- h
->root
.root
.string
;
5630 shortname
= (char *) bfd_malloc (amt
+ 1);
5632 goto error_free_vers
;
5633 memcpy (shortname
, h
->root
.root
.string
, amt
);
5634 shortname
[amt
] = '\0';
5636 hi
= (struct elf_link_hash_entry
*)
5637 bfd_link_hash_lookup (&htab
->root
, shortname
,
5638 false, false, false);
5640 && hi
->root
.type
== h
->root
.type
5641 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5642 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5644 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
5645 hi
->root
.type
= bfd_link_hash_indirect
;
5646 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5647 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5648 sym_hash
= elf_sym_hashes (abfd
);
5650 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5651 if (sym_hash
[symidx
] == hi
)
5653 sym_hash
[symidx
] = h
;
5659 free (nondeflt_vers
);
5660 nondeflt_vers
= NULL
;
5663 /* Now set the alias field correctly for all the weak defined
5664 symbols we found. The only way to do this is to search all the
5665 symbols. Since we only need the information for non functions in
5666 dynamic objects, that's the only time we actually put anything on
5667 the list WEAKS. We need this information so that if a regular
5668 object refers to a symbol defined weakly in a dynamic object, the
5669 real symbol in the dynamic object is also put in the dynamic
5670 symbols; we also must arrange for both symbols to point to the
5671 same memory location. We could handle the general case of symbol
5672 aliasing, but a general symbol alias can only be generated in
5673 assembler code, handling it correctly would be very time
5674 consuming, and other ELF linkers don't handle general aliasing
5678 struct elf_link_hash_entry
**hpp
;
5679 struct elf_link_hash_entry
**hppend
;
5680 struct elf_link_hash_entry
**sorted_sym_hash
;
5681 struct elf_link_hash_entry
*h
;
5682 size_t sym_count
, amt
;
5684 /* Since we have to search the whole symbol list for each weak
5685 defined symbol, search time for N weak defined symbols will be
5686 O(N^2). Binary search will cut it down to O(NlogN). */
5687 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5688 sorted_sym_hash
= bfd_malloc (amt
);
5689 if (sorted_sym_hash
== NULL
)
5691 sym_hash
= sorted_sym_hash
;
5692 hpp
= elf_sym_hashes (abfd
);
5693 hppend
= hpp
+ extsymcount
;
5695 for (; hpp
< hppend
; hpp
++)
5699 && h
->root
.type
== bfd_link_hash_defined
5700 && !bed
->is_function_type (h
->type
))
5708 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5711 while (weaks
!= NULL
)
5713 struct elf_link_hash_entry
*hlook
;
5716 size_t i
, j
, idx
= 0;
5719 weaks
= hlook
->u
.alias
;
5720 hlook
->u
.alias
= NULL
;
5722 if (hlook
->root
.type
!= bfd_link_hash_defined
5723 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5726 slook
= hlook
->root
.u
.def
.section
;
5727 vlook
= hlook
->root
.u
.def
.value
;
5733 bfd_signed_vma vdiff
;
5735 h
= sorted_sym_hash
[idx
];
5736 vdiff
= vlook
- h
->root
.u
.def
.value
;
5743 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5753 /* We didn't find a value/section match. */
5757 /* With multiple aliases, or when the weak symbol is already
5758 strongly defined, we have multiple matching symbols and
5759 the binary search above may land on any of them. Step
5760 one past the matching symbol(s). */
5763 h
= sorted_sym_hash
[idx
];
5764 if (h
->root
.u
.def
.section
!= slook
5765 || h
->root
.u
.def
.value
!= vlook
)
5769 /* Now look back over the aliases. Since we sorted by size
5770 as well as value and section, we'll choose the one with
5771 the largest size. */
5774 h
= sorted_sym_hash
[idx
];
5776 /* Stop if value or section doesn't match. */
5777 if (h
->root
.u
.def
.section
!= slook
5778 || h
->root
.u
.def
.value
!= vlook
)
5780 else if (h
!= hlook
)
5782 struct elf_link_hash_entry
*t
;
5785 hlook
->is_weakalias
= 1;
5787 if (t
->u
.alias
!= NULL
)
5788 while (t
->u
.alias
!= h
)
5792 /* If the weak definition is in the list of dynamic
5793 symbols, make sure the real definition is put
5795 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5797 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5800 free (sorted_sym_hash
);
5805 /* If the real definition is in the list of dynamic
5806 symbols, make sure the weak definition is put
5807 there as well. If we don't do this, then the
5808 dynamic loader might not merge the entries for the
5809 real definition and the weak definition. */
5810 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5812 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5813 goto err_free_sym_hash
;
5820 free (sorted_sym_hash
);
5823 if (bed
->check_directives
5824 && !(*bed
->check_directives
) (abfd
, info
))
5827 /* If this is a non-traditional link, try to optimize the handling
5828 of the .stab/.stabstr sections. */
5830 && ! info
->traditional_format
5831 && is_elf_hash_table (&htab
->root
)
5832 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5836 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5837 if (stabstr
!= NULL
)
5839 bfd_size_type string_offset
= 0;
5842 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5843 if (startswith (stab
->name
, ".stab")
5844 && (!stab
->name
[5] ||
5845 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5846 && (stab
->flags
& SEC_MERGE
) == 0
5847 && !bfd_is_abs_section (stab
->output_section
))
5849 struct bfd_elf_section_data
*secdata
;
5851 secdata
= elf_section_data (stab
);
5852 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5853 stabstr
, &secdata
->sec_info
,
5856 if (secdata
->sec_info
)
5857 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5862 if (dynamic
&& add_needed
)
5864 /* Add this bfd to the loaded list. */
5865 struct elf_link_loaded_list
*n
;
5867 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5871 n
->next
= htab
->dyn_loaded
;
5872 htab
->dyn_loaded
= n
;
5874 if (dynamic
&& !add_needed
5875 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5876 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5883 free (nondeflt_vers
);
5891 /* Return the linker hash table entry of a symbol that might be
5892 satisfied by an archive symbol. Return -1 on error. */
5894 struct bfd_link_hash_entry
*
5895 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5896 struct bfd_link_info
*info
,
5899 struct bfd_link_hash_entry
*h
;
5903 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
5907 /* If this is a default version (the name contains @@), look up the
5908 symbol again with only one `@' as well as without the version.
5909 The effect is that references to the symbol with and without the
5910 version will be matched by the default symbol in the archive. */
5912 p
= strchr (name
, ELF_VER_CHR
);
5913 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5916 /* First check with only one `@'. */
5917 len
= strlen (name
);
5918 copy
= (char *) bfd_alloc (abfd
, len
);
5920 return (struct bfd_link_hash_entry
*) -1;
5922 first
= p
- name
+ 1;
5923 memcpy (copy
, name
, first
);
5924 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5926 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5929 /* We also need to check references to the symbol without the
5931 copy
[first
- 1] = '\0';
5932 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5935 bfd_release (abfd
, copy
);
5939 /* Add symbols from an ELF archive file to the linker hash table. We
5940 don't use _bfd_generic_link_add_archive_symbols because we need to
5941 handle versioned symbols.
5943 Fortunately, ELF archive handling is simpler than that done by
5944 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5945 oddities. In ELF, if we find a symbol in the archive map, and the
5946 symbol is currently undefined, we know that we must pull in that
5949 Unfortunately, we do have to make multiple passes over the symbol
5950 table until nothing further is resolved. */
5953 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5956 unsigned char *included
= NULL
;
5960 const struct elf_backend_data
*bed
;
5961 struct bfd_link_hash_entry
* (*archive_symbol_lookup
)
5962 (bfd
*, struct bfd_link_info
*, const char *);
5964 if (! bfd_has_map (abfd
))
5966 /* An empty archive is a special case. */
5967 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5969 bfd_set_error (bfd_error_no_armap
);
5973 /* Keep track of all symbols we know to be already defined, and all
5974 files we know to be already included. This is to speed up the
5975 second and subsequent passes. */
5976 c
= bfd_ardata (abfd
)->symdef_count
;
5979 amt
= c
* sizeof (*included
);
5980 included
= (unsigned char *) bfd_zmalloc (amt
);
5981 if (included
== NULL
)
5984 symdefs
= bfd_ardata (abfd
)->symdefs
;
5985 bed
= get_elf_backend_data (abfd
);
5986 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5999 symdefend
= symdef
+ c
;
6000 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
6002 struct bfd_link_hash_entry
*h
;
6004 struct bfd_link_hash_entry
*undefs_tail
;
6009 if (symdef
->file_offset
== last
)
6015 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
6016 if (h
== (struct bfd_link_hash_entry
*) -1)
6022 if (h
->type
== bfd_link_hash_undefined
)
6024 /* If the archive element has already been loaded then one
6025 of the symbols defined by that element might have been
6026 made undefined due to being in a discarded section. */
6027 if (is_elf_hash_table (info
->hash
)
6028 && ((struct elf_link_hash_entry
*) h
)->indx
== -3)
6031 else if (h
->type
== bfd_link_hash_common
)
6033 /* We currently have a common symbol. The archive map contains
6034 a reference to this symbol, so we may want to include it. We
6035 only want to include it however, if this archive element
6036 contains a definition of the symbol, not just another common
6039 Unfortunately some archivers (including GNU ar) will put
6040 declarations of common symbols into their archive maps, as
6041 well as real definitions, so we cannot just go by the archive
6042 map alone. Instead we must read in the element's symbol
6043 table and check that to see what kind of symbol definition
6045 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
6050 if (h
->type
!= bfd_link_hash_undefweak
)
6051 /* Symbol must be defined. Don't check it again. */
6056 /* We need to include this archive member. */
6057 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
,
6059 if (element
== NULL
)
6062 if (! bfd_check_format (element
, bfd_object
))
6065 undefs_tail
= info
->hash
->undefs_tail
;
6067 if (!(*info
->callbacks
6068 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
6070 if (!bfd_link_add_symbols (element
, info
))
6073 /* If there are any new undefined symbols, we need to make
6074 another pass through the archive in order to see whether
6075 they can be defined. FIXME: This isn't perfect, because
6076 common symbols wind up on undefs_tail and because an
6077 undefined symbol which is defined later on in this pass
6078 does not require another pass. This isn't a bug, but it
6079 does make the code less efficient than it could be. */
6080 if (undefs_tail
!= info
->hash
->undefs_tail
)
6083 /* Look backward to mark all symbols from this object file
6084 which we have already seen in this pass. */
6088 included
[mark
] = true;
6093 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
6095 /* We mark subsequent symbols from this object file as we go
6096 on through the loop. */
6097 last
= symdef
->file_offset
;
6110 /* Given an ELF BFD, add symbols to the global hash table as
6114 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6116 switch (bfd_get_format (abfd
))
6119 return elf_link_add_object_symbols (abfd
, info
);
6121 return elf_link_add_archive_symbols (abfd
, info
);
6123 bfd_set_error (bfd_error_wrong_format
);
6128 struct hash_codes_info
6130 unsigned long *hashcodes
;
6134 /* This function will be called though elf_link_hash_traverse to store
6135 all hash value of the exported symbols in an array. */
6138 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6140 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
6145 /* Ignore indirect symbols. These are added by the versioning code. */
6146 if (h
->dynindx
== -1)
6149 name
= h
->root
.root
.string
;
6150 if (h
->versioned
>= versioned
)
6152 char *p
= strchr (name
, ELF_VER_CHR
);
6155 alc
= (char *) bfd_malloc (p
- name
+ 1);
6161 memcpy (alc
, name
, p
- name
);
6162 alc
[p
- name
] = '\0';
6167 /* Compute the hash value. */
6168 ha
= bfd_elf_hash (name
);
6170 /* Store the found hash value in the array given as the argument. */
6171 *(inf
->hashcodes
)++ = ha
;
6173 /* And store it in the struct so that we can put it in the hash table
6175 h
->u
.elf_hash_value
= ha
;
6181 struct collect_gnu_hash_codes
6184 const struct elf_backend_data
*bed
;
6185 unsigned long int nsyms
;
6186 unsigned long int maskbits
;
6187 unsigned long int *hashcodes
;
6188 unsigned long int *hashval
;
6189 unsigned long int *indx
;
6190 unsigned long int *counts
;
6194 long int min_dynindx
;
6195 unsigned long int bucketcount
;
6196 unsigned long int symindx
;
6197 long int local_indx
;
6198 long int shift1
, shift2
;
6199 unsigned long int mask
;
6203 /* This function will be called though elf_link_hash_traverse to store
6204 all hash value of the exported symbols in an array. */
6207 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6209 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6214 /* Ignore indirect symbols. These are added by the versioning code. */
6215 if (h
->dynindx
== -1)
6218 /* Ignore also local symbols and undefined symbols. */
6219 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6222 name
= h
->root
.root
.string
;
6223 if (h
->versioned
>= versioned
)
6225 char *p
= strchr (name
, ELF_VER_CHR
);
6228 alc
= (char *) bfd_malloc (p
- name
+ 1);
6234 memcpy (alc
, name
, p
- name
);
6235 alc
[p
- name
] = '\0';
6240 /* Compute the hash value. */
6241 ha
= bfd_elf_gnu_hash (name
);
6243 /* Store the found hash value in the array for compute_bucket_count,
6244 and also for .dynsym reordering purposes. */
6245 s
->hashcodes
[s
->nsyms
] = ha
;
6246 s
->hashval
[h
->dynindx
] = ha
;
6248 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6249 s
->min_dynindx
= h
->dynindx
;
6255 /* This function will be called though elf_link_hash_traverse to do
6256 final dynamic symbol renumbering in case of .gnu.hash.
6257 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6258 to the translation table. */
6261 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6263 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6264 unsigned long int bucket
;
6265 unsigned long int val
;
6267 /* Ignore indirect symbols. */
6268 if (h
->dynindx
== -1)
6271 /* Ignore also local symbols and undefined symbols. */
6272 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6274 if (h
->dynindx
>= s
->min_dynindx
)
6276 if (s
->bed
->record_xhash_symbol
!= NULL
)
6278 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6282 h
->dynindx
= s
->local_indx
++;
6287 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6288 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6289 & ((s
->maskbits
>> s
->shift1
) - 1);
6290 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6292 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6293 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6294 if (s
->counts
[bucket
] == 1)
6295 /* Last element terminates the chain. */
6297 bfd_put_32 (s
->output_bfd
, val
,
6298 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6299 --s
->counts
[bucket
];
6300 if (s
->bed
->record_xhash_symbol
!= NULL
)
6302 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6304 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6307 h
->dynindx
= s
->indx
[bucket
]++;
6311 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6314 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6316 return !(h
->forced_local
6317 || h
->root
.type
== bfd_link_hash_undefined
6318 || h
->root
.type
== bfd_link_hash_undefweak
6319 || ((h
->root
.type
== bfd_link_hash_defined
6320 || h
->root
.type
== bfd_link_hash_defweak
)
6321 && h
->root
.u
.def
.section
->output_section
== NULL
));
6324 /* Array used to determine the number of hash table buckets to use
6325 based on the number of symbols there are. If there are fewer than
6326 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6327 fewer than 37 we use 17 buckets, and so forth. We never use more
6328 than 32771 buckets. */
6330 static const size_t elf_buckets
[] =
6332 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6336 /* Compute bucket count for hashing table. We do not use a static set
6337 of possible tables sizes anymore. Instead we determine for all
6338 possible reasonable sizes of the table the outcome (i.e., the
6339 number of collisions etc) and choose the best solution. The
6340 weighting functions are not too simple to allow the table to grow
6341 without bounds. Instead one of the weighting factors is the size.
6342 Therefore the result is always a good payoff between few collisions
6343 (= short chain lengths) and table size. */
6345 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6346 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6347 unsigned long int nsyms
,
6350 size_t best_size
= 0;
6351 unsigned long int i
;
6353 /* We have a problem here. The following code to optimize the table
6354 size requires an integer type with more the 32 bits. If
6355 BFD_HOST_U_64_BIT is set we know about such a type. */
6356 #ifdef BFD_HOST_U_64_BIT
6361 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
6362 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6363 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6364 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6365 unsigned long int *counts
;
6367 unsigned int no_improvement_count
= 0;
6369 /* Possible optimization parameters: if we have NSYMS symbols we say
6370 that the hashing table must at least have NSYMS/4 and at most
6372 minsize
= nsyms
/ 4;
6375 best_size
= maxsize
= nsyms
* 2;
6380 if ((best_size
& 31) == 0)
6384 /* Create array where we count the collisions in. We must use bfd_malloc
6385 since the size could be large. */
6387 amt
*= sizeof (unsigned long int);
6388 counts
= (unsigned long int *) bfd_malloc (amt
);
6392 /* Compute the "optimal" size for the hash table. The criteria is a
6393 minimal chain length. The minor criteria is (of course) the size
6395 for (i
= minsize
; i
< maxsize
; ++i
)
6397 /* Walk through the array of hashcodes and count the collisions. */
6398 BFD_HOST_U_64_BIT max
;
6399 unsigned long int j
;
6400 unsigned long int fact
;
6402 if (gnu_hash
&& (i
& 31) == 0)
6405 memset (counts
, '\0', i
* sizeof (unsigned long int));
6407 /* Determine how often each hash bucket is used. */
6408 for (j
= 0; j
< nsyms
; ++j
)
6409 ++counts
[hashcodes
[j
] % i
];
6411 /* For the weight function we need some information about the
6412 pagesize on the target. This is information need not be 100%
6413 accurate. Since this information is not available (so far) we
6414 define it here to a reasonable default value. If it is crucial
6415 to have a better value some day simply define this value. */
6416 # ifndef BFD_TARGET_PAGESIZE
6417 # define BFD_TARGET_PAGESIZE (4096)
6420 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6422 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6425 /* Variant 1: optimize for short chains. We add the squares
6426 of all the chain lengths (which favors many small chain
6427 over a few long chains). */
6428 for (j
= 0; j
< i
; ++j
)
6429 max
+= counts
[j
] * counts
[j
];
6431 /* This adds penalties for the overall size of the table. */
6432 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6435 /* Variant 2: Optimize a lot more for small table. Here we
6436 also add squares of the size but we also add penalties for
6437 empty slots (the +1 term). */
6438 for (j
= 0; j
< i
; ++j
)
6439 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6441 /* The overall size of the table is considered, but not as
6442 strong as in variant 1, where it is squared. */
6443 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6447 /* Compare with current best results. */
6448 if (max
< best_chlen
)
6452 no_improvement_count
= 0;
6454 /* PR 11843: Avoid futile long searches for the best bucket size
6455 when there are a large number of symbols. */
6456 else if (++no_improvement_count
== 100)
6463 #endif /* defined (BFD_HOST_U_64_BIT) */
6465 /* This is the fallback solution if no 64bit type is available or if we
6466 are not supposed to spend much time on optimizations. We select the
6467 bucket count using a fixed set of numbers. */
6468 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6470 best_size
= elf_buckets
[i
];
6471 if (nsyms
< elf_buckets
[i
+ 1])
6474 if (gnu_hash
&& best_size
< 2)
6481 /* Size any SHT_GROUP section for ld -r. */
6484 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6489 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6490 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6491 && (s
= ibfd
->sections
) != NULL
6492 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6493 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6498 /* Set a default stack segment size. The value in INFO wins. If it
6499 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6500 undefined it is initialized. */
6503 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6504 struct bfd_link_info
*info
,
6505 const char *legacy_symbol
,
6506 bfd_vma default_size
)
6508 struct elf_link_hash_entry
*h
= NULL
;
6510 /* Look for legacy symbol. */
6512 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6513 false, false, false);
6514 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6515 || h
->root
.type
== bfd_link_hash_defweak
)
6517 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6519 /* The symbol has no type if specified on the command line. */
6520 h
->type
= STT_OBJECT
;
6521 if (info
->stacksize
)
6522 /* xgettext:c-format */
6523 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6524 output_bfd
, legacy_symbol
);
6525 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6526 /* xgettext:c-format */
6527 _bfd_error_handler (_("%pB: %s not absolute"),
6528 output_bfd
, legacy_symbol
);
6530 info
->stacksize
= h
->root
.u
.def
.value
;
6533 if (!info
->stacksize
)
6534 /* If the user didn't set a size, or explicitly inhibit the
6535 size, set it now. */
6536 info
->stacksize
= default_size
;
6538 /* Provide the legacy symbol, if it is referenced. */
6539 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6540 || h
->root
.type
== bfd_link_hash_undefweak
))
6542 struct bfd_link_hash_entry
*bh
= NULL
;
6544 if (!(_bfd_generic_link_add_one_symbol
6545 (info
, output_bfd
, legacy_symbol
,
6546 BSF_GLOBAL
, bfd_abs_section_ptr
,
6547 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6548 NULL
, false, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6551 h
= (struct elf_link_hash_entry
*) bh
;
6553 h
->type
= STT_OBJECT
;
6559 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6561 struct elf_gc_sweep_symbol_info
6563 struct bfd_link_info
*info
;
6564 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6569 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6572 && (((h
->root
.type
== bfd_link_hash_defined
6573 || h
->root
.type
== bfd_link_hash_defweak
)
6574 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6575 && h
->root
.u
.def
.section
->gc_mark
))
6576 || h
->root
.type
== bfd_link_hash_undefined
6577 || h
->root
.type
== bfd_link_hash_undefweak
))
6579 struct elf_gc_sweep_symbol_info
*inf
;
6581 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6582 (*inf
->hide_symbol
) (inf
->info
, h
, true);
6585 h
->ref_regular_nonweak
= 0;
6591 /* Set up the sizes and contents of the ELF dynamic sections. This is
6592 called by the ELF linker emulation before_allocation routine. We
6593 must set the sizes of the sections before the linker sets the
6594 addresses of the various sections. */
6597 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6600 const char *filter_shlib
,
6602 const char *depaudit
,
6603 const char * const *auxiliary_filters
,
6604 struct bfd_link_info
*info
,
6605 asection
**sinterpptr
)
6608 const struct elf_backend_data
*bed
;
6612 if (!is_elf_hash_table (info
->hash
))
6615 /* Any syms created from now on start with -1 in
6616 got.refcount/offset and plt.refcount/offset. */
6617 elf_hash_table (info
)->init_got_refcount
6618 = elf_hash_table (info
)->init_got_offset
;
6619 elf_hash_table (info
)->init_plt_refcount
6620 = elf_hash_table (info
)->init_plt_offset
;
6622 bed
= get_elf_backend_data (output_bfd
);
6624 /* The backend may have to create some sections regardless of whether
6625 we're dynamic or not. */
6626 if (bed
->elf_backend_always_size_sections
6627 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6630 dynobj
= elf_hash_table (info
)->dynobj
;
6632 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6634 struct bfd_elf_version_tree
*verdefs
;
6635 struct elf_info_failed asvinfo
;
6636 struct bfd_elf_version_tree
*t
;
6637 struct bfd_elf_version_expr
*d
;
6641 /* If we are supposed to export all symbols into the dynamic symbol
6642 table (this is not the normal case), then do so. */
6643 if (info
->export_dynamic
6644 || (bfd_link_executable (info
) && info
->dynamic
))
6646 struct elf_info_failed eif
;
6650 elf_link_hash_traverse (elf_hash_table (info
),
6651 _bfd_elf_export_symbol
,
6659 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6661 if (soname_indx
== (size_t) -1
6662 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6666 soname_indx
= (size_t) -1;
6668 /* Make all global versions with definition. */
6669 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6670 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6671 if (!d
->symver
&& d
->literal
)
6673 const char *verstr
, *name
;
6674 size_t namelen
, verlen
, newlen
;
6675 char *newname
, *p
, leading_char
;
6676 struct elf_link_hash_entry
*newh
;
6678 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6680 namelen
= strlen (name
) + (leading_char
!= '\0');
6682 verlen
= strlen (verstr
);
6683 newlen
= namelen
+ verlen
+ 3;
6685 newname
= (char *) bfd_malloc (newlen
);
6686 if (newname
== NULL
)
6688 newname
[0] = leading_char
;
6689 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6691 /* Check the hidden versioned definition. */
6692 p
= newname
+ namelen
;
6694 memcpy (p
, verstr
, verlen
+ 1);
6695 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6696 newname
, false, false,
6699 || (newh
->root
.type
!= bfd_link_hash_defined
6700 && newh
->root
.type
!= bfd_link_hash_defweak
))
6702 /* Check the default versioned definition. */
6704 memcpy (p
, verstr
, verlen
+ 1);
6705 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6706 newname
, false, false,
6711 /* Mark this version if there is a definition and it is
6712 not defined in a shared object. */
6714 && !newh
->def_dynamic
6715 && (newh
->root
.type
== bfd_link_hash_defined
6716 || newh
->root
.type
== bfd_link_hash_defweak
))
6720 /* Attach all the symbols to their version information. */
6721 asvinfo
.info
= info
;
6722 asvinfo
.failed
= false;
6724 elf_link_hash_traverse (elf_hash_table (info
),
6725 _bfd_elf_link_assign_sym_version
,
6730 if (!info
->allow_undefined_version
)
6732 /* Check if all global versions have a definition. */
6733 bool all_defined
= true;
6734 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6735 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6736 if (d
->literal
&& !d
->symver
&& !d
->script
)
6739 (_("%s: undefined version: %s"),
6740 d
->pattern
, t
->name
);
6741 all_defined
= false;
6746 bfd_set_error (bfd_error_bad_value
);
6751 /* Set up the version definition section. */
6752 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6753 BFD_ASSERT (s
!= NULL
);
6755 /* We may have created additional version definitions if we are
6756 just linking a regular application. */
6757 verdefs
= info
->version_info
;
6759 /* Skip anonymous version tag. */
6760 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6761 verdefs
= verdefs
->next
;
6763 if (verdefs
== NULL
&& !info
->create_default_symver
)
6764 s
->flags
|= SEC_EXCLUDE
;
6770 Elf_Internal_Verdef def
;
6771 Elf_Internal_Verdaux defaux
;
6772 struct bfd_link_hash_entry
*bh
;
6773 struct elf_link_hash_entry
*h
;
6779 /* Make space for the base version. */
6780 size
+= sizeof (Elf_External_Verdef
);
6781 size
+= sizeof (Elf_External_Verdaux
);
6784 /* Make space for the default version. */
6785 if (info
->create_default_symver
)
6787 size
+= sizeof (Elf_External_Verdef
);
6791 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6793 struct bfd_elf_version_deps
*n
;
6795 /* Don't emit base version twice. */
6799 size
+= sizeof (Elf_External_Verdef
);
6800 size
+= sizeof (Elf_External_Verdaux
);
6803 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6804 size
+= sizeof (Elf_External_Verdaux
);
6808 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6809 if (s
->contents
== NULL
&& s
->size
!= 0)
6812 /* Fill in the version definition section. */
6816 def
.vd_version
= VER_DEF_CURRENT
;
6817 def
.vd_flags
= VER_FLG_BASE
;
6820 if (info
->create_default_symver
)
6822 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6823 def
.vd_next
= sizeof (Elf_External_Verdef
);
6827 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6828 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6829 + sizeof (Elf_External_Verdaux
));
6832 if (soname_indx
!= (size_t) -1)
6834 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6836 def
.vd_hash
= bfd_elf_hash (soname
);
6837 defaux
.vda_name
= soname_indx
;
6844 name
= lbasename (bfd_get_filename (output_bfd
));
6845 def
.vd_hash
= bfd_elf_hash (name
);
6846 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6848 if (indx
== (size_t) -1)
6850 defaux
.vda_name
= indx
;
6852 defaux
.vda_next
= 0;
6854 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6855 (Elf_External_Verdef
*) p
);
6856 p
+= sizeof (Elf_External_Verdef
);
6857 if (info
->create_default_symver
)
6859 /* Add a symbol representing this version. */
6861 if (! (_bfd_generic_link_add_one_symbol
6862 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6864 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6866 h
= (struct elf_link_hash_entry
*) bh
;
6869 h
->type
= STT_OBJECT
;
6870 h
->verinfo
.vertree
= NULL
;
6872 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6875 /* Create a duplicate of the base version with the same
6876 aux block, but different flags. */
6879 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6881 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6882 + sizeof (Elf_External_Verdaux
));
6885 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6886 (Elf_External_Verdef
*) p
);
6887 p
+= sizeof (Elf_External_Verdef
);
6889 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6890 (Elf_External_Verdaux
*) p
);
6891 p
+= sizeof (Elf_External_Verdaux
);
6893 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6896 struct bfd_elf_version_deps
*n
;
6898 /* Don't emit the base version twice. */
6903 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6906 /* Add a symbol representing this version. */
6908 if (! (_bfd_generic_link_add_one_symbol
6909 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6911 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6913 h
= (struct elf_link_hash_entry
*) bh
;
6916 h
->type
= STT_OBJECT
;
6917 h
->verinfo
.vertree
= t
;
6919 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6922 def
.vd_version
= VER_DEF_CURRENT
;
6924 if (t
->globals
.list
== NULL
6925 && t
->locals
.list
== NULL
6927 def
.vd_flags
|= VER_FLG_WEAK
;
6928 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6929 def
.vd_cnt
= cdeps
+ 1;
6930 def
.vd_hash
= bfd_elf_hash (t
->name
);
6931 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6934 /* If a basever node is next, it *must* be the last node in
6935 the chain, otherwise Verdef construction breaks. */
6936 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6937 BFD_ASSERT (t
->next
->next
== NULL
);
6939 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6940 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6941 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6943 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6944 (Elf_External_Verdef
*) p
);
6945 p
+= sizeof (Elf_External_Verdef
);
6947 defaux
.vda_name
= h
->dynstr_index
;
6948 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6950 defaux
.vda_next
= 0;
6951 if (t
->deps
!= NULL
)
6952 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6953 t
->name_indx
= defaux
.vda_name
;
6955 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6956 (Elf_External_Verdaux
*) p
);
6957 p
+= sizeof (Elf_External_Verdaux
);
6959 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6961 if (n
->version_needed
== NULL
)
6963 /* This can happen if there was an error in the
6965 defaux
.vda_name
= 0;
6969 defaux
.vda_name
= n
->version_needed
->name_indx
;
6970 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6973 if (n
->next
== NULL
)
6974 defaux
.vda_next
= 0;
6976 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6978 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6979 (Elf_External_Verdaux
*) p
);
6980 p
+= sizeof (Elf_External_Verdaux
);
6984 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6988 if (info
->gc_sections
&& bed
->can_gc_sections
)
6990 struct elf_gc_sweep_symbol_info sweep_info
;
6992 /* Remove the symbols that were in the swept sections from the
6993 dynamic symbol table. */
6994 sweep_info
.info
= info
;
6995 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6996 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
7000 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7003 struct elf_find_verdep_info sinfo
;
7005 /* Work out the size of the version reference section. */
7007 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
7008 BFD_ASSERT (s
!= NULL
);
7011 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
7012 if (sinfo
.vers
== 0)
7014 sinfo
.failed
= false;
7016 elf_link_hash_traverse (elf_hash_table (info
),
7017 _bfd_elf_link_find_version_dependencies
,
7022 if (info
->enable_dt_relr
)
7024 elf_link_add_dt_relr_dependency (&sinfo
);
7029 if (elf_tdata (output_bfd
)->verref
== NULL
)
7030 s
->flags
|= SEC_EXCLUDE
;
7033 Elf_Internal_Verneed
*vn
;
7038 /* Build the version dependency section. */
7041 for (vn
= elf_tdata (output_bfd
)->verref
;
7043 vn
= vn
->vn_nextref
)
7045 Elf_Internal_Vernaux
*a
;
7047 size
+= sizeof (Elf_External_Verneed
);
7049 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7050 size
+= sizeof (Elf_External_Vernaux
);
7054 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7055 if (s
->contents
== NULL
)
7059 for (vn
= elf_tdata (output_bfd
)->verref
;
7061 vn
= vn
->vn_nextref
)
7064 Elf_Internal_Vernaux
*a
;
7068 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7071 vn
->vn_version
= VER_NEED_CURRENT
;
7073 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7074 elf_dt_name (vn
->vn_bfd
) != NULL
7075 ? elf_dt_name (vn
->vn_bfd
)
7076 : lbasename (bfd_get_filename
7079 if (indx
== (size_t) -1)
7082 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
7083 if (vn
->vn_nextref
== NULL
)
7086 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
7087 + caux
* sizeof (Elf_External_Vernaux
));
7089 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
7090 (Elf_External_Verneed
*) p
);
7091 p
+= sizeof (Elf_External_Verneed
);
7093 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7095 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
7096 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7097 a
->vna_nodename
, false);
7098 if (indx
== (size_t) -1)
7101 if (a
->vna_nextptr
== NULL
)
7104 a
->vna_next
= sizeof (Elf_External_Vernaux
);
7106 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
7107 (Elf_External_Vernaux
*) p
);
7108 p
+= sizeof (Elf_External_Vernaux
);
7112 elf_tdata (output_bfd
)->cverrefs
= crefs
;
7116 if (bfd_link_relocatable (info
)
7117 && !_bfd_elf_size_group_sections (info
))
7120 /* Determine any GNU_STACK segment requirements, after the backend
7121 has had a chance to set a default segment size. */
7122 if (info
->execstack
)
7123 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
7124 else if (info
->noexecstack
)
7125 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
7129 asection
*notesec
= NULL
;
7132 for (inputobj
= info
->input_bfds
;
7134 inputobj
= inputobj
->link
.next
)
7139 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
7141 s
= inputobj
->sections
;
7142 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
7145 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
7148 if (s
->flags
& SEC_CODE
)
7152 else if (bed
->default_execstack
)
7155 if (notesec
|| info
->stacksize
> 0)
7156 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
7157 if (notesec
&& exec
&& bfd_link_relocatable (info
)
7158 && notesec
->output_section
!= bfd_abs_section_ptr
)
7159 notesec
->output_section
->flags
|= SEC_CODE
;
7162 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7164 struct elf_info_failed eif
;
7165 struct elf_link_hash_entry
*h
;
7169 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7170 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7174 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7176 info
->flags
|= DF_SYMBOLIC
;
7184 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7186 if (indx
== (size_t) -1)
7189 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7190 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7194 if (filter_shlib
!= NULL
)
7198 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7199 filter_shlib
, true);
7200 if (indx
== (size_t) -1
7201 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7205 if (auxiliary_filters
!= NULL
)
7207 const char * const *p
;
7209 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7213 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7215 if (indx
== (size_t) -1
7216 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7225 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7227 if (indx
== (size_t) -1
7228 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7232 if (depaudit
!= NULL
)
7236 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7238 if (indx
== (size_t) -1
7239 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7246 /* Find all symbols which were defined in a dynamic object and make
7247 the backend pick a reasonable value for them. */
7248 elf_link_hash_traverse (elf_hash_table (info
),
7249 _bfd_elf_adjust_dynamic_symbol
,
7254 /* Add some entries to the .dynamic section. We fill in some of the
7255 values later, in bfd_elf_final_link, but we must add the entries
7256 now so that we know the final size of the .dynamic section. */
7258 /* If there are initialization and/or finalization functions to
7259 call then add the corresponding DT_INIT/DT_FINI entries. */
7260 h
= (info
->init_function
7261 ? elf_link_hash_lookup (elf_hash_table (info
),
7262 info
->init_function
, false,
7269 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7272 h
= (info
->fini_function
7273 ? elf_link_hash_lookup (elf_hash_table (info
),
7274 info
->fini_function
, false,
7281 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7285 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7286 if (s
!= NULL
&& s
->linker_has_input
)
7288 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7289 if (! bfd_link_executable (info
))
7294 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7295 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7296 && (o
= sub
->sections
) != NULL
7297 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7298 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7299 if (elf_section_data (o
)->this_hdr
.sh_type
7300 == SHT_PREINIT_ARRAY
)
7303 (_("%pB: .preinit_array section is not allowed in DSO"),
7308 bfd_set_error (bfd_error_nonrepresentable_section
);
7312 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7313 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7316 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7317 if (s
!= NULL
&& s
->linker_has_input
)
7319 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7320 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7323 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7324 if (s
!= NULL
&& s
->linker_has_input
)
7326 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7327 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7331 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7332 /* If .dynstr is excluded from the link, we don't want any of
7333 these tags. Strictly, we should be checking each section
7334 individually; This quick check covers for the case where
7335 someone does a /DISCARD/ : { *(*) }. */
7336 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7338 bfd_size_type strsize
;
7340 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7341 if ((info
->emit_hash
7342 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7343 || (info
->emit_gnu_hash
7344 && (bed
->record_xhash_symbol
== NULL
7345 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7346 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7347 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7348 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7349 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7351 || (info
->gnu_flags_1
7352 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_FLAGS_1
,
7353 info
->gnu_flags_1
)))
7358 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7361 /* The backend must work out the sizes of all the other dynamic
7364 && bed
->elf_backend_size_dynamic_sections
!= NULL
7365 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7368 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7370 if (elf_tdata (output_bfd
)->cverdefs
)
7372 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7374 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7375 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7379 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7381 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7384 else if (info
->flags
& DF_BIND_NOW
)
7386 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7392 if (bfd_link_executable (info
))
7393 info
->flags_1
&= ~ (DF_1_INITFIRST
7396 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7400 if (elf_tdata (output_bfd
)->cverrefs
)
7402 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7404 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7405 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7409 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7410 && elf_tdata (output_bfd
)->cverdefs
== 0)
7411 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7415 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7416 s
->flags
|= SEC_EXCLUDE
;
7422 /* Find the first non-excluded output section. We'll use its
7423 section symbol for some emitted relocs. */
7425 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7428 asection
*found
= NULL
;
7430 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7431 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7432 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7435 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7438 elf_hash_table (info
)->text_index_section
= found
;
7441 /* Find two non-excluded output sections, one for code, one for data.
7442 We'll use their section symbols for some emitted relocs. */
7444 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7447 asection
*found
= NULL
;
7449 /* Data first, since setting text_index_section changes
7450 _bfd_elf_omit_section_dynsym_default. */
7451 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7452 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7453 && !(s
->flags
& SEC_READONLY
)
7454 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7457 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7460 elf_hash_table (info
)->data_index_section
= found
;
7462 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7463 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7464 && (s
->flags
& SEC_READONLY
)
7465 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7470 elf_hash_table (info
)->text_index_section
= found
;
7473 #define GNU_HASH_SECTION_NAME(bed) \
7474 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7477 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7479 const struct elf_backend_data
*bed
;
7480 unsigned long section_sym_count
;
7481 bfd_size_type dynsymcount
= 0;
7483 if (!is_elf_hash_table (info
->hash
))
7486 bed
= get_elf_backend_data (output_bfd
);
7487 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7489 /* Assign dynsym indices. In a shared library we generate a section
7490 symbol for each output section, which come first. Next come all
7491 of the back-end allocated local dynamic syms, followed by the rest
7492 of the global symbols.
7494 This is usually not needed for static binaries, however backends
7495 can request to always do it, e.g. the MIPS backend uses dynamic
7496 symbol counts to lay out GOT, which will be produced in the
7497 presence of GOT relocations even in static binaries (holding fixed
7498 data in that case, to satisfy those relocations). */
7500 if (elf_hash_table (info
)->dynamic_sections_created
7501 || bed
->always_renumber_dynsyms
)
7502 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7503 §ion_sym_count
);
7505 if (elf_hash_table (info
)->dynamic_sections_created
)
7509 unsigned int dtagcount
;
7511 dynobj
= elf_hash_table (info
)->dynobj
;
7513 /* Work out the size of the symbol version section. */
7514 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7515 BFD_ASSERT (s
!= NULL
);
7516 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7518 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7519 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7520 if (s
->contents
== NULL
)
7523 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7527 /* Set the size of the .dynsym and .hash sections. We counted
7528 the number of dynamic symbols in elf_link_add_object_symbols.
7529 We will build the contents of .dynsym and .hash when we build
7530 the final symbol table, because until then we do not know the
7531 correct value to give the symbols. We built the .dynstr
7532 section as we went along in elf_link_add_object_symbols. */
7533 s
= elf_hash_table (info
)->dynsym
;
7534 BFD_ASSERT (s
!= NULL
);
7535 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7537 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7538 if (s
->contents
== NULL
)
7541 /* The first entry in .dynsym is a dummy symbol. Clear all the
7542 section syms, in case we don't output them all. */
7543 ++section_sym_count
;
7544 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7546 elf_hash_table (info
)->bucketcount
= 0;
7548 /* Compute the size of the hashing table. As a side effect this
7549 computes the hash values for all the names we export. */
7550 if (info
->emit_hash
)
7552 unsigned long int *hashcodes
;
7553 struct hash_codes_info hashinf
;
7555 unsigned long int nsyms
;
7557 size_t hash_entry_size
;
7559 /* Compute the hash values for all exported symbols. At the same
7560 time store the values in an array so that we could use them for
7562 amt
= dynsymcount
* sizeof (unsigned long int);
7563 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7564 if (hashcodes
== NULL
)
7566 hashinf
.hashcodes
= hashcodes
;
7567 hashinf
.error
= false;
7569 /* Put all hash values in HASHCODES. */
7570 elf_link_hash_traverse (elf_hash_table (info
),
7571 elf_collect_hash_codes
, &hashinf
);
7578 nsyms
= hashinf
.hashcodes
- hashcodes
;
7580 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7583 if (bucketcount
== 0 && nsyms
> 0)
7586 elf_hash_table (info
)->bucketcount
= bucketcount
;
7588 s
= bfd_get_linker_section (dynobj
, ".hash");
7589 BFD_ASSERT (s
!= NULL
);
7590 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7591 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7592 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7593 if (s
->contents
== NULL
)
7596 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7597 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7598 s
->contents
+ hash_entry_size
);
7601 if (info
->emit_gnu_hash
)
7604 unsigned char *contents
;
7605 struct collect_gnu_hash_codes cinfo
;
7609 memset (&cinfo
, 0, sizeof (cinfo
));
7611 /* Compute the hash values for all exported symbols. At the same
7612 time store the values in an array so that we could use them for
7614 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7615 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7616 if (cinfo
.hashcodes
== NULL
)
7619 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7620 cinfo
.min_dynindx
= -1;
7621 cinfo
.output_bfd
= output_bfd
;
7624 /* Put all hash values in HASHCODES. */
7625 elf_link_hash_traverse (elf_hash_table (info
),
7626 elf_collect_gnu_hash_codes
, &cinfo
);
7629 free (cinfo
.hashcodes
);
7634 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7636 if (bucketcount
== 0)
7638 free (cinfo
.hashcodes
);
7642 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7643 BFD_ASSERT (s
!= NULL
);
7645 if (cinfo
.nsyms
== 0)
7647 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7648 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7649 free (cinfo
.hashcodes
);
7650 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7651 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7652 if (contents
== NULL
)
7654 s
->contents
= contents
;
7655 /* 1 empty bucket. */
7656 bfd_put_32 (output_bfd
, 1, contents
);
7657 /* SYMIDX above the special symbol 0. */
7658 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7659 /* Just one word for bitmask. */
7660 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7661 /* Only hash fn bloom filter. */
7662 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7663 /* No hashes are valid - empty bitmask. */
7664 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7665 /* No hashes in the only bucket. */
7666 bfd_put_32 (output_bfd
, 0,
7667 contents
+ 16 + bed
->s
->arch_size
/ 8);
7671 unsigned long int maskwords
, maskbitslog2
, x
;
7672 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7676 while ((x
>>= 1) != 0)
7678 if (maskbitslog2
< 3)
7680 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7681 maskbitslog2
= maskbitslog2
+ 3;
7683 maskbitslog2
= maskbitslog2
+ 2;
7684 if (bed
->s
->arch_size
== 64)
7686 if (maskbitslog2
== 5)
7692 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7693 cinfo
.shift2
= maskbitslog2
;
7694 cinfo
.maskbits
= 1 << maskbitslog2
;
7695 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7696 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7697 amt
+= maskwords
* sizeof (bfd_vma
);
7698 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7699 if (cinfo
.bitmask
== NULL
)
7701 free (cinfo
.hashcodes
);
7705 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7706 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7707 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7708 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7710 /* Determine how often each hash bucket is used. */
7711 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7712 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7713 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7715 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7716 if (cinfo
.counts
[i
] != 0)
7718 cinfo
.indx
[i
] = cnt
;
7719 cnt
+= cinfo
.counts
[i
];
7721 BFD_ASSERT (cnt
== dynsymcount
);
7722 cinfo
.bucketcount
= bucketcount
;
7723 cinfo
.local_indx
= cinfo
.min_dynindx
;
7725 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7726 s
->size
+= cinfo
.maskbits
/ 8;
7727 if (bed
->record_xhash_symbol
!= NULL
)
7728 s
->size
+= cinfo
.nsyms
* 4;
7729 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7730 if (contents
== NULL
)
7732 free (cinfo
.bitmask
);
7733 free (cinfo
.hashcodes
);
7737 s
->contents
= contents
;
7738 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7739 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7740 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7741 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7742 contents
+= 16 + cinfo
.maskbits
/ 8;
7744 for (i
= 0; i
< bucketcount
; ++i
)
7746 if (cinfo
.counts
[i
] == 0)
7747 bfd_put_32 (output_bfd
, 0, contents
);
7749 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7753 cinfo
.contents
= contents
;
7755 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7756 /* Renumber dynamic symbols, if populating .gnu.hash section.
7757 If using .MIPS.xhash, populate the translation table. */
7758 elf_link_hash_traverse (elf_hash_table (info
),
7759 elf_gnu_hash_process_symidx
, &cinfo
);
7761 contents
= s
->contents
+ 16;
7762 for (i
= 0; i
< maskwords
; ++i
)
7764 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7766 contents
+= bed
->s
->arch_size
/ 8;
7769 free (cinfo
.bitmask
);
7770 free (cinfo
.hashcodes
);
7774 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7775 BFD_ASSERT (s
!= NULL
);
7777 elf_finalize_dynstr (output_bfd
, info
);
7779 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7781 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7782 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7789 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7792 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7795 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7796 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7799 /* Finish SHF_MERGE section merging. */
7802 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7807 if (!is_elf_hash_table (info
->hash
))
7810 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7811 if ((ibfd
->flags
& DYNAMIC
) == 0
7812 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7813 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7814 == get_elf_backend_data (obfd
)->s
->elfclass
))
7815 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7816 if ((sec
->flags
& SEC_MERGE
) != 0
7817 && !bfd_is_abs_section (sec
->output_section
))
7819 struct bfd_elf_section_data
*secdata
;
7821 secdata
= elf_section_data (sec
);
7822 if (! _bfd_add_merge_section (obfd
,
7823 &elf_hash_table (info
)->merge_info
,
7824 sec
, &secdata
->sec_info
))
7826 else if (secdata
->sec_info
)
7827 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7830 if (elf_hash_table (info
)->merge_info
!= NULL
)
7831 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7832 merge_sections_remove_hook
);
7836 /* Create an entry in an ELF linker hash table. */
7838 struct bfd_hash_entry
*
7839 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7840 struct bfd_hash_table
*table
,
7843 /* Allocate the structure if it has not already been allocated by a
7847 entry
= (struct bfd_hash_entry
*)
7848 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7853 /* Call the allocation method of the superclass. */
7854 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7857 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7858 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7860 /* Set local fields. */
7863 ret
->got
= htab
->init_got_refcount
;
7864 ret
->plt
= htab
->init_plt_refcount
;
7865 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7866 - offsetof (struct elf_link_hash_entry
, size
)));
7867 /* Assume that we have been called by a non-ELF symbol reader.
7868 This flag is then reset by the code which reads an ELF input
7869 file. This ensures that a symbol created by a non-ELF symbol
7870 reader will have the flag set correctly. */
7877 /* Copy data from an indirect symbol to its direct symbol, hiding the
7878 old indirect symbol. Also used for copying flags to a weakdef. */
7881 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7882 struct elf_link_hash_entry
*dir
,
7883 struct elf_link_hash_entry
*ind
)
7885 struct elf_link_hash_table
*htab
;
7887 if (ind
->dyn_relocs
!= NULL
)
7889 if (dir
->dyn_relocs
!= NULL
)
7891 struct elf_dyn_relocs
**pp
;
7892 struct elf_dyn_relocs
*p
;
7894 /* Add reloc counts against the indirect sym to the direct sym
7895 list. Merge any entries against the same section. */
7896 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
7898 struct elf_dyn_relocs
*q
;
7900 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
7901 if (q
->sec
== p
->sec
)
7903 q
->pc_count
+= p
->pc_count
;
7904 q
->count
+= p
->count
;
7911 *pp
= dir
->dyn_relocs
;
7914 dir
->dyn_relocs
= ind
->dyn_relocs
;
7915 ind
->dyn_relocs
= NULL
;
7918 /* Copy down any references that we may have already seen to the
7919 symbol which just became indirect. */
7921 if (dir
->versioned
!= versioned_hidden
)
7922 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7923 dir
->ref_regular
|= ind
->ref_regular
;
7924 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7925 dir
->non_got_ref
|= ind
->non_got_ref
;
7926 dir
->needs_plt
|= ind
->needs_plt
;
7927 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7929 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7932 /* Copy over the global and procedure linkage table refcount entries.
7933 These may have been already set up by a check_relocs routine. */
7934 htab
= elf_hash_table (info
);
7935 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7937 if (dir
->got
.refcount
< 0)
7938 dir
->got
.refcount
= 0;
7939 dir
->got
.refcount
+= ind
->got
.refcount
;
7940 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7943 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7945 if (dir
->plt
.refcount
< 0)
7946 dir
->plt
.refcount
= 0;
7947 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7948 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7951 if (ind
->dynindx
!= -1)
7953 if (dir
->dynindx
!= -1)
7954 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7955 dir
->dynindx
= ind
->dynindx
;
7956 dir
->dynstr_index
= ind
->dynstr_index
;
7958 ind
->dynstr_index
= 0;
7963 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7964 struct elf_link_hash_entry
*h
,
7967 /* STT_GNU_IFUNC symbol must go through PLT. */
7968 if (h
->type
!= STT_GNU_IFUNC
)
7970 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7975 h
->forced_local
= 1;
7976 if (h
->dynindx
!= -1)
7978 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7981 h
->dynstr_index
= 0;
7986 /* Hide a symbol. */
7989 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
7990 struct bfd_link_info
*info
,
7991 struct bfd_link_hash_entry
*h
)
7993 if (is_elf_hash_table (info
->hash
))
7995 const struct elf_backend_data
*bed
7996 = get_elf_backend_data (output_bfd
);
7997 struct elf_link_hash_entry
*eh
7998 = (struct elf_link_hash_entry
*) h
;
7999 bed
->elf_backend_hide_symbol (info
, eh
, true);
8000 eh
->def_dynamic
= 0;
8001 eh
->ref_dynamic
= 0;
8002 eh
->dynamic_def
= 0;
8006 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8010 _bfd_elf_link_hash_table_init
8011 (struct elf_link_hash_table
*table
,
8013 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
8014 struct bfd_hash_table
*,
8016 unsigned int entsize
,
8017 enum elf_target_id target_id
)
8020 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
8022 table
->init_got_refcount
.refcount
= can_refcount
- 1;
8023 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
8024 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
8025 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
8026 /* The first dynamic symbol is a dummy. */
8027 table
->dynsymcount
= 1;
8029 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
8031 table
->root
.type
= bfd_link_elf_hash_table
;
8032 table
->hash_table_id
= target_id
;
8033 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
8038 /* Create an ELF linker hash table. */
8040 struct bfd_link_hash_table
*
8041 _bfd_elf_link_hash_table_create (bfd
*abfd
)
8043 struct elf_link_hash_table
*ret
;
8044 size_t amt
= sizeof (struct elf_link_hash_table
);
8046 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
8050 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
8051 sizeof (struct elf_link_hash_entry
),
8057 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
8062 /* Destroy an ELF linker hash table. */
8065 _bfd_elf_link_hash_table_free (bfd
*obfd
)
8067 struct elf_link_hash_table
*htab
;
8069 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
8070 if (htab
->dynstr
!= NULL
)
8071 _bfd_elf_strtab_free (htab
->dynstr
);
8072 _bfd_merge_sections_free (htab
->merge_info
);
8073 _bfd_generic_link_hash_table_free (obfd
);
8076 /* This is a hook for the ELF emulation code in the generic linker to
8077 tell the backend linker what file name to use for the DT_NEEDED
8078 entry for a dynamic object. */
8081 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
8083 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8084 && bfd_get_format (abfd
) == bfd_object
)
8085 elf_dt_name (abfd
) = name
;
8089 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
8092 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8093 && bfd_get_format (abfd
) == bfd_object
)
8094 lib_class
= elf_dyn_lib_class (abfd
);
8101 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
8103 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8104 && bfd_get_format (abfd
) == bfd_object
)
8105 elf_dyn_lib_class (abfd
) = lib_class
;
8108 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8109 the linker ELF emulation code. */
8111 struct bfd_link_needed_list
*
8112 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8113 struct bfd_link_info
*info
)
8115 if (! is_elf_hash_table (info
->hash
))
8117 return elf_hash_table (info
)->needed
;
8120 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8121 hook for the linker ELF emulation code. */
8123 struct bfd_link_needed_list
*
8124 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8125 struct bfd_link_info
*info
)
8127 if (! is_elf_hash_table (info
->hash
))
8129 return elf_hash_table (info
)->runpath
;
8132 /* Get the name actually used for a dynamic object for a link. This
8133 is the SONAME entry if there is one. Otherwise, it is the string
8134 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8137 bfd_elf_get_dt_soname (bfd
*abfd
)
8139 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8140 && bfd_get_format (abfd
) == bfd_object
)
8141 return elf_dt_name (abfd
);
8145 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8146 the ELF linker emulation code. */
8149 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
8150 struct bfd_link_needed_list
**pneeded
)
8153 bfd_byte
*dynbuf
= NULL
;
8154 unsigned int elfsec
;
8155 unsigned long shlink
;
8156 bfd_byte
*extdyn
, *extdynend
;
8158 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
8162 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
8163 || bfd_get_format (abfd
) != bfd_object
)
8166 s
= bfd_get_section_by_name (abfd
, ".dynamic");
8167 if (s
== NULL
|| s
->size
== 0)
8170 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
8173 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8174 if (elfsec
== SHN_BAD
)
8177 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8179 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8180 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8183 extdynend
= extdyn
+ s
->size
;
8184 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
8186 Elf_Internal_Dyn dyn
;
8188 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8190 if (dyn
.d_tag
== DT_NULL
)
8193 if (dyn
.d_tag
== DT_NEEDED
)
8196 struct bfd_link_needed_list
*l
;
8197 unsigned int tagv
= dyn
.d_un
.d_val
;
8200 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8205 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8225 struct elf_symbuf_symbol
8227 unsigned long st_name
; /* Symbol name, index in string tbl */
8228 unsigned char st_info
; /* Type and binding attributes */
8229 unsigned char st_other
; /* Visibilty, and target specific */
8232 struct elf_symbuf_head
8234 struct elf_symbuf_symbol
*ssym
;
8236 unsigned int st_shndx
;
8243 Elf_Internal_Sym
*isym
;
8244 struct elf_symbuf_symbol
*ssym
;
8250 /* Sort references to symbols by ascending section number. */
8253 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8255 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8256 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8258 if (s1
->st_shndx
!= s2
->st_shndx
)
8259 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8260 /* Final sort by the address of the sym in the symbuf ensures
8263 return s1
> s2
? 1 : -1;
8268 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8270 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8271 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8272 int ret
= strcmp (s1
->name
, s2
->name
);
8275 if (s1
->u
.p
!= s2
->u
.p
)
8276 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8280 static struct elf_symbuf_head
*
8281 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8283 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8284 struct elf_symbuf_symbol
*ssym
;
8285 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8286 size_t i
, shndx_count
, total_size
, amt
;
8288 amt
= symcount
* sizeof (*indbuf
);
8289 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8293 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8294 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8295 *ind
++ = &isymbuf
[i
];
8298 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8299 elf_sort_elf_symbol
);
8302 if (indbufend
> indbuf
)
8303 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8304 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8307 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8308 + (indbufend
- indbuf
) * sizeof (*ssym
));
8309 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8310 if (ssymbuf
== NULL
)
8316 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8317 ssymbuf
->ssym
= NULL
;
8318 ssymbuf
->count
= shndx_count
;
8319 ssymbuf
->st_shndx
= 0;
8320 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8322 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8325 ssymhead
->ssym
= ssym
;
8326 ssymhead
->count
= 0;
8327 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8329 ssym
->st_name
= (*ind
)->st_name
;
8330 ssym
->st_info
= (*ind
)->st_info
;
8331 ssym
->st_other
= (*ind
)->st_other
;
8334 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8335 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
8342 /* Check if 2 sections define the same set of local and global
8346 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8347 struct bfd_link_info
*info
)
8350 const struct elf_backend_data
*bed1
, *bed2
;
8351 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8352 size_t symcount1
, symcount2
;
8353 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8354 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8355 Elf_Internal_Sym
*isym
, *isymend
;
8356 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8357 size_t count1
, count2
, sec_count1
, sec_count2
, i
;
8358 unsigned int shndx1
, shndx2
;
8360 bool ignore_section_symbol_p
;
8365 /* Both sections have to be in ELF. */
8366 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8367 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8370 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8373 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8374 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8375 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8378 bed1
= get_elf_backend_data (bfd1
);
8379 bed2
= get_elf_backend_data (bfd2
);
8380 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8381 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8382 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8383 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8385 if (symcount1
== 0 || symcount2
== 0)
8391 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8392 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8394 /* Ignore section symbols only when matching non-debugging sections
8395 or linkonce section with comdat section. */
8396 ignore_section_symbol_p
8397 = ((sec1
->flags
& SEC_DEBUGGING
) == 0
8398 || ((elf_section_flags (sec1
) & SHF_GROUP
)
8399 != (elf_section_flags (sec2
) & SHF_GROUP
)));
8401 if (ssymbuf1
== NULL
)
8403 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8405 if (isymbuf1
== NULL
)
8408 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8410 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8411 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8415 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8417 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8419 if (isymbuf2
== NULL
)
8422 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8424 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8425 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8429 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8431 /* Optimized faster version. */
8433 struct elf_symbol
*symp
;
8434 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8437 hi
= ssymbuf1
->count
;
8443 mid
= (lo
+ hi
) / 2;
8444 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8446 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8450 count1
= ssymbuf1
[mid
].count
;
8455 if (ignore_section_symbol_p
)
8457 for (i
= 0; i
< count1
; i
++)
8458 if (ELF_ST_TYPE (ssymbuf1
->ssym
[i
].st_info
) == STT_SECTION
)
8460 count1
-= sec_count1
;
8464 hi
= ssymbuf2
->count
;
8470 mid
= (lo
+ hi
) / 2;
8471 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8473 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8477 count2
= ssymbuf2
[mid
].count
;
8482 if (ignore_section_symbol_p
)
8484 for (i
= 0; i
< count2
; i
++)
8485 if (ELF_ST_TYPE (ssymbuf2
->ssym
[i
].st_info
) == STT_SECTION
)
8487 count2
-= sec_count2
;
8490 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8494 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8496 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8497 if (symtable1
== NULL
|| symtable2
== NULL
)
8501 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
+ sec_count1
;
8502 ssym
< ssymend
; ssym
++)
8504 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8506 symp
->u
.ssym
= ssym
;
8507 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8514 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
+ sec_count2
;
8515 ssym
< ssymend
; ssym
++)
8517 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8519 symp
->u
.ssym
= ssym
;
8520 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8526 /* Sort symbol by name. */
8527 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8528 elf_sym_name_compare
);
8529 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8530 elf_sym_name_compare
);
8532 for (i
= 0; i
< count1
; i
++)
8533 /* Two symbols must have the same binding, type and name. */
8534 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8535 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8536 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8543 symtable1
= (struct elf_symbol
*)
8544 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8545 symtable2
= (struct elf_symbol
*)
8546 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8547 if (symtable1
== NULL
|| symtable2
== NULL
)
8550 /* Count definitions in the section. */
8552 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8553 if (isym
->st_shndx
== shndx1
8554 && (!ignore_section_symbol_p
8555 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8556 symtable1
[count1
++].u
.isym
= isym
;
8559 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8560 if (isym
->st_shndx
== shndx2
8561 && (!ignore_section_symbol_p
8562 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8563 symtable2
[count2
++].u
.isym
= isym
;
8565 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8568 for (i
= 0; i
< count1
; i
++)
8570 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8571 symtable1
[i
].u
.isym
->st_name
);
8573 for (i
= 0; i
< count2
; i
++)
8575 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8576 symtable2
[i
].u
.isym
->st_name
);
8578 /* Sort symbol by name. */
8579 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8580 elf_sym_name_compare
);
8581 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8582 elf_sym_name_compare
);
8584 for (i
= 0; i
< count1
; i
++)
8585 /* Two symbols must have the same binding, type and name. */
8586 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8587 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8588 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8602 /* Return TRUE if 2 section types are compatible. */
8605 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8606 bfd
*bbfd
, const asection
*bsec
)
8610 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8611 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8614 return elf_section_type (asec
) == elf_section_type (bsec
);
8617 /* Final phase of ELF linker. */
8619 /* A structure we use to avoid passing large numbers of arguments. */
8621 struct elf_final_link_info
8623 /* General link information. */
8624 struct bfd_link_info
*info
;
8627 /* Symbol string table. */
8628 struct elf_strtab_hash
*symstrtab
;
8629 /* .hash section. */
8631 /* symbol version section (.gnu.version). */
8632 asection
*symver_sec
;
8633 /* Buffer large enough to hold contents of any section. */
8635 /* Buffer large enough to hold external relocs of any section. */
8636 void *external_relocs
;
8637 /* Buffer large enough to hold internal relocs of any section. */
8638 Elf_Internal_Rela
*internal_relocs
;
8639 /* Buffer large enough to hold external local symbols of any input
8641 bfd_byte
*external_syms
;
8642 /* And a buffer for symbol section indices. */
8643 Elf_External_Sym_Shndx
*locsym_shndx
;
8644 /* Buffer large enough to hold internal local symbols of any input
8646 Elf_Internal_Sym
*internal_syms
;
8647 /* Array large enough to hold a symbol index for each local symbol
8648 of any input BFD. */
8650 /* Array large enough to hold a section pointer for each local
8651 symbol of any input BFD. */
8652 asection
**sections
;
8653 /* Buffer for SHT_SYMTAB_SHNDX section. */
8654 Elf_External_Sym_Shndx
*symshndxbuf
;
8655 /* Number of STT_FILE syms seen. */
8656 size_t filesym_count
;
8657 /* Local symbol hash table. */
8658 struct bfd_hash_table local_hash_table
;
8661 struct local_hash_entry
8663 /* Base hash table entry structure. */
8664 struct bfd_hash_entry root
;
8665 /* Size of the local symbol name. */
8667 /* Number of the duplicated local symbol names. */
8671 /* Create an entry in the local symbol hash table. */
8673 static struct bfd_hash_entry
*
8674 local_hash_newfunc (struct bfd_hash_entry
*entry
,
8675 struct bfd_hash_table
*table
,
8679 /* Allocate the structure if it has not already been allocated by a
8683 entry
= bfd_hash_allocate (table
,
8684 sizeof (struct local_hash_entry
));
8689 /* Call the allocation method of the superclass. */
8690 entry
= bfd_hash_newfunc (entry
, table
, string
);
8693 ((struct local_hash_entry
*) entry
)->count
= 0;
8694 ((struct local_hash_entry
*) entry
)->size
= 0;
8700 /* This struct is used to pass information to elf_link_output_extsym. */
8702 struct elf_outext_info
8707 struct elf_final_link_info
*flinfo
;
8711 /* Support for evaluating a complex relocation.
8713 Complex relocations are generalized, self-describing relocations. The
8714 implementation of them consists of two parts: complex symbols, and the
8715 relocations themselves.
8717 The relocations use a reserved elf-wide relocation type code (R_RELC
8718 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8719 information (start bit, end bit, word width, etc) into the addend. This
8720 information is extracted from CGEN-generated operand tables within gas.
8722 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8723 internal) representing prefix-notation expressions, including but not
8724 limited to those sorts of expressions normally encoded as addends in the
8725 addend field. The symbol mangling format is:
8728 | <unary-operator> ':' <node>
8729 | <binary-operator> ':' <node> ':' <node>
8732 <literal> := 's' <digits=N> ':' <N character symbol name>
8733 | 'S' <digits=N> ':' <N character section name>
8737 <binary-operator> := as in C
8738 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8741 set_symbol_value (bfd
*bfd_with_globals
,
8742 Elf_Internal_Sym
*isymbuf
,
8747 struct elf_link_hash_entry
**sym_hashes
;
8748 struct elf_link_hash_entry
*h
;
8749 size_t extsymoff
= locsymcount
;
8751 if (symidx
< locsymcount
)
8753 Elf_Internal_Sym
*sym
;
8755 sym
= isymbuf
+ symidx
;
8756 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8758 /* It is a local symbol: move it to the
8759 "absolute" section and give it a value. */
8760 sym
->st_shndx
= SHN_ABS
;
8761 sym
->st_value
= val
;
8764 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8768 /* It is a global symbol: set its link type
8769 to "defined" and give it a value. */
8771 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8772 h
= sym_hashes
[symidx
- extsymoff
];
8773 while (h
->root
.type
== bfd_link_hash_indirect
8774 || h
->root
.type
== bfd_link_hash_warning
)
8775 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8776 h
->root
.type
= bfd_link_hash_defined
;
8777 h
->root
.u
.def
.value
= val
;
8778 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8782 resolve_symbol (const char *name
,
8784 struct elf_final_link_info
*flinfo
,
8786 Elf_Internal_Sym
*isymbuf
,
8789 Elf_Internal_Sym
*sym
;
8790 struct bfd_link_hash_entry
*global_entry
;
8791 const char *candidate
= NULL
;
8792 Elf_Internal_Shdr
*symtab_hdr
;
8795 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8797 for (i
= 0; i
< locsymcount
; ++ i
)
8801 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8804 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8805 symtab_hdr
->sh_link
,
8808 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8809 name
, candidate
, (unsigned long) sym
->st_value
);
8811 if (candidate
&& strcmp (candidate
, name
) == 0)
8813 asection
*sec
= flinfo
->sections
[i
];
8815 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8816 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8818 printf ("Found symbol with value %8.8lx\n",
8819 (unsigned long) *result
);
8825 /* Hmm, haven't found it yet. perhaps it is a global. */
8826 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8827 false, false, true);
8831 if (global_entry
->type
== bfd_link_hash_defined
8832 || global_entry
->type
== bfd_link_hash_defweak
)
8834 *result
= (global_entry
->u
.def
.value
8835 + global_entry
->u
.def
.section
->output_section
->vma
8836 + global_entry
->u
.def
.section
->output_offset
);
8838 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8839 global_entry
->root
.string
, (unsigned long) *result
);
8847 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8848 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8849 names like "foo.end" which is the end address of section "foo". */
8852 resolve_section (const char *name
,
8860 for (curr
= sections
; curr
; curr
= curr
->next
)
8861 if (strcmp (curr
->name
, name
) == 0)
8863 *result
= curr
->vma
;
8867 /* Hmm. still haven't found it. try pseudo-section names. */
8868 /* FIXME: This could be coded more efficiently... */
8869 for (curr
= sections
; curr
; curr
= curr
->next
)
8871 len
= strlen (curr
->name
);
8872 if (len
> strlen (name
))
8875 if (strncmp (curr
->name
, name
, len
) == 0)
8877 if (startswith (name
+ len
, ".end"))
8879 *result
= (curr
->vma
8880 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
8884 /* Insert more pseudo-section names here, if you like. */
8892 undefined_reference (const char *reftype
, const char *name
)
8894 /* xgettext:c-format */
8895 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8897 bfd_set_error (bfd_error_bad_value
);
8901 eval_symbol (bfd_vma
*result
,
8904 struct elf_final_link_info
*flinfo
,
8906 Elf_Internal_Sym
*isymbuf
,
8915 const char *sym
= *symp
;
8917 bool symbol_is_section
= false;
8922 if (len
< 1 || len
> sizeof (symbuf
))
8924 bfd_set_error (bfd_error_invalid_operation
);
8937 *result
= strtoul (sym
, (char **) symp
, 16);
8941 symbol_is_section
= true;
8945 symlen
= strtol (sym
, (char **) symp
, 10);
8946 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8948 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8950 bfd_set_error (bfd_error_invalid_operation
);
8954 memcpy (symbuf
, sym
, symlen
);
8955 symbuf
[symlen
] = '\0';
8956 *symp
= sym
+ symlen
;
8958 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8959 the symbol as a section, or vice-versa. so we're pretty liberal in our
8960 interpretation here; section means "try section first", not "must be a
8961 section", and likewise with symbol. */
8963 if (symbol_is_section
)
8965 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8966 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8967 isymbuf
, locsymcount
))
8969 undefined_reference ("section", symbuf
);
8975 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8976 isymbuf
, locsymcount
)
8977 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8980 undefined_reference ("symbol", symbuf
);
8987 /* All that remains are operators. */
8989 #define UNARY_OP(op) \
8990 if (startswith (sym, #op)) \
8992 sym += strlen (#op); \
8996 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8997 isymbuf, locsymcount, signed_p)) \
9000 *result = op ((bfd_signed_vma) a); \
9006 #define BINARY_OP_HEAD(op) \
9007 if (startswith (sym, #op)) \
9009 sym += strlen (#op); \
9013 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9014 isymbuf, locsymcount, signed_p)) \
9017 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9018 isymbuf, locsymcount, signed_p)) \
9020 #define BINARY_OP_TAIL(op) \
9022 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9027 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9031 BINARY_OP_HEAD (<<);
9032 if (b
>= sizeof (a
) * CHAR_BIT
)
9038 BINARY_OP_TAIL (<<);
9039 BINARY_OP_HEAD (>>);
9040 if (b
>= sizeof (a
) * CHAR_BIT
)
9042 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
9045 BINARY_OP_TAIL (>>);
9058 _bfd_error_handler (_("division by zero"));
9059 bfd_set_error (bfd_error_bad_value
);
9066 _bfd_error_handler (_("division by zero"));
9067 bfd_set_error (bfd_error_bad_value
);
9080 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
9081 bfd_set_error (bfd_error_invalid_operation
);
9087 put_value (bfd_vma size
,
9088 unsigned long chunksz
,
9093 location
+= (size
- chunksz
);
9095 for (; size
; size
-= chunksz
, location
-= chunksz
)
9100 bfd_put_8 (input_bfd
, x
, location
);
9104 bfd_put_16 (input_bfd
, x
, location
);
9108 bfd_put_32 (input_bfd
, x
, location
);
9109 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9115 bfd_put_64 (input_bfd
, x
, location
);
9116 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9129 get_value (bfd_vma size
,
9130 unsigned long chunksz
,
9137 /* Sanity checks. */
9138 BFD_ASSERT (chunksz
<= sizeof (x
)
9141 && (size
% chunksz
) == 0
9142 && input_bfd
!= NULL
9143 && location
!= NULL
);
9145 if (chunksz
== sizeof (x
))
9147 BFD_ASSERT (size
== chunksz
);
9149 /* Make sure that we do not perform an undefined shift operation.
9150 We know that size == chunksz so there will only be one iteration
9151 of the loop below. */
9155 shift
= 8 * chunksz
;
9157 for (; size
; size
-= chunksz
, location
+= chunksz
)
9162 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
9165 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
9168 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
9172 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
9183 decode_complex_addend (unsigned long *start
, /* in bits */
9184 unsigned long *oplen
, /* in bits */
9185 unsigned long *len
, /* in bits */
9186 unsigned long *wordsz
, /* in bytes */
9187 unsigned long *chunksz
, /* in bytes */
9188 unsigned long *lsb0_p
,
9189 unsigned long *signed_p
,
9190 unsigned long *trunc_p
,
9191 unsigned long encoded
)
9193 * start
= encoded
& 0x3F;
9194 * len
= (encoded
>> 6) & 0x3F;
9195 * oplen
= (encoded
>> 12) & 0x3F;
9196 * wordsz
= (encoded
>> 18) & 0xF;
9197 * chunksz
= (encoded
>> 22) & 0xF;
9198 * lsb0_p
= (encoded
>> 27) & 1;
9199 * signed_p
= (encoded
>> 28) & 1;
9200 * trunc_p
= (encoded
>> 29) & 1;
9203 bfd_reloc_status_type
9204 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
9205 asection
*input_section
,
9207 Elf_Internal_Rela
*rel
,
9210 bfd_vma shift
, x
, mask
;
9211 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9212 bfd_reloc_status_type r
;
9213 bfd_size_type octets
;
9215 /* Perform this reloc, since it is complex.
9216 (this is not to say that it necessarily refers to a complex
9217 symbol; merely that it is a self-describing CGEN based reloc.
9218 i.e. the addend has the complete reloc information (bit start, end,
9219 word size, etc) encoded within it.). */
9221 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9222 &chunksz
, &lsb0_p
, &signed_p
,
9223 &trunc_p
, rel
->r_addend
);
9225 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9228 shift
= (start
+ 1) - len
;
9230 shift
= (8 * wordsz
) - (start
+ len
);
9232 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9233 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9236 printf ("Doing complex reloc: "
9237 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9238 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9239 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9240 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9241 oplen
, (unsigned long) x
, (unsigned long) mask
,
9242 (unsigned long) relocation
);
9247 /* Now do an overflow check. */
9248 r
= bfd_check_overflow ((signed_p
9249 ? complain_overflow_signed
9250 : complain_overflow_unsigned
),
9251 len
, 0, (8 * wordsz
),
9255 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9258 printf (" relocation: %8.8lx\n"
9259 " shifted mask: %8.8lx\n"
9260 " shifted/masked reloc: %8.8lx\n"
9261 " result: %8.8lx\n",
9262 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9263 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9265 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9269 /* Functions to read r_offset from external (target order) reloc
9270 entry. Faster than bfd_getl32 et al, because we let the compiler
9271 know the value is aligned. */
9274 ext32l_r_offset (const void *p
)
9281 const union aligned32
*a
9282 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9284 uint32_t aval
= ( (uint32_t) a
->c
[0]
9285 | (uint32_t) a
->c
[1] << 8
9286 | (uint32_t) a
->c
[2] << 16
9287 | (uint32_t) a
->c
[3] << 24);
9292 ext32b_r_offset (const void *p
)
9299 const union aligned32
*a
9300 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9302 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9303 | (uint32_t) a
->c
[1] << 16
9304 | (uint32_t) a
->c
[2] << 8
9305 | (uint32_t) a
->c
[3]);
9309 #ifdef BFD_HOST_64_BIT
9311 ext64l_r_offset (const void *p
)
9318 const union aligned64
*a
9319 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9321 uint64_t aval
= ( (uint64_t) a
->c
[0]
9322 | (uint64_t) a
->c
[1] << 8
9323 | (uint64_t) a
->c
[2] << 16
9324 | (uint64_t) a
->c
[3] << 24
9325 | (uint64_t) a
->c
[4] << 32
9326 | (uint64_t) a
->c
[5] << 40
9327 | (uint64_t) a
->c
[6] << 48
9328 | (uint64_t) a
->c
[7] << 56);
9333 ext64b_r_offset (const void *p
)
9340 const union aligned64
*a
9341 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9343 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9344 | (uint64_t) a
->c
[1] << 48
9345 | (uint64_t) a
->c
[2] << 40
9346 | (uint64_t) a
->c
[3] << 32
9347 | (uint64_t) a
->c
[4] << 24
9348 | (uint64_t) a
->c
[5] << 16
9349 | (uint64_t) a
->c
[6] << 8
9350 | (uint64_t) a
->c
[7]);
9355 /* When performing a relocatable link, the input relocations are
9356 preserved. But, if they reference global symbols, the indices
9357 referenced must be updated. Update all the relocations found in
9361 elf_link_adjust_relocs (bfd
*abfd
,
9363 struct bfd_elf_section_reloc_data
*reldata
,
9365 struct bfd_link_info
*info
)
9368 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9370 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9371 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9372 bfd_vma r_type_mask
;
9374 unsigned int count
= reldata
->count
;
9375 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9377 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9379 swap_in
= bed
->s
->swap_reloc_in
;
9380 swap_out
= bed
->s
->swap_reloc_out
;
9382 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9384 swap_in
= bed
->s
->swap_reloca_in
;
9385 swap_out
= bed
->s
->swap_reloca_out
;
9390 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9393 if (bed
->s
->arch_size
== 32)
9400 r_type_mask
= 0xffffffff;
9404 erela
= reldata
->hdr
->contents
;
9405 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9407 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9410 if (*rel_hash
== NULL
)
9413 if ((*rel_hash
)->indx
== -2
9414 && info
->gc_sections
9415 && ! info
->gc_keep_exported
)
9417 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9418 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9420 (*rel_hash
)->root
.root
.string
);
9421 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9423 bfd_set_error (bfd_error_invalid_operation
);
9426 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9428 (*swap_in
) (abfd
, erela
, irela
);
9429 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9430 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9431 | (irela
[j
].r_info
& r_type_mask
));
9432 (*swap_out
) (abfd
, irela
, erela
);
9435 if (bed
->elf_backend_update_relocs
)
9436 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9438 if (sort
&& count
!= 0)
9440 bfd_vma (*ext_r_off
) (const void *);
9443 bfd_byte
*base
, *end
, *p
, *loc
;
9444 bfd_byte
*buf
= NULL
;
9446 if (bed
->s
->arch_size
== 32)
9448 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9449 ext_r_off
= ext32l_r_offset
;
9450 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9451 ext_r_off
= ext32b_r_offset
;
9457 #ifdef BFD_HOST_64_BIT
9458 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9459 ext_r_off
= ext64l_r_offset
;
9460 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9461 ext_r_off
= ext64b_r_offset
;
9467 /* Must use a stable sort here. A modified insertion sort,
9468 since the relocs are mostly sorted already. */
9469 elt_size
= reldata
->hdr
->sh_entsize
;
9470 base
= reldata
->hdr
->contents
;
9471 end
= base
+ count
* elt_size
;
9472 if (elt_size
> sizeof (Elf64_External_Rela
))
9475 /* Ensure the first element is lowest. This acts as a sentinel,
9476 speeding the main loop below. */
9477 r_off
= (*ext_r_off
) (base
);
9478 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9480 bfd_vma r_off2
= (*ext_r_off
) (p
);
9489 /* Don't just swap *base and *loc as that changes the order
9490 of the original base[0] and base[1] if they happen to
9491 have the same r_offset. */
9492 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9493 memcpy (onebuf
, loc
, elt_size
);
9494 memmove (base
+ elt_size
, base
, loc
- base
);
9495 memcpy (base
, onebuf
, elt_size
);
9498 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9500 /* base to p is sorted, *p is next to insert. */
9501 r_off
= (*ext_r_off
) (p
);
9502 /* Search the sorted region for location to insert. */
9504 while (r_off
< (*ext_r_off
) (loc
))
9509 /* Chances are there is a run of relocs to insert here,
9510 from one of more input files. Files are not always
9511 linked in order due to the way elf_link_input_bfd is
9512 called. See pr17666. */
9513 size_t sortlen
= p
- loc
;
9514 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9515 size_t runlen
= elt_size
;
9516 size_t buf_size
= 96 * 1024;
9517 while (p
+ runlen
< end
9518 && (sortlen
<= buf_size
9519 || runlen
+ elt_size
<= buf_size
)
9520 && r_off2
> (*ext_r_off
) (p
+ runlen
))
9524 buf
= bfd_malloc (buf_size
);
9528 if (runlen
< sortlen
)
9530 memcpy (buf
, p
, runlen
);
9531 memmove (loc
+ runlen
, loc
, sortlen
);
9532 memcpy (loc
, buf
, runlen
);
9536 memcpy (buf
, loc
, sortlen
);
9537 memmove (loc
, p
, runlen
);
9538 memcpy (loc
+ runlen
, buf
, sortlen
);
9540 p
+= runlen
- elt_size
;
9543 /* Hashes are no longer valid. */
9544 free (reldata
->hashes
);
9545 reldata
->hashes
= NULL
;
9551 struct elf_link_sort_rela
9557 enum elf_reloc_type_class type
;
9558 /* We use this as an array of size int_rels_per_ext_rel. */
9559 Elf_Internal_Rela rela
[1];
9562 /* qsort stability here and for cmp2 is only an issue if multiple
9563 dynamic relocations are emitted at the same address. But targets
9564 that apply a series of dynamic relocations each operating on the
9565 result of the prior relocation can't use -z combreloc as
9566 implemented anyway. Such schemes tend to be broken by sorting on
9567 symbol index. That leaves dynamic NONE relocs as the only other
9568 case where ld might emit multiple relocs at the same address, and
9569 those are only emitted due to target bugs. */
9572 elf_link_sort_cmp1 (const void *A
, const void *B
)
9574 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9575 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9576 int relativea
, relativeb
;
9578 relativea
= a
->type
== reloc_class_relative
;
9579 relativeb
= b
->type
== reloc_class_relative
;
9581 if (relativea
< relativeb
)
9583 if (relativea
> relativeb
)
9585 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9587 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9589 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9591 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9597 elf_link_sort_cmp2 (const void *A
, const void *B
)
9599 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9600 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9602 if (a
->type
< b
->type
)
9604 if (a
->type
> b
->type
)
9606 if (a
->u
.offset
< b
->u
.offset
)
9608 if (a
->u
.offset
> b
->u
.offset
)
9610 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9612 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9618 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9620 asection
*dynamic_relocs
;
9623 bfd_size_type count
, size
;
9624 size_t i
, ret
, sort_elt
, ext_size
;
9625 bfd_byte
*sort
, *s_non_relative
, *p
;
9626 struct elf_link_sort_rela
*sq
;
9627 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9628 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9629 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9630 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9631 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9632 struct bfd_link_order
*lo
;
9636 /* Find a dynamic reloc section. */
9637 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9638 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9639 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9640 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9642 bool use_rela_initialised
= false;
9644 /* This is just here to stop gcc from complaining.
9645 Its initialization checking code is not perfect. */
9648 /* Both sections are present. Examine the sizes
9649 of the indirect sections to help us choose. */
9650 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9651 if (lo
->type
== bfd_indirect_link_order
)
9653 asection
*o
= lo
->u
.indirect
.section
;
9655 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9657 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9658 /* Section size is divisible by both rel and rela sizes.
9659 It is of no help to us. */
9663 /* Section size is only divisible by rela. */
9664 if (use_rela_initialised
&& !use_rela
)
9666 _bfd_error_handler (_("%pB: unable to sort relocs - "
9667 "they are in more than one size"),
9669 bfd_set_error (bfd_error_invalid_operation
);
9675 use_rela_initialised
= true;
9679 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9681 /* Section size is only divisible by rel. */
9682 if (use_rela_initialised
&& use_rela
)
9684 _bfd_error_handler (_("%pB: unable to sort relocs - "
9685 "they are in more than one size"),
9687 bfd_set_error (bfd_error_invalid_operation
);
9693 use_rela_initialised
= true;
9698 /* The section size is not divisible by either -
9699 something is wrong. */
9700 _bfd_error_handler (_("%pB: unable to sort relocs - "
9701 "they are of an unknown size"), abfd
);
9702 bfd_set_error (bfd_error_invalid_operation
);
9707 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9708 if (lo
->type
== bfd_indirect_link_order
)
9710 asection
*o
= lo
->u
.indirect
.section
;
9712 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9714 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9715 /* Section size is divisible by both rel and rela sizes.
9716 It is of no help to us. */
9720 /* Section size is only divisible by rela. */
9721 if (use_rela_initialised
&& !use_rela
)
9723 _bfd_error_handler (_("%pB: unable to sort relocs - "
9724 "they are in more than one size"),
9726 bfd_set_error (bfd_error_invalid_operation
);
9732 use_rela_initialised
= true;
9736 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9738 /* Section size is only divisible by rel. */
9739 if (use_rela_initialised
&& use_rela
)
9741 _bfd_error_handler (_("%pB: unable to sort relocs - "
9742 "they are in more than one size"),
9744 bfd_set_error (bfd_error_invalid_operation
);
9750 use_rela_initialised
= true;
9755 /* The section size is not divisible by either -
9756 something is wrong. */
9757 _bfd_error_handler (_("%pB: unable to sort relocs - "
9758 "they are of an unknown size"), abfd
);
9759 bfd_set_error (bfd_error_invalid_operation
);
9764 if (! use_rela_initialised
)
9768 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9770 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9777 dynamic_relocs
= rela_dyn
;
9778 ext_size
= bed
->s
->sizeof_rela
;
9779 swap_in
= bed
->s
->swap_reloca_in
;
9780 swap_out
= bed
->s
->swap_reloca_out
;
9784 dynamic_relocs
= rel_dyn
;
9785 ext_size
= bed
->s
->sizeof_rel
;
9786 swap_in
= bed
->s
->swap_reloc_in
;
9787 swap_out
= bed
->s
->swap_reloc_out
;
9791 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9792 if (lo
->type
== bfd_indirect_link_order
)
9793 size
+= lo
->u
.indirect
.section
->size
;
9795 if (size
!= dynamic_relocs
->size
)
9798 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9799 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9801 count
= dynamic_relocs
->size
/ ext_size
;
9804 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9808 (*info
->callbacks
->warning
)
9809 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9813 if (bed
->s
->arch_size
== 32)
9814 r_sym_mask
= ~(bfd_vma
) 0xff;
9816 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9818 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9819 if (lo
->type
== bfd_indirect_link_order
)
9821 bfd_byte
*erel
, *erelend
;
9822 asection
*o
= lo
->u
.indirect
.section
;
9824 if (o
->contents
== NULL
&& o
->size
!= 0)
9826 /* This is a reloc section that is being handled as a normal
9827 section. See bfd_section_from_shdr. We can't combine
9828 relocs in this case. */
9833 erelend
= o
->contents
+ o
->size
;
9834 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9836 while (erel
< erelend
)
9838 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9840 (*swap_in
) (abfd
, erel
, s
->rela
);
9841 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9842 s
->u
.sym_mask
= r_sym_mask
;
9848 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9850 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9852 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9853 if (s
->type
!= reloc_class_relative
)
9859 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9860 for (; i
< count
; i
++, p
+= sort_elt
)
9862 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9863 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9865 sp
->u
.offset
= sq
->rela
->r_offset
;
9868 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9870 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9871 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9873 /* We have plt relocs in .rela.dyn. */
9874 sq
= (struct elf_link_sort_rela
*) sort
;
9875 for (i
= 0; i
< count
; i
++)
9876 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9878 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9880 struct bfd_link_order
**plo
;
9881 /* Put srelplt link_order last. This is so the output_offset
9882 set in the next loop is correct for DT_JMPREL. */
9883 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9884 if ((*plo
)->type
== bfd_indirect_link_order
9885 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9891 plo
= &(*plo
)->next
;
9894 dynamic_relocs
->map_tail
.link_order
= lo
;
9899 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9900 if (lo
->type
== bfd_indirect_link_order
)
9902 bfd_byte
*erel
, *erelend
;
9903 asection
*o
= lo
->u
.indirect
.section
;
9906 erelend
= o
->contents
+ o
->size
;
9907 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9908 while (erel
< erelend
)
9910 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9911 (*swap_out
) (abfd
, s
->rela
, erel
);
9918 *psec
= dynamic_relocs
;
9922 /* Add a symbol to the output symbol string table. */
9925 elf_link_output_symstrtab (void *finf
,
9927 Elf_Internal_Sym
*elfsym
,
9928 asection
*input_sec
,
9929 struct elf_link_hash_entry
*h
)
9931 struct elf_final_link_info
*flinfo
= finf
;
9932 int (*output_symbol_hook
)
9933 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9934 struct elf_link_hash_entry
*);
9935 struct elf_link_hash_table
*hash_table
;
9936 const struct elf_backend_data
*bed
;
9937 bfd_size_type strtabsize
;
9939 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9941 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9942 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9943 if (output_symbol_hook
!= NULL
)
9945 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9950 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
9951 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
9952 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
9953 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
9957 || (input_sec
->flags
& SEC_EXCLUDE
))
9958 elfsym
->st_name
= (unsigned long) -1;
9961 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9962 to get the final offset for st_name. */
9963 char *versioned_name
= (char *) name
;
9966 if (h
->versioned
== versioned
&& h
->def_dynamic
)
9968 /* Keep only one '@' for versioned symbols defined in
9970 char *version
= strrchr (name
, ELF_VER_CHR
);
9971 char *base_end
= strchr (name
, ELF_VER_CHR
);
9972 if (version
!= base_end
)
9975 size_t len
= strlen (name
);
9976 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
9977 if (versioned_name
== NULL
)
9979 base_len
= base_end
- name
;
9980 memcpy (versioned_name
, name
, base_len
);
9981 memcpy (versioned_name
+ base_len
, version
,
9986 else if (flinfo
->info
->unique_symbol
9987 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
9989 struct local_hash_entry
*lh
;
9993 switch (ELF_ST_TYPE (elfsym
->st_info
))
9999 lh
= (struct local_hash_entry
*) bfd_hash_lookup
10000 (&flinfo
->local_hash_table
, name
, true, false);
10003 /* Always append ".COUNT" to local symbols to avoid
10004 potential conflicts with local symbol "XXX.COUNT". */
10005 sprintf (buf
, "%lx", lh
->count
);
10006 base_len
= lh
->size
;
10009 base_len
= strlen (name
);
10010 lh
->size
= base_len
;
10012 count_len
= strlen (buf
);
10013 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
10014 base_len
+ count_len
+ 2);
10015 if (versioned_name
== NULL
)
10017 memcpy (versioned_name
, name
, base_len
);
10018 versioned_name
[base_len
] = '.';
10019 memcpy (versioned_name
+ base_len
+ 1, buf
,
10026 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
10027 versioned_name
, false);
10028 if (elfsym
->st_name
== (unsigned long) -1)
10032 hash_table
= elf_hash_table (flinfo
->info
);
10033 strtabsize
= hash_table
->strtabsize
;
10034 if (strtabsize
<= flinfo
->output_bfd
->symcount
)
10036 strtabsize
+= strtabsize
;
10037 hash_table
->strtabsize
= strtabsize
;
10038 strtabsize
*= sizeof (*hash_table
->strtab
);
10040 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
10042 if (hash_table
->strtab
== NULL
)
10045 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].sym
= *elfsym
;
10046 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].dest_index
10047 = flinfo
->output_bfd
->symcount
;
10048 flinfo
->output_bfd
->symcount
+= 1;
10053 /* Swap symbols out to the symbol table and flush the output symbols to
10057 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
10059 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
10062 const struct elf_backend_data
*bed
;
10064 Elf_Internal_Shdr
*hdr
;
10068 if (flinfo
->output_bfd
->symcount
== 0)
10071 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10073 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10075 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10076 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
10077 if (symbuf
== NULL
)
10080 if (flinfo
->symshndxbuf
)
10082 amt
= sizeof (Elf_External_Sym_Shndx
);
10083 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
10084 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
10085 if (flinfo
->symshndxbuf
== NULL
)
10092 /* Now swap out the symbols. */
10093 for (i
= 0; i
< flinfo
->output_bfd
->symcount
; i
++)
10095 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
10096 if (elfsym
->sym
.st_name
== (unsigned long) -1)
10097 elfsym
->sym
.st_name
= 0;
10099 elfsym
->sym
.st_name
10100 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
10101 elfsym
->sym
.st_name
);
10103 /* Inform the linker of the addition of this symbol. */
10105 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
10106 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
10109 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
10110 ((bfd_byte
*) symbuf
10111 + (elfsym
->dest_index
10112 * bed
->s
->sizeof_sym
)),
10113 NPTR_ADD (flinfo
->symshndxbuf
,
10114 elfsym
->dest_index
));
10117 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
10118 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
10119 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10120 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
10121 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
10123 hdr
->sh_size
+= amt
;
10131 free (hash_table
->strtab
);
10132 hash_table
->strtab
= NULL
;
10137 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10140 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
10142 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
10143 && sym
->st_shndx
< SHN_LORESERVE
)
10145 /* The gABI doesn't support dynamic symbols in output sections
10148 /* xgettext:c-format */
10149 (_("%pB: too many sections: %d (>= %d)"),
10150 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
10151 bfd_set_error (bfd_error_nonrepresentable_section
);
10157 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10158 allowing an unsatisfied unversioned symbol in the DSO to match a
10159 versioned symbol that would normally require an explicit version.
10160 We also handle the case that a DSO references a hidden symbol
10161 which may be satisfied by a versioned symbol in another DSO. */
10164 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
10165 const struct elf_backend_data
*bed
,
10166 struct elf_link_hash_entry
*h
)
10169 struct elf_link_loaded_list
*loaded
;
10171 if (!is_elf_hash_table (info
->hash
))
10174 /* Check indirect symbol. */
10175 while (h
->root
.type
== bfd_link_hash_indirect
)
10176 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10178 switch (h
->root
.type
)
10184 case bfd_link_hash_undefined
:
10185 case bfd_link_hash_undefweak
:
10186 abfd
= h
->root
.u
.undef
.abfd
;
10188 || (abfd
->flags
& DYNAMIC
) == 0
10189 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
10193 case bfd_link_hash_defined
:
10194 case bfd_link_hash_defweak
:
10195 abfd
= h
->root
.u
.def
.section
->owner
;
10198 case bfd_link_hash_common
:
10199 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10202 BFD_ASSERT (abfd
!= NULL
);
10204 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10206 loaded
= loaded
->next
)
10209 Elf_Internal_Shdr
*hdr
;
10211 size_t extsymcount
;
10213 Elf_Internal_Shdr
*versymhdr
;
10214 Elf_Internal_Sym
*isym
;
10215 Elf_Internal_Sym
*isymend
;
10216 Elf_Internal_Sym
*isymbuf
;
10217 Elf_External_Versym
*ever
;
10218 Elf_External_Versym
*extversym
;
10220 input
= loaded
->abfd
;
10222 /* We check each DSO for a possible hidden versioned definition. */
10224 || elf_dynversym (input
) == 0)
10227 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10229 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10230 if (elf_bad_symtab (input
))
10232 extsymcount
= symcount
;
10237 extsymcount
= symcount
- hdr
->sh_info
;
10238 extsymoff
= hdr
->sh_info
;
10241 if (extsymcount
== 0)
10244 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10246 if (isymbuf
== NULL
)
10249 /* Read in any version definitions. */
10250 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10251 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10252 || (extversym
= (Elf_External_Versym
*)
10253 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10254 versymhdr
->sh_size
)) == NULL
)
10260 ever
= extversym
+ extsymoff
;
10261 isymend
= isymbuf
+ extsymcount
;
10262 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10265 Elf_Internal_Versym iver
;
10266 unsigned short version_index
;
10268 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10269 || isym
->st_shndx
== SHN_UNDEF
)
10272 name
= bfd_elf_string_from_elf_section (input
,
10275 if (strcmp (name
, h
->root
.root
.string
) != 0)
10278 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10280 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10281 && !(h
->def_regular
10282 && h
->forced_local
))
10284 /* If we have a non-hidden versioned sym, then it should
10285 have provided a definition for the undefined sym unless
10286 it is defined in a non-shared object and forced local.
10291 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10292 if (version_index
== 1 || version_index
== 2)
10294 /* This is the base or first version. We can use it. */
10308 /* Convert ELF common symbol TYPE. */
10311 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10313 /* Commom symbol can only appear in relocatable link. */
10314 if (!bfd_link_relocatable (info
))
10316 switch (info
->elf_stt_common
)
10320 case elf_stt_common
:
10323 case no_elf_stt_common
:
10330 /* Add an external symbol to the symbol table. This is called from
10331 the hash table traversal routine. When generating a shared object,
10332 we go through the symbol table twice. The first time we output
10333 anything that might have been forced to local scope in a version
10334 script. The second time we output the symbols that are still
10338 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10340 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10341 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10342 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10344 Elf_Internal_Sym sym
;
10345 asection
*input_sec
;
10346 const struct elf_backend_data
*bed
;
10351 if (h
->root
.type
== bfd_link_hash_warning
)
10353 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10354 if (h
->root
.type
== bfd_link_hash_new
)
10358 /* Decide whether to output this symbol in this pass. */
10359 if (eoinfo
->localsyms
)
10361 if (!h
->forced_local
)
10366 if (h
->forced_local
)
10370 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10372 if (h
->root
.type
== bfd_link_hash_undefined
)
10374 /* If we have an undefined symbol reference here then it must have
10375 come from a shared library that is being linked in. (Undefined
10376 references in regular files have already been handled unless
10377 they are in unreferenced sections which are removed by garbage
10379 bool ignore_undef
= false;
10381 /* Some symbols may be special in that the fact that they're
10382 undefined can be safely ignored - let backend determine that. */
10383 if (bed
->elf_backend_ignore_undef_symbol
)
10384 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10386 /* If we are reporting errors for this situation then do so now. */
10388 && h
->ref_dynamic_nonweak
10389 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10390 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10391 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10393 flinfo
->info
->callbacks
->undefined_symbol
10394 (flinfo
->info
, h
->root
.root
.string
,
10395 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10396 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10397 && !flinfo
->info
->warn_unresolved_syms
);
10400 /* Strip a global symbol defined in a discarded section. */
10405 /* We should also warn if a forced local symbol is referenced from
10406 shared libraries. */
10407 if (bfd_link_executable (flinfo
->info
)
10412 && h
->ref_dynamic_nonweak
10413 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10417 struct elf_link_hash_entry
*hi
= h
;
10419 /* Check indirect symbol. */
10420 while (hi
->root
.type
== bfd_link_hash_indirect
)
10421 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10423 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10424 /* xgettext:c-format */
10425 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10426 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10427 /* xgettext:c-format */
10428 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10430 /* xgettext:c-format */
10431 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10432 def_bfd
= flinfo
->output_bfd
;
10433 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10434 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10435 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10436 h
->root
.root
.string
, def_bfd
);
10437 bfd_set_error (bfd_error_bad_value
);
10438 eoinfo
->failed
= true;
10442 /* We don't want to output symbols that have never been mentioned by
10443 a regular file, or that we have been told to strip. However, if
10444 h->indx is set to -2, the symbol is used by a reloc and we must
10449 else if ((h
->def_dynamic
10451 || h
->root
.type
== bfd_link_hash_new
)
10453 && !h
->ref_regular
)
10455 else if (flinfo
->info
->strip
== strip_all
)
10457 else if (flinfo
->info
->strip
== strip_some
10458 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10459 h
->root
.root
.string
, false, false) == NULL
)
10461 else if ((h
->root
.type
== bfd_link_hash_defined
10462 || h
->root
.type
== bfd_link_hash_defweak
)
10463 && ((flinfo
->info
->strip_discarded
10464 && discarded_section (h
->root
.u
.def
.section
))
10465 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10466 && h
->root
.u
.def
.section
->owner
!= NULL
10467 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10469 else if ((h
->root
.type
== bfd_link_hash_undefined
10470 || h
->root
.type
== bfd_link_hash_undefweak
)
10471 && h
->root
.u
.undef
.abfd
!= NULL
10472 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10477 /* If we're stripping it, and it's not a dynamic symbol, there's
10478 nothing else to do. However, if it is a forced local symbol or
10479 an ifunc symbol we need to give the backend finish_dynamic_symbol
10480 function a chance to make it dynamic. */
10482 && h
->dynindx
== -1
10483 && type
!= STT_GNU_IFUNC
10484 && !h
->forced_local
)
10488 sym
.st_size
= h
->size
;
10489 sym
.st_other
= h
->other
;
10490 switch (h
->root
.type
)
10493 case bfd_link_hash_new
:
10494 case bfd_link_hash_warning
:
10498 case bfd_link_hash_undefined
:
10499 case bfd_link_hash_undefweak
:
10500 input_sec
= bfd_und_section_ptr
;
10501 sym
.st_shndx
= SHN_UNDEF
;
10504 case bfd_link_hash_defined
:
10505 case bfd_link_hash_defweak
:
10507 input_sec
= h
->root
.u
.def
.section
;
10508 if (input_sec
->output_section
!= NULL
)
10511 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10512 input_sec
->output_section
);
10513 if (sym
.st_shndx
== SHN_BAD
)
10516 /* xgettext:c-format */
10517 (_("%pB: could not find output section %pA for input section %pA"),
10518 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10519 bfd_set_error (bfd_error_nonrepresentable_section
);
10520 eoinfo
->failed
= true;
10524 /* ELF symbols in relocatable files are section relative,
10525 but in nonrelocatable files they are virtual
10527 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10528 if (!bfd_link_relocatable (flinfo
->info
))
10530 sym
.st_value
+= input_sec
->output_section
->vma
;
10531 if (h
->type
== STT_TLS
)
10533 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10534 if (tls_sec
!= NULL
)
10535 sym
.st_value
-= tls_sec
->vma
;
10541 BFD_ASSERT (input_sec
->owner
== NULL
10542 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10543 sym
.st_shndx
= SHN_UNDEF
;
10544 input_sec
= bfd_und_section_ptr
;
10549 case bfd_link_hash_common
:
10550 input_sec
= h
->root
.u
.c
.p
->section
;
10551 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10552 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10555 case bfd_link_hash_indirect
:
10556 /* These symbols are created by symbol versioning. They point
10557 to the decorated version of the name. For example, if the
10558 symbol foo@@GNU_1.2 is the default, which should be used when
10559 foo is used with no version, then we add an indirect symbol
10560 foo which points to foo@@GNU_1.2. We ignore these symbols,
10561 since the indirected symbol is already in the hash table. */
10565 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10566 switch (h
->root
.type
)
10568 case bfd_link_hash_common
:
10569 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10571 case bfd_link_hash_defined
:
10572 case bfd_link_hash_defweak
:
10573 if (bed
->common_definition (&sym
))
10574 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10578 case bfd_link_hash_undefined
:
10579 case bfd_link_hash_undefweak
:
10585 if (h
->forced_local
)
10587 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10588 /* Turn off visibility on local symbol. */
10589 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10591 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10592 else if (h
->unique_global
&& h
->def_regular
)
10593 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10594 else if (h
->root
.type
== bfd_link_hash_undefweak
10595 || h
->root
.type
== bfd_link_hash_defweak
)
10596 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10598 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10599 sym
.st_target_internal
= h
->target_internal
;
10601 /* Give the processor backend a chance to tweak the symbol value,
10602 and also to finish up anything that needs to be done for this
10603 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10604 forced local syms when non-shared is due to a historical quirk.
10605 STT_GNU_IFUNC symbol must go through PLT. */
10606 if ((h
->type
== STT_GNU_IFUNC
10608 && !bfd_link_relocatable (flinfo
->info
))
10609 || ((h
->dynindx
!= -1
10610 || h
->forced_local
)
10611 && ((bfd_link_pic (flinfo
->info
)
10612 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10613 || h
->root
.type
!= bfd_link_hash_undefweak
))
10614 || !h
->forced_local
)
10615 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10617 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10618 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10620 eoinfo
->failed
= true;
10625 /* If we are marking the symbol as undefined, and there are no
10626 non-weak references to this symbol from a regular object, then
10627 mark the symbol as weak undefined; if there are non-weak
10628 references, mark the symbol as strong. We can't do this earlier,
10629 because it might not be marked as undefined until the
10630 finish_dynamic_symbol routine gets through with it. */
10631 if (sym
.st_shndx
== SHN_UNDEF
10633 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10634 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10637 type
= ELF_ST_TYPE (sym
.st_info
);
10639 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10640 if (type
== STT_GNU_IFUNC
)
10643 if (h
->ref_regular_nonweak
)
10644 bindtype
= STB_GLOBAL
;
10646 bindtype
= STB_WEAK
;
10647 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10650 /* If this is a symbol defined in a dynamic library, don't use the
10651 symbol size from the dynamic library. Relinking an executable
10652 against a new library may introduce gratuitous changes in the
10653 executable's symbols if we keep the size. */
10654 if (sym
.st_shndx
== SHN_UNDEF
10659 /* If a non-weak symbol with non-default visibility is not defined
10660 locally, it is a fatal error. */
10661 if (!bfd_link_relocatable (flinfo
->info
)
10662 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10663 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10664 && h
->root
.type
== bfd_link_hash_undefined
10665 && !h
->def_regular
)
10669 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10670 /* xgettext:c-format */
10671 msg
= _("%pB: protected symbol `%s' isn't defined");
10672 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10673 /* xgettext:c-format */
10674 msg
= _("%pB: internal symbol `%s' isn't defined");
10676 /* xgettext:c-format */
10677 msg
= _("%pB: hidden symbol `%s' isn't defined");
10678 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10679 bfd_set_error (bfd_error_bad_value
);
10680 eoinfo
->failed
= true;
10684 /* If this symbol should be put in the .dynsym section, then put it
10685 there now. We already know the symbol index. We also fill in
10686 the entry in the .hash section. */
10687 if (h
->dynindx
!= -1
10688 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10689 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10690 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10694 /* Since there is no version information in the dynamic string,
10695 if there is no version info in symbol version section, we will
10696 have a run-time problem if not linking executable, referenced
10697 by shared library, or not bound locally. */
10698 if (h
->verinfo
.verdef
== NULL
10699 && (!bfd_link_executable (flinfo
->info
)
10701 || !h
->def_regular
))
10703 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10705 if (p
&& p
[1] != '\0')
10708 /* xgettext:c-format */
10709 (_("%pB: no symbol version section for versioned symbol `%s'"),
10710 flinfo
->output_bfd
, h
->root
.root
.string
);
10711 eoinfo
->failed
= true;
10716 sym
.st_name
= h
->dynstr_index
;
10717 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10718 + h
->dynindx
* bed
->s
->sizeof_sym
);
10719 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10721 eoinfo
->failed
= true;
10725 /* Inform the linker of the addition of this symbol. */
10727 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
10728 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
10730 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10732 if (flinfo
->hash_sec
!= NULL
)
10734 size_t hash_entry_size
;
10735 bfd_byte
*bucketpos
;
10737 size_t bucketcount
;
10740 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10741 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10744 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10745 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10746 + (bucket
+ 2) * hash_entry_size
);
10747 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10748 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10750 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10751 ((bfd_byte
*) flinfo
->hash_sec
->contents
10752 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10755 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10757 Elf_Internal_Versym iversym
;
10758 Elf_External_Versym
*eversym
;
10760 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10762 if (h
->verinfo
.verdef
== NULL
10763 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10764 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10765 iversym
.vs_vers
= 1;
10767 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10771 if (h
->verinfo
.vertree
== NULL
)
10772 iversym
.vs_vers
= 1;
10774 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10775 if (flinfo
->info
->create_default_symver
)
10779 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10780 defined locally. */
10781 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10782 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10784 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10785 eversym
+= h
->dynindx
;
10786 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10790 /* If the symbol is undefined, and we didn't output it to .dynsym,
10791 strip it from .symtab too. Obviously we can't do this for
10792 relocatable output or when needed for --emit-relocs. */
10793 else if (input_sec
== bfd_und_section_ptr
10795 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10796 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10797 && !bfd_link_relocatable (flinfo
->info
))
10800 /* Also strip others that we couldn't earlier due to dynamic symbol
10804 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10807 /* Output a FILE symbol so that following locals are not associated
10808 with the wrong input file. We need one for forced local symbols
10809 if we've seen more than one FILE symbol or when we have exactly
10810 one FILE symbol but global symbols are present in a file other
10811 than the one with the FILE symbol. We also need one if linker
10812 defined symbols are present. In practice these conditions are
10813 always met, so just emit the FILE symbol unconditionally. */
10814 if (eoinfo
->localsyms
10815 && !eoinfo
->file_sym_done
10816 && eoinfo
->flinfo
->filesym_count
!= 0)
10818 Elf_Internal_Sym fsym
;
10820 memset (&fsym
, 0, sizeof (fsym
));
10821 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10822 fsym
.st_shndx
= SHN_ABS
;
10823 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10824 bfd_und_section_ptr
, NULL
))
10827 eoinfo
->file_sym_done
= true;
10830 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10831 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10835 eoinfo
->failed
= true;
10840 else if (h
->indx
== -2)
10846 /* Return TRUE if special handling is done for relocs in SEC against
10847 symbols defined in discarded sections. */
10850 elf_section_ignore_discarded_relocs (asection
*sec
)
10852 const struct elf_backend_data
*bed
;
10854 switch (sec
->sec_info_type
)
10856 case SEC_INFO_TYPE_STABS
:
10857 case SEC_INFO_TYPE_EH_FRAME
:
10858 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10864 bed
= get_elf_backend_data (sec
->owner
);
10865 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10866 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10872 /* Return a mask saying how ld should treat relocations in SEC against
10873 symbols defined in discarded sections. If this function returns
10874 COMPLAIN set, ld will issue a warning message. If this function
10875 returns PRETEND set, and the discarded section was link-once and the
10876 same size as the kept link-once section, ld will pretend that the
10877 symbol was actually defined in the kept section. Otherwise ld will
10878 zero the reloc (at least that is the intent, but some cooperation by
10879 the target dependent code is needed, particularly for REL targets). */
10882 _bfd_elf_default_action_discarded (asection
*sec
)
10884 if (sec
->flags
& SEC_DEBUGGING
)
10887 if (strcmp (".eh_frame", sec
->name
) == 0)
10890 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10893 return COMPLAIN
| PRETEND
;
10896 /* Find a match between a section and a member of a section group. */
10899 match_group_member (asection
*sec
, asection
*group
,
10900 struct bfd_link_info
*info
)
10902 asection
*first
= elf_next_in_group (group
);
10903 asection
*s
= first
;
10907 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10910 s
= elf_next_in_group (s
);
10918 /* Check if the kept section of a discarded section SEC can be used
10919 to replace it. Return the replacement if it is OK. Otherwise return
10923 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10927 kept
= sec
->kept_section
;
10930 if ((kept
->flags
& SEC_GROUP
) != 0)
10931 kept
= match_group_member (sec
, kept
, info
);
10934 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10935 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
10939 /* Get the real kept section. */
10941 for (next
= kept
->kept_section
;
10943 next
= next
->kept_section
)
10947 sec
->kept_section
= kept
;
10952 /* Link an input file into the linker output file. This function
10953 handles all the sections and relocations of the input file at once.
10954 This is so that we only have to read the local symbols once, and
10955 don't have to keep them in memory. */
10958 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10960 int (*relocate_section
)
10961 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10962 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10964 Elf_Internal_Shdr
*symtab_hdr
;
10965 size_t locsymcount
;
10967 Elf_Internal_Sym
*isymbuf
;
10968 Elf_Internal_Sym
*isym
;
10969 Elf_Internal_Sym
*isymend
;
10971 asection
**ppsection
;
10973 const struct elf_backend_data
*bed
;
10974 struct elf_link_hash_entry
**sym_hashes
;
10975 bfd_size_type address_size
;
10976 bfd_vma r_type_mask
;
10978 bool have_file_sym
= false;
10980 output_bfd
= flinfo
->output_bfd
;
10981 bed
= get_elf_backend_data (output_bfd
);
10982 relocate_section
= bed
->elf_backend_relocate_section
;
10984 /* If this is a dynamic object, we don't want to do anything here:
10985 we don't want the local symbols, and we don't want the section
10987 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10990 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10991 if (elf_bad_symtab (input_bfd
))
10993 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10998 locsymcount
= symtab_hdr
->sh_info
;
10999 extsymoff
= symtab_hdr
->sh_info
;
11002 /* Enable GNU OSABI features in the output BFD that are used in the input
11004 if (bed
->elf_osabi
== ELFOSABI_NONE
11005 || bed
->elf_osabi
== ELFOSABI_GNU
11006 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
11007 elf_tdata (output_bfd
)->has_gnu_osabi
11008 |= (elf_tdata (input_bfd
)->has_gnu_osabi
11009 & (bfd_link_relocatable (flinfo
->info
)
11010 ? -1 : ~elf_gnu_osabi_retain
));
11012 /* Read the local symbols. */
11013 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11014 if (isymbuf
== NULL
&& locsymcount
!= 0)
11016 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
11017 flinfo
->internal_syms
,
11018 flinfo
->external_syms
,
11019 flinfo
->locsym_shndx
);
11020 if (isymbuf
== NULL
)
11024 /* Find local symbol sections and adjust values of symbols in
11025 SEC_MERGE sections. Write out those local symbols we know are
11026 going into the output file. */
11027 isymend
= PTR_ADD (isymbuf
, locsymcount
);
11028 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
11030 isym
++, pindex
++, ppsection
++)
11034 Elf_Internal_Sym osym
;
11040 if (elf_bad_symtab (input_bfd
))
11042 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
11049 if (isym
->st_shndx
== SHN_UNDEF
)
11050 isec
= bfd_und_section_ptr
;
11051 else if (isym
->st_shndx
== SHN_ABS
)
11052 isec
= bfd_abs_section_ptr
;
11053 else if (isym
->st_shndx
== SHN_COMMON
)
11054 isec
= bfd_com_section_ptr
;
11057 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
11060 /* Don't attempt to output symbols with st_shnx in the
11061 reserved range other than SHN_ABS and SHN_COMMON. */
11062 isec
= bfd_und_section_ptr
;
11064 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
11065 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
11067 _bfd_merged_section_offset (output_bfd
, &isec
,
11068 elf_section_data (isec
)->sec_info
,
11074 /* Don't output the first, undefined, symbol. In fact, don't
11075 output any undefined local symbol. */
11076 if (isec
== bfd_und_section_ptr
)
11079 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
11081 /* We never output section symbols. Instead, we use the
11082 section symbol of the corresponding section in the output
11087 /* If we are stripping all symbols, we don't want to output this
11089 if (flinfo
->info
->strip
== strip_all
)
11092 /* If we are discarding all local symbols, we don't want to
11093 output this one. If we are generating a relocatable output
11094 file, then some of the local symbols may be required by
11095 relocs; we output them below as we discover that they are
11097 if (flinfo
->info
->discard
== discard_all
)
11100 /* If this symbol is defined in a section which we are
11101 discarding, we don't need to keep it. */
11102 if (isym
->st_shndx
!= SHN_UNDEF
11103 && isym
->st_shndx
< SHN_LORESERVE
11104 && isec
->output_section
== NULL
11105 && flinfo
->info
->non_contiguous_regions
11106 && flinfo
->info
->non_contiguous_regions_warnings
)
11108 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
11109 "discards section `%s' from '%s'\n"),
11110 isec
->name
, bfd_get_filename (isec
->owner
));
11114 if (isym
->st_shndx
!= SHN_UNDEF
11115 && isym
->st_shndx
< SHN_LORESERVE
11116 && bfd_section_removed_from_list (output_bfd
,
11117 isec
->output_section
))
11120 /* Get the name of the symbol. */
11121 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
11126 /* See if we are discarding symbols with this name. */
11127 if ((flinfo
->info
->strip
== strip_some
11128 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, false, false)
11130 || (((flinfo
->info
->discard
== discard_sec_merge
11131 && (isec
->flags
& SEC_MERGE
)
11132 && !bfd_link_relocatable (flinfo
->info
))
11133 || flinfo
->info
->discard
== discard_l
)
11134 && bfd_is_local_label_name (input_bfd
, name
)))
11137 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
11139 if (input_bfd
->lto_output
)
11140 /* -flto puts a temp file name here. This means builds
11141 are not reproducible. Discard the symbol. */
11143 have_file_sym
= true;
11144 flinfo
->filesym_count
+= 1;
11146 if (!have_file_sym
)
11148 /* In the absence of debug info, bfd_find_nearest_line uses
11149 FILE symbols to determine the source file for local
11150 function symbols. Provide a FILE symbol here if input
11151 files lack such, so that their symbols won't be
11152 associated with a previous input file. It's not the
11153 source file, but the best we can do. */
11154 const char *filename
;
11155 have_file_sym
= true;
11156 flinfo
->filesym_count
+= 1;
11157 memset (&osym
, 0, sizeof (osym
));
11158 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11159 osym
.st_shndx
= SHN_ABS
;
11160 if (input_bfd
->lto_output
)
11163 filename
= lbasename (bfd_get_filename (input_bfd
));
11164 if (!elf_link_output_symstrtab (flinfo
, filename
, &osym
,
11165 bfd_abs_section_ptr
, NULL
))
11171 /* Adjust the section index for the output file. */
11172 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11173 isec
->output_section
);
11174 if (osym
.st_shndx
== SHN_BAD
)
11177 /* ELF symbols in relocatable files are section relative, but
11178 in executable files they are virtual addresses. Note that
11179 this code assumes that all ELF sections have an associated
11180 BFD section with a reasonable value for output_offset; below
11181 we assume that they also have a reasonable value for
11182 output_section. Any special sections must be set up to meet
11183 these requirements. */
11184 osym
.st_value
+= isec
->output_offset
;
11185 if (!bfd_link_relocatable (flinfo
->info
))
11187 osym
.st_value
+= isec
->output_section
->vma
;
11188 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
11190 /* STT_TLS symbols are relative to PT_TLS segment base. */
11191 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
11192 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
11194 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
11199 indx
= bfd_get_symcount (output_bfd
);
11200 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11207 if (bed
->s
->arch_size
== 32)
11209 r_type_mask
= 0xff;
11215 r_type_mask
= 0xffffffff;
11220 /* Relocate the contents of each section. */
11221 sym_hashes
= elf_sym_hashes (input_bfd
);
11222 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11224 bfd_byte
*contents
;
11226 if (! o
->linker_mark
)
11228 /* This section was omitted from the link. */
11232 if (!flinfo
->info
->resolve_section_groups
11233 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11235 /* Deal with the group signature symbol. */
11236 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11237 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11238 asection
*osec
= o
->output_section
;
11240 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11241 if (symndx
>= locsymcount
11242 || (elf_bad_symtab (input_bfd
)
11243 && flinfo
->sections
[symndx
] == NULL
))
11245 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
11246 while (h
->root
.type
== bfd_link_hash_indirect
11247 || h
->root
.type
== bfd_link_hash_warning
)
11248 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11249 /* Arrange for symbol to be output. */
11251 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11253 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11255 /* We'll use the output section target_index. */
11256 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11257 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11261 if (flinfo
->indices
[symndx
] == -1)
11263 /* Otherwise output the local symbol now. */
11264 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11265 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11270 name
= bfd_elf_string_from_elf_section (input_bfd
,
11271 symtab_hdr
->sh_link
,
11276 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11278 if (sym
.st_shndx
== SHN_BAD
)
11281 sym
.st_value
+= o
->output_offset
;
11283 indx
= bfd_get_symcount (output_bfd
);
11284 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11289 flinfo
->indices
[symndx
] = indx
;
11293 elf_section_data (osec
)->this_hdr
.sh_info
11294 = flinfo
->indices
[symndx
];
11298 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11299 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11302 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11304 /* Section was created by _bfd_elf_link_create_dynamic_sections
11309 /* Get the contents of the section. They have been cached by a
11310 relaxation routine. Note that o is a section in an input
11311 file, so the contents field will not have been set by any of
11312 the routines which work on output files. */
11313 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11315 contents
= elf_section_data (o
)->this_hdr
.contents
;
11316 if (bed
->caches_rawsize
11318 && o
->rawsize
< o
->size
)
11320 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11321 contents
= flinfo
->contents
;
11326 contents
= flinfo
->contents
;
11327 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
11331 if ((o
->flags
& SEC_RELOC
) != 0)
11333 Elf_Internal_Rela
*internal_relocs
;
11334 Elf_Internal_Rela
*rel
, *relend
;
11335 int action_discarded
;
11338 /* Get the swapped relocs. */
11340 = _bfd_elf_link_info_read_relocs (input_bfd
, flinfo
->info
, o
,
11341 flinfo
->external_relocs
,
11342 flinfo
->internal_relocs
,
11344 if (internal_relocs
== NULL
11345 && o
->reloc_count
> 0)
11348 action_discarded
= -1;
11349 if (!elf_section_ignore_discarded_relocs (o
))
11350 action_discarded
= (*bed
->action_discarded
) (o
);
11352 /* Run through the relocs evaluating complex reloc symbols and
11353 looking for relocs against symbols from discarded sections
11354 or section symbols from removed link-once sections.
11355 Complain about relocs against discarded sections. Zero
11356 relocs against removed link-once sections. */
11358 rel
= internal_relocs
;
11359 relend
= rel
+ o
->reloc_count
;
11360 for ( ; rel
< relend
; rel
++)
11362 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11363 unsigned int s_type
;
11364 asection
**ps
, *sec
;
11365 struct elf_link_hash_entry
*h
= NULL
;
11366 const char *sym_name
;
11368 if (r_symndx
== STN_UNDEF
)
11371 if (r_symndx
>= locsymcount
11372 || (elf_bad_symtab (input_bfd
)
11373 && flinfo
->sections
[r_symndx
] == NULL
))
11375 h
= sym_hashes
[r_symndx
- extsymoff
];
11377 /* Badly formatted input files can contain relocs that
11378 reference non-existant symbols. Check here so that
11379 we do not seg fault. */
11383 /* xgettext:c-format */
11384 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11385 "that references a non-existent global symbol"),
11386 input_bfd
, (uint64_t) rel
->r_info
, o
);
11387 bfd_set_error (bfd_error_bad_value
);
11391 while (h
->root
.type
== bfd_link_hash_indirect
11392 || h
->root
.type
== bfd_link_hash_warning
)
11393 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11397 /* If a plugin symbol is referenced from a non-IR file,
11398 mark the symbol as undefined. Note that the
11399 linker may attach linker created dynamic sections
11400 to the plugin bfd. Symbols defined in linker
11401 created sections are not plugin symbols. */
11402 if ((h
->root
.non_ir_ref_regular
11403 || h
->root
.non_ir_ref_dynamic
)
11404 && (h
->root
.type
== bfd_link_hash_defined
11405 || h
->root
.type
== bfd_link_hash_defweak
)
11406 && (h
->root
.u
.def
.section
->flags
11407 & SEC_LINKER_CREATED
) == 0
11408 && h
->root
.u
.def
.section
->owner
!= NULL
11409 && (h
->root
.u
.def
.section
->owner
->flags
11410 & BFD_PLUGIN
) != 0)
11412 h
->root
.type
= bfd_link_hash_undefined
;
11413 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11417 if (h
->root
.type
== bfd_link_hash_defined
11418 || h
->root
.type
== bfd_link_hash_defweak
)
11419 ps
= &h
->root
.u
.def
.section
;
11421 sym_name
= h
->root
.root
.string
;
11425 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11427 s_type
= ELF_ST_TYPE (sym
->st_info
);
11428 ps
= &flinfo
->sections
[r_symndx
];
11429 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11433 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11434 && !bfd_link_relocatable (flinfo
->info
))
11437 bfd_vma dot
= (rel
->r_offset
11438 + o
->output_offset
+ o
->output_section
->vma
);
11440 printf ("Encountered a complex symbol!");
11441 printf (" (input_bfd %s, section %s, reloc %ld\n",
11442 bfd_get_filename (input_bfd
), o
->name
,
11443 (long) (rel
- internal_relocs
));
11444 printf (" symbol: idx %8.8lx, name %s\n",
11445 r_symndx
, sym_name
);
11446 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11447 (unsigned long) rel
->r_info
,
11448 (unsigned long) rel
->r_offset
);
11450 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11451 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11454 /* Symbol evaluated OK. Update to absolute value. */
11455 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11460 if (action_discarded
!= -1 && ps
!= NULL
)
11462 /* Complain if the definition comes from a
11463 discarded section. */
11464 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11466 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11467 if (action_discarded
& COMPLAIN
)
11468 (*flinfo
->info
->callbacks
->einfo
)
11469 /* xgettext:c-format */
11470 (_("%X`%s' referenced in section `%pA' of %pB: "
11471 "defined in discarded section `%pA' of %pB\n"),
11472 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11474 /* Try to do the best we can to support buggy old
11475 versions of gcc. Pretend that the symbol is
11476 really defined in the kept linkonce section.
11477 FIXME: This is quite broken. Modifying the
11478 symbol here means we will be changing all later
11479 uses of the symbol, not just in this section. */
11480 if (action_discarded
& PRETEND
)
11484 kept
= _bfd_elf_check_kept_section (sec
,
11496 /* Relocate the section by invoking a back end routine.
11498 The back end routine is responsible for adjusting the
11499 section contents as necessary, and (if using Rela relocs
11500 and generating a relocatable output file) adjusting the
11501 reloc addend as necessary.
11503 The back end routine does not have to worry about setting
11504 the reloc address or the reloc symbol index.
11506 The back end routine is given a pointer to the swapped in
11507 internal symbols, and can access the hash table entries
11508 for the external symbols via elf_sym_hashes (input_bfd).
11510 When generating relocatable output, the back end routine
11511 must handle STB_LOCAL/STT_SECTION symbols specially. The
11512 output symbol is going to be a section symbol
11513 corresponding to the output section, which will require
11514 the addend to be adjusted. */
11516 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11517 input_bfd
, o
, contents
,
11525 || bfd_link_relocatable (flinfo
->info
)
11526 || flinfo
->info
->emitrelocations
)
11528 Elf_Internal_Rela
*irela
;
11529 Elf_Internal_Rela
*irelaend
, *irelamid
;
11530 bfd_vma last_offset
;
11531 struct elf_link_hash_entry
**rel_hash
;
11532 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11533 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11534 unsigned int next_erel
;
11536 struct bfd_elf_section_data
*esdi
, *esdo
;
11538 esdi
= elf_section_data (o
);
11539 esdo
= elf_section_data (o
->output_section
);
11540 rela_normal
= false;
11542 /* Adjust the reloc addresses and symbol indices. */
11544 irela
= internal_relocs
;
11545 irelaend
= irela
+ o
->reloc_count
;
11546 rel_hash
= PTR_ADD (esdo
->rel
.hashes
, esdo
->rel
.count
);
11547 /* We start processing the REL relocs, if any. When we reach
11548 IRELAMID in the loop, we switch to the RELA relocs. */
11550 if (esdi
->rel
.hdr
!= NULL
)
11551 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11552 * bed
->s
->int_rels_per_ext_rel
);
11553 rel_hash_list
= rel_hash
;
11554 rela_hash_list
= NULL
;
11555 last_offset
= o
->output_offset
;
11556 if (!bfd_link_relocatable (flinfo
->info
))
11557 last_offset
+= o
->output_section
->vma
;
11558 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11560 unsigned long r_symndx
;
11562 Elf_Internal_Sym sym
;
11564 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11570 if (irela
== irelamid
)
11572 rel_hash
= PTR_ADD (esdo
->rela
.hashes
, esdo
->rela
.count
);
11573 rela_hash_list
= rel_hash
;
11574 rela_normal
= bed
->rela_normal
;
11577 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11580 if (irela
->r_offset
>= (bfd_vma
) -2)
11582 /* This is a reloc for a deleted entry or somesuch.
11583 Turn it into an R_*_NONE reloc, at the same
11584 offset as the last reloc. elf_eh_frame.c and
11585 bfd_elf_discard_info rely on reloc offsets
11587 irela
->r_offset
= last_offset
;
11589 irela
->r_addend
= 0;
11593 irela
->r_offset
+= o
->output_offset
;
11595 /* Relocs in an executable have to be virtual addresses. */
11596 if (!bfd_link_relocatable (flinfo
->info
))
11597 irela
->r_offset
+= o
->output_section
->vma
;
11599 last_offset
= irela
->r_offset
;
11601 r_symndx
= irela
->r_info
>> r_sym_shift
;
11602 if (r_symndx
== STN_UNDEF
)
11605 if (r_symndx
>= locsymcount
11606 || (elf_bad_symtab (input_bfd
)
11607 && flinfo
->sections
[r_symndx
] == NULL
))
11609 struct elf_link_hash_entry
*rh
;
11610 unsigned long indx
;
11612 /* This is a reloc against a global symbol. We
11613 have not yet output all the local symbols, so
11614 we do not know the symbol index of any global
11615 symbol. We set the rel_hash entry for this
11616 reloc to point to the global hash table entry
11617 for this symbol. The symbol index is then
11618 set at the end of bfd_elf_final_link. */
11619 indx
= r_symndx
- extsymoff
;
11620 rh
= elf_sym_hashes (input_bfd
)[indx
];
11621 while (rh
->root
.type
== bfd_link_hash_indirect
11622 || rh
->root
.type
== bfd_link_hash_warning
)
11623 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11625 /* Setting the index to -2 tells
11626 elf_link_output_extsym that this symbol is
11627 used by a reloc. */
11628 BFD_ASSERT (rh
->indx
< 0);
11635 /* This is a reloc against a local symbol. */
11638 sym
= isymbuf
[r_symndx
];
11639 sec
= flinfo
->sections
[r_symndx
];
11640 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11642 /* I suppose the backend ought to fill in the
11643 section of any STT_SECTION symbol against a
11644 processor specific section. */
11645 r_symndx
= STN_UNDEF
;
11646 if (bfd_is_abs_section (sec
))
11648 else if (sec
== NULL
|| sec
->owner
== NULL
)
11650 bfd_set_error (bfd_error_bad_value
);
11655 asection
*osec
= sec
->output_section
;
11657 /* If we have discarded a section, the output
11658 section will be the absolute section. In
11659 case of discarded SEC_MERGE sections, use
11660 the kept section. relocate_section should
11661 have already handled discarded linkonce
11663 if (bfd_is_abs_section (osec
)
11664 && sec
->kept_section
!= NULL
11665 && sec
->kept_section
->output_section
!= NULL
)
11667 osec
= sec
->kept_section
->output_section
;
11668 irela
->r_addend
-= osec
->vma
;
11671 if (!bfd_is_abs_section (osec
))
11673 r_symndx
= osec
->target_index
;
11674 if (r_symndx
== STN_UNDEF
)
11676 irela
->r_addend
+= osec
->vma
;
11677 osec
= _bfd_nearby_section (output_bfd
, osec
,
11679 irela
->r_addend
-= osec
->vma
;
11680 r_symndx
= osec
->target_index
;
11685 /* Adjust the addend according to where the
11686 section winds up in the output section. */
11688 irela
->r_addend
+= sec
->output_offset
;
11692 if (flinfo
->indices
[r_symndx
] == -1)
11694 unsigned long shlink
;
11699 if (flinfo
->info
->strip
== strip_all
)
11701 /* You can't do ld -r -s. */
11702 bfd_set_error (bfd_error_invalid_operation
);
11706 /* This symbol was skipped earlier, but
11707 since it is needed by a reloc, we
11708 must output it now. */
11709 shlink
= symtab_hdr
->sh_link
;
11710 name
= (bfd_elf_string_from_elf_section
11711 (input_bfd
, shlink
, sym
.st_name
));
11715 osec
= sec
->output_section
;
11717 _bfd_elf_section_from_bfd_section (output_bfd
,
11719 if (sym
.st_shndx
== SHN_BAD
)
11722 sym
.st_value
+= sec
->output_offset
;
11723 if (!bfd_link_relocatable (flinfo
->info
))
11725 sym
.st_value
+= osec
->vma
;
11726 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11728 struct elf_link_hash_table
*htab
11729 = elf_hash_table (flinfo
->info
);
11731 /* STT_TLS symbols are relative to PT_TLS
11733 if (htab
->tls_sec
!= NULL
)
11734 sym
.st_value
-= htab
->tls_sec
->vma
;
11737 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11742 indx
= bfd_get_symcount (output_bfd
);
11743 ret
= elf_link_output_symstrtab (flinfo
, name
,
11749 flinfo
->indices
[r_symndx
] = indx
;
11754 r_symndx
= flinfo
->indices
[r_symndx
];
11757 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11758 | (irela
->r_info
& r_type_mask
));
11761 /* Swap out the relocs. */
11762 input_rel_hdr
= esdi
->rel
.hdr
;
11763 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11765 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11770 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11771 * bed
->s
->int_rels_per_ext_rel
);
11772 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11775 input_rela_hdr
= esdi
->rela
.hdr
;
11776 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11778 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11787 /* Write out the modified section contents. */
11788 if (bed
->elf_backend_write_section
11789 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11792 /* Section written out. */
11794 else switch (o
->sec_info_type
)
11796 case SEC_INFO_TYPE_STABS
:
11797 if (! (_bfd_write_section_stabs
11799 &elf_hash_table (flinfo
->info
)->stab_info
,
11800 o
, &elf_section_data (o
)->sec_info
, contents
)))
11803 case SEC_INFO_TYPE_MERGE
:
11804 if (! _bfd_write_merged_section (output_bfd
, o
,
11805 elf_section_data (o
)->sec_info
))
11808 case SEC_INFO_TYPE_EH_FRAME
:
11810 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11815 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11817 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11825 if (! (o
->flags
& SEC_EXCLUDE
))
11827 file_ptr offset
= (file_ptr
) o
->output_offset
;
11828 bfd_size_type todo
= o
->size
;
11830 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11832 if ((o
->flags
& SEC_ELF_REVERSE_COPY
)
11833 && o
->size
> address_size
)
11835 /* Reverse-copy input section to output. */
11837 if ((o
->size
& (address_size
- 1)) != 0
11838 || (o
->reloc_count
!= 0
11839 && (o
->size
* bed
->s
->int_rels_per_ext_rel
11840 != o
->reloc_count
* address_size
)))
11843 /* xgettext:c-format */
11844 (_("error: %pB: size of section %pA is not "
11845 "multiple of address size"),
11847 bfd_set_error (bfd_error_bad_value
);
11853 todo
-= address_size
;
11854 if (! bfd_set_section_contents (output_bfd
,
11862 offset
+= address_size
;
11866 else if (! bfd_set_section_contents (output_bfd
,
11880 /* Generate a reloc when linking an ELF file. This is a reloc
11881 requested by the linker, and does not come from any input file. This
11882 is used to build constructor and destructor tables when linking
11886 elf_reloc_link_order (bfd
*output_bfd
,
11887 struct bfd_link_info
*info
,
11888 asection
*output_section
,
11889 struct bfd_link_order
*link_order
)
11891 reloc_howto_type
*howto
;
11895 struct bfd_elf_section_reloc_data
*reldata
;
11896 struct elf_link_hash_entry
**rel_hash_ptr
;
11897 Elf_Internal_Shdr
*rel_hdr
;
11898 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11899 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11902 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11904 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11907 bfd_set_error (bfd_error_bad_value
);
11911 addend
= link_order
->u
.reloc
.p
->addend
;
11914 reldata
= &esdo
->rel
;
11915 else if (esdo
->rela
.hdr
)
11916 reldata
= &esdo
->rela
;
11923 /* Figure out the symbol index. */
11924 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11925 if (link_order
->type
== bfd_section_reloc_link_order
)
11927 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11928 BFD_ASSERT (indx
!= 0);
11929 *rel_hash_ptr
= NULL
;
11933 struct elf_link_hash_entry
*h
;
11935 /* Treat a reloc against a defined symbol as though it were
11936 actually against the section. */
11937 h
= ((struct elf_link_hash_entry
*)
11938 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11939 link_order
->u
.reloc
.p
->u
.name
,
11940 false, false, true));
11942 && (h
->root
.type
== bfd_link_hash_defined
11943 || h
->root
.type
== bfd_link_hash_defweak
))
11947 section
= h
->root
.u
.def
.section
;
11948 indx
= section
->output_section
->target_index
;
11949 *rel_hash_ptr
= NULL
;
11950 /* It seems that we ought to add the symbol value to the
11951 addend here, but in practice it has already been added
11952 because it was passed to constructor_callback. */
11953 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11955 else if (h
!= NULL
)
11957 /* Setting the index to -2 tells elf_link_output_extsym that
11958 this symbol is used by a reloc. */
11965 (*info
->callbacks
->unattached_reloc
)
11966 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11971 /* If this is an inplace reloc, we must write the addend into the
11973 if (howto
->partial_inplace
&& addend
!= 0)
11975 bfd_size_type size
;
11976 bfd_reloc_status_type rstat
;
11979 const char *sym_name
;
11980 bfd_size_type octets
;
11982 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11983 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11984 if (buf
== NULL
&& size
!= 0)
11986 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11993 case bfd_reloc_outofrange
:
11996 case bfd_reloc_overflow
:
11997 if (link_order
->type
== bfd_section_reloc_link_order
)
11998 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
12000 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
12001 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
12002 howto
->name
, addend
, NULL
, NULL
,
12007 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
12009 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
12016 /* The address of a reloc is relative to the section in a
12017 relocatable file, and is a virtual address in an executable
12019 offset
= link_order
->offset
;
12020 if (! bfd_link_relocatable (info
))
12021 offset
+= output_section
->vma
;
12023 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
12025 irel
[i
].r_offset
= offset
;
12026 irel
[i
].r_info
= 0;
12027 irel
[i
].r_addend
= 0;
12029 if (bed
->s
->arch_size
== 32)
12030 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
12032 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
12034 rel_hdr
= reldata
->hdr
;
12035 erel
= rel_hdr
->contents
;
12036 if (rel_hdr
->sh_type
== SHT_REL
)
12038 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
12039 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
12043 irel
[0].r_addend
= addend
;
12044 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
12045 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
12053 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12054 Returns TRUE upon success, FALSE otherwise. */
12057 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
12061 const struct elf_backend_data
*bed
;
12063 enum bfd_architecture arch
;
12065 asymbol
**sympp
= NULL
;
12069 elf_symbol_type
*osymbuf
;
12072 implib_bfd
= info
->out_implib_bfd
;
12073 bed
= get_elf_backend_data (abfd
);
12075 if (!bfd_set_format (implib_bfd
, bfd_object
))
12078 /* Use flag from executable but make it a relocatable object. */
12079 flags
= bfd_get_file_flags (abfd
);
12080 flags
&= ~HAS_RELOC
;
12081 if (!bfd_set_start_address (implib_bfd
, 0)
12082 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
12085 /* Copy architecture of output file to import library file. */
12086 arch
= bfd_get_arch (abfd
);
12087 mach
= bfd_get_mach (abfd
);
12088 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
12089 && (abfd
->target_defaulted
12090 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
12093 /* Get symbol table size. */
12094 symsize
= bfd_get_symtab_upper_bound (abfd
);
12098 /* Read in the symbol table. */
12099 sympp
= (asymbol
**) bfd_malloc (symsize
);
12103 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
12107 /* Allow the BFD backend to copy any private header data it
12108 understands from the output BFD to the import library BFD. */
12109 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
12112 /* Filter symbols to appear in the import library. */
12113 if (bed
->elf_backend_filter_implib_symbols
)
12114 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
12117 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
12120 bfd_set_error (bfd_error_no_symbols
);
12121 _bfd_error_handler (_("%pB: no symbol found for import library"),
12127 /* Make symbols absolute. */
12128 amt
= symcount
* sizeof (*osymbuf
);
12129 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
12130 if (osymbuf
== NULL
)
12133 for (src_count
= 0; src_count
< symcount
; src_count
++)
12135 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
12136 sizeof (*osymbuf
));
12137 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
12138 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
12139 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
12140 osymbuf
[src_count
].internal_elf_sym
.st_value
=
12141 osymbuf
[src_count
].symbol
.value
;
12142 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
12145 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
12147 /* Allow the BFD backend to copy any private data it understands
12148 from the output BFD to the import library BFD. This is done last
12149 to permit the routine to look at the filtered symbol table. */
12150 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
12153 if (!bfd_close (implib_bfd
))
12164 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
12168 if (flinfo
->symstrtab
!= NULL
)
12169 _bfd_elf_strtab_free (flinfo
->symstrtab
);
12170 free (flinfo
->contents
);
12171 free (flinfo
->external_relocs
);
12172 free (flinfo
->internal_relocs
);
12173 free (flinfo
->external_syms
);
12174 free (flinfo
->locsym_shndx
);
12175 free (flinfo
->internal_syms
);
12176 free (flinfo
->indices
);
12177 free (flinfo
->sections
);
12178 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12179 free (flinfo
->symshndxbuf
);
12180 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12182 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12183 free (esdo
->rel
.hashes
);
12184 free (esdo
->rela
.hashes
);
12188 /* Do the final step of an ELF link. */
12191 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12196 struct elf_final_link_info flinfo
;
12198 struct bfd_link_order
*p
;
12200 bfd_size_type max_contents_size
;
12201 bfd_size_type max_external_reloc_size
;
12202 bfd_size_type max_internal_reloc_count
;
12203 bfd_size_type max_sym_count
;
12204 bfd_size_type max_sym_shndx_count
;
12205 Elf_Internal_Sym elfsym
;
12207 Elf_Internal_Shdr
*symtab_hdr
;
12208 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12209 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12210 struct elf_outext_info eoinfo
;
12212 size_t relativecount
;
12213 size_t relr_entsize
;
12214 asection
*reldyn
= 0;
12216 asection
*attr_section
= NULL
;
12217 bfd_vma attr_size
= 0;
12218 const char *std_attrs_section
;
12219 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12220 bool sections_removed
;
12223 if (!is_elf_hash_table (&htab
->root
))
12226 if (bfd_link_pic (info
))
12227 abfd
->flags
|= DYNAMIC
;
12229 dynamic
= htab
->dynamic_sections_created
;
12230 dynobj
= htab
->dynobj
;
12232 emit_relocs
= (bfd_link_relocatable (info
)
12233 || info
->emitrelocations
);
12235 memset (&flinfo
, 0, sizeof (flinfo
));
12236 flinfo
.info
= info
;
12237 flinfo
.output_bfd
= abfd
;
12238 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12239 if (flinfo
.symstrtab
== NULL
)
12244 flinfo
.hash_sec
= NULL
;
12245 flinfo
.symver_sec
= NULL
;
12249 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12250 /* Note that dynsym_sec can be NULL (on VMS). */
12251 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12252 /* Note that it is OK if symver_sec is NULL. */
12255 if (info
->unique_symbol
12256 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12257 local_hash_newfunc
,
12258 sizeof (struct local_hash_entry
)))
12261 /* The object attributes have been merged. Remove the input
12262 sections from the link, and set the contents of the output
12264 sections_removed
= false;
12265 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12266 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12268 bool remove_section
= false;
12270 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12271 || strcmp (o
->name
, ".gnu.attributes") == 0)
12273 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12275 asection
*input_section
;
12277 if (p
->type
!= bfd_indirect_link_order
)
12279 input_section
= p
->u
.indirect
.section
;
12280 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12281 elf_link_input_bfd ignores this section. */
12282 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12285 attr_size
= bfd_elf_obj_attr_size (abfd
);
12286 bfd_set_section_size (o
, attr_size
);
12287 /* Skip this section later on. */
12288 o
->map_head
.link_order
= NULL
;
12292 remove_section
= true;
12294 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12296 /* Remove empty group section from linker output. */
12297 remove_section
= true;
12299 if (remove_section
)
12301 o
->flags
|= SEC_EXCLUDE
;
12302 bfd_section_list_remove (abfd
, o
);
12303 abfd
->section_count
--;
12304 sections_removed
= true;
12307 if (sections_removed
)
12308 _bfd_fix_excluded_sec_syms (abfd
, info
);
12310 /* Count up the number of relocations we will output for each output
12311 section, so that we know the sizes of the reloc sections. We
12312 also figure out some maximum sizes. */
12313 max_contents_size
= 0;
12314 max_external_reloc_size
= 0;
12315 max_internal_reloc_count
= 0;
12317 max_sym_shndx_count
= 0;
12319 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12321 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12322 o
->reloc_count
= 0;
12324 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12326 unsigned int reloc_count
= 0;
12327 unsigned int additional_reloc_count
= 0;
12328 struct bfd_elf_section_data
*esdi
= NULL
;
12330 if (p
->type
== bfd_section_reloc_link_order
12331 || p
->type
== bfd_symbol_reloc_link_order
)
12333 else if (p
->type
== bfd_indirect_link_order
)
12337 sec
= p
->u
.indirect
.section
;
12339 /* Mark all sections which are to be included in the
12340 link. This will normally be every section. We need
12341 to do this so that we can identify any sections which
12342 the linker has decided to not include. */
12343 sec
->linker_mark
= true;
12345 if (sec
->flags
& SEC_MERGE
)
12348 if (sec
->rawsize
> max_contents_size
)
12349 max_contents_size
= sec
->rawsize
;
12350 if (sec
->size
> max_contents_size
)
12351 max_contents_size
= sec
->size
;
12353 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12354 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12358 /* We are interested in just local symbols, not all
12360 if (elf_bad_symtab (sec
->owner
))
12361 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12362 / bed
->s
->sizeof_sym
);
12364 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12366 if (sym_count
> max_sym_count
)
12367 max_sym_count
= sym_count
;
12369 if (sym_count
> max_sym_shndx_count
12370 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12371 max_sym_shndx_count
= sym_count
;
12373 if (esdo
->this_hdr
.sh_type
== SHT_REL
12374 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
12375 /* Some backends use reloc_count in relocation sections
12376 to count particular types of relocs. Of course,
12377 reloc sections themselves can't have relocations. */
12379 else if (emit_relocs
)
12381 reloc_count
= sec
->reloc_count
;
12382 if (bed
->elf_backend_count_additional_relocs
)
12385 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12386 additional_reloc_count
+= c
;
12389 else if (bed
->elf_backend_count_relocs
)
12390 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12392 esdi
= elf_section_data (sec
);
12394 if ((sec
->flags
& SEC_RELOC
) != 0)
12396 size_t ext_size
= 0;
12398 if (esdi
->rel
.hdr
!= NULL
)
12399 ext_size
= esdi
->rel
.hdr
->sh_size
;
12400 if (esdi
->rela
.hdr
!= NULL
)
12401 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12403 if (ext_size
> max_external_reloc_size
)
12404 max_external_reloc_size
= ext_size
;
12405 if (sec
->reloc_count
> max_internal_reloc_count
)
12406 max_internal_reloc_count
= sec
->reloc_count
;
12411 if (reloc_count
== 0)
12414 reloc_count
+= additional_reloc_count
;
12415 o
->reloc_count
+= reloc_count
;
12417 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12421 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12422 esdo
->rel
.count
+= additional_reloc_count
;
12424 if (esdi
->rela
.hdr
)
12426 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12427 esdo
->rela
.count
+= additional_reloc_count
;
12433 esdo
->rela
.count
+= reloc_count
;
12435 esdo
->rel
.count
+= reloc_count
;
12439 if (o
->reloc_count
> 0)
12440 o
->flags
|= SEC_RELOC
;
12443 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12444 set it (this is probably a bug) and if it is set
12445 assign_section_numbers will create a reloc section. */
12446 o
->flags
&=~ SEC_RELOC
;
12449 /* If the SEC_ALLOC flag is not set, force the section VMA to
12450 zero. This is done in elf_fake_sections as well, but forcing
12451 the VMA to 0 here will ensure that relocs against these
12452 sections are handled correctly. */
12453 if ((o
->flags
& SEC_ALLOC
) == 0
12454 && ! o
->user_set_vma
)
12458 if (! bfd_link_relocatable (info
) && merged
)
12459 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12461 /* Figure out the file positions for everything but the symbol table
12462 and the relocs. We set symcount to force assign_section_numbers
12463 to create a symbol table. */
12464 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12465 BFD_ASSERT (! abfd
->output_has_begun
);
12466 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12469 /* Set sizes, and assign file positions for reloc sections. */
12470 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12472 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12473 if ((o
->flags
& SEC_RELOC
) != 0)
12476 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12480 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12484 /* _bfd_elf_compute_section_file_positions makes temporary use
12485 of target_index. Reset it. */
12486 o
->target_index
= 0;
12488 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12489 to count upwards while actually outputting the relocations. */
12490 esdo
->rel
.count
= 0;
12491 esdo
->rela
.count
= 0;
12493 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12494 && !bfd_section_is_ctf (o
))
12496 /* Cache the section contents so that they can be compressed
12497 later. Use bfd_malloc since it will be freed by
12498 bfd_compress_section_contents. */
12499 unsigned char *contents
= esdo
->this_hdr
.contents
;
12500 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
12503 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12504 if (contents
== NULL
)
12506 esdo
->this_hdr
.contents
= contents
;
12510 /* We have now assigned file positions for all the sections except .symtab,
12511 .strtab, and non-loaded reloc and compressed debugging sections. We start
12512 the .symtab section at the current file position, and write directly to it.
12513 We build the .strtab section in memory. */
12514 abfd
->symcount
= 0;
12515 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12516 /* sh_name is set in prep_headers. */
12517 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12518 /* sh_flags, sh_addr and sh_size all start off zero. */
12519 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12520 /* sh_link is set in assign_section_numbers. */
12521 /* sh_info is set below. */
12522 /* sh_offset is set just below. */
12523 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12525 if (max_sym_count
< 20)
12526 max_sym_count
= 20;
12527 htab
->strtabsize
= max_sym_count
;
12528 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12529 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12530 if (htab
->strtab
== NULL
)
12532 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12534 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12535 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12537 if (info
->strip
!= strip_all
|| emit_relocs
)
12539 file_ptr off
= elf_next_file_pos (abfd
);
12541 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
12543 /* Note that at this point elf_next_file_pos (abfd) is
12544 incorrect. We do not yet know the size of the .symtab section.
12545 We correct next_file_pos below, after we do know the size. */
12547 /* Start writing out the symbol table. The first symbol is always a
12549 elfsym
.st_value
= 0;
12550 elfsym
.st_size
= 0;
12551 elfsym
.st_info
= 0;
12552 elfsym
.st_other
= 0;
12553 elfsym
.st_shndx
= SHN_UNDEF
;
12554 elfsym
.st_target_internal
= 0;
12555 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12556 bfd_und_section_ptr
, NULL
) != 1)
12559 /* Output a symbol for each section if asked or they are used for
12560 relocs. These symbols usually have no names. We store the
12561 index of each one in the index field of the section, so that
12562 we can find it again when outputting relocs. */
12564 if (bfd_keep_unused_section_symbols (abfd
) || emit_relocs
)
12566 bool name_local_sections
12567 = (bed
->elf_backend_name_local_section_symbols
12568 && bed
->elf_backend_name_local_section_symbols (abfd
));
12569 const char *name
= NULL
;
12571 elfsym
.st_size
= 0;
12572 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12573 elfsym
.st_other
= 0;
12574 elfsym
.st_value
= 0;
12575 elfsym
.st_target_internal
= 0;
12576 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12578 o
= bfd_section_from_elf_index (abfd
, i
);
12581 o
->target_index
= bfd_get_symcount (abfd
);
12582 elfsym
.st_shndx
= i
;
12583 if (!bfd_link_relocatable (info
))
12584 elfsym
.st_value
= o
->vma
;
12585 if (name_local_sections
)
12587 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12595 /* On some targets like Irix 5 the symbol split between local and global
12596 ones recorded in the sh_info field needs to be done between section
12597 and all other symbols. */
12598 if (bed
->elf_backend_elfsym_local_is_section
12599 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12600 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12602 /* Allocate some memory to hold information read in from the input
12604 if (max_contents_size
!= 0)
12606 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12607 if (flinfo
.contents
== NULL
)
12611 if (max_external_reloc_size
!= 0)
12613 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12614 if (flinfo
.external_relocs
== NULL
)
12618 if (max_internal_reloc_count
!= 0)
12620 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12621 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12622 if (flinfo
.internal_relocs
== NULL
)
12626 if (max_sym_count
!= 0)
12628 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12629 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12630 if (flinfo
.external_syms
== NULL
)
12633 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12634 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12635 if (flinfo
.internal_syms
== NULL
)
12638 amt
= max_sym_count
* sizeof (long);
12639 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12640 if (flinfo
.indices
== NULL
)
12643 amt
= max_sym_count
* sizeof (asection
*);
12644 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12645 if (flinfo
.sections
== NULL
)
12649 if (max_sym_shndx_count
!= 0)
12651 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12652 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12653 if (flinfo
.locsym_shndx
== NULL
)
12659 bfd_vma base
, end
= 0; /* Both bytes. */
12662 for (sec
= htab
->tls_sec
;
12663 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12666 bfd_size_type size
= sec
->size
;
12667 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12670 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12672 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12675 size
= ord
->offset
* opb
+ ord
->size
;
12677 end
= sec
->vma
+ size
/ opb
;
12679 base
= htab
->tls_sec
->vma
;
12680 /* Only align end of TLS section if static TLS doesn't have special
12681 alignment requirements. */
12682 if (bed
->static_tls_alignment
== 1)
12683 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12684 htab
->tls_size
= end
- base
;
12687 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12690 /* Finish relative relocations here after regular symbol processing
12691 is finished if DT_RELR is enabled. */
12692 if (info
->enable_dt_relr
12693 && bed
->finish_relative_relocs
12694 && !bed
->finish_relative_relocs (info
))
12695 info
->callbacks
->einfo
12696 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd
);
12698 /* Since ELF permits relocations to be against local symbols, we
12699 must have the local symbols available when we do the relocations.
12700 Since we would rather only read the local symbols once, and we
12701 would rather not keep them in memory, we handle all the
12702 relocations for a single input file at the same time.
12704 Unfortunately, there is no way to know the total number of local
12705 symbols until we have seen all of them, and the local symbol
12706 indices precede the global symbol indices. This means that when
12707 we are generating relocatable output, and we see a reloc against
12708 a global symbol, we can not know the symbol index until we have
12709 finished examining all the local symbols to see which ones we are
12710 going to output. To deal with this, we keep the relocations in
12711 memory, and don't output them until the end of the link. This is
12712 an unfortunate waste of memory, but I don't see a good way around
12713 it. Fortunately, it only happens when performing a relocatable
12714 link, which is not the common case. FIXME: If keep_memory is set
12715 we could write the relocs out and then read them again; I don't
12716 know how bad the memory loss will be. */
12718 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12719 sub
->output_has_begun
= false;
12720 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12722 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12724 if (p
->type
== bfd_indirect_link_order
12725 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12726 == bfd_target_elf_flavour
)
12727 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12729 if (! sub
->output_has_begun
)
12731 if (! elf_link_input_bfd (&flinfo
, sub
))
12733 sub
->output_has_begun
= true;
12736 else if (p
->type
== bfd_section_reloc_link_order
12737 || p
->type
== bfd_symbol_reloc_link_order
)
12739 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12744 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12746 if (p
->type
== bfd_indirect_link_order
12747 && (bfd_get_flavour (sub
)
12748 == bfd_target_elf_flavour
)
12749 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12750 != bed
->s
->elfclass
))
12752 const char *iclass
, *oclass
;
12754 switch (bed
->s
->elfclass
)
12756 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12757 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12758 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12762 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12764 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12765 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12766 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12770 bfd_set_error (bfd_error_wrong_format
);
12772 /* xgettext:c-format */
12773 (_("%pB: file class %s incompatible with %s"),
12774 sub
, iclass
, oclass
);
12783 /* Free symbol buffer if needed. */
12784 if (!info
->reduce_memory_overheads
)
12786 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12787 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
12789 free (elf_tdata (sub
)->symbuf
);
12790 elf_tdata (sub
)->symbuf
= NULL
;
12796 /* Output any global symbols that got converted to local in a
12797 version script or due to symbol visibility. We do this in a
12798 separate step since ELF requires all local symbols to appear
12799 prior to any global symbols. FIXME: We should only do this if
12800 some global symbols were, in fact, converted to become local.
12801 FIXME: Will this work correctly with the Irix 5 linker? */
12802 eoinfo
.failed
= false;
12803 eoinfo
.flinfo
= &flinfo
;
12804 eoinfo
.localsyms
= true;
12805 eoinfo
.file_sym_done
= false;
12806 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12810 goto return_local_hash_table
;
12813 /* If backend needs to output some local symbols not present in the hash
12814 table, do it now. */
12815 if (bed
->elf_backend_output_arch_local_syms
12816 && (info
->strip
!= strip_all
|| emit_relocs
))
12818 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12819 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
12822 goto return_local_hash_table
;
12826 /* That wrote out all the local symbols. Finish up the symbol table
12827 with the global symbols. Even if we want to strip everything we
12828 can, we still need to deal with those global symbols that got
12829 converted to local in a version script. */
12831 /* The sh_info field records the index of the first non local symbol. */
12832 if (!symtab_hdr
->sh_info
)
12833 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12836 && htab
->dynsym
!= NULL
12837 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12839 Elf_Internal_Sym sym
;
12840 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12842 o
= htab
->dynsym
->output_section
;
12843 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12845 /* Write out the section symbols for the output sections. */
12846 if (bfd_link_pic (info
)
12847 || htab
->is_relocatable_executable
)
12853 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12855 sym
.st_target_internal
= 0;
12857 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12863 dynindx
= elf_section_data (s
)->dynindx
;
12866 indx
= elf_section_data (s
)->this_idx
;
12867 BFD_ASSERT (indx
> 0);
12868 sym
.st_shndx
= indx
;
12869 if (! check_dynsym (abfd
, &sym
))
12872 goto return_local_hash_table
;
12874 sym
.st_value
= s
->vma
;
12875 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12877 /* Inform the linker of the addition of this symbol. */
12879 if (info
->callbacks
->ctf_new_dynsym
)
12880 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
12882 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12886 /* Write out the local dynsyms. */
12887 if (htab
->dynlocal
)
12889 struct elf_link_local_dynamic_entry
*e
;
12890 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12895 /* Copy the internal symbol and turn off visibility.
12896 Note that we saved a word of storage and overwrote
12897 the original st_name with the dynstr_index. */
12899 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12900 sym
.st_shndx
= SHN_UNDEF
;
12902 s
= bfd_section_from_elf_index (e
->input_bfd
,
12905 && s
->output_section
!= NULL
12906 && elf_section_data (s
->output_section
) != NULL
)
12909 elf_section_data (s
->output_section
)->this_idx
;
12910 if (! check_dynsym (abfd
, &sym
))
12913 goto return_local_hash_table
;
12915 sym
.st_value
= (s
->output_section
->vma
12917 + e
->isym
.st_value
);
12920 /* Inform the linker of the addition of this symbol. */
12922 if (info
->callbacks
->ctf_new_dynsym
)
12923 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
12925 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12926 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12931 /* We get the global symbols from the hash table. */
12932 eoinfo
.failed
= false;
12933 eoinfo
.localsyms
= false;
12934 eoinfo
.flinfo
= &flinfo
;
12935 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12939 goto return_local_hash_table
;
12942 /* If backend needs to output some symbols not present in the hash
12943 table, do it now. */
12944 if (bed
->elf_backend_output_arch_syms
12945 && (info
->strip
!= strip_all
|| emit_relocs
))
12947 if (! ((*bed
->elf_backend_output_arch_syms
)
12948 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
12951 goto return_local_hash_table
;
12955 /* Finalize the .strtab section. */
12956 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12958 /* Swap out the .strtab section. */
12959 if (!elf_link_swap_symbols_out (&flinfo
))
12962 goto return_local_hash_table
;
12965 /* Now we know the size of the symtab section. */
12966 if (bfd_get_symcount (abfd
) > 0)
12968 /* Finish up and write out the symbol string table (.strtab)
12970 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12971 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12973 if (elf_symtab_shndx_list (abfd
))
12975 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12977 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12979 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12980 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12981 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12982 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12983 symtab_shndx_hdr
->sh_size
= amt
;
12985 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12988 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12989 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12992 goto return_local_hash_table
;
12997 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12998 /* sh_name was set in prep_headers. */
12999 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
13000 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
13001 symstrtab_hdr
->sh_addr
= 0;
13002 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
13003 symstrtab_hdr
->sh_entsize
= 0;
13004 symstrtab_hdr
->sh_link
= 0;
13005 symstrtab_hdr
->sh_info
= 0;
13006 /* sh_offset is set just below. */
13007 symstrtab_hdr
->sh_addralign
= 1;
13009 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
13011 elf_next_file_pos (abfd
) = off
;
13013 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
13014 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
13017 goto return_local_hash_table
;
13021 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
13023 _bfd_error_handler (_("%pB: failed to generate import library"),
13024 info
->out_implib_bfd
);
13026 goto return_local_hash_table
;
13029 /* Adjust the relocs to have the correct symbol indices. */
13030 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
13032 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
13035 if ((o
->flags
& SEC_RELOC
) == 0)
13038 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
13039 if (esdo
->rel
.hdr
!= NULL
13040 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
13043 goto return_local_hash_table
;
13045 if (esdo
->rela
.hdr
!= NULL
13046 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
13049 goto return_local_hash_table
;
13052 /* Set the reloc_count field to 0 to prevent write_relocs from
13053 trying to swap the relocs out itself. */
13054 o
->reloc_count
= 0;
13058 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
13059 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
13062 if (htab
->srelrdyn
!= NULL
13063 && htab
->srelrdyn
->output_section
!= NULL
13064 && htab
->srelrdyn
->size
!= 0)
13066 asection
*s
= htab
->srelrdyn
->output_section
;
13067 relr_entsize
= elf_section_data (s
)->this_hdr
.sh_entsize
;
13068 if (relr_entsize
== 0)
13070 relr_entsize
= bed
->s
->arch_size
/ 8;
13071 elf_section_data (s
)->this_hdr
.sh_entsize
= relr_entsize
;
13075 /* If we are linking against a dynamic object, or generating a
13076 shared library, finish up the dynamic linking information. */
13079 bfd_byte
*dyncon
, *dynconend
;
13081 /* Fix up .dynamic entries. */
13082 o
= bfd_get_linker_section (dynobj
, ".dynamic");
13083 BFD_ASSERT (o
!= NULL
);
13085 dyncon
= o
->contents
;
13086 dynconend
= PTR_ADD (o
->contents
, o
->size
);
13087 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13089 Elf_Internal_Dyn dyn
;
13092 bfd_size_type sh_size
;
13095 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13102 if (relativecount
!= 0)
13104 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
13106 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
13107 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
13109 if (dyn
.d_tag
!= DT_NULL
13110 && dynconend
- dyncon
>= bed
->s
->sizeof_dyn
)
13112 dyn
.d_un
.d_val
= relativecount
;
13118 if (relr_entsize
!= 0)
13120 if (dynconend
- dyncon
>= 3 * bed
->s
->sizeof_dyn
)
13122 asection
*s
= htab
->srelrdyn
;
13123 dyn
.d_tag
= DT_RELR
;
13125 = s
->output_section
->vma
+ s
->output_offset
;
13126 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13127 dyncon
+= bed
->s
->sizeof_dyn
;
13129 dyn
.d_tag
= DT_RELRSZ
;
13130 dyn
.d_un
.d_val
= s
->size
;
13131 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13132 dyncon
+= bed
->s
->sizeof_dyn
;
13134 dyn
.d_tag
= DT_RELRENT
;
13135 dyn
.d_un
.d_val
= relr_entsize
;
13144 name
= info
->init_function
;
13147 name
= info
->fini_function
;
13150 struct elf_link_hash_entry
*h
;
13152 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
13154 && (h
->root
.type
== bfd_link_hash_defined
13155 || h
->root
.type
== bfd_link_hash_defweak
))
13157 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
13158 o
= h
->root
.u
.def
.section
;
13159 if (o
->output_section
!= NULL
)
13160 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
13161 + o
->output_offset
);
13164 /* The symbol is imported from another shared
13165 library and does not apply to this one. */
13166 dyn
.d_un
.d_ptr
= 0;
13173 case DT_PREINIT_ARRAYSZ
:
13174 name
= ".preinit_array";
13176 case DT_INIT_ARRAYSZ
:
13177 name
= ".init_array";
13179 case DT_FINI_ARRAYSZ
:
13180 name
= ".fini_array";
13182 o
= bfd_get_section_by_name (abfd
, name
);
13186 (_("could not find section %s"), name
);
13191 (_("warning: %s section has zero size"), name
);
13192 dyn
.d_un
.d_val
= o
->size
;
13195 case DT_PREINIT_ARRAY
:
13196 name
= ".preinit_array";
13198 case DT_INIT_ARRAY
:
13199 name
= ".init_array";
13201 case DT_FINI_ARRAY
:
13202 name
= ".fini_array";
13204 o
= bfd_get_section_by_name (abfd
, name
);
13211 name
= ".gnu.hash";
13220 name
= ".gnu.version_d";
13223 name
= ".gnu.version_r";
13226 name
= ".gnu.version";
13228 o
= bfd_get_linker_section (dynobj
, name
);
13230 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13233 (_("could not find section %s"), name
);
13236 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13239 (_("warning: section '%s' is being made into a note"), name
);
13240 bfd_set_error (bfd_error_nonrepresentable_section
);
13243 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13250 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13256 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13258 Elf_Internal_Shdr
*hdr
;
13260 hdr
= elf_elfsections (abfd
)[i
];
13261 if (hdr
->sh_type
== type
13262 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13264 sh_size
+= hdr
->sh_size
;
13266 || sh_addr
> hdr
->sh_addr
)
13267 sh_addr
= hdr
->sh_addr
;
13271 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13273 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13275 /* Don't count procedure linkage table relocs in the
13276 overall reloc count. */
13277 sh_size
-= htab
->srelplt
->size
;
13279 /* If the size is zero, make the address zero too.
13280 This is to avoid a glibc bug. If the backend
13281 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13282 zero, then we'll put DT_RELA at the end of
13283 DT_JMPREL. glibc will interpret the end of
13284 DT_RELA matching the end of DT_JMPREL as the
13285 case where DT_RELA includes DT_JMPREL, and for
13286 LD_BIND_NOW will decide that processing DT_RELA
13287 will process the PLT relocs too. Net result:
13288 No PLT relocs applied. */
13291 /* If .rela.plt is the first .rela section, exclude
13292 it from DT_RELA. */
13293 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13294 + htab
->srelplt
->output_offset
) * opb
)
13295 sh_addr
+= htab
->srelplt
->size
;
13298 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13299 dyn
.d_un
.d_val
= sh_size
;
13301 dyn
.d_un
.d_ptr
= sh_addr
;
13304 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13308 /* If we have created any dynamic sections, then output them. */
13309 if (dynobj
!= NULL
)
13311 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13314 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13315 if (bfd_link_textrel_check (info
)
13316 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
13319 bfd_byte
*dyncon
, *dynconend
;
13321 dyncon
= o
->contents
;
13322 dynconend
= o
->contents
+ o
->size
;
13323 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13325 Elf_Internal_Dyn dyn
;
13327 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13329 if (dyn
.d_tag
== DT_TEXTREL
)
13331 if (info
->textrel_check
== textrel_check_error
)
13332 info
->callbacks
->einfo
13333 (_("%P%X: read-only segment has dynamic relocations\n"));
13334 else if (bfd_link_dll (info
))
13335 info
->callbacks
->einfo
13336 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13337 else if (bfd_link_pde (info
))
13338 info
->callbacks
->einfo
13339 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13341 info
->callbacks
->einfo
13342 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13348 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13350 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13352 || o
->output_section
== bfd_abs_section_ptr
)
13354 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13356 /* At this point, we are only interested in sections
13357 created by _bfd_elf_link_create_dynamic_sections. */
13360 if (htab
->stab_info
.stabstr
== o
)
13362 if (htab
->eh_info
.hdr_sec
== o
)
13364 if (strcmp (o
->name
, ".dynstr") != 0)
13366 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13367 * bfd_octets_per_byte (abfd
, o
));
13368 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13369 o
->contents
, octets
, o
->size
))
13374 /* The contents of the .dynstr section are actually in a
13378 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13379 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13380 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13386 if (!info
->resolve_section_groups
)
13388 bool failed
= false;
13390 BFD_ASSERT (bfd_link_relocatable (info
));
13391 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13396 /* If we have optimized stabs strings, output them. */
13397 if (htab
->stab_info
.stabstr
!= NULL
)
13399 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13403 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13406 if (info
->callbacks
->emit_ctf
)
13407 info
->callbacks
->emit_ctf ();
13409 elf_final_link_free (abfd
, &flinfo
);
13413 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13414 if (contents
== NULL
)
13416 /* Bail out and fail. */
13418 goto return_local_hash_table
;
13420 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13421 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13425 return_local_hash_table
:
13426 if (info
->unique_symbol
)
13427 bfd_hash_table_free (&flinfo
.local_hash_table
);
13431 elf_final_link_free (abfd
, &flinfo
);
13433 goto return_local_hash_table
;
13436 /* Initialize COOKIE for input bfd ABFD. */
13439 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13440 struct bfd_link_info
*info
, bfd
*abfd
)
13442 Elf_Internal_Shdr
*symtab_hdr
;
13443 const struct elf_backend_data
*bed
;
13445 bed
= get_elf_backend_data (abfd
);
13446 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13448 cookie
->abfd
= abfd
;
13449 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13450 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13451 if (cookie
->bad_symtab
)
13453 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13454 cookie
->extsymoff
= 0;
13458 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13459 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13462 if (bed
->s
->arch_size
== 32)
13463 cookie
->r_sym_shift
= 8;
13465 cookie
->r_sym_shift
= 32;
13467 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13468 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13470 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13471 cookie
->locsymcount
, 0,
13473 if (cookie
->locsyms
== NULL
)
13475 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13478 if (_bfd_link_keep_memory (info
) )
13480 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13481 info
->cache_size
+= (cookie
->locsymcount
13482 * sizeof (Elf_External_Sym_Shndx
));
13488 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13491 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13493 Elf_Internal_Shdr
*symtab_hdr
;
13495 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13496 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13497 free (cookie
->locsyms
);
13500 /* Initialize the relocation information in COOKIE for input section SEC
13501 of input bfd ABFD. */
13504 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13505 struct bfd_link_info
*info
, bfd
*abfd
,
13508 if (sec
->reloc_count
== 0)
13510 cookie
->rels
= NULL
;
13511 cookie
->relend
= NULL
;
13515 cookie
->rels
= _bfd_elf_link_info_read_relocs (abfd
, info
, sec
,
13517 _bfd_link_keep_memory (info
));
13518 if (cookie
->rels
== NULL
)
13520 cookie
->rel
= cookie
->rels
;
13521 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13523 cookie
->rel
= cookie
->rels
;
13527 /* Free the memory allocated by init_reloc_cookie_rels,
13531 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13534 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13535 free (cookie
->rels
);
13538 /* Initialize the whole of COOKIE for input section SEC. */
13541 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13542 struct bfd_link_info
*info
,
13545 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13547 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13552 fini_reloc_cookie (cookie
, sec
->owner
);
13557 /* Free the memory allocated by init_reloc_cookie_for_section,
13561 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13564 fini_reloc_cookie_rels (cookie
, sec
);
13565 fini_reloc_cookie (cookie
, sec
->owner
);
13568 /* Garbage collect unused sections. */
13570 /* Default gc_mark_hook. */
13573 _bfd_elf_gc_mark_hook (asection
*sec
,
13574 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13575 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13576 struct elf_link_hash_entry
*h
,
13577 Elf_Internal_Sym
*sym
)
13581 switch (h
->root
.type
)
13583 case bfd_link_hash_defined
:
13584 case bfd_link_hash_defweak
:
13585 return h
->root
.u
.def
.section
;
13587 case bfd_link_hash_common
:
13588 return h
->root
.u
.c
.p
->section
;
13595 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13600 /* Return the debug definition section. */
13603 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13604 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13605 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13606 struct elf_link_hash_entry
*h
,
13607 Elf_Internal_Sym
*sym
)
13611 /* Return the global debug definition section. */
13612 if ((h
->root
.type
== bfd_link_hash_defined
13613 || h
->root
.type
== bfd_link_hash_defweak
)
13614 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13615 return h
->root
.u
.def
.section
;
13619 /* Return the local debug definition section. */
13620 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13622 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
13629 /* COOKIE->rel describes a relocation against section SEC, which is
13630 a section we've decided to keep. Return the section that contains
13631 the relocation symbol, or NULL if no section contains it. */
13634 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13635 elf_gc_mark_hook_fn gc_mark_hook
,
13636 struct elf_reloc_cookie
*cookie
,
13639 unsigned long r_symndx
;
13640 struct elf_link_hash_entry
*h
, *hw
;
13642 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13643 if (r_symndx
== STN_UNDEF
)
13646 if (r_symndx
>= cookie
->locsymcount
13647 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13651 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13654 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13658 while (h
->root
.type
== bfd_link_hash_indirect
13659 || h
->root
.type
== bfd_link_hash_warning
)
13660 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13662 was_marked
= h
->mark
;
13664 /* Keep all aliases of the symbol too. If an object symbol
13665 needs to be copied into .dynbss then all of its aliases
13666 should be present as dynamic symbols, not just the one used
13667 on the copy relocation. */
13669 while (hw
->is_weakalias
)
13675 if (!was_marked
&& h
->start_stop
&& !h
->root
.ldscript_def
)
13677 if (info
->start_stop_gc
)
13680 /* To work around a glibc bug, mark XXX input sections
13681 when there is a reference to __start_XXX or __stop_XXX
13683 else if (start_stop
!= NULL
)
13685 asection
*s
= h
->u2
.start_stop_section
;
13686 *start_stop
= true;
13691 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13694 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13695 &cookie
->locsyms
[r_symndx
]);
13698 /* COOKIE->rel describes a relocation against section SEC, which is
13699 a section we've decided to keep. Mark the section that contains
13700 the relocation symbol. */
13703 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13705 elf_gc_mark_hook_fn gc_mark_hook
,
13706 struct elf_reloc_cookie
*cookie
)
13709 bool start_stop
= false;
13711 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13712 while (rsec
!= NULL
)
13714 if (!rsec
->gc_mark
)
13716 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13717 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13719 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13724 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13729 /* The mark phase of garbage collection. For a given section, mark
13730 it and any sections in this section's group, and all the sections
13731 which define symbols to which it refers. */
13734 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13736 elf_gc_mark_hook_fn gc_mark_hook
)
13739 asection
*group_sec
, *eh_frame
;
13743 /* Mark all the sections in the group. */
13744 group_sec
= elf_section_data (sec
)->next_in_group
;
13745 if (group_sec
&& !group_sec
->gc_mark
)
13746 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13749 /* Look through the section relocs. */
13751 eh_frame
= elf_eh_frame_section (sec
->owner
);
13752 if ((sec
->flags
& SEC_RELOC
) != 0
13753 && sec
->reloc_count
> 0
13754 && sec
!= eh_frame
)
13756 struct elf_reloc_cookie cookie
;
13758 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13762 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13763 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13768 fini_reloc_cookie_for_section (&cookie
, sec
);
13772 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13774 struct elf_reloc_cookie cookie
;
13776 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13780 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13781 gc_mark_hook
, &cookie
))
13783 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13787 eh_frame
= elf_section_eh_frame_entry (sec
);
13788 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13789 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13795 /* Scan and mark sections in a special or debug section group. */
13798 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13800 /* Point to first section of section group. */
13802 /* Used to iterate the section group. */
13805 bool is_special_grp
= true;
13806 bool is_debug_grp
= true;
13808 /* First scan to see if group contains any section other than debug
13809 and special section. */
13810 ssec
= msec
= elf_next_in_group (grp
);
13813 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13814 is_debug_grp
= false;
13816 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13817 is_special_grp
= false;
13819 msec
= elf_next_in_group (msec
);
13821 while (msec
!= ssec
);
13823 /* If this is a pure debug section group or pure special section group,
13824 keep all sections in this group. */
13825 if (is_debug_grp
|| is_special_grp
)
13830 msec
= elf_next_in_group (msec
);
13832 while (msec
!= ssec
);
13836 /* Keep debug and special sections. */
13839 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13840 elf_gc_mark_hook_fn mark_hook
)
13844 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13848 bool debug_frag_seen
;
13849 bool has_kept_debug_info
;
13851 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13853 isec
= ibfd
->sections
;
13854 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13857 /* Ensure all linker created sections are kept,
13858 see if any other section is already marked,
13859 and note if we have any fragmented debug sections. */
13860 debug_frag_seen
= some_kept
= has_kept_debug_info
= false;
13861 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13863 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13865 else if (isec
->gc_mark
13866 && (isec
->flags
& SEC_ALLOC
) != 0
13867 && elf_section_type (isec
) != SHT_NOTE
)
13871 /* Since all sections, except for backend specific ones,
13872 have been garbage collected, call mark_hook on this
13873 section if any of its linked-to sections is marked. */
13874 asection
*linked_to_sec
;
13875 for (linked_to_sec
= elf_linked_to_section (isec
);
13876 linked_to_sec
!= NULL
&& !linked_to_sec
->linker_mark
;
13877 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13879 if (linked_to_sec
->gc_mark
)
13881 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
13885 linked_to_sec
->linker_mark
= 1;
13887 for (linked_to_sec
= elf_linked_to_section (isec
);
13888 linked_to_sec
!= NULL
&& linked_to_sec
->linker_mark
;
13889 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13890 linked_to_sec
->linker_mark
= 0;
13893 if (!debug_frag_seen
13894 && (isec
->flags
& SEC_DEBUGGING
)
13895 && startswith (isec
->name
, ".debug_line."))
13896 debug_frag_seen
= true;
13897 else if (strcmp (bfd_section_name (isec
),
13898 "__patchable_function_entries") == 0
13899 && elf_linked_to_section (isec
) == NULL
)
13900 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
13901 "need linked-to section "
13902 "for --gc-sections\n"),
13903 isec
->owner
, isec
);
13906 /* If no non-note alloc section in this file will be kept, then
13907 we can toss out the debug and special sections. */
13911 /* Keep debug and special sections like .comment when they are
13912 not part of a group. Also keep section groups that contain
13913 just debug sections or special sections. NB: Sections with
13914 linked-to section has been handled above. */
13915 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13917 if ((isec
->flags
& SEC_GROUP
) != 0)
13918 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13919 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13920 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13921 && elf_next_in_group (isec
) == NULL
13922 && elf_linked_to_section (isec
) == NULL
)
13924 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13925 has_kept_debug_info
= true;
13928 /* Look for CODE sections which are going to be discarded,
13929 and find and discard any fragmented debug sections which
13930 are associated with that code section. */
13931 if (debug_frag_seen
)
13932 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13933 if ((isec
->flags
& SEC_CODE
) != 0
13934 && isec
->gc_mark
== 0)
13939 ilen
= strlen (isec
->name
);
13941 /* Association is determined by the name of the debug
13942 section containing the name of the code section as
13943 a suffix. For example .debug_line.text.foo is a
13944 debug section associated with .text.foo. */
13945 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13949 if (dsec
->gc_mark
== 0
13950 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13953 dlen
= strlen (dsec
->name
);
13956 && strncmp (dsec
->name
+ (dlen
- ilen
),
13957 isec
->name
, ilen
) == 0)
13962 /* Mark debug sections referenced by kept debug sections. */
13963 if (has_kept_debug_info
)
13964 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13966 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13967 if (!_bfd_elf_gc_mark (info
, isec
,
13968 elf_gc_mark_debug_section
))
13975 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13978 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13980 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13984 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13985 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13986 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13989 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13992 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13994 /* When any section in a section group is kept, we keep all
13995 sections in the section group. If the first member of
13996 the section group is excluded, we will also exclude the
13998 if (o
->flags
& SEC_GROUP
)
14000 asection
*first
= elf_next_in_group (o
);
14001 o
->gc_mark
= first
->gc_mark
;
14007 /* Skip sweeping sections already excluded. */
14008 if (o
->flags
& SEC_EXCLUDE
)
14011 /* Since this is early in the link process, it is simple
14012 to remove a section from the output. */
14013 o
->flags
|= SEC_EXCLUDE
;
14015 if (info
->print_gc_sections
&& o
->size
!= 0)
14016 /* xgettext:c-format */
14017 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14025 /* Propagate collected vtable information. This is called through
14026 elf_link_hash_traverse. */
14029 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
14031 /* Those that are not vtables. */
14033 || h
->u2
.vtable
== NULL
14034 || h
->u2
.vtable
->parent
== NULL
)
14037 /* Those vtables that do not have parents, we cannot merge. */
14038 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
14041 /* If we've already been done, exit. */
14042 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
14045 /* Make sure the parent's table is up to date. */
14046 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
14048 if (h
->u2
.vtable
->used
== NULL
)
14050 /* None of this table's entries were referenced. Re-use the
14052 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14053 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
14060 /* Or the parent's entries into ours. */
14061 cu
= h
->u2
.vtable
->used
;
14063 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14066 const struct elf_backend_data
*bed
;
14067 unsigned int log_file_align
;
14069 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
14070 log_file_align
= bed
->s
->log_file_align
;
14071 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
14085 struct link_info_ok
14087 struct bfd_link_info
*info
;
14092 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
,
14096 bfd_vma hstart
, hend
;
14097 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
14098 const struct elf_backend_data
*bed
;
14099 unsigned int log_file_align
;
14100 struct link_info_ok
*info
= (struct link_info_ok
*) ptr
;
14102 /* Take care of both those symbols that do not describe vtables as
14103 well as those that are not loaded. */
14105 || h
->u2
.vtable
== NULL
14106 || h
->u2
.vtable
->parent
== NULL
)
14109 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
14110 || h
->root
.type
== bfd_link_hash_defweak
);
14112 sec
= h
->root
.u
.def
.section
;
14113 hstart
= h
->root
.u
.def
.value
;
14114 hend
= hstart
+ h
->size
;
14116 relstart
= _bfd_elf_link_info_read_relocs (sec
->owner
, info
->info
,
14117 sec
, NULL
, NULL
, true);
14119 return info
->ok
= false;
14120 bed
= get_elf_backend_data (sec
->owner
);
14121 log_file_align
= bed
->s
->log_file_align
;
14123 relend
= relstart
+ sec
->reloc_count
;
14125 for (rel
= relstart
; rel
< relend
; ++rel
)
14126 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
14128 /* If the entry is in use, do nothing. */
14129 if (h
->u2
.vtable
->used
14130 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
14132 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
14133 if (h
->u2
.vtable
->used
[entry
])
14136 /* Otherwise, kill it. */
14137 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
14143 /* Mark sections containing dynamically referenced symbols. When
14144 building shared libraries, we must assume that any visible symbol is
14148 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
14150 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
14151 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
14153 if ((h
->root
.type
== bfd_link_hash_defined
14154 || h
->root
.type
== bfd_link_hash_defweak
)
14156 || h
->root
.ldscript_def
14157 || !info
->start_stop_gc
)
14158 && ((h
->ref_dynamic
&& !h
->forced_local
)
14159 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
14160 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
14161 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
14162 && (!bfd_link_executable (info
)
14163 || info
->gc_keep_exported
14164 || info
->export_dynamic
14167 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
14168 && (h
->versioned
>= versioned
14169 || !bfd_hide_sym_by_version (info
->version_info
,
14170 h
->root
.root
.string
)))))
14171 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14176 /* Keep all sections containing symbols undefined on the command-line,
14177 and the section containing the entry symbol. */
14180 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
14182 struct bfd_sym_chain
*sym
;
14184 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
14186 struct elf_link_hash_entry
*h
;
14188 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
14189 false, false, false);
14192 && (h
->root
.type
== bfd_link_hash_defined
14193 || h
->root
.type
== bfd_link_hash_defweak
)
14194 && !bfd_is_const_section (h
->root
.u
.def
.section
))
14195 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14200 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
14201 struct bfd_link_info
*info
)
14203 bfd
*ibfd
= info
->input_bfds
;
14205 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14208 struct elf_reloc_cookie cookie
;
14210 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14212 sec
= ibfd
->sections
;
14213 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14216 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
14219 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
14221 if (startswith (bfd_section_name (sec
), ".eh_frame_entry")
14222 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
14224 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
14225 fini_reloc_cookie_rels (&cookie
, sec
);
14232 /* Do mark and sweep of unused sections. */
14235 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14239 elf_gc_mark_hook_fn gc_mark_hook
;
14240 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14241 struct elf_link_hash_table
*htab
;
14242 struct link_info_ok info_ok
;
14244 if (!bed
->can_gc_sections
14245 || !is_elf_hash_table (info
->hash
))
14247 _bfd_error_handler(_("warning: gc-sections option ignored"));
14251 bed
->gc_keep (info
);
14252 htab
= elf_hash_table (info
);
14254 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14255 at the .eh_frame section if we can mark the FDEs individually. */
14256 for (sub
= info
->input_bfds
;
14257 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14258 sub
= sub
->link
.next
)
14261 struct elf_reloc_cookie cookie
;
14263 sec
= sub
->sections
;
14264 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14266 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14267 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
14269 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14270 if (elf_section_data (sec
)->sec_info
14271 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14272 elf_eh_frame_section (sub
) = sec
;
14273 fini_reloc_cookie_for_section (&cookie
, sec
);
14274 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14278 /* Apply transitive closure to the vtable entry usage info. */
14279 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14283 /* Kill the vtable relocations that were not used. */
14284 info_ok
.info
= info
;
14286 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &info_ok
);
14290 /* Mark dynamically referenced symbols. */
14291 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14292 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14294 /* Grovel through relocs to find out who stays ... */
14295 gc_mark_hook
= bed
->gc_mark_hook
;
14296 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14300 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14301 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14302 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14306 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14309 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14310 Also treat note sections as a root, if the section is not part
14311 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14312 well as FINI_ARRAY sections for ld -r. */
14313 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14315 && (o
->flags
& SEC_EXCLUDE
) == 0
14316 && ((o
->flags
& SEC_KEEP
) != 0
14317 || (bfd_link_relocatable (info
)
14318 && ((elf_section_data (o
)->this_hdr
.sh_type
14319 == SHT_PREINIT_ARRAY
)
14320 || (elf_section_data (o
)->this_hdr
.sh_type
14322 || (elf_section_data (o
)->this_hdr
.sh_type
14323 == SHT_FINI_ARRAY
)))
14324 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14325 && elf_next_in_group (o
) == NULL
14326 && elf_linked_to_section (o
) == NULL
)
14327 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14328 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14330 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14335 /* Allow the backend to mark additional target specific sections. */
14336 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14338 /* ... and mark SEC_EXCLUDE for those that go. */
14339 return elf_gc_sweep (abfd
, info
);
14342 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14345 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14347 struct elf_link_hash_entry
*h
,
14350 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14351 struct elf_link_hash_entry
**search
, *child
;
14352 size_t extsymcount
;
14353 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14355 /* The sh_info field of the symtab header tells us where the
14356 external symbols start. We don't care about the local symbols at
14358 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14359 if (!elf_bad_symtab (abfd
))
14360 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14362 sym_hashes
= elf_sym_hashes (abfd
);
14363 sym_hashes_end
= PTR_ADD (sym_hashes
, extsymcount
);
14365 /* Hunt down the child symbol, which is in this section at the same
14366 offset as the relocation. */
14367 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14369 if ((child
= *search
) != NULL
14370 && (child
->root
.type
== bfd_link_hash_defined
14371 || child
->root
.type
== bfd_link_hash_defweak
)
14372 && child
->root
.u
.def
.section
== sec
14373 && child
->root
.u
.def
.value
== offset
)
14377 /* xgettext:c-format */
14378 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14379 abfd
, sec
, (uint64_t) offset
);
14380 bfd_set_error (bfd_error_invalid_operation
);
14384 if (!child
->u2
.vtable
)
14386 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14387 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14388 if (!child
->u2
.vtable
)
14393 /* This *should* only be the absolute section. It could potentially
14394 be that someone has defined a non-global vtable though, which
14395 would be bad. It isn't worth paging in the local symbols to be
14396 sure though; that case should simply be handled by the assembler. */
14398 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14401 child
->u2
.vtable
->parent
= h
;
14406 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14409 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14410 struct elf_link_hash_entry
*h
,
14413 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14414 unsigned int log_file_align
= bed
->s
->log_file_align
;
14418 /* xgettext:c-format */
14419 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14421 bfd_set_error (bfd_error_bad_value
);
14427 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14428 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14433 if (addend
>= h
->u2
.vtable
->size
)
14435 size_t size
, bytes
, file_align
;
14436 bool *ptr
= h
->u2
.vtable
->used
;
14438 /* While the symbol is undefined, we have to be prepared to handle
14440 file_align
= 1 << log_file_align
;
14441 if (h
->root
.type
== bfd_link_hash_undefined
)
14442 size
= addend
+ file_align
;
14446 if (addend
>= size
)
14448 /* Oops! We've got a reference past the defined end of
14449 the table. This is probably a bug -- shall we warn? */
14450 size
= addend
+ file_align
;
14453 size
= (size
+ file_align
- 1) & -file_align
;
14455 /* Allocate one extra entry for use as a "done" flag for the
14456 consolidation pass. */
14457 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bool);
14461 ptr
= (bool *) bfd_realloc (ptr
- 1, bytes
);
14467 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14469 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14473 ptr
= (bool *) bfd_zmalloc (bytes
);
14478 /* And arrange for that done flag to be at index -1. */
14479 h
->u2
.vtable
->used
= ptr
+ 1;
14480 h
->u2
.vtable
->size
= size
;
14483 h
->u2
.vtable
->used
[addend
>> log_file_align
] = true;
14488 /* Map an ELF section header flag to its corresponding string. */
14492 flagword flag_value
;
14493 } elf_flags_to_name_table
;
14495 static const elf_flags_to_name_table elf_flags_to_names
[] =
14497 { "SHF_WRITE", SHF_WRITE
},
14498 { "SHF_ALLOC", SHF_ALLOC
},
14499 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14500 { "SHF_MERGE", SHF_MERGE
},
14501 { "SHF_STRINGS", SHF_STRINGS
},
14502 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14503 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14504 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14505 { "SHF_GROUP", SHF_GROUP
},
14506 { "SHF_TLS", SHF_TLS
},
14507 { "SHF_MASKOS", SHF_MASKOS
},
14508 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14511 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14513 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14514 struct flag_info
*flaginfo
,
14517 const bfd_vma sh_flags
= elf_section_flags (section
);
14519 if (!flaginfo
->flags_initialized
)
14521 bfd
*obfd
= info
->output_bfd
;
14522 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14523 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14525 int without_hex
= 0;
14527 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14530 flagword (*lookup
) (char *);
14532 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14533 if (lookup
!= NULL
)
14535 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14539 if (tf
->with
== with_flags
)
14540 with_hex
|= hexval
;
14541 else if (tf
->with
== without_flags
)
14542 without_hex
|= hexval
;
14547 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14549 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14551 if (tf
->with
== with_flags
)
14552 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14553 else if (tf
->with
== without_flags
)
14554 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14561 info
->callbacks
->einfo
14562 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14566 flaginfo
->flags_initialized
= true;
14567 flaginfo
->only_with_flags
|= with_hex
;
14568 flaginfo
->not_with_flags
|= without_hex
;
14571 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14574 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14580 struct alloc_got_off_arg
{
14582 struct bfd_link_info
*info
;
14585 /* We need a special top-level link routine to convert got reference counts
14586 to real got offsets. */
14589 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14591 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14592 bfd
*obfd
= gofarg
->info
->output_bfd
;
14593 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14595 if (h
->got
.refcount
> 0)
14597 h
->got
.offset
= gofarg
->gotoff
;
14598 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14601 h
->got
.offset
= (bfd_vma
) -1;
14606 /* And an accompanying bit to work out final got entry offsets once
14607 we're done. Should be called from final_link. */
14610 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14611 struct bfd_link_info
*info
)
14614 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14616 struct alloc_got_off_arg gofarg
;
14618 BFD_ASSERT (abfd
== info
->output_bfd
);
14620 if (! is_elf_hash_table (info
->hash
))
14623 /* The GOT offset is relative to the .got section, but the GOT header is
14624 put into the .got.plt section, if the backend uses it. */
14625 if (bed
->want_got_plt
)
14628 gotoff
= bed
->got_header_size
;
14630 /* Do the local .got entries first. */
14631 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14633 bfd_signed_vma
*local_got
;
14634 size_t j
, locsymcount
;
14635 Elf_Internal_Shdr
*symtab_hdr
;
14637 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14640 local_got
= elf_local_got_refcounts (i
);
14644 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14645 if (elf_bad_symtab (i
))
14646 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14648 locsymcount
= symtab_hdr
->sh_info
;
14650 for (j
= 0; j
< locsymcount
; ++j
)
14652 if (local_got
[j
] > 0)
14654 local_got
[j
] = gotoff
;
14655 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14658 local_got
[j
] = (bfd_vma
) -1;
14662 /* Then the global .got entries. .plt refcounts are handled by
14663 adjust_dynamic_symbol */
14664 gofarg
.gotoff
= gotoff
;
14665 gofarg
.info
= info
;
14666 elf_link_hash_traverse (elf_hash_table (info
),
14667 elf_gc_allocate_got_offsets
,
14672 /* Many folk need no more in the way of final link than this, once
14673 got entry reference counting is enabled. */
14676 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14678 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14681 /* Invoke the regular ELF backend linker to do all the work. */
14682 return bfd_elf_final_link (abfd
, info
);
14686 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14688 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14690 if (rcookie
->bad_symtab
)
14691 rcookie
->rel
= rcookie
->rels
;
14693 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14695 unsigned long r_symndx
;
14697 if (! rcookie
->bad_symtab
)
14698 if (rcookie
->rel
->r_offset
> offset
)
14700 if (rcookie
->rel
->r_offset
!= offset
)
14703 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14704 if (r_symndx
== STN_UNDEF
)
14707 if (r_symndx
>= rcookie
->locsymcount
14708 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14710 struct elf_link_hash_entry
*h
;
14712 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14714 while (h
->root
.type
== bfd_link_hash_indirect
14715 || h
->root
.type
== bfd_link_hash_warning
)
14716 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14718 if ((h
->root
.type
== bfd_link_hash_defined
14719 || h
->root
.type
== bfd_link_hash_defweak
)
14720 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14721 || h
->root
.u
.def
.section
->kept_section
!= NULL
14722 || discarded_section (h
->root
.u
.def
.section
)))
14727 /* It's not a relocation against a global symbol,
14728 but it could be a relocation against a local
14729 symbol for a discarded section. */
14731 Elf_Internal_Sym
*isym
;
14733 /* Need to: get the symbol; get the section. */
14734 isym
= &rcookie
->locsyms
[r_symndx
];
14735 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14737 && (isec
->kept_section
!= NULL
14738 || discarded_section (isec
)))
14746 /* Discard unneeded references to discarded sections.
14747 Returns -1 on error, 1 if any section's size was changed, 0 if
14748 nothing changed. This function assumes that the relocations are in
14749 sorted order, which is true for all known assemblers. */
14752 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14754 struct elf_reloc_cookie cookie
;
14759 if (info
->traditional_format
14760 || !is_elf_hash_table (info
->hash
))
14763 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14768 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14771 || i
->reloc_count
== 0
14772 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14776 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14779 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14782 if (_bfd_discard_section_stabs (abfd
, i
,
14783 elf_section_data (i
)->sec_info
,
14784 bfd_elf_reloc_symbol_deleted_p
,
14788 fini_reloc_cookie_for_section (&cookie
, i
);
14793 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14794 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14798 int eh_changed
= 0;
14799 unsigned int eh_alignment
; /* Octets. */
14801 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14807 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14810 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14813 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14814 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14815 bfd_elf_reloc_symbol_deleted_p
,
14819 if (i
->size
!= i
->rawsize
)
14823 fini_reloc_cookie_for_section (&cookie
, i
);
14826 eh_alignment
= ((1 << o
->alignment_power
)
14827 * bfd_octets_per_byte (output_bfd
, o
));
14828 /* Skip over zero terminator, and prevent empty sections from
14829 adding alignment padding at the end. */
14830 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14832 i
->flags
|= SEC_EXCLUDE
;
14833 else if (i
->size
> 4)
14835 /* The last non-empty eh_frame section doesn't need padding. */
14838 /* Any prior sections must pad the last FDE out to the output
14839 section alignment. Otherwise we might have zero padding
14840 between sections, which would be seen as a terminator. */
14841 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14843 /* All but the last zero terminator should have been removed. */
14848 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14849 if (i
->size
!= size
)
14857 elf_link_hash_traverse (elf_hash_table (info
),
14858 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14861 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14863 const struct elf_backend_data
*bed
;
14866 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14868 s
= abfd
->sections
;
14869 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14872 bed
= get_elf_backend_data (abfd
);
14874 if (bed
->elf_backend_discard_info
!= NULL
)
14876 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14879 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14882 fini_reloc_cookie (&cookie
, abfd
);
14886 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14887 _bfd_elf_end_eh_frame_parsing (info
);
14889 if (info
->eh_frame_hdr_type
14890 && !bfd_link_relocatable (info
)
14891 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
14898 _bfd_elf_section_already_linked (bfd
*abfd
,
14900 struct bfd_link_info
*info
)
14903 const char *name
, *key
;
14904 struct bfd_section_already_linked
*l
;
14905 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14907 if (sec
->output_section
== bfd_abs_section_ptr
)
14910 flags
= sec
->flags
;
14912 /* Return if it isn't a linkonce section. A comdat group section
14913 also has SEC_LINK_ONCE set. */
14914 if ((flags
& SEC_LINK_ONCE
) == 0)
14917 /* Don't put group member sections on our list of already linked
14918 sections. They are handled as a group via their group section. */
14919 if (elf_sec_group (sec
) != NULL
)
14922 /* For a SHT_GROUP section, use the group signature as the key. */
14924 if ((flags
& SEC_GROUP
) != 0
14925 && elf_next_in_group (sec
) != NULL
14926 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14927 key
= elf_group_name (elf_next_in_group (sec
));
14930 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14931 if (startswith (name
, ".gnu.linkonce.")
14932 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14935 /* Must be a user linkonce section that doesn't follow gcc's
14936 naming convention. In this case we won't be matching
14937 single member groups. */
14941 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14943 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14945 /* We may have 2 different types of sections on the list: group
14946 sections with a signature of <key> (<key> is some string),
14947 and linkonce sections named .gnu.linkonce.<type>.<key>.
14948 Match like sections. LTO plugin sections are an exception.
14949 They are always named .gnu.linkonce.t.<key> and match either
14950 type of section. */
14951 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14952 && ((flags
& SEC_GROUP
) != 0
14953 || strcmp (name
, l
->sec
->name
) == 0))
14954 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
14955 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14957 /* The section has already been linked. See if we should
14958 issue a warning. */
14959 if (!_bfd_handle_already_linked (sec
, l
, info
))
14962 if (flags
& SEC_GROUP
)
14964 asection
*first
= elf_next_in_group (sec
);
14965 asection
*s
= first
;
14969 s
->output_section
= bfd_abs_section_ptr
;
14970 /* Record which group discards it. */
14971 s
->kept_section
= l
->sec
;
14972 s
= elf_next_in_group (s
);
14973 /* These lists are circular. */
14983 /* A single member comdat group section may be discarded by a
14984 linkonce section and vice versa. */
14985 if ((flags
& SEC_GROUP
) != 0)
14987 asection
*first
= elf_next_in_group (sec
);
14989 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14990 /* Check this single member group against linkonce sections. */
14991 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14992 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14993 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14995 first
->output_section
= bfd_abs_section_ptr
;
14996 first
->kept_section
= l
->sec
;
14997 sec
->output_section
= bfd_abs_section_ptr
;
15002 /* Check this linkonce section against single member groups. */
15003 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15004 if (l
->sec
->flags
& SEC_GROUP
)
15006 asection
*first
= elf_next_in_group (l
->sec
);
15009 && elf_next_in_group (first
) == first
15010 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
15012 sec
->output_section
= bfd_abs_section_ptr
;
15013 sec
->kept_section
= first
;
15018 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15019 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15020 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15021 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15022 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15023 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15024 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15025 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15026 The reverse order cannot happen as there is never a bfd with only the
15027 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15028 matter as here were are looking only for cross-bfd sections. */
15030 if ((flags
& SEC_GROUP
) == 0 && startswith (name
, ".gnu.linkonce.r."))
15031 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15032 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15033 && startswith (l
->sec
->name
, ".gnu.linkonce.t."))
15035 if (abfd
!= l
->sec
->owner
)
15036 sec
->output_section
= bfd_abs_section_ptr
;
15040 /* This is the first section with this name. Record it. */
15041 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
15042 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
15043 return sec
->output_section
== bfd_abs_section_ptr
;
15047 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
15049 return sym
->st_shndx
== SHN_COMMON
;
15053 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
15059 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
15061 return bfd_com_section_ptr
;
15065 _bfd_elf_default_got_elt_size (bfd
*abfd
,
15066 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
15067 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
15068 bfd
*ibfd ATTRIBUTE_UNUSED
,
15069 unsigned long symndx ATTRIBUTE_UNUSED
)
15071 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15072 return bed
->s
->arch_size
/ 8;
15075 /* Routines to support the creation of dynamic relocs. */
15077 /* Returns the name of the dynamic reloc section associated with SEC. */
15079 static const char *
15080 get_dynamic_reloc_section_name (bfd
* abfd
,
15085 const char *old_name
= bfd_section_name (sec
);
15086 const char *prefix
= is_rela
? ".rela" : ".rel";
15088 if (old_name
== NULL
)
15091 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
15092 sprintf (name
, "%s%s", prefix
, old_name
);
15097 /* Returns the dynamic reloc section associated with SEC.
15098 If necessary compute the name of the dynamic reloc section based
15099 on SEC's name (looked up in ABFD's string table) and the setting
15103 _bfd_elf_get_dynamic_reloc_section (bfd
*abfd
,
15107 asection
*reloc_sec
= elf_section_data (sec
)->sreloc
;
15109 if (reloc_sec
== NULL
)
15111 const char *name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15115 reloc_sec
= bfd_get_linker_section (abfd
, name
);
15117 if (reloc_sec
!= NULL
)
15118 elf_section_data (sec
)->sreloc
= reloc_sec
;
15125 /* Returns the dynamic reloc section associated with SEC. If the
15126 section does not exist it is created and attached to the DYNOBJ
15127 bfd and stored in the SRELOC field of SEC's elf_section_data
15130 ALIGNMENT is the alignment for the newly created section and
15131 IS_RELA defines whether the name should be .rela.<SEC's name>
15132 or .rel.<SEC's name>. The section name is looked up in the
15133 string table associated with ABFD. */
15136 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
15138 unsigned int alignment
,
15142 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
15144 if (reloc_sec
== NULL
)
15146 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15151 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
15153 if (reloc_sec
== NULL
)
15155 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
15156 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
15157 if ((sec
->flags
& SEC_ALLOC
) != 0)
15158 flags
|= SEC_ALLOC
| SEC_LOAD
;
15160 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
15161 if (reloc_sec
!= NULL
)
15163 /* _bfd_elf_get_sec_type_attr chooses a section type by
15164 name. Override as it may be wrong, eg. for a user
15165 section named "auto" we'll get ".relauto" which is
15166 seen to be a .rela section. */
15167 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
15168 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
15173 elf_section_data (sec
)->sreloc
= reloc_sec
;
15179 /* Copy the ELF symbol type and other attributes for a linker script
15180 assignment from HSRC to HDEST. Generally this should be treated as
15181 if we found a strong non-dynamic definition for HDEST (except that
15182 ld ignores multiple definition errors). */
15184 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
15185 struct bfd_link_hash_entry
*hdest
,
15186 struct bfd_link_hash_entry
*hsrc
)
15188 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
15189 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
15190 Elf_Internal_Sym isym
;
15192 ehdest
->type
= ehsrc
->type
;
15193 ehdest
->target_internal
= ehsrc
->target_internal
;
15195 isym
.st_other
= ehsrc
->other
;
15196 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, true, false);
15199 /* Append a RELA relocation REL to section S in BFD. */
15202 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15204 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15205 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
15206 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
15207 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
15210 /* Append a REL relocation REL to section S in BFD. */
15213 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15215 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15216 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
15217 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
15218 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
15221 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15223 struct bfd_link_hash_entry
*
15224 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
15225 const char *symbol
, asection
*sec
)
15227 struct elf_link_hash_entry
*h
;
15229 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
15230 false, false, true);
15231 /* NB: Common symbols will be turned into definition later. */
15233 && !h
->root
.ldscript_def
15234 && (h
->root
.type
== bfd_link_hash_undefined
15235 || h
->root
.type
== bfd_link_hash_undefweak
15236 || ((h
->ref_regular
|| h
->def_dynamic
)
15238 && h
->root
.type
!= bfd_link_hash_common
)))
15240 bool was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15241 h
->verinfo
.verdef
= NULL
;
15242 h
->root
.type
= bfd_link_hash_defined
;
15243 h
->root
.u
.def
.section
= sec
;
15244 h
->root
.u
.def
.value
= 0;
15245 h
->def_regular
= 1;
15246 h
->def_dynamic
= 0;
15248 h
->u2
.start_stop_section
= sec
;
15249 if (symbol
[0] == '.')
15251 /* .startof. and .sizeof. symbols are local. */
15252 const struct elf_backend_data
*bed
;
15253 bed
= get_elf_backend_data (info
->output_bfd
);
15254 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
15258 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15259 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15260 | info
->start_stop_visibility
);
15262 bfd_elf_link_record_dynamic_symbol (info
, h
);
15269 /* Find dynamic relocs for H that apply to read-only sections. */
15272 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15274 struct elf_dyn_relocs
*p
;
15276 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15278 asection
*s
= p
->sec
->output_section
;
15280 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15286 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15287 read-only sections. */
15290 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15294 if (h
->root
.type
== bfd_link_hash_indirect
)
15297 sec
= _bfd_elf_readonly_dynrelocs (h
);
15300 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15302 info
->flags
|= DF_TEXTREL
;
15303 /* xgettext:c-format */
15304 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15305 "in read-only section `%pA'\n"),
15306 sec
->owner
, h
->root
.root
.string
, sec
);
15308 if (bfd_link_textrel_check (info
))
15309 /* xgettext:c-format */
15310 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15311 "in read-only section `%pA'\n"),
15312 sec
->owner
, h
->root
.root
.string
, sec
);
15314 /* Not an error, just cut short the traversal. */
15320 /* Add dynamic tags. */
15323 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15324 bool need_dynamic_reloc
)
15326 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15328 if (htab
->dynamic_sections_created
)
15330 /* Add some entries to the .dynamic section. We fill in the
15331 values later, in finish_dynamic_sections, but we must add
15332 the entries now so that we get the correct size for the
15333 .dynamic section. The DT_DEBUG entry is filled in by the
15334 dynamic linker and used by the debugger. */
15335 #define add_dynamic_entry(TAG, VAL) \
15336 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15338 const struct elf_backend_data
*bed
15339 = get_elf_backend_data (output_bfd
);
15341 if (bfd_link_executable (info
))
15343 if (!add_dynamic_entry (DT_DEBUG
, 0))
15347 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15349 /* DT_PLTGOT is used by prelink even if there is no PLT
15351 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15355 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15357 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15358 || !add_dynamic_entry (DT_PLTREL
,
15359 (bed
->rela_plts_and_copies_p
15360 ? DT_RELA
: DT_REL
))
15361 || !add_dynamic_entry (DT_JMPREL
, 0))
15365 if (htab
->tlsdesc_plt
15366 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15367 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15370 if (need_dynamic_reloc
)
15372 if (bed
->rela_plts_and_copies_p
)
15374 if (!add_dynamic_entry (DT_RELA
, 0)
15375 || !add_dynamic_entry (DT_RELASZ
, 0)
15376 || !add_dynamic_entry (DT_RELAENT
,
15377 bed
->s
->sizeof_rela
))
15382 if (!add_dynamic_entry (DT_REL
, 0)
15383 || !add_dynamic_entry (DT_RELSZ
, 0)
15384 || !add_dynamic_entry (DT_RELENT
,
15385 bed
->s
->sizeof_rel
))
15389 /* If any dynamic relocs apply to a read-only section,
15390 then we need a DT_TEXTREL entry. */
15391 if ((info
->flags
& DF_TEXTREL
) == 0)
15392 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15395 if ((info
->flags
& DF_TEXTREL
) != 0)
15397 if (htab
->ifunc_resolvers
)
15398 info
->callbacks
->einfo
15399 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15400 "may result in a segfault at runtime; recompile with %s\n"),
15401 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15403 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15408 #undef add_dynamic_entry