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;
1297 else if ((oldbfd
->flags
& BFD_PLUGIN
) != 0
1298 && hi
->root
.type
== bfd_link_hash_indirect
)
1300 /* Change indirect symbol from IR to undefined. */
1301 hi
->root
.type
= bfd_link_hash_undefined
;
1302 hi
->root
.u
.undef
.abfd
= oldbfd
;
1306 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1307 respectively, appear to be a definition rather than reference. */
1309 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1311 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1312 && h
->root
.type
!= bfd_link_hash_undefweak
1313 && h
->root
.type
!= bfd_link_hash_common
);
1315 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1316 respectively, appear to be a function. */
1318 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1319 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1321 oldfunc
= (h
->type
!= STT_NOTYPE
1322 && bed
->is_function_type (h
->type
));
1324 if (!(newfunc
&& oldfunc
)
1325 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1326 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1327 && h
->type
!= STT_NOTYPE
1328 && (newdef
|| bfd_is_com_section (sec
))
1329 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1331 /* If creating a default indirect symbol ("foo" or "foo@") from
1332 a dynamic versioned definition ("foo@@") skip doing so if
1333 there is an existing regular definition with a different
1334 type. We don't want, for example, a "time" variable in the
1335 executable overriding a "time" function in a shared library. */
1343 /* When adding a symbol from a regular object file after we have
1344 created indirect symbols, undo the indirection and any
1351 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1352 h
->forced_local
= 0;
1356 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1358 h
->root
.type
= bfd_link_hash_undefined
;
1359 h
->root
.u
.undef
.abfd
= abfd
;
1363 h
->root
.type
= bfd_link_hash_new
;
1364 h
->root
.u
.undef
.abfd
= NULL
;
1370 /* Check TLS symbols. We don't check undefined symbols introduced
1371 by "ld -u" which have no type (and oldbfd NULL), and we don't
1372 check symbols from plugins because they also have no type. */
1374 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1375 && (abfd
->flags
& BFD_PLUGIN
) == 0
1376 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1377 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1381 asection
*ntsec
, *tsec
;
1383 if (h
->type
== STT_TLS
)
1404 /* xgettext:c-format */
1405 (_("%s: TLS definition in %pB section %pA "
1406 "mismatches non-TLS definition in %pB section %pA"),
1407 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1408 else if (!tdef
&& !ntdef
)
1410 /* xgettext:c-format */
1411 (_("%s: TLS reference in %pB "
1412 "mismatches non-TLS reference in %pB"),
1413 h
->root
.root
.string
, tbfd
, ntbfd
);
1416 /* xgettext:c-format */
1417 (_("%s: TLS definition in %pB section %pA "
1418 "mismatches non-TLS reference in %pB"),
1419 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1422 /* xgettext:c-format */
1423 (_("%s: TLS reference in %pB "
1424 "mismatches non-TLS definition in %pB section %pA"),
1425 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1427 bfd_set_error (bfd_error_bad_value
);
1431 /* If the old symbol has non-default visibility, we ignore the new
1432 definition from a dynamic object. */
1434 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1435 && !bfd_is_und_section (sec
))
1438 /* Make sure this symbol is dynamic. */
1440 hi
->ref_dynamic
= 1;
1441 /* A protected symbol has external availability. Make sure it is
1442 recorded as dynamic.
1444 FIXME: Should we check type and size for protected symbol? */
1445 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1446 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1451 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1454 /* If the new symbol with non-default visibility comes from a
1455 relocatable file and the old definition comes from a dynamic
1456 object, we remove the old definition. */
1457 if (hi
->root
.type
== bfd_link_hash_indirect
)
1459 /* Handle the case where the old dynamic definition is
1460 default versioned. We need to copy the symbol info from
1461 the symbol with default version to the normal one if it
1462 was referenced before. */
1465 hi
->root
.type
= h
->root
.type
;
1466 h
->root
.type
= bfd_link_hash_indirect
;
1467 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1469 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1470 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1472 /* If the new symbol is hidden or internal, completely undo
1473 any dynamic link state. */
1474 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1475 h
->forced_local
= 0;
1482 /* FIXME: Should we check type and size for protected symbol? */
1492 /* If the old symbol was undefined before, then it will still be
1493 on the undefs list. If the new symbol is undefined or
1494 common, we can't make it bfd_link_hash_new here, because new
1495 undefined or common symbols will be added to the undefs list
1496 by _bfd_generic_link_add_one_symbol. Symbols may not be
1497 added twice to the undefs list. Also, if the new symbol is
1498 undefweak then we don't want to lose the strong undef. */
1499 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1501 h
->root
.type
= bfd_link_hash_undefined
;
1502 h
->root
.u
.undef
.abfd
= abfd
;
1506 h
->root
.type
= bfd_link_hash_new
;
1507 h
->root
.u
.undef
.abfd
= NULL
;
1510 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1512 /* If the new symbol is hidden or internal, completely undo
1513 any dynamic link state. */
1514 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1515 h
->forced_local
= 0;
1521 /* FIXME: Should we check type and size for protected symbol? */
1527 /* If a new weak symbol definition comes from a regular file and the
1528 old symbol comes from a dynamic library, we treat the new one as
1529 strong. Similarly, an old weak symbol definition from a regular
1530 file is treated as strong when the new symbol comes from a dynamic
1531 library. Further, an old weak symbol from a dynamic library is
1532 treated as strong if the new symbol is from a dynamic library.
1533 This reflects the way glibc's ld.so works.
1535 Also allow a weak symbol to override a linker script symbol
1536 defined by an early pass over the script. This is done so the
1537 linker knows the symbol is defined in an object file, for the
1538 DEFINED script function.
1540 Do this before setting *type_change_ok or *size_change_ok so that
1541 we warn properly when dynamic library symbols are overridden. */
1543 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1545 if (olddef
&& newdyn
)
1548 /* Allow changes between different types of function symbol. */
1549 if (newfunc
&& oldfunc
)
1550 *type_change_ok
= true;
1552 /* It's OK to change the type if either the existing symbol or the
1553 new symbol is weak. A type change is also OK if the old symbol
1554 is undefined and the new symbol is defined. */
1559 && h
->root
.type
== bfd_link_hash_undefined
))
1560 *type_change_ok
= true;
1562 /* It's OK to change the size if either the existing symbol or the
1563 new symbol is weak, or if the old symbol is undefined. */
1566 || h
->root
.type
== bfd_link_hash_undefined
)
1567 *size_change_ok
= true;
1569 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1570 symbol, respectively, appears to be a common symbol in a dynamic
1571 object. If a symbol appears in an uninitialized section, and is
1572 not weak, and is not a function, then it may be a common symbol
1573 which was resolved when the dynamic object was created. We want
1574 to treat such symbols specially, because they raise special
1575 considerations when setting the symbol size: if the symbol
1576 appears as a common symbol in a regular object, and the size in
1577 the regular object is larger, we must make sure that we use the
1578 larger size. This problematic case can always be avoided in C,
1579 but it must be handled correctly when using Fortran shared
1582 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1583 likewise for OLDDYNCOMMON and OLDDEF.
1585 Note that this test is just a heuristic, and that it is quite
1586 possible to have an uninitialized symbol in a shared object which
1587 is really a definition, rather than a common symbol. This could
1588 lead to some minor confusion when the symbol really is a common
1589 symbol in some regular object. However, I think it will be
1595 && (sec
->flags
& SEC_ALLOC
) != 0
1596 && (sec
->flags
& SEC_LOAD
) == 0
1599 newdyncommon
= true;
1601 newdyncommon
= false;
1605 && h
->root
.type
== bfd_link_hash_defined
1607 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1608 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1611 olddyncommon
= true;
1613 olddyncommon
= false;
1615 /* We now know everything about the old and new symbols. We ask the
1616 backend to check if we can merge them. */
1617 if (bed
->merge_symbol
!= NULL
)
1619 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1624 /* There are multiple definitions of a normal symbol. Skip the
1625 default symbol as well as definition from an IR object. */
1626 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1627 && !default_sym
&& h
->def_regular
1629 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1630 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1632 /* Handle a multiple definition. */
1633 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1634 abfd
, sec
, *pvalue
);
1639 /* If both the old and the new symbols look like common symbols in a
1640 dynamic object, set the size of the symbol to the larger of the
1645 && sym
->st_size
!= h
->size
)
1647 /* Since we think we have two common symbols, issue a multiple
1648 common warning if desired. Note that we only warn if the
1649 size is different. If the size is the same, we simply let
1650 the old symbol override the new one as normally happens with
1651 symbols defined in dynamic objects. */
1653 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1654 bfd_link_hash_common
, sym
->st_size
);
1655 if (sym
->st_size
> h
->size
)
1656 h
->size
= sym
->st_size
;
1658 *size_change_ok
= true;
1661 /* If we are looking at a dynamic object, and we have found a
1662 definition, we need to see if the symbol was already defined by
1663 some other object. If so, we want to use the existing
1664 definition, and we do not want to report a multiple symbol
1665 definition error; we do this by clobbering *PSEC to be
1666 bfd_und_section_ptr.
1668 We treat a common symbol as a definition if the symbol in the
1669 shared library is a function, since common symbols always
1670 represent variables; this can cause confusion in principle, but
1671 any such confusion would seem to indicate an erroneous program or
1672 shared library. We also permit a common symbol in a regular
1673 object to override a weak symbol in a shared object. */
1678 || (h
->root
.type
== bfd_link_hash_common
1679 && (newweak
|| newfunc
))))
1683 newdyncommon
= false;
1685 *psec
= sec
= bfd_und_section_ptr
;
1686 *size_change_ok
= true;
1688 /* If we get here when the old symbol is a common symbol, then
1689 we are explicitly letting it override a weak symbol or
1690 function in a dynamic object, and we don't want to warn about
1691 a type change. If the old symbol is a defined symbol, a type
1692 change warning may still be appropriate. */
1694 if (h
->root
.type
== bfd_link_hash_common
)
1695 *type_change_ok
= true;
1698 /* Handle the special case of an old common symbol merging with a
1699 new symbol which looks like a common symbol in a shared object.
1700 We change *PSEC and *PVALUE to make the new symbol look like a
1701 common symbol, and let _bfd_generic_link_add_one_symbol do the
1705 && h
->root
.type
== bfd_link_hash_common
)
1709 newdyncommon
= false;
1710 *pvalue
= sym
->st_size
;
1711 *psec
= sec
= bed
->common_section (oldsec
);
1712 *size_change_ok
= true;
1715 /* Skip weak definitions of symbols that are already defined. */
1716 if (newdef
&& olddef
&& newweak
)
1718 /* Don't skip new non-IR weak syms. */
1719 if (!(oldbfd
!= NULL
1720 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1721 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1727 /* Merge st_other. If the symbol already has a dynamic index,
1728 but visibility says it should not be visible, turn it into a
1730 elf_merge_st_other (abfd
, h
, sym
->st_other
, sec
, newdef
, newdyn
);
1731 if (h
->dynindx
!= -1)
1732 switch (ELF_ST_VISIBILITY (h
->other
))
1736 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1741 /* If the old symbol is from a dynamic object, and the new symbol is
1742 a definition which is not from a dynamic object, then the new
1743 symbol overrides the old symbol. Symbols from regular files
1744 always take precedence over symbols from dynamic objects, even if
1745 they are defined after the dynamic object in the link.
1747 As above, we again permit a common symbol in a regular object to
1748 override a definition in a shared object if the shared object
1749 symbol is a function or is weak. */
1754 || (bfd_is_com_section (sec
)
1755 && (oldweak
|| oldfunc
)))
1760 /* Change the hash table entry to undefined, and let
1761 _bfd_generic_link_add_one_symbol do the right thing with the
1764 h
->root
.type
= bfd_link_hash_undefined
;
1765 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1766 *size_change_ok
= true;
1769 olddyncommon
= false;
1771 /* We again permit a type change when a common symbol may be
1772 overriding a function. */
1774 if (bfd_is_com_section (sec
))
1778 /* If a common symbol overrides a function, make sure
1779 that it isn't defined dynamically nor has type
1782 h
->type
= STT_NOTYPE
;
1784 *type_change_ok
= true;
1787 if (hi
->root
.type
== bfd_link_hash_indirect
)
1790 /* This union may have been set to be non-NULL when this symbol
1791 was seen in a dynamic object. We must force the union to be
1792 NULL, so that it is correct for a regular symbol. */
1793 h
->verinfo
.vertree
= NULL
;
1796 /* Handle the special case of a new common symbol merging with an
1797 old symbol that looks like it might be a common symbol defined in
1798 a shared object. Note that we have already handled the case in
1799 which a new common symbol should simply override the definition
1800 in the shared library. */
1803 && bfd_is_com_section (sec
)
1806 /* It would be best if we could set the hash table entry to a
1807 common symbol, but we don't know what to use for the section
1808 or the alignment. */
1809 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1810 bfd_link_hash_common
, sym
->st_size
);
1812 /* If the presumed common symbol in the dynamic object is
1813 larger, pretend that the new symbol has its size. */
1815 if (h
->size
> *pvalue
)
1818 /* We need to remember the alignment required by the symbol
1819 in the dynamic object. */
1820 BFD_ASSERT (pold_alignment
);
1821 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1824 olddyncommon
= false;
1826 h
->root
.type
= bfd_link_hash_undefined
;
1827 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1829 *size_change_ok
= true;
1830 *type_change_ok
= true;
1832 if (hi
->root
.type
== bfd_link_hash_indirect
)
1835 h
->verinfo
.vertree
= NULL
;
1840 /* Handle the case where we had a versioned symbol in a dynamic
1841 library and now find a definition in a normal object. In this
1842 case, we make the versioned symbol point to the normal one. */
1843 flip
->root
.type
= h
->root
.type
;
1844 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1845 h
->root
.type
= bfd_link_hash_indirect
;
1846 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1847 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1851 flip
->ref_dynamic
= 1;
1858 /* This function is called to create an indirect symbol from the
1859 default for the symbol with the default version if needed. The
1860 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1861 set DYNSYM if the new indirect symbol is dynamic. */
1864 _bfd_elf_add_default_symbol (bfd
*abfd
,
1865 struct bfd_link_info
*info
,
1866 struct elf_link_hash_entry
*h
,
1868 Elf_Internal_Sym
*sym
,
1874 bool type_change_ok
;
1875 bool size_change_ok
;
1878 struct elf_link_hash_entry
*hi
;
1879 struct bfd_link_hash_entry
*bh
;
1880 const struct elf_backend_data
*bed
;
1885 size_t len
, shortlen
;
1889 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1892 /* If this symbol has a version, and it is the default version, we
1893 create an indirect symbol from the default name to the fully
1894 decorated name. This will cause external references which do not
1895 specify a version to be bound to this version of the symbol. */
1896 p
= strchr (name
, ELF_VER_CHR
);
1897 if (h
->versioned
== unknown
)
1901 h
->versioned
= unversioned
;
1906 if (p
[1] != ELF_VER_CHR
)
1908 h
->versioned
= versioned_hidden
;
1912 h
->versioned
= versioned
;
1917 /* PR ld/19073: We may see an unversioned definition after the
1923 bed
= get_elf_backend_data (abfd
);
1924 collect
= bed
->collect
;
1925 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1927 shortlen
= p
- name
;
1928 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1929 if (shortname
== NULL
)
1931 memcpy (shortname
, name
, shortlen
);
1932 shortname
[shortlen
] = '\0';
1934 /* We are going to create a new symbol. Merge it with any existing
1935 symbol with this name. For the purposes of the merge, act as
1936 though we were defining the symbol we just defined, although we
1937 actually going to define an indirect symbol. */
1938 type_change_ok
= false;
1939 size_change_ok
= false;
1942 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1943 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1944 &type_change_ok
, &size_change_ok
, &matched
))
1950 if (hi
->def_regular
|| ELF_COMMON_DEF_P (hi
))
1952 /* If the undecorated symbol will have a version added by a
1953 script different to H, then don't indirect to/from the
1954 undecorated symbol. This isn't ideal because we may not yet
1955 have seen symbol versions, if given by a script on the
1956 command line rather than via --version-script. */
1957 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1962 = bfd_find_version_for_sym (info
->version_info
,
1963 hi
->root
.root
.string
, &hide
);
1964 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1966 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
1970 if (hi
->verinfo
.vertree
!= NULL
1971 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1977 /* Add the default symbol if not performing a relocatable link. */
1978 if (! bfd_link_relocatable (info
))
1981 if (bh
->type
== bfd_link_hash_defined
1982 && bh
->u
.def
.section
->owner
!= NULL
1983 && (bh
->u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
1985 /* Mark the previous definition from IR object as
1986 undefined so that the generic linker will override
1988 bh
->type
= bfd_link_hash_undefined
;
1989 bh
->u
.undef
.abfd
= bh
->u
.def
.section
->owner
;
1991 if (! (_bfd_generic_link_add_one_symbol
1992 (info
, abfd
, shortname
, BSF_INDIRECT
,
1993 bfd_ind_section_ptr
,
1994 0, name
, false, collect
, &bh
)))
1996 hi
= (struct elf_link_hash_entry
*) bh
;
2001 /* In this case the symbol named SHORTNAME is overriding the
2002 indirect symbol we want to add. We were planning on making
2003 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2004 is the name without a version. NAME is the fully versioned
2005 name, and it is the default version.
2007 Overriding means that we already saw a definition for the
2008 symbol SHORTNAME in a regular object, and it is overriding
2009 the symbol defined in the dynamic object.
2011 When this happens, we actually want to change NAME, the
2012 symbol we just added, to refer to SHORTNAME. This will cause
2013 references to NAME in the shared object to become references
2014 to SHORTNAME in the regular object. This is what we expect
2015 when we override a function in a shared object: that the
2016 references in the shared object will be mapped to the
2017 definition in the regular object. */
2019 while (hi
->root
.type
== bfd_link_hash_indirect
2020 || hi
->root
.type
== bfd_link_hash_warning
)
2021 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2023 h
->root
.type
= bfd_link_hash_indirect
;
2024 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
2028 hi
->ref_dynamic
= 1;
2032 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
2037 /* Now set HI to H, so that the following code will set the
2038 other fields correctly. */
2042 /* Check if HI is a warning symbol. */
2043 if (hi
->root
.type
== bfd_link_hash_warning
)
2044 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2046 /* If there is a duplicate definition somewhere, then HI may not
2047 point to an indirect symbol. We will have reported an error to
2048 the user in that case. */
2050 if (hi
->root
.type
== bfd_link_hash_indirect
)
2052 struct elf_link_hash_entry
*ht
;
2054 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2055 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2057 /* If we first saw a reference to SHORTNAME with non-default
2058 visibility, merge that visibility to the @@VER symbol. */
2059 elf_merge_st_other (abfd
, ht
, hi
->other
, sec
, true, dynamic
);
2061 /* A reference to the SHORTNAME symbol from a dynamic library
2062 will be satisfied by the versioned symbol at runtime. In
2063 effect, we have a reference to the versioned symbol. */
2064 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2065 hi
->dynamic_def
|= ht
->dynamic_def
;
2067 /* See if the new flags lead us to realize that the symbol must
2073 if (! bfd_link_executable (info
)
2080 if (hi
->ref_regular
)
2086 /* We also need to define an indirection from the nondefault version
2090 len
= strlen (name
);
2091 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2092 if (shortname
== NULL
)
2094 memcpy (shortname
, name
, shortlen
);
2095 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2097 /* Once again, merge with any existing symbol. */
2098 type_change_ok
= false;
2099 size_change_ok
= false;
2101 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2102 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2103 &type_change_ok
, &size_change_ok
, &matched
))
2109 && h
->root
.type
== bfd_link_hash_defweak
2110 && hi
->root
.type
== bfd_link_hash_defined
)
2112 /* We are handling a weak sym@@ver and attempting to define
2113 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2114 new weak sym@ver because there is already a strong sym@ver.
2115 However, sym@ver and sym@@ver are really the same symbol.
2116 The existing strong sym@ver ought to override sym@@ver. */
2117 h
->root
.type
= bfd_link_hash_defined
;
2118 h
->root
.u
.def
.section
= hi
->root
.u
.def
.section
;
2119 h
->root
.u
.def
.value
= hi
->root
.u
.def
.value
;
2120 hi
->root
.type
= bfd_link_hash_indirect
;
2121 hi
->root
.u
.i
.link
= &h
->root
;
2128 /* Here SHORTNAME is a versioned name, so we don't expect to see
2129 the type of override we do in the case above unless it is
2130 overridden by a versioned definition. */
2131 if (hi
->root
.type
!= bfd_link_hash_defined
2132 && hi
->root
.type
!= bfd_link_hash_defweak
)
2134 /* xgettext:c-format */
2135 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2142 if (! (_bfd_generic_link_add_one_symbol
2143 (info
, abfd
, shortname
, BSF_INDIRECT
,
2144 bfd_ind_section_ptr
, 0, name
, false, collect
, &bh
)))
2146 hi
= (struct elf_link_hash_entry
*) bh
;
2149 /* If there is a duplicate definition somewhere, then HI may not
2150 point to an indirect symbol. We will have reported an error
2151 to the user in that case. */
2152 if (hi
->root
.type
== bfd_link_hash_indirect
)
2154 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2155 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2156 hi
->dynamic_def
|= h
->dynamic_def
;
2158 /* If we first saw a reference to @VER symbol with
2159 non-default visibility, merge that visibility to the
2161 elf_merge_st_other (abfd
, h
, hi
->other
, sec
, true, dynamic
);
2163 /* See if the new flags lead us to realize that the symbol
2169 if (! bfd_link_executable (info
)
2175 if (hi
->ref_regular
)
2184 /* This routine is used to export all defined symbols into the dynamic
2185 symbol table. It is called via elf_link_hash_traverse. */
2188 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2190 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2192 /* Ignore indirect symbols. These are added by the versioning code. */
2193 if (h
->root
.type
== bfd_link_hash_indirect
)
2196 /* Ignore this if we won't export it. */
2197 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2200 if (h
->dynindx
== -1
2201 && (h
->def_regular
|| h
->ref_regular
)
2202 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2203 h
->root
.root
.string
))
2205 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2215 /* Return true if GLIBC_ABI_DT_RELR is added to the list of version
2216 dependencies successfully. GLIBC_ABI_DT_RELR will be put into the
2217 .gnu.version_r section. */
2220 elf_link_add_dt_relr_dependency (struct elf_find_verdep_info
*rinfo
)
2222 bfd
*glibc_bfd
= NULL
;
2223 Elf_Internal_Verneed
*t
;
2224 Elf_Internal_Vernaux
*a
;
2226 const char *relr
= "GLIBC_ABI_DT_RELR";
2228 /* See if we already know about GLIBC_PRIVATE_DT_RELR. */
2229 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2233 const char *soname
= bfd_elf_get_dt_soname (t
->vn_bfd
);
2234 /* Skip the shared library if it isn't libc.so. */
2235 if (!soname
|| !startswith (soname
, "libc.so."))
2238 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2240 /* Return if GLIBC_PRIVATE_DT_RELR dependency has been
2242 if (a
->vna_nodename
== relr
2243 || strcmp (a
->vna_nodename
, relr
) == 0)
2246 /* Check if libc.so provides GLIBC_2.XX version. */
2247 if (!glibc_bfd
&& startswith (a
->vna_nodename
, "GLIBC_2."))
2248 glibc_bfd
= t
->vn_bfd
;
2254 /* Skip if it isn't linked against glibc. */
2255 if (glibc_bfd
== NULL
)
2258 /* This is a new version. Add it to tree we are building. */
2262 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
,
2266 rinfo
->failed
= true;
2270 t
->vn_bfd
= glibc_bfd
;
2271 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2272 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2276 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2279 rinfo
->failed
= true;
2283 a
->vna_nodename
= relr
;
2285 a
->vna_nextptr
= t
->vn_auxptr
;
2286 a
->vna_other
= rinfo
->vers
+ 1;
2294 /* Look through the symbols which are defined in other shared
2295 libraries and referenced here. Update the list of version
2296 dependencies. This will be put into the .gnu.version_r section.
2297 This function is called via elf_link_hash_traverse. */
2300 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2303 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2304 Elf_Internal_Verneed
*t
;
2305 Elf_Internal_Vernaux
*a
;
2308 /* We only care about symbols defined in shared objects with version
2313 || h
->verinfo
.verdef
== NULL
2314 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2315 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2318 /* See if we already know about this version. */
2319 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2323 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2326 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2327 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2333 /* This is a new version. Add it to tree we are building. */
2338 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2341 rinfo
->failed
= true;
2345 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2346 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2347 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2351 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2354 rinfo
->failed
= true;
2358 /* Note that we are copying a string pointer here, and testing it
2359 above. If bfd_elf_string_from_elf_section is ever changed to
2360 discard the string data when low in memory, this will have to be
2362 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2364 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2365 a
->vna_nextptr
= t
->vn_auxptr
;
2367 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2370 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2377 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2378 hidden. Set *T_P to NULL if there is no match. */
2381 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2382 struct elf_link_hash_entry
*h
,
2383 const char *version_p
,
2384 struct bfd_elf_version_tree
**t_p
,
2387 struct bfd_elf_version_tree
*t
;
2389 /* Look for the version. If we find it, it is no longer weak. */
2390 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2392 if (strcmp (t
->name
, version_p
) == 0)
2396 struct bfd_elf_version_expr
*d
;
2398 len
= version_p
- h
->root
.root
.string
;
2399 alc
= (char *) bfd_malloc (len
);
2402 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2403 alc
[len
- 1] = '\0';
2404 if (alc
[len
- 2] == ELF_VER_CHR
)
2405 alc
[len
- 2] = '\0';
2407 h
->verinfo
.vertree
= t
;
2411 if (t
->globals
.list
!= NULL
)
2412 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2414 /* See if there is anything to force this symbol to
2416 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2418 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2421 && ! info
->export_dynamic
)
2435 /* Return TRUE if the symbol H is hidden by version script. */
2438 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2439 struct elf_link_hash_entry
*h
)
2443 const struct elf_backend_data
*bed
2444 = get_elf_backend_data (info
->output_bfd
);
2446 /* Version script only hides symbols defined in regular objects. */
2447 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2450 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2451 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2453 struct bfd_elf_version_tree
*t
;
2456 if (*p
== ELF_VER_CHR
)
2460 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2464 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2469 /* If we don't have a version for this symbol, see if we can find
2471 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2474 = bfd_find_version_for_sym (info
->version_info
,
2475 h
->root
.root
.string
, &hide
);
2476 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2478 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2486 /* Figure out appropriate versions for all the symbols. We may not
2487 have the version number script until we have read all of the input
2488 files, so until that point we don't know which symbols should be
2489 local. This function is called via elf_link_hash_traverse. */
2492 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2494 struct elf_info_failed
*sinfo
;
2495 struct bfd_link_info
*info
;
2496 const struct elf_backend_data
*bed
;
2497 struct elf_info_failed eif
;
2501 sinfo
= (struct elf_info_failed
*) data
;
2504 /* Fix the symbol flags. */
2507 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2510 sinfo
->failed
= true;
2514 bed
= get_elf_backend_data (info
->output_bfd
);
2516 /* We only need version numbers for symbols defined in regular
2518 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2520 /* Hide symbols defined in discarded input sections. */
2521 if ((h
->root
.type
== bfd_link_hash_defined
2522 || h
->root
.type
== bfd_link_hash_defweak
)
2523 && discarded_section (h
->root
.u
.def
.section
))
2524 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2529 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2530 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2532 struct bfd_elf_version_tree
*t
;
2535 if (*p
== ELF_VER_CHR
)
2538 /* If there is no version string, we can just return out. */
2542 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2544 sinfo
->failed
= true;
2549 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2551 /* If we are building an application, we need to create a
2552 version node for this version. */
2553 if (t
== NULL
&& bfd_link_executable (info
))
2555 struct bfd_elf_version_tree
**pp
;
2558 /* If we aren't going to export this symbol, we don't need
2559 to worry about it. */
2560 if (h
->dynindx
== -1)
2563 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2567 sinfo
->failed
= true;
2572 t
->name_indx
= (unsigned int) -1;
2576 /* Don't count anonymous version tag. */
2577 if (sinfo
->info
->version_info
!= NULL
2578 && sinfo
->info
->version_info
->vernum
== 0)
2580 for (pp
= &sinfo
->info
->version_info
;
2584 t
->vernum
= version_index
;
2588 h
->verinfo
.vertree
= t
;
2592 /* We could not find the version for a symbol when
2593 generating a shared archive. Return an error. */
2595 /* xgettext:c-format */
2596 (_("%pB: version node not found for symbol %s"),
2597 info
->output_bfd
, h
->root
.root
.string
);
2598 bfd_set_error (bfd_error_bad_value
);
2599 sinfo
->failed
= true;
2604 /* If we don't have a version for this symbol, see if we can find
2607 && h
->verinfo
.vertree
== NULL
2608 && sinfo
->info
->version_info
!= NULL
)
2611 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2612 h
->root
.root
.string
, &hide
);
2613 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2614 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2620 /* Read and swap the relocs from the section indicated by SHDR. This
2621 may be either a REL or a RELA section. The relocations are
2622 translated into RELA relocations and stored in INTERNAL_RELOCS,
2623 which should have already been allocated to contain enough space.
2624 The EXTERNAL_RELOCS are a buffer where the external form of the
2625 relocations should be stored.
2627 Returns FALSE if something goes wrong. */
2630 elf_link_read_relocs_from_section (bfd
*abfd
,
2632 Elf_Internal_Shdr
*shdr
,
2633 void *external_relocs
,
2634 Elf_Internal_Rela
*internal_relocs
)
2636 const struct elf_backend_data
*bed
;
2637 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2638 const bfd_byte
*erela
;
2639 const bfd_byte
*erelaend
;
2640 Elf_Internal_Rela
*irela
;
2641 Elf_Internal_Shdr
*symtab_hdr
;
2644 /* Position ourselves at the start of the section. */
2645 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2648 /* Read the relocations. */
2649 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2652 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2653 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2655 bed
= get_elf_backend_data (abfd
);
2657 /* Convert the external relocations to the internal format. */
2658 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2659 swap_in
= bed
->s
->swap_reloc_in
;
2660 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2661 swap_in
= bed
->s
->swap_reloca_in
;
2664 bfd_set_error (bfd_error_wrong_format
);
2668 erela
= (const bfd_byte
*) external_relocs
;
2669 /* Setting erelaend like this and comparing with <= handles case of
2670 a fuzzed object with sh_size not a multiple of sh_entsize. */
2671 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2672 irela
= internal_relocs
;
2673 while (erela
<= erelaend
)
2677 (*swap_in
) (abfd
, erela
, irela
);
2678 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2679 if (bed
->s
->arch_size
== 64)
2683 if ((size_t) r_symndx
>= nsyms
)
2686 /* xgettext:c-format */
2687 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2688 " for offset %#" PRIx64
" in section `%pA'"),
2689 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2690 (uint64_t) irela
->r_offset
, sec
);
2691 bfd_set_error (bfd_error_bad_value
);
2695 else if (r_symndx
!= STN_UNDEF
)
2698 /* xgettext:c-format */
2699 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2700 " for offset %#" PRIx64
" in section `%pA'"
2701 " when the object file has no symbol table"),
2702 abfd
, (uint64_t) r_symndx
,
2703 (uint64_t) irela
->r_offset
, sec
);
2704 bfd_set_error (bfd_error_bad_value
);
2707 irela
+= bed
->s
->int_rels_per_ext_rel
;
2708 erela
+= shdr
->sh_entsize
;
2714 /* Read and swap the relocs for a section O. They may have been
2715 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2716 not NULL, they are used as buffers to read into. They are known to
2717 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2718 the return value is allocated using either malloc or bfd_alloc,
2719 according to the KEEP_MEMORY argument. If O has two relocation
2720 sections (both REL and RELA relocations), then the REL_HDR
2721 relocations will appear first in INTERNAL_RELOCS, followed by the
2722 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2723 update cache_size. */
2726 _bfd_elf_link_info_read_relocs (bfd
*abfd
,
2727 struct bfd_link_info
*info
,
2729 void *external_relocs
,
2730 Elf_Internal_Rela
*internal_relocs
,
2733 void *alloc1
= NULL
;
2734 Elf_Internal_Rela
*alloc2
= NULL
;
2735 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2736 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2737 Elf_Internal_Rela
*internal_rela_relocs
;
2739 if (esdo
->relocs
!= NULL
)
2740 return esdo
->relocs
;
2742 if (o
->reloc_count
== 0)
2745 if (internal_relocs
== NULL
)
2749 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2752 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2754 info
->cache_size
+= size
;
2757 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2758 if (internal_relocs
== NULL
)
2762 if (external_relocs
== NULL
)
2764 bfd_size_type size
= 0;
2767 size
+= esdo
->rel
.hdr
->sh_size
;
2769 size
+= esdo
->rela
.hdr
->sh_size
;
2771 alloc1
= bfd_malloc (size
);
2774 external_relocs
= alloc1
;
2777 internal_rela_relocs
= internal_relocs
;
2780 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2784 external_relocs
= (((bfd_byte
*) external_relocs
)
2785 + esdo
->rel
.hdr
->sh_size
);
2786 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2787 * bed
->s
->int_rels_per_ext_rel
);
2791 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2793 internal_rela_relocs
)))
2796 /* Cache the results for next time, if we can. */
2798 esdo
->relocs
= internal_relocs
;
2802 /* Don't free alloc2, since if it was allocated we are passing it
2803 back (under the name of internal_relocs). */
2805 return internal_relocs
;
2812 bfd_release (abfd
, alloc2
);
2819 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2820 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2821 struct bfd_link_info. */
2824 _bfd_elf_link_read_relocs (bfd
*abfd
,
2826 void *external_relocs
,
2827 Elf_Internal_Rela
*internal_relocs
,
2830 return _bfd_elf_link_info_read_relocs (abfd
, NULL
, o
, external_relocs
,
2831 internal_relocs
, keep_memory
);
2835 /* Compute the size of, and allocate space for, REL_HDR which is the
2836 section header for a section containing relocations for O. */
2839 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2840 struct bfd_elf_section_reloc_data
*reldata
)
2842 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2844 /* That allows us to calculate the size of the section. */
2845 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2847 /* The contents field must last into write_object_contents, so we
2848 allocate it with bfd_alloc rather than malloc. Also since we
2849 cannot be sure that the contents will actually be filled in,
2850 we zero the allocated space. */
2851 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2852 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2855 if (reldata
->hashes
== NULL
&& reldata
->count
)
2857 struct elf_link_hash_entry
**p
;
2859 p
= ((struct elf_link_hash_entry
**)
2860 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2864 reldata
->hashes
= p
;
2870 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2871 originated from the section given by INPUT_REL_HDR) to the
2875 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2876 asection
*input_section
,
2877 Elf_Internal_Shdr
*input_rel_hdr
,
2878 Elf_Internal_Rela
*internal_relocs
,
2879 struct elf_link_hash_entry
**rel_hash
2882 Elf_Internal_Rela
*irela
;
2883 Elf_Internal_Rela
*irelaend
;
2885 struct bfd_elf_section_reloc_data
*output_reldata
;
2886 asection
*output_section
;
2887 const struct elf_backend_data
*bed
;
2888 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2889 struct bfd_elf_section_data
*esdo
;
2891 output_section
= input_section
->output_section
;
2893 bed
= get_elf_backend_data (output_bfd
);
2894 esdo
= elf_section_data (output_section
);
2895 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2897 output_reldata
= &esdo
->rel
;
2898 swap_out
= bed
->s
->swap_reloc_out
;
2900 else if (esdo
->rela
.hdr
2901 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2903 output_reldata
= &esdo
->rela
;
2904 swap_out
= bed
->s
->swap_reloca_out
;
2909 /* xgettext:c-format */
2910 (_("%pB: relocation size mismatch in %pB section %pA"),
2911 output_bfd
, input_section
->owner
, input_section
);
2912 bfd_set_error (bfd_error_wrong_format
);
2916 erel
= output_reldata
->hdr
->contents
;
2917 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2918 irela
= internal_relocs
;
2919 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2920 * bed
->s
->int_rels_per_ext_rel
);
2921 while (irela
< irelaend
)
2923 (*swap_out
) (output_bfd
, irela
, erel
);
2924 irela
+= bed
->s
->int_rels_per_ext_rel
;
2925 erel
+= input_rel_hdr
->sh_entsize
;
2928 /* Bump the counter, so that we know where to add the next set of
2930 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2935 /* Make weak undefined symbols in PIE dynamic. */
2938 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2939 struct elf_link_hash_entry
*h
)
2941 if (bfd_link_pie (info
)
2943 && h
->root
.type
== bfd_link_hash_undefweak
)
2944 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2949 /* Fix up the flags for a symbol. This handles various cases which
2950 can only be fixed after all the input files are seen. This is
2951 currently called by both adjust_dynamic_symbol and
2952 assign_sym_version, which is unnecessary but perhaps more robust in
2953 the face of future changes. */
2956 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2957 struct elf_info_failed
*eif
)
2959 const struct elf_backend_data
*bed
;
2961 /* If this symbol was mentioned in a non-ELF file, try to set
2962 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2963 permit a non-ELF file to correctly refer to a symbol defined in
2964 an ELF dynamic object. */
2967 while (h
->root
.type
== bfd_link_hash_indirect
)
2968 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2970 if (h
->root
.type
!= bfd_link_hash_defined
2971 && h
->root
.type
!= bfd_link_hash_defweak
)
2974 h
->ref_regular_nonweak
= 1;
2978 if (h
->root
.u
.def
.section
->owner
!= NULL
2979 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2980 == bfd_target_elf_flavour
))
2983 h
->ref_regular_nonweak
= 1;
2989 if (h
->dynindx
== -1
2993 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3002 /* Unfortunately, NON_ELF is only correct if the symbol
3003 was first seen in a non-ELF file. Fortunately, if the symbol
3004 was first seen in an ELF file, we're probably OK unless the
3005 symbol was defined in a non-ELF file. Catch that case here.
3006 FIXME: We're still in trouble if the symbol was first seen in
3007 a dynamic object, and then later in a non-ELF regular object. */
3008 if ((h
->root
.type
== bfd_link_hash_defined
3009 || h
->root
.type
== bfd_link_hash_defweak
)
3011 && (h
->root
.u
.def
.section
->owner
!= NULL
3012 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3013 != bfd_target_elf_flavour
)
3014 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3015 && !h
->def_dynamic
)))
3019 /* Backend specific symbol fixup. */
3020 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3021 if (bed
->elf_backend_fixup_symbol
3022 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
3025 /* If this is a final link, and the symbol was defined as a common
3026 symbol in a regular object file, and there was no definition in
3027 any dynamic object, then the linker will have allocated space for
3028 the symbol in a common section but the DEF_REGULAR
3029 flag will not have been set. */
3030 if (h
->root
.type
== bfd_link_hash_defined
3034 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
3037 /* Symbols defined in discarded sections shouldn't be dynamic. */
3038 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
3039 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3041 /* If a weak undefined symbol has non-default visibility, we also
3042 hide it from the dynamic linker. */
3043 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
3044 && h
->root
.type
== bfd_link_hash_undefweak
)
3045 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3047 /* A hidden versioned symbol in executable should be forced local if
3048 it is is locally defined, not referenced by shared library and not
3050 else if (bfd_link_executable (eif
->info
)
3051 && h
->versioned
== versioned_hidden
3052 && !eif
->info
->export_dynamic
3056 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3058 /* If -Bsymbolic was used (which means to bind references to global
3059 symbols to the definition within the shared object), and this
3060 symbol was defined in a regular object, then it actually doesn't
3061 need a PLT entry. Likewise, if the symbol has non-default
3062 visibility. If the symbol has hidden or internal visibility, we
3063 will force it local. */
3064 else if (h
->needs_plt
3065 && bfd_link_pic (eif
->info
)
3066 && is_elf_hash_table (eif
->info
->hash
)
3067 && (SYMBOLIC_BIND (eif
->info
, h
)
3068 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
3073 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3074 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
3075 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3078 /* If this is a weak defined symbol in a dynamic object, and we know
3079 the real definition in the dynamic object, copy interesting flags
3080 over to the real definition. */
3081 if (h
->is_weakalias
)
3083 struct elf_link_hash_entry
*def
= weakdef (h
);
3085 /* If the real definition is defined by a regular object file,
3086 don't do anything special. See the longer description in
3087 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3088 bfd_link_hash_defined as it was when put on the alias list
3089 then it must have originally been a versioned symbol (for
3090 which a non-versioned indirect symbol is created) and later
3091 a definition for the non-versioned symbol is found. In that
3092 case the indirection is flipped with the versioned symbol
3093 becoming an indirect pointing at the non-versioned symbol.
3094 Thus, not an alias any more. */
3095 if (def
->def_regular
3096 || def
->root
.type
!= bfd_link_hash_defined
)
3099 while ((h
= h
->u
.alias
) != def
)
3100 h
->is_weakalias
= 0;
3104 while (h
->root
.type
== bfd_link_hash_indirect
)
3105 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3106 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3107 || h
->root
.type
== bfd_link_hash_defweak
);
3108 BFD_ASSERT (def
->def_dynamic
);
3109 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
3116 /* Make the backend pick a good value for a dynamic symbol. This is
3117 called via elf_link_hash_traverse, and also calls itself
3121 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
3123 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3124 struct elf_link_hash_table
*htab
;
3125 const struct elf_backend_data
*bed
;
3127 if (! is_elf_hash_table (eif
->info
->hash
))
3130 /* Ignore indirect symbols. These are added by the versioning code. */
3131 if (h
->root
.type
== bfd_link_hash_indirect
)
3134 /* Fix the symbol flags. */
3135 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
3138 htab
= elf_hash_table (eif
->info
);
3139 bed
= get_elf_backend_data (htab
->dynobj
);
3141 if (h
->root
.type
== bfd_link_hash_undefweak
)
3143 if (eif
->info
->dynamic_undefined_weak
== 0)
3144 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3145 else if (eif
->info
->dynamic_undefined_weak
> 0
3147 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3148 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
3149 h
->root
.root
.string
))
3151 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3159 /* If this symbol does not require a PLT entry, and it is not
3160 defined by a dynamic object, or is not referenced by a regular
3161 object, ignore it. We do have to handle a weak defined symbol,
3162 even if no regular object refers to it, if we decided to add it
3163 to the dynamic symbol table. FIXME: Do we normally need to worry
3164 about symbols which are defined by one dynamic object and
3165 referenced by another one? */
3167 && h
->type
!= STT_GNU_IFUNC
3171 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3173 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3177 /* If we've already adjusted this symbol, don't do it again. This
3178 can happen via a recursive call. */
3179 if (h
->dynamic_adjusted
)
3182 /* Don't look at this symbol again. Note that we must set this
3183 after checking the above conditions, because we may look at a
3184 symbol once, decide not to do anything, and then get called
3185 recursively later after REF_REGULAR is set below. */
3186 h
->dynamic_adjusted
= 1;
3188 /* If this is a weak definition, and we know a real definition, and
3189 the real symbol is not itself defined by a regular object file,
3190 then get a good value for the real definition. We handle the
3191 real symbol first, for the convenience of the backend routine.
3193 Note that there is a confusing case here. If the real definition
3194 is defined by a regular object file, we don't get the real symbol
3195 from the dynamic object, but we do get the weak symbol. If the
3196 processor backend uses a COPY reloc, then if some routine in the
3197 dynamic object changes the real symbol, we will not see that
3198 change in the corresponding weak symbol. This is the way other
3199 ELF linkers work as well, and seems to be a result of the shared
3202 I will clarify this issue. Most SVR4 shared libraries define the
3203 variable _timezone and define timezone as a weak synonym. The
3204 tzset call changes _timezone. If you write
3205 extern int timezone;
3207 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3208 you might expect that, since timezone is a synonym for _timezone,
3209 the same number will print both times. However, if the processor
3210 backend uses a COPY reloc, then actually timezone will be copied
3211 into your process image, and, since you define _timezone
3212 yourself, _timezone will not. Thus timezone and _timezone will
3213 wind up at different memory locations. The tzset call will set
3214 _timezone, leaving timezone unchanged. */
3216 if (h
->is_weakalias
)
3218 struct elf_link_hash_entry
*def
= weakdef (h
);
3220 /* If we get to this point, there is an implicit reference to
3221 the alias by a regular object file via the weak symbol H. */
3222 def
->ref_regular
= 1;
3224 /* Ensure that the backend adjust_dynamic_symbol function sees
3225 the strong alias before H by recursively calling ourselves. */
3226 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3230 /* If a symbol has no type and no size and does not require a PLT
3231 entry, then we are probably about to do the wrong thing here: we
3232 are probably going to create a COPY reloc for an empty object.
3233 This case can arise when a shared object is built with assembly
3234 code, and the assembly code fails to set the symbol type. */
3236 && h
->type
== STT_NOTYPE
3239 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3240 h
->root
.root
.string
);
3242 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3251 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3255 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3256 struct elf_link_hash_entry
*h
,
3259 unsigned int power_of_two
;
3261 asection
*sec
= h
->root
.u
.def
.section
;
3263 /* The section alignment of the definition is the maximum alignment
3264 requirement of symbols defined in the section. Since we don't
3265 know the symbol alignment requirement, we start with the
3266 maximum alignment and check low bits of the symbol address
3267 for the minimum alignment. */
3268 power_of_two
= bfd_section_alignment (sec
);
3269 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3270 while ((h
->root
.u
.def
.value
& mask
) != 0)
3276 if (power_of_two
> bfd_section_alignment (dynbss
))
3278 /* Adjust the section alignment if needed. */
3279 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3283 /* We make sure that the symbol will be aligned properly. */
3284 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3286 /* Define the symbol as being at this point in DYNBSS. */
3287 h
->root
.u
.def
.section
= dynbss
;
3288 h
->root
.u
.def
.value
= dynbss
->size
;
3290 /* Increment the size of DYNBSS to make room for the symbol. */
3291 dynbss
->size
+= h
->size
;
3293 /* No error if extern_protected_data is true. */
3294 if (h
->protected_def
3295 && (!info
->extern_protected_data
3296 || (info
->extern_protected_data
< 0
3297 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3298 info
->callbacks
->einfo
3299 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3300 h
->root
.root
.string
);
3305 /* Adjust all external symbols pointing into SEC_MERGE sections
3306 to reflect the object merging within the sections. */
3309 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3313 if ((h
->root
.type
== bfd_link_hash_defined
3314 || h
->root
.type
== bfd_link_hash_defweak
)
3315 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3316 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3318 bfd
*output_bfd
= (bfd
*) data
;
3320 h
->root
.u
.def
.value
=
3321 _bfd_merged_section_offset (output_bfd
,
3322 &h
->root
.u
.def
.section
,
3323 elf_section_data (sec
)->sec_info
,
3324 h
->root
.u
.def
.value
);
3330 /* Returns false if the symbol referred to by H should be considered
3331 to resolve local to the current module, and true if it should be
3332 considered to bind dynamically. */
3335 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3336 struct bfd_link_info
*info
,
3337 bool not_local_protected
)
3339 bool binding_stays_local_p
;
3340 const struct elf_backend_data
*bed
;
3341 struct elf_link_hash_table
*hash_table
;
3346 while (h
->root
.type
== bfd_link_hash_indirect
3347 || h
->root
.type
== bfd_link_hash_warning
)
3348 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3350 /* If it was forced local, then clearly it's not dynamic. */
3351 if (h
->dynindx
== -1)
3353 if (h
->forced_local
)
3356 /* Identify the cases where name binding rules say that a
3357 visible symbol resolves locally. */
3358 binding_stays_local_p
= (bfd_link_executable (info
)
3359 || SYMBOLIC_BIND (info
, h
));
3361 switch (ELF_ST_VISIBILITY (h
->other
))
3368 hash_table
= elf_hash_table (info
);
3369 if (!is_elf_hash_table (&hash_table
->root
))
3372 bed
= get_elf_backend_data (hash_table
->dynobj
);
3374 /* Proper resolution for function pointer equality may require
3375 that these symbols perhaps be resolved dynamically, even though
3376 we should be resolving them to the current module. */
3377 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3378 binding_stays_local_p
= true;
3385 /* If it isn't defined locally, then clearly it's dynamic. */
3386 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3389 /* Otherwise, the symbol is dynamic if binding rules don't tell
3390 us that it remains local. */
3391 return !binding_stays_local_p
;
3394 /* Return true if the symbol referred to by H should be considered
3395 to resolve local to the current module, and false otherwise. Differs
3396 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3397 undefined symbols. The two functions are virtually identical except
3398 for the place where dynindx == -1 is tested. If that test is true,
3399 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3400 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3402 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3403 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3404 treatment of undefined weak symbols. For those that do not make
3405 undefined weak symbols dynamic, both functions may return false. */
3408 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3409 struct bfd_link_info
*info
,
3410 bool local_protected
)
3412 const struct elf_backend_data
*bed
;
3413 struct elf_link_hash_table
*hash_table
;
3415 /* If it's a local sym, of course we resolve locally. */
3419 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3420 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3421 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3424 /* Forced local symbols resolve locally. */
3425 if (h
->forced_local
)
3428 /* Common symbols that become definitions don't get the DEF_REGULAR
3429 flag set, so test it first, and don't bail out. */
3430 if (ELF_COMMON_DEF_P (h
))
3432 /* If we don't have a definition in a regular file, then we can't
3433 resolve locally. The sym is either undefined or dynamic. */
3434 else if (!h
->def_regular
)
3437 /* Non-dynamic symbols resolve locally. */
3438 if (h
->dynindx
== -1)
3441 /* At this point, we know the symbol is defined and dynamic. In an
3442 executable it must resolve locally, likewise when building symbolic
3443 shared libraries. */
3444 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3447 /* Now deal with defined dynamic symbols in shared libraries. Ones
3448 with default visibility might not resolve locally. */
3449 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3452 hash_table
= elf_hash_table (info
);
3453 if (!is_elf_hash_table (&hash_table
->root
))
3456 /* STV_PROTECTED symbols with indirect external access are local. */
3457 if (info
->indirect_extern_access
> 0)
3460 bed
= get_elf_backend_data (hash_table
->dynobj
);
3462 /* If extern_protected_data is false, STV_PROTECTED non-function
3463 symbols are local. */
3464 if ((!info
->extern_protected_data
3465 || (info
->extern_protected_data
< 0
3466 && !bed
->extern_protected_data
))
3467 && !bed
->is_function_type (h
->type
))
3470 /* Function pointer equality tests may require that STV_PROTECTED
3471 symbols be treated as dynamic symbols. If the address of a
3472 function not defined in an executable is set to that function's
3473 plt entry in the executable, then the address of the function in
3474 a shared library must also be the plt entry in the executable. */
3475 return local_protected
;
3478 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3479 aligned. Returns the first TLS output section. */
3481 struct bfd_section
*
3482 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3484 struct bfd_section
*sec
, *tls
;
3485 unsigned int align
= 0;
3487 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3488 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3492 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3493 if (sec
->alignment_power
> align
)
3494 align
= sec
->alignment_power
;
3496 elf_hash_table (info
)->tls_sec
= tls
;
3498 /* Ensure the alignment of the first section (usually .tdata) is the largest
3499 alignment, so that the tls segment starts aligned. */
3501 tls
->alignment_power
= align
;
3506 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3508 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3509 Elf_Internal_Sym
*sym
)
3511 const struct elf_backend_data
*bed
;
3513 /* Local symbols do not count, but target specific ones might. */
3514 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3515 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3518 bed
= get_elf_backend_data (abfd
);
3519 /* Function symbols do not count. */
3520 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3523 /* If the section is undefined, then so is the symbol. */
3524 if (sym
->st_shndx
== SHN_UNDEF
)
3527 /* If the symbol is defined in the common section, then
3528 it is a common definition and so does not count. */
3529 if (bed
->common_definition (sym
))
3532 /* If the symbol is in a target specific section then we
3533 must rely upon the backend to tell us what it is. */
3534 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3535 /* FIXME - this function is not coded yet:
3537 return _bfd_is_global_symbol_definition (abfd, sym);
3539 Instead for now assume that the definition is not global,
3540 Even if this is wrong, at least the linker will behave
3541 in the same way that it used to do. */
3547 /* Search the symbol table of the archive element of the archive ABFD
3548 whose archive map contains a mention of SYMDEF, and determine if
3549 the symbol is defined in this element. */
3551 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3553 Elf_Internal_Shdr
* hdr
;
3557 Elf_Internal_Sym
*isymbuf
;
3558 Elf_Internal_Sym
*isym
;
3559 Elf_Internal_Sym
*isymend
;
3562 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
, NULL
);
3566 if (! bfd_check_format (abfd
, bfd_object
))
3569 /* Select the appropriate symbol table. If we don't know if the
3570 object file is an IR object, give linker LTO plugin a chance to
3571 get the correct symbol table. */
3572 if (abfd
->plugin_format
== bfd_plugin_yes
3573 #if BFD_SUPPORTS_PLUGINS
3574 || (abfd
->plugin_format
== bfd_plugin_unknown
3575 && bfd_link_plugin_object_p (abfd
))
3579 /* Use the IR symbol table if the object has been claimed by
3581 abfd
= abfd
->plugin_dummy_bfd
;
3582 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3584 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3585 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3587 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3589 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3591 /* The sh_info field of the symtab header tells us where the
3592 external symbols start. We don't care about the local symbols. */
3593 if (elf_bad_symtab (abfd
))
3595 extsymcount
= symcount
;
3600 extsymcount
= symcount
- hdr
->sh_info
;
3601 extsymoff
= hdr
->sh_info
;
3604 if (extsymcount
== 0)
3607 /* Read in the symbol table. */
3608 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3610 if (isymbuf
== NULL
)
3613 /* Scan the symbol table looking for SYMDEF. */
3615 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3619 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3624 if (strcmp (name
, symdef
->name
) == 0)
3626 result
= is_global_data_symbol_definition (abfd
, isym
);
3636 /* Add an entry to the .dynamic table. */
3639 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3643 struct elf_link_hash_table
*hash_table
;
3644 const struct elf_backend_data
*bed
;
3646 bfd_size_type newsize
;
3647 bfd_byte
*newcontents
;
3648 Elf_Internal_Dyn dyn
;
3650 hash_table
= elf_hash_table (info
);
3651 if (! is_elf_hash_table (&hash_table
->root
))
3654 if (tag
== DT_RELA
|| tag
== DT_REL
)
3655 hash_table
->dynamic_relocs
= true;
3657 bed
= get_elf_backend_data (hash_table
->dynobj
);
3658 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3659 BFD_ASSERT (s
!= NULL
);
3661 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3662 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3663 if (newcontents
== NULL
)
3667 dyn
.d_un
.d_val
= val
;
3668 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3671 s
->contents
= newcontents
;
3676 /* Strip zero-sized dynamic sections. */
3679 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3681 struct elf_link_hash_table
*hash_table
;
3682 const struct elf_backend_data
*bed
;
3683 asection
*s
, *sdynamic
, **pp
;
3684 asection
*rela_dyn
, *rel_dyn
;
3685 Elf_Internal_Dyn dyn
;
3686 bfd_byte
*extdyn
, *next
;
3687 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3688 bool strip_zero_sized
;
3689 bool strip_zero_sized_plt
;
3691 if (bfd_link_relocatable (info
))
3694 hash_table
= elf_hash_table (info
);
3695 if (!is_elf_hash_table (&hash_table
->root
))
3698 if (!hash_table
->dynobj
)
3701 sdynamic
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3705 bed
= get_elf_backend_data (hash_table
->dynobj
);
3706 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3708 strip_zero_sized
= false;
3709 strip_zero_sized_plt
= false;
3711 /* Strip zero-sized dynamic sections. */
3712 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3713 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3714 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3718 || s
== hash_table
->srelplt
->output_section
3719 || s
== hash_table
->splt
->output_section
))
3722 info
->output_bfd
->section_count
--;
3723 strip_zero_sized
= true;
3728 else if (s
== hash_table
->splt
->output_section
)
3730 s
= hash_table
->splt
;
3731 strip_zero_sized_plt
= true;
3734 s
= hash_table
->srelplt
;
3735 s
->flags
|= SEC_EXCLUDE
;
3736 s
->output_section
= bfd_abs_section_ptr
;
3741 if (strip_zero_sized_plt
&& sdynamic
->size
!= 0)
3742 for (extdyn
= sdynamic
->contents
;
3743 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3746 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3747 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3755 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3756 the procedure linkage table (the .plt section) has been
3758 memmove (extdyn
, next
,
3759 sdynamic
->size
- (next
- sdynamic
->contents
));
3764 if (strip_zero_sized
)
3766 /* Regenerate program headers. */
3767 elf_seg_map (info
->output_bfd
) = NULL
;
3768 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
,
3775 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3776 1 if a DT_NEEDED tag already exists, and 0 on success. */
3779 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3781 struct elf_link_hash_table
*hash_table
;
3785 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3788 hash_table
= elf_hash_table (info
);
3789 soname
= elf_dt_name (abfd
);
3790 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, false);
3791 if (strindex
== (size_t) -1)
3794 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3797 const struct elf_backend_data
*bed
;
3800 bed
= get_elf_backend_data (hash_table
->dynobj
);
3801 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3802 if (sdyn
!= NULL
&& sdyn
->size
!= 0)
3803 for (extdyn
= sdyn
->contents
;
3804 extdyn
< sdyn
->contents
+ sdyn
->size
;
3805 extdyn
+= bed
->s
->sizeof_dyn
)
3807 Elf_Internal_Dyn dyn
;
3809 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3810 if (dyn
.d_tag
== DT_NEEDED
3811 && dyn
.d_un
.d_val
== strindex
)
3813 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3819 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3822 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3828 /* Return true if SONAME is on the needed list between NEEDED and STOP
3829 (or the end of list if STOP is NULL), and needed by a library that
3833 on_needed_list (const char *soname
,
3834 struct bfd_link_needed_list
*needed
,
3835 struct bfd_link_needed_list
*stop
)
3837 struct bfd_link_needed_list
*look
;
3838 for (look
= needed
; look
!= stop
; look
= look
->next
)
3839 if (strcmp (soname
, look
->name
) == 0
3840 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3841 /* If needed by a library that itself is not directly
3842 needed, recursively check whether that library is
3843 indirectly needed. Since we add DT_NEEDED entries to
3844 the end of the list, library dependencies appear after
3845 the library. Therefore search prior to the current
3846 LOOK, preventing possible infinite recursion. */
3847 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3853 /* Sort symbol by value, section, size, and type. */
3855 elf_sort_symbol (const void *arg1
, const void *arg2
)
3857 const struct elf_link_hash_entry
*h1
;
3858 const struct elf_link_hash_entry
*h2
;
3859 bfd_signed_vma vdiff
;
3864 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3865 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3866 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3868 return vdiff
> 0 ? 1 : -1;
3870 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3874 /* Sort so that sized symbols are selected over zero size symbols. */
3875 vdiff
= h1
->size
- h2
->size
;
3877 return vdiff
> 0 ? 1 : -1;
3879 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3880 if (h1
->type
!= h2
->type
)
3881 return h1
->type
- h2
->type
;
3883 /* If symbols are properly sized and typed, and multiple strong
3884 aliases are not defined in a shared library by the user we
3885 shouldn't get here. Unfortunately linker script symbols like
3886 __bss_start sometimes match a user symbol defined at the start of
3887 .bss without proper size and type. We'd like to preference the
3888 user symbol over reserved system symbols. Sort on leading
3890 n1
= h1
->root
.root
.string
;
3891 n2
= h2
->root
.root
.string
;
3904 /* Final sort on name selects user symbols like '_u' over reserved
3905 system symbols like '_Z' and also will avoid qsort instability. */
3909 /* This function is used to adjust offsets into .dynstr for
3910 dynamic symbols. This is called via elf_link_hash_traverse. */
3913 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3915 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3917 if (h
->dynindx
!= -1)
3918 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3922 /* Assign string offsets in .dynstr, update all structures referencing
3926 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3928 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3929 struct elf_link_local_dynamic_entry
*entry
;
3930 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3931 bfd
*dynobj
= hash_table
->dynobj
;
3934 const struct elf_backend_data
*bed
;
3937 _bfd_elf_strtab_finalize (dynstr
);
3938 size
= _bfd_elf_strtab_size (dynstr
);
3940 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3942 if (info
->callbacks
->examine_strtab
)
3943 info
->callbacks
->examine_strtab (dynstr
);
3945 bed
= get_elf_backend_data (dynobj
);
3946 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3947 BFD_ASSERT (sdyn
!= NULL
);
3949 /* Update all .dynamic entries referencing .dynstr strings. */
3950 for (extdyn
= sdyn
->contents
;
3951 extdyn
< PTR_ADD (sdyn
->contents
, sdyn
->size
);
3952 extdyn
+= bed
->s
->sizeof_dyn
)
3954 Elf_Internal_Dyn dyn
;
3956 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3960 dyn
.d_un
.d_val
= size
;
3970 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3975 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3978 /* Now update local dynamic symbols. */
3979 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3980 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3981 entry
->isym
.st_name
);
3983 /* And the rest of dynamic symbols. */
3984 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3986 /* Adjust version definitions. */
3987 if (elf_tdata (output_bfd
)->cverdefs
)
3992 Elf_Internal_Verdef def
;
3993 Elf_Internal_Verdaux defaux
;
3995 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3999 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
4001 p
+= sizeof (Elf_External_Verdef
);
4002 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
4004 for (i
= 0; i
< def
.vd_cnt
; ++i
)
4006 _bfd_elf_swap_verdaux_in (output_bfd
,
4007 (Elf_External_Verdaux
*) p
, &defaux
);
4008 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
4010 _bfd_elf_swap_verdaux_out (output_bfd
,
4011 &defaux
, (Elf_External_Verdaux
*) p
);
4012 p
+= sizeof (Elf_External_Verdaux
);
4015 while (def
.vd_next
);
4018 /* Adjust version references. */
4019 if (elf_tdata (output_bfd
)->verref
)
4024 Elf_Internal_Verneed need
;
4025 Elf_Internal_Vernaux needaux
;
4027 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
4031 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
4033 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
4034 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
4035 (Elf_External_Verneed
*) p
);
4036 p
+= sizeof (Elf_External_Verneed
);
4037 for (i
= 0; i
< need
.vn_cnt
; ++i
)
4039 _bfd_elf_swap_vernaux_in (output_bfd
,
4040 (Elf_External_Vernaux
*) p
, &needaux
);
4041 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
4043 _bfd_elf_swap_vernaux_out (output_bfd
,
4045 (Elf_External_Vernaux
*) p
);
4046 p
+= sizeof (Elf_External_Vernaux
);
4049 while (need
.vn_next
);
4055 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4056 The default is to only match when the INPUT and OUTPUT are exactly
4060 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
4061 const bfd_target
*output
)
4063 return input
== output
;
4066 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4067 This version is used when different targets for the same architecture
4068 are virtually identical. */
4071 _bfd_elf_relocs_compatible (const bfd_target
*input
,
4072 const bfd_target
*output
)
4074 const struct elf_backend_data
*obed
, *ibed
;
4076 if (input
== output
)
4079 ibed
= xvec_get_elf_backend_data (input
);
4080 obed
= xvec_get_elf_backend_data (output
);
4082 if (ibed
->arch
!= obed
->arch
)
4085 /* If both backends are using this function, deem them compatible. */
4086 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
4089 /* Make a special call to the linker "notice" function to tell it that
4090 we are about to handle an as-needed lib, or have finished
4091 processing the lib. */
4094 _bfd_elf_notice_as_needed (bfd
*ibfd
,
4095 struct bfd_link_info
*info
,
4096 enum notice_asneeded_action act
)
4098 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
4101 /* Call ACTION on each relocation in an ELF object file. */
4104 _bfd_elf_link_iterate_on_relocs
4105 (bfd
*abfd
, struct bfd_link_info
*info
,
4106 bool (*action
) (bfd
*, struct bfd_link_info
*, asection
*,
4107 const Elf_Internal_Rela
*))
4109 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4110 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4112 /* If this object is the same format as the output object, and it is
4113 not a shared library, then let the backend look through the
4116 This is required to build global offset table entries and to
4117 arrange for dynamic relocs. It is not required for the
4118 particular common case of linking non PIC code, even when linking
4119 against shared libraries, but unfortunately there is no way of
4120 knowing whether an object file has been compiled PIC or not.
4121 Looking through the relocs is not particularly time consuming.
4122 The problem is that we must either (1) keep the relocs in memory,
4123 which causes the linker to require additional runtime memory or
4124 (2) read the relocs twice from the input file, which wastes time.
4125 This would be a good case for using mmap.
4127 I have no idea how to handle linking PIC code into a file of a
4128 different format. It probably can't be done. */
4129 if ((abfd
->flags
& DYNAMIC
) == 0
4130 && is_elf_hash_table (&htab
->root
)
4131 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4132 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4136 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4138 Elf_Internal_Rela
*internal_relocs
;
4141 /* Don't check relocations in excluded sections. Don't do
4142 anything special with non-loaded, non-alloced sections.
4143 In particular, any relocs in such sections should not
4144 affect GOT and PLT reference counting (ie. we don't
4145 allow them to create GOT or PLT entries), there's no
4146 possibility or desire to optimize TLS relocs, and
4147 there's not much point in propagating relocs to shared
4148 libs that the dynamic linker won't relocate. */
4149 if ((o
->flags
& SEC_ALLOC
) == 0
4150 || (o
->flags
& SEC_RELOC
) == 0
4151 || (o
->flags
& SEC_EXCLUDE
) != 0
4152 || o
->reloc_count
== 0
4153 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4154 && (o
->flags
& SEC_DEBUGGING
) != 0)
4155 || bfd_is_abs_section (o
->output_section
))
4158 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
4161 _bfd_link_keep_memory (info
));
4162 if (internal_relocs
== NULL
)
4165 ok
= action (abfd
, info
, o
, internal_relocs
);
4167 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4168 free (internal_relocs
);
4178 /* Check relocations in an ELF object file. This is called after
4179 all input files have been opened. */
4182 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
4184 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4185 if (bed
->check_relocs
!= NULL
)
4186 return _bfd_elf_link_iterate_on_relocs (abfd
, info
,
4191 /* Add symbols from an ELF object file to the linker hash table. */
4194 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4196 Elf_Internal_Ehdr
*ehdr
;
4197 Elf_Internal_Shdr
*hdr
;
4201 struct elf_link_hash_entry
**sym_hash
;
4203 Elf_External_Versym
*extversym
= NULL
;
4204 Elf_External_Versym
*extversym_end
= NULL
;
4205 Elf_External_Versym
*ever
;
4206 struct elf_link_hash_entry
*weaks
;
4207 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4208 size_t nondeflt_vers_cnt
= 0;
4209 Elf_Internal_Sym
*isymbuf
= NULL
;
4210 Elf_Internal_Sym
*isym
;
4211 Elf_Internal_Sym
*isymend
;
4212 const struct elf_backend_data
*bed
;
4214 struct elf_link_hash_table
*htab
;
4215 void *alloc_mark
= NULL
;
4216 struct bfd_hash_entry
**old_table
= NULL
;
4217 unsigned int old_size
= 0;
4218 unsigned int old_count
= 0;
4219 void *old_tab
= NULL
;
4221 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4222 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4223 void *old_strtab
= NULL
;
4228 htab
= elf_hash_table (info
);
4229 bed
= get_elf_backend_data (abfd
);
4231 if ((abfd
->flags
& DYNAMIC
) == 0)
4237 /* You can't use -r against a dynamic object. Also, there's no
4238 hope of using a dynamic object which does not exactly match
4239 the format of the output file. */
4240 if (bfd_link_relocatable (info
)
4241 || !is_elf_hash_table (&htab
->root
)
4242 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4244 if (bfd_link_relocatable (info
))
4245 bfd_set_error (bfd_error_invalid_operation
);
4247 bfd_set_error (bfd_error_wrong_format
);
4252 ehdr
= elf_elfheader (abfd
);
4253 if (info
->warn_alternate_em
4254 && bed
->elf_machine_code
!= ehdr
->e_machine
4255 && ((bed
->elf_machine_alt1
!= 0
4256 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4257 || (bed
->elf_machine_alt2
!= 0
4258 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4260 /* xgettext:c-format */
4261 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4262 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4264 /* As a GNU extension, any input sections which are named
4265 .gnu.warning.SYMBOL are treated as warning symbols for the given
4266 symbol. This differs from .gnu.warning sections, which generate
4267 warnings when they are included in an output file. */
4268 /* PR 12761: Also generate this warning when building shared libraries. */
4269 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4273 name
= bfd_section_name (s
);
4274 if (startswith (name
, ".gnu.warning."))
4279 name
+= sizeof ".gnu.warning." - 1;
4281 /* If this is a shared object, then look up the symbol
4282 in the hash table. If it is there, and it is already
4283 been defined, then we will not be using the entry
4284 from this shared object, so we don't need to warn.
4285 FIXME: If we see the definition in a regular object
4286 later on, we will warn, but we shouldn't. The only
4287 fix is to keep track of what warnings we are supposed
4288 to emit, and then handle them all at the end of the
4292 struct elf_link_hash_entry
*h
;
4294 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
4296 /* FIXME: What about bfd_link_hash_common? */
4298 && (h
->root
.type
== bfd_link_hash_defined
4299 || h
->root
.type
== bfd_link_hash_defweak
))
4304 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4308 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4313 if (! (_bfd_generic_link_add_one_symbol
4314 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4315 false, bed
->collect
, NULL
)))
4318 if (bfd_link_executable (info
))
4320 /* Clobber the section size so that the warning does
4321 not get copied into the output file. */
4324 /* Also set SEC_EXCLUDE, so that symbols defined in
4325 the warning section don't get copied to the output. */
4326 s
->flags
|= SEC_EXCLUDE
;
4331 just_syms
= ((s
= abfd
->sections
) != NULL
4332 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4337 /* If we are creating a shared library, create all the dynamic
4338 sections immediately. We need to attach them to something,
4339 so we attach them to this BFD, provided it is the right
4340 format and is not from ld --just-symbols. Always create the
4341 dynamic sections for -E/--dynamic-list. FIXME: If there
4342 are no input BFD's of the same format as the output, we can't
4343 make a shared library. */
4345 && (bfd_link_pic (info
)
4346 || (!bfd_link_relocatable (info
)
4348 && (info
->export_dynamic
|| info
->dynamic
)))
4349 && is_elf_hash_table (&htab
->root
)
4350 && info
->output_bfd
->xvec
== abfd
->xvec
4351 && !htab
->dynamic_sections_created
)
4353 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4357 else if (!is_elf_hash_table (&htab
->root
))
4361 const char *soname
= NULL
;
4363 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4364 const Elf_Internal_Phdr
*phdr
;
4365 struct elf_link_loaded_list
*loaded_lib
;
4367 /* ld --just-symbols and dynamic objects don't mix very well.
4368 ld shouldn't allow it. */
4372 /* If this dynamic lib was specified on the command line with
4373 --as-needed in effect, then we don't want to add a DT_NEEDED
4374 tag unless the lib is actually used. Similary for libs brought
4375 in by another lib's DT_NEEDED. When --no-add-needed is used
4376 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4377 any dynamic library in DT_NEEDED tags in the dynamic lib at
4379 add_needed
= (elf_dyn_lib_class (abfd
)
4380 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4381 | DYN_NO_NEEDED
)) == 0;
4383 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4384 if (s
!= NULL
&& s
->size
!= 0)
4388 unsigned int elfsec
;
4389 unsigned long shlink
;
4391 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4398 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4399 if (elfsec
== SHN_BAD
)
4400 goto error_free_dyn
;
4401 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4403 for (extdyn
= dynbuf
;
4404 extdyn
<= dynbuf
+ s
->size
- bed
->s
->sizeof_dyn
;
4405 extdyn
+= bed
->s
->sizeof_dyn
)
4407 Elf_Internal_Dyn dyn
;
4409 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4410 if (dyn
.d_tag
== DT_SONAME
)
4412 unsigned int tagv
= dyn
.d_un
.d_val
;
4413 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4415 goto error_free_dyn
;
4417 if (dyn
.d_tag
== DT_NEEDED
)
4419 struct bfd_link_needed_list
*n
, **pn
;
4421 unsigned int tagv
= dyn
.d_un
.d_val
;
4422 size_t amt
= sizeof (struct bfd_link_needed_list
);
4424 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4425 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4426 if (n
== NULL
|| fnm
== NULL
)
4427 goto error_free_dyn
;
4428 amt
= strlen (fnm
) + 1;
4429 anm
= (char *) bfd_alloc (abfd
, amt
);
4431 goto error_free_dyn
;
4432 memcpy (anm
, fnm
, amt
);
4436 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4440 if (dyn
.d_tag
== DT_RUNPATH
)
4442 struct bfd_link_needed_list
*n
, **pn
;
4444 unsigned int tagv
= dyn
.d_un
.d_val
;
4445 size_t amt
= sizeof (struct bfd_link_needed_list
);
4447 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4448 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4449 if (n
== NULL
|| fnm
== NULL
)
4450 goto error_free_dyn
;
4451 amt
= strlen (fnm
) + 1;
4452 anm
= (char *) bfd_alloc (abfd
, amt
);
4454 goto error_free_dyn
;
4455 memcpy (anm
, fnm
, amt
);
4459 for (pn
= & runpath
;
4465 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4466 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4468 struct bfd_link_needed_list
*n
, **pn
;
4470 unsigned int tagv
= dyn
.d_un
.d_val
;
4471 size_t amt
= sizeof (struct bfd_link_needed_list
);
4473 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4474 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4475 if (n
== NULL
|| fnm
== NULL
)
4476 goto error_free_dyn
;
4477 amt
= strlen (fnm
) + 1;
4478 anm
= (char *) bfd_alloc (abfd
, amt
);
4480 goto error_free_dyn
;
4481 memcpy (anm
, fnm
, amt
);
4491 if (dyn
.d_tag
== DT_AUDIT
)
4493 unsigned int tagv
= dyn
.d_un
.d_val
;
4494 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4496 if (dyn
.d_tag
== DT_FLAGS_1
)
4497 elf_tdata (abfd
)->is_pie
= (dyn
.d_un
.d_val
& DF_1_PIE
) != 0;
4503 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4504 frees all more recently bfd_alloc'd blocks as well. */
4510 struct bfd_link_needed_list
**pn
;
4511 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4516 /* If we have a PT_GNU_RELRO program header, mark as read-only
4517 all sections contained fully therein. This makes relro
4518 shared library sections appear as they will at run-time. */
4519 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4520 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4521 if (phdr
->p_type
== PT_GNU_RELRO
)
4523 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4525 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4527 if ((s
->flags
& SEC_ALLOC
) != 0
4528 && s
->vma
* opb
>= phdr
->p_vaddr
4529 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4530 s
->flags
|= SEC_READONLY
;
4535 /* We do not want to include any of the sections in a dynamic
4536 object in the output file. We hack by simply clobbering the
4537 list of sections in the BFD. This could be handled more
4538 cleanly by, say, a new section flag; the existing
4539 SEC_NEVER_LOAD flag is not the one we want, because that one
4540 still implies that the section takes up space in the output
4542 bfd_section_list_clear (abfd
);
4544 /* Find the name to use in a DT_NEEDED entry that refers to this
4545 object. If the object has a DT_SONAME entry, we use it.
4546 Otherwise, if the generic linker stuck something in
4547 elf_dt_name, we use that. Otherwise, we just use the file
4549 if (soname
== NULL
|| *soname
== '\0')
4551 soname
= elf_dt_name (abfd
);
4552 if (soname
== NULL
|| *soname
== '\0')
4553 soname
= bfd_get_filename (abfd
);
4556 /* Save the SONAME because sometimes the linker emulation code
4557 will need to know it. */
4558 elf_dt_name (abfd
) = soname
;
4560 /* If we have already included this dynamic object in the
4561 link, just ignore it. There is no reason to include a
4562 particular dynamic object more than once. */
4563 for (loaded_lib
= htab
->dyn_loaded
;
4565 loaded_lib
= loaded_lib
->next
)
4567 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4571 /* Create dynamic sections for backends that require that be done
4572 before setup_gnu_properties. */
4574 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4577 /* Save the DT_AUDIT entry for the linker emulation code. */
4578 elf_dt_audit (abfd
) = audit
;
4581 /* If this is a dynamic object, we always link against the .dynsym
4582 symbol table, not the .symtab symbol table. The dynamic linker
4583 will only see the .dynsym symbol table, so there is no reason to
4584 look at .symtab for a dynamic object. */
4586 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4587 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4589 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4591 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4593 /* The sh_info field of the symtab header tells us where the
4594 external symbols start. We don't care about the local symbols at
4596 if (elf_bad_symtab (abfd
))
4598 extsymcount
= symcount
;
4603 extsymcount
= symcount
- hdr
->sh_info
;
4604 extsymoff
= hdr
->sh_info
;
4607 sym_hash
= elf_sym_hashes (abfd
);
4608 if (extsymcount
!= 0)
4610 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4612 if (isymbuf
== NULL
)
4615 if (sym_hash
== NULL
)
4617 /* We store a pointer to the hash table entry for each
4619 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4620 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4621 if (sym_hash
== NULL
)
4622 goto error_free_sym
;
4623 elf_sym_hashes (abfd
) = sym_hash
;
4629 /* Read in any version definitions. */
4630 if (!_bfd_elf_slurp_version_tables (abfd
,
4631 info
->default_imported_symver
))
4632 goto error_free_sym
;
4634 /* Read in the symbol versions, but don't bother to convert them
4635 to internal format. */
4636 if (elf_dynversym (abfd
) != 0)
4638 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4639 bfd_size_type amt
= versymhdr
->sh_size
;
4641 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4642 goto error_free_sym
;
4643 extversym
= (Elf_External_Versym
*)
4644 _bfd_malloc_and_read (abfd
, amt
, amt
);
4645 if (extversym
== NULL
)
4646 goto error_free_sym
;
4647 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4651 /* If we are loading an as-needed shared lib, save the symbol table
4652 state before we start adding symbols. If the lib turns out
4653 to be unneeded, restore the state. */
4654 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4659 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4661 struct bfd_hash_entry
*p
;
4662 struct elf_link_hash_entry
*h
;
4664 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4666 h
= (struct elf_link_hash_entry
*) p
;
4667 entsize
+= htab
->root
.table
.entsize
;
4668 if (h
->root
.type
== bfd_link_hash_warning
)
4670 entsize
+= htab
->root
.table
.entsize
;
4671 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4673 if (h
->root
.type
== bfd_link_hash_common
)
4674 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4678 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4679 old_tab
= bfd_malloc (tabsize
+ entsize
);
4680 if (old_tab
== NULL
)
4681 goto error_free_vers
;
4683 /* Remember the current objalloc pointer, so that all mem for
4684 symbols added can later be reclaimed. */
4685 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4686 if (alloc_mark
== NULL
)
4687 goto error_free_vers
;
4689 /* Make a special call to the linker "notice" function to
4690 tell it that we are about to handle an as-needed lib. */
4691 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4692 goto error_free_vers
;
4694 /* Clone the symbol table. Remember some pointers into the
4695 symbol table, and dynamic symbol count. */
4696 old_ent
= (char *) old_tab
+ tabsize
;
4697 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4698 old_undefs
= htab
->root
.undefs
;
4699 old_undefs_tail
= htab
->root
.undefs_tail
;
4700 old_table
= htab
->root
.table
.table
;
4701 old_size
= htab
->root
.table
.size
;
4702 old_count
= htab
->root
.table
.count
;
4704 if (htab
->dynstr
!= NULL
)
4706 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4707 if (old_strtab
== NULL
)
4708 goto error_free_vers
;
4711 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4713 struct bfd_hash_entry
*p
;
4714 struct elf_link_hash_entry
*h
;
4716 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4718 h
= (struct elf_link_hash_entry
*) p
;
4719 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4720 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4721 if (h
->root
.type
== bfd_link_hash_warning
)
4723 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4724 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4725 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4727 if (h
->root
.type
== bfd_link_hash_common
)
4729 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4730 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4737 if (extversym
== NULL
)
4739 else if (extversym
+ extsymoff
< extversym_end
)
4740 ever
= extversym
+ extsymoff
;
4743 /* xgettext:c-format */
4744 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4745 abfd
, (long) extsymoff
,
4746 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4747 bfd_set_error (bfd_error_bad_value
);
4748 goto error_free_vers
;
4751 if (!bfd_link_relocatable (info
)
4752 && abfd
->lto_slim_object
)
4755 (_("%pB: plugin needed to handle lto object"), abfd
);
4758 for (isym
= isymbuf
, isymend
= PTR_ADD (isymbuf
, extsymcount
);
4760 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4764 asection
*sec
, *new_sec
;
4767 struct elf_link_hash_entry
*h
;
4768 struct elf_link_hash_entry
*hi
;
4770 bool size_change_ok
;
4771 bool type_change_ok
;
4777 unsigned int old_alignment
;
4778 unsigned int shindex
;
4784 flags
= BSF_NO_FLAGS
;
4786 value
= isym
->st_value
;
4787 common
= bed
->common_definition (isym
);
4788 if (common
&& info
->inhibit_common_definition
)
4790 /* Treat common symbol as undefined for --no-define-common. */
4791 isym
->st_shndx
= SHN_UNDEF
;
4796 bind
= ELF_ST_BIND (isym
->st_info
);
4800 /* This should be impossible, since ELF requires that all
4801 global symbols follow all local symbols, and that sh_info
4802 point to the first global symbol. Unfortunately, Irix 5
4804 if (elf_bad_symtab (abfd
))
4807 /* If we aren't prepared to handle locals within the globals
4808 then we'll likely segfault on a NULL symbol hash if the
4809 symbol is ever referenced in relocations. */
4810 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4811 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4812 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4813 " (>= sh_info of %lu)"),
4814 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4817 /* Dynamic object relocations are not processed by ld, so
4818 ld won't run into the problem mentioned above. */
4821 bfd_set_error (bfd_error_bad_value
);
4822 goto error_free_vers
;
4825 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4833 case STB_GNU_UNIQUE
:
4834 flags
= BSF_GNU_UNIQUE
;
4838 /* Leave it up to the processor backend. */
4842 if (isym
->st_shndx
== SHN_UNDEF
)
4843 sec
= bfd_und_section_ptr
;
4844 else if (isym
->st_shndx
== SHN_ABS
)
4845 sec
= bfd_abs_section_ptr
;
4846 else if (isym
->st_shndx
== SHN_COMMON
)
4848 sec
= bfd_com_section_ptr
;
4849 /* What ELF calls the size we call the value. What ELF
4850 calls the value we call the alignment. */
4851 value
= isym
->st_size
;
4855 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4857 sec
= bfd_abs_section_ptr
;
4858 else if (discarded_section (sec
))
4860 /* Symbols from discarded section are undefined. We keep
4862 sec
= bfd_und_section_ptr
;
4864 isym
->st_shndx
= SHN_UNDEF
;
4866 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4870 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4873 goto error_free_vers
;
4875 if (isym
->st_shndx
== SHN_COMMON
4876 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4878 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4882 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4884 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4886 goto error_free_vers
;
4890 else if (isym
->st_shndx
== SHN_COMMON
4891 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4892 && !bfd_link_relocatable (info
))
4894 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4898 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4899 | SEC_LINKER_CREATED
);
4900 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4902 goto error_free_vers
;
4906 else if (bed
->elf_add_symbol_hook
)
4908 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4910 goto error_free_vers
;
4912 /* The hook function sets the name to NULL if this symbol
4913 should be skipped for some reason. */
4918 /* Sanity check that all possibilities were handled. */
4922 /* Silently discard TLS symbols from --just-syms. There's
4923 no way to combine a static TLS block with a new TLS block
4924 for this executable. */
4925 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4926 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4929 if (bfd_is_und_section (sec
)
4930 || bfd_is_com_section (sec
))
4935 size_change_ok
= false;
4936 type_change_ok
= bed
->type_change_ok
;
4943 if (is_elf_hash_table (&htab
->root
))
4945 Elf_Internal_Versym iver
;
4946 unsigned int vernum
= 0;
4951 if (info
->default_imported_symver
)
4952 /* Use the default symbol version created earlier. */
4953 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4957 else if (ever
>= extversym_end
)
4959 /* xgettext:c-format */
4960 _bfd_error_handler (_("%pB: not enough version information"),
4962 bfd_set_error (bfd_error_bad_value
);
4963 goto error_free_vers
;
4966 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4968 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4970 /* If this is a hidden symbol, or if it is not version
4971 1, we append the version name to the symbol name.
4972 However, we do not modify a non-hidden absolute symbol
4973 if it is not a function, because it might be the version
4974 symbol itself. FIXME: What if it isn't? */
4975 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4977 && (!bfd_is_abs_section (sec
)
4978 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4981 size_t namelen
, verlen
, newlen
;
4984 if (isym
->st_shndx
!= SHN_UNDEF
)
4986 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4988 else if (vernum
> 1)
4990 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4997 /* xgettext:c-format */
4998 (_("%pB: %s: invalid version %u (max %d)"),
5000 elf_tdata (abfd
)->cverdefs
);
5001 bfd_set_error (bfd_error_bad_value
);
5002 goto error_free_vers
;
5007 /* We cannot simply test for the number of
5008 entries in the VERNEED section since the
5009 numbers for the needed versions do not start
5011 Elf_Internal_Verneed
*t
;
5014 for (t
= elf_tdata (abfd
)->verref
;
5018 Elf_Internal_Vernaux
*a
;
5020 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5022 if (a
->vna_other
== vernum
)
5024 verstr
= a
->vna_nodename
;
5034 /* xgettext:c-format */
5035 (_("%pB: %s: invalid needed version %d"),
5036 abfd
, name
, vernum
);
5037 bfd_set_error (bfd_error_bad_value
);
5038 goto error_free_vers
;
5042 namelen
= strlen (name
);
5043 verlen
= strlen (verstr
);
5044 newlen
= namelen
+ verlen
+ 2;
5045 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5046 && isym
->st_shndx
!= SHN_UNDEF
)
5049 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
5050 if (newname
== NULL
)
5051 goto error_free_vers
;
5052 memcpy (newname
, name
, namelen
);
5053 p
= newname
+ namelen
;
5055 /* If this is a defined non-hidden version symbol,
5056 we add another @ to the name. This indicates the
5057 default version of the symbol. */
5058 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5059 && isym
->st_shndx
!= SHN_UNDEF
)
5061 memcpy (p
, verstr
, verlen
+ 1);
5066 /* If this symbol has default visibility and the user has
5067 requested we not re-export it, then mark it as hidden. */
5068 if (!bfd_is_und_section (sec
)
5071 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
5072 isym
->st_other
= (STV_HIDDEN
5073 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
5075 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
5076 sym_hash
, &old_bfd
, &old_weak
,
5077 &old_alignment
, &skip
, &override
,
5078 &type_change_ok
, &size_change_ok
,
5080 goto error_free_vers
;
5085 /* Override a definition only if the new symbol matches the
5087 if (override
&& matched
)
5091 while (h
->root
.type
== bfd_link_hash_indirect
5092 || h
->root
.type
== bfd_link_hash_warning
)
5093 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5095 if (h
->versioned
!= unversioned
5096 && elf_tdata (abfd
)->verdef
!= NULL
5099 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
5102 if (! (_bfd_generic_link_add_one_symbol
5103 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
5104 NULL
, false, bed
->collect
,
5105 (struct bfd_link_hash_entry
**) sym_hash
)))
5106 goto error_free_vers
;
5109 /* We need to make sure that indirect symbol dynamic flags are
5112 while (h
->root
.type
== bfd_link_hash_indirect
5113 || h
->root
.type
== bfd_link_hash_warning
)
5114 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5118 /* Setting the index to -3 tells elf_link_output_extsym that
5119 this symbol is defined in a discarded section. */
5120 if (discarded
&& is_elf_hash_table (&htab
->root
))
5123 new_weak
= (flags
& BSF_WEAK
) != 0;
5127 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
5128 && is_elf_hash_table (&htab
->root
)
5129 && h
->u
.alias
== NULL
)
5131 /* Keep a list of all weak defined non function symbols from
5132 a dynamic object, using the alias field. Later in this
5133 function we will set the alias field to the correct
5134 value. We only put non-function symbols from dynamic
5135 objects on this list, because that happens to be the only
5136 time we need to know the normal symbol corresponding to a
5137 weak symbol, and the information is time consuming to
5138 figure out. If the alias field is not already NULL,
5139 then this symbol was already defined by some previous
5140 dynamic object, and we will be using that previous
5141 definition anyhow. */
5147 /* Set the alignment of a common symbol. */
5148 if ((common
|| bfd_is_com_section (sec
))
5149 && h
->root
.type
== bfd_link_hash_common
)
5154 align
= bfd_log2 (isym
->st_value
);
5157 /* The new symbol is a common symbol in a shared object.
5158 We need to get the alignment from the section. */
5159 align
= new_sec
->alignment_power
;
5161 if (align
> old_alignment
)
5162 h
->root
.u
.c
.p
->alignment_power
= align
;
5164 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5167 if (is_elf_hash_table (&htab
->root
))
5169 /* Set a flag in the hash table entry indicating the type of
5170 reference or definition we just found. A dynamic symbol
5171 is one which is referenced or defined by both a regular
5172 object and a shared object. */
5173 bool dynsym
= false;
5175 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5176 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5178 /* Except for this flag to track nonweak references. */
5180 && bind
!= STB_WEAK
)
5181 h
->ref_ir_nonweak
= 1;
5188 if (bind
!= STB_WEAK
)
5189 h
->ref_regular_nonweak
= 1;
5206 hi
->ref_dynamic
= 1;
5211 hi
->def_dynamic
= 1;
5215 /* If an indirect symbol has been forced local, don't
5216 make the real symbol dynamic. */
5217 if (h
!= hi
&& hi
->forced_local
)
5221 if (bfd_link_dll (info
)
5231 && weakdef (h
)->dynindx
!= -1))
5235 /* Check to see if we need to add an indirect symbol for
5236 the default name. */
5238 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5240 && hi
->versioned
== versioned_hidden
))
5241 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5242 sec
, value
, &old_bfd
, &dynsym
))
5243 goto error_free_vers
;
5245 /* Check the alignment when a common symbol is involved. This
5246 can change when a common symbol is overridden by a normal
5247 definition or a common symbol is ignored due to the old
5248 normal definition. We need to make sure the maximum
5249 alignment is maintained. */
5250 if ((old_alignment
|| common
)
5251 && h
->root
.type
!= bfd_link_hash_common
)
5253 unsigned int common_align
;
5254 unsigned int normal_align
;
5255 unsigned int symbol_align
;
5259 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5260 || h
->root
.type
== bfd_link_hash_defweak
);
5262 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5263 if (h
->root
.u
.def
.section
->owner
!= NULL
5264 && (h
->root
.u
.def
.section
->owner
->flags
5265 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5267 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5268 if (normal_align
> symbol_align
)
5269 normal_align
= symbol_align
;
5272 normal_align
= symbol_align
;
5276 common_align
= old_alignment
;
5277 common_bfd
= old_bfd
;
5282 common_align
= bfd_log2 (isym
->st_value
);
5284 normal_bfd
= old_bfd
;
5287 if (normal_align
< common_align
)
5289 /* PR binutils/2735 */
5290 if (normal_bfd
== NULL
)
5292 /* xgettext:c-format */
5293 (_("warning: alignment %u of common symbol `%s' in %pB is"
5294 " greater than the alignment (%u) of its section %pA"),
5295 1 << common_align
, name
, common_bfd
,
5296 1 << normal_align
, h
->root
.u
.def
.section
);
5299 /* xgettext:c-format */
5300 (_("warning: alignment %u of symbol `%s' in %pB"
5301 " is smaller than %u in %pB"),
5302 1 << normal_align
, name
, normal_bfd
,
5303 1 << common_align
, common_bfd
);
5307 /* Remember the symbol size if it isn't undefined. */
5308 if (isym
->st_size
!= 0
5309 && isym
->st_shndx
!= SHN_UNDEF
5310 && (definition
|| h
->size
== 0))
5313 && h
->size
!= isym
->st_size
5314 && ! size_change_ok
)
5316 /* xgettext:c-format */
5317 (_("warning: size of symbol `%s' changed"
5318 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5319 name
, (uint64_t) h
->size
, old_bfd
,
5320 (uint64_t) isym
->st_size
, abfd
);
5322 h
->size
= isym
->st_size
;
5325 /* If this is a common symbol, then we always want H->SIZE
5326 to be the size of the common symbol. The code just above
5327 won't fix the size if a common symbol becomes larger. We
5328 don't warn about a size change here, because that is
5329 covered by --warn-common. Allow changes between different
5331 if (h
->root
.type
== bfd_link_hash_common
)
5332 h
->size
= h
->root
.u
.c
.size
;
5334 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5335 && ((definition
&& !new_weak
)
5336 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5337 || h
->type
== STT_NOTYPE
))
5339 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5341 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5343 if (type
== STT_GNU_IFUNC
5344 && (abfd
->flags
& DYNAMIC
) != 0)
5347 if (h
->type
!= type
)
5349 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5350 /* xgettext:c-format */
5352 (_("warning: type of symbol `%s' changed"
5353 " from %d to %d in %pB"),
5354 name
, h
->type
, type
, abfd
);
5360 /* Merge st_other field. */
5361 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5362 definition
, dynamic
);
5364 /* We don't want to make debug symbol dynamic. */
5366 && (sec
->flags
& SEC_DEBUGGING
)
5367 && !bfd_link_relocatable (info
))
5370 /* Nor should we make plugin symbols dynamic. */
5371 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5376 h
->target_internal
= isym
->st_target_internal
;
5377 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5380 if (definition
&& !dynamic
)
5382 char *p
= strchr (name
, ELF_VER_CHR
);
5383 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5385 /* Queue non-default versions so that .symver x, x@FOO
5386 aliases can be checked. */
5389 size_t amt
= ((isymend
- isym
+ 1)
5390 * sizeof (struct elf_link_hash_entry
*));
5392 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5394 goto error_free_vers
;
5396 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5400 if (dynsym
&& h
->dynindx
== -1)
5402 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5403 goto error_free_vers
;
5405 && weakdef (h
)->dynindx
== -1)
5407 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5408 goto error_free_vers
;
5411 else if (h
->dynindx
!= -1)
5412 /* If the symbol already has a dynamic index, but
5413 visibility says it should not be visible, turn it into
5415 switch (ELF_ST_VISIBILITY (h
->other
))
5419 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
5427 && h
->root
.type
!= bfd_link_hash_indirect
5429 && h
->ref_regular_nonweak
)
5431 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5432 && h
->ref_ir_nonweak
5433 && !info
->lto_all_symbols_read
)
5434 || (h
->ref_dynamic_nonweak
5435 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5436 && !on_needed_list (elf_dt_name (abfd
),
5437 htab
->needed
, NULL
))))
5439 const char *soname
= elf_dt_name (abfd
);
5441 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5442 h
->root
.root
.string
);
5444 /* A symbol from a library loaded via DT_NEEDED of some
5445 other library is referenced by a regular object.
5446 Add a DT_NEEDED entry for it. Issue an error if
5447 --no-add-needed is used and the reference was not
5450 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5453 /* xgettext:c-format */
5454 (_("%pB: undefined reference to symbol '%s'"),
5456 bfd_set_error (bfd_error_missing_dso
);
5457 goto error_free_vers
;
5460 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5461 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5463 /* Create dynamic sections for backends that require
5464 that be done before setup_gnu_properties. */
5465 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5472 if (info
->lto_plugin_active
5473 && !bfd_link_relocatable (info
)
5474 && (abfd
->flags
& BFD_PLUGIN
) == 0
5480 if (bed
->s
->arch_size
== 32)
5485 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5486 referenced in regular objects so that linker plugin will get
5487 the correct symbol resolution. */
5489 sym_hash
= elf_sym_hashes (abfd
);
5490 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5492 Elf_Internal_Rela
*internal_relocs
;
5493 Elf_Internal_Rela
*rel
, *relend
;
5495 /* Don't check relocations in excluded sections. */
5496 if ((s
->flags
& SEC_RELOC
) == 0
5497 || s
->reloc_count
== 0
5498 || (s
->flags
& SEC_EXCLUDE
) != 0
5499 || ((info
->strip
== strip_all
5500 || info
->strip
== strip_debugger
)
5501 && (s
->flags
& SEC_DEBUGGING
) != 0))
5504 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
5507 _bfd_link_keep_memory (info
));
5508 if (internal_relocs
== NULL
)
5509 goto error_free_vers
;
5511 rel
= internal_relocs
;
5512 relend
= rel
+ s
->reloc_count
;
5513 for ( ; rel
< relend
; rel
++)
5515 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5516 struct elf_link_hash_entry
*h
;
5518 /* Skip local symbols. */
5519 if (r_symndx
< extsymoff
)
5522 h
= sym_hash
[r_symndx
- extsymoff
];
5524 h
->root
.non_ir_ref_regular
= 1;
5527 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5528 free (internal_relocs
);
5537 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5541 /* Restore the symbol table. */
5542 old_ent
= (char *) old_tab
+ tabsize
;
5543 memset (elf_sym_hashes (abfd
), 0,
5544 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5545 htab
->root
.table
.table
= old_table
;
5546 htab
->root
.table
.size
= old_size
;
5547 htab
->root
.table
.count
= old_count
;
5548 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5549 htab
->root
.undefs
= old_undefs
;
5550 htab
->root
.undefs_tail
= old_undefs_tail
;
5551 if (htab
->dynstr
!= NULL
)
5552 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5555 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5557 struct bfd_hash_entry
*p
;
5558 struct elf_link_hash_entry
*h
;
5559 unsigned int non_ir_ref_dynamic
;
5561 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5563 /* Preserve non_ir_ref_dynamic so that this symbol
5564 will be exported when the dynamic lib becomes needed
5565 in the second pass. */
5566 h
= (struct elf_link_hash_entry
*) p
;
5567 if (h
->root
.type
== bfd_link_hash_warning
)
5568 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5569 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5571 h
= (struct elf_link_hash_entry
*) p
;
5572 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5573 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5574 if (h
->root
.type
== bfd_link_hash_warning
)
5576 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5577 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5578 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5580 if (h
->root
.type
== bfd_link_hash_common
)
5582 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5583 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5585 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5589 /* Make a special call to the linker "notice" function to
5590 tell it that symbols added for crefs may need to be removed. */
5591 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5592 goto error_free_vers
;
5595 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5597 free (nondeflt_vers
);
5601 if (old_tab
!= NULL
)
5603 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5604 goto error_free_vers
;
5609 /* Now that all the symbols from this input file are created, if
5610 not performing a relocatable link, handle .symver foo, foo@BAR
5611 such that any relocs against foo become foo@BAR. */
5612 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5616 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5618 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5619 char *shortname
, *p
;
5622 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5624 || (h
->root
.type
!= bfd_link_hash_defined
5625 && h
->root
.type
!= bfd_link_hash_defweak
))
5628 amt
= p
- h
->root
.root
.string
;
5629 shortname
= (char *) bfd_malloc (amt
+ 1);
5631 goto error_free_vers
;
5632 memcpy (shortname
, h
->root
.root
.string
, amt
);
5633 shortname
[amt
] = '\0';
5635 hi
= (struct elf_link_hash_entry
*)
5636 bfd_link_hash_lookup (&htab
->root
, shortname
,
5637 false, false, false);
5639 && hi
->root
.type
== h
->root
.type
5640 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5641 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5643 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
5644 hi
->root
.type
= bfd_link_hash_indirect
;
5645 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5646 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5647 sym_hash
= elf_sym_hashes (abfd
);
5649 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5650 if (sym_hash
[symidx
] == hi
)
5652 sym_hash
[symidx
] = h
;
5658 free (nondeflt_vers
);
5659 nondeflt_vers
= NULL
;
5662 /* Now set the alias field correctly for all the weak defined
5663 symbols we found. The only way to do this is to search all the
5664 symbols. Since we only need the information for non functions in
5665 dynamic objects, that's the only time we actually put anything on
5666 the list WEAKS. We need this information so that if a regular
5667 object refers to a symbol defined weakly in a dynamic object, the
5668 real symbol in the dynamic object is also put in the dynamic
5669 symbols; we also must arrange for both symbols to point to the
5670 same memory location. We could handle the general case of symbol
5671 aliasing, but a general symbol alias can only be generated in
5672 assembler code, handling it correctly would be very time
5673 consuming, and other ELF linkers don't handle general aliasing
5677 struct elf_link_hash_entry
**hpp
;
5678 struct elf_link_hash_entry
**hppend
;
5679 struct elf_link_hash_entry
**sorted_sym_hash
;
5680 struct elf_link_hash_entry
*h
;
5681 size_t sym_count
, amt
;
5683 /* Since we have to search the whole symbol list for each weak
5684 defined symbol, search time for N weak defined symbols will be
5685 O(N^2). Binary search will cut it down to O(NlogN). */
5686 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5687 sorted_sym_hash
= bfd_malloc (amt
);
5688 if (sorted_sym_hash
== NULL
)
5690 sym_hash
= sorted_sym_hash
;
5691 hpp
= elf_sym_hashes (abfd
);
5692 hppend
= hpp
+ extsymcount
;
5694 for (; hpp
< hppend
; hpp
++)
5698 && h
->root
.type
== bfd_link_hash_defined
5699 && !bed
->is_function_type (h
->type
))
5707 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5710 while (weaks
!= NULL
)
5712 struct elf_link_hash_entry
*hlook
;
5715 size_t i
, j
, idx
= 0;
5718 weaks
= hlook
->u
.alias
;
5719 hlook
->u
.alias
= NULL
;
5721 if (hlook
->root
.type
!= bfd_link_hash_defined
5722 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5725 slook
= hlook
->root
.u
.def
.section
;
5726 vlook
= hlook
->root
.u
.def
.value
;
5732 bfd_signed_vma vdiff
;
5734 h
= sorted_sym_hash
[idx
];
5735 vdiff
= vlook
- h
->root
.u
.def
.value
;
5742 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5752 /* We didn't find a value/section match. */
5756 /* With multiple aliases, or when the weak symbol is already
5757 strongly defined, we have multiple matching symbols and
5758 the binary search above may land on any of them. Step
5759 one past the matching symbol(s). */
5762 h
= sorted_sym_hash
[idx
];
5763 if (h
->root
.u
.def
.section
!= slook
5764 || h
->root
.u
.def
.value
!= vlook
)
5768 /* Now look back over the aliases. Since we sorted by size
5769 as well as value and section, we'll choose the one with
5770 the largest size. */
5773 h
= sorted_sym_hash
[idx
];
5775 /* Stop if value or section doesn't match. */
5776 if (h
->root
.u
.def
.section
!= slook
5777 || h
->root
.u
.def
.value
!= vlook
)
5779 else if (h
!= hlook
)
5781 struct elf_link_hash_entry
*t
;
5784 hlook
->is_weakalias
= 1;
5786 if (t
->u
.alias
!= NULL
)
5787 while (t
->u
.alias
!= h
)
5791 /* If the weak definition is in the list of dynamic
5792 symbols, make sure the real definition is put
5794 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5796 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5799 free (sorted_sym_hash
);
5804 /* If the real definition is in the list of dynamic
5805 symbols, make sure the weak definition is put
5806 there as well. If we don't do this, then the
5807 dynamic loader might not merge the entries for the
5808 real definition and the weak definition. */
5809 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5811 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5812 goto err_free_sym_hash
;
5819 free (sorted_sym_hash
);
5822 if (bed
->check_directives
5823 && !(*bed
->check_directives
) (abfd
, info
))
5826 /* If this is a non-traditional link, try to optimize the handling
5827 of the .stab/.stabstr sections. */
5829 && ! info
->traditional_format
5830 && is_elf_hash_table (&htab
->root
)
5831 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5835 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5836 if (stabstr
!= NULL
)
5838 bfd_size_type string_offset
= 0;
5841 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5842 if (startswith (stab
->name
, ".stab")
5843 && (!stab
->name
[5] ||
5844 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5845 && (stab
->flags
& SEC_MERGE
) == 0
5846 && !bfd_is_abs_section (stab
->output_section
))
5848 struct bfd_elf_section_data
*secdata
;
5850 secdata
= elf_section_data (stab
);
5851 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5852 stabstr
, &secdata
->sec_info
,
5855 if (secdata
->sec_info
)
5856 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5861 if (dynamic
&& add_needed
)
5863 /* Add this bfd to the loaded list. */
5864 struct elf_link_loaded_list
*n
;
5866 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5870 n
->next
= htab
->dyn_loaded
;
5871 htab
->dyn_loaded
= n
;
5873 if (dynamic
&& !add_needed
5874 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5875 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5882 free (nondeflt_vers
);
5890 /* Return the linker hash table entry of a symbol that might be
5891 satisfied by an archive symbol. Return -1 on error. */
5893 struct bfd_link_hash_entry
*
5894 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5895 struct bfd_link_info
*info
,
5898 struct bfd_link_hash_entry
*h
;
5902 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
5906 /* If this is a default version (the name contains @@), look up the
5907 symbol again with only one `@' as well as without the version.
5908 The effect is that references to the symbol with and without the
5909 version will be matched by the default symbol in the archive. */
5911 p
= strchr (name
, ELF_VER_CHR
);
5912 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5915 /* First check with only one `@'. */
5916 len
= strlen (name
);
5917 copy
= (char *) bfd_alloc (abfd
, len
);
5919 return (struct bfd_link_hash_entry
*) -1;
5921 first
= p
- name
+ 1;
5922 memcpy (copy
, name
, first
);
5923 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5925 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5928 /* We also need to check references to the symbol without the
5930 copy
[first
- 1] = '\0';
5931 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5934 bfd_release (abfd
, copy
);
5938 /* Add symbols from an ELF archive file to the linker hash table. We
5939 don't use _bfd_generic_link_add_archive_symbols because we need to
5940 handle versioned symbols.
5942 Fortunately, ELF archive handling is simpler than that done by
5943 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5944 oddities. In ELF, if we find a symbol in the archive map, and the
5945 symbol is currently undefined, we know that we must pull in that
5948 Unfortunately, we do have to make multiple passes over the symbol
5949 table until nothing further is resolved. */
5952 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5955 unsigned char *included
= NULL
;
5959 const struct elf_backend_data
*bed
;
5960 struct bfd_link_hash_entry
* (*archive_symbol_lookup
)
5961 (bfd
*, struct bfd_link_info
*, const char *);
5963 if (! bfd_has_map (abfd
))
5965 /* An empty archive is a special case. */
5966 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5968 bfd_set_error (bfd_error_no_armap
);
5972 /* Keep track of all symbols we know to be already defined, and all
5973 files we know to be already included. This is to speed up the
5974 second and subsequent passes. */
5975 c
= bfd_ardata (abfd
)->symdef_count
;
5978 amt
= c
* sizeof (*included
);
5979 included
= (unsigned char *) bfd_zmalloc (amt
);
5980 if (included
== NULL
)
5983 symdefs
= bfd_ardata (abfd
)->symdefs
;
5984 bed
= get_elf_backend_data (abfd
);
5985 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5998 symdefend
= symdef
+ c
;
5999 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
6001 struct bfd_link_hash_entry
*h
;
6003 struct bfd_link_hash_entry
*undefs_tail
;
6008 if (symdef
->file_offset
== last
)
6014 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
6015 if (h
== (struct bfd_link_hash_entry
*) -1)
6021 if (h
->type
== bfd_link_hash_undefined
)
6023 /* If the archive element has already been loaded then one
6024 of the symbols defined by that element might have been
6025 made undefined due to being in a discarded section. */
6026 if (is_elf_hash_table (info
->hash
)
6027 && ((struct elf_link_hash_entry
*) h
)->indx
== -3)
6030 else if (h
->type
== bfd_link_hash_common
)
6032 /* We currently have a common symbol. The archive map contains
6033 a reference to this symbol, so we may want to include it. We
6034 only want to include it however, if this archive element
6035 contains a definition of the symbol, not just another common
6038 Unfortunately some archivers (including GNU ar) will put
6039 declarations of common symbols into their archive maps, as
6040 well as real definitions, so we cannot just go by the archive
6041 map alone. Instead we must read in the element's symbol
6042 table and check that to see what kind of symbol definition
6044 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
6049 if (h
->type
!= bfd_link_hash_undefweak
)
6050 /* Symbol must be defined. Don't check it again. */
6055 /* We need to include this archive member. */
6056 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
,
6058 if (element
== NULL
)
6061 if (! bfd_check_format (element
, bfd_object
))
6064 undefs_tail
= info
->hash
->undefs_tail
;
6066 if (!(*info
->callbacks
6067 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
6069 if (!bfd_link_add_symbols (element
, info
))
6072 /* If there are any new undefined symbols, we need to make
6073 another pass through the archive in order to see whether
6074 they can be defined. FIXME: This isn't perfect, because
6075 common symbols wind up on undefs_tail and because an
6076 undefined symbol which is defined later on in this pass
6077 does not require another pass. This isn't a bug, but it
6078 does make the code less efficient than it could be. */
6079 if (undefs_tail
!= info
->hash
->undefs_tail
)
6082 /* Look backward to mark all symbols from this object file
6083 which we have already seen in this pass. */
6087 included
[mark
] = true;
6092 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
6094 /* We mark subsequent symbols from this object file as we go
6095 on through the loop. */
6096 last
= symdef
->file_offset
;
6109 /* Given an ELF BFD, add symbols to the global hash table as
6113 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6115 switch (bfd_get_format (abfd
))
6118 return elf_link_add_object_symbols (abfd
, info
);
6120 return elf_link_add_archive_symbols (abfd
, info
);
6122 bfd_set_error (bfd_error_wrong_format
);
6127 struct hash_codes_info
6129 unsigned long *hashcodes
;
6133 /* This function will be called though elf_link_hash_traverse to store
6134 all hash value of the exported symbols in an array. */
6137 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6139 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
6144 /* Ignore indirect symbols. These are added by the versioning code. */
6145 if (h
->dynindx
== -1)
6148 name
= h
->root
.root
.string
;
6149 if (h
->versioned
>= versioned
)
6151 char *p
= strchr (name
, ELF_VER_CHR
);
6154 alc
= (char *) bfd_malloc (p
- name
+ 1);
6160 memcpy (alc
, name
, p
- name
);
6161 alc
[p
- name
] = '\0';
6166 /* Compute the hash value. */
6167 ha
= bfd_elf_hash (name
);
6169 /* Store the found hash value in the array given as the argument. */
6170 *(inf
->hashcodes
)++ = ha
;
6172 /* And store it in the struct so that we can put it in the hash table
6174 h
->u
.elf_hash_value
= ha
;
6180 struct collect_gnu_hash_codes
6183 const struct elf_backend_data
*bed
;
6184 unsigned long int nsyms
;
6185 unsigned long int maskbits
;
6186 unsigned long int *hashcodes
;
6187 unsigned long int *hashval
;
6188 unsigned long int *indx
;
6189 unsigned long int *counts
;
6193 long int min_dynindx
;
6194 unsigned long int bucketcount
;
6195 unsigned long int symindx
;
6196 long int local_indx
;
6197 long int shift1
, shift2
;
6198 unsigned long int mask
;
6202 /* This function will be called though elf_link_hash_traverse to store
6203 all hash value of the exported symbols in an array. */
6206 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6208 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6213 /* Ignore indirect symbols. These are added by the versioning code. */
6214 if (h
->dynindx
== -1)
6217 /* Ignore also local symbols and undefined symbols. */
6218 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6221 name
= h
->root
.root
.string
;
6222 if (h
->versioned
>= versioned
)
6224 char *p
= strchr (name
, ELF_VER_CHR
);
6227 alc
= (char *) bfd_malloc (p
- name
+ 1);
6233 memcpy (alc
, name
, p
- name
);
6234 alc
[p
- name
] = '\0';
6239 /* Compute the hash value. */
6240 ha
= bfd_elf_gnu_hash (name
);
6242 /* Store the found hash value in the array for compute_bucket_count,
6243 and also for .dynsym reordering purposes. */
6244 s
->hashcodes
[s
->nsyms
] = ha
;
6245 s
->hashval
[h
->dynindx
] = ha
;
6247 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6248 s
->min_dynindx
= h
->dynindx
;
6254 /* This function will be called though elf_link_hash_traverse to do
6255 final dynamic symbol renumbering in case of .gnu.hash.
6256 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6257 to the translation table. */
6260 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6262 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6263 unsigned long int bucket
;
6264 unsigned long int val
;
6266 /* Ignore indirect symbols. */
6267 if (h
->dynindx
== -1)
6270 /* Ignore also local symbols and undefined symbols. */
6271 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6273 if (h
->dynindx
>= s
->min_dynindx
)
6275 if (s
->bed
->record_xhash_symbol
!= NULL
)
6277 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6281 h
->dynindx
= s
->local_indx
++;
6286 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6287 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6288 & ((s
->maskbits
>> s
->shift1
) - 1);
6289 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6291 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6292 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6293 if (s
->counts
[bucket
] == 1)
6294 /* Last element terminates the chain. */
6296 bfd_put_32 (s
->output_bfd
, val
,
6297 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6298 --s
->counts
[bucket
];
6299 if (s
->bed
->record_xhash_symbol
!= NULL
)
6301 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6303 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6306 h
->dynindx
= s
->indx
[bucket
]++;
6310 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6313 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6315 return !(h
->forced_local
6316 || h
->root
.type
== bfd_link_hash_undefined
6317 || h
->root
.type
== bfd_link_hash_undefweak
6318 || ((h
->root
.type
== bfd_link_hash_defined
6319 || h
->root
.type
== bfd_link_hash_defweak
)
6320 && h
->root
.u
.def
.section
->output_section
== NULL
));
6323 /* Array used to determine the number of hash table buckets to use
6324 based on the number of symbols there are. If there are fewer than
6325 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6326 fewer than 37 we use 17 buckets, and so forth. We never use more
6327 than 32771 buckets. */
6329 static const size_t elf_buckets
[] =
6331 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6335 /* Compute bucket count for hashing table. We do not use a static set
6336 of possible tables sizes anymore. Instead we determine for all
6337 possible reasonable sizes of the table the outcome (i.e., the
6338 number of collisions etc) and choose the best solution. The
6339 weighting functions are not too simple to allow the table to grow
6340 without bounds. Instead one of the weighting factors is the size.
6341 Therefore the result is always a good payoff between few collisions
6342 (= short chain lengths) and table size. */
6344 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6345 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6346 unsigned long int nsyms
,
6349 size_t best_size
= 0;
6350 unsigned long int i
;
6352 /* We have a problem here. The following code to optimize the table
6353 size requires an integer type with more the 32 bits. If
6354 BFD_HOST_U_64_BIT is set we know about such a type. */
6355 #ifdef BFD_HOST_U_64_BIT
6360 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
6361 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6362 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6363 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6364 unsigned long int *counts
;
6366 unsigned int no_improvement_count
= 0;
6368 /* Possible optimization parameters: if we have NSYMS symbols we say
6369 that the hashing table must at least have NSYMS/4 and at most
6371 minsize
= nsyms
/ 4;
6374 best_size
= maxsize
= nsyms
* 2;
6379 if ((best_size
& 31) == 0)
6383 /* Create array where we count the collisions in. We must use bfd_malloc
6384 since the size could be large. */
6386 amt
*= sizeof (unsigned long int);
6387 counts
= (unsigned long int *) bfd_malloc (amt
);
6391 /* Compute the "optimal" size for the hash table. The criteria is a
6392 minimal chain length. The minor criteria is (of course) the size
6394 for (i
= minsize
; i
< maxsize
; ++i
)
6396 /* Walk through the array of hashcodes and count the collisions. */
6397 BFD_HOST_U_64_BIT max
;
6398 unsigned long int j
;
6399 unsigned long int fact
;
6401 if (gnu_hash
&& (i
& 31) == 0)
6404 memset (counts
, '\0', i
* sizeof (unsigned long int));
6406 /* Determine how often each hash bucket is used. */
6407 for (j
= 0; j
< nsyms
; ++j
)
6408 ++counts
[hashcodes
[j
] % i
];
6410 /* For the weight function we need some information about the
6411 pagesize on the target. This is information need not be 100%
6412 accurate. Since this information is not available (so far) we
6413 define it here to a reasonable default value. If it is crucial
6414 to have a better value some day simply define this value. */
6415 # ifndef BFD_TARGET_PAGESIZE
6416 # define BFD_TARGET_PAGESIZE (4096)
6419 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6421 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6424 /* Variant 1: optimize for short chains. We add the squares
6425 of all the chain lengths (which favors many small chain
6426 over a few long chains). */
6427 for (j
= 0; j
< i
; ++j
)
6428 max
+= counts
[j
] * counts
[j
];
6430 /* This adds penalties for the overall size of the table. */
6431 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6434 /* Variant 2: Optimize a lot more for small table. Here we
6435 also add squares of the size but we also add penalties for
6436 empty slots (the +1 term). */
6437 for (j
= 0; j
< i
; ++j
)
6438 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6440 /* The overall size of the table is considered, but not as
6441 strong as in variant 1, where it is squared. */
6442 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6446 /* Compare with current best results. */
6447 if (max
< best_chlen
)
6451 no_improvement_count
= 0;
6453 /* PR 11843: Avoid futile long searches for the best bucket size
6454 when there are a large number of symbols. */
6455 else if (++no_improvement_count
== 100)
6462 #endif /* defined (BFD_HOST_U_64_BIT) */
6464 /* This is the fallback solution if no 64bit type is available or if we
6465 are not supposed to spend much time on optimizations. We select the
6466 bucket count using a fixed set of numbers. */
6467 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6469 best_size
= elf_buckets
[i
];
6470 if (nsyms
< elf_buckets
[i
+ 1])
6473 if (gnu_hash
&& best_size
< 2)
6480 /* Size any SHT_GROUP section for ld -r. */
6483 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6488 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6489 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6490 && (s
= ibfd
->sections
) != NULL
6491 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6492 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6497 /* Set a default stack segment size. The value in INFO wins. If it
6498 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6499 undefined it is initialized. */
6502 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6503 struct bfd_link_info
*info
,
6504 const char *legacy_symbol
,
6505 bfd_vma default_size
)
6507 struct elf_link_hash_entry
*h
= NULL
;
6509 /* Look for legacy symbol. */
6511 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6512 false, false, false);
6513 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6514 || h
->root
.type
== bfd_link_hash_defweak
)
6516 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6518 /* The symbol has no type if specified on the command line. */
6519 h
->type
= STT_OBJECT
;
6520 if (info
->stacksize
)
6521 /* xgettext:c-format */
6522 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6523 output_bfd
, legacy_symbol
);
6524 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6525 /* xgettext:c-format */
6526 _bfd_error_handler (_("%pB: %s not absolute"),
6527 output_bfd
, legacy_symbol
);
6529 info
->stacksize
= h
->root
.u
.def
.value
;
6532 if (!info
->stacksize
)
6533 /* If the user didn't set a size, or explicitly inhibit the
6534 size, set it now. */
6535 info
->stacksize
= default_size
;
6537 /* Provide the legacy symbol, if it is referenced. */
6538 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6539 || h
->root
.type
== bfd_link_hash_undefweak
))
6541 struct bfd_link_hash_entry
*bh
= NULL
;
6543 if (!(_bfd_generic_link_add_one_symbol
6544 (info
, output_bfd
, legacy_symbol
,
6545 BSF_GLOBAL
, bfd_abs_section_ptr
,
6546 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6547 NULL
, false, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6550 h
= (struct elf_link_hash_entry
*) bh
;
6552 h
->type
= STT_OBJECT
;
6558 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6560 struct elf_gc_sweep_symbol_info
6562 struct bfd_link_info
*info
;
6563 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6568 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6571 && (((h
->root
.type
== bfd_link_hash_defined
6572 || h
->root
.type
== bfd_link_hash_defweak
)
6573 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6574 && h
->root
.u
.def
.section
->gc_mark
))
6575 || h
->root
.type
== bfd_link_hash_undefined
6576 || h
->root
.type
== bfd_link_hash_undefweak
))
6578 struct elf_gc_sweep_symbol_info
*inf
;
6580 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6581 (*inf
->hide_symbol
) (inf
->info
, h
, true);
6584 h
->ref_regular_nonweak
= 0;
6590 /* Set up the sizes and contents of the ELF dynamic sections. This is
6591 called by the ELF linker emulation before_allocation routine. We
6592 must set the sizes of the sections before the linker sets the
6593 addresses of the various sections. */
6596 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6599 const char *filter_shlib
,
6601 const char *depaudit
,
6602 const char * const *auxiliary_filters
,
6603 struct bfd_link_info
*info
,
6604 asection
**sinterpptr
)
6607 const struct elf_backend_data
*bed
;
6611 if (!is_elf_hash_table (info
->hash
))
6614 /* Any syms created from now on start with -1 in
6615 got.refcount/offset and plt.refcount/offset. */
6616 elf_hash_table (info
)->init_got_refcount
6617 = elf_hash_table (info
)->init_got_offset
;
6618 elf_hash_table (info
)->init_plt_refcount
6619 = elf_hash_table (info
)->init_plt_offset
;
6621 bed
= get_elf_backend_data (output_bfd
);
6623 /* The backend may have to create some sections regardless of whether
6624 we're dynamic or not. */
6625 if (bed
->elf_backend_always_size_sections
6626 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6629 dynobj
= elf_hash_table (info
)->dynobj
;
6631 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6633 struct bfd_elf_version_tree
*verdefs
;
6634 struct elf_info_failed asvinfo
;
6635 struct bfd_elf_version_tree
*t
;
6636 struct bfd_elf_version_expr
*d
;
6640 /* If we are supposed to export all symbols into the dynamic symbol
6641 table (this is not the normal case), then do so. */
6642 if (info
->export_dynamic
6643 || (bfd_link_executable (info
) && info
->dynamic
))
6645 struct elf_info_failed eif
;
6649 elf_link_hash_traverse (elf_hash_table (info
),
6650 _bfd_elf_export_symbol
,
6658 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6660 if (soname_indx
== (size_t) -1
6661 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6665 soname_indx
= (size_t) -1;
6667 /* Make all global versions with definition. */
6668 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6669 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6670 if (!d
->symver
&& d
->literal
)
6672 const char *verstr
, *name
;
6673 size_t namelen
, verlen
, newlen
;
6674 char *newname
, *p
, leading_char
;
6675 struct elf_link_hash_entry
*newh
;
6677 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6679 namelen
= strlen (name
) + (leading_char
!= '\0');
6681 verlen
= strlen (verstr
);
6682 newlen
= namelen
+ verlen
+ 3;
6684 newname
= (char *) bfd_malloc (newlen
);
6685 if (newname
== NULL
)
6687 newname
[0] = leading_char
;
6688 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6690 /* Check the hidden versioned definition. */
6691 p
= newname
+ namelen
;
6693 memcpy (p
, verstr
, verlen
+ 1);
6694 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6695 newname
, false, false,
6698 || (newh
->root
.type
!= bfd_link_hash_defined
6699 && newh
->root
.type
!= bfd_link_hash_defweak
))
6701 /* Check the default versioned definition. */
6703 memcpy (p
, verstr
, verlen
+ 1);
6704 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6705 newname
, false, false,
6710 /* Mark this version if there is a definition and it is
6711 not defined in a shared object. */
6713 && !newh
->def_dynamic
6714 && (newh
->root
.type
== bfd_link_hash_defined
6715 || newh
->root
.type
== bfd_link_hash_defweak
))
6719 /* Attach all the symbols to their version information. */
6720 asvinfo
.info
= info
;
6721 asvinfo
.failed
= false;
6723 elf_link_hash_traverse (elf_hash_table (info
),
6724 _bfd_elf_link_assign_sym_version
,
6729 if (!info
->allow_undefined_version
)
6731 /* Check if all global versions have a definition. */
6732 bool all_defined
= true;
6733 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6734 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6735 if (d
->literal
&& !d
->symver
&& !d
->script
)
6738 (_("%s: undefined version: %s"),
6739 d
->pattern
, t
->name
);
6740 all_defined
= false;
6745 bfd_set_error (bfd_error_bad_value
);
6750 /* Set up the version definition section. */
6751 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6752 BFD_ASSERT (s
!= NULL
);
6754 /* We may have created additional version definitions if we are
6755 just linking a regular application. */
6756 verdefs
= info
->version_info
;
6758 /* Skip anonymous version tag. */
6759 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6760 verdefs
= verdefs
->next
;
6762 if (verdefs
== NULL
&& !info
->create_default_symver
)
6763 s
->flags
|= SEC_EXCLUDE
;
6769 Elf_Internal_Verdef def
;
6770 Elf_Internal_Verdaux defaux
;
6771 struct bfd_link_hash_entry
*bh
;
6772 struct elf_link_hash_entry
*h
;
6778 /* Make space for the base version. */
6779 size
+= sizeof (Elf_External_Verdef
);
6780 size
+= sizeof (Elf_External_Verdaux
);
6783 /* Make space for the default version. */
6784 if (info
->create_default_symver
)
6786 size
+= sizeof (Elf_External_Verdef
);
6790 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6792 struct bfd_elf_version_deps
*n
;
6794 /* Don't emit base version twice. */
6798 size
+= sizeof (Elf_External_Verdef
);
6799 size
+= sizeof (Elf_External_Verdaux
);
6802 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6803 size
+= sizeof (Elf_External_Verdaux
);
6807 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6808 if (s
->contents
== NULL
&& s
->size
!= 0)
6811 /* Fill in the version definition section. */
6815 def
.vd_version
= VER_DEF_CURRENT
;
6816 def
.vd_flags
= VER_FLG_BASE
;
6819 if (info
->create_default_symver
)
6821 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6822 def
.vd_next
= sizeof (Elf_External_Verdef
);
6826 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6827 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6828 + sizeof (Elf_External_Verdaux
));
6831 if (soname_indx
!= (size_t) -1)
6833 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6835 def
.vd_hash
= bfd_elf_hash (soname
);
6836 defaux
.vda_name
= soname_indx
;
6843 name
= lbasename (bfd_get_filename (output_bfd
));
6844 def
.vd_hash
= bfd_elf_hash (name
);
6845 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6847 if (indx
== (size_t) -1)
6849 defaux
.vda_name
= indx
;
6851 defaux
.vda_next
= 0;
6853 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6854 (Elf_External_Verdef
*) p
);
6855 p
+= sizeof (Elf_External_Verdef
);
6856 if (info
->create_default_symver
)
6858 /* Add a symbol representing this version. */
6860 if (! (_bfd_generic_link_add_one_symbol
6861 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6863 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6865 h
= (struct elf_link_hash_entry
*) bh
;
6868 h
->type
= STT_OBJECT
;
6869 h
->verinfo
.vertree
= NULL
;
6871 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6874 /* Create a duplicate of the base version with the same
6875 aux block, but different flags. */
6878 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6880 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6881 + sizeof (Elf_External_Verdaux
));
6884 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6885 (Elf_External_Verdef
*) p
);
6886 p
+= sizeof (Elf_External_Verdef
);
6888 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6889 (Elf_External_Verdaux
*) p
);
6890 p
+= sizeof (Elf_External_Verdaux
);
6892 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6895 struct bfd_elf_version_deps
*n
;
6897 /* Don't emit the base version twice. */
6902 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6905 /* Add a symbol representing this version. */
6907 if (! (_bfd_generic_link_add_one_symbol
6908 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6910 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6912 h
= (struct elf_link_hash_entry
*) bh
;
6915 h
->type
= STT_OBJECT
;
6916 h
->verinfo
.vertree
= t
;
6918 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6921 def
.vd_version
= VER_DEF_CURRENT
;
6923 if (t
->globals
.list
== NULL
6924 && t
->locals
.list
== NULL
6926 def
.vd_flags
|= VER_FLG_WEAK
;
6927 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6928 def
.vd_cnt
= cdeps
+ 1;
6929 def
.vd_hash
= bfd_elf_hash (t
->name
);
6930 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6933 /* If a basever node is next, it *must* be the last node in
6934 the chain, otherwise Verdef construction breaks. */
6935 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6936 BFD_ASSERT (t
->next
->next
== NULL
);
6938 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6939 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6940 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6942 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6943 (Elf_External_Verdef
*) p
);
6944 p
+= sizeof (Elf_External_Verdef
);
6946 defaux
.vda_name
= h
->dynstr_index
;
6947 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6949 defaux
.vda_next
= 0;
6950 if (t
->deps
!= NULL
)
6951 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6952 t
->name_indx
= defaux
.vda_name
;
6954 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6955 (Elf_External_Verdaux
*) p
);
6956 p
+= sizeof (Elf_External_Verdaux
);
6958 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6960 if (n
->version_needed
== NULL
)
6962 /* This can happen if there was an error in the
6964 defaux
.vda_name
= 0;
6968 defaux
.vda_name
= n
->version_needed
->name_indx
;
6969 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6972 if (n
->next
== NULL
)
6973 defaux
.vda_next
= 0;
6975 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6977 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6978 (Elf_External_Verdaux
*) p
);
6979 p
+= sizeof (Elf_External_Verdaux
);
6983 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6987 if (info
->gc_sections
&& bed
->can_gc_sections
)
6989 struct elf_gc_sweep_symbol_info sweep_info
;
6991 /* Remove the symbols that were in the swept sections from the
6992 dynamic symbol table. */
6993 sweep_info
.info
= info
;
6994 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6995 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6999 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7002 struct elf_find_verdep_info sinfo
;
7004 /* Work out the size of the version reference section. */
7006 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
7007 BFD_ASSERT (s
!= NULL
);
7010 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
7011 if (sinfo
.vers
== 0)
7013 sinfo
.failed
= false;
7015 elf_link_hash_traverse (elf_hash_table (info
),
7016 _bfd_elf_link_find_version_dependencies
,
7021 if (info
->enable_dt_relr
)
7023 elf_link_add_dt_relr_dependency (&sinfo
);
7028 if (elf_tdata (output_bfd
)->verref
== NULL
)
7029 s
->flags
|= SEC_EXCLUDE
;
7032 Elf_Internal_Verneed
*vn
;
7037 /* Build the version dependency section. */
7040 for (vn
= elf_tdata (output_bfd
)->verref
;
7042 vn
= vn
->vn_nextref
)
7044 Elf_Internal_Vernaux
*a
;
7046 size
+= sizeof (Elf_External_Verneed
);
7048 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7049 size
+= sizeof (Elf_External_Vernaux
);
7053 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7054 if (s
->contents
== NULL
)
7058 for (vn
= elf_tdata (output_bfd
)->verref
;
7060 vn
= vn
->vn_nextref
)
7063 Elf_Internal_Vernaux
*a
;
7067 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7070 vn
->vn_version
= VER_NEED_CURRENT
;
7072 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7073 elf_dt_name (vn
->vn_bfd
) != NULL
7074 ? elf_dt_name (vn
->vn_bfd
)
7075 : lbasename (bfd_get_filename
7078 if (indx
== (size_t) -1)
7081 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
7082 if (vn
->vn_nextref
== NULL
)
7085 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
7086 + caux
* sizeof (Elf_External_Vernaux
));
7088 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
7089 (Elf_External_Verneed
*) p
);
7090 p
+= sizeof (Elf_External_Verneed
);
7092 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7094 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
7095 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7096 a
->vna_nodename
, false);
7097 if (indx
== (size_t) -1)
7100 if (a
->vna_nextptr
== NULL
)
7103 a
->vna_next
= sizeof (Elf_External_Vernaux
);
7105 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
7106 (Elf_External_Vernaux
*) p
);
7107 p
+= sizeof (Elf_External_Vernaux
);
7111 elf_tdata (output_bfd
)->cverrefs
= crefs
;
7115 if (bfd_link_relocatable (info
)
7116 && !_bfd_elf_size_group_sections (info
))
7119 /* Determine any GNU_STACK segment requirements, after the backend
7120 has had a chance to set a default segment size. */
7121 if (info
->execstack
)
7122 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
7123 else if (info
->noexecstack
)
7124 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
7128 asection
*notesec
= NULL
;
7131 for (inputobj
= info
->input_bfds
;
7133 inputobj
= inputobj
->link
.next
)
7138 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
7140 s
= inputobj
->sections
;
7141 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
7144 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
7147 if (s
->flags
& SEC_CODE
)
7151 else if (bed
->default_execstack
)
7154 if (notesec
|| info
->stacksize
> 0)
7155 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
7156 if (notesec
&& exec
&& bfd_link_relocatable (info
)
7157 && notesec
->output_section
!= bfd_abs_section_ptr
)
7158 notesec
->output_section
->flags
|= SEC_CODE
;
7161 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7163 struct elf_info_failed eif
;
7164 struct elf_link_hash_entry
*h
;
7168 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7169 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7173 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7175 info
->flags
|= DF_SYMBOLIC
;
7183 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7185 if (indx
== (size_t) -1)
7188 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7189 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7193 if (filter_shlib
!= NULL
)
7197 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7198 filter_shlib
, true);
7199 if (indx
== (size_t) -1
7200 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7204 if (auxiliary_filters
!= NULL
)
7206 const char * const *p
;
7208 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7212 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7214 if (indx
== (size_t) -1
7215 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7224 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7226 if (indx
== (size_t) -1
7227 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7231 if (depaudit
!= NULL
)
7235 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7237 if (indx
== (size_t) -1
7238 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7245 /* Find all symbols which were defined in a dynamic object and make
7246 the backend pick a reasonable value for them. */
7247 elf_link_hash_traverse (elf_hash_table (info
),
7248 _bfd_elf_adjust_dynamic_symbol
,
7253 /* Add some entries to the .dynamic section. We fill in some of the
7254 values later, in bfd_elf_final_link, but we must add the entries
7255 now so that we know the final size of the .dynamic section. */
7257 /* If there are initialization and/or finalization functions to
7258 call then add the corresponding DT_INIT/DT_FINI entries. */
7259 h
= (info
->init_function
7260 ? elf_link_hash_lookup (elf_hash_table (info
),
7261 info
->init_function
, false,
7268 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7271 h
= (info
->fini_function
7272 ? elf_link_hash_lookup (elf_hash_table (info
),
7273 info
->fini_function
, false,
7280 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7284 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7285 if (s
!= NULL
&& s
->linker_has_input
)
7287 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7288 if (! bfd_link_executable (info
))
7293 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7294 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7295 && (o
= sub
->sections
) != NULL
7296 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7297 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7298 if (elf_section_data (o
)->this_hdr
.sh_type
7299 == SHT_PREINIT_ARRAY
)
7302 (_("%pB: .preinit_array section is not allowed in DSO"),
7307 bfd_set_error (bfd_error_nonrepresentable_section
);
7311 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7312 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7315 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7316 if (s
!= NULL
&& s
->linker_has_input
)
7318 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7319 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7322 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7323 if (s
!= NULL
&& s
->linker_has_input
)
7325 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7326 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7330 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7331 /* If .dynstr is excluded from the link, we don't want any of
7332 these tags. Strictly, we should be checking each section
7333 individually; This quick check covers for the case where
7334 someone does a /DISCARD/ : { *(*) }. */
7335 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7337 bfd_size_type strsize
;
7339 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7340 if ((info
->emit_hash
7341 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7342 || (info
->emit_gnu_hash
7343 && (bed
->record_xhash_symbol
== NULL
7344 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7345 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7346 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7347 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7348 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7350 || (info
->gnu_flags_1
7351 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_FLAGS_1
,
7352 info
->gnu_flags_1
)))
7357 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7360 /* The backend must work out the sizes of all the other dynamic
7363 && bed
->elf_backend_size_dynamic_sections
!= NULL
7364 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7367 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7369 if (elf_tdata (output_bfd
)->cverdefs
)
7371 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7373 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7374 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7378 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7380 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7383 else if (info
->flags
& DF_BIND_NOW
)
7385 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7391 if (bfd_link_executable (info
))
7392 info
->flags_1
&= ~ (DF_1_INITFIRST
7395 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7399 if (elf_tdata (output_bfd
)->cverrefs
)
7401 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7403 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7404 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7408 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7409 && elf_tdata (output_bfd
)->cverdefs
== 0)
7410 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7414 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7415 s
->flags
|= SEC_EXCLUDE
;
7421 /* Find the first non-excluded output section. We'll use its
7422 section symbol for some emitted relocs. */
7424 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7427 asection
*found
= NULL
;
7429 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7430 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7431 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7434 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7437 elf_hash_table (info
)->text_index_section
= found
;
7440 /* Find two non-excluded output sections, one for code, one for data.
7441 We'll use their section symbols for some emitted relocs. */
7443 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7446 asection
*found
= NULL
;
7448 /* Data first, since setting text_index_section changes
7449 _bfd_elf_omit_section_dynsym_default. */
7450 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7451 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7452 && !(s
->flags
& SEC_READONLY
)
7453 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7456 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7459 elf_hash_table (info
)->data_index_section
= found
;
7461 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7462 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7463 && (s
->flags
& SEC_READONLY
)
7464 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7469 elf_hash_table (info
)->text_index_section
= found
;
7472 #define GNU_HASH_SECTION_NAME(bed) \
7473 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7476 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7478 const struct elf_backend_data
*bed
;
7479 unsigned long section_sym_count
;
7480 bfd_size_type dynsymcount
= 0;
7482 if (!is_elf_hash_table (info
->hash
))
7485 bed
= get_elf_backend_data (output_bfd
);
7486 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7488 /* Assign dynsym indices. In a shared library we generate a section
7489 symbol for each output section, which come first. Next come all
7490 of the back-end allocated local dynamic syms, followed by the rest
7491 of the global symbols.
7493 This is usually not needed for static binaries, however backends
7494 can request to always do it, e.g. the MIPS backend uses dynamic
7495 symbol counts to lay out GOT, which will be produced in the
7496 presence of GOT relocations even in static binaries (holding fixed
7497 data in that case, to satisfy those relocations). */
7499 if (elf_hash_table (info
)->dynamic_sections_created
7500 || bed
->always_renumber_dynsyms
)
7501 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7502 §ion_sym_count
);
7504 if (elf_hash_table (info
)->dynamic_sections_created
)
7508 unsigned int dtagcount
;
7510 dynobj
= elf_hash_table (info
)->dynobj
;
7512 /* Work out the size of the symbol version section. */
7513 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7514 BFD_ASSERT (s
!= NULL
);
7515 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7517 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7518 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7519 if (s
->contents
== NULL
)
7522 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7526 /* Set the size of the .dynsym and .hash sections. We counted
7527 the number of dynamic symbols in elf_link_add_object_symbols.
7528 We will build the contents of .dynsym and .hash when we build
7529 the final symbol table, because until then we do not know the
7530 correct value to give the symbols. We built the .dynstr
7531 section as we went along in elf_link_add_object_symbols. */
7532 s
= elf_hash_table (info
)->dynsym
;
7533 BFD_ASSERT (s
!= NULL
);
7534 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7536 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7537 if (s
->contents
== NULL
)
7540 /* The first entry in .dynsym is a dummy symbol. Clear all the
7541 section syms, in case we don't output them all. */
7542 ++section_sym_count
;
7543 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7545 elf_hash_table (info
)->bucketcount
= 0;
7547 /* Compute the size of the hashing table. As a side effect this
7548 computes the hash values for all the names we export. */
7549 if (info
->emit_hash
)
7551 unsigned long int *hashcodes
;
7552 struct hash_codes_info hashinf
;
7554 unsigned long int nsyms
;
7556 size_t hash_entry_size
;
7558 /* Compute the hash values for all exported symbols. At the same
7559 time store the values in an array so that we could use them for
7561 amt
= dynsymcount
* sizeof (unsigned long int);
7562 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7563 if (hashcodes
== NULL
)
7565 hashinf
.hashcodes
= hashcodes
;
7566 hashinf
.error
= false;
7568 /* Put all hash values in HASHCODES. */
7569 elf_link_hash_traverse (elf_hash_table (info
),
7570 elf_collect_hash_codes
, &hashinf
);
7577 nsyms
= hashinf
.hashcodes
- hashcodes
;
7579 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7582 if (bucketcount
== 0 && nsyms
> 0)
7585 elf_hash_table (info
)->bucketcount
= bucketcount
;
7587 s
= bfd_get_linker_section (dynobj
, ".hash");
7588 BFD_ASSERT (s
!= NULL
);
7589 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7590 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7591 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7592 if (s
->contents
== NULL
)
7595 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7596 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7597 s
->contents
+ hash_entry_size
);
7600 if (info
->emit_gnu_hash
)
7603 unsigned char *contents
;
7604 struct collect_gnu_hash_codes cinfo
;
7608 memset (&cinfo
, 0, sizeof (cinfo
));
7610 /* Compute the hash values for all exported symbols. At the same
7611 time store the values in an array so that we could use them for
7613 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7614 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7615 if (cinfo
.hashcodes
== NULL
)
7618 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7619 cinfo
.min_dynindx
= -1;
7620 cinfo
.output_bfd
= output_bfd
;
7623 /* Put all hash values in HASHCODES. */
7624 elf_link_hash_traverse (elf_hash_table (info
),
7625 elf_collect_gnu_hash_codes
, &cinfo
);
7628 free (cinfo
.hashcodes
);
7633 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7635 if (bucketcount
== 0)
7637 free (cinfo
.hashcodes
);
7641 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7642 BFD_ASSERT (s
!= NULL
);
7644 if (cinfo
.nsyms
== 0)
7646 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7647 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7648 free (cinfo
.hashcodes
);
7649 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7650 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7651 if (contents
== NULL
)
7653 s
->contents
= contents
;
7654 /* 1 empty bucket. */
7655 bfd_put_32 (output_bfd
, 1, contents
);
7656 /* SYMIDX above the special symbol 0. */
7657 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7658 /* Just one word for bitmask. */
7659 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7660 /* Only hash fn bloom filter. */
7661 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7662 /* No hashes are valid - empty bitmask. */
7663 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7664 /* No hashes in the only bucket. */
7665 bfd_put_32 (output_bfd
, 0,
7666 contents
+ 16 + bed
->s
->arch_size
/ 8);
7670 unsigned long int maskwords
, maskbitslog2
, x
;
7671 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7675 while ((x
>>= 1) != 0)
7677 if (maskbitslog2
< 3)
7679 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7680 maskbitslog2
= maskbitslog2
+ 3;
7682 maskbitslog2
= maskbitslog2
+ 2;
7683 if (bed
->s
->arch_size
== 64)
7685 if (maskbitslog2
== 5)
7691 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7692 cinfo
.shift2
= maskbitslog2
;
7693 cinfo
.maskbits
= 1 << maskbitslog2
;
7694 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7695 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7696 amt
+= maskwords
* sizeof (bfd_vma
);
7697 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7698 if (cinfo
.bitmask
== NULL
)
7700 free (cinfo
.hashcodes
);
7704 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7705 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7706 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7707 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7709 /* Determine how often each hash bucket is used. */
7710 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7711 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7712 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7714 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7715 if (cinfo
.counts
[i
] != 0)
7717 cinfo
.indx
[i
] = cnt
;
7718 cnt
+= cinfo
.counts
[i
];
7720 BFD_ASSERT (cnt
== dynsymcount
);
7721 cinfo
.bucketcount
= bucketcount
;
7722 cinfo
.local_indx
= cinfo
.min_dynindx
;
7724 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7725 s
->size
+= cinfo
.maskbits
/ 8;
7726 if (bed
->record_xhash_symbol
!= NULL
)
7727 s
->size
+= cinfo
.nsyms
* 4;
7728 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7729 if (contents
== NULL
)
7731 free (cinfo
.bitmask
);
7732 free (cinfo
.hashcodes
);
7736 s
->contents
= contents
;
7737 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7738 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7739 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7740 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7741 contents
+= 16 + cinfo
.maskbits
/ 8;
7743 for (i
= 0; i
< bucketcount
; ++i
)
7745 if (cinfo
.counts
[i
] == 0)
7746 bfd_put_32 (output_bfd
, 0, contents
);
7748 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7752 cinfo
.contents
= contents
;
7754 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7755 /* Renumber dynamic symbols, if populating .gnu.hash section.
7756 If using .MIPS.xhash, populate the translation table. */
7757 elf_link_hash_traverse (elf_hash_table (info
),
7758 elf_gnu_hash_process_symidx
, &cinfo
);
7760 contents
= s
->contents
+ 16;
7761 for (i
= 0; i
< maskwords
; ++i
)
7763 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7765 contents
+= bed
->s
->arch_size
/ 8;
7768 free (cinfo
.bitmask
);
7769 free (cinfo
.hashcodes
);
7773 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7774 BFD_ASSERT (s
!= NULL
);
7776 elf_finalize_dynstr (output_bfd
, info
);
7778 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7780 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7781 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7788 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7791 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7794 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7795 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7798 /* Finish SHF_MERGE section merging. */
7801 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7806 if (!is_elf_hash_table (info
->hash
))
7809 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7810 if ((ibfd
->flags
& DYNAMIC
) == 0
7811 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7812 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7813 == get_elf_backend_data (obfd
)->s
->elfclass
))
7814 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7815 if ((sec
->flags
& SEC_MERGE
) != 0
7816 && !bfd_is_abs_section (sec
->output_section
))
7818 struct bfd_elf_section_data
*secdata
;
7820 secdata
= elf_section_data (sec
);
7821 if (! _bfd_add_merge_section (obfd
,
7822 &elf_hash_table (info
)->merge_info
,
7823 sec
, &secdata
->sec_info
))
7825 else if (secdata
->sec_info
)
7826 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7829 if (elf_hash_table (info
)->merge_info
!= NULL
)
7830 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7831 merge_sections_remove_hook
);
7835 /* Create an entry in an ELF linker hash table. */
7837 struct bfd_hash_entry
*
7838 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7839 struct bfd_hash_table
*table
,
7842 /* Allocate the structure if it has not already been allocated by a
7846 entry
= (struct bfd_hash_entry
*)
7847 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7852 /* Call the allocation method of the superclass. */
7853 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7856 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7857 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7859 /* Set local fields. */
7862 ret
->got
= htab
->init_got_refcount
;
7863 ret
->plt
= htab
->init_plt_refcount
;
7864 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7865 - offsetof (struct elf_link_hash_entry
, size
)));
7866 /* Assume that we have been called by a non-ELF symbol reader.
7867 This flag is then reset by the code which reads an ELF input
7868 file. This ensures that a symbol created by a non-ELF symbol
7869 reader will have the flag set correctly. */
7876 /* Copy data from an indirect symbol to its direct symbol, hiding the
7877 old indirect symbol. Also used for copying flags to a weakdef. */
7880 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7881 struct elf_link_hash_entry
*dir
,
7882 struct elf_link_hash_entry
*ind
)
7884 struct elf_link_hash_table
*htab
;
7886 if (ind
->dyn_relocs
!= NULL
)
7888 if (dir
->dyn_relocs
!= NULL
)
7890 struct elf_dyn_relocs
**pp
;
7891 struct elf_dyn_relocs
*p
;
7893 /* Add reloc counts against the indirect sym to the direct sym
7894 list. Merge any entries against the same section. */
7895 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
7897 struct elf_dyn_relocs
*q
;
7899 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
7900 if (q
->sec
== p
->sec
)
7902 q
->pc_count
+= p
->pc_count
;
7903 q
->count
+= p
->count
;
7910 *pp
= dir
->dyn_relocs
;
7913 dir
->dyn_relocs
= ind
->dyn_relocs
;
7914 ind
->dyn_relocs
= NULL
;
7917 /* Copy down any references that we may have already seen to the
7918 symbol which just became indirect. */
7920 if (dir
->versioned
!= versioned_hidden
)
7921 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7922 dir
->ref_regular
|= ind
->ref_regular
;
7923 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7924 dir
->non_got_ref
|= ind
->non_got_ref
;
7925 dir
->needs_plt
|= ind
->needs_plt
;
7926 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7928 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7931 /* Copy over the global and procedure linkage table refcount entries.
7932 These may have been already set up by a check_relocs routine. */
7933 htab
= elf_hash_table (info
);
7934 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7936 if (dir
->got
.refcount
< 0)
7937 dir
->got
.refcount
= 0;
7938 dir
->got
.refcount
+= ind
->got
.refcount
;
7939 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7942 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7944 if (dir
->plt
.refcount
< 0)
7945 dir
->plt
.refcount
= 0;
7946 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7947 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7950 if (ind
->dynindx
!= -1)
7952 if (dir
->dynindx
!= -1)
7953 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7954 dir
->dynindx
= ind
->dynindx
;
7955 dir
->dynstr_index
= ind
->dynstr_index
;
7957 ind
->dynstr_index
= 0;
7962 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7963 struct elf_link_hash_entry
*h
,
7966 /* STT_GNU_IFUNC symbol must go through PLT. */
7967 if (h
->type
!= STT_GNU_IFUNC
)
7969 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7974 h
->forced_local
= 1;
7975 if (h
->dynindx
!= -1)
7977 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7980 h
->dynstr_index
= 0;
7985 /* Hide a symbol. */
7988 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
7989 struct bfd_link_info
*info
,
7990 struct bfd_link_hash_entry
*h
)
7992 if (is_elf_hash_table (info
->hash
))
7994 const struct elf_backend_data
*bed
7995 = get_elf_backend_data (output_bfd
);
7996 struct elf_link_hash_entry
*eh
7997 = (struct elf_link_hash_entry
*) h
;
7998 bed
->elf_backend_hide_symbol (info
, eh
, true);
7999 eh
->def_dynamic
= 0;
8000 eh
->ref_dynamic
= 0;
8001 eh
->dynamic_def
= 0;
8005 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8009 _bfd_elf_link_hash_table_init
8010 (struct elf_link_hash_table
*table
,
8012 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
8013 struct bfd_hash_table
*,
8015 unsigned int entsize
,
8016 enum elf_target_id target_id
)
8019 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
8021 table
->init_got_refcount
.refcount
= can_refcount
- 1;
8022 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
8023 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
8024 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
8025 /* The first dynamic symbol is a dummy. */
8026 table
->dynsymcount
= 1;
8028 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
8030 table
->root
.type
= bfd_link_elf_hash_table
;
8031 table
->hash_table_id
= target_id
;
8032 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
8037 /* Create an ELF linker hash table. */
8039 struct bfd_link_hash_table
*
8040 _bfd_elf_link_hash_table_create (bfd
*abfd
)
8042 struct elf_link_hash_table
*ret
;
8043 size_t amt
= sizeof (struct elf_link_hash_table
);
8045 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
8049 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
8050 sizeof (struct elf_link_hash_entry
),
8056 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
8061 /* Destroy an ELF linker hash table. */
8064 _bfd_elf_link_hash_table_free (bfd
*obfd
)
8066 struct elf_link_hash_table
*htab
;
8068 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
8069 if (htab
->dynstr
!= NULL
)
8070 _bfd_elf_strtab_free (htab
->dynstr
);
8071 _bfd_merge_sections_free (htab
->merge_info
);
8072 _bfd_generic_link_hash_table_free (obfd
);
8075 /* This is a hook for the ELF emulation code in the generic linker to
8076 tell the backend linker what file name to use for the DT_NEEDED
8077 entry for a dynamic object. */
8080 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
8082 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8083 && bfd_get_format (abfd
) == bfd_object
)
8084 elf_dt_name (abfd
) = name
;
8088 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
8091 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8092 && bfd_get_format (abfd
) == bfd_object
)
8093 lib_class
= elf_dyn_lib_class (abfd
);
8100 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
8102 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8103 && bfd_get_format (abfd
) == bfd_object
)
8104 elf_dyn_lib_class (abfd
) = lib_class
;
8107 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8108 the linker ELF emulation code. */
8110 struct bfd_link_needed_list
*
8111 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8112 struct bfd_link_info
*info
)
8114 if (! is_elf_hash_table (info
->hash
))
8116 return elf_hash_table (info
)->needed
;
8119 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8120 hook for the linker ELF emulation code. */
8122 struct bfd_link_needed_list
*
8123 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8124 struct bfd_link_info
*info
)
8126 if (! is_elf_hash_table (info
->hash
))
8128 return elf_hash_table (info
)->runpath
;
8131 /* Get the name actually used for a dynamic object for a link. This
8132 is the SONAME entry if there is one. Otherwise, it is the string
8133 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8136 bfd_elf_get_dt_soname (bfd
*abfd
)
8138 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8139 && bfd_get_format (abfd
) == bfd_object
)
8140 return elf_dt_name (abfd
);
8144 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8145 the ELF linker emulation code. */
8148 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
8149 struct bfd_link_needed_list
**pneeded
)
8152 bfd_byte
*dynbuf
= NULL
;
8153 unsigned int elfsec
;
8154 unsigned long shlink
;
8155 bfd_byte
*extdyn
, *extdynend
;
8157 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
8161 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
8162 || bfd_get_format (abfd
) != bfd_object
)
8165 s
= bfd_get_section_by_name (abfd
, ".dynamic");
8166 if (s
== NULL
|| s
->size
== 0)
8169 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
8172 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8173 if (elfsec
== SHN_BAD
)
8176 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8178 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8179 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8182 extdynend
= extdyn
+ s
->size
;
8183 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
8185 Elf_Internal_Dyn dyn
;
8187 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8189 if (dyn
.d_tag
== DT_NULL
)
8192 if (dyn
.d_tag
== DT_NEEDED
)
8195 struct bfd_link_needed_list
*l
;
8196 unsigned int tagv
= dyn
.d_un
.d_val
;
8199 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8204 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8224 struct elf_symbuf_symbol
8226 unsigned long st_name
; /* Symbol name, index in string tbl */
8227 unsigned char st_info
; /* Type and binding attributes */
8228 unsigned char st_other
; /* Visibilty, and target specific */
8231 struct elf_symbuf_head
8233 struct elf_symbuf_symbol
*ssym
;
8235 unsigned int st_shndx
;
8242 Elf_Internal_Sym
*isym
;
8243 struct elf_symbuf_symbol
*ssym
;
8249 /* Sort references to symbols by ascending section number. */
8252 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8254 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8255 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8257 if (s1
->st_shndx
!= s2
->st_shndx
)
8258 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8259 /* Final sort by the address of the sym in the symbuf ensures
8262 return s1
> s2
? 1 : -1;
8267 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8269 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8270 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8271 int ret
= strcmp (s1
->name
, s2
->name
);
8274 if (s1
->u
.p
!= s2
->u
.p
)
8275 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8279 static struct elf_symbuf_head
*
8280 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8282 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8283 struct elf_symbuf_symbol
*ssym
;
8284 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8285 size_t i
, shndx_count
, total_size
, amt
;
8287 amt
= symcount
* sizeof (*indbuf
);
8288 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8292 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8293 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8294 *ind
++ = &isymbuf
[i
];
8297 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8298 elf_sort_elf_symbol
);
8301 if (indbufend
> indbuf
)
8302 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8303 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8306 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8307 + (indbufend
- indbuf
) * sizeof (*ssym
));
8308 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8309 if (ssymbuf
== NULL
)
8315 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8316 ssymbuf
->ssym
= NULL
;
8317 ssymbuf
->count
= shndx_count
;
8318 ssymbuf
->st_shndx
= 0;
8319 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8321 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8324 ssymhead
->ssym
= ssym
;
8325 ssymhead
->count
= 0;
8326 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8328 ssym
->st_name
= (*ind
)->st_name
;
8329 ssym
->st_info
= (*ind
)->st_info
;
8330 ssym
->st_other
= (*ind
)->st_other
;
8333 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8334 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
8341 /* Check if 2 sections define the same set of local and global
8345 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8346 struct bfd_link_info
*info
)
8349 const struct elf_backend_data
*bed1
, *bed2
;
8350 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8351 size_t symcount1
, symcount2
;
8352 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8353 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8354 Elf_Internal_Sym
*isym
, *isymend
;
8355 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8356 size_t count1
, count2
, sec_count1
, sec_count2
, i
;
8357 unsigned int shndx1
, shndx2
;
8359 bool ignore_section_symbol_p
;
8364 /* Both sections have to be in ELF. */
8365 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8366 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8369 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8372 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8373 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8374 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8377 bed1
= get_elf_backend_data (bfd1
);
8378 bed2
= get_elf_backend_data (bfd2
);
8379 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8380 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8381 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8382 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8384 if (symcount1
== 0 || symcount2
== 0)
8390 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8391 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8393 /* Ignore section symbols only when matching non-debugging sections
8394 or linkonce section with comdat section. */
8395 ignore_section_symbol_p
8396 = ((sec1
->flags
& SEC_DEBUGGING
) == 0
8397 || ((elf_section_flags (sec1
) & SHF_GROUP
)
8398 != (elf_section_flags (sec2
) & SHF_GROUP
)));
8400 if (ssymbuf1
== NULL
)
8402 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8404 if (isymbuf1
== NULL
)
8407 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8409 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8410 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8414 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8416 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8418 if (isymbuf2
== NULL
)
8421 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8423 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8424 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8428 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8430 /* Optimized faster version. */
8432 struct elf_symbol
*symp
;
8433 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8436 hi
= ssymbuf1
->count
;
8442 mid
= (lo
+ hi
) / 2;
8443 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8445 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8449 count1
= ssymbuf1
[mid
].count
;
8454 if (ignore_section_symbol_p
)
8456 for (i
= 0; i
< count1
; i
++)
8457 if (ELF_ST_TYPE (ssymbuf1
->ssym
[i
].st_info
) == STT_SECTION
)
8459 count1
-= sec_count1
;
8463 hi
= ssymbuf2
->count
;
8469 mid
= (lo
+ hi
) / 2;
8470 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8472 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8476 count2
= ssymbuf2
[mid
].count
;
8481 if (ignore_section_symbol_p
)
8483 for (i
= 0; i
< count2
; i
++)
8484 if (ELF_ST_TYPE (ssymbuf2
->ssym
[i
].st_info
) == STT_SECTION
)
8486 count2
-= sec_count2
;
8489 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8493 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8495 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8496 if (symtable1
== NULL
|| symtable2
== NULL
)
8500 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
+ sec_count1
;
8501 ssym
< ssymend
; ssym
++)
8503 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8505 symp
->u
.ssym
= ssym
;
8506 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8513 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
+ sec_count2
;
8514 ssym
< ssymend
; ssym
++)
8516 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8518 symp
->u
.ssym
= ssym
;
8519 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8525 /* Sort symbol by name. */
8526 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8527 elf_sym_name_compare
);
8528 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8529 elf_sym_name_compare
);
8531 for (i
= 0; i
< count1
; i
++)
8532 /* Two symbols must have the same binding, type and name. */
8533 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8534 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8535 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8542 symtable1
= (struct elf_symbol
*)
8543 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8544 symtable2
= (struct elf_symbol
*)
8545 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8546 if (symtable1
== NULL
|| symtable2
== NULL
)
8549 /* Count definitions in the section. */
8551 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8552 if (isym
->st_shndx
== shndx1
8553 && (!ignore_section_symbol_p
8554 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8555 symtable1
[count1
++].u
.isym
= isym
;
8558 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8559 if (isym
->st_shndx
== shndx2
8560 && (!ignore_section_symbol_p
8561 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8562 symtable2
[count2
++].u
.isym
= isym
;
8564 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8567 for (i
= 0; i
< count1
; i
++)
8569 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8570 symtable1
[i
].u
.isym
->st_name
);
8572 for (i
= 0; i
< count2
; i
++)
8574 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8575 symtable2
[i
].u
.isym
->st_name
);
8577 /* Sort symbol by name. */
8578 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8579 elf_sym_name_compare
);
8580 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8581 elf_sym_name_compare
);
8583 for (i
= 0; i
< count1
; i
++)
8584 /* Two symbols must have the same binding, type and name. */
8585 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8586 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8587 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8601 /* Return TRUE if 2 section types are compatible. */
8604 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8605 bfd
*bbfd
, const asection
*bsec
)
8609 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8610 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8613 return elf_section_type (asec
) == elf_section_type (bsec
);
8616 /* Final phase of ELF linker. */
8618 /* A structure we use to avoid passing large numbers of arguments. */
8620 struct elf_final_link_info
8622 /* General link information. */
8623 struct bfd_link_info
*info
;
8626 /* Symbol string table. */
8627 struct elf_strtab_hash
*symstrtab
;
8628 /* .hash section. */
8630 /* symbol version section (.gnu.version). */
8631 asection
*symver_sec
;
8632 /* Buffer large enough to hold contents of any section. */
8634 /* Buffer large enough to hold external relocs of any section. */
8635 void *external_relocs
;
8636 /* Buffer large enough to hold internal relocs of any section. */
8637 Elf_Internal_Rela
*internal_relocs
;
8638 /* Buffer large enough to hold external local symbols of any input
8640 bfd_byte
*external_syms
;
8641 /* And a buffer for symbol section indices. */
8642 Elf_External_Sym_Shndx
*locsym_shndx
;
8643 /* Buffer large enough to hold internal local symbols of any input
8645 Elf_Internal_Sym
*internal_syms
;
8646 /* Array large enough to hold a symbol index for each local symbol
8647 of any input BFD. */
8649 /* Array large enough to hold a section pointer for each local
8650 symbol of any input BFD. */
8651 asection
**sections
;
8652 /* Buffer for SHT_SYMTAB_SHNDX section. */
8653 Elf_External_Sym_Shndx
*symshndxbuf
;
8654 /* Number of STT_FILE syms seen. */
8655 size_t filesym_count
;
8656 /* Local symbol hash table. */
8657 struct bfd_hash_table local_hash_table
;
8660 struct local_hash_entry
8662 /* Base hash table entry structure. */
8663 struct bfd_hash_entry root
;
8664 /* Size of the local symbol name. */
8666 /* Number of the duplicated local symbol names. */
8670 /* Create an entry in the local symbol hash table. */
8672 static struct bfd_hash_entry
*
8673 local_hash_newfunc (struct bfd_hash_entry
*entry
,
8674 struct bfd_hash_table
*table
,
8678 /* Allocate the structure if it has not already been allocated by a
8682 entry
= bfd_hash_allocate (table
,
8683 sizeof (struct local_hash_entry
));
8688 /* Call the allocation method of the superclass. */
8689 entry
= bfd_hash_newfunc (entry
, table
, string
);
8692 ((struct local_hash_entry
*) entry
)->count
= 0;
8693 ((struct local_hash_entry
*) entry
)->size
= 0;
8699 /* This struct is used to pass information to elf_link_output_extsym. */
8701 struct elf_outext_info
8706 struct elf_final_link_info
*flinfo
;
8710 /* Support for evaluating a complex relocation.
8712 Complex relocations are generalized, self-describing relocations. The
8713 implementation of them consists of two parts: complex symbols, and the
8714 relocations themselves.
8716 The relocations use a reserved elf-wide relocation type code (R_RELC
8717 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8718 information (start bit, end bit, word width, etc) into the addend. This
8719 information is extracted from CGEN-generated operand tables within gas.
8721 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8722 internal) representing prefix-notation expressions, including but not
8723 limited to those sorts of expressions normally encoded as addends in the
8724 addend field. The symbol mangling format is:
8727 | <unary-operator> ':' <node>
8728 | <binary-operator> ':' <node> ':' <node>
8731 <literal> := 's' <digits=N> ':' <N character symbol name>
8732 | 'S' <digits=N> ':' <N character section name>
8736 <binary-operator> := as in C
8737 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8740 set_symbol_value (bfd
*bfd_with_globals
,
8741 Elf_Internal_Sym
*isymbuf
,
8746 struct elf_link_hash_entry
**sym_hashes
;
8747 struct elf_link_hash_entry
*h
;
8748 size_t extsymoff
= locsymcount
;
8750 if (symidx
< locsymcount
)
8752 Elf_Internal_Sym
*sym
;
8754 sym
= isymbuf
+ symidx
;
8755 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8757 /* It is a local symbol: move it to the
8758 "absolute" section and give it a value. */
8759 sym
->st_shndx
= SHN_ABS
;
8760 sym
->st_value
= val
;
8763 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8767 /* It is a global symbol: set its link type
8768 to "defined" and give it a value. */
8770 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8771 h
= sym_hashes
[symidx
- extsymoff
];
8772 while (h
->root
.type
== bfd_link_hash_indirect
8773 || h
->root
.type
== bfd_link_hash_warning
)
8774 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8775 h
->root
.type
= bfd_link_hash_defined
;
8776 h
->root
.u
.def
.value
= val
;
8777 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8781 resolve_symbol (const char *name
,
8783 struct elf_final_link_info
*flinfo
,
8785 Elf_Internal_Sym
*isymbuf
,
8788 Elf_Internal_Sym
*sym
;
8789 struct bfd_link_hash_entry
*global_entry
;
8790 const char *candidate
= NULL
;
8791 Elf_Internal_Shdr
*symtab_hdr
;
8794 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8796 for (i
= 0; i
< locsymcount
; ++ i
)
8800 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8803 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8804 symtab_hdr
->sh_link
,
8807 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8808 name
, candidate
, (unsigned long) sym
->st_value
);
8810 if (candidate
&& strcmp (candidate
, name
) == 0)
8812 asection
*sec
= flinfo
->sections
[i
];
8814 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8815 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8817 printf ("Found symbol with value %8.8lx\n",
8818 (unsigned long) *result
);
8824 /* Hmm, haven't found it yet. perhaps it is a global. */
8825 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8826 false, false, true);
8830 if (global_entry
->type
== bfd_link_hash_defined
8831 || global_entry
->type
== bfd_link_hash_defweak
)
8833 *result
= (global_entry
->u
.def
.value
8834 + global_entry
->u
.def
.section
->output_section
->vma
8835 + global_entry
->u
.def
.section
->output_offset
);
8837 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8838 global_entry
->root
.string
, (unsigned long) *result
);
8846 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8847 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8848 names like "foo.end" which is the end address of section "foo". */
8851 resolve_section (const char *name
,
8859 for (curr
= sections
; curr
; curr
= curr
->next
)
8860 if (strcmp (curr
->name
, name
) == 0)
8862 *result
= curr
->vma
;
8866 /* Hmm. still haven't found it. try pseudo-section names. */
8867 /* FIXME: This could be coded more efficiently... */
8868 for (curr
= sections
; curr
; curr
= curr
->next
)
8870 len
= strlen (curr
->name
);
8871 if (len
> strlen (name
))
8874 if (strncmp (curr
->name
, name
, len
) == 0)
8876 if (startswith (name
+ len
, ".end"))
8878 *result
= (curr
->vma
8879 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
8883 /* Insert more pseudo-section names here, if you like. */
8891 undefined_reference (const char *reftype
, const char *name
)
8893 /* xgettext:c-format */
8894 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8896 bfd_set_error (bfd_error_bad_value
);
8900 eval_symbol (bfd_vma
*result
,
8903 struct elf_final_link_info
*flinfo
,
8905 Elf_Internal_Sym
*isymbuf
,
8914 const char *sym
= *symp
;
8916 bool symbol_is_section
= false;
8921 if (len
< 1 || len
> sizeof (symbuf
))
8923 bfd_set_error (bfd_error_invalid_operation
);
8936 *result
= strtoul (sym
, (char **) symp
, 16);
8940 symbol_is_section
= true;
8944 symlen
= strtol (sym
, (char **) symp
, 10);
8945 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8947 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8949 bfd_set_error (bfd_error_invalid_operation
);
8953 memcpy (symbuf
, sym
, symlen
);
8954 symbuf
[symlen
] = '\0';
8955 *symp
= sym
+ symlen
;
8957 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8958 the symbol as a section, or vice-versa. so we're pretty liberal in our
8959 interpretation here; section means "try section first", not "must be a
8960 section", and likewise with symbol. */
8962 if (symbol_is_section
)
8964 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8965 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8966 isymbuf
, locsymcount
))
8968 undefined_reference ("section", symbuf
);
8974 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8975 isymbuf
, locsymcount
)
8976 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8979 undefined_reference ("symbol", symbuf
);
8986 /* All that remains are operators. */
8988 #define UNARY_OP(op) \
8989 if (startswith (sym, #op)) \
8991 sym += strlen (#op); \
8995 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8996 isymbuf, locsymcount, signed_p)) \
8999 *result = op ((bfd_signed_vma) a); \
9005 #define BINARY_OP_HEAD(op) \
9006 if (startswith (sym, #op)) \
9008 sym += strlen (#op); \
9012 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9013 isymbuf, locsymcount, signed_p)) \
9016 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9017 isymbuf, locsymcount, signed_p)) \
9019 #define BINARY_OP_TAIL(op) \
9021 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9026 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9030 BINARY_OP_HEAD (<<);
9031 if (b
>= sizeof (a
) * CHAR_BIT
)
9037 BINARY_OP_TAIL (<<);
9038 BINARY_OP_HEAD (>>);
9039 if (b
>= sizeof (a
) * CHAR_BIT
)
9041 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
9044 BINARY_OP_TAIL (>>);
9057 _bfd_error_handler (_("division by zero"));
9058 bfd_set_error (bfd_error_bad_value
);
9065 _bfd_error_handler (_("division by zero"));
9066 bfd_set_error (bfd_error_bad_value
);
9079 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
9080 bfd_set_error (bfd_error_invalid_operation
);
9086 put_value (bfd_vma size
,
9087 unsigned long chunksz
,
9092 location
+= (size
- chunksz
);
9094 for (; size
; size
-= chunksz
, location
-= chunksz
)
9099 bfd_put_8 (input_bfd
, x
, location
);
9103 bfd_put_16 (input_bfd
, x
, location
);
9107 bfd_put_32 (input_bfd
, x
, location
);
9108 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9114 bfd_put_64 (input_bfd
, x
, location
);
9115 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9128 get_value (bfd_vma size
,
9129 unsigned long chunksz
,
9136 /* Sanity checks. */
9137 BFD_ASSERT (chunksz
<= sizeof (x
)
9140 && (size
% chunksz
) == 0
9141 && input_bfd
!= NULL
9142 && location
!= NULL
);
9144 if (chunksz
== sizeof (x
))
9146 BFD_ASSERT (size
== chunksz
);
9148 /* Make sure that we do not perform an undefined shift operation.
9149 We know that size == chunksz so there will only be one iteration
9150 of the loop below. */
9154 shift
= 8 * chunksz
;
9156 for (; size
; size
-= chunksz
, location
+= chunksz
)
9161 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
9164 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
9167 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
9171 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
9182 decode_complex_addend (unsigned long *start
, /* in bits */
9183 unsigned long *oplen
, /* in bits */
9184 unsigned long *len
, /* in bits */
9185 unsigned long *wordsz
, /* in bytes */
9186 unsigned long *chunksz
, /* in bytes */
9187 unsigned long *lsb0_p
,
9188 unsigned long *signed_p
,
9189 unsigned long *trunc_p
,
9190 unsigned long encoded
)
9192 * start
= encoded
& 0x3F;
9193 * len
= (encoded
>> 6) & 0x3F;
9194 * oplen
= (encoded
>> 12) & 0x3F;
9195 * wordsz
= (encoded
>> 18) & 0xF;
9196 * chunksz
= (encoded
>> 22) & 0xF;
9197 * lsb0_p
= (encoded
>> 27) & 1;
9198 * signed_p
= (encoded
>> 28) & 1;
9199 * trunc_p
= (encoded
>> 29) & 1;
9202 bfd_reloc_status_type
9203 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
9204 asection
*input_section
,
9206 Elf_Internal_Rela
*rel
,
9209 bfd_vma shift
, x
, mask
;
9210 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9211 bfd_reloc_status_type r
;
9212 bfd_size_type octets
;
9214 /* Perform this reloc, since it is complex.
9215 (this is not to say that it necessarily refers to a complex
9216 symbol; merely that it is a self-describing CGEN based reloc.
9217 i.e. the addend has the complete reloc information (bit start, end,
9218 word size, etc) encoded within it.). */
9220 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9221 &chunksz
, &lsb0_p
, &signed_p
,
9222 &trunc_p
, rel
->r_addend
);
9224 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9227 shift
= (start
+ 1) - len
;
9229 shift
= (8 * wordsz
) - (start
+ len
);
9231 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9232 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9235 printf ("Doing complex reloc: "
9236 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9237 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9238 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9239 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9240 oplen
, (unsigned long) x
, (unsigned long) mask
,
9241 (unsigned long) relocation
);
9246 /* Now do an overflow check. */
9247 r
= bfd_check_overflow ((signed_p
9248 ? complain_overflow_signed
9249 : complain_overflow_unsigned
),
9250 len
, 0, (8 * wordsz
),
9254 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9257 printf (" relocation: %8.8lx\n"
9258 " shifted mask: %8.8lx\n"
9259 " shifted/masked reloc: %8.8lx\n"
9260 " result: %8.8lx\n",
9261 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9262 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9264 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9268 /* Functions to read r_offset from external (target order) reloc
9269 entry. Faster than bfd_getl32 et al, because we let the compiler
9270 know the value is aligned. */
9273 ext32l_r_offset (const void *p
)
9280 const union aligned32
*a
9281 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9283 uint32_t aval
= ( (uint32_t) a
->c
[0]
9284 | (uint32_t) a
->c
[1] << 8
9285 | (uint32_t) a
->c
[2] << 16
9286 | (uint32_t) a
->c
[3] << 24);
9291 ext32b_r_offset (const void *p
)
9298 const union aligned32
*a
9299 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9301 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9302 | (uint32_t) a
->c
[1] << 16
9303 | (uint32_t) a
->c
[2] << 8
9304 | (uint32_t) a
->c
[3]);
9308 #ifdef BFD_HOST_64_BIT
9310 ext64l_r_offset (const void *p
)
9317 const union aligned64
*a
9318 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9320 uint64_t aval
= ( (uint64_t) a
->c
[0]
9321 | (uint64_t) a
->c
[1] << 8
9322 | (uint64_t) a
->c
[2] << 16
9323 | (uint64_t) a
->c
[3] << 24
9324 | (uint64_t) a
->c
[4] << 32
9325 | (uint64_t) a
->c
[5] << 40
9326 | (uint64_t) a
->c
[6] << 48
9327 | (uint64_t) a
->c
[7] << 56);
9332 ext64b_r_offset (const void *p
)
9339 const union aligned64
*a
9340 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9342 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9343 | (uint64_t) a
->c
[1] << 48
9344 | (uint64_t) a
->c
[2] << 40
9345 | (uint64_t) a
->c
[3] << 32
9346 | (uint64_t) a
->c
[4] << 24
9347 | (uint64_t) a
->c
[5] << 16
9348 | (uint64_t) a
->c
[6] << 8
9349 | (uint64_t) a
->c
[7]);
9354 /* When performing a relocatable link, the input relocations are
9355 preserved. But, if they reference global symbols, the indices
9356 referenced must be updated. Update all the relocations found in
9360 elf_link_adjust_relocs (bfd
*abfd
,
9362 struct bfd_elf_section_reloc_data
*reldata
,
9364 struct bfd_link_info
*info
)
9367 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9369 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9370 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9371 bfd_vma r_type_mask
;
9373 unsigned int count
= reldata
->count
;
9374 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9376 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9378 swap_in
= bed
->s
->swap_reloc_in
;
9379 swap_out
= bed
->s
->swap_reloc_out
;
9381 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9383 swap_in
= bed
->s
->swap_reloca_in
;
9384 swap_out
= bed
->s
->swap_reloca_out
;
9389 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9392 if (bed
->s
->arch_size
== 32)
9399 r_type_mask
= 0xffffffff;
9403 erela
= reldata
->hdr
->contents
;
9404 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9406 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9409 if (*rel_hash
== NULL
)
9412 if ((*rel_hash
)->indx
== -2
9413 && info
->gc_sections
9414 && ! info
->gc_keep_exported
)
9416 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9417 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9419 (*rel_hash
)->root
.root
.string
);
9420 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9422 bfd_set_error (bfd_error_invalid_operation
);
9425 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9427 (*swap_in
) (abfd
, erela
, irela
);
9428 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9429 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9430 | (irela
[j
].r_info
& r_type_mask
));
9431 (*swap_out
) (abfd
, irela
, erela
);
9434 if (bed
->elf_backend_update_relocs
)
9435 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9437 if (sort
&& count
!= 0)
9439 bfd_vma (*ext_r_off
) (const void *);
9442 bfd_byte
*base
, *end
, *p
, *loc
;
9443 bfd_byte
*buf
= NULL
;
9445 if (bed
->s
->arch_size
== 32)
9447 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9448 ext_r_off
= ext32l_r_offset
;
9449 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9450 ext_r_off
= ext32b_r_offset
;
9456 #ifdef BFD_HOST_64_BIT
9457 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9458 ext_r_off
= ext64l_r_offset
;
9459 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9460 ext_r_off
= ext64b_r_offset
;
9466 /* Must use a stable sort here. A modified insertion sort,
9467 since the relocs are mostly sorted already. */
9468 elt_size
= reldata
->hdr
->sh_entsize
;
9469 base
= reldata
->hdr
->contents
;
9470 end
= base
+ count
* elt_size
;
9471 if (elt_size
> sizeof (Elf64_External_Rela
))
9474 /* Ensure the first element is lowest. This acts as a sentinel,
9475 speeding the main loop below. */
9476 r_off
= (*ext_r_off
) (base
);
9477 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9479 bfd_vma r_off2
= (*ext_r_off
) (p
);
9488 /* Don't just swap *base and *loc as that changes the order
9489 of the original base[0] and base[1] if they happen to
9490 have the same r_offset. */
9491 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9492 memcpy (onebuf
, loc
, elt_size
);
9493 memmove (base
+ elt_size
, base
, loc
- base
);
9494 memcpy (base
, onebuf
, elt_size
);
9497 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9499 /* base to p is sorted, *p is next to insert. */
9500 r_off
= (*ext_r_off
) (p
);
9501 /* Search the sorted region for location to insert. */
9503 while (r_off
< (*ext_r_off
) (loc
))
9508 /* Chances are there is a run of relocs to insert here,
9509 from one of more input files. Files are not always
9510 linked in order due to the way elf_link_input_bfd is
9511 called. See pr17666. */
9512 size_t sortlen
= p
- loc
;
9513 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9514 size_t runlen
= elt_size
;
9515 size_t buf_size
= 96 * 1024;
9516 while (p
+ runlen
< end
9517 && (sortlen
<= buf_size
9518 || runlen
+ elt_size
<= buf_size
)
9519 && r_off2
> (*ext_r_off
) (p
+ runlen
))
9523 buf
= bfd_malloc (buf_size
);
9527 if (runlen
< sortlen
)
9529 memcpy (buf
, p
, runlen
);
9530 memmove (loc
+ runlen
, loc
, sortlen
);
9531 memcpy (loc
, buf
, runlen
);
9535 memcpy (buf
, loc
, sortlen
);
9536 memmove (loc
, p
, runlen
);
9537 memcpy (loc
+ runlen
, buf
, sortlen
);
9539 p
+= runlen
- elt_size
;
9542 /* Hashes are no longer valid. */
9543 free (reldata
->hashes
);
9544 reldata
->hashes
= NULL
;
9550 struct elf_link_sort_rela
9556 enum elf_reloc_type_class type
;
9557 /* We use this as an array of size int_rels_per_ext_rel. */
9558 Elf_Internal_Rela rela
[1];
9561 /* qsort stability here and for cmp2 is only an issue if multiple
9562 dynamic relocations are emitted at the same address. But targets
9563 that apply a series of dynamic relocations each operating on the
9564 result of the prior relocation can't use -z combreloc as
9565 implemented anyway. Such schemes tend to be broken by sorting on
9566 symbol index. That leaves dynamic NONE relocs as the only other
9567 case where ld might emit multiple relocs at the same address, and
9568 those are only emitted due to target bugs. */
9571 elf_link_sort_cmp1 (const void *A
, const void *B
)
9573 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9574 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9575 int relativea
, relativeb
;
9577 relativea
= a
->type
== reloc_class_relative
;
9578 relativeb
= b
->type
== reloc_class_relative
;
9580 if (relativea
< relativeb
)
9582 if (relativea
> relativeb
)
9584 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9586 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9588 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9590 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9596 elf_link_sort_cmp2 (const void *A
, const void *B
)
9598 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9599 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9601 if (a
->type
< b
->type
)
9603 if (a
->type
> b
->type
)
9605 if (a
->u
.offset
< b
->u
.offset
)
9607 if (a
->u
.offset
> b
->u
.offset
)
9609 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9611 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9617 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9619 asection
*dynamic_relocs
;
9622 bfd_size_type count
, size
;
9623 size_t i
, ret
, sort_elt
, ext_size
;
9624 bfd_byte
*sort
, *s_non_relative
, *p
;
9625 struct elf_link_sort_rela
*sq
;
9626 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9627 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9628 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9629 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9630 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9631 struct bfd_link_order
*lo
;
9635 /* Find a dynamic reloc section. */
9636 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9637 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9638 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9639 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9641 bool use_rela_initialised
= false;
9643 /* This is just here to stop gcc from complaining.
9644 Its initialization checking code is not perfect. */
9647 /* Both sections are present. Examine the sizes
9648 of the indirect sections to help us choose. */
9649 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9650 if (lo
->type
== bfd_indirect_link_order
)
9652 asection
*o
= lo
->u
.indirect
.section
;
9654 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9656 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9657 /* Section size is divisible by both rel and rela sizes.
9658 It is of no help to us. */
9662 /* Section size is only divisible by rela. */
9663 if (use_rela_initialised
&& !use_rela
)
9665 _bfd_error_handler (_("%pB: unable to sort relocs - "
9666 "they are in more than one size"),
9668 bfd_set_error (bfd_error_invalid_operation
);
9674 use_rela_initialised
= true;
9678 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9680 /* Section size is only divisible by rel. */
9681 if (use_rela_initialised
&& use_rela
)
9683 _bfd_error_handler (_("%pB: unable to sort relocs - "
9684 "they are in more than one size"),
9686 bfd_set_error (bfd_error_invalid_operation
);
9692 use_rela_initialised
= true;
9697 /* The section size is not divisible by either -
9698 something is wrong. */
9699 _bfd_error_handler (_("%pB: unable to sort relocs - "
9700 "they are of an unknown size"), abfd
);
9701 bfd_set_error (bfd_error_invalid_operation
);
9706 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9707 if (lo
->type
== bfd_indirect_link_order
)
9709 asection
*o
= lo
->u
.indirect
.section
;
9711 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9713 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9714 /* Section size is divisible by both rel and rela sizes.
9715 It is of no help to us. */
9719 /* Section size is only divisible by rela. */
9720 if (use_rela_initialised
&& !use_rela
)
9722 _bfd_error_handler (_("%pB: unable to sort relocs - "
9723 "they are in more than one size"),
9725 bfd_set_error (bfd_error_invalid_operation
);
9731 use_rela_initialised
= true;
9735 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9737 /* Section size is only divisible by rel. */
9738 if (use_rela_initialised
&& use_rela
)
9740 _bfd_error_handler (_("%pB: unable to sort relocs - "
9741 "they are in more than one size"),
9743 bfd_set_error (bfd_error_invalid_operation
);
9749 use_rela_initialised
= true;
9754 /* The section size is not divisible by either -
9755 something is wrong. */
9756 _bfd_error_handler (_("%pB: unable to sort relocs - "
9757 "they are of an unknown size"), abfd
);
9758 bfd_set_error (bfd_error_invalid_operation
);
9763 if (! use_rela_initialised
)
9767 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9769 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9776 dynamic_relocs
= rela_dyn
;
9777 ext_size
= bed
->s
->sizeof_rela
;
9778 swap_in
= bed
->s
->swap_reloca_in
;
9779 swap_out
= bed
->s
->swap_reloca_out
;
9783 dynamic_relocs
= rel_dyn
;
9784 ext_size
= bed
->s
->sizeof_rel
;
9785 swap_in
= bed
->s
->swap_reloc_in
;
9786 swap_out
= bed
->s
->swap_reloc_out
;
9790 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9791 if (lo
->type
== bfd_indirect_link_order
)
9792 size
+= lo
->u
.indirect
.section
->size
;
9794 if (size
!= dynamic_relocs
->size
)
9797 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9798 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9800 count
= dynamic_relocs
->size
/ ext_size
;
9803 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9807 (*info
->callbacks
->warning
)
9808 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9812 if (bed
->s
->arch_size
== 32)
9813 r_sym_mask
= ~(bfd_vma
) 0xff;
9815 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9817 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9818 if (lo
->type
== bfd_indirect_link_order
)
9820 bfd_byte
*erel
, *erelend
;
9821 asection
*o
= lo
->u
.indirect
.section
;
9823 if (o
->contents
== NULL
&& o
->size
!= 0)
9825 /* This is a reloc section that is being handled as a normal
9826 section. See bfd_section_from_shdr. We can't combine
9827 relocs in this case. */
9832 erelend
= o
->contents
+ o
->size
;
9833 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9835 while (erel
< erelend
)
9837 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9839 (*swap_in
) (abfd
, erel
, s
->rela
);
9840 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9841 s
->u
.sym_mask
= r_sym_mask
;
9847 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9849 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9851 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9852 if (s
->type
!= reloc_class_relative
)
9858 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9859 for (; i
< count
; i
++, p
+= sort_elt
)
9861 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9862 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9864 sp
->u
.offset
= sq
->rela
->r_offset
;
9867 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9869 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9870 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9872 /* We have plt relocs in .rela.dyn. */
9873 sq
= (struct elf_link_sort_rela
*) sort
;
9874 for (i
= 0; i
< count
; i
++)
9875 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9877 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9879 struct bfd_link_order
**plo
;
9880 /* Put srelplt link_order last. This is so the output_offset
9881 set in the next loop is correct for DT_JMPREL. */
9882 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9883 if ((*plo
)->type
== bfd_indirect_link_order
9884 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9890 plo
= &(*plo
)->next
;
9893 dynamic_relocs
->map_tail
.link_order
= lo
;
9898 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9899 if (lo
->type
== bfd_indirect_link_order
)
9901 bfd_byte
*erel
, *erelend
;
9902 asection
*o
= lo
->u
.indirect
.section
;
9905 erelend
= o
->contents
+ o
->size
;
9906 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9907 while (erel
< erelend
)
9909 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9910 (*swap_out
) (abfd
, s
->rela
, erel
);
9917 *psec
= dynamic_relocs
;
9921 /* Add a symbol to the output symbol string table. */
9924 elf_link_output_symstrtab (void *finf
,
9926 Elf_Internal_Sym
*elfsym
,
9927 asection
*input_sec
,
9928 struct elf_link_hash_entry
*h
)
9930 struct elf_final_link_info
*flinfo
= finf
;
9931 int (*output_symbol_hook
)
9932 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9933 struct elf_link_hash_entry
*);
9934 struct elf_link_hash_table
*hash_table
;
9935 const struct elf_backend_data
*bed
;
9936 bfd_size_type strtabsize
;
9938 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9940 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9941 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9942 if (output_symbol_hook
!= NULL
)
9944 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9949 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
9950 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
9951 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
9952 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
9956 || (input_sec
->flags
& SEC_EXCLUDE
))
9957 elfsym
->st_name
= (unsigned long) -1;
9960 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9961 to get the final offset for st_name. */
9962 char *versioned_name
= (char *) name
;
9965 if (h
->versioned
== versioned
&& h
->def_dynamic
)
9967 /* Keep only one '@' for versioned symbols defined in
9969 char *version
= strrchr (name
, ELF_VER_CHR
);
9970 char *base_end
= strchr (name
, ELF_VER_CHR
);
9971 if (version
!= base_end
)
9974 size_t len
= strlen (name
);
9975 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
9976 if (versioned_name
== NULL
)
9978 base_len
= base_end
- name
;
9979 memcpy (versioned_name
, name
, base_len
);
9980 memcpy (versioned_name
+ base_len
, version
,
9985 else if (flinfo
->info
->unique_symbol
9986 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
9988 struct local_hash_entry
*lh
;
9992 switch (ELF_ST_TYPE (elfsym
->st_info
))
9998 lh
= (struct local_hash_entry
*) bfd_hash_lookup
9999 (&flinfo
->local_hash_table
, name
, true, false);
10002 /* Always append ".COUNT" to local symbols to avoid
10003 potential conflicts with local symbol "XXX.COUNT". */
10004 sprintf (buf
, "%lx", lh
->count
);
10005 base_len
= lh
->size
;
10008 base_len
= strlen (name
);
10009 lh
->size
= base_len
;
10011 count_len
= strlen (buf
);
10012 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
10013 base_len
+ count_len
+ 2);
10014 if (versioned_name
== NULL
)
10016 memcpy (versioned_name
, name
, base_len
);
10017 versioned_name
[base_len
] = '.';
10018 memcpy (versioned_name
+ base_len
+ 1, buf
,
10025 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
10026 versioned_name
, false);
10027 if (elfsym
->st_name
== (unsigned long) -1)
10031 hash_table
= elf_hash_table (flinfo
->info
);
10032 strtabsize
= hash_table
->strtabsize
;
10033 if (strtabsize
<= flinfo
->output_bfd
->symcount
)
10035 strtabsize
+= strtabsize
;
10036 hash_table
->strtabsize
= strtabsize
;
10037 strtabsize
*= sizeof (*hash_table
->strtab
);
10039 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
10041 if (hash_table
->strtab
== NULL
)
10044 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].sym
= *elfsym
;
10045 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].dest_index
10046 = flinfo
->output_bfd
->symcount
;
10047 flinfo
->output_bfd
->symcount
+= 1;
10052 /* Swap symbols out to the symbol table and flush the output symbols to
10056 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
10058 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
10061 const struct elf_backend_data
*bed
;
10063 Elf_Internal_Shdr
*hdr
;
10067 if (flinfo
->output_bfd
->symcount
== 0)
10070 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10072 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10074 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10075 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
10076 if (symbuf
== NULL
)
10079 if (flinfo
->symshndxbuf
)
10081 amt
= sizeof (Elf_External_Sym_Shndx
);
10082 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
10083 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
10084 if (flinfo
->symshndxbuf
== NULL
)
10091 /* Now swap out the symbols. */
10092 for (i
= 0; i
< flinfo
->output_bfd
->symcount
; i
++)
10094 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
10095 if (elfsym
->sym
.st_name
== (unsigned long) -1)
10096 elfsym
->sym
.st_name
= 0;
10098 elfsym
->sym
.st_name
10099 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
10100 elfsym
->sym
.st_name
);
10102 /* Inform the linker of the addition of this symbol. */
10104 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
10105 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
10108 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
10109 ((bfd_byte
*) symbuf
10110 + (elfsym
->dest_index
10111 * bed
->s
->sizeof_sym
)),
10112 NPTR_ADD (flinfo
->symshndxbuf
,
10113 elfsym
->dest_index
));
10116 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
10117 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
10118 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10119 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
10120 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
10122 hdr
->sh_size
+= amt
;
10130 free (hash_table
->strtab
);
10131 hash_table
->strtab
= NULL
;
10136 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10139 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
10141 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
10142 && sym
->st_shndx
< SHN_LORESERVE
)
10144 /* The gABI doesn't support dynamic symbols in output sections
10147 /* xgettext:c-format */
10148 (_("%pB: too many sections: %d (>= %d)"),
10149 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
10150 bfd_set_error (bfd_error_nonrepresentable_section
);
10156 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10157 allowing an unsatisfied unversioned symbol in the DSO to match a
10158 versioned symbol that would normally require an explicit version.
10159 We also handle the case that a DSO references a hidden symbol
10160 which may be satisfied by a versioned symbol in another DSO. */
10163 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
10164 const struct elf_backend_data
*bed
,
10165 struct elf_link_hash_entry
*h
)
10168 struct elf_link_loaded_list
*loaded
;
10170 if (!is_elf_hash_table (info
->hash
))
10173 /* Check indirect symbol. */
10174 while (h
->root
.type
== bfd_link_hash_indirect
)
10175 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10177 switch (h
->root
.type
)
10183 case bfd_link_hash_undefined
:
10184 case bfd_link_hash_undefweak
:
10185 abfd
= h
->root
.u
.undef
.abfd
;
10187 || (abfd
->flags
& DYNAMIC
) == 0
10188 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
10192 case bfd_link_hash_defined
:
10193 case bfd_link_hash_defweak
:
10194 abfd
= h
->root
.u
.def
.section
->owner
;
10197 case bfd_link_hash_common
:
10198 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10201 BFD_ASSERT (abfd
!= NULL
);
10203 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10205 loaded
= loaded
->next
)
10208 Elf_Internal_Shdr
*hdr
;
10210 size_t extsymcount
;
10212 Elf_Internal_Shdr
*versymhdr
;
10213 Elf_Internal_Sym
*isym
;
10214 Elf_Internal_Sym
*isymend
;
10215 Elf_Internal_Sym
*isymbuf
;
10216 Elf_External_Versym
*ever
;
10217 Elf_External_Versym
*extversym
;
10219 input
= loaded
->abfd
;
10221 /* We check each DSO for a possible hidden versioned definition. */
10223 || elf_dynversym (input
) == 0)
10226 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10228 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10229 if (elf_bad_symtab (input
))
10231 extsymcount
= symcount
;
10236 extsymcount
= symcount
- hdr
->sh_info
;
10237 extsymoff
= hdr
->sh_info
;
10240 if (extsymcount
== 0)
10243 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10245 if (isymbuf
== NULL
)
10248 /* Read in any version definitions. */
10249 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10250 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10251 || (extversym
= (Elf_External_Versym
*)
10252 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10253 versymhdr
->sh_size
)) == NULL
)
10259 ever
= extversym
+ extsymoff
;
10260 isymend
= isymbuf
+ extsymcount
;
10261 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10264 Elf_Internal_Versym iver
;
10265 unsigned short version_index
;
10267 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10268 || isym
->st_shndx
== SHN_UNDEF
)
10271 name
= bfd_elf_string_from_elf_section (input
,
10274 if (strcmp (name
, h
->root
.root
.string
) != 0)
10277 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10279 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10280 && !(h
->def_regular
10281 && h
->forced_local
))
10283 /* If we have a non-hidden versioned sym, then it should
10284 have provided a definition for the undefined sym unless
10285 it is defined in a non-shared object and forced local.
10290 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10291 if (version_index
== 1 || version_index
== 2)
10293 /* This is the base or first version. We can use it. */
10307 /* Convert ELF common symbol TYPE. */
10310 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10312 /* Commom symbol can only appear in relocatable link. */
10313 if (!bfd_link_relocatable (info
))
10315 switch (info
->elf_stt_common
)
10319 case elf_stt_common
:
10322 case no_elf_stt_common
:
10329 /* Add an external symbol to the symbol table. This is called from
10330 the hash table traversal routine. When generating a shared object,
10331 we go through the symbol table twice. The first time we output
10332 anything that might have been forced to local scope in a version
10333 script. The second time we output the symbols that are still
10337 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10339 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10340 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10341 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10343 Elf_Internal_Sym sym
;
10344 asection
*input_sec
;
10345 const struct elf_backend_data
*bed
;
10350 if (h
->root
.type
== bfd_link_hash_warning
)
10352 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10353 if (h
->root
.type
== bfd_link_hash_new
)
10357 /* Decide whether to output this symbol in this pass. */
10358 if (eoinfo
->localsyms
)
10360 if (!h
->forced_local
)
10365 if (h
->forced_local
)
10369 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10371 if (h
->root
.type
== bfd_link_hash_undefined
)
10373 /* If we have an undefined symbol reference here then it must have
10374 come from a shared library that is being linked in. (Undefined
10375 references in regular files have already been handled unless
10376 they are in unreferenced sections which are removed by garbage
10378 bool ignore_undef
= false;
10380 /* Some symbols may be special in that the fact that they're
10381 undefined can be safely ignored - let backend determine that. */
10382 if (bed
->elf_backend_ignore_undef_symbol
)
10383 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10385 /* If we are reporting errors for this situation then do so now. */
10387 && h
->ref_dynamic_nonweak
10388 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10389 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10390 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10392 flinfo
->info
->callbacks
->undefined_symbol
10393 (flinfo
->info
, h
->root
.root
.string
,
10394 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10395 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10396 && !flinfo
->info
->warn_unresolved_syms
);
10399 /* Strip a global symbol defined in a discarded section. */
10404 /* We should also warn if a forced local symbol is referenced from
10405 shared libraries. */
10406 if (bfd_link_executable (flinfo
->info
)
10411 && h
->ref_dynamic_nonweak
10412 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10416 struct elf_link_hash_entry
*hi
= h
;
10418 /* Check indirect symbol. */
10419 while (hi
->root
.type
== bfd_link_hash_indirect
)
10420 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10422 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10423 /* xgettext:c-format */
10424 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10425 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10426 /* xgettext:c-format */
10427 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10429 /* xgettext:c-format */
10430 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10431 def_bfd
= flinfo
->output_bfd
;
10432 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10433 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10434 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10435 h
->root
.root
.string
, def_bfd
);
10436 bfd_set_error (bfd_error_bad_value
);
10437 eoinfo
->failed
= true;
10441 /* We don't want to output symbols that have never been mentioned by
10442 a regular file, or that we have been told to strip. However, if
10443 h->indx is set to -2, the symbol is used by a reloc and we must
10448 else if ((h
->def_dynamic
10450 || h
->root
.type
== bfd_link_hash_new
)
10452 && !h
->ref_regular
)
10454 else if (flinfo
->info
->strip
== strip_all
)
10456 else if (flinfo
->info
->strip
== strip_some
10457 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10458 h
->root
.root
.string
, false, false) == NULL
)
10460 else if ((h
->root
.type
== bfd_link_hash_defined
10461 || h
->root
.type
== bfd_link_hash_defweak
)
10462 && ((flinfo
->info
->strip_discarded
10463 && discarded_section (h
->root
.u
.def
.section
))
10464 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10465 && h
->root
.u
.def
.section
->owner
!= NULL
10466 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10468 else if ((h
->root
.type
== bfd_link_hash_undefined
10469 || h
->root
.type
== bfd_link_hash_undefweak
)
10470 && h
->root
.u
.undef
.abfd
!= NULL
10471 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10476 /* If we're stripping it, and it's not a dynamic symbol, there's
10477 nothing else to do. However, if it is a forced local symbol or
10478 an ifunc symbol we need to give the backend finish_dynamic_symbol
10479 function a chance to make it dynamic. */
10481 && h
->dynindx
== -1
10482 && type
!= STT_GNU_IFUNC
10483 && !h
->forced_local
)
10487 sym
.st_size
= h
->size
;
10488 sym
.st_other
= h
->other
;
10489 switch (h
->root
.type
)
10492 case bfd_link_hash_new
:
10493 case bfd_link_hash_warning
:
10497 case bfd_link_hash_undefined
:
10498 case bfd_link_hash_undefweak
:
10499 input_sec
= bfd_und_section_ptr
;
10500 sym
.st_shndx
= SHN_UNDEF
;
10503 case bfd_link_hash_defined
:
10504 case bfd_link_hash_defweak
:
10506 input_sec
= h
->root
.u
.def
.section
;
10507 if (input_sec
->output_section
!= NULL
)
10510 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10511 input_sec
->output_section
);
10512 if (sym
.st_shndx
== SHN_BAD
)
10515 /* xgettext:c-format */
10516 (_("%pB: could not find output section %pA for input section %pA"),
10517 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10518 bfd_set_error (bfd_error_nonrepresentable_section
);
10519 eoinfo
->failed
= true;
10523 /* ELF symbols in relocatable files are section relative,
10524 but in nonrelocatable files they are virtual
10526 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10527 if (!bfd_link_relocatable (flinfo
->info
))
10529 sym
.st_value
+= input_sec
->output_section
->vma
;
10530 if (h
->type
== STT_TLS
)
10532 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10533 if (tls_sec
!= NULL
)
10534 sym
.st_value
-= tls_sec
->vma
;
10540 BFD_ASSERT (input_sec
->owner
== NULL
10541 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10542 sym
.st_shndx
= SHN_UNDEF
;
10543 input_sec
= bfd_und_section_ptr
;
10548 case bfd_link_hash_common
:
10549 input_sec
= h
->root
.u
.c
.p
->section
;
10550 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10551 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10554 case bfd_link_hash_indirect
:
10555 /* These symbols are created by symbol versioning. They point
10556 to the decorated version of the name. For example, if the
10557 symbol foo@@GNU_1.2 is the default, which should be used when
10558 foo is used with no version, then we add an indirect symbol
10559 foo which points to foo@@GNU_1.2. We ignore these symbols,
10560 since the indirected symbol is already in the hash table. */
10564 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10565 switch (h
->root
.type
)
10567 case bfd_link_hash_common
:
10568 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10570 case bfd_link_hash_defined
:
10571 case bfd_link_hash_defweak
:
10572 if (bed
->common_definition (&sym
))
10573 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10577 case bfd_link_hash_undefined
:
10578 case bfd_link_hash_undefweak
:
10584 if (h
->forced_local
)
10586 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10587 /* Turn off visibility on local symbol. */
10588 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10590 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10591 else if (h
->unique_global
&& h
->def_regular
)
10592 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10593 else if (h
->root
.type
== bfd_link_hash_undefweak
10594 || h
->root
.type
== bfd_link_hash_defweak
)
10595 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10597 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10598 sym
.st_target_internal
= h
->target_internal
;
10600 /* Give the processor backend a chance to tweak the symbol value,
10601 and also to finish up anything that needs to be done for this
10602 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10603 forced local syms when non-shared is due to a historical quirk.
10604 STT_GNU_IFUNC symbol must go through PLT. */
10605 if ((h
->type
== STT_GNU_IFUNC
10607 && !bfd_link_relocatable (flinfo
->info
))
10608 || ((h
->dynindx
!= -1
10609 || h
->forced_local
)
10610 && ((bfd_link_pic (flinfo
->info
)
10611 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10612 || h
->root
.type
!= bfd_link_hash_undefweak
))
10613 || !h
->forced_local
)
10614 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10616 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10617 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10619 eoinfo
->failed
= true;
10624 /* If we are marking the symbol as undefined, and there are no
10625 non-weak references to this symbol from a regular object, then
10626 mark the symbol as weak undefined; if there are non-weak
10627 references, mark the symbol as strong. We can't do this earlier,
10628 because it might not be marked as undefined until the
10629 finish_dynamic_symbol routine gets through with it. */
10630 if (sym
.st_shndx
== SHN_UNDEF
10632 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10633 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10636 type
= ELF_ST_TYPE (sym
.st_info
);
10638 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10639 if (type
== STT_GNU_IFUNC
)
10642 if (h
->ref_regular_nonweak
)
10643 bindtype
= STB_GLOBAL
;
10645 bindtype
= STB_WEAK
;
10646 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10649 /* If this is a symbol defined in a dynamic library, don't use the
10650 symbol size from the dynamic library. Relinking an executable
10651 against a new library may introduce gratuitous changes in the
10652 executable's symbols if we keep the size. */
10653 if (sym
.st_shndx
== SHN_UNDEF
10658 /* If a non-weak symbol with non-default visibility is not defined
10659 locally, it is a fatal error. */
10660 if (!bfd_link_relocatable (flinfo
->info
)
10661 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10662 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10663 && h
->root
.type
== bfd_link_hash_undefined
10664 && !h
->def_regular
)
10668 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10669 /* xgettext:c-format */
10670 msg
= _("%pB: protected symbol `%s' isn't defined");
10671 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10672 /* xgettext:c-format */
10673 msg
= _("%pB: internal symbol `%s' isn't defined");
10675 /* xgettext:c-format */
10676 msg
= _("%pB: hidden symbol `%s' isn't defined");
10677 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10678 bfd_set_error (bfd_error_bad_value
);
10679 eoinfo
->failed
= true;
10683 /* If this symbol should be put in the .dynsym section, then put it
10684 there now. We already know the symbol index. We also fill in
10685 the entry in the .hash section. */
10686 if (h
->dynindx
!= -1
10687 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10688 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10689 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10693 /* Since there is no version information in the dynamic string,
10694 if there is no version info in symbol version section, we will
10695 have a run-time problem if not linking executable, referenced
10696 by shared library, or not bound locally. */
10697 if (h
->verinfo
.verdef
== NULL
10698 && (!bfd_link_executable (flinfo
->info
)
10700 || !h
->def_regular
))
10702 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10704 if (p
&& p
[1] != '\0')
10707 /* xgettext:c-format */
10708 (_("%pB: no symbol version section for versioned symbol `%s'"),
10709 flinfo
->output_bfd
, h
->root
.root
.string
);
10710 eoinfo
->failed
= true;
10715 sym
.st_name
= h
->dynstr_index
;
10716 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10717 + h
->dynindx
* bed
->s
->sizeof_sym
);
10718 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10720 eoinfo
->failed
= true;
10724 /* Inform the linker of the addition of this symbol. */
10726 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
10727 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
10729 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10731 if (flinfo
->hash_sec
!= NULL
)
10733 size_t hash_entry_size
;
10734 bfd_byte
*bucketpos
;
10736 size_t bucketcount
;
10739 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10740 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10743 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10744 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10745 + (bucket
+ 2) * hash_entry_size
);
10746 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10747 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10749 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10750 ((bfd_byte
*) flinfo
->hash_sec
->contents
10751 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10754 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10756 Elf_Internal_Versym iversym
;
10757 Elf_External_Versym
*eversym
;
10759 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10761 if (h
->verinfo
.verdef
== NULL
10762 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10763 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10764 iversym
.vs_vers
= 1;
10766 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10770 if (h
->verinfo
.vertree
== NULL
)
10771 iversym
.vs_vers
= 1;
10773 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10774 if (flinfo
->info
->create_default_symver
)
10778 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10779 defined locally. */
10780 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10781 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10783 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10784 eversym
+= h
->dynindx
;
10785 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10789 /* If the symbol is undefined, and we didn't output it to .dynsym,
10790 strip it from .symtab too. Obviously we can't do this for
10791 relocatable output or when needed for --emit-relocs. */
10792 else if (input_sec
== bfd_und_section_ptr
10794 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10795 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10796 && !bfd_link_relocatable (flinfo
->info
))
10799 /* Also strip others that we couldn't earlier due to dynamic symbol
10803 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10806 /* Output a FILE symbol so that following locals are not associated
10807 with the wrong input file. We need one for forced local symbols
10808 if we've seen more than one FILE symbol or when we have exactly
10809 one FILE symbol but global symbols are present in a file other
10810 than the one with the FILE symbol. We also need one if linker
10811 defined symbols are present. In practice these conditions are
10812 always met, so just emit the FILE symbol unconditionally. */
10813 if (eoinfo
->localsyms
10814 && !eoinfo
->file_sym_done
10815 && eoinfo
->flinfo
->filesym_count
!= 0)
10817 Elf_Internal_Sym fsym
;
10819 memset (&fsym
, 0, sizeof (fsym
));
10820 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10821 fsym
.st_shndx
= SHN_ABS
;
10822 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10823 bfd_und_section_ptr
, NULL
))
10826 eoinfo
->file_sym_done
= true;
10829 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10830 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10834 eoinfo
->failed
= true;
10839 else if (h
->indx
== -2)
10845 /* Return TRUE if special handling is done for relocs in SEC against
10846 symbols defined in discarded sections. */
10849 elf_section_ignore_discarded_relocs (asection
*sec
)
10851 const struct elf_backend_data
*bed
;
10853 switch (sec
->sec_info_type
)
10855 case SEC_INFO_TYPE_STABS
:
10856 case SEC_INFO_TYPE_EH_FRAME
:
10857 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10863 bed
= get_elf_backend_data (sec
->owner
);
10864 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10865 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10871 /* Return a mask saying how ld should treat relocations in SEC against
10872 symbols defined in discarded sections. If this function returns
10873 COMPLAIN set, ld will issue a warning message. If this function
10874 returns PRETEND set, and the discarded section was link-once and the
10875 same size as the kept link-once section, ld will pretend that the
10876 symbol was actually defined in the kept section. Otherwise ld will
10877 zero the reloc (at least that is the intent, but some cooperation by
10878 the target dependent code is needed, particularly for REL targets). */
10881 _bfd_elf_default_action_discarded (asection
*sec
)
10883 if (sec
->flags
& SEC_DEBUGGING
)
10886 if (strcmp (".eh_frame", sec
->name
) == 0)
10889 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10892 return COMPLAIN
| PRETEND
;
10895 /* Find a match between a section and a member of a section group. */
10898 match_group_member (asection
*sec
, asection
*group
,
10899 struct bfd_link_info
*info
)
10901 asection
*first
= elf_next_in_group (group
);
10902 asection
*s
= first
;
10906 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10909 s
= elf_next_in_group (s
);
10917 /* Check if the kept section of a discarded section SEC can be used
10918 to replace it. Return the replacement if it is OK. Otherwise return
10922 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10926 kept
= sec
->kept_section
;
10929 if ((kept
->flags
& SEC_GROUP
) != 0)
10930 kept
= match_group_member (sec
, kept
, info
);
10933 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10934 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
10938 /* Get the real kept section. */
10940 for (next
= kept
->kept_section
;
10942 next
= next
->kept_section
)
10946 sec
->kept_section
= kept
;
10951 /* Link an input file into the linker output file. This function
10952 handles all the sections and relocations of the input file at once.
10953 This is so that we only have to read the local symbols once, and
10954 don't have to keep them in memory. */
10957 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10959 int (*relocate_section
)
10960 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10961 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10963 Elf_Internal_Shdr
*symtab_hdr
;
10964 size_t locsymcount
;
10966 Elf_Internal_Sym
*isymbuf
;
10967 Elf_Internal_Sym
*isym
;
10968 Elf_Internal_Sym
*isymend
;
10970 asection
**ppsection
;
10972 const struct elf_backend_data
*bed
;
10973 struct elf_link_hash_entry
**sym_hashes
;
10974 bfd_size_type address_size
;
10975 bfd_vma r_type_mask
;
10977 bool have_file_sym
= false;
10979 output_bfd
= flinfo
->output_bfd
;
10980 bed
= get_elf_backend_data (output_bfd
);
10981 relocate_section
= bed
->elf_backend_relocate_section
;
10983 /* If this is a dynamic object, we don't want to do anything here:
10984 we don't want the local symbols, and we don't want the section
10986 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10989 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10990 if (elf_bad_symtab (input_bfd
))
10992 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10997 locsymcount
= symtab_hdr
->sh_info
;
10998 extsymoff
= symtab_hdr
->sh_info
;
11001 /* Enable GNU OSABI features in the output BFD that are used in the input
11003 if (bed
->elf_osabi
== ELFOSABI_NONE
11004 || bed
->elf_osabi
== ELFOSABI_GNU
11005 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
11006 elf_tdata (output_bfd
)->has_gnu_osabi
11007 |= (elf_tdata (input_bfd
)->has_gnu_osabi
11008 & (bfd_link_relocatable (flinfo
->info
)
11009 ? -1 : ~elf_gnu_osabi_retain
));
11011 /* Read the local symbols. */
11012 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11013 if (isymbuf
== NULL
&& locsymcount
!= 0)
11015 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
11016 flinfo
->internal_syms
,
11017 flinfo
->external_syms
,
11018 flinfo
->locsym_shndx
);
11019 if (isymbuf
== NULL
)
11023 /* Find local symbol sections and adjust values of symbols in
11024 SEC_MERGE sections. Write out those local symbols we know are
11025 going into the output file. */
11026 isymend
= PTR_ADD (isymbuf
, locsymcount
);
11027 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
11029 isym
++, pindex
++, ppsection
++)
11033 Elf_Internal_Sym osym
;
11039 if (elf_bad_symtab (input_bfd
))
11041 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
11048 if (isym
->st_shndx
== SHN_UNDEF
)
11049 isec
= bfd_und_section_ptr
;
11050 else if (isym
->st_shndx
== SHN_ABS
)
11051 isec
= bfd_abs_section_ptr
;
11052 else if (isym
->st_shndx
== SHN_COMMON
)
11053 isec
= bfd_com_section_ptr
;
11056 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
11059 /* Don't attempt to output symbols with st_shnx in the
11060 reserved range other than SHN_ABS and SHN_COMMON. */
11061 isec
= bfd_und_section_ptr
;
11063 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
11064 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
11066 _bfd_merged_section_offset (output_bfd
, &isec
,
11067 elf_section_data (isec
)->sec_info
,
11073 /* Don't output the first, undefined, symbol. In fact, don't
11074 output any undefined local symbol. */
11075 if (isec
== bfd_und_section_ptr
)
11078 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
11080 /* We never output section symbols. Instead, we use the
11081 section symbol of the corresponding section in the output
11086 /* If we are stripping all symbols, we don't want to output this
11088 if (flinfo
->info
->strip
== strip_all
)
11091 /* If we are discarding all local symbols, we don't want to
11092 output this one. If we are generating a relocatable output
11093 file, then some of the local symbols may be required by
11094 relocs; we output them below as we discover that they are
11096 if (flinfo
->info
->discard
== discard_all
)
11099 /* If this symbol is defined in a section which we are
11100 discarding, we don't need to keep it. */
11101 if (isym
->st_shndx
!= SHN_UNDEF
11102 && isym
->st_shndx
< SHN_LORESERVE
11103 && isec
->output_section
== NULL
11104 && flinfo
->info
->non_contiguous_regions
11105 && flinfo
->info
->non_contiguous_regions_warnings
)
11107 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
11108 "discards section `%s' from '%s'\n"),
11109 isec
->name
, bfd_get_filename (isec
->owner
));
11113 if (isym
->st_shndx
!= SHN_UNDEF
11114 && isym
->st_shndx
< SHN_LORESERVE
11115 && bfd_section_removed_from_list (output_bfd
,
11116 isec
->output_section
))
11119 /* Get the name of the symbol. */
11120 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
11125 /* See if we are discarding symbols with this name. */
11126 if ((flinfo
->info
->strip
== strip_some
11127 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, false, false)
11129 || (((flinfo
->info
->discard
== discard_sec_merge
11130 && (isec
->flags
& SEC_MERGE
)
11131 && !bfd_link_relocatable (flinfo
->info
))
11132 || flinfo
->info
->discard
== discard_l
)
11133 && bfd_is_local_label_name (input_bfd
, name
)))
11136 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
11138 if (input_bfd
->lto_output
)
11139 /* -flto puts a temp file name here. This means builds
11140 are not reproducible. Discard the symbol. */
11142 have_file_sym
= true;
11143 flinfo
->filesym_count
+= 1;
11145 if (!have_file_sym
)
11147 /* In the absence of debug info, bfd_find_nearest_line uses
11148 FILE symbols to determine the source file for local
11149 function symbols. Provide a FILE symbol here if input
11150 files lack such, so that their symbols won't be
11151 associated with a previous input file. It's not the
11152 source file, but the best we can do. */
11153 const char *filename
;
11154 have_file_sym
= true;
11155 flinfo
->filesym_count
+= 1;
11156 memset (&osym
, 0, sizeof (osym
));
11157 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11158 osym
.st_shndx
= SHN_ABS
;
11159 if (input_bfd
->lto_output
)
11162 filename
= lbasename (bfd_get_filename (input_bfd
));
11163 if (!elf_link_output_symstrtab (flinfo
, filename
, &osym
,
11164 bfd_abs_section_ptr
, NULL
))
11170 /* Adjust the section index for the output file. */
11171 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11172 isec
->output_section
);
11173 if (osym
.st_shndx
== SHN_BAD
)
11176 /* ELF symbols in relocatable files are section relative, but
11177 in executable files they are virtual addresses. Note that
11178 this code assumes that all ELF sections have an associated
11179 BFD section with a reasonable value for output_offset; below
11180 we assume that they also have a reasonable value for
11181 output_section. Any special sections must be set up to meet
11182 these requirements. */
11183 osym
.st_value
+= isec
->output_offset
;
11184 if (!bfd_link_relocatable (flinfo
->info
))
11186 osym
.st_value
+= isec
->output_section
->vma
;
11187 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
11189 /* STT_TLS symbols are relative to PT_TLS segment base. */
11190 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
11191 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
11193 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
11198 indx
= bfd_get_symcount (output_bfd
);
11199 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11206 if (bed
->s
->arch_size
== 32)
11208 r_type_mask
= 0xff;
11214 r_type_mask
= 0xffffffff;
11219 /* Relocate the contents of each section. */
11220 sym_hashes
= elf_sym_hashes (input_bfd
);
11221 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11223 bfd_byte
*contents
;
11225 if (! o
->linker_mark
)
11227 /* This section was omitted from the link. */
11231 if (!flinfo
->info
->resolve_section_groups
11232 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11234 /* Deal with the group signature symbol. */
11235 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11236 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11237 asection
*osec
= o
->output_section
;
11239 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11240 if (symndx
>= locsymcount
11241 || (elf_bad_symtab (input_bfd
)
11242 && flinfo
->sections
[symndx
] == NULL
))
11244 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
11245 while (h
->root
.type
== bfd_link_hash_indirect
11246 || h
->root
.type
== bfd_link_hash_warning
)
11247 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11248 /* Arrange for symbol to be output. */
11250 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11252 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11254 /* We'll use the output section target_index. */
11255 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11256 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11260 if (flinfo
->indices
[symndx
] == -1)
11262 /* Otherwise output the local symbol now. */
11263 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11264 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11269 name
= bfd_elf_string_from_elf_section (input_bfd
,
11270 symtab_hdr
->sh_link
,
11275 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11277 if (sym
.st_shndx
== SHN_BAD
)
11280 sym
.st_value
+= o
->output_offset
;
11282 indx
= bfd_get_symcount (output_bfd
);
11283 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11288 flinfo
->indices
[symndx
] = indx
;
11292 elf_section_data (osec
)->this_hdr
.sh_info
11293 = flinfo
->indices
[symndx
];
11297 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11298 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11301 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11303 /* Section was created by _bfd_elf_link_create_dynamic_sections
11308 /* Get the contents of the section. They have been cached by a
11309 relaxation routine. Note that o is a section in an input
11310 file, so the contents field will not have been set by any of
11311 the routines which work on output files. */
11312 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11314 contents
= elf_section_data (o
)->this_hdr
.contents
;
11315 if (bed
->caches_rawsize
11317 && o
->rawsize
< o
->size
)
11319 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11320 contents
= flinfo
->contents
;
11325 contents
= flinfo
->contents
;
11326 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
11330 if ((o
->flags
& SEC_RELOC
) != 0)
11332 Elf_Internal_Rela
*internal_relocs
;
11333 Elf_Internal_Rela
*rel
, *relend
;
11334 int action_discarded
;
11337 /* Get the swapped relocs. */
11339 = _bfd_elf_link_info_read_relocs (input_bfd
, flinfo
->info
, o
,
11340 flinfo
->external_relocs
,
11341 flinfo
->internal_relocs
,
11343 if (internal_relocs
== NULL
11344 && o
->reloc_count
> 0)
11347 action_discarded
= -1;
11348 if (!elf_section_ignore_discarded_relocs (o
))
11349 action_discarded
= (*bed
->action_discarded
) (o
);
11351 /* Run through the relocs evaluating complex reloc symbols and
11352 looking for relocs against symbols from discarded sections
11353 or section symbols from removed link-once sections.
11354 Complain about relocs against discarded sections. Zero
11355 relocs against removed link-once sections. */
11357 rel
= internal_relocs
;
11358 relend
= rel
+ o
->reloc_count
;
11359 for ( ; rel
< relend
; rel
++)
11361 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11362 unsigned int s_type
;
11363 asection
**ps
, *sec
;
11364 struct elf_link_hash_entry
*h
= NULL
;
11365 const char *sym_name
;
11367 if (r_symndx
== STN_UNDEF
)
11370 if (r_symndx
>= locsymcount
11371 || (elf_bad_symtab (input_bfd
)
11372 && flinfo
->sections
[r_symndx
] == NULL
))
11374 h
= sym_hashes
[r_symndx
- extsymoff
];
11376 /* Badly formatted input files can contain relocs that
11377 reference non-existant symbols. Check here so that
11378 we do not seg fault. */
11382 /* xgettext:c-format */
11383 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11384 "that references a non-existent global symbol"),
11385 input_bfd
, (uint64_t) rel
->r_info
, o
);
11386 bfd_set_error (bfd_error_bad_value
);
11390 while (h
->root
.type
== bfd_link_hash_indirect
11391 || h
->root
.type
== bfd_link_hash_warning
)
11392 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11396 /* If a plugin symbol is referenced from a non-IR file,
11397 mark the symbol as undefined. Note that the
11398 linker may attach linker created dynamic sections
11399 to the plugin bfd. Symbols defined in linker
11400 created sections are not plugin symbols. */
11401 if ((h
->root
.non_ir_ref_regular
11402 || h
->root
.non_ir_ref_dynamic
)
11403 && (h
->root
.type
== bfd_link_hash_defined
11404 || h
->root
.type
== bfd_link_hash_defweak
)
11405 && (h
->root
.u
.def
.section
->flags
11406 & SEC_LINKER_CREATED
) == 0
11407 && h
->root
.u
.def
.section
->owner
!= NULL
11408 && (h
->root
.u
.def
.section
->owner
->flags
11409 & BFD_PLUGIN
) != 0)
11411 h
->root
.type
= bfd_link_hash_undefined
;
11412 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11416 if (h
->root
.type
== bfd_link_hash_defined
11417 || h
->root
.type
== bfd_link_hash_defweak
)
11418 ps
= &h
->root
.u
.def
.section
;
11420 sym_name
= h
->root
.root
.string
;
11424 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11426 s_type
= ELF_ST_TYPE (sym
->st_info
);
11427 ps
= &flinfo
->sections
[r_symndx
];
11428 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11432 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11433 && !bfd_link_relocatable (flinfo
->info
))
11436 bfd_vma dot
= (rel
->r_offset
11437 + o
->output_offset
+ o
->output_section
->vma
);
11439 printf ("Encountered a complex symbol!");
11440 printf (" (input_bfd %s, section %s, reloc %ld\n",
11441 bfd_get_filename (input_bfd
), o
->name
,
11442 (long) (rel
- internal_relocs
));
11443 printf (" symbol: idx %8.8lx, name %s\n",
11444 r_symndx
, sym_name
);
11445 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11446 (unsigned long) rel
->r_info
,
11447 (unsigned long) rel
->r_offset
);
11449 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11450 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11453 /* Symbol evaluated OK. Update to absolute value. */
11454 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11459 if (action_discarded
!= -1 && ps
!= NULL
)
11461 /* Complain if the definition comes from a
11462 discarded section. */
11463 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11465 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11466 if (action_discarded
& COMPLAIN
)
11467 (*flinfo
->info
->callbacks
->einfo
)
11468 /* xgettext:c-format */
11469 (_("%X`%s' referenced in section `%pA' of %pB: "
11470 "defined in discarded section `%pA' of %pB\n"),
11471 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11473 /* Try to do the best we can to support buggy old
11474 versions of gcc. Pretend that the symbol is
11475 really defined in the kept linkonce section.
11476 FIXME: This is quite broken. Modifying the
11477 symbol here means we will be changing all later
11478 uses of the symbol, not just in this section. */
11479 if (action_discarded
& PRETEND
)
11483 kept
= _bfd_elf_check_kept_section (sec
,
11495 /* Relocate the section by invoking a back end routine.
11497 The back end routine is responsible for adjusting the
11498 section contents as necessary, and (if using Rela relocs
11499 and generating a relocatable output file) adjusting the
11500 reloc addend as necessary.
11502 The back end routine does not have to worry about setting
11503 the reloc address or the reloc symbol index.
11505 The back end routine is given a pointer to the swapped in
11506 internal symbols, and can access the hash table entries
11507 for the external symbols via elf_sym_hashes (input_bfd).
11509 When generating relocatable output, the back end routine
11510 must handle STB_LOCAL/STT_SECTION symbols specially. The
11511 output symbol is going to be a section symbol
11512 corresponding to the output section, which will require
11513 the addend to be adjusted. */
11515 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11516 input_bfd
, o
, contents
,
11524 || bfd_link_relocatable (flinfo
->info
)
11525 || flinfo
->info
->emitrelocations
)
11527 Elf_Internal_Rela
*irela
;
11528 Elf_Internal_Rela
*irelaend
, *irelamid
;
11529 bfd_vma last_offset
;
11530 struct elf_link_hash_entry
**rel_hash
;
11531 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11532 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11533 unsigned int next_erel
;
11535 struct bfd_elf_section_data
*esdi
, *esdo
;
11537 esdi
= elf_section_data (o
);
11538 esdo
= elf_section_data (o
->output_section
);
11539 rela_normal
= false;
11541 /* Adjust the reloc addresses and symbol indices. */
11543 irela
= internal_relocs
;
11544 irelaend
= irela
+ o
->reloc_count
;
11545 rel_hash
= PTR_ADD (esdo
->rel
.hashes
, esdo
->rel
.count
);
11546 /* We start processing the REL relocs, if any. When we reach
11547 IRELAMID in the loop, we switch to the RELA relocs. */
11549 if (esdi
->rel
.hdr
!= NULL
)
11550 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11551 * bed
->s
->int_rels_per_ext_rel
);
11552 rel_hash_list
= rel_hash
;
11553 rela_hash_list
= NULL
;
11554 last_offset
= o
->output_offset
;
11555 if (!bfd_link_relocatable (flinfo
->info
))
11556 last_offset
+= o
->output_section
->vma
;
11557 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11559 unsigned long r_symndx
;
11561 Elf_Internal_Sym sym
;
11563 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11569 if (irela
== irelamid
)
11571 rel_hash
= PTR_ADD (esdo
->rela
.hashes
, esdo
->rela
.count
);
11572 rela_hash_list
= rel_hash
;
11573 rela_normal
= bed
->rela_normal
;
11576 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11579 if (irela
->r_offset
>= (bfd_vma
) -2)
11581 /* This is a reloc for a deleted entry or somesuch.
11582 Turn it into an R_*_NONE reloc, at the same
11583 offset as the last reloc. elf_eh_frame.c and
11584 bfd_elf_discard_info rely on reloc offsets
11586 irela
->r_offset
= last_offset
;
11588 irela
->r_addend
= 0;
11592 irela
->r_offset
+= o
->output_offset
;
11594 /* Relocs in an executable have to be virtual addresses. */
11595 if (!bfd_link_relocatable (flinfo
->info
))
11596 irela
->r_offset
+= o
->output_section
->vma
;
11598 last_offset
= irela
->r_offset
;
11600 r_symndx
= irela
->r_info
>> r_sym_shift
;
11601 if (r_symndx
== STN_UNDEF
)
11604 if (r_symndx
>= locsymcount
11605 || (elf_bad_symtab (input_bfd
)
11606 && flinfo
->sections
[r_symndx
] == NULL
))
11608 struct elf_link_hash_entry
*rh
;
11609 unsigned long indx
;
11611 /* This is a reloc against a global symbol. We
11612 have not yet output all the local symbols, so
11613 we do not know the symbol index of any global
11614 symbol. We set the rel_hash entry for this
11615 reloc to point to the global hash table entry
11616 for this symbol. The symbol index is then
11617 set at the end of bfd_elf_final_link. */
11618 indx
= r_symndx
- extsymoff
;
11619 rh
= elf_sym_hashes (input_bfd
)[indx
];
11620 while (rh
->root
.type
== bfd_link_hash_indirect
11621 || rh
->root
.type
== bfd_link_hash_warning
)
11622 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11624 /* Setting the index to -2 tells
11625 elf_link_output_extsym that this symbol is
11626 used by a reloc. */
11627 BFD_ASSERT (rh
->indx
< 0);
11634 /* This is a reloc against a local symbol. */
11637 sym
= isymbuf
[r_symndx
];
11638 sec
= flinfo
->sections
[r_symndx
];
11639 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11641 /* I suppose the backend ought to fill in the
11642 section of any STT_SECTION symbol against a
11643 processor specific section. */
11644 r_symndx
= STN_UNDEF
;
11645 if (bfd_is_abs_section (sec
))
11647 else if (sec
== NULL
|| sec
->owner
== NULL
)
11649 bfd_set_error (bfd_error_bad_value
);
11654 asection
*osec
= sec
->output_section
;
11656 /* If we have discarded a section, the output
11657 section will be the absolute section. In
11658 case of discarded SEC_MERGE sections, use
11659 the kept section. relocate_section should
11660 have already handled discarded linkonce
11662 if (bfd_is_abs_section (osec
)
11663 && sec
->kept_section
!= NULL
11664 && sec
->kept_section
->output_section
!= NULL
)
11666 osec
= sec
->kept_section
->output_section
;
11667 irela
->r_addend
-= osec
->vma
;
11670 if (!bfd_is_abs_section (osec
))
11672 r_symndx
= osec
->target_index
;
11673 if (r_symndx
== STN_UNDEF
)
11675 irela
->r_addend
+= osec
->vma
;
11676 osec
= _bfd_nearby_section (output_bfd
, osec
,
11678 irela
->r_addend
-= osec
->vma
;
11679 r_symndx
= osec
->target_index
;
11684 /* Adjust the addend according to where the
11685 section winds up in the output section. */
11687 irela
->r_addend
+= sec
->output_offset
;
11691 if (flinfo
->indices
[r_symndx
] == -1)
11693 unsigned long shlink
;
11698 if (flinfo
->info
->strip
== strip_all
)
11700 /* You can't do ld -r -s. */
11701 bfd_set_error (bfd_error_invalid_operation
);
11705 /* This symbol was skipped earlier, but
11706 since it is needed by a reloc, we
11707 must output it now. */
11708 shlink
= symtab_hdr
->sh_link
;
11709 name
= (bfd_elf_string_from_elf_section
11710 (input_bfd
, shlink
, sym
.st_name
));
11714 osec
= sec
->output_section
;
11716 _bfd_elf_section_from_bfd_section (output_bfd
,
11718 if (sym
.st_shndx
== SHN_BAD
)
11721 sym
.st_value
+= sec
->output_offset
;
11722 if (!bfd_link_relocatable (flinfo
->info
))
11724 sym
.st_value
+= osec
->vma
;
11725 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11727 struct elf_link_hash_table
*htab
11728 = elf_hash_table (flinfo
->info
);
11730 /* STT_TLS symbols are relative to PT_TLS
11732 if (htab
->tls_sec
!= NULL
)
11733 sym
.st_value
-= htab
->tls_sec
->vma
;
11736 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11741 indx
= bfd_get_symcount (output_bfd
);
11742 ret
= elf_link_output_symstrtab (flinfo
, name
,
11748 flinfo
->indices
[r_symndx
] = indx
;
11753 r_symndx
= flinfo
->indices
[r_symndx
];
11756 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11757 | (irela
->r_info
& r_type_mask
));
11760 /* Swap out the relocs. */
11761 input_rel_hdr
= esdi
->rel
.hdr
;
11762 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11764 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11769 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11770 * bed
->s
->int_rels_per_ext_rel
);
11771 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11774 input_rela_hdr
= esdi
->rela
.hdr
;
11775 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11777 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11786 /* Write out the modified section contents. */
11787 if (bed
->elf_backend_write_section
11788 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11791 /* Section written out. */
11793 else switch (o
->sec_info_type
)
11795 case SEC_INFO_TYPE_STABS
:
11796 if (! (_bfd_write_section_stabs
11798 &elf_hash_table (flinfo
->info
)->stab_info
,
11799 o
, &elf_section_data (o
)->sec_info
, contents
)))
11802 case SEC_INFO_TYPE_MERGE
:
11803 if (! _bfd_write_merged_section (output_bfd
, o
,
11804 elf_section_data (o
)->sec_info
))
11807 case SEC_INFO_TYPE_EH_FRAME
:
11809 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11814 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11816 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11824 if (! (o
->flags
& SEC_EXCLUDE
))
11826 file_ptr offset
= (file_ptr
) o
->output_offset
;
11827 bfd_size_type todo
= o
->size
;
11829 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11831 if ((o
->flags
& SEC_ELF_REVERSE_COPY
)
11832 && o
->size
> address_size
)
11834 /* Reverse-copy input section to output. */
11836 if ((o
->size
& (address_size
- 1)) != 0
11837 || (o
->reloc_count
!= 0
11838 && (o
->size
* bed
->s
->int_rels_per_ext_rel
11839 != o
->reloc_count
* address_size
)))
11842 /* xgettext:c-format */
11843 (_("error: %pB: size of section %pA is not "
11844 "multiple of address size"),
11846 bfd_set_error (bfd_error_bad_value
);
11852 todo
-= address_size
;
11853 if (! bfd_set_section_contents (output_bfd
,
11861 offset
+= address_size
;
11865 else if (! bfd_set_section_contents (output_bfd
,
11879 /* Generate a reloc when linking an ELF file. This is a reloc
11880 requested by the linker, and does not come from any input file. This
11881 is used to build constructor and destructor tables when linking
11885 elf_reloc_link_order (bfd
*output_bfd
,
11886 struct bfd_link_info
*info
,
11887 asection
*output_section
,
11888 struct bfd_link_order
*link_order
)
11890 reloc_howto_type
*howto
;
11894 struct bfd_elf_section_reloc_data
*reldata
;
11895 struct elf_link_hash_entry
**rel_hash_ptr
;
11896 Elf_Internal_Shdr
*rel_hdr
;
11897 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11898 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11901 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11903 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11906 bfd_set_error (bfd_error_bad_value
);
11910 addend
= link_order
->u
.reloc
.p
->addend
;
11913 reldata
= &esdo
->rel
;
11914 else if (esdo
->rela
.hdr
)
11915 reldata
= &esdo
->rela
;
11922 /* Figure out the symbol index. */
11923 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11924 if (link_order
->type
== bfd_section_reloc_link_order
)
11926 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11927 BFD_ASSERT (indx
!= 0);
11928 *rel_hash_ptr
= NULL
;
11932 struct elf_link_hash_entry
*h
;
11934 /* Treat a reloc against a defined symbol as though it were
11935 actually against the section. */
11936 h
= ((struct elf_link_hash_entry
*)
11937 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11938 link_order
->u
.reloc
.p
->u
.name
,
11939 false, false, true));
11941 && (h
->root
.type
== bfd_link_hash_defined
11942 || h
->root
.type
== bfd_link_hash_defweak
))
11946 section
= h
->root
.u
.def
.section
;
11947 indx
= section
->output_section
->target_index
;
11948 *rel_hash_ptr
= NULL
;
11949 /* It seems that we ought to add the symbol value to the
11950 addend here, but in practice it has already been added
11951 because it was passed to constructor_callback. */
11952 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11954 else if (h
!= NULL
)
11956 /* Setting the index to -2 tells elf_link_output_extsym that
11957 this symbol is used by a reloc. */
11964 (*info
->callbacks
->unattached_reloc
)
11965 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11970 /* If this is an inplace reloc, we must write the addend into the
11972 if (howto
->partial_inplace
&& addend
!= 0)
11974 bfd_size_type size
;
11975 bfd_reloc_status_type rstat
;
11978 const char *sym_name
;
11979 bfd_size_type octets
;
11981 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11982 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11983 if (buf
== NULL
&& size
!= 0)
11985 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11992 case bfd_reloc_outofrange
:
11995 case bfd_reloc_overflow
:
11996 if (link_order
->type
== bfd_section_reloc_link_order
)
11997 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
11999 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
12000 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
12001 howto
->name
, addend
, NULL
, NULL
,
12006 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
12008 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
12015 /* The address of a reloc is relative to the section in a
12016 relocatable file, and is a virtual address in an executable
12018 offset
= link_order
->offset
;
12019 if (! bfd_link_relocatable (info
))
12020 offset
+= output_section
->vma
;
12022 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
12024 irel
[i
].r_offset
= offset
;
12025 irel
[i
].r_info
= 0;
12026 irel
[i
].r_addend
= 0;
12028 if (bed
->s
->arch_size
== 32)
12029 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
12031 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
12033 rel_hdr
= reldata
->hdr
;
12034 erel
= rel_hdr
->contents
;
12035 if (rel_hdr
->sh_type
== SHT_REL
)
12037 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
12038 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
12042 irel
[0].r_addend
= addend
;
12043 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
12044 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
12052 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12053 Returns TRUE upon success, FALSE otherwise. */
12056 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
12060 const struct elf_backend_data
*bed
;
12062 enum bfd_architecture arch
;
12064 asymbol
**sympp
= NULL
;
12068 elf_symbol_type
*osymbuf
;
12071 implib_bfd
= info
->out_implib_bfd
;
12072 bed
= get_elf_backend_data (abfd
);
12074 if (!bfd_set_format (implib_bfd
, bfd_object
))
12077 /* Use flag from executable but make it a relocatable object. */
12078 flags
= bfd_get_file_flags (abfd
);
12079 flags
&= ~HAS_RELOC
;
12080 if (!bfd_set_start_address (implib_bfd
, 0)
12081 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
12084 /* Copy architecture of output file to import library file. */
12085 arch
= bfd_get_arch (abfd
);
12086 mach
= bfd_get_mach (abfd
);
12087 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
12088 && (abfd
->target_defaulted
12089 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
12092 /* Get symbol table size. */
12093 symsize
= bfd_get_symtab_upper_bound (abfd
);
12097 /* Read in the symbol table. */
12098 sympp
= (asymbol
**) bfd_malloc (symsize
);
12102 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
12106 /* Allow the BFD backend to copy any private header data it
12107 understands from the output BFD to the import library BFD. */
12108 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
12111 /* Filter symbols to appear in the import library. */
12112 if (bed
->elf_backend_filter_implib_symbols
)
12113 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
12116 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
12119 bfd_set_error (bfd_error_no_symbols
);
12120 _bfd_error_handler (_("%pB: no symbol found for import library"),
12126 /* Make symbols absolute. */
12127 amt
= symcount
* sizeof (*osymbuf
);
12128 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
12129 if (osymbuf
== NULL
)
12132 for (src_count
= 0; src_count
< symcount
; src_count
++)
12134 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
12135 sizeof (*osymbuf
));
12136 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
12137 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
12138 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
12139 osymbuf
[src_count
].internal_elf_sym
.st_value
=
12140 osymbuf
[src_count
].symbol
.value
;
12141 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
12144 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
12146 /* Allow the BFD backend to copy any private data it understands
12147 from the output BFD to the import library BFD. This is done last
12148 to permit the routine to look at the filtered symbol table. */
12149 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
12152 if (!bfd_close (implib_bfd
))
12163 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
12167 if (flinfo
->symstrtab
!= NULL
)
12168 _bfd_elf_strtab_free (flinfo
->symstrtab
);
12169 free (flinfo
->contents
);
12170 free (flinfo
->external_relocs
);
12171 free (flinfo
->internal_relocs
);
12172 free (flinfo
->external_syms
);
12173 free (flinfo
->locsym_shndx
);
12174 free (flinfo
->internal_syms
);
12175 free (flinfo
->indices
);
12176 free (flinfo
->sections
);
12177 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12178 free (flinfo
->symshndxbuf
);
12179 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12181 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12182 free (esdo
->rel
.hashes
);
12183 free (esdo
->rela
.hashes
);
12187 /* Do the final step of an ELF link. */
12190 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12195 struct elf_final_link_info flinfo
;
12197 struct bfd_link_order
*p
;
12199 bfd_size_type max_contents_size
;
12200 bfd_size_type max_external_reloc_size
;
12201 bfd_size_type max_internal_reloc_count
;
12202 bfd_size_type max_sym_count
;
12203 bfd_size_type max_sym_shndx_count
;
12204 Elf_Internal_Sym elfsym
;
12206 Elf_Internal_Shdr
*symtab_hdr
;
12207 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12208 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12209 struct elf_outext_info eoinfo
;
12211 size_t relativecount
;
12212 size_t relr_entsize
;
12213 asection
*reldyn
= 0;
12215 asection
*attr_section
= NULL
;
12216 bfd_vma attr_size
= 0;
12217 const char *std_attrs_section
;
12218 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12219 bool sections_removed
;
12222 if (!is_elf_hash_table (&htab
->root
))
12225 if (bfd_link_pic (info
))
12226 abfd
->flags
|= DYNAMIC
;
12228 dynamic
= htab
->dynamic_sections_created
;
12229 dynobj
= htab
->dynobj
;
12231 emit_relocs
= (bfd_link_relocatable (info
)
12232 || info
->emitrelocations
);
12234 memset (&flinfo
, 0, sizeof (flinfo
));
12235 flinfo
.info
= info
;
12236 flinfo
.output_bfd
= abfd
;
12237 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12238 if (flinfo
.symstrtab
== NULL
)
12243 flinfo
.hash_sec
= NULL
;
12244 flinfo
.symver_sec
= NULL
;
12248 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12249 /* Note that dynsym_sec can be NULL (on VMS). */
12250 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12251 /* Note that it is OK if symver_sec is NULL. */
12254 if (info
->unique_symbol
12255 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12256 local_hash_newfunc
,
12257 sizeof (struct local_hash_entry
)))
12260 /* The object attributes have been merged. Remove the input
12261 sections from the link, and set the contents of the output
12263 sections_removed
= false;
12264 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12265 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12267 bool remove_section
= false;
12269 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12270 || strcmp (o
->name
, ".gnu.attributes") == 0)
12272 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12274 asection
*input_section
;
12276 if (p
->type
!= bfd_indirect_link_order
)
12278 input_section
= p
->u
.indirect
.section
;
12279 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12280 elf_link_input_bfd ignores this section. */
12281 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12284 attr_size
= bfd_elf_obj_attr_size (abfd
);
12285 bfd_set_section_size (o
, attr_size
);
12286 /* Skip this section later on. */
12287 o
->map_head
.link_order
= NULL
;
12291 remove_section
= true;
12293 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12295 /* Remove empty group section from linker output. */
12296 remove_section
= true;
12298 if (remove_section
)
12300 o
->flags
|= SEC_EXCLUDE
;
12301 bfd_section_list_remove (abfd
, o
);
12302 abfd
->section_count
--;
12303 sections_removed
= true;
12306 if (sections_removed
)
12307 _bfd_fix_excluded_sec_syms (abfd
, info
);
12309 /* Count up the number of relocations we will output for each output
12310 section, so that we know the sizes of the reloc sections. We
12311 also figure out some maximum sizes. */
12312 max_contents_size
= 0;
12313 max_external_reloc_size
= 0;
12314 max_internal_reloc_count
= 0;
12316 max_sym_shndx_count
= 0;
12318 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12320 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12321 o
->reloc_count
= 0;
12323 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12325 unsigned int reloc_count
= 0;
12326 unsigned int additional_reloc_count
= 0;
12327 struct bfd_elf_section_data
*esdi
= NULL
;
12329 if (p
->type
== bfd_section_reloc_link_order
12330 || p
->type
== bfd_symbol_reloc_link_order
)
12332 else if (p
->type
== bfd_indirect_link_order
)
12336 sec
= p
->u
.indirect
.section
;
12338 /* Mark all sections which are to be included in the
12339 link. This will normally be every section. We need
12340 to do this so that we can identify any sections which
12341 the linker has decided to not include. */
12342 sec
->linker_mark
= true;
12344 if (sec
->flags
& SEC_MERGE
)
12347 if (sec
->rawsize
> max_contents_size
)
12348 max_contents_size
= sec
->rawsize
;
12349 if (sec
->size
> max_contents_size
)
12350 max_contents_size
= sec
->size
;
12352 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12353 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12357 /* We are interested in just local symbols, not all
12359 if (elf_bad_symtab (sec
->owner
))
12360 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12361 / bed
->s
->sizeof_sym
);
12363 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12365 if (sym_count
> max_sym_count
)
12366 max_sym_count
= sym_count
;
12368 if (sym_count
> max_sym_shndx_count
12369 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12370 max_sym_shndx_count
= sym_count
;
12372 if (esdo
->this_hdr
.sh_type
== SHT_REL
12373 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
12374 /* Some backends use reloc_count in relocation sections
12375 to count particular types of relocs. Of course,
12376 reloc sections themselves can't have relocations. */
12378 else if (emit_relocs
)
12380 reloc_count
= sec
->reloc_count
;
12381 if (bed
->elf_backend_count_additional_relocs
)
12384 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12385 additional_reloc_count
+= c
;
12388 else if (bed
->elf_backend_count_relocs
)
12389 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12391 esdi
= elf_section_data (sec
);
12393 if ((sec
->flags
& SEC_RELOC
) != 0)
12395 size_t ext_size
= 0;
12397 if (esdi
->rel
.hdr
!= NULL
)
12398 ext_size
= esdi
->rel
.hdr
->sh_size
;
12399 if (esdi
->rela
.hdr
!= NULL
)
12400 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12402 if (ext_size
> max_external_reloc_size
)
12403 max_external_reloc_size
= ext_size
;
12404 if (sec
->reloc_count
> max_internal_reloc_count
)
12405 max_internal_reloc_count
= sec
->reloc_count
;
12410 if (reloc_count
== 0)
12413 reloc_count
+= additional_reloc_count
;
12414 o
->reloc_count
+= reloc_count
;
12416 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12420 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12421 esdo
->rel
.count
+= additional_reloc_count
;
12423 if (esdi
->rela
.hdr
)
12425 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12426 esdo
->rela
.count
+= additional_reloc_count
;
12432 esdo
->rela
.count
+= reloc_count
;
12434 esdo
->rel
.count
+= reloc_count
;
12438 if (o
->reloc_count
> 0)
12439 o
->flags
|= SEC_RELOC
;
12442 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12443 set it (this is probably a bug) and if it is set
12444 assign_section_numbers will create a reloc section. */
12445 o
->flags
&=~ SEC_RELOC
;
12448 /* If the SEC_ALLOC flag is not set, force the section VMA to
12449 zero. This is done in elf_fake_sections as well, but forcing
12450 the VMA to 0 here will ensure that relocs against these
12451 sections are handled correctly. */
12452 if ((o
->flags
& SEC_ALLOC
) == 0
12453 && ! o
->user_set_vma
)
12457 if (! bfd_link_relocatable (info
) && merged
)
12458 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12460 /* Figure out the file positions for everything but the symbol table
12461 and the relocs. We set symcount to force assign_section_numbers
12462 to create a symbol table. */
12463 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12464 BFD_ASSERT (! abfd
->output_has_begun
);
12465 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12468 /* Set sizes, and assign file positions for reloc sections. */
12469 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12471 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12472 if ((o
->flags
& SEC_RELOC
) != 0)
12475 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12479 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12483 /* _bfd_elf_compute_section_file_positions makes temporary use
12484 of target_index. Reset it. */
12485 o
->target_index
= 0;
12487 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12488 to count upwards while actually outputting the relocations. */
12489 esdo
->rel
.count
= 0;
12490 esdo
->rela
.count
= 0;
12492 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12493 && !bfd_section_is_ctf (o
))
12495 /* Cache the section contents so that they can be compressed
12496 later. Use bfd_malloc since it will be freed by
12497 bfd_compress_section_contents. */
12498 unsigned char *contents
= esdo
->this_hdr
.contents
;
12499 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
12502 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12503 if (contents
== NULL
)
12505 esdo
->this_hdr
.contents
= contents
;
12509 /* We have now assigned file positions for all the sections except .symtab,
12510 .strtab, and non-loaded reloc and compressed debugging sections. We start
12511 the .symtab section at the current file position, and write directly to it.
12512 We build the .strtab section in memory. */
12513 abfd
->symcount
= 0;
12514 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12515 /* sh_name is set in prep_headers. */
12516 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12517 /* sh_flags, sh_addr and sh_size all start off zero. */
12518 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12519 /* sh_link is set in assign_section_numbers. */
12520 /* sh_info is set below. */
12521 /* sh_offset is set just below. */
12522 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12524 if (max_sym_count
< 20)
12525 max_sym_count
= 20;
12526 htab
->strtabsize
= max_sym_count
;
12527 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12528 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12529 if (htab
->strtab
== NULL
)
12531 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12533 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12534 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12536 if (info
->strip
!= strip_all
|| emit_relocs
)
12538 file_ptr off
= elf_next_file_pos (abfd
);
12540 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
12542 /* Note that at this point elf_next_file_pos (abfd) is
12543 incorrect. We do not yet know the size of the .symtab section.
12544 We correct next_file_pos below, after we do know the size. */
12546 /* Start writing out the symbol table. The first symbol is always a
12548 elfsym
.st_value
= 0;
12549 elfsym
.st_size
= 0;
12550 elfsym
.st_info
= 0;
12551 elfsym
.st_other
= 0;
12552 elfsym
.st_shndx
= SHN_UNDEF
;
12553 elfsym
.st_target_internal
= 0;
12554 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12555 bfd_und_section_ptr
, NULL
) != 1)
12558 /* Output a symbol for each section if asked or they are used for
12559 relocs. These symbols usually have no names. We store the
12560 index of each one in the index field of the section, so that
12561 we can find it again when outputting relocs. */
12563 if (bfd_keep_unused_section_symbols (abfd
) || emit_relocs
)
12565 bool name_local_sections
12566 = (bed
->elf_backend_name_local_section_symbols
12567 && bed
->elf_backend_name_local_section_symbols (abfd
));
12568 const char *name
= NULL
;
12570 elfsym
.st_size
= 0;
12571 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12572 elfsym
.st_other
= 0;
12573 elfsym
.st_value
= 0;
12574 elfsym
.st_target_internal
= 0;
12575 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12577 o
= bfd_section_from_elf_index (abfd
, i
);
12580 o
->target_index
= bfd_get_symcount (abfd
);
12581 elfsym
.st_shndx
= i
;
12582 if (!bfd_link_relocatable (info
))
12583 elfsym
.st_value
= o
->vma
;
12584 if (name_local_sections
)
12586 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12594 /* On some targets like Irix 5 the symbol split between local and global
12595 ones recorded in the sh_info field needs to be done between section
12596 and all other symbols. */
12597 if (bed
->elf_backend_elfsym_local_is_section
12598 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12599 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12601 /* Allocate some memory to hold information read in from the input
12603 if (max_contents_size
!= 0)
12605 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12606 if (flinfo
.contents
== NULL
)
12610 if (max_external_reloc_size
!= 0)
12612 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12613 if (flinfo
.external_relocs
== NULL
)
12617 if (max_internal_reloc_count
!= 0)
12619 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12620 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12621 if (flinfo
.internal_relocs
== NULL
)
12625 if (max_sym_count
!= 0)
12627 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12628 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12629 if (flinfo
.external_syms
== NULL
)
12632 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12633 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12634 if (flinfo
.internal_syms
== NULL
)
12637 amt
= max_sym_count
* sizeof (long);
12638 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12639 if (flinfo
.indices
== NULL
)
12642 amt
= max_sym_count
* sizeof (asection
*);
12643 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12644 if (flinfo
.sections
== NULL
)
12648 if (max_sym_shndx_count
!= 0)
12650 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12651 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12652 if (flinfo
.locsym_shndx
== NULL
)
12658 bfd_vma base
, end
= 0; /* Both bytes. */
12661 for (sec
= htab
->tls_sec
;
12662 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12665 bfd_size_type size
= sec
->size
;
12666 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12669 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12671 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12674 size
= ord
->offset
* opb
+ ord
->size
;
12676 end
= sec
->vma
+ size
/ opb
;
12678 base
= htab
->tls_sec
->vma
;
12679 /* Only align end of TLS section if static TLS doesn't have special
12680 alignment requirements. */
12681 if (bed
->static_tls_alignment
== 1)
12682 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12683 htab
->tls_size
= end
- base
;
12686 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12689 /* Finish relative relocations here after regular symbol processing
12690 is finished if DT_RELR is enabled. */
12691 if (info
->enable_dt_relr
12692 && bed
->finish_relative_relocs
12693 && !bed
->finish_relative_relocs (info
))
12694 info
->callbacks
->einfo
12695 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd
);
12697 /* Since ELF permits relocations to be against local symbols, we
12698 must have the local symbols available when we do the relocations.
12699 Since we would rather only read the local symbols once, and we
12700 would rather not keep them in memory, we handle all the
12701 relocations for a single input file at the same time.
12703 Unfortunately, there is no way to know the total number of local
12704 symbols until we have seen all of them, and the local symbol
12705 indices precede the global symbol indices. This means that when
12706 we are generating relocatable output, and we see a reloc against
12707 a global symbol, we can not know the symbol index until we have
12708 finished examining all the local symbols to see which ones we are
12709 going to output. To deal with this, we keep the relocations in
12710 memory, and don't output them until the end of the link. This is
12711 an unfortunate waste of memory, but I don't see a good way around
12712 it. Fortunately, it only happens when performing a relocatable
12713 link, which is not the common case. FIXME: If keep_memory is set
12714 we could write the relocs out and then read them again; I don't
12715 know how bad the memory loss will be. */
12717 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12718 sub
->output_has_begun
= false;
12719 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12721 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12723 if (p
->type
== bfd_indirect_link_order
12724 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12725 == bfd_target_elf_flavour
)
12726 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12728 if (! sub
->output_has_begun
)
12730 if (! elf_link_input_bfd (&flinfo
, sub
))
12732 sub
->output_has_begun
= true;
12735 else if (p
->type
== bfd_section_reloc_link_order
12736 || p
->type
== bfd_symbol_reloc_link_order
)
12738 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12743 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12745 if (p
->type
== bfd_indirect_link_order
12746 && (bfd_get_flavour (sub
)
12747 == bfd_target_elf_flavour
)
12748 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12749 != bed
->s
->elfclass
))
12751 const char *iclass
, *oclass
;
12753 switch (bed
->s
->elfclass
)
12755 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12756 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12757 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12761 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12763 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12764 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12765 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12769 bfd_set_error (bfd_error_wrong_format
);
12771 /* xgettext:c-format */
12772 (_("%pB: file class %s incompatible with %s"),
12773 sub
, iclass
, oclass
);
12782 /* Free symbol buffer if needed. */
12783 if (!info
->reduce_memory_overheads
)
12785 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12786 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
12788 free (elf_tdata (sub
)->symbuf
);
12789 elf_tdata (sub
)->symbuf
= NULL
;
12795 /* Output any global symbols that got converted to local in a
12796 version script or due to symbol visibility. We do this in a
12797 separate step since ELF requires all local symbols to appear
12798 prior to any global symbols. FIXME: We should only do this if
12799 some global symbols were, in fact, converted to become local.
12800 FIXME: Will this work correctly with the Irix 5 linker? */
12801 eoinfo
.failed
= false;
12802 eoinfo
.flinfo
= &flinfo
;
12803 eoinfo
.localsyms
= true;
12804 eoinfo
.file_sym_done
= false;
12805 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12809 goto return_local_hash_table
;
12812 /* If backend needs to output some local symbols not present in the hash
12813 table, do it now. */
12814 if (bed
->elf_backend_output_arch_local_syms
12815 && (info
->strip
!= strip_all
|| emit_relocs
))
12817 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12818 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
12821 goto return_local_hash_table
;
12825 /* That wrote out all the local symbols. Finish up the symbol table
12826 with the global symbols. Even if we want to strip everything we
12827 can, we still need to deal with those global symbols that got
12828 converted to local in a version script. */
12830 /* The sh_info field records the index of the first non local symbol. */
12831 if (!symtab_hdr
->sh_info
)
12832 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12835 && htab
->dynsym
!= NULL
12836 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12838 Elf_Internal_Sym sym
;
12839 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12841 o
= htab
->dynsym
->output_section
;
12842 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12844 /* Write out the section symbols for the output sections. */
12845 if (bfd_link_pic (info
)
12846 || htab
->is_relocatable_executable
)
12852 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12854 sym
.st_target_internal
= 0;
12856 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12862 dynindx
= elf_section_data (s
)->dynindx
;
12865 indx
= elf_section_data (s
)->this_idx
;
12866 BFD_ASSERT (indx
> 0);
12867 sym
.st_shndx
= indx
;
12868 if (! check_dynsym (abfd
, &sym
))
12871 goto return_local_hash_table
;
12873 sym
.st_value
= s
->vma
;
12874 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12876 /* Inform the linker of the addition of this symbol. */
12878 if (info
->callbacks
->ctf_new_dynsym
)
12879 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
12881 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12885 /* Write out the local dynsyms. */
12886 if (htab
->dynlocal
)
12888 struct elf_link_local_dynamic_entry
*e
;
12889 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12894 /* Copy the internal symbol and turn off visibility.
12895 Note that we saved a word of storage and overwrote
12896 the original st_name with the dynstr_index. */
12898 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12899 sym
.st_shndx
= SHN_UNDEF
;
12901 s
= bfd_section_from_elf_index (e
->input_bfd
,
12904 && s
->output_section
!= NULL
12905 && elf_section_data (s
->output_section
) != NULL
)
12908 elf_section_data (s
->output_section
)->this_idx
;
12909 if (! check_dynsym (abfd
, &sym
))
12912 goto return_local_hash_table
;
12914 sym
.st_value
= (s
->output_section
->vma
12916 + e
->isym
.st_value
);
12919 /* Inform the linker of the addition of this symbol. */
12921 if (info
->callbacks
->ctf_new_dynsym
)
12922 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
12924 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12925 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12930 /* We get the global symbols from the hash table. */
12931 eoinfo
.failed
= false;
12932 eoinfo
.localsyms
= false;
12933 eoinfo
.flinfo
= &flinfo
;
12934 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12938 goto return_local_hash_table
;
12941 /* If backend needs to output some symbols not present in the hash
12942 table, do it now. */
12943 if (bed
->elf_backend_output_arch_syms
12944 && (info
->strip
!= strip_all
|| emit_relocs
))
12946 if (! ((*bed
->elf_backend_output_arch_syms
)
12947 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
12950 goto return_local_hash_table
;
12954 /* Finalize the .strtab section. */
12955 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12957 /* Swap out the .strtab section. */
12958 if (!elf_link_swap_symbols_out (&flinfo
))
12961 goto return_local_hash_table
;
12964 /* Now we know the size of the symtab section. */
12965 if (bfd_get_symcount (abfd
) > 0)
12967 /* Finish up and write out the symbol string table (.strtab)
12969 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12970 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12972 if (elf_symtab_shndx_list (abfd
))
12974 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12976 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12978 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12979 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12980 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12981 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12982 symtab_shndx_hdr
->sh_size
= amt
;
12984 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12987 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12988 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12991 goto return_local_hash_table
;
12996 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12997 /* sh_name was set in prep_headers. */
12998 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12999 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
13000 symstrtab_hdr
->sh_addr
= 0;
13001 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
13002 symstrtab_hdr
->sh_entsize
= 0;
13003 symstrtab_hdr
->sh_link
= 0;
13004 symstrtab_hdr
->sh_info
= 0;
13005 /* sh_offset is set just below. */
13006 symstrtab_hdr
->sh_addralign
= 1;
13008 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
13010 elf_next_file_pos (abfd
) = off
;
13012 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
13013 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
13016 goto return_local_hash_table
;
13020 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
13022 _bfd_error_handler (_("%pB: failed to generate import library"),
13023 info
->out_implib_bfd
);
13025 goto return_local_hash_table
;
13028 /* Adjust the relocs to have the correct symbol indices. */
13029 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
13031 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
13034 if ((o
->flags
& SEC_RELOC
) == 0)
13037 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
13038 if (esdo
->rel
.hdr
!= NULL
13039 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
13042 goto return_local_hash_table
;
13044 if (esdo
->rela
.hdr
!= NULL
13045 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
13048 goto return_local_hash_table
;
13051 /* Set the reloc_count field to 0 to prevent write_relocs from
13052 trying to swap the relocs out itself. */
13053 o
->reloc_count
= 0;
13057 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
13058 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
13061 if (htab
->srelrdyn
!= NULL
13062 && htab
->srelrdyn
->output_section
!= NULL
13063 && htab
->srelrdyn
->size
!= 0)
13065 asection
*s
= htab
->srelrdyn
->output_section
;
13066 relr_entsize
= elf_section_data (s
)->this_hdr
.sh_entsize
;
13067 if (relr_entsize
== 0)
13069 relr_entsize
= bed
->s
->arch_size
/ 8;
13070 elf_section_data (s
)->this_hdr
.sh_entsize
= relr_entsize
;
13074 /* If we are linking against a dynamic object, or generating a
13075 shared library, finish up the dynamic linking information. */
13078 bfd_byte
*dyncon
, *dynconend
;
13080 /* Fix up .dynamic entries. */
13081 o
= bfd_get_linker_section (dynobj
, ".dynamic");
13082 BFD_ASSERT (o
!= NULL
);
13084 dyncon
= o
->contents
;
13085 dynconend
= PTR_ADD (o
->contents
, o
->size
);
13086 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13088 Elf_Internal_Dyn dyn
;
13091 bfd_size_type sh_size
;
13094 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13101 if (relativecount
!= 0)
13103 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
13105 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
13106 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
13108 if (dyn
.d_tag
!= DT_NULL
13109 && dynconend
- dyncon
>= bed
->s
->sizeof_dyn
)
13111 dyn
.d_un
.d_val
= relativecount
;
13117 if (relr_entsize
!= 0)
13119 if (dynconend
- dyncon
>= 3 * bed
->s
->sizeof_dyn
)
13121 asection
*s
= htab
->srelrdyn
;
13122 dyn
.d_tag
= DT_RELR
;
13124 = s
->output_section
->vma
+ s
->output_offset
;
13125 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13126 dyncon
+= bed
->s
->sizeof_dyn
;
13128 dyn
.d_tag
= DT_RELRSZ
;
13129 dyn
.d_un
.d_val
= s
->size
;
13130 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13131 dyncon
+= bed
->s
->sizeof_dyn
;
13133 dyn
.d_tag
= DT_RELRENT
;
13134 dyn
.d_un
.d_val
= relr_entsize
;
13143 name
= info
->init_function
;
13146 name
= info
->fini_function
;
13149 struct elf_link_hash_entry
*h
;
13151 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
13153 && (h
->root
.type
== bfd_link_hash_defined
13154 || h
->root
.type
== bfd_link_hash_defweak
))
13156 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
13157 o
= h
->root
.u
.def
.section
;
13158 if (o
->output_section
!= NULL
)
13159 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
13160 + o
->output_offset
);
13163 /* The symbol is imported from another shared
13164 library and does not apply to this one. */
13165 dyn
.d_un
.d_ptr
= 0;
13172 case DT_PREINIT_ARRAYSZ
:
13173 name
= ".preinit_array";
13175 case DT_INIT_ARRAYSZ
:
13176 name
= ".init_array";
13178 case DT_FINI_ARRAYSZ
:
13179 name
= ".fini_array";
13181 o
= bfd_get_section_by_name (abfd
, name
);
13185 (_("could not find section %s"), name
);
13190 (_("warning: %s section has zero size"), name
);
13191 dyn
.d_un
.d_val
= o
->size
;
13194 case DT_PREINIT_ARRAY
:
13195 name
= ".preinit_array";
13197 case DT_INIT_ARRAY
:
13198 name
= ".init_array";
13200 case DT_FINI_ARRAY
:
13201 name
= ".fini_array";
13203 o
= bfd_get_section_by_name (abfd
, name
);
13210 name
= ".gnu.hash";
13219 name
= ".gnu.version_d";
13222 name
= ".gnu.version_r";
13225 name
= ".gnu.version";
13227 o
= bfd_get_linker_section (dynobj
, name
);
13229 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13232 (_("could not find section %s"), name
);
13235 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13238 (_("warning: section '%s' is being made into a note"), name
);
13239 bfd_set_error (bfd_error_nonrepresentable_section
);
13242 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13249 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13255 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13257 Elf_Internal_Shdr
*hdr
;
13259 hdr
= elf_elfsections (abfd
)[i
];
13260 if (hdr
->sh_type
== type
13261 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13263 sh_size
+= hdr
->sh_size
;
13265 || sh_addr
> hdr
->sh_addr
)
13266 sh_addr
= hdr
->sh_addr
;
13270 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13272 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13274 /* Don't count procedure linkage table relocs in the
13275 overall reloc count. */
13276 sh_size
-= htab
->srelplt
->size
;
13278 /* If the size is zero, make the address zero too.
13279 This is to avoid a glibc bug. If the backend
13280 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13281 zero, then we'll put DT_RELA at the end of
13282 DT_JMPREL. glibc will interpret the end of
13283 DT_RELA matching the end of DT_JMPREL as the
13284 case where DT_RELA includes DT_JMPREL, and for
13285 LD_BIND_NOW will decide that processing DT_RELA
13286 will process the PLT relocs too. Net result:
13287 No PLT relocs applied. */
13290 /* If .rela.plt is the first .rela section, exclude
13291 it from DT_RELA. */
13292 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13293 + htab
->srelplt
->output_offset
) * opb
)
13294 sh_addr
+= htab
->srelplt
->size
;
13297 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13298 dyn
.d_un
.d_val
= sh_size
;
13300 dyn
.d_un
.d_ptr
= sh_addr
;
13303 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13307 /* If we have created any dynamic sections, then output them. */
13308 if (dynobj
!= NULL
)
13310 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13313 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13314 if (bfd_link_textrel_check (info
)
13315 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
13318 bfd_byte
*dyncon
, *dynconend
;
13320 dyncon
= o
->contents
;
13321 dynconend
= o
->contents
+ o
->size
;
13322 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13324 Elf_Internal_Dyn dyn
;
13326 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13328 if (dyn
.d_tag
== DT_TEXTREL
)
13330 if (info
->textrel_check
== textrel_check_error
)
13331 info
->callbacks
->einfo
13332 (_("%P%X: read-only segment has dynamic relocations\n"));
13333 else if (bfd_link_dll (info
))
13334 info
->callbacks
->einfo
13335 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13336 else if (bfd_link_pde (info
))
13337 info
->callbacks
->einfo
13338 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13340 info
->callbacks
->einfo
13341 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13347 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13349 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13351 || o
->output_section
== bfd_abs_section_ptr
)
13353 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13355 /* At this point, we are only interested in sections
13356 created by _bfd_elf_link_create_dynamic_sections. */
13359 if (htab
->stab_info
.stabstr
== o
)
13361 if (htab
->eh_info
.hdr_sec
== o
)
13363 if (strcmp (o
->name
, ".dynstr") != 0)
13365 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13366 * bfd_octets_per_byte (abfd
, o
));
13367 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13368 o
->contents
, octets
, o
->size
))
13373 /* The contents of the .dynstr section are actually in a
13377 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13378 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13379 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13385 if (!info
->resolve_section_groups
)
13387 bool failed
= false;
13389 BFD_ASSERT (bfd_link_relocatable (info
));
13390 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13395 /* If we have optimized stabs strings, output them. */
13396 if (htab
->stab_info
.stabstr
!= NULL
)
13398 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13402 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13405 if (info
->callbacks
->emit_ctf
)
13406 info
->callbacks
->emit_ctf ();
13408 elf_final_link_free (abfd
, &flinfo
);
13412 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13413 if (contents
== NULL
)
13415 /* Bail out and fail. */
13417 goto return_local_hash_table
;
13419 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13420 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13424 return_local_hash_table
:
13425 if (info
->unique_symbol
)
13426 bfd_hash_table_free (&flinfo
.local_hash_table
);
13430 elf_final_link_free (abfd
, &flinfo
);
13432 goto return_local_hash_table
;
13435 /* Initialize COOKIE for input bfd ABFD. */
13438 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13439 struct bfd_link_info
*info
, bfd
*abfd
)
13441 Elf_Internal_Shdr
*symtab_hdr
;
13442 const struct elf_backend_data
*bed
;
13444 bed
= get_elf_backend_data (abfd
);
13445 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13447 cookie
->abfd
= abfd
;
13448 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13449 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13450 if (cookie
->bad_symtab
)
13452 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13453 cookie
->extsymoff
= 0;
13457 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13458 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13461 if (bed
->s
->arch_size
== 32)
13462 cookie
->r_sym_shift
= 8;
13464 cookie
->r_sym_shift
= 32;
13466 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13467 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13469 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13470 cookie
->locsymcount
, 0,
13472 if (cookie
->locsyms
== NULL
)
13474 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13477 if (_bfd_link_keep_memory (info
) )
13479 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13480 info
->cache_size
+= (cookie
->locsymcount
13481 * sizeof (Elf_External_Sym_Shndx
));
13487 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13490 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13492 Elf_Internal_Shdr
*symtab_hdr
;
13494 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13495 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13496 free (cookie
->locsyms
);
13499 /* Initialize the relocation information in COOKIE for input section SEC
13500 of input bfd ABFD. */
13503 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13504 struct bfd_link_info
*info
, bfd
*abfd
,
13507 if (sec
->reloc_count
== 0)
13509 cookie
->rels
= NULL
;
13510 cookie
->relend
= NULL
;
13514 cookie
->rels
= _bfd_elf_link_info_read_relocs (abfd
, info
, sec
,
13516 _bfd_link_keep_memory (info
));
13517 if (cookie
->rels
== NULL
)
13519 cookie
->rel
= cookie
->rels
;
13520 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13522 cookie
->rel
= cookie
->rels
;
13526 /* Free the memory allocated by init_reloc_cookie_rels,
13530 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13533 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13534 free (cookie
->rels
);
13537 /* Initialize the whole of COOKIE for input section SEC. */
13540 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13541 struct bfd_link_info
*info
,
13544 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13546 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13551 fini_reloc_cookie (cookie
, sec
->owner
);
13556 /* Free the memory allocated by init_reloc_cookie_for_section,
13560 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13563 fini_reloc_cookie_rels (cookie
, sec
);
13564 fini_reloc_cookie (cookie
, sec
->owner
);
13567 /* Garbage collect unused sections. */
13569 /* Default gc_mark_hook. */
13572 _bfd_elf_gc_mark_hook (asection
*sec
,
13573 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13574 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13575 struct elf_link_hash_entry
*h
,
13576 Elf_Internal_Sym
*sym
)
13580 switch (h
->root
.type
)
13582 case bfd_link_hash_defined
:
13583 case bfd_link_hash_defweak
:
13584 return h
->root
.u
.def
.section
;
13586 case bfd_link_hash_common
:
13587 return h
->root
.u
.c
.p
->section
;
13594 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13599 /* Return the debug definition section. */
13602 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13603 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13604 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13605 struct elf_link_hash_entry
*h
,
13606 Elf_Internal_Sym
*sym
)
13610 /* Return the global debug definition section. */
13611 if ((h
->root
.type
== bfd_link_hash_defined
13612 || h
->root
.type
== bfd_link_hash_defweak
)
13613 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13614 return h
->root
.u
.def
.section
;
13618 /* Return the local debug definition section. */
13619 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13621 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
13628 /* COOKIE->rel describes a relocation against section SEC, which is
13629 a section we've decided to keep. Return the section that contains
13630 the relocation symbol, or NULL if no section contains it. */
13633 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13634 elf_gc_mark_hook_fn gc_mark_hook
,
13635 struct elf_reloc_cookie
*cookie
,
13638 unsigned long r_symndx
;
13639 struct elf_link_hash_entry
*h
, *hw
;
13641 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13642 if (r_symndx
== STN_UNDEF
)
13645 if (r_symndx
>= cookie
->locsymcount
13646 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13650 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13653 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13657 while (h
->root
.type
== bfd_link_hash_indirect
13658 || h
->root
.type
== bfd_link_hash_warning
)
13659 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13661 was_marked
= h
->mark
;
13663 /* Keep all aliases of the symbol too. If an object symbol
13664 needs to be copied into .dynbss then all of its aliases
13665 should be present as dynamic symbols, not just the one used
13666 on the copy relocation. */
13668 while (hw
->is_weakalias
)
13674 if (!was_marked
&& h
->start_stop
&& !h
->root
.ldscript_def
)
13676 if (info
->start_stop_gc
)
13679 /* To work around a glibc bug, mark XXX input sections
13680 when there is a reference to __start_XXX or __stop_XXX
13682 else if (start_stop
!= NULL
)
13684 asection
*s
= h
->u2
.start_stop_section
;
13685 *start_stop
= true;
13690 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13693 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13694 &cookie
->locsyms
[r_symndx
]);
13697 /* COOKIE->rel describes a relocation against section SEC, which is
13698 a section we've decided to keep. Mark the section that contains
13699 the relocation symbol. */
13702 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13704 elf_gc_mark_hook_fn gc_mark_hook
,
13705 struct elf_reloc_cookie
*cookie
)
13708 bool start_stop
= false;
13710 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13711 while (rsec
!= NULL
)
13713 if (!rsec
->gc_mark
)
13715 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13716 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13718 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13723 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13728 /* The mark phase of garbage collection. For a given section, mark
13729 it and any sections in this section's group, and all the sections
13730 which define symbols to which it refers. */
13733 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13735 elf_gc_mark_hook_fn gc_mark_hook
)
13738 asection
*group_sec
, *eh_frame
;
13742 /* Mark all the sections in the group. */
13743 group_sec
= elf_section_data (sec
)->next_in_group
;
13744 if (group_sec
&& !group_sec
->gc_mark
)
13745 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13748 /* Look through the section relocs. */
13750 eh_frame
= elf_eh_frame_section (sec
->owner
);
13751 if ((sec
->flags
& SEC_RELOC
) != 0
13752 && sec
->reloc_count
> 0
13753 && sec
!= eh_frame
)
13755 struct elf_reloc_cookie cookie
;
13757 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13761 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13762 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13767 fini_reloc_cookie_for_section (&cookie
, sec
);
13771 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13773 struct elf_reloc_cookie cookie
;
13775 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13779 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13780 gc_mark_hook
, &cookie
))
13782 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13786 eh_frame
= elf_section_eh_frame_entry (sec
);
13787 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13788 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13794 /* Scan and mark sections in a special or debug section group. */
13797 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13799 /* Point to first section of section group. */
13801 /* Used to iterate the section group. */
13804 bool is_special_grp
= true;
13805 bool is_debug_grp
= true;
13807 /* First scan to see if group contains any section other than debug
13808 and special section. */
13809 ssec
= msec
= elf_next_in_group (grp
);
13812 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13813 is_debug_grp
= false;
13815 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13816 is_special_grp
= false;
13818 msec
= elf_next_in_group (msec
);
13820 while (msec
!= ssec
);
13822 /* If this is a pure debug section group or pure special section group,
13823 keep all sections in this group. */
13824 if (is_debug_grp
|| is_special_grp
)
13829 msec
= elf_next_in_group (msec
);
13831 while (msec
!= ssec
);
13835 /* Keep debug and special sections. */
13838 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13839 elf_gc_mark_hook_fn mark_hook
)
13843 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13847 bool debug_frag_seen
;
13848 bool has_kept_debug_info
;
13850 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13852 isec
= ibfd
->sections
;
13853 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13856 /* Ensure all linker created sections are kept,
13857 see if any other section is already marked,
13858 and note if we have any fragmented debug sections. */
13859 debug_frag_seen
= some_kept
= has_kept_debug_info
= false;
13860 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13862 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13864 else if (isec
->gc_mark
13865 && (isec
->flags
& SEC_ALLOC
) != 0
13866 && elf_section_type (isec
) != SHT_NOTE
)
13870 /* Since all sections, except for backend specific ones,
13871 have been garbage collected, call mark_hook on this
13872 section if any of its linked-to sections is marked. */
13873 asection
*linked_to_sec
;
13874 for (linked_to_sec
= elf_linked_to_section (isec
);
13875 linked_to_sec
!= NULL
&& !linked_to_sec
->linker_mark
;
13876 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13878 if (linked_to_sec
->gc_mark
)
13880 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
13884 linked_to_sec
->linker_mark
= 1;
13886 for (linked_to_sec
= elf_linked_to_section (isec
);
13887 linked_to_sec
!= NULL
&& linked_to_sec
->linker_mark
;
13888 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13889 linked_to_sec
->linker_mark
= 0;
13892 if (!debug_frag_seen
13893 && (isec
->flags
& SEC_DEBUGGING
)
13894 && startswith (isec
->name
, ".debug_line."))
13895 debug_frag_seen
= true;
13896 else if (strcmp (bfd_section_name (isec
),
13897 "__patchable_function_entries") == 0
13898 && elf_linked_to_section (isec
) == NULL
)
13899 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
13900 "need linked-to section "
13901 "for --gc-sections\n"),
13902 isec
->owner
, isec
);
13905 /* If no non-note alloc section in this file will be kept, then
13906 we can toss out the debug and special sections. */
13910 /* Keep debug and special sections like .comment when they are
13911 not part of a group. Also keep section groups that contain
13912 just debug sections or special sections. NB: Sections with
13913 linked-to section has been handled above. */
13914 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13916 if ((isec
->flags
& SEC_GROUP
) != 0)
13917 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13918 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13919 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13920 && elf_next_in_group (isec
) == NULL
13921 && elf_linked_to_section (isec
) == NULL
)
13923 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13924 has_kept_debug_info
= true;
13927 /* Look for CODE sections which are going to be discarded,
13928 and find and discard any fragmented debug sections which
13929 are associated with that code section. */
13930 if (debug_frag_seen
)
13931 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13932 if ((isec
->flags
& SEC_CODE
) != 0
13933 && isec
->gc_mark
== 0)
13938 ilen
= strlen (isec
->name
);
13940 /* Association is determined by the name of the debug
13941 section containing the name of the code section as
13942 a suffix. For example .debug_line.text.foo is a
13943 debug section associated with .text.foo. */
13944 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13948 if (dsec
->gc_mark
== 0
13949 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13952 dlen
= strlen (dsec
->name
);
13955 && strncmp (dsec
->name
+ (dlen
- ilen
),
13956 isec
->name
, ilen
) == 0)
13961 /* Mark debug sections referenced by kept debug sections. */
13962 if (has_kept_debug_info
)
13963 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13965 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13966 if (!_bfd_elf_gc_mark (info
, isec
,
13967 elf_gc_mark_debug_section
))
13974 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13977 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13979 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13983 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13984 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13985 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13988 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13991 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13993 /* When any section in a section group is kept, we keep all
13994 sections in the section group. If the first member of
13995 the section group is excluded, we will also exclude the
13997 if (o
->flags
& SEC_GROUP
)
13999 asection
*first
= elf_next_in_group (o
);
14000 o
->gc_mark
= first
->gc_mark
;
14006 /* Skip sweeping sections already excluded. */
14007 if (o
->flags
& SEC_EXCLUDE
)
14010 /* Since this is early in the link process, it is simple
14011 to remove a section from the output. */
14012 o
->flags
|= SEC_EXCLUDE
;
14014 if (info
->print_gc_sections
&& o
->size
!= 0)
14015 /* xgettext:c-format */
14016 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14024 /* Propagate collected vtable information. This is called through
14025 elf_link_hash_traverse. */
14028 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
14030 /* Those that are not vtables. */
14032 || h
->u2
.vtable
== NULL
14033 || h
->u2
.vtable
->parent
== NULL
)
14036 /* Those vtables that do not have parents, we cannot merge. */
14037 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
14040 /* If we've already been done, exit. */
14041 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
14044 /* Make sure the parent's table is up to date. */
14045 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
14047 if (h
->u2
.vtable
->used
== NULL
)
14049 /* None of this table's entries were referenced. Re-use the
14051 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14052 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
14059 /* Or the parent's entries into ours. */
14060 cu
= h
->u2
.vtable
->used
;
14062 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14065 const struct elf_backend_data
*bed
;
14066 unsigned int log_file_align
;
14068 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
14069 log_file_align
= bed
->s
->log_file_align
;
14070 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
14084 struct link_info_ok
14086 struct bfd_link_info
*info
;
14091 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
,
14095 bfd_vma hstart
, hend
;
14096 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
14097 const struct elf_backend_data
*bed
;
14098 unsigned int log_file_align
;
14099 struct link_info_ok
*info
= (struct link_info_ok
*) ptr
;
14101 /* Take care of both those symbols that do not describe vtables as
14102 well as those that are not loaded. */
14104 || h
->u2
.vtable
== NULL
14105 || h
->u2
.vtable
->parent
== NULL
)
14108 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
14109 || h
->root
.type
== bfd_link_hash_defweak
);
14111 sec
= h
->root
.u
.def
.section
;
14112 hstart
= h
->root
.u
.def
.value
;
14113 hend
= hstart
+ h
->size
;
14115 relstart
= _bfd_elf_link_info_read_relocs (sec
->owner
, info
->info
,
14116 sec
, NULL
, NULL
, true);
14118 return info
->ok
= false;
14119 bed
= get_elf_backend_data (sec
->owner
);
14120 log_file_align
= bed
->s
->log_file_align
;
14122 relend
= relstart
+ sec
->reloc_count
;
14124 for (rel
= relstart
; rel
< relend
; ++rel
)
14125 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
14127 /* If the entry is in use, do nothing. */
14128 if (h
->u2
.vtable
->used
14129 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
14131 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
14132 if (h
->u2
.vtable
->used
[entry
])
14135 /* Otherwise, kill it. */
14136 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
14142 /* Mark sections containing dynamically referenced symbols. When
14143 building shared libraries, we must assume that any visible symbol is
14147 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
14149 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
14150 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
14152 if ((h
->root
.type
== bfd_link_hash_defined
14153 || h
->root
.type
== bfd_link_hash_defweak
)
14155 || h
->root
.ldscript_def
14156 || !info
->start_stop_gc
)
14157 && ((h
->ref_dynamic
&& !h
->forced_local
)
14158 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
14159 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
14160 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
14161 && (!bfd_link_executable (info
)
14162 || info
->gc_keep_exported
14163 || info
->export_dynamic
14166 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
14167 && (h
->versioned
>= versioned
14168 || !bfd_hide_sym_by_version (info
->version_info
,
14169 h
->root
.root
.string
)))))
14170 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14175 /* Keep all sections containing symbols undefined on the command-line,
14176 and the section containing the entry symbol. */
14179 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
14181 struct bfd_sym_chain
*sym
;
14183 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
14185 struct elf_link_hash_entry
*h
;
14187 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
14188 false, false, false);
14191 && (h
->root
.type
== bfd_link_hash_defined
14192 || h
->root
.type
== bfd_link_hash_defweak
)
14193 && !bfd_is_const_section (h
->root
.u
.def
.section
))
14194 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14199 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
14200 struct bfd_link_info
*info
)
14202 bfd
*ibfd
= info
->input_bfds
;
14204 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14207 struct elf_reloc_cookie cookie
;
14209 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14211 sec
= ibfd
->sections
;
14212 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14215 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
14218 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
14220 if (startswith (bfd_section_name (sec
), ".eh_frame_entry")
14221 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
14223 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
14224 fini_reloc_cookie_rels (&cookie
, sec
);
14231 /* Do mark and sweep of unused sections. */
14234 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14238 elf_gc_mark_hook_fn gc_mark_hook
;
14239 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14240 struct elf_link_hash_table
*htab
;
14241 struct link_info_ok info_ok
;
14243 if (!bed
->can_gc_sections
14244 || !is_elf_hash_table (info
->hash
))
14246 _bfd_error_handler(_("warning: gc-sections option ignored"));
14250 bed
->gc_keep (info
);
14251 htab
= elf_hash_table (info
);
14253 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14254 at the .eh_frame section if we can mark the FDEs individually. */
14255 for (sub
= info
->input_bfds
;
14256 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14257 sub
= sub
->link
.next
)
14260 struct elf_reloc_cookie cookie
;
14262 sec
= sub
->sections
;
14263 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14265 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14266 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
14268 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14269 if (elf_section_data (sec
)->sec_info
14270 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14271 elf_eh_frame_section (sub
) = sec
;
14272 fini_reloc_cookie_for_section (&cookie
, sec
);
14273 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14277 /* Apply transitive closure to the vtable entry usage info. */
14278 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14282 /* Kill the vtable relocations that were not used. */
14283 info_ok
.info
= info
;
14285 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &info_ok
);
14289 /* Mark dynamically referenced symbols. */
14290 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14291 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14293 /* Grovel through relocs to find out who stays ... */
14294 gc_mark_hook
= bed
->gc_mark_hook
;
14295 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14299 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14300 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14301 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14305 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14308 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14309 Also treat note sections as a root, if the section is not part
14310 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14311 well as FINI_ARRAY sections for ld -r. */
14312 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14314 && (o
->flags
& SEC_EXCLUDE
) == 0
14315 && ((o
->flags
& SEC_KEEP
) != 0
14316 || (bfd_link_relocatable (info
)
14317 && ((elf_section_data (o
)->this_hdr
.sh_type
14318 == SHT_PREINIT_ARRAY
)
14319 || (elf_section_data (o
)->this_hdr
.sh_type
14321 || (elf_section_data (o
)->this_hdr
.sh_type
14322 == SHT_FINI_ARRAY
)))
14323 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14324 && elf_next_in_group (o
) == NULL
14325 && elf_linked_to_section (o
) == NULL
)
14326 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14327 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14329 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14334 /* Allow the backend to mark additional target specific sections. */
14335 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14337 /* ... and mark SEC_EXCLUDE for those that go. */
14338 return elf_gc_sweep (abfd
, info
);
14341 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14344 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14346 struct elf_link_hash_entry
*h
,
14349 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14350 struct elf_link_hash_entry
**search
, *child
;
14351 size_t extsymcount
;
14352 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14354 /* The sh_info field of the symtab header tells us where the
14355 external symbols start. We don't care about the local symbols at
14357 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14358 if (!elf_bad_symtab (abfd
))
14359 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14361 sym_hashes
= elf_sym_hashes (abfd
);
14362 sym_hashes_end
= PTR_ADD (sym_hashes
, extsymcount
);
14364 /* Hunt down the child symbol, which is in this section at the same
14365 offset as the relocation. */
14366 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14368 if ((child
= *search
) != NULL
14369 && (child
->root
.type
== bfd_link_hash_defined
14370 || child
->root
.type
== bfd_link_hash_defweak
)
14371 && child
->root
.u
.def
.section
== sec
14372 && child
->root
.u
.def
.value
== offset
)
14376 /* xgettext:c-format */
14377 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14378 abfd
, sec
, (uint64_t) offset
);
14379 bfd_set_error (bfd_error_invalid_operation
);
14383 if (!child
->u2
.vtable
)
14385 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14386 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14387 if (!child
->u2
.vtable
)
14392 /* This *should* only be the absolute section. It could potentially
14393 be that someone has defined a non-global vtable though, which
14394 would be bad. It isn't worth paging in the local symbols to be
14395 sure though; that case should simply be handled by the assembler. */
14397 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14400 child
->u2
.vtable
->parent
= h
;
14405 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14408 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14409 struct elf_link_hash_entry
*h
,
14412 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14413 unsigned int log_file_align
= bed
->s
->log_file_align
;
14417 /* xgettext:c-format */
14418 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14420 bfd_set_error (bfd_error_bad_value
);
14426 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14427 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14432 if (addend
>= h
->u2
.vtable
->size
)
14434 size_t size
, bytes
, file_align
;
14435 bool *ptr
= h
->u2
.vtable
->used
;
14437 /* While the symbol is undefined, we have to be prepared to handle
14439 file_align
= 1 << log_file_align
;
14440 if (h
->root
.type
== bfd_link_hash_undefined
)
14441 size
= addend
+ file_align
;
14445 if (addend
>= size
)
14447 /* Oops! We've got a reference past the defined end of
14448 the table. This is probably a bug -- shall we warn? */
14449 size
= addend
+ file_align
;
14452 size
= (size
+ file_align
- 1) & -file_align
;
14454 /* Allocate one extra entry for use as a "done" flag for the
14455 consolidation pass. */
14456 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bool);
14460 ptr
= (bool *) bfd_realloc (ptr
- 1, bytes
);
14466 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14468 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14472 ptr
= (bool *) bfd_zmalloc (bytes
);
14477 /* And arrange for that done flag to be at index -1. */
14478 h
->u2
.vtable
->used
= ptr
+ 1;
14479 h
->u2
.vtable
->size
= size
;
14482 h
->u2
.vtable
->used
[addend
>> log_file_align
] = true;
14487 /* Map an ELF section header flag to its corresponding string. */
14491 flagword flag_value
;
14492 } elf_flags_to_name_table
;
14494 static const elf_flags_to_name_table elf_flags_to_names
[] =
14496 { "SHF_WRITE", SHF_WRITE
},
14497 { "SHF_ALLOC", SHF_ALLOC
},
14498 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14499 { "SHF_MERGE", SHF_MERGE
},
14500 { "SHF_STRINGS", SHF_STRINGS
},
14501 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14502 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14503 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14504 { "SHF_GROUP", SHF_GROUP
},
14505 { "SHF_TLS", SHF_TLS
},
14506 { "SHF_MASKOS", SHF_MASKOS
},
14507 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14510 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14512 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14513 struct flag_info
*flaginfo
,
14516 const bfd_vma sh_flags
= elf_section_flags (section
);
14518 if (!flaginfo
->flags_initialized
)
14520 bfd
*obfd
= info
->output_bfd
;
14521 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14522 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14524 int without_hex
= 0;
14526 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14529 flagword (*lookup
) (char *);
14531 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14532 if (lookup
!= NULL
)
14534 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14538 if (tf
->with
== with_flags
)
14539 with_hex
|= hexval
;
14540 else if (tf
->with
== without_flags
)
14541 without_hex
|= hexval
;
14546 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14548 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14550 if (tf
->with
== with_flags
)
14551 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14552 else if (tf
->with
== without_flags
)
14553 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14560 info
->callbacks
->einfo
14561 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14565 flaginfo
->flags_initialized
= true;
14566 flaginfo
->only_with_flags
|= with_hex
;
14567 flaginfo
->not_with_flags
|= without_hex
;
14570 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14573 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14579 struct alloc_got_off_arg
{
14581 struct bfd_link_info
*info
;
14584 /* We need a special top-level link routine to convert got reference counts
14585 to real got offsets. */
14588 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14590 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14591 bfd
*obfd
= gofarg
->info
->output_bfd
;
14592 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14594 if (h
->got
.refcount
> 0)
14596 h
->got
.offset
= gofarg
->gotoff
;
14597 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14600 h
->got
.offset
= (bfd_vma
) -1;
14605 /* And an accompanying bit to work out final got entry offsets once
14606 we're done. Should be called from final_link. */
14609 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14610 struct bfd_link_info
*info
)
14613 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14615 struct alloc_got_off_arg gofarg
;
14617 BFD_ASSERT (abfd
== info
->output_bfd
);
14619 if (! is_elf_hash_table (info
->hash
))
14622 /* The GOT offset is relative to the .got section, but the GOT header is
14623 put into the .got.plt section, if the backend uses it. */
14624 if (bed
->want_got_plt
)
14627 gotoff
= bed
->got_header_size
;
14629 /* Do the local .got entries first. */
14630 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14632 bfd_signed_vma
*local_got
;
14633 size_t j
, locsymcount
;
14634 Elf_Internal_Shdr
*symtab_hdr
;
14636 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14639 local_got
= elf_local_got_refcounts (i
);
14643 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14644 if (elf_bad_symtab (i
))
14645 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14647 locsymcount
= symtab_hdr
->sh_info
;
14649 for (j
= 0; j
< locsymcount
; ++j
)
14651 if (local_got
[j
] > 0)
14653 local_got
[j
] = gotoff
;
14654 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14657 local_got
[j
] = (bfd_vma
) -1;
14661 /* Then the global .got entries. .plt refcounts are handled by
14662 adjust_dynamic_symbol */
14663 gofarg
.gotoff
= gotoff
;
14664 gofarg
.info
= info
;
14665 elf_link_hash_traverse (elf_hash_table (info
),
14666 elf_gc_allocate_got_offsets
,
14671 /* Many folk need no more in the way of final link than this, once
14672 got entry reference counting is enabled. */
14675 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14677 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14680 /* Invoke the regular ELF backend linker to do all the work. */
14681 return bfd_elf_final_link (abfd
, info
);
14685 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14687 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14689 if (rcookie
->bad_symtab
)
14690 rcookie
->rel
= rcookie
->rels
;
14692 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14694 unsigned long r_symndx
;
14696 if (! rcookie
->bad_symtab
)
14697 if (rcookie
->rel
->r_offset
> offset
)
14699 if (rcookie
->rel
->r_offset
!= offset
)
14702 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14703 if (r_symndx
== STN_UNDEF
)
14706 if (r_symndx
>= rcookie
->locsymcount
14707 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14709 struct elf_link_hash_entry
*h
;
14711 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14713 while (h
->root
.type
== bfd_link_hash_indirect
14714 || h
->root
.type
== bfd_link_hash_warning
)
14715 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14717 if ((h
->root
.type
== bfd_link_hash_defined
14718 || h
->root
.type
== bfd_link_hash_defweak
)
14719 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14720 || h
->root
.u
.def
.section
->kept_section
!= NULL
14721 || discarded_section (h
->root
.u
.def
.section
)))
14726 /* It's not a relocation against a global symbol,
14727 but it could be a relocation against a local
14728 symbol for a discarded section. */
14730 Elf_Internal_Sym
*isym
;
14732 /* Need to: get the symbol; get the section. */
14733 isym
= &rcookie
->locsyms
[r_symndx
];
14734 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14736 && (isec
->kept_section
!= NULL
14737 || discarded_section (isec
)))
14745 /* Discard unneeded references to discarded sections.
14746 Returns -1 on error, 1 if any section's size was changed, 0 if
14747 nothing changed. This function assumes that the relocations are in
14748 sorted order, which is true for all known assemblers. */
14751 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14753 struct elf_reloc_cookie cookie
;
14758 if (info
->traditional_format
14759 || !is_elf_hash_table (info
->hash
))
14762 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14767 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14770 || i
->reloc_count
== 0
14771 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14775 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14778 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14781 if (_bfd_discard_section_stabs (abfd
, i
,
14782 elf_section_data (i
)->sec_info
,
14783 bfd_elf_reloc_symbol_deleted_p
,
14787 fini_reloc_cookie_for_section (&cookie
, i
);
14792 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14793 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14797 int eh_changed
= 0;
14798 unsigned int eh_alignment
; /* Octets. */
14800 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14806 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14809 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14812 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14813 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14814 bfd_elf_reloc_symbol_deleted_p
,
14818 if (i
->size
!= i
->rawsize
)
14822 fini_reloc_cookie_for_section (&cookie
, i
);
14825 eh_alignment
= ((1 << o
->alignment_power
)
14826 * bfd_octets_per_byte (output_bfd
, o
));
14827 /* Skip over zero terminator, and prevent empty sections from
14828 adding alignment padding at the end. */
14829 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14831 i
->flags
|= SEC_EXCLUDE
;
14832 else if (i
->size
> 4)
14834 /* The last non-empty eh_frame section doesn't need padding. */
14837 /* Any prior sections must pad the last FDE out to the output
14838 section alignment. Otherwise we might have zero padding
14839 between sections, which would be seen as a terminator. */
14840 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14842 /* All but the last zero terminator should have been removed. */
14847 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14848 if (i
->size
!= size
)
14856 elf_link_hash_traverse (elf_hash_table (info
),
14857 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14860 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14862 const struct elf_backend_data
*bed
;
14865 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14867 s
= abfd
->sections
;
14868 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14871 bed
= get_elf_backend_data (abfd
);
14873 if (bed
->elf_backend_discard_info
!= NULL
)
14875 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14878 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14881 fini_reloc_cookie (&cookie
, abfd
);
14885 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14886 _bfd_elf_end_eh_frame_parsing (info
);
14888 if (info
->eh_frame_hdr_type
14889 && !bfd_link_relocatable (info
)
14890 && _bfd_elf_discard_section_eh_frame_hdr (info
))
14897 _bfd_elf_section_already_linked (bfd
*abfd
,
14899 struct bfd_link_info
*info
)
14902 const char *name
, *key
;
14903 struct bfd_section_already_linked
*l
;
14904 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14906 if (sec
->output_section
== bfd_abs_section_ptr
)
14909 flags
= sec
->flags
;
14911 /* Return if it isn't a linkonce section. A comdat group section
14912 also has SEC_LINK_ONCE set. */
14913 if ((flags
& SEC_LINK_ONCE
) == 0)
14916 /* Don't put group member sections on our list of already linked
14917 sections. They are handled as a group via their group section. */
14918 if (elf_sec_group (sec
) != NULL
)
14921 /* For a SHT_GROUP section, use the group signature as the key. */
14923 if ((flags
& SEC_GROUP
) != 0
14924 && elf_next_in_group (sec
) != NULL
14925 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14926 key
= elf_group_name (elf_next_in_group (sec
));
14929 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14930 if (startswith (name
, ".gnu.linkonce.")
14931 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14934 /* Must be a user linkonce section that doesn't follow gcc's
14935 naming convention. In this case we won't be matching
14936 single member groups. */
14940 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14942 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14944 /* We may have 2 different types of sections on the list: group
14945 sections with a signature of <key> (<key> is some string),
14946 and linkonce sections named .gnu.linkonce.<type>.<key>.
14947 Match like sections. LTO plugin sections are an exception.
14948 They are always named .gnu.linkonce.t.<key> and match either
14949 type of section. */
14950 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14951 && ((flags
& SEC_GROUP
) != 0
14952 || strcmp (name
, l
->sec
->name
) == 0))
14953 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
14954 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14956 /* The section has already been linked. See if we should
14957 issue a warning. */
14958 if (!_bfd_handle_already_linked (sec
, l
, info
))
14961 if (flags
& SEC_GROUP
)
14963 asection
*first
= elf_next_in_group (sec
);
14964 asection
*s
= first
;
14968 s
->output_section
= bfd_abs_section_ptr
;
14969 /* Record which group discards it. */
14970 s
->kept_section
= l
->sec
;
14971 s
= elf_next_in_group (s
);
14972 /* These lists are circular. */
14982 /* A single member comdat group section may be discarded by a
14983 linkonce section and vice versa. */
14984 if ((flags
& SEC_GROUP
) != 0)
14986 asection
*first
= elf_next_in_group (sec
);
14988 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14989 /* Check this single member group against linkonce sections. */
14990 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14991 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14992 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14994 first
->output_section
= bfd_abs_section_ptr
;
14995 first
->kept_section
= l
->sec
;
14996 sec
->output_section
= bfd_abs_section_ptr
;
15001 /* Check this linkonce section against single member groups. */
15002 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15003 if (l
->sec
->flags
& SEC_GROUP
)
15005 asection
*first
= elf_next_in_group (l
->sec
);
15008 && elf_next_in_group (first
) == first
15009 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
15011 sec
->output_section
= bfd_abs_section_ptr
;
15012 sec
->kept_section
= first
;
15017 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15018 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15019 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15020 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15021 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15022 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15023 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15024 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15025 The reverse order cannot happen as there is never a bfd with only the
15026 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15027 matter as here were are looking only for cross-bfd sections. */
15029 if ((flags
& SEC_GROUP
) == 0 && startswith (name
, ".gnu.linkonce.r."))
15030 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15031 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15032 && startswith (l
->sec
->name
, ".gnu.linkonce.t."))
15034 if (abfd
!= l
->sec
->owner
)
15035 sec
->output_section
= bfd_abs_section_ptr
;
15039 /* This is the first section with this name. Record it. */
15040 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
15041 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
15042 return sec
->output_section
== bfd_abs_section_ptr
;
15046 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
15048 return sym
->st_shndx
== SHN_COMMON
;
15052 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
15058 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
15060 return bfd_com_section_ptr
;
15064 _bfd_elf_default_got_elt_size (bfd
*abfd
,
15065 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
15066 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
15067 bfd
*ibfd ATTRIBUTE_UNUSED
,
15068 unsigned long symndx ATTRIBUTE_UNUSED
)
15070 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15071 return bed
->s
->arch_size
/ 8;
15074 /* Routines to support the creation of dynamic relocs. */
15076 /* Returns the name of the dynamic reloc section associated with SEC. */
15078 static const char *
15079 get_dynamic_reloc_section_name (bfd
* abfd
,
15084 const char *old_name
= bfd_section_name (sec
);
15085 const char *prefix
= is_rela
? ".rela" : ".rel";
15087 if (old_name
== NULL
)
15090 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
15091 sprintf (name
, "%s%s", prefix
, old_name
);
15096 /* Returns the dynamic reloc section associated with SEC.
15097 If necessary compute the name of the dynamic reloc section based
15098 on SEC's name (looked up in ABFD's string table) and the setting
15102 _bfd_elf_get_dynamic_reloc_section (bfd
*abfd
,
15106 asection
*reloc_sec
= elf_section_data (sec
)->sreloc
;
15108 if (reloc_sec
== NULL
)
15110 const char *name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15114 reloc_sec
= bfd_get_linker_section (abfd
, name
);
15116 if (reloc_sec
!= NULL
)
15117 elf_section_data (sec
)->sreloc
= reloc_sec
;
15124 /* Returns the dynamic reloc section associated with SEC. If the
15125 section does not exist it is created and attached to the DYNOBJ
15126 bfd and stored in the SRELOC field of SEC's elf_section_data
15129 ALIGNMENT is the alignment for the newly created section and
15130 IS_RELA defines whether the name should be .rela.<SEC's name>
15131 or .rel.<SEC's name>. The section name is looked up in the
15132 string table associated with ABFD. */
15135 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
15137 unsigned int alignment
,
15141 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
15143 if (reloc_sec
== NULL
)
15145 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15150 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
15152 if (reloc_sec
== NULL
)
15154 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
15155 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
15156 if ((sec
->flags
& SEC_ALLOC
) != 0)
15157 flags
|= SEC_ALLOC
| SEC_LOAD
;
15159 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
15160 if (reloc_sec
!= NULL
)
15162 /* _bfd_elf_get_sec_type_attr chooses a section type by
15163 name. Override as it may be wrong, eg. for a user
15164 section named "auto" we'll get ".relauto" which is
15165 seen to be a .rela section. */
15166 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
15167 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
15172 elf_section_data (sec
)->sreloc
= reloc_sec
;
15178 /* Copy the ELF symbol type and other attributes for a linker script
15179 assignment from HSRC to HDEST. Generally this should be treated as
15180 if we found a strong non-dynamic definition for HDEST (except that
15181 ld ignores multiple definition errors). */
15183 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
15184 struct bfd_link_hash_entry
*hdest
,
15185 struct bfd_link_hash_entry
*hsrc
)
15187 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
15188 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
15189 Elf_Internal_Sym isym
;
15191 ehdest
->type
= ehsrc
->type
;
15192 ehdest
->target_internal
= ehsrc
->target_internal
;
15194 isym
.st_other
= ehsrc
->other
;
15195 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, true, false);
15198 /* Append a RELA relocation REL to section S in BFD. */
15201 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15203 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15204 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
15205 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
15206 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
15209 /* Append a REL relocation REL to section S in BFD. */
15212 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15214 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15215 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
15216 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
15217 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
15220 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15222 struct bfd_link_hash_entry
*
15223 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
15224 const char *symbol
, asection
*sec
)
15226 struct elf_link_hash_entry
*h
;
15228 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
15229 false, false, true);
15230 /* NB: Common symbols will be turned into definition later. */
15232 && !h
->root
.ldscript_def
15233 && (h
->root
.type
== bfd_link_hash_undefined
15234 || h
->root
.type
== bfd_link_hash_undefweak
15235 || ((h
->ref_regular
|| h
->def_dynamic
)
15237 && h
->root
.type
!= bfd_link_hash_common
)))
15239 bool was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15240 h
->verinfo
.verdef
= NULL
;
15241 h
->root
.type
= bfd_link_hash_defined
;
15242 h
->root
.u
.def
.section
= sec
;
15243 h
->root
.u
.def
.value
= 0;
15244 h
->def_regular
= 1;
15245 h
->def_dynamic
= 0;
15247 h
->u2
.start_stop_section
= sec
;
15248 if (symbol
[0] == '.')
15250 /* .startof. and .sizeof. symbols are local. */
15251 const struct elf_backend_data
*bed
;
15252 bed
= get_elf_backend_data (info
->output_bfd
);
15253 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
15257 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15258 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15259 | info
->start_stop_visibility
);
15261 bfd_elf_link_record_dynamic_symbol (info
, h
);
15268 /* Find dynamic relocs for H that apply to read-only sections. */
15271 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15273 struct elf_dyn_relocs
*p
;
15275 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15277 asection
*s
= p
->sec
->output_section
;
15279 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15285 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15286 read-only sections. */
15289 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15293 if (h
->root
.type
== bfd_link_hash_indirect
)
15296 sec
= _bfd_elf_readonly_dynrelocs (h
);
15299 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15301 info
->flags
|= DF_TEXTREL
;
15302 /* xgettext:c-format */
15303 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15304 "in read-only section `%pA'\n"),
15305 sec
->owner
, h
->root
.root
.string
, sec
);
15307 if (bfd_link_textrel_check (info
))
15308 /* xgettext:c-format */
15309 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15310 "in read-only section `%pA'\n"),
15311 sec
->owner
, h
->root
.root
.string
, sec
);
15313 /* Not an error, just cut short the traversal. */
15319 /* Add dynamic tags. */
15322 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15323 bool need_dynamic_reloc
)
15325 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15327 if (htab
->dynamic_sections_created
)
15329 /* Add some entries to the .dynamic section. We fill in the
15330 values later, in finish_dynamic_sections, but we must add
15331 the entries now so that we get the correct size for the
15332 .dynamic section. The DT_DEBUG entry is filled in by the
15333 dynamic linker and used by the debugger. */
15334 #define add_dynamic_entry(TAG, VAL) \
15335 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15337 const struct elf_backend_data
*bed
15338 = get_elf_backend_data (output_bfd
);
15340 if (bfd_link_executable (info
))
15342 if (!add_dynamic_entry (DT_DEBUG
, 0))
15346 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15348 /* DT_PLTGOT is used by prelink even if there is no PLT
15350 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15354 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15356 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15357 || !add_dynamic_entry (DT_PLTREL
,
15358 (bed
->rela_plts_and_copies_p
15359 ? DT_RELA
: DT_REL
))
15360 || !add_dynamic_entry (DT_JMPREL
, 0))
15364 if (htab
->tlsdesc_plt
15365 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15366 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15369 if (need_dynamic_reloc
)
15371 if (bed
->rela_plts_and_copies_p
)
15373 if (!add_dynamic_entry (DT_RELA
, 0)
15374 || !add_dynamic_entry (DT_RELASZ
, 0)
15375 || !add_dynamic_entry (DT_RELAENT
,
15376 bed
->s
->sizeof_rela
))
15381 if (!add_dynamic_entry (DT_REL
, 0)
15382 || !add_dynamic_entry (DT_RELSZ
, 0)
15383 || !add_dynamic_entry (DT_RELENT
,
15384 bed
->s
->sizeof_rel
))
15388 /* If any dynamic relocs apply to a read-only section,
15389 then we need a DT_TEXTREL entry. */
15390 if ((info
->flags
& DF_TEXTREL
) == 0)
15391 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15394 if ((info
->flags
& DF_TEXTREL
) != 0)
15396 if (htab
->ifunc_resolvers
)
15397 info
->callbacks
->einfo
15398 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15399 "may result in a segfault at runtime; recompile with %s\n"),
15400 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15402 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15407 #undef add_dynamic_entry