1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2022 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
43 struct elf_info_failed
45 struct bfd_link_info
*info
;
49 /* This structure is used to pass information to
50 _bfd_elf_link_find_version_dependencies. */
52 struct elf_find_verdep_info
54 /* General link information. */
55 struct bfd_link_info
*info
;
56 /* The number of dependencies. */
58 /* Whether we had a failure. */
62 static bool _bfd_elf_fix_symbol_flags
63 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
66 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
67 unsigned long r_symndx
,
70 if (r_symndx
>= cookie
->locsymcount
71 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
73 struct elf_link_hash_entry
*h
;
75 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
77 while (h
->root
.type
== bfd_link_hash_indirect
78 || h
->root
.type
== bfd_link_hash_warning
)
79 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
81 if ((h
->root
.type
== bfd_link_hash_defined
82 || h
->root
.type
== bfd_link_hash_defweak
)
83 && discarded_section (h
->root
.u
.def
.section
))
84 return h
->root
.u
.def
.section
;
90 /* It's not a relocation against a global symbol,
91 but it could be a relocation against a local
92 symbol for a discarded section. */
94 Elf_Internal_Sym
*isym
;
96 /* Need to: get the symbol; get the section. */
97 isym
= &cookie
->locsyms
[r_symndx
];
98 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
100 && discard
? discarded_section (isec
) : 1)
106 /* Define a symbol in a dynamic linkage section. */
108 struct elf_link_hash_entry
*
109 _bfd_elf_define_linkage_sym (bfd
*abfd
,
110 struct bfd_link_info
*info
,
114 struct elf_link_hash_entry
*h
;
115 struct bfd_link_hash_entry
*bh
;
116 const struct elf_backend_data
*bed
;
118 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, false, false, false);
121 /* Zap symbol defined in an as-needed lib that wasn't linked.
122 This is a symptom of a larger problem: Absolute symbols
123 defined in shared libraries can't be overridden, because we
124 lose the link to the bfd which is via the symbol section. */
125 h
->root
.type
= bfd_link_hash_new
;
131 bed
= get_elf_backend_data (abfd
);
132 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
133 sec
, 0, NULL
, false, bed
->collect
,
136 h
= (struct elf_link_hash_entry
*) bh
;
137 BFD_ASSERT (h
!= NULL
);
140 h
->root
.linker_def
= 1;
141 h
->type
= STT_OBJECT
;
142 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
143 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
145 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
150 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
154 struct elf_link_hash_entry
*h
;
155 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
156 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
158 /* This function may be called more than once. */
159 if (htab
->sgot
!= NULL
)
162 flags
= bed
->dynamic_sec_flags
;
164 s
= bfd_make_section_anyway_with_flags (abfd
,
165 (bed
->rela_plts_and_copies_p
166 ? ".rela.got" : ".rel.got"),
167 (bed
->dynamic_sec_flags
170 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
174 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
176 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
180 if (bed
->want_got_plt
)
182 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
184 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
189 /* The first bit of the global offset table is the header. */
190 s
->size
+= bed
->got_header_size
;
192 if (bed
->want_got_sym
)
194 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
195 (or .got.plt) section. We don't do this in the linker script
196 because we don't want to define the symbol if we are not creating
197 a global offset table. */
198 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
199 "_GLOBAL_OFFSET_TABLE_");
200 elf_hash_table (info
)->hgot
= h
;
208 /* Create a strtab to hold the dynamic symbol names. */
210 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
212 struct elf_link_hash_table
*hash_table
;
214 hash_table
= elf_hash_table (info
);
215 if (hash_table
->dynobj
== NULL
)
217 /* We may not set dynobj, an input file holding linker created
218 dynamic sections to abfd, which may be a dynamic object with
219 its own dynamic sections. We need to find a normal input file
220 to hold linker created sections if possible. */
221 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
225 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
227 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0
228 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
229 && elf_object_id (ibfd
) == elf_hash_table_id (hash_table
)
230 && !((s
= ibfd
->sections
) != NULL
231 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
))
237 hash_table
->dynobj
= abfd
;
240 if (hash_table
->dynstr
== NULL
)
242 hash_table
->dynstr
= _bfd_elf_strtab_init ();
243 if (hash_table
->dynstr
== NULL
)
249 /* Create some sections which will be filled in with dynamic linking
250 information. ABFD is an input file which requires dynamic sections
251 to be created. The dynamic sections take up virtual memory space
252 when the final executable is run, so we need to create them before
253 addresses are assigned to the output sections. We work out the
254 actual contents and size of these sections later. */
257 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
261 const struct elf_backend_data
*bed
;
262 struct elf_link_hash_entry
*h
;
264 if (! is_elf_hash_table (info
->hash
))
267 if (elf_hash_table (info
)->dynamic_sections_created
)
270 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
273 abfd
= elf_hash_table (info
)->dynobj
;
274 bed
= get_elf_backend_data (abfd
);
276 flags
= bed
->dynamic_sec_flags
;
278 /* A dynamically linked executable has a .interp section, but a
279 shared library does not. */
280 if (bfd_link_executable (info
) && !info
->nointerp
)
282 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
283 flags
| SEC_READONLY
);
288 /* Create sections to hold version informations. These are removed
289 if they are not needed. */
290 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
291 flags
| SEC_READONLY
);
293 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
296 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
297 flags
| SEC_READONLY
);
299 || !bfd_set_section_alignment (s
, 1))
302 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
303 flags
| SEC_READONLY
);
305 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
308 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
309 flags
| SEC_READONLY
);
311 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
313 elf_hash_table (info
)->dynsym
= s
;
315 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
316 flags
| SEC_READONLY
);
320 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
322 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
325 /* The special symbol _DYNAMIC is always set to the start of the
326 .dynamic section. We could set _DYNAMIC in a linker script, but we
327 only want to define it if we are, in fact, creating a .dynamic
328 section. We don't want to define it if there is no .dynamic
329 section, since on some ELF platforms the start up code examines it
330 to decide how to initialize the process. */
331 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
332 elf_hash_table (info
)->hdynamic
= h
;
338 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
339 flags
| SEC_READONLY
);
341 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
343 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
346 if (info
->emit_gnu_hash
&& bed
->record_xhash_symbol
== NULL
)
348 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
349 flags
| SEC_READONLY
);
351 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
353 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
354 4 32-bit words followed by variable count of 64-bit words, then
355 variable count of 32-bit words. */
356 if (bed
->s
->arch_size
== 64)
357 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
359 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
362 if (info
->enable_dt_relr
)
364 s
= bfd_make_section_anyway_with_flags (abfd
, ".relr.dyn",
365 (bed
->dynamic_sec_flags
368 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
370 elf_hash_table (info
)->srelrdyn
= s
;
373 /* Let the backend create the rest of the sections. This lets the
374 backend set the right flags. The backend will normally create
375 the .got and .plt sections. */
376 if (bed
->elf_backend_create_dynamic_sections
== NULL
377 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
380 elf_hash_table (info
)->dynamic_sections_created
= true;
385 /* Create dynamic sections when linking against a dynamic object. */
388 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
390 flagword flags
, pltflags
;
391 struct elf_link_hash_entry
*h
;
393 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
394 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
396 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
397 .rel[a].bss sections. */
398 flags
= bed
->dynamic_sec_flags
;
401 if (bed
->plt_not_loaded
)
402 /* We do not clear SEC_ALLOC here because we still want the OS to
403 allocate space for the section; it's just that there's nothing
404 to read in from the object file. */
405 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
407 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
408 if (bed
->plt_readonly
)
409 pltflags
|= SEC_READONLY
;
411 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
413 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
417 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
419 if (bed
->want_plt_sym
)
421 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
422 "_PROCEDURE_LINKAGE_TABLE_");
423 elf_hash_table (info
)->hplt
= h
;
428 s
= bfd_make_section_anyway_with_flags (abfd
,
429 (bed
->rela_plts_and_copies_p
430 ? ".rela.plt" : ".rel.plt"),
431 flags
| SEC_READONLY
);
433 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
437 if (! _bfd_elf_create_got_section (abfd
, info
))
440 if (bed
->want_dynbss
)
442 /* The .dynbss section is a place to put symbols which are defined
443 by dynamic objects, are referenced by regular objects, and are
444 not functions. We must allocate space for them in the process
445 image and use a R_*_COPY reloc to tell the dynamic linker to
446 initialize them at run time. The linker script puts the .dynbss
447 section into the .bss section of the final image. */
448 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
449 SEC_ALLOC
| SEC_LINKER_CREATED
);
454 if (bed
->want_dynrelro
)
456 /* Similarly, but for symbols that were originally in read-only
457 sections. This section doesn't really need to have contents,
458 but make it like other .data.rel.ro sections. */
459 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
466 /* The .rel[a].bss section holds copy relocs. This section is not
467 normally needed. We need to create it here, though, so that the
468 linker will map it to an output section. We can't just create it
469 only if we need it, because we will not know whether we need it
470 until we have seen all the input files, and the first time the
471 main linker code calls BFD after examining all the input files
472 (size_dynamic_sections) the input sections have already been
473 mapped to the output sections. If the section turns out not to
474 be needed, we can discard it later. We will never need this
475 section when generating a shared object, since they do not use
477 if (bfd_link_executable (info
))
479 s
= bfd_make_section_anyway_with_flags (abfd
,
480 (bed
->rela_plts_and_copies_p
481 ? ".rela.bss" : ".rel.bss"),
482 flags
| SEC_READONLY
);
484 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
488 if (bed
->want_dynrelro
)
490 s
= (bfd_make_section_anyway_with_flags
491 (abfd
, (bed
->rela_plts_and_copies_p
492 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
493 flags
| SEC_READONLY
));
495 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
497 htab
->sreldynrelro
= s
;
505 /* Record a new dynamic symbol. We record the dynamic symbols as we
506 read the input files, since we need to have a list of all of them
507 before we can determine the final sizes of the output sections.
508 Note that we may actually call this function even though we are not
509 going to output any dynamic symbols; in some cases we know that a
510 symbol should be in the dynamic symbol table, but only if there is
514 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
515 struct elf_link_hash_entry
*h
)
517 if (h
->dynindx
== -1)
519 struct elf_strtab_hash
*dynstr
;
524 if (h
->root
.type
== bfd_link_hash_defined
525 || h
->root
.type
== bfd_link_hash_defweak
)
527 /* An IR symbol should not be made dynamic. */
528 if (h
->root
.u
.def
.section
!= NULL
529 && h
->root
.u
.def
.section
->owner
!= NULL
530 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
534 /* XXX: The ABI draft says the linker must turn hidden and
535 internal symbols into STB_LOCAL symbols when producing the
536 DSO. However, if ld.so honors st_other in the dynamic table,
537 this would not be necessary. */
538 switch (ELF_ST_VISIBILITY (h
->other
))
542 if (h
->root
.type
!= bfd_link_hash_undefined
543 && h
->root
.type
!= bfd_link_hash_undefweak
)
546 if (!elf_hash_table (info
)->is_relocatable_executable
547 || ((h
->root
.type
== bfd_link_hash_defined
548 || h
->root
.type
== bfd_link_hash_defweak
)
549 && h
->root
.u
.def
.section
->owner
!= NULL
550 && h
->root
.u
.def
.section
->owner
->no_export
)
551 || (h
->root
.type
== bfd_link_hash_common
552 && h
->root
.u
.c
.p
->section
->owner
!= NULL
553 && h
->root
.u
.c
.p
->section
->owner
->no_export
))
561 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
562 ++elf_hash_table (info
)->dynsymcount
;
564 dynstr
= elf_hash_table (info
)->dynstr
;
567 /* Create a strtab to hold the dynamic symbol names. */
568 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
573 /* We don't put any version information in the dynamic string
575 name
= h
->root
.root
.string
;
576 p
= strchr (name
, ELF_VER_CHR
);
578 /* We know that the p points into writable memory. In fact,
579 there are only a few symbols that have read-only names, being
580 those like _GLOBAL_OFFSET_TABLE_ that are created specially
581 by the backends. Most symbols will have names pointing into
582 an ELF string table read from a file, or to objalloc memory. */
585 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
590 if (indx
== (size_t) -1)
592 h
->dynstr_index
= indx
;
598 /* Mark a symbol dynamic. */
601 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
602 struct elf_link_hash_entry
*h
,
603 Elf_Internal_Sym
*sym
)
605 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
607 /* It may be called more than once on the same H. */
608 if(h
->dynamic
|| bfd_link_relocatable (info
))
611 if ((info
->dynamic_data
612 && (h
->type
== STT_OBJECT
613 || h
->type
== STT_COMMON
615 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
616 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
619 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
622 /* NB: If a symbol is made dynamic by --dynamic-list, it has
624 h
->root
.non_ir_ref_dynamic
= 1;
628 /* Record an assignment to a symbol made by a linker script. We need
629 this in case some dynamic object refers to this symbol. */
632 bfd_elf_record_link_assignment (bfd
*output_bfd
,
633 struct bfd_link_info
*info
,
638 struct elf_link_hash_entry
*h
, *hv
;
639 struct elf_link_hash_table
*htab
;
640 const struct elf_backend_data
*bed
;
642 if (!is_elf_hash_table (info
->hash
))
645 htab
= elf_hash_table (info
);
646 h
= elf_link_hash_lookup (htab
, name
, !provide
, true, false);
650 if (h
->root
.type
== bfd_link_hash_warning
)
651 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
653 if (h
->versioned
== unknown
)
655 /* Set versioned if symbol version is unknown. */
656 char *version
= strrchr (name
, ELF_VER_CHR
);
659 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
660 h
->versioned
= versioned_hidden
;
662 h
->versioned
= versioned
;
666 /* Symbols defined in a linker script but not referenced anywhere
667 else will have non_elf set. */
670 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
674 switch (h
->root
.type
)
676 case bfd_link_hash_defined
:
677 case bfd_link_hash_defweak
:
678 case bfd_link_hash_common
:
680 case bfd_link_hash_undefweak
:
681 case bfd_link_hash_undefined
:
682 /* Since we're defining the symbol, don't let it seem to have not
683 been defined. record_dynamic_symbol and size_dynamic_sections
684 may depend on this. */
685 h
->root
.type
= bfd_link_hash_new
;
686 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
687 bfd_link_repair_undef_list (&htab
->root
);
689 case bfd_link_hash_new
:
691 case bfd_link_hash_indirect
:
692 /* We had a versioned symbol in a dynamic library. We make the
693 the versioned symbol point to this one. */
694 bed
= get_elf_backend_data (output_bfd
);
696 while (hv
->root
.type
== bfd_link_hash_indirect
697 || hv
->root
.type
== bfd_link_hash_warning
)
698 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
699 /* We don't need to update h->root.u since linker will set them
701 h
->root
.type
= bfd_link_hash_undefined
;
702 hv
->root
.type
= bfd_link_hash_indirect
;
703 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
704 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
711 /* If this symbol is being provided by the linker script, and it is
712 currently defined by a dynamic object, but not by a regular
713 object, then mark it as undefined so that the generic linker will
714 force the correct value. */
718 h
->root
.type
= bfd_link_hash_undefined
;
720 /* If this symbol is currently defined by a dynamic object, but not
721 by a regular object, then clear out any version information because
722 the symbol will not be associated with the dynamic object any
724 if (h
->def_dynamic
&& !h
->def_regular
)
725 h
->verinfo
.verdef
= NULL
;
727 /* Make sure this symbol is not garbage collected. */
734 bed
= get_elf_backend_data (output_bfd
);
735 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
736 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
737 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
740 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
742 if (!bfd_link_relocatable (info
)
744 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
745 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
750 || bfd_link_dll (info
)
751 || elf_hash_table (info
)->is_relocatable_executable
)
755 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
758 /* If this is a weak defined symbol, and we know a corresponding
759 real symbol from the same dynamic object, make sure the real
760 symbol is also made into a dynamic symbol. */
763 struct elf_link_hash_entry
*def
= weakdef (h
);
765 if (def
->dynindx
== -1
766 && !bfd_elf_link_record_dynamic_symbol (info
, def
))
774 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
775 success, and 2 on a failure caused by attempting to record a symbol
776 in a discarded section, eg. a discarded link-once section symbol. */
779 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
784 struct elf_link_local_dynamic_entry
*entry
;
785 struct elf_link_hash_table
*eht
;
786 struct elf_strtab_hash
*dynstr
;
789 Elf_External_Sym_Shndx eshndx
;
790 char esym
[sizeof (Elf64_External_Sym
)];
792 if (! is_elf_hash_table (info
->hash
))
795 /* See if the entry exists already. */
796 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
797 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
800 amt
= sizeof (*entry
);
801 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
805 /* Go find the symbol, so that we can find it's name. */
806 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
807 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
809 bfd_release (input_bfd
, entry
);
813 if (entry
->isym
.st_shndx
!= SHN_UNDEF
814 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
818 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
819 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
821 /* We can still bfd_release here as nothing has done another
822 bfd_alloc. We can't do this later in this function. */
823 bfd_release (input_bfd
, entry
);
828 name
= (bfd_elf_string_from_elf_section
829 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
830 entry
->isym
.st_name
));
832 dynstr
= elf_hash_table (info
)->dynstr
;
835 /* Create a strtab to hold the dynamic symbol names. */
836 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
841 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, false);
842 if (dynstr_index
== (size_t) -1)
844 entry
->isym
.st_name
= dynstr_index
;
846 eht
= elf_hash_table (info
);
848 entry
->next
= eht
->dynlocal
;
849 eht
->dynlocal
= entry
;
850 entry
->input_bfd
= input_bfd
;
851 entry
->input_indx
= input_indx
;
854 /* Whatever binding the symbol had before, it's now local. */
856 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
858 /* The dynindx will be set at the end of size_dynamic_sections. */
863 /* Return the dynindex of a local dynamic symbol. */
866 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
870 struct elf_link_local_dynamic_entry
*e
;
872 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
873 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
878 /* This function is used to renumber the dynamic symbols, if some of
879 them are removed because they are marked as local. This is called
880 via elf_link_hash_traverse. */
883 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
886 size_t *count
= (size_t *) data
;
891 if (h
->dynindx
!= -1)
892 h
->dynindx
= ++(*count
);
898 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
899 STB_LOCAL binding. */
902 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
905 size_t *count
= (size_t *) data
;
907 if (!h
->forced_local
)
910 if (h
->dynindx
!= -1)
911 h
->dynindx
= ++(*count
);
916 /* Return true if the dynamic symbol for a given section should be
917 omitted when creating a shared library. */
919 _bfd_elf_omit_section_dynsym_default (bfd
*output_bfd ATTRIBUTE_UNUSED
,
920 struct bfd_link_info
*info
,
923 struct elf_link_hash_table
*htab
;
926 switch (elf_section_data (p
)->this_hdr
.sh_type
)
930 /* If sh_type is yet undecided, assume it could be
931 SHT_PROGBITS/SHT_NOBITS. */
933 htab
= elf_hash_table (info
);
934 if (htab
->text_index_section
!= NULL
)
935 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
937 return (htab
->dynobj
!= NULL
938 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
939 && ip
->output_section
== p
);
941 /* There shouldn't be section relative relocations
942 against any other section. */
949 _bfd_elf_omit_section_dynsym_all
950 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
951 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
952 asection
*p ATTRIBUTE_UNUSED
)
957 /* Assign dynsym indices. In a shared library we generate a section
958 symbol for each output section, which come first. Next come symbols
959 which have been forced to local binding. Then all of the back-end
960 allocated local dynamic syms, followed by the rest of the global
961 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
962 (This prevents the early call before elf_backend_init_index_section
963 and strip_excluded_output_sections setting dynindx for sections
964 that are stripped.) */
967 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
968 struct bfd_link_info
*info
,
969 unsigned long *section_sym_count
)
971 unsigned long dynsymcount
= 0;
972 bool do_sec
= section_sym_count
!= NULL
;
974 if (bfd_link_pic (info
)
975 || elf_hash_table (info
)->is_relocatable_executable
)
977 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
979 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
980 if ((p
->flags
& SEC_EXCLUDE
) == 0
981 && (p
->flags
& SEC_ALLOC
) != 0
982 && elf_hash_table (info
)->dynamic_relocs
983 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
987 elf_section_data (p
)->dynindx
= dynsymcount
;
990 elf_section_data (p
)->dynindx
= 0;
993 *section_sym_count
= dynsymcount
;
995 elf_link_hash_traverse (elf_hash_table (info
),
996 elf_link_renumber_local_hash_table_dynsyms
,
999 if (elf_hash_table (info
)->dynlocal
)
1001 struct elf_link_local_dynamic_entry
*p
;
1002 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
1003 p
->dynindx
= ++dynsymcount
;
1005 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
1007 elf_link_hash_traverse (elf_hash_table (info
),
1008 elf_link_renumber_hash_table_dynsyms
,
1011 /* There is an unused NULL entry at the head of the table which we
1012 must account for in our count even if the table is empty since it
1013 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1014 .dynamic section. */
1017 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
1021 /* Merge st_other field. */
1024 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
1025 unsigned int st_other
, asection
*sec
,
1026 bool definition
, bool dynamic
)
1028 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1030 /* If st_other has a processor-specific meaning, specific
1031 code might be needed here. */
1032 if (bed
->elf_backend_merge_symbol_attribute
)
1033 (*bed
->elf_backend_merge_symbol_attribute
) (h
, st_other
, definition
,
1038 unsigned symvis
= ELF_ST_VISIBILITY (st_other
);
1039 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
1041 /* Keep the most constraining visibility. Leave the remainder
1042 of the st_other field to elf_backend_merge_symbol_attribute. */
1043 if (symvis
- 1 < hvis
- 1)
1044 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
1047 && ELF_ST_VISIBILITY (st_other
) != STV_DEFAULT
1048 && (sec
->flags
& SEC_READONLY
) == 0)
1049 h
->protected_def
= 1;
1052 /* This function is called when we want to merge a new symbol with an
1053 existing symbol. It handles the various cases which arise when we
1054 find a definition in a dynamic object, or when there is already a
1055 definition in a dynamic object. The new symbol is described by
1056 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1057 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1058 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1059 of an old common symbol. We set OVERRIDE if the old symbol is
1060 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1061 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1062 to change. By OK to change, we mean that we shouldn't warn if the
1063 type or size does change. */
1066 _bfd_elf_merge_symbol (bfd
*abfd
,
1067 struct bfd_link_info
*info
,
1069 Elf_Internal_Sym
*sym
,
1072 struct elf_link_hash_entry
**sym_hash
,
1075 unsigned int *pold_alignment
,
1078 bool *type_change_ok
,
1079 bool *size_change_ok
,
1082 asection
*sec
, *oldsec
;
1083 struct elf_link_hash_entry
*h
;
1084 struct elf_link_hash_entry
*hi
;
1085 struct elf_link_hash_entry
*flip
;
1088 bool newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1089 bool newweak
, oldweak
, newfunc
, oldfunc
;
1090 const struct elf_backend_data
*bed
;
1092 bool default_sym
= *matched
;
1098 bind
= ELF_ST_BIND (sym
->st_info
);
1100 if (! bfd_is_und_section (sec
))
1101 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
1103 h
= ((struct elf_link_hash_entry
*)
1104 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
1109 bed
= get_elf_backend_data (abfd
);
1111 /* NEW_VERSION is the symbol version of the new symbol. */
1112 if (h
->versioned
!= unversioned
)
1114 /* Symbol version is unknown or versioned. */
1115 new_version
= strrchr (name
, ELF_VER_CHR
);
1118 if (h
->versioned
== unknown
)
1120 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1121 h
->versioned
= versioned_hidden
;
1123 h
->versioned
= versioned
;
1126 if (new_version
[0] == '\0')
1130 h
->versioned
= unversioned
;
1135 /* For merging, we only care about real symbols. But we need to make
1136 sure that indirect symbol dynamic flags are updated. */
1138 while (h
->root
.type
== bfd_link_hash_indirect
1139 || h
->root
.type
== bfd_link_hash_warning
)
1140 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1144 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1148 /* OLD_HIDDEN is true if the existing symbol is only visible
1149 to the symbol with the same symbol version. NEW_HIDDEN is
1150 true if the new symbol is only visible to the symbol with
1151 the same symbol version. */
1152 bool old_hidden
= h
->versioned
== versioned_hidden
;
1153 bool new_hidden
= hi
->versioned
== versioned_hidden
;
1154 if (!old_hidden
&& !new_hidden
)
1155 /* The new symbol matches the existing symbol if both
1160 /* OLD_VERSION is the symbol version of the existing
1164 if (h
->versioned
>= versioned
)
1165 old_version
= strrchr (h
->root
.root
.string
,
1170 /* The new symbol matches the existing symbol if they
1171 have the same symbol version. */
1172 *matched
= (old_version
== new_version
1173 || (old_version
!= NULL
1174 && new_version
!= NULL
1175 && strcmp (old_version
, new_version
) == 0));
1180 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1185 switch (h
->root
.type
)
1190 case bfd_link_hash_undefined
:
1191 case bfd_link_hash_undefweak
:
1192 oldbfd
= h
->root
.u
.undef
.abfd
;
1195 case bfd_link_hash_defined
:
1196 case bfd_link_hash_defweak
:
1197 oldbfd
= h
->root
.u
.def
.section
->owner
;
1198 oldsec
= h
->root
.u
.def
.section
;
1201 case bfd_link_hash_common
:
1202 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1203 oldsec
= h
->root
.u
.c
.p
->section
;
1205 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1208 if (poldbfd
&& *poldbfd
== NULL
)
1211 /* Differentiate strong and weak symbols. */
1212 newweak
= bind
== STB_WEAK
;
1213 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1214 || h
->root
.type
== bfd_link_hash_undefweak
);
1216 *pold_weak
= oldweak
;
1218 /* We have to check it for every instance since the first few may be
1219 references and not all compilers emit symbol type for undefined
1221 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1223 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1224 respectively, is from a dynamic object. */
1226 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1228 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1229 syms and defined syms in dynamic libraries respectively.
1230 ref_dynamic on the other hand can be set for a symbol defined in
1231 a dynamic library, and def_dynamic may not be set; When the
1232 definition in a dynamic lib is overridden by a definition in the
1233 executable use of the symbol in the dynamic lib becomes a
1234 reference to the executable symbol. */
1237 if (bfd_is_und_section (sec
))
1239 if (bind
!= STB_WEAK
)
1241 h
->ref_dynamic_nonweak
= 1;
1242 hi
->ref_dynamic_nonweak
= 1;
1247 /* Update the existing symbol only if they match. */
1250 hi
->dynamic_def
= 1;
1254 /* If we just created the symbol, mark it as being an ELF symbol.
1255 Other than that, there is nothing to do--there is no merge issue
1256 with a newly defined symbol--so we just return. */
1258 if (h
->root
.type
== bfd_link_hash_new
)
1264 /* In cases involving weak versioned symbols, we may wind up trying
1265 to merge a symbol with itself. Catch that here, to avoid the
1266 confusion that results if we try to override a symbol with
1267 itself. The additional tests catch cases like
1268 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1269 dynamic object, which we do want to handle here. */
1271 && (newweak
|| oldweak
)
1272 && ((abfd
->flags
& DYNAMIC
) == 0
1273 || !h
->def_regular
))
1278 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1279 else if (oldsec
!= NULL
)
1281 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1282 indices used by MIPS ELF. */
1283 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1287 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
))
1289 if (newdyn
!= olddyn
)
1291 /* Handle a case where plugin_notice won't be called and thus
1292 won't set the non_ir_ref flags on the first pass over
1294 h
->root
.non_ir_ref_dynamic
= true;
1295 hi
->root
.non_ir_ref_dynamic
= true;
1298 if ((oldbfd
->flags
& BFD_PLUGIN
) != 0
1299 && hi
->root
.type
== bfd_link_hash_indirect
)
1301 /* Change indirect symbol from IR to undefined. */
1302 hi
->root
.type
= bfd_link_hash_undefined
;
1303 hi
->root
.u
.undef
.abfd
= oldbfd
;
1307 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1308 respectively, appear to be a definition rather than reference. */
1310 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1312 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1313 && h
->root
.type
!= bfd_link_hash_undefweak
1314 && h
->root
.type
!= bfd_link_hash_common
);
1316 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1317 respectively, appear to be a function. */
1319 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1320 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1322 oldfunc
= (h
->type
!= STT_NOTYPE
1323 && bed
->is_function_type (h
->type
));
1325 if (!(newfunc
&& oldfunc
)
1326 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1327 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1328 && h
->type
!= STT_NOTYPE
1329 && (newdef
|| bfd_is_com_section (sec
))
1330 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1332 /* If creating a default indirect symbol ("foo" or "foo@") from
1333 a dynamic versioned definition ("foo@@") skip doing so if
1334 there is an existing regular definition with a different
1335 type. We don't want, for example, a "time" variable in the
1336 executable overriding a "time" function in a shared library. */
1344 /* When adding a symbol from a regular object file after we have
1345 created indirect symbols, undo the indirection and any
1352 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1353 h
->forced_local
= 0;
1357 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1359 h
->root
.type
= bfd_link_hash_undefined
;
1360 h
->root
.u
.undef
.abfd
= abfd
;
1364 h
->root
.type
= bfd_link_hash_new
;
1365 h
->root
.u
.undef
.abfd
= NULL
;
1371 /* Check TLS symbols. We don't check undefined symbols introduced
1372 by "ld -u" which have no type (and oldbfd NULL), and we don't
1373 check symbols from plugins because they also have no type. */
1375 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1376 && (abfd
->flags
& BFD_PLUGIN
) == 0
1377 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1378 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1382 asection
*ntsec
, *tsec
;
1384 if (h
->type
== STT_TLS
)
1405 /* xgettext:c-format */
1406 (_("%s: TLS definition in %pB section %pA "
1407 "mismatches non-TLS definition in %pB section %pA"),
1408 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1409 else if (!tdef
&& !ntdef
)
1411 /* xgettext:c-format */
1412 (_("%s: TLS reference in %pB "
1413 "mismatches non-TLS reference in %pB"),
1414 h
->root
.root
.string
, tbfd
, ntbfd
);
1417 /* xgettext:c-format */
1418 (_("%s: TLS definition in %pB section %pA "
1419 "mismatches non-TLS reference in %pB"),
1420 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1423 /* xgettext:c-format */
1424 (_("%s: TLS reference in %pB "
1425 "mismatches non-TLS definition in %pB section %pA"),
1426 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1428 bfd_set_error (bfd_error_bad_value
);
1432 /* If the old symbol has non-default visibility, we ignore the new
1433 definition from a dynamic object. */
1435 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1436 && !bfd_is_und_section (sec
))
1439 /* Make sure this symbol is dynamic. */
1441 hi
->ref_dynamic
= 1;
1442 /* A protected symbol has external availability. Make sure it is
1443 recorded as dynamic.
1445 FIXME: Should we check type and size for protected symbol? */
1446 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1447 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1452 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1455 /* If the new symbol with non-default visibility comes from a
1456 relocatable file and the old definition comes from a dynamic
1457 object, we remove the old definition. */
1458 if (hi
->root
.type
== bfd_link_hash_indirect
)
1460 /* Handle the case where the old dynamic definition is
1461 default versioned. We need to copy the symbol info from
1462 the symbol with default version to the normal one if it
1463 was referenced before. */
1466 hi
->root
.type
= h
->root
.type
;
1467 h
->root
.type
= bfd_link_hash_indirect
;
1468 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1470 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1471 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1473 /* If the new symbol is hidden or internal, completely undo
1474 any dynamic link state. */
1475 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1476 h
->forced_local
= 0;
1483 /* FIXME: Should we check type and size for protected symbol? */
1493 /* If the old symbol was undefined before, then it will still be
1494 on the undefs list. If the new symbol is undefined or
1495 common, we can't make it bfd_link_hash_new here, because new
1496 undefined or common symbols will be added to the undefs list
1497 by _bfd_generic_link_add_one_symbol. Symbols may not be
1498 added twice to the undefs list. Also, if the new symbol is
1499 undefweak then we don't want to lose the strong undef. */
1500 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1502 h
->root
.type
= bfd_link_hash_undefined
;
1503 h
->root
.u
.undef
.abfd
= abfd
;
1507 h
->root
.type
= bfd_link_hash_new
;
1508 h
->root
.u
.undef
.abfd
= NULL
;
1511 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1513 /* If the new symbol is hidden or internal, completely undo
1514 any dynamic link state. */
1515 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1516 h
->forced_local
= 0;
1522 /* FIXME: Should we check type and size for protected symbol? */
1528 /* If a new weak symbol definition comes from a regular file and the
1529 old symbol comes from a dynamic library, we treat the new one as
1530 strong. Similarly, an old weak symbol definition from a regular
1531 file is treated as strong when the new symbol comes from a dynamic
1532 library. Further, an old weak symbol from a dynamic library is
1533 treated as strong if the new symbol is from a dynamic library.
1534 This reflects the way glibc's ld.so works.
1536 Also allow a weak symbol to override a linker script symbol
1537 defined by an early pass over the script. This is done so the
1538 linker knows the symbol is defined in an object file, for the
1539 DEFINED script function.
1541 Do this before setting *type_change_ok or *size_change_ok so that
1542 we warn properly when dynamic library symbols are overridden. */
1544 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1546 if (olddef
&& newdyn
)
1549 /* Allow changes between different types of function symbol. */
1550 if (newfunc
&& oldfunc
)
1551 *type_change_ok
= true;
1553 /* It's OK to change the type if either the existing symbol or the
1554 new symbol is weak. A type change is also OK if the old symbol
1555 is undefined and the new symbol is defined. */
1560 && h
->root
.type
== bfd_link_hash_undefined
))
1561 *type_change_ok
= true;
1563 /* It's OK to change the size if either the existing symbol or the
1564 new symbol is weak, or if the old symbol is undefined. */
1567 || h
->root
.type
== bfd_link_hash_undefined
)
1568 *size_change_ok
= true;
1570 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1571 symbol, respectively, appears to be a common symbol in a dynamic
1572 object. If a symbol appears in an uninitialized section, and is
1573 not weak, and is not a function, then it may be a common symbol
1574 which was resolved when the dynamic object was created. We want
1575 to treat such symbols specially, because they raise special
1576 considerations when setting the symbol size: if the symbol
1577 appears as a common symbol in a regular object, and the size in
1578 the regular object is larger, we must make sure that we use the
1579 larger size. This problematic case can always be avoided in C,
1580 but it must be handled correctly when using Fortran shared
1583 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1584 likewise for OLDDYNCOMMON and OLDDEF.
1586 Note that this test is just a heuristic, and that it is quite
1587 possible to have an uninitialized symbol in a shared object which
1588 is really a definition, rather than a common symbol. This could
1589 lead to some minor confusion when the symbol really is a common
1590 symbol in some regular object. However, I think it will be
1596 && (sec
->flags
& SEC_ALLOC
) != 0
1597 && (sec
->flags
& SEC_LOAD
) == 0
1600 newdyncommon
= true;
1602 newdyncommon
= false;
1606 && h
->root
.type
== bfd_link_hash_defined
1608 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1609 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1612 olddyncommon
= true;
1614 olddyncommon
= false;
1616 /* We now know everything about the old and new symbols. We ask the
1617 backend to check if we can merge them. */
1618 if (bed
->merge_symbol
!= NULL
)
1620 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1625 /* There are multiple definitions of a normal symbol. Skip the
1626 default symbol as well as definition from an IR object. */
1627 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1628 && !default_sym
&& h
->def_regular
1630 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1631 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1633 /* Handle a multiple definition. */
1634 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1635 abfd
, sec
, *pvalue
);
1640 /* If both the old and the new symbols look like common symbols in a
1641 dynamic object, set the size of the symbol to the larger of the
1646 && sym
->st_size
!= h
->size
)
1648 /* Since we think we have two common symbols, issue a multiple
1649 common warning if desired. Note that we only warn if the
1650 size is different. If the size is the same, we simply let
1651 the old symbol override the new one as normally happens with
1652 symbols defined in dynamic objects. */
1654 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1655 bfd_link_hash_common
, sym
->st_size
);
1656 if (sym
->st_size
> h
->size
)
1657 h
->size
= sym
->st_size
;
1659 *size_change_ok
= true;
1662 /* If we are looking at a dynamic object, and we have found a
1663 definition, we need to see if the symbol was already defined by
1664 some other object. If so, we want to use the existing
1665 definition, and we do not want to report a multiple symbol
1666 definition error; we do this by clobbering *PSEC to be
1667 bfd_und_section_ptr.
1669 We treat a common symbol as a definition if the symbol in the
1670 shared library is a function, since common symbols always
1671 represent variables; this can cause confusion in principle, but
1672 any such confusion would seem to indicate an erroneous program or
1673 shared library. We also permit a common symbol in a regular
1674 object to override a weak symbol in a shared object. */
1679 || (h
->root
.type
== bfd_link_hash_common
1680 && (newweak
|| newfunc
))))
1684 newdyncommon
= false;
1686 *psec
= sec
= bfd_und_section_ptr
;
1687 *size_change_ok
= true;
1689 /* If we get here when the old symbol is a common symbol, then
1690 we are explicitly letting it override a weak symbol or
1691 function in a dynamic object, and we don't want to warn about
1692 a type change. If the old symbol is a defined symbol, a type
1693 change warning may still be appropriate. */
1695 if (h
->root
.type
== bfd_link_hash_common
)
1696 *type_change_ok
= true;
1699 /* Handle the special case of an old common symbol merging with a
1700 new symbol which looks like a common symbol in a shared object.
1701 We change *PSEC and *PVALUE to make the new symbol look like a
1702 common symbol, and let _bfd_generic_link_add_one_symbol do the
1706 && h
->root
.type
== bfd_link_hash_common
)
1710 newdyncommon
= false;
1711 *pvalue
= sym
->st_size
;
1712 *psec
= sec
= bed
->common_section (oldsec
);
1713 *size_change_ok
= true;
1716 /* Skip weak definitions of symbols that are already defined. */
1717 if (newdef
&& olddef
&& newweak
)
1719 /* Don't skip new non-IR weak syms. */
1720 if (!(oldbfd
!= NULL
1721 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1722 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1728 /* Merge st_other. If the symbol already has a dynamic index,
1729 but visibility says it should not be visible, turn it into a
1731 elf_merge_st_other (abfd
, h
, sym
->st_other
, sec
, newdef
, newdyn
);
1732 if (h
->dynindx
!= -1)
1733 switch (ELF_ST_VISIBILITY (h
->other
))
1737 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1742 /* If the old symbol is from a dynamic object, and the new symbol is
1743 a definition which is not from a dynamic object, then the new
1744 symbol overrides the old symbol. Symbols from regular files
1745 always take precedence over symbols from dynamic objects, even if
1746 they are defined after the dynamic object in the link.
1748 As above, we again permit a common symbol in a regular object to
1749 override a definition in a shared object if the shared object
1750 symbol is a function or is weak. */
1755 || (bfd_is_com_section (sec
)
1756 && (oldweak
|| oldfunc
)))
1761 /* Change the hash table entry to undefined, and let
1762 _bfd_generic_link_add_one_symbol do the right thing with the
1765 h
->root
.type
= bfd_link_hash_undefined
;
1766 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1767 *size_change_ok
= true;
1770 olddyncommon
= false;
1772 /* We again permit a type change when a common symbol may be
1773 overriding a function. */
1775 if (bfd_is_com_section (sec
))
1779 /* If a common symbol overrides a function, make sure
1780 that it isn't defined dynamically nor has type
1783 h
->type
= STT_NOTYPE
;
1785 *type_change_ok
= true;
1788 if (hi
->root
.type
== bfd_link_hash_indirect
)
1791 /* This union may have been set to be non-NULL when this symbol
1792 was seen in a dynamic object. We must force the union to be
1793 NULL, so that it is correct for a regular symbol. */
1794 h
->verinfo
.vertree
= NULL
;
1797 /* Handle the special case of a new common symbol merging with an
1798 old symbol that looks like it might be a common symbol defined in
1799 a shared object. Note that we have already handled the case in
1800 which a new common symbol should simply override the definition
1801 in the shared library. */
1804 && bfd_is_com_section (sec
)
1807 /* It would be best if we could set the hash table entry to a
1808 common symbol, but we don't know what to use for the section
1809 or the alignment. */
1810 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1811 bfd_link_hash_common
, sym
->st_size
);
1813 /* If the presumed common symbol in the dynamic object is
1814 larger, pretend that the new symbol has its size. */
1816 if (h
->size
> *pvalue
)
1819 /* We need to remember the alignment required by the symbol
1820 in the dynamic object. */
1821 BFD_ASSERT (pold_alignment
);
1822 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1825 olddyncommon
= false;
1827 h
->root
.type
= bfd_link_hash_undefined
;
1828 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1830 *size_change_ok
= true;
1831 *type_change_ok
= true;
1833 if (hi
->root
.type
== bfd_link_hash_indirect
)
1836 h
->verinfo
.vertree
= NULL
;
1841 /* Handle the case where we had a versioned symbol in a dynamic
1842 library and now find a definition in a normal object. In this
1843 case, we make the versioned symbol point to the normal one. */
1844 flip
->root
.type
= h
->root
.type
;
1845 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1846 h
->root
.type
= bfd_link_hash_indirect
;
1847 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1848 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1852 flip
->ref_dynamic
= 1;
1859 /* This function is called to create an indirect symbol from the
1860 default for the symbol with the default version if needed. The
1861 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1862 set DYNSYM if the new indirect symbol is dynamic. */
1865 _bfd_elf_add_default_symbol (bfd
*abfd
,
1866 struct bfd_link_info
*info
,
1867 struct elf_link_hash_entry
*h
,
1869 Elf_Internal_Sym
*sym
,
1875 bool type_change_ok
;
1876 bool size_change_ok
;
1879 struct elf_link_hash_entry
*hi
;
1880 struct bfd_link_hash_entry
*bh
;
1881 const struct elf_backend_data
*bed
;
1886 size_t len
, shortlen
;
1890 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1893 /* If this symbol has a version, and it is the default version, we
1894 create an indirect symbol from the default name to the fully
1895 decorated name. This will cause external references which do not
1896 specify a version to be bound to this version of the symbol. */
1897 p
= strchr (name
, ELF_VER_CHR
);
1898 if (h
->versioned
== unknown
)
1902 h
->versioned
= unversioned
;
1907 if (p
[1] != ELF_VER_CHR
)
1909 h
->versioned
= versioned_hidden
;
1913 h
->versioned
= versioned
;
1918 /* PR ld/19073: We may see an unversioned definition after the
1924 bed
= get_elf_backend_data (abfd
);
1925 collect
= bed
->collect
;
1926 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1928 shortlen
= p
- name
;
1929 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1930 if (shortname
== NULL
)
1932 memcpy (shortname
, name
, shortlen
);
1933 shortname
[shortlen
] = '\0';
1935 /* We are going to create a new symbol. Merge it with any existing
1936 symbol with this name. For the purposes of the merge, act as
1937 though we were defining the symbol we just defined, although we
1938 actually going to define an indirect symbol. */
1939 type_change_ok
= false;
1940 size_change_ok
= false;
1943 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1944 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1945 &type_change_ok
, &size_change_ok
, &matched
))
1951 if (hi
->def_regular
|| ELF_COMMON_DEF_P (hi
))
1953 /* If the undecorated symbol will have a version added by a
1954 script different to H, then don't indirect to/from the
1955 undecorated symbol. This isn't ideal because we may not yet
1956 have seen symbol versions, if given by a script on the
1957 command line rather than via --version-script. */
1958 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1963 = bfd_find_version_for_sym (info
->version_info
,
1964 hi
->root
.root
.string
, &hide
);
1965 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1967 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
1971 if (hi
->verinfo
.vertree
!= NULL
1972 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1978 /* Add the default symbol if not performing a relocatable link. */
1979 if (! bfd_link_relocatable (info
))
1982 if (bh
->type
== bfd_link_hash_defined
1983 && bh
->u
.def
.section
->owner
!= NULL
1984 && (bh
->u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
1986 /* Mark the previous definition from IR object as
1987 undefined so that the generic linker will override
1989 bh
->type
= bfd_link_hash_undefined
;
1990 bh
->u
.undef
.abfd
= bh
->u
.def
.section
->owner
;
1992 if (! (_bfd_generic_link_add_one_symbol
1993 (info
, abfd
, shortname
, BSF_INDIRECT
,
1994 bfd_ind_section_ptr
,
1995 0, name
, false, collect
, &bh
)))
1997 hi
= (struct elf_link_hash_entry
*) bh
;
2002 /* In this case the symbol named SHORTNAME is overriding the
2003 indirect symbol we want to add. We were planning on making
2004 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2005 is the name without a version. NAME is the fully versioned
2006 name, and it is the default version.
2008 Overriding means that we already saw a definition for the
2009 symbol SHORTNAME in a regular object, and it is overriding
2010 the symbol defined in the dynamic object.
2012 When this happens, we actually want to change NAME, the
2013 symbol we just added, to refer to SHORTNAME. This will cause
2014 references to NAME in the shared object to become references
2015 to SHORTNAME in the regular object. This is what we expect
2016 when we override a function in a shared object: that the
2017 references in the shared object will be mapped to the
2018 definition in the regular object. */
2020 while (hi
->root
.type
== bfd_link_hash_indirect
2021 || hi
->root
.type
== bfd_link_hash_warning
)
2022 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2024 h
->root
.type
= bfd_link_hash_indirect
;
2025 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
2029 hi
->ref_dynamic
= 1;
2033 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
2038 /* Now set HI to H, so that the following code will set the
2039 other fields correctly. */
2043 /* Check if HI is a warning symbol. */
2044 if (hi
->root
.type
== bfd_link_hash_warning
)
2045 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2047 /* If there is a duplicate definition somewhere, then HI may not
2048 point to an indirect symbol. We will have reported an error to
2049 the user in that case. */
2051 if (hi
->root
.type
== bfd_link_hash_indirect
)
2053 struct elf_link_hash_entry
*ht
;
2055 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2056 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2058 /* If we first saw a reference to SHORTNAME with non-default
2059 visibility, merge that visibility to the @@VER symbol. */
2060 elf_merge_st_other (abfd
, ht
, hi
->other
, sec
, true, dynamic
);
2062 /* A reference to the SHORTNAME symbol from a dynamic library
2063 will be satisfied by the versioned symbol at runtime. In
2064 effect, we have a reference to the versioned symbol. */
2065 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2066 hi
->dynamic_def
|= ht
->dynamic_def
;
2068 /* See if the new flags lead us to realize that the symbol must
2074 if (! bfd_link_executable (info
)
2081 if (hi
->ref_regular
)
2087 /* We also need to define an indirection from the nondefault version
2091 len
= strlen (name
);
2092 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2093 if (shortname
== NULL
)
2095 memcpy (shortname
, name
, shortlen
);
2096 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2098 /* Once again, merge with any existing symbol. */
2099 type_change_ok
= false;
2100 size_change_ok
= false;
2102 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2103 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2104 &type_change_ok
, &size_change_ok
, &matched
))
2110 && h
->root
.type
== bfd_link_hash_defweak
2111 && hi
->root
.type
== bfd_link_hash_defined
)
2113 /* We are handling a weak sym@@ver and attempting to define
2114 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2115 new weak sym@ver because there is already a strong sym@ver.
2116 However, sym@ver and sym@@ver are really the same symbol.
2117 The existing strong sym@ver ought to override sym@@ver. */
2118 h
->root
.type
= bfd_link_hash_defined
;
2119 h
->root
.u
.def
.section
= hi
->root
.u
.def
.section
;
2120 h
->root
.u
.def
.value
= hi
->root
.u
.def
.value
;
2121 hi
->root
.type
= bfd_link_hash_indirect
;
2122 hi
->root
.u
.i
.link
= &h
->root
;
2129 /* Here SHORTNAME is a versioned name, so we don't expect to see
2130 the type of override we do in the case above unless it is
2131 overridden by a versioned definition. */
2132 if (hi
->root
.type
!= bfd_link_hash_defined
2133 && hi
->root
.type
!= bfd_link_hash_defweak
)
2135 /* xgettext:c-format */
2136 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2143 if (! (_bfd_generic_link_add_one_symbol
2144 (info
, abfd
, shortname
, BSF_INDIRECT
,
2145 bfd_ind_section_ptr
, 0, name
, false, collect
, &bh
)))
2147 hi
= (struct elf_link_hash_entry
*) bh
;
2150 /* If there is a duplicate definition somewhere, then HI may not
2151 point to an indirect symbol. We will have reported an error
2152 to the user in that case. */
2153 if (hi
->root
.type
== bfd_link_hash_indirect
)
2155 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2156 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2157 hi
->dynamic_def
|= h
->dynamic_def
;
2159 /* If we first saw a reference to @VER symbol with
2160 non-default visibility, merge that visibility to the
2162 elf_merge_st_other (abfd
, h
, hi
->other
, sec
, true, dynamic
);
2164 /* See if the new flags lead us to realize that the symbol
2170 if (! bfd_link_executable (info
)
2176 if (hi
->ref_regular
)
2185 /* This routine is used to export all defined symbols into the dynamic
2186 symbol table. It is called via elf_link_hash_traverse. */
2189 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2191 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2193 /* Ignore indirect symbols. These are added by the versioning code. */
2194 if (h
->root
.type
== bfd_link_hash_indirect
)
2197 /* Ignore this if we won't export it. */
2198 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2201 if (h
->dynindx
== -1
2202 && (h
->def_regular
|| h
->ref_regular
)
2203 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2204 h
->root
.root
.string
))
2206 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2216 /* Look through the symbols which are defined in other shared
2217 libraries and referenced here. Update the list of version
2218 dependencies. This will be put into the .gnu.version_r section.
2219 This function is called via elf_link_hash_traverse. */
2222 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2225 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2226 Elf_Internal_Verneed
*t
;
2227 Elf_Internal_Vernaux
*a
;
2230 /* We only care about symbols defined in shared objects with version
2235 || h
->verinfo
.verdef
== NULL
2236 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2237 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2240 /* See if we already know about this version. */
2241 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2245 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2248 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2249 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2255 /* This is a new version. Add it to tree we are building. */
2260 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2263 rinfo
->failed
= true;
2267 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2268 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2269 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2273 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2276 rinfo
->failed
= true;
2280 /* Note that we are copying a string pointer here, and testing it
2281 above. If bfd_elf_string_from_elf_section is ever changed to
2282 discard the string data when low in memory, this will have to be
2284 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2286 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2287 a
->vna_nextptr
= t
->vn_auxptr
;
2289 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2292 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2299 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2300 hidden. Set *T_P to NULL if there is no match. */
2303 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2304 struct elf_link_hash_entry
*h
,
2305 const char *version_p
,
2306 struct bfd_elf_version_tree
**t_p
,
2309 struct bfd_elf_version_tree
*t
;
2311 /* Look for the version. If we find it, it is no longer weak. */
2312 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2314 if (strcmp (t
->name
, version_p
) == 0)
2318 struct bfd_elf_version_expr
*d
;
2320 len
= version_p
- h
->root
.root
.string
;
2321 alc
= (char *) bfd_malloc (len
);
2324 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2325 alc
[len
- 1] = '\0';
2326 if (alc
[len
- 2] == ELF_VER_CHR
)
2327 alc
[len
- 2] = '\0';
2329 h
->verinfo
.vertree
= t
;
2333 if (t
->globals
.list
!= NULL
)
2334 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2336 /* See if there is anything to force this symbol to
2338 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2340 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2343 && ! info
->export_dynamic
)
2357 /* Return TRUE if the symbol H is hidden by version script. */
2360 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2361 struct elf_link_hash_entry
*h
)
2365 const struct elf_backend_data
*bed
2366 = get_elf_backend_data (info
->output_bfd
);
2368 /* Version script only hides symbols defined in regular objects. */
2369 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2372 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2373 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2375 struct bfd_elf_version_tree
*t
;
2378 if (*p
== ELF_VER_CHR
)
2382 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2386 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2391 /* If we don't have a version for this symbol, see if we can find
2393 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2396 = bfd_find_version_for_sym (info
->version_info
,
2397 h
->root
.root
.string
, &hide
);
2398 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2400 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2408 /* Figure out appropriate versions for all the symbols. We may not
2409 have the version number script until we have read all of the input
2410 files, so until that point we don't know which symbols should be
2411 local. This function is called via elf_link_hash_traverse. */
2414 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2416 struct elf_info_failed
*sinfo
;
2417 struct bfd_link_info
*info
;
2418 const struct elf_backend_data
*bed
;
2419 struct elf_info_failed eif
;
2423 sinfo
= (struct elf_info_failed
*) data
;
2426 /* Fix the symbol flags. */
2429 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2432 sinfo
->failed
= true;
2436 bed
= get_elf_backend_data (info
->output_bfd
);
2438 /* We only need version numbers for symbols defined in regular
2440 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2442 /* Hide symbols defined in discarded input sections. */
2443 if ((h
->root
.type
== bfd_link_hash_defined
2444 || h
->root
.type
== bfd_link_hash_defweak
)
2445 && discarded_section (h
->root
.u
.def
.section
))
2446 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2451 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2452 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2454 struct bfd_elf_version_tree
*t
;
2457 if (*p
== ELF_VER_CHR
)
2460 /* If there is no version string, we can just return out. */
2464 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2466 sinfo
->failed
= true;
2471 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2473 /* If we are building an application, we need to create a
2474 version node for this version. */
2475 if (t
== NULL
&& bfd_link_executable (info
))
2477 struct bfd_elf_version_tree
**pp
;
2480 /* If we aren't going to export this symbol, we don't need
2481 to worry about it. */
2482 if (h
->dynindx
== -1)
2485 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2489 sinfo
->failed
= true;
2494 t
->name_indx
= (unsigned int) -1;
2498 /* Don't count anonymous version tag. */
2499 if (sinfo
->info
->version_info
!= NULL
2500 && sinfo
->info
->version_info
->vernum
== 0)
2502 for (pp
= &sinfo
->info
->version_info
;
2506 t
->vernum
= version_index
;
2510 h
->verinfo
.vertree
= t
;
2514 /* We could not find the version for a symbol when
2515 generating a shared archive. Return an error. */
2517 /* xgettext:c-format */
2518 (_("%pB: version node not found for symbol %s"),
2519 info
->output_bfd
, h
->root
.root
.string
);
2520 bfd_set_error (bfd_error_bad_value
);
2521 sinfo
->failed
= true;
2526 /* If we don't have a version for this symbol, see if we can find
2529 && h
->verinfo
.vertree
== NULL
2530 && sinfo
->info
->version_info
!= NULL
)
2533 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2534 h
->root
.root
.string
, &hide
);
2535 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2536 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2542 /* Read and swap the relocs from the section indicated by SHDR. This
2543 may be either a REL or a RELA section. The relocations are
2544 translated into RELA relocations and stored in INTERNAL_RELOCS,
2545 which should have already been allocated to contain enough space.
2546 The EXTERNAL_RELOCS are a buffer where the external form of the
2547 relocations should be stored.
2549 Returns FALSE if something goes wrong. */
2552 elf_link_read_relocs_from_section (bfd
*abfd
,
2554 Elf_Internal_Shdr
*shdr
,
2555 void *external_relocs
,
2556 Elf_Internal_Rela
*internal_relocs
)
2558 const struct elf_backend_data
*bed
;
2559 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2560 const bfd_byte
*erela
;
2561 const bfd_byte
*erelaend
;
2562 Elf_Internal_Rela
*irela
;
2563 Elf_Internal_Shdr
*symtab_hdr
;
2566 /* Position ourselves at the start of the section. */
2567 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2570 /* Read the relocations. */
2571 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2574 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2575 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2577 bed
= get_elf_backend_data (abfd
);
2579 /* Convert the external relocations to the internal format. */
2580 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2581 swap_in
= bed
->s
->swap_reloc_in
;
2582 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2583 swap_in
= bed
->s
->swap_reloca_in
;
2586 bfd_set_error (bfd_error_wrong_format
);
2590 erela
= (const bfd_byte
*) external_relocs
;
2591 /* Setting erelaend like this and comparing with <= handles case of
2592 a fuzzed object with sh_size not a multiple of sh_entsize. */
2593 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2594 irela
= internal_relocs
;
2595 while (erela
<= erelaend
)
2599 (*swap_in
) (abfd
, erela
, irela
);
2600 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2601 if (bed
->s
->arch_size
== 64)
2605 if ((size_t) r_symndx
>= nsyms
)
2608 /* xgettext:c-format */
2609 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2610 " for offset %#" PRIx64
" in section `%pA'"),
2611 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2612 (uint64_t) irela
->r_offset
, sec
);
2613 bfd_set_error (bfd_error_bad_value
);
2617 else if (r_symndx
!= STN_UNDEF
)
2620 /* xgettext:c-format */
2621 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2622 " for offset %#" PRIx64
" in section `%pA'"
2623 " when the object file has no symbol table"),
2624 abfd
, (uint64_t) r_symndx
,
2625 (uint64_t) irela
->r_offset
, sec
);
2626 bfd_set_error (bfd_error_bad_value
);
2629 irela
+= bed
->s
->int_rels_per_ext_rel
;
2630 erela
+= shdr
->sh_entsize
;
2636 /* Read and swap the relocs for a section O. They may have been
2637 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2638 not NULL, they are used as buffers to read into. They are known to
2639 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2640 the return value is allocated using either malloc or bfd_alloc,
2641 according to the KEEP_MEMORY argument. If O has two relocation
2642 sections (both REL and RELA relocations), then the REL_HDR
2643 relocations will appear first in INTERNAL_RELOCS, followed by the
2644 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2645 update cache_size. */
2648 _bfd_elf_link_info_read_relocs (bfd
*abfd
,
2649 struct bfd_link_info
*info
,
2651 void *external_relocs
,
2652 Elf_Internal_Rela
*internal_relocs
,
2655 void *alloc1
= NULL
;
2656 Elf_Internal_Rela
*alloc2
= NULL
;
2657 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2658 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2659 Elf_Internal_Rela
*internal_rela_relocs
;
2661 if (esdo
->relocs
!= NULL
)
2662 return esdo
->relocs
;
2664 if (o
->reloc_count
== 0)
2667 if (internal_relocs
== NULL
)
2671 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2674 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2676 info
->cache_size
+= size
;
2679 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2680 if (internal_relocs
== NULL
)
2684 if (external_relocs
== NULL
)
2686 bfd_size_type size
= 0;
2689 size
+= esdo
->rel
.hdr
->sh_size
;
2691 size
+= esdo
->rela
.hdr
->sh_size
;
2693 alloc1
= bfd_malloc (size
);
2696 external_relocs
= alloc1
;
2699 internal_rela_relocs
= internal_relocs
;
2702 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2706 external_relocs
= (((bfd_byte
*) external_relocs
)
2707 + esdo
->rel
.hdr
->sh_size
);
2708 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2709 * bed
->s
->int_rels_per_ext_rel
);
2713 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2715 internal_rela_relocs
)))
2718 /* Cache the results for next time, if we can. */
2720 esdo
->relocs
= internal_relocs
;
2724 /* Don't free alloc2, since if it was allocated we are passing it
2725 back (under the name of internal_relocs). */
2727 return internal_relocs
;
2734 bfd_release (abfd
, alloc2
);
2741 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2742 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2743 struct bfd_link_info. */
2746 _bfd_elf_link_read_relocs (bfd
*abfd
,
2748 void *external_relocs
,
2749 Elf_Internal_Rela
*internal_relocs
,
2752 return _bfd_elf_link_info_read_relocs (abfd
, NULL
, o
, external_relocs
,
2753 internal_relocs
, keep_memory
);
2757 /* Compute the size of, and allocate space for, REL_HDR which is the
2758 section header for a section containing relocations for O. */
2761 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2762 struct bfd_elf_section_reloc_data
*reldata
)
2764 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2766 /* That allows us to calculate the size of the section. */
2767 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2769 /* The contents field must last into write_object_contents, so we
2770 allocate it with bfd_alloc rather than malloc. Also since we
2771 cannot be sure that the contents will actually be filled in,
2772 we zero the allocated space. */
2773 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2774 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2777 if (reldata
->hashes
== NULL
&& reldata
->count
)
2779 struct elf_link_hash_entry
**p
;
2781 p
= ((struct elf_link_hash_entry
**)
2782 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2786 reldata
->hashes
= p
;
2792 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2793 originated from the section given by INPUT_REL_HDR) to the
2797 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2798 asection
*input_section
,
2799 Elf_Internal_Shdr
*input_rel_hdr
,
2800 Elf_Internal_Rela
*internal_relocs
,
2801 struct elf_link_hash_entry
**rel_hash
2804 Elf_Internal_Rela
*irela
;
2805 Elf_Internal_Rela
*irelaend
;
2807 struct bfd_elf_section_reloc_data
*output_reldata
;
2808 asection
*output_section
;
2809 const struct elf_backend_data
*bed
;
2810 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2811 struct bfd_elf_section_data
*esdo
;
2813 output_section
= input_section
->output_section
;
2815 bed
= get_elf_backend_data (output_bfd
);
2816 esdo
= elf_section_data (output_section
);
2817 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2819 output_reldata
= &esdo
->rel
;
2820 swap_out
= bed
->s
->swap_reloc_out
;
2822 else if (esdo
->rela
.hdr
2823 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2825 output_reldata
= &esdo
->rela
;
2826 swap_out
= bed
->s
->swap_reloca_out
;
2831 /* xgettext:c-format */
2832 (_("%pB: relocation size mismatch in %pB section %pA"),
2833 output_bfd
, input_section
->owner
, input_section
);
2834 bfd_set_error (bfd_error_wrong_format
);
2838 erel
= output_reldata
->hdr
->contents
;
2839 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2840 irela
= internal_relocs
;
2841 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2842 * bed
->s
->int_rels_per_ext_rel
);
2843 while (irela
< irelaend
)
2845 (*swap_out
) (output_bfd
, irela
, erel
);
2846 irela
+= bed
->s
->int_rels_per_ext_rel
;
2847 erel
+= input_rel_hdr
->sh_entsize
;
2850 /* Bump the counter, so that we know where to add the next set of
2852 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2857 /* Make weak undefined symbols in PIE dynamic. */
2860 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2861 struct elf_link_hash_entry
*h
)
2863 if (bfd_link_pie (info
)
2865 && h
->root
.type
== bfd_link_hash_undefweak
)
2866 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2871 /* Fix up the flags for a symbol. This handles various cases which
2872 can only be fixed after all the input files are seen. This is
2873 currently called by both adjust_dynamic_symbol and
2874 assign_sym_version, which is unnecessary but perhaps more robust in
2875 the face of future changes. */
2878 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2879 struct elf_info_failed
*eif
)
2881 const struct elf_backend_data
*bed
;
2883 /* If this symbol was mentioned in a non-ELF file, try to set
2884 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2885 permit a non-ELF file to correctly refer to a symbol defined in
2886 an ELF dynamic object. */
2889 while (h
->root
.type
== bfd_link_hash_indirect
)
2890 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2892 if (h
->root
.type
!= bfd_link_hash_defined
2893 && h
->root
.type
!= bfd_link_hash_defweak
)
2896 h
->ref_regular_nonweak
= 1;
2900 if (h
->root
.u
.def
.section
->owner
!= NULL
2901 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2902 == bfd_target_elf_flavour
))
2905 h
->ref_regular_nonweak
= 1;
2911 if (h
->dynindx
== -1
2915 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2924 /* Unfortunately, NON_ELF is only correct if the symbol
2925 was first seen in a non-ELF file. Fortunately, if the symbol
2926 was first seen in an ELF file, we're probably OK unless the
2927 symbol was defined in a non-ELF file. Catch that case here.
2928 FIXME: We're still in trouble if the symbol was first seen in
2929 a dynamic object, and then later in a non-ELF regular object. */
2930 if ((h
->root
.type
== bfd_link_hash_defined
2931 || h
->root
.type
== bfd_link_hash_defweak
)
2933 && (h
->root
.u
.def
.section
->owner
!= NULL
2934 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2935 != bfd_target_elf_flavour
)
2936 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2937 && !h
->def_dynamic
)))
2941 /* Backend specific symbol fixup. */
2942 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2943 if (bed
->elf_backend_fixup_symbol
2944 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2947 /* If this is a final link, and the symbol was defined as a common
2948 symbol in a regular object file, and there was no definition in
2949 any dynamic object, then the linker will have allocated space for
2950 the symbol in a common section but the DEF_REGULAR
2951 flag will not have been set. */
2952 if (h
->root
.type
== bfd_link_hash_defined
2956 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2959 /* Symbols defined in discarded sections shouldn't be dynamic. */
2960 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
2961 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
2963 /* If a weak undefined symbol has non-default visibility, we also
2964 hide it from the dynamic linker. */
2965 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2966 && h
->root
.type
== bfd_link_hash_undefweak
)
2967 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
2969 /* A hidden versioned symbol in executable should be forced local if
2970 it is is locally defined, not referenced by shared library and not
2972 else if (bfd_link_executable (eif
->info
)
2973 && h
->versioned
== versioned_hidden
2974 && !eif
->info
->export_dynamic
2978 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
2980 /* If -Bsymbolic was used (which means to bind references to global
2981 symbols to the definition within the shared object), and this
2982 symbol was defined in a regular object, then it actually doesn't
2983 need a PLT entry. Likewise, if the symbol has non-default
2984 visibility. If the symbol has hidden or internal visibility, we
2985 will force it local. */
2986 else if (h
->needs_plt
2987 && bfd_link_pic (eif
->info
)
2988 && is_elf_hash_table (eif
->info
->hash
)
2989 && (SYMBOLIC_BIND (eif
->info
, h
)
2990 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2995 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2996 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2997 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3000 /* If this is a weak defined symbol in a dynamic object, and we know
3001 the real definition in the dynamic object, copy interesting flags
3002 over to the real definition. */
3003 if (h
->is_weakalias
)
3005 struct elf_link_hash_entry
*def
= weakdef (h
);
3007 /* If the real definition is defined by a regular object file,
3008 don't do anything special. See the longer description in
3009 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3010 bfd_link_hash_defined as it was when put on the alias list
3011 then it must have originally been a versioned symbol (for
3012 which a non-versioned indirect symbol is created) and later
3013 a definition for the non-versioned symbol is found. In that
3014 case the indirection is flipped with the versioned symbol
3015 becoming an indirect pointing at the non-versioned symbol.
3016 Thus, not an alias any more. */
3017 if (def
->def_regular
3018 || def
->root
.type
!= bfd_link_hash_defined
)
3021 while ((h
= h
->u
.alias
) != def
)
3022 h
->is_weakalias
= 0;
3026 while (h
->root
.type
== bfd_link_hash_indirect
)
3027 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3028 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3029 || h
->root
.type
== bfd_link_hash_defweak
);
3030 BFD_ASSERT (def
->def_dynamic
);
3031 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
3038 /* Make the backend pick a good value for a dynamic symbol. This is
3039 called via elf_link_hash_traverse, and also calls itself
3043 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
3045 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3046 struct elf_link_hash_table
*htab
;
3047 const struct elf_backend_data
*bed
;
3049 if (! is_elf_hash_table (eif
->info
->hash
))
3052 /* Ignore indirect symbols. These are added by the versioning code. */
3053 if (h
->root
.type
== bfd_link_hash_indirect
)
3056 /* Fix the symbol flags. */
3057 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
3060 htab
= elf_hash_table (eif
->info
);
3061 bed
= get_elf_backend_data (htab
->dynobj
);
3063 if (h
->root
.type
== bfd_link_hash_undefweak
)
3065 if (eif
->info
->dynamic_undefined_weak
== 0)
3066 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3067 else if (eif
->info
->dynamic_undefined_weak
> 0
3069 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3070 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
3071 h
->root
.root
.string
))
3073 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3081 /* If this symbol does not require a PLT entry, and it is not
3082 defined by a dynamic object, or is not referenced by a regular
3083 object, ignore it. We do have to handle a weak defined symbol,
3084 even if no regular object refers to it, if we decided to add it
3085 to the dynamic symbol table. FIXME: Do we normally need to worry
3086 about symbols which are defined by one dynamic object and
3087 referenced by another one? */
3089 && h
->type
!= STT_GNU_IFUNC
3093 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3095 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3099 /* If we've already adjusted this symbol, don't do it again. This
3100 can happen via a recursive call. */
3101 if (h
->dynamic_adjusted
)
3104 /* Don't look at this symbol again. Note that we must set this
3105 after checking the above conditions, because we may look at a
3106 symbol once, decide not to do anything, and then get called
3107 recursively later after REF_REGULAR is set below. */
3108 h
->dynamic_adjusted
= 1;
3110 /* If this is a weak definition, and we know a real definition, and
3111 the real symbol is not itself defined by a regular object file,
3112 then get a good value for the real definition. We handle the
3113 real symbol first, for the convenience of the backend routine.
3115 Note that there is a confusing case here. If the real definition
3116 is defined by a regular object file, we don't get the real symbol
3117 from the dynamic object, but we do get the weak symbol. If the
3118 processor backend uses a COPY reloc, then if some routine in the
3119 dynamic object changes the real symbol, we will not see that
3120 change in the corresponding weak symbol. This is the way other
3121 ELF linkers work as well, and seems to be a result of the shared
3124 I will clarify this issue. Most SVR4 shared libraries define the
3125 variable _timezone and define timezone as a weak synonym. The
3126 tzset call changes _timezone. If you write
3127 extern int timezone;
3129 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3130 you might expect that, since timezone is a synonym for _timezone,
3131 the same number will print both times. However, if the processor
3132 backend uses a COPY reloc, then actually timezone will be copied
3133 into your process image, and, since you define _timezone
3134 yourself, _timezone will not. Thus timezone and _timezone will
3135 wind up at different memory locations. The tzset call will set
3136 _timezone, leaving timezone unchanged. */
3138 if (h
->is_weakalias
)
3140 struct elf_link_hash_entry
*def
= weakdef (h
);
3142 /* If we get to this point, there is an implicit reference to
3143 the alias by a regular object file via the weak symbol H. */
3144 def
->ref_regular
= 1;
3146 /* Ensure that the backend adjust_dynamic_symbol function sees
3147 the strong alias before H by recursively calling ourselves. */
3148 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3152 /* If a symbol has no type and no size and does not require a PLT
3153 entry, then we are probably about to do the wrong thing here: we
3154 are probably going to create a COPY reloc for an empty object.
3155 This case can arise when a shared object is built with assembly
3156 code, and the assembly code fails to set the symbol type. */
3158 && h
->type
== STT_NOTYPE
3161 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3162 h
->root
.root
.string
);
3164 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3173 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3177 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3178 struct elf_link_hash_entry
*h
,
3181 unsigned int power_of_two
;
3183 asection
*sec
= h
->root
.u
.def
.section
;
3185 /* The section alignment of the definition is the maximum alignment
3186 requirement of symbols defined in the section. Since we don't
3187 know the symbol alignment requirement, we start with the
3188 maximum alignment and check low bits of the symbol address
3189 for the minimum alignment. */
3190 power_of_two
= bfd_section_alignment (sec
);
3191 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3192 while ((h
->root
.u
.def
.value
& mask
) != 0)
3198 if (power_of_two
> bfd_section_alignment (dynbss
))
3200 /* Adjust the section alignment if needed. */
3201 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3205 /* We make sure that the symbol will be aligned properly. */
3206 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3208 /* Define the symbol as being at this point in DYNBSS. */
3209 h
->root
.u
.def
.section
= dynbss
;
3210 h
->root
.u
.def
.value
= dynbss
->size
;
3212 /* Increment the size of DYNBSS to make room for the symbol. */
3213 dynbss
->size
+= h
->size
;
3215 /* No error if extern_protected_data is true. */
3216 if (h
->protected_def
3217 && (!info
->extern_protected_data
3218 || (info
->extern_protected_data
< 0
3219 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3220 info
->callbacks
->einfo
3221 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3222 h
->root
.root
.string
);
3227 /* Adjust all external symbols pointing into SEC_MERGE sections
3228 to reflect the object merging within the sections. */
3231 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3235 if ((h
->root
.type
== bfd_link_hash_defined
3236 || h
->root
.type
== bfd_link_hash_defweak
)
3237 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3238 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3240 bfd
*output_bfd
= (bfd
*) data
;
3242 h
->root
.u
.def
.value
=
3243 _bfd_merged_section_offset (output_bfd
,
3244 &h
->root
.u
.def
.section
,
3245 elf_section_data (sec
)->sec_info
,
3246 h
->root
.u
.def
.value
);
3252 /* Returns false if the symbol referred to by H should be considered
3253 to resolve local to the current module, and true if it should be
3254 considered to bind dynamically. */
3257 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3258 struct bfd_link_info
*info
,
3259 bool not_local_protected
)
3261 bool binding_stays_local_p
;
3262 const struct elf_backend_data
*bed
;
3263 struct elf_link_hash_table
*hash_table
;
3268 while (h
->root
.type
== bfd_link_hash_indirect
3269 || h
->root
.type
== bfd_link_hash_warning
)
3270 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3272 /* If it was forced local, then clearly it's not dynamic. */
3273 if (h
->dynindx
== -1)
3275 if (h
->forced_local
)
3278 /* Identify the cases where name binding rules say that a
3279 visible symbol resolves locally. */
3280 binding_stays_local_p
= (bfd_link_executable (info
)
3281 || SYMBOLIC_BIND (info
, h
));
3283 switch (ELF_ST_VISIBILITY (h
->other
))
3290 hash_table
= elf_hash_table (info
);
3291 if (!is_elf_hash_table (&hash_table
->root
))
3294 bed
= get_elf_backend_data (hash_table
->dynobj
);
3296 /* Proper resolution for function pointer equality may require
3297 that these symbols perhaps be resolved dynamically, even though
3298 we should be resolving them to the current module. */
3299 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3300 binding_stays_local_p
= true;
3307 /* If it isn't defined locally, then clearly it's dynamic. */
3308 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3311 /* Otherwise, the symbol is dynamic if binding rules don't tell
3312 us that it remains local. */
3313 return !binding_stays_local_p
;
3316 /* Return true if the symbol referred to by H should be considered
3317 to resolve local to the current module, and false otherwise. Differs
3318 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3319 undefined symbols. The two functions are virtually identical except
3320 for the place where dynindx == -1 is tested. If that test is true,
3321 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3322 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3324 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3325 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3326 treatment of undefined weak symbols. For those that do not make
3327 undefined weak symbols dynamic, both functions may return false. */
3330 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3331 struct bfd_link_info
*info
,
3332 bool local_protected
)
3334 const struct elf_backend_data
*bed
;
3335 struct elf_link_hash_table
*hash_table
;
3337 /* If it's a local sym, of course we resolve locally. */
3341 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3342 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3343 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3346 /* Forced local symbols resolve locally. */
3347 if (h
->forced_local
)
3350 /* Common symbols that become definitions don't get the DEF_REGULAR
3351 flag set, so test it first, and don't bail out. */
3352 if (ELF_COMMON_DEF_P (h
))
3354 /* If we don't have a definition in a regular file, then we can't
3355 resolve locally. The sym is either undefined or dynamic. */
3356 else if (!h
->def_regular
)
3359 /* Non-dynamic symbols resolve locally. */
3360 if (h
->dynindx
== -1)
3363 /* At this point, we know the symbol is defined and dynamic. In an
3364 executable it must resolve locally, likewise when building symbolic
3365 shared libraries. */
3366 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3369 /* Now deal with defined dynamic symbols in shared libraries. Ones
3370 with default visibility might not resolve locally. */
3371 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3374 hash_table
= elf_hash_table (info
);
3375 if (!is_elf_hash_table (&hash_table
->root
))
3378 /* STV_PROTECTED symbols with indirect external access are local. */
3379 if (info
->indirect_extern_access
> 0)
3382 bed
= get_elf_backend_data (hash_table
->dynobj
);
3384 /* If extern_protected_data is false, STV_PROTECTED non-function
3385 symbols are local. */
3386 if ((!info
->extern_protected_data
3387 || (info
->extern_protected_data
< 0
3388 && !bed
->extern_protected_data
))
3389 && !bed
->is_function_type (h
->type
))
3392 /* Function pointer equality tests may require that STV_PROTECTED
3393 symbols be treated as dynamic symbols. If the address of a
3394 function not defined in an executable is set to that function's
3395 plt entry in the executable, then the address of the function in
3396 a shared library must also be the plt entry in the executable. */
3397 return local_protected
;
3400 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3401 aligned. Returns the first TLS output section. */
3403 struct bfd_section
*
3404 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3406 struct bfd_section
*sec
, *tls
;
3407 unsigned int align
= 0;
3409 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3410 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3414 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3415 if (sec
->alignment_power
> align
)
3416 align
= sec
->alignment_power
;
3418 elf_hash_table (info
)->tls_sec
= tls
;
3420 /* Ensure the alignment of the first section (usually .tdata) is the largest
3421 alignment, so that the tls segment starts aligned. */
3423 tls
->alignment_power
= align
;
3428 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3430 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3431 Elf_Internal_Sym
*sym
)
3433 const struct elf_backend_data
*bed
;
3435 /* Local symbols do not count, but target specific ones might. */
3436 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3437 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3440 bed
= get_elf_backend_data (abfd
);
3441 /* Function symbols do not count. */
3442 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3445 /* If the section is undefined, then so is the symbol. */
3446 if (sym
->st_shndx
== SHN_UNDEF
)
3449 /* If the symbol is defined in the common section, then
3450 it is a common definition and so does not count. */
3451 if (bed
->common_definition (sym
))
3454 /* If the symbol is in a target specific section then we
3455 must rely upon the backend to tell us what it is. */
3456 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3457 /* FIXME - this function is not coded yet:
3459 return _bfd_is_global_symbol_definition (abfd, sym);
3461 Instead for now assume that the definition is not global,
3462 Even if this is wrong, at least the linker will behave
3463 in the same way that it used to do. */
3469 /* Search the symbol table of the archive element of the archive ABFD
3470 whose archive map contains a mention of SYMDEF, and determine if
3471 the symbol is defined in this element. */
3473 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3475 Elf_Internal_Shdr
* hdr
;
3479 Elf_Internal_Sym
*isymbuf
;
3480 Elf_Internal_Sym
*isym
;
3481 Elf_Internal_Sym
*isymend
;
3484 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
, NULL
);
3488 if (! bfd_check_format (abfd
, bfd_object
))
3491 /* Select the appropriate symbol table. If we don't know if the
3492 object file is an IR object, give linker LTO plugin a chance to
3493 get the correct symbol table. */
3494 if (abfd
->plugin_format
== bfd_plugin_yes
3495 #if BFD_SUPPORTS_PLUGINS
3496 || (abfd
->plugin_format
== bfd_plugin_unknown
3497 && bfd_link_plugin_object_p (abfd
))
3501 /* Use the IR symbol table if the object has been claimed by
3503 abfd
= abfd
->plugin_dummy_bfd
;
3504 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3506 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3507 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3509 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3511 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3513 /* The sh_info field of the symtab header tells us where the
3514 external symbols start. We don't care about the local symbols. */
3515 if (elf_bad_symtab (abfd
))
3517 extsymcount
= symcount
;
3522 extsymcount
= symcount
- hdr
->sh_info
;
3523 extsymoff
= hdr
->sh_info
;
3526 if (extsymcount
== 0)
3529 /* Read in the symbol table. */
3530 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3532 if (isymbuf
== NULL
)
3535 /* Scan the symbol table looking for SYMDEF. */
3537 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3541 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3546 if (strcmp (name
, symdef
->name
) == 0)
3548 result
= is_global_data_symbol_definition (abfd
, isym
);
3558 /* Add an entry to the .dynamic table. */
3561 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3565 struct elf_link_hash_table
*hash_table
;
3566 const struct elf_backend_data
*bed
;
3568 bfd_size_type newsize
;
3569 bfd_byte
*newcontents
;
3570 Elf_Internal_Dyn dyn
;
3572 hash_table
= elf_hash_table (info
);
3573 if (! is_elf_hash_table (&hash_table
->root
))
3576 if (tag
== DT_RELA
|| tag
== DT_REL
)
3577 hash_table
->dynamic_relocs
= true;
3579 bed
= get_elf_backend_data (hash_table
->dynobj
);
3580 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3581 BFD_ASSERT (s
!= NULL
);
3583 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3584 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3585 if (newcontents
== NULL
)
3589 dyn
.d_un
.d_val
= val
;
3590 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3593 s
->contents
= newcontents
;
3598 /* Strip zero-sized dynamic sections. */
3601 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3603 struct elf_link_hash_table
*hash_table
;
3604 const struct elf_backend_data
*bed
;
3605 asection
*s
, *sdynamic
, **pp
;
3606 asection
*rela_dyn
, *rel_dyn
;
3607 Elf_Internal_Dyn dyn
;
3608 bfd_byte
*extdyn
, *next
;
3609 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3610 bool strip_zero_sized
;
3611 bool strip_zero_sized_plt
;
3613 if (bfd_link_relocatable (info
))
3616 hash_table
= elf_hash_table (info
);
3617 if (!is_elf_hash_table (&hash_table
->root
))
3620 if (!hash_table
->dynobj
)
3623 sdynamic
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3627 bed
= get_elf_backend_data (hash_table
->dynobj
);
3628 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3630 strip_zero_sized
= false;
3631 strip_zero_sized_plt
= false;
3633 /* Strip zero-sized dynamic sections. */
3634 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3635 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3636 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3640 || s
== hash_table
->srelplt
->output_section
3641 || s
== hash_table
->splt
->output_section
))
3644 info
->output_bfd
->section_count
--;
3645 strip_zero_sized
= true;
3650 else if (s
== hash_table
->splt
->output_section
)
3652 s
= hash_table
->splt
;
3653 strip_zero_sized_plt
= true;
3656 s
= hash_table
->srelplt
;
3657 s
->flags
|= SEC_EXCLUDE
;
3658 s
->output_section
= bfd_abs_section_ptr
;
3663 if (strip_zero_sized_plt
&& sdynamic
->size
!= 0)
3664 for (extdyn
= sdynamic
->contents
;
3665 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3668 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3669 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3677 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3678 the procedure linkage table (the .plt section) has been
3680 memmove (extdyn
, next
,
3681 sdynamic
->size
- (next
- sdynamic
->contents
));
3686 if (strip_zero_sized
)
3688 /* Regenerate program headers. */
3689 elf_seg_map (info
->output_bfd
) = NULL
;
3690 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
,
3697 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3698 1 if a DT_NEEDED tag already exists, and 0 on success. */
3701 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3703 struct elf_link_hash_table
*hash_table
;
3707 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3710 hash_table
= elf_hash_table (info
);
3711 soname
= elf_dt_name (abfd
);
3712 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, false);
3713 if (strindex
== (size_t) -1)
3716 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3719 const struct elf_backend_data
*bed
;
3722 bed
= get_elf_backend_data (hash_table
->dynobj
);
3723 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3724 if (sdyn
!= NULL
&& sdyn
->size
!= 0)
3725 for (extdyn
= sdyn
->contents
;
3726 extdyn
< sdyn
->contents
+ sdyn
->size
;
3727 extdyn
+= bed
->s
->sizeof_dyn
)
3729 Elf_Internal_Dyn dyn
;
3731 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3732 if (dyn
.d_tag
== DT_NEEDED
3733 && dyn
.d_un
.d_val
== strindex
)
3735 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3741 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3744 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3750 /* Return true if SONAME is on the needed list between NEEDED and STOP
3751 (or the end of list if STOP is NULL), and needed by a library that
3755 on_needed_list (const char *soname
,
3756 struct bfd_link_needed_list
*needed
,
3757 struct bfd_link_needed_list
*stop
)
3759 struct bfd_link_needed_list
*look
;
3760 for (look
= needed
; look
!= stop
; look
= look
->next
)
3761 if (strcmp (soname
, look
->name
) == 0
3762 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3763 /* If needed by a library that itself is not directly
3764 needed, recursively check whether that library is
3765 indirectly needed. Since we add DT_NEEDED entries to
3766 the end of the list, library dependencies appear after
3767 the library. Therefore search prior to the current
3768 LOOK, preventing possible infinite recursion. */
3769 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3775 /* Sort symbol by value, section, size, and type. */
3777 elf_sort_symbol (const void *arg1
, const void *arg2
)
3779 const struct elf_link_hash_entry
*h1
;
3780 const struct elf_link_hash_entry
*h2
;
3781 bfd_signed_vma vdiff
;
3786 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3787 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3788 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3790 return vdiff
> 0 ? 1 : -1;
3792 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3796 /* Sort so that sized symbols are selected over zero size symbols. */
3797 vdiff
= h1
->size
- h2
->size
;
3799 return vdiff
> 0 ? 1 : -1;
3801 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3802 if (h1
->type
!= h2
->type
)
3803 return h1
->type
- h2
->type
;
3805 /* If symbols are properly sized and typed, and multiple strong
3806 aliases are not defined in a shared library by the user we
3807 shouldn't get here. Unfortunately linker script symbols like
3808 __bss_start sometimes match a user symbol defined at the start of
3809 .bss without proper size and type. We'd like to preference the
3810 user symbol over reserved system symbols. Sort on leading
3812 n1
= h1
->root
.root
.string
;
3813 n2
= h2
->root
.root
.string
;
3826 /* Final sort on name selects user symbols like '_u' over reserved
3827 system symbols like '_Z' and also will avoid qsort instability. */
3831 /* This function is used to adjust offsets into .dynstr for
3832 dynamic symbols. This is called via elf_link_hash_traverse. */
3835 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3837 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3839 if (h
->dynindx
!= -1)
3840 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3844 /* Assign string offsets in .dynstr, update all structures referencing
3848 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3850 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3851 struct elf_link_local_dynamic_entry
*entry
;
3852 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3853 bfd
*dynobj
= hash_table
->dynobj
;
3856 const struct elf_backend_data
*bed
;
3859 _bfd_elf_strtab_finalize (dynstr
);
3860 size
= _bfd_elf_strtab_size (dynstr
);
3862 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3864 if (info
->callbacks
->examine_strtab
)
3865 info
->callbacks
->examine_strtab (dynstr
);
3867 bed
= get_elf_backend_data (dynobj
);
3868 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3869 BFD_ASSERT (sdyn
!= NULL
);
3871 /* Update all .dynamic entries referencing .dynstr strings. */
3872 for (extdyn
= sdyn
->contents
;
3873 extdyn
< PTR_ADD (sdyn
->contents
, sdyn
->size
);
3874 extdyn
+= bed
->s
->sizeof_dyn
)
3876 Elf_Internal_Dyn dyn
;
3878 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3882 dyn
.d_un
.d_val
= size
;
3892 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3897 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3900 /* Now update local dynamic symbols. */
3901 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3902 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3903 entry
->isym
.st_name
);
3905 /* And the rest of dynamic symbols. */
3906 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3908 /* Adjust version definitions. */
3909 if (elf_tdata (output_bfd
)->cverdefs
)
3914 Elf_Internal_Verdef def
;
3915 Elf_Internal_Verdaux defaux
;
3917 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3921 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3923 p
+= sizeof (Elf_External_Verdef
);
3924 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3926 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3928 _bfd_elf_swap_verdaux_in (output_bfd
,
3929 (Elf_External_Verdaux
*) p
, &defaux
);
3930 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3932 _bfd_elf_swap_verdaux_out (output_bfd
,
3933 &defaux
, (Elf_External_Verdaux
*) p
);
3934 p
+= sizeof (Elf_External_Verdaux
);
3937 while (def
.vd_next
);
3940 /* Adjust version references. */
3941 if (elf_tdata (output_bfd
)->verref
)
3946 Elf_Internal_Verneed need
;
3947 Elf_Internal_Vernaux needaux
;
3949 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3953 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3955 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3956 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3957 (Elf_External_Verneed
*) p
);
3958 p
+= sizeof (Elf_External_Verneed
);
3959 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3961 _bfd_elf_swap_vernaux_in (output_bfd
,
3962 (Elf_External_Vernaux
*) p
, &needaux
);
3963 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3965 _bfd_elf_swap_vernaux_out (output_bfd
,
3967 (Elf_External_Vernaux
*) p
);
3968 p
+= sizeof (Elf_External_Vernaux
);
3971 while (need
.vn_next
);
3977 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3978 The default is to only match when the INPUT and OUTPUT are exactly
3982 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3983 const bfd_target
*output
)
3985 return input
== output
;
3988 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3989 This version is used when different targets for the same architecture
3990 are virtually identical. */
3993 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3994 const bfd_target
*output
)
3996 const struct elf_backend_data
*obed
, *ibed
;
3998 if (input
== output
)
4001 ibed
= xvec_get_elf_backend_data (input
);
4002 obed
= xvec_get_elf_backend_data (output
);
4004 if (ibed
->arch
!= obed
->arch
)
4007 /* If both backends are using this function, deem them compatible. */
4008 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
4011 /* Make a special call to the linker "notice" function to tell it that
4012 we are about to handle an as-needed lib, or have finished
4013 processing the lib. */
4016 _bfd_elf_notice_as_needed (bfd
*ibfd
,
4017 struct bfd_link_info
*info
,
4018 enum notice_asneeded_action act
)
4020 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
4023 /* Call ACTION on each relocation in an ELF object file. */
4026 _bfd_elf_link_iterate_on_relocs
4027 (bfd
*abfd
, struct bfd_link_info
*info
,
4028 bool (*action
) (bfd
*, struct bfd_link_info
*, asection
*,
4029 const Elf_Internal_Rela
*))
4031 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4032 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4034 /* If this object is the same format as the output object, and it is
4035 not a shared library, then let the backend look through the
4038 This is required to build global offset table entries and to
4039 arrange for dynamic relocs. It is not required for the
4040 particular common case of linking non PIC code, even when linking
4041 against shared libraries, but unfortunately there is no way of
4042 knowing whether an object file has been compiled PIC or not.
4043 Looking through the relocs is not particularly time consuming.
4044 The problem is that we must either (1) keep the relocs in memory,
4045 which causes the linker to require additional runtime memory or
4046 (2) read the relocs twice from the input file, which wastes time.
4047 This would be a good case for using mmap.
4049 I have no idea how to handle linking PIC code into a file of a
4050 different format. It probably can't be done. */
4051 if ((abfd
->flags
& DYNAMIC
) == 0
4052 && is_elf_hash_table (&htab
->root
)
4053 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4054 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4058 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4060 Elf_Internal_Rela
*internal_relocs
;
4063 /* Don't check relocations in excluded sections. Don't do
4064 anything special with non-loaded, non-alloced sections.
4065 In particular, any relocs in such sections should not
4066 affect GOT and PLT reference counting (ie. we don't
4067 allow them to create GOT or PLT entries), there's no
4068 possibility or desire to optimize TLS relocs, and
4069 there's not much point in propagating relocs to shared
4070 libs that the dynamic linker won't relocate. */
4071 if ((o
->flags
& SEC_ALLOC
) == 0
4072 || (o
->flags
& SEC_RELOC
) == 0
4073 || (o
->flags
& SEC_EXCLUDE
) != 0
4074 || o
->reloc_count
== 0
4075 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4076 && (o
->flags
& SEC_DEBUGGING
) != 0)
4077 || bfd_is_abs_section (o
->output_section
))
4080 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
4083 _bfd_link_keep_memory (info
));
4084 if (internal_relocs
== NULL
)
4087 ok
= action (abfd
, info
, o
, internal_relocs
);
4089 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4090 free (internal_relocs
);
4100 /* Check relocations in an ELF object file. This is called after
4101 all input files have been opened. */
4104 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
4106 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4107 if (bed
->check_relocs
!= NULL
)
4108 return _bfd_elf_link_iterate_on_relocs (abfd
, info
,
4113 /* Add symbols from an ELF object file to the linker hash table. */
4116 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4118 Elf_Internal_Ehdr
*ehdr
;
4119 Elf_Internal_Shdr
*hdr
;
4123 struct elf_link_hash_entry
**sym_hash
;
4125 Elf_External_Versym
*extversym
= NULL
;
4126 Elf_External_Versym
*extversym_end
= NULL
;
4127 Elf_External_Versym
*ever
;
4128 struct elf_link_hash_entry
*weaks
;
4129 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4130 size_t nondeflt_vers_cnt
= 0;
4131 Elf_Internal_Sym
*isymbuf
= NULL
;
4132 Elf_Internal_Sym
*isym
;
4133 Elf_Internal_Sym
*isymend
;
4134 const struct elf_backend_data
*bed
;
4136 struct elf_link_hash_table
*htab
;
4137 void *alloc_mark
= NULL
;
4138 struct bfd_hash_entry
**old_table
= NULL
;
4139 unsigned int old_size
= 0;
4140 unsigned int old_count
= 0;
4141 void *old_tab
= NULL
;
4143 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4144 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4145 void *old_strtab
= NULL
;
4150 htab
= elf_hash_table (info
);
4151 bed
= get_elf_backend_data (abfd
);
4153 if ((abfd
->flags
& DYNAMIC
) == 0)
4159 /* You can't use -r against a dynamic object. Also, there's no
4160 hope of using a dynamic object which does not exactly match
4161 the format of the output file. */
4162 if (bfd_link_relocatable (info
)
4163 || !is_elf_hash_table (&htab
->root
)
4164 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4166 if (bfd_link_relocatable (info
))
4167 bfd_set_error (bfd_error_invalid_operation
);
4169 bfd_set_error (bfd_error_wrong_format
);
4174 ehdr
= elf_elfheader (abfd
);
4175 if (info
->warn_alternate_em
4176 && bed
->elf_machine_code
!= ehdr
->e_machine
4177 && ((bed
->elf_machine_alt1
!= 0
4178 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4179 || (bed
->elf_machine_alt2
!= 0
4180 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4182 /* xgettext:c-format */
4183 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4184 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4186 /* As a GNU extension, any input sections which are named
4187 .gnu.warning.SYMBOL are treated as warning symbols for the given
4188 symbol. This differs from .gnu.warning sections, which generate
4189 warnings when they are included in an output file. */
4190 /* PR 12761: Also generate this warning when building shared libraries. */
4191 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4195 name
= bfd_section_name (s
);
4196 if (startswith (name
, ".gnu.warning."))
4201 name
+= sizeof ".gnu.warning." - 1;
4203 /* If this is a shared object, then look up the symbol
4204 in the hash table. If it is there, and it is already
4205 been defined, then we will not be using the entry
4206 from this shared object, so we don't need to warn.
4207 FIXME: If we see the definition in a regular object
4208 later on, we will warn, but we shouldn't. The only
4209 fix is to keep track of what warnings we are supposed
4210 to emit, and then handle them all at the end of the
4214 struct elf_link_hash_entry
*h
;
4216 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
4218 /* FIXME: What about bfd_link_hash_common? */
4220 && (h
->root
.type
== bfd_link_hash_defined
4221 || h
->root
.type
== bfd_link_hash_defweak
))
4226 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4230 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4235 if (! (_bfd_generic_link_add_one_symbol
4236 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4237 false, bed
->collect
, NULL
)))
4240 if (bfd_link_executable (info
))
4242 /* Clobber the section size so that the warning does
4243 not get copied into the output file. */
4246 /* Also set SEC_EXCLUDE, so that symbols defined in
4247 the warning section don't get copied to the output. */
4248 s
->flags
|= SEC_EXCLUDE
;
4253 just_syms
= ((s
= abfd
->sections
) != NULL
4254 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4259 /* If we are creating a shared library, create all the dynamic
4260 sections immediately. We need to attach them to something,
4261 so we attach them to this BFD, provided it is the right
4262 format and is not from ld --just-symbols. Always create the
4263 dynamic sections for -E/--dynamic-list. FIXME: If there
4264 are no input BFD's of the same format as the output, we can't
4265 make a shared library. */
4267 && (bfd_link_pic (info
)
4268 || (!bfd_link_relocatable (info
)
4270 && (info
->export_dynamic
|| info
->dynamic
)))
4271 && is_elf_hash_table (&htab
->root
)
4272 && info
->output_bfd
->xvec
== abfd
->xvec
4273 && !htab
->dynamic_sections_created
)
4275 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4279 else if (!is_elf_hash_table (&htab
->root
))
4283 const char *soname
= NULL
;
4285 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4286 const Elf_Internal_Phdr
*phdr
;
4287 struct elf_link_loaded_list
*loaded_lib
;
4289 /* ld --just-symbols and dynamic objects don't mix very well.
4290 ld shouldn't allow it. */
4294 /* If this dynamic lib was specified on the command line with
4295 --as-needed in effect, then we don't want to add a DT_NEEDED
4296 tag unless the lib is actually used. Similary for libs brought
4297 in by another lib's DT_NEEDED. When --no-add-needed is used
4298 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4299 any dynamic library in DT_NEEDED tags in the dynamic lib at
4301 add_needed
= (elf_dyn_lib_class (abfd
)
4302 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4303 | DYN_NO_NEEDED
)) == 0;
4305 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4306 if (s
!= NULL
&& s
->size
!= 0)
4310 unsigned int elfsec
;
4311 unsigned long shlink
;
4313 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
4320 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4321 if (elfsec
== SHN_BAD
)
4322 goto error_free_dyn
;
4323 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4325 for (extdyn
= dynbuf
;
4326 extdyn
<= dynbuf
+ s
->size
- bed
->s
->sizeof_dyn
;
4327 extdyn
+= bed
->s
->sizeof_dyn
)
4329 Elf_Internal_Dyn dyn
;
4331 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4332 if (dyn
.d_tag
== DT_SONAME
)
4334 unsigned int tagv
= dyn
.d_un
.d_val
;
4335 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4337 goto error_free_dyn
;
4339 if (dyn
.d_tag
== DT_NEEDED
)
4341 struct bfd_link_needed_list
*n
, **pn
;
4343 unsigned int tagv
= dyn
.d_un
.d_val
;
4344 size_t amt
= sizeof (struct bfd_link_needed_list
);
4346 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4347 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4348 if (n
== NULL
|| fnm
== NULL
)
4349 goto error_free_dyn
;
4350 amt
= strlen (fnm
) + 1;
4351 anm
= (char *) bfd_alloc (abfd
, amt
);
4353 goto error_free_dyn
;
4354 memcpy (anm
, fnm
, amt
);
4358 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4362 if (dyn
.d_tag
== DT_RUNPATH
)
4364 struct bfd_link_needed_list
*n
, **pn
;
4366 unsigned int tagv
= dyn
.d_un
.d_val
;
4367 size_t amt
= sizeof (struct bfd_link_needed_list
);
4369 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4370 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4371 if (n
== NULL
|| fnm
== NULL
)
4372 goto error_free_dyn
;
4373 amt
= strlen (fnm
) + 1;
4374 anm
= (char *) bfd_alloc (abfd
, amt
);
4376 goto error_free_dyn
;
4377 memcpy (anm
, fnm
, amt
);
4381 for (pn
= & runpath
;
4387 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4388 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4390 struct bfd_link_needed_list
*n
, **pn
;
4392 unsigned int tagv
= dyn
.d_un
.d_val
;
4393 size_t amt
= sizeof (struct bfd_link_needed_list
);
4395 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4396 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4397 if (n
== NULL
|| fnm
== NULL
)
4398 goto error_free_dyn
;
4399 amt
= strlen (fnm
) + 1;
4400 anm
= (char *) bfd_alloc (abfd
, amt
);
4402 goto error_free_dyn
;
4403 memcpy (anm
, fnm
, amt
);
4413 if (dyn
.d_tag
== DT_AUDIT
)
4415 unsigned int tagv
= dyn
.d_un
.d_val
;
4416 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4418 if (dyn
.d_tag
== DT_FLAGS_1
)
4419 elf_tdata (abfd
)->is_pie
= (dyn
.d_un
.d_val
& DF_1_PIE
) != 0;
4425 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4426 frees all more recently bfd_alloc'd blocks as well. */
4432 struct bfd_link_needed_list
**pn
;
4433 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4438 /* If we have a PT_GNU_RELRO program header, mark as read-only
4439 all sections contained fully therein. This makes relro
4440 shared library sections appear as they will at run-time. */
4441 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4442 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4443 if (phdr
->p_type
== PT_GNU_RELRO
)
4445 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4447 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4449 if ((s
->flags
& SEC_ALLOC
) != 0
4450 && s
->vma
* opb
>= phdr
->p_vaddr
4451 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4452 s
->flags
|= SEC_READONLY
;
4457 /* We do not want to include any of the sections in a dynamic
4458 object in the output file. We hack by simply clobbering the
4459 list of sections in the BFD. This could be handled more
4460 cleanly by, say, a new section flag; the existing
4461 SEC_NEVER_LOAD flag is not the one we want, because that one
4462 still implies that the section takes up space in the output
4464 bfd_section_list_clear (abfd
);
4466 /* Find the name to use in a DT_NEEDED entry that refers to this
4467 object. If the object has a DT_SONAME entry, we use it.
4468 Otherwise, if the generic linker stuck something in
4469 elf_dt_name, we use that. Otherwise, we just use the file
4471 if (soname
== NULL
|| *soname
== '\0')
4473 soname
= elf_dt_name (abfd
);
4474 if (soname
== NULL
|| *soname
== '\0')
4475 soname
= bfd_get_filename (abfd
);
4478 /* Save the SONAME because sometimes the linker emulation code
4479 will need to know it. */
4480 elf_dt_name (abfd
) = soname
;
4482 /* If we have already included this dynamic object in the
4483 link, just ignore it. There is no reason to include a
4484 particular dynamic object more than once. */
4485 for (loaded_lib
= htab
->dyn_loaded
;
4487 loaded_lib
= loaded_lib
->next
)
4489 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4493 /* Create dynamic sections for backends that require that be done
4494 before setup_gnu_properties. */
4496 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4499 /* Save the DT_AUDIT entry for the linker emulation code. */
4500 elf_dt_audit (abfd
) = audit
;
4503 /* If this is a dynamic object, we always link against the .dynsym
4504 symbol table, not the .symtab symbol table. The dynamic linker
4505 will only see the .dynsym symbol table, so there is no reason to
4506 look at .symtab for a dynamic object. */
4508 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4509 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4511 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4513 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4515 /* The sh_info field of the symtab header tells us where the
4516 external symbols start. We don't care about the local symbols at
4518 if (elf_bad_symtab (abfd
))
4520 extsymcount
= symcount
;
4525 extsymcount
= symcount
- hdr
->sh_info
;
4526 extsymoff
= hdr
->sh_info
;
4529 sym_hash
= elf_sym_hashes (abfd
);
4530 if (extsymcount
!= 0)
4532 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4534 if (isymbuf
== NULL
)
4537 if (sym_hash
== NULL
)
4539 /* We store a pointer to the hash table entry for each
4541 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4542 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4543 if (sym_hash
== NULL
)
4544 goto error_free_sym
;
4545 elf_sym_hashes (abfd
) = sym_hash
;
4551 /* Read in any version definitions. */
4552 if (!_bfd_elf_slurp_version_tables (abfd
,
4553 info
->default_imported_symver
))
4554 goto error_free_sym
;
4556 /* Read in the symbol versions, but don't bother to convert them
4557 to internal format. */
4558 if (elf_dynversym (abfd
) != 0)
4560 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4561 bfd_size_type amt
= versymhdr
->sh_size
;
4563 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4564 goto error_free_sym
;
4565 extversym
= (Elf_External_Versym
*)
4566 _bfd_malloc_and_read (abfd
, amt
, amt
);
4567 if (extversym
== NULL
)
4568 goto error_free_sym
;
4569 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4573 /* If we are loading an as-needed shared lib, save the symbol table
4574 state before we start adding symbols. If the lib turns out
4575 to be unneeded, restore the state. */
4576 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4581 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4583 struct bfd_hash_entry
*p
;
4584 struct elf_link_hash_entry
*h
;
4586 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4588 h
= (struct elf_link_hash_entry
*) p
;
4589 entsize
+= htab
->root
.table
.entsize
;
4590 if (h
->root
.type
== bfd_link_hash_warning
)
4592 entsize
+= htab
->root
.table
.entsize
;
4593 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4595 if (h
->root
.type
== bfd_link_hash_common
)
4596 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4600 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4601 old_tab
= bfd_malloc (tabsize
+ entsize
);
4602 if (old_tab
== NULL
)
4603 goto error_free_vers
;
4605 /* Remember the current objalloc pointer, so that all mem for
4606 symbols added can later be reclaimed. */
4607 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4608 if (alloc_mark
== NULL
)
4609 goto error_free_vers
;
4611 /* Make a special call to the linker "notice" function to
4612 tell it that we are about to handle an as-needed lib. */
4613 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4614 goto error_free_vers
;
4616 /* Clone the symbol table. Remember some pointers into the
4617 symbol table, and dynamic symbol count. */
4618 old_ent
= (char *) old_tab
+ tabsize
;
4619 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4620 old_undefs
= htab
->root
.undefs
;
4621 old_undefs_tail
= htab
->root
.undefs_tail
;
4622 old_table
= htab
->root
.table
.table
;
4623 old_size
= htab
->root
.table
.size
;
4624 old_count
= htab
->root
.table
.count
;
4626 if (htab
->dynstr
!= NULL
)
4628 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4629 if (old_strtab
== NULL
)
4630 goto error_free_vers
;
4633 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4635 struct bfd_hash_entry
*p
;
4636 struct elf_link_hash_entry
*h
;
4638 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4640 h
= (struct elf_link_hash_entry
*) p
;
4641 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4642 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4643 if (h
->root
.type
== bfd_link_hash_warning
)
4645 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4646 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4647 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4649 if (h
->root
.type
== bfd_link_hash_common
)
4651 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4652 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4659 if (extversym
== NULL
)
4661 else if (extversym
+ extsymoff
< extversym_end
)
4662 ever
= extversym
+ extsymoff
;
4665 /* xgettext:c-format */
4666 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4667 abfd
, (long) extsymoff
,
4668 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4669 bfd_set_error (bfd_error_bad_value
);
4670 goto error_free_vers
;
4673 if (!bfd_link_relocatable (info
)
4674 && abfd
->lto_slim_object
)
4677 (_("%pB: plugin needed to handle lto object"), abfd
);
4680 for (isym
= isymbuf
, isymend
= PTR_ADD (isymbuf
, extsymcount
);
4682 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4686 asection
*sec
, *new_sec
;
4689 struct elf_link_hash_entry
*h
;
4690 struct elf_link_hash_entry
*hi
;
4692 bool size_change_ok
;
4693 bool type_change_ok
;
4699 unsigned int old_alignment
;
4700 unsigned int shindex
;
4706 flags
= BSF_NO_FLAGS
;
4708 value
= isym
->st_value
;
4709 common
= bed
->common_definition (isym
);
4710 if (common
&& info
->inhibit_common_definition
)
4712 /* Treat common symbol as undefined for --no-define-common. */
4713 isym
->st_shndx
= SHN_UNDEF
;
4718 bind
= ELF_ST_BIND (isym
->st_info
);
4722 /* This should be impossible, since ELF requires that all
4723 global symbols follow all local symbols, and that sh_info
4724 point to the first global symbol. Unfortunately, Irix 5
4726 if (elf_bad_symtab (abfd
))
4729 /* If we aren't prepared to handle locals within the globals
4730 then we'll likely segfault on a NULL symbol hash if the
4731 symbol is ever referenced in relocations. */
4732 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4733 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4734 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4735 " (>= sh_info of %lu)"),
4736 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4739 /* Dynamic object relocations are not processed by ld, so
4740 ld won't run into the problem mentioned above. */
4743 bfd_set_error (bfd_error_bad_value
);
4744 goto error_free_vers
;
4747 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4755 case STB_GNU_UNIQUE
:
4756 flags
= BSF_GNU_UNIQUE
;
4760 /* Leave it up to the processor backend. */
4764 if (isym
->st_shndx
== SHN_UNDEF
)
4765 sec
= bfd_und_section_ptr
;
4766 else if (isym
->st_shndx
== SHN_ABS
)
4767 sec
= bfd_abs_section_ptr
;
4768 else if (isym
->st_shndx
== SHN_COMMON
)
4770 sec
= bfd_com_section_ptr
;
4771 /* What ELF calls the size we call the value. What ELF
4772 calls the value we call the alignment. */
4773 value
= isym
->st_size
;
4777 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4779 sec
= bfd_abs_section_ptr
;
4780 else if (discarded_section (sec
))
4782 /* Symbols from discarded section are undefined. We keep
4784 sec
= bfd_und_section_ptr
;
4786 isym
->st_shndx
= SHN_UNDEF
;
4788 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4792 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4795 goto error_free_vers
;
4797 if (isym
->st_shndx
== SHN_COMMON
4798 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4800 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4804 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4806 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4808 goto error_free_vers
;
4812 else if (isym
->st_shndx
== SHN_COMMON
4813 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4814 && !bfd_link_relocatable (info
))
4816 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4820 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4821 | SEC_LINKER_CREATED
);
4822 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4824 goto error_free_vers
;
4828 else if (bed
->elf_add_symbol_hook
)
4830 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4832 goto error_free_vers
;
4834 /* The hook function sets the name to NULL if this symbol
4835 should be skipped for some reason. */
4840 /* Sanity check that all possibilities were handled. */
4844 /* Silently discard TLS symbols from --just-syms. There's
4845 no way to combine a static TLS block with a new TLS block
4846 for this executable. */
4847 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4848 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4851 if (bfd_is_und_section (sec
)
4852 || bfd_is_com_section (sec
))
4857 size_change_ok
= false;
4858 type_change_ok
= bed
->type_change_ok
;
4865 if (is_elf_hash_table (&htab
->root
))
4867 Elf_Internal_Versym iver
;
4868 unsigned int vernum
= 0;
4873 if (info
->default_imported_symver
)
4874 /* Use the default symbol version created earlier. */
4875 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4879 else if (ever
>= extversym_end
)
4881 /* xgettext:c-format */
4882 _bfd_error_handler (_("%pB: not enough version information"),
4884 bfd_set_error (bfd_error_bad_value
);
4885 goto error_free_vers
;
4888 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4890 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4892 /* If this is a hidden symbol, or if it is not version
4893 1, we append the version name to the symbol name.
4894 However, we do not modify a non-hidden absolute symbol
4895 if it is not a function, because it might be the version
4896 symbol itself. FIXME: What if it isn't? */
4897 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4899 && (!bfd_is_abs_section (sec
)
4900 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4903 size_t namelen
, verlen
, newlen
;
4906 if (isym
->st_shndx
!= SHN_UNDEF
)
4908 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4910 else if (vernum
> 1)
4912 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4919 /* xgettext:c-format */
4920 (_("%pB: %s: invalid version %u (max %d)"),
4922 elf_tdata (abfd
)->cverdefs
);
4923 bfd_set_error (bfd_error_bad_value
);
4924 goto error_free_vers
;
4929 /* We cannot simply test for the number of
4930 entries in the VERNEED section since the
4931 numbers for the needed versions do not start
4933 Elf_Internal_Verneed
*t
;
4936 for (t
= elf_tdata (abfd
)->verref
;
4940 Elf_Internal_Vernaux
*a
;
4942 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4944 if (a
->vna_other
== vernum
)
4946 verstr
= a
->vna_nodename
;
4956 /* xgettext:c-format */
4957 (_("%pB: %s: invalid needed version %d"),
4958 abfd
, name
, vernum
);
4959 bfd_set_error (bfd_error_bad_value
);
4960 goto error_free_vers
;
4964 namelen
= strlen (name
);
4965 verlen
= strlen (verstr
);
4966 newlen
= namelen
+ verlen
+ 2;
4967 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4968 && isym
->st_shndx
!= SHN_UNDEF
)
4971 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4972 if (newname
== NULL
)
4973 goto error_free_vers
;
4974 memcpy (newname
, name
, namelen
);
4975 p
= newname
+ namelen
;
4977 /* If this is a defined non-hidden version symbol,
4978 we add another @ to the name. This indicates the
4979 default version of the symbol. */
4980 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4981 && isym
->st_shndx
!= SHN_UNDEF
)
4983 memcpy (p
, verstr
, verlen
+ 1);
4988 /* If this symbol has default visibility and the user has
4989 requested we not re-export it, then mark it as hidden. */
4990 if (!bfd_is_und_section (sec
)
4993 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4994 isym
->st_other
= (STV_HIDDEN
4995 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4997 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4998 sym_hash
, &old_bfd
, &old_weak
,
4999 &old_alignment
, &skip
, &override
,
5000 &type_change_ok
, &size_change_ok
,
5002 goto error_free_vers
;
5007 /* Override a definition only if the new symbol matches the
5009 if (override
&& matched
)
5013 while (h
->root
.type
== bfd_link_hash_indirect
5014 || h
->root
.type
== bfd_link_hash_warning
)
5015 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5017 if (h
->versioned
!= unversioned
5018 && elf_tdata (abfd
)->verdef
!= NULL
5021 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
5024 if (! (_bfd_generic_link_add_one_symbol
5025 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
5026 NULL
, false, bed
->collect
,
5027 (struct bfd_link_hash_entry
**) sym_hash
)))
5028 goto error_free_vers
;
5031 /* We need to make sure that indirect symbol dynamic flags are
5034 while (h
->root
.type
== bfd_link_hash_indirect
5035 || h
->root
.type
== bfd_link_hash_warning
)
5036 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5040 /* Setting the index to -3 tells elf_link_output_extsym that
5041 this symbol is defined in a discarded section. */
5042 if (discarded
&& is_elf_hash_table (&htab
->root
))
5045 new_weak
= (flags
& BSF_WEAK
) != 0;
5049 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
5050 && is_elf_hash_table (&htab
->root
)
5051 && h
->u
.alias
== NULL
)
5053 /* Keep a list of all weak defined non function symbols from
5054 a dynamic object, using the alias field. Later in this
5055 function we will set the alias field to the correct
5056 value. We only put non-function symbols from dynamic
5057 objects on this list, because that happens to be the only
5058 time we need to know the normal symbol corresponding to a
5059 weak symbol, and the information is time consuming to
5060 figure out. If the alias field is not already NULL,
5061 then this symbol was already defined by some previous
5062 dynamic object, and we will be using that previous
5063 definition anyhow. */
5069 /* Set the alignment of a common symbol. */
5070 if ((common
|| bfd_is_com_section (sec
))
5071 && h
->root
.type
== bfd_link_hash_common
)
5076 align
= bfd_log2 (isym
->st_value
);
5079 /* The new symbol is a common symbol in a shared object.
5080 We need to get the alignment from the section. */
5081 align
= new_sec
->alignment_power
;
5083 if (align
> old_alignment
)
5084 h
->root
.u
.c
.p
->alignment_power
= align
;
5086 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5089 if (is_elf_hash_table (&htab
->root
))
5091 /* Set a flag in the hash table entry indicating the type of
5092 reference or definition we just found. A dynamic symbol
5093 is one which is referenced or defined by both a regular
5094 object and a shared object. */
5095 bool dynsym
= false;
5097 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5098 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5100 /* Except for this flag to track nonweak references. */
5102 && bind
!= STB_WEAK
)
5103 h
->ref_ir_nonweak
= 1;
5110 if (bind
!= STB_WEAK
)
5111 h
->ref_regular_nonweak
= 1;
5128 hi
->ref_dynamic
= 1;
5133 hi
->def_dynamic
= 1;
5137 /* If an indirect symbol has been forced local, don't
5138 make the real symbol dynamic. */
5139 if (h
!= hi
&& hi
->forced_local
)
5143 if (bfd_link_dll (info
)
5153 && weakdef (h
)->dynindx
!= -1))
5157 /* Check to see if we need to add an indirect symbol for
5158 the default name. */
5160 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5162 && hi
->versioned
== versioned_hidden
))
5163 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5164 sec
, value
, &old_bfd
, &dynsym
))
5165 goto error_free_vers
;
5167 /* Check the alignment when a common symbol is involved. This
5168 can change when a common symbol is overridden by a normal
5169 definition or a common symbol is ignored due to the old
5170 normal definition. We need to make sure the maximum
5171 alignment is maintained. */
5172 if ((old_alignment
|| common
)
5173 && h
->root
.type
!= bfd_link_hash_common
)
5175 unsigned int common_align
;
5176 unsigned int normal_align
;
5177 unsigned int symbol_align
;
5181 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5182 || h
->root
.type
== bfd_link_hash_defweak
);
5184 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5185 if (h
->root
.u
.def
.section
->owner
!= NULL
5186 && (h
->root
.u
.def
.section
->owner
->flags
5187 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5189 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5190 if (normal_align
> symbol_align
)
5191 normal_align
= symbol_align
;
5194 normal_align
= symbol_align
;
5198 common_align
= old_alignment
;
5199 common_bfd
= old_bfd
;
5204 common_align
= bfd_log2 (isym
->st_value
);
5206 normal_bfd
= old_bfd
;
5209 if (normal_align
< common_align
)
5211 /* PR binutils/2735 */
5212 if (normal_bfd
== NULL
)
5214 /* xgettext:c-format */
5215 (_("warning: alignment %u of common symbol `%s' in %pB is"
5216 " greater than the alignment (%u) of its section %pA"),
5217 1 << common_align
, name
, common_bfd
,
5218 1 << normal_align
, h
->root
.u
.def
.section
);
5221 /* xgettext:c-format */
5222 (_("warning: alignment %u of symbol `%s' in %pB"
5223 " is smaller than %u in %pB"),
5224 1 << normal_align
, name
, normal_bfd
,
5225 1 << common_align
, common_bfd
);
5229 /* Remember the symbol size if it isn't undefined. */
5230 if (isym
->st_size
!= 0
5231 && isym
->st_shndx
!= SHN_UNDEF
5232 && (definition
|| h
->size
== 0))
5235 && h
->size
!= isym
->st_size
5236 && ! size_change_ok
)
5238 /* xgettext:c-format */
5239 (_("warning: size of symbol `%s' changed"
5240 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5241 name
, (uint64_t) h
->size
, old_bfd
,
5242 (uint64_t) isym
->st_size
, abfd
);
5244 h
->size
= isym
->st_size
;
5247 /* If this is a common symbol, then we always want H->SIZE
5248 to be the size of the common symbol. The code just above
5249 won't fix the size if a common symbol becomes larger. We
5250 don't warn about a size change here, because that is
5251 covered by --warn-common. Allow changes between different
5253 if (h
->root
.type
== bfd_link_hash_common
)
5254 h
->size
= h
->root
.u
.c
.size
;
5256 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5257 && ((definition
&& !new_weak
)
5258 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5259 || h
->type
== STT_NOTYPE
))
5261 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5263 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5265 if (type
== STT_GNU_IFUNC
5266 && (abfd
->flags
& DYNAMIC
) != 0)
5269 if (h
->type
!= type
)
5271 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5272 /* xgettext:c-format */
5274 (_("warning: type of symbol `%s' changed"
5275 " from %d to %d in %pB"),
5276 name
, h
->type
, type
, abfd
);
5282 /* Merge st_other field. */
5283 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5284 definition
, dynamic
);
5286 /* We don't want to make debug symbol dynamic. */
5288 && (sec
->flags
& SEC_DEBUGGING
)
5289 && !bfd_link_relocatable (info
))
5292 /* Nor should we make plugin symbols dynamic. */
5293 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5298 h
->target_internal
= isym
->st_target_internal
;
5299 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5302 if (definition
&& !dynamic
)
5304 char *p
= strchr (name
, ELF_VER_CHR
);
5305 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5307 /* Queue non-default versions so that .symver x, x@FOO
5308 aliases can be checked. */
5311 size_t amt
= ((isymend
- isym
+ 1)
5312 * sizeof (struct elf_link_hash_entry
*));
5314 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5316 goto error_free_vers
;
5318 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5322 if (dynsym
&& h
->dynindx
== -1)
5324 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5325 goto error_free_vers
;
5327 && weakdef (h
)->dynindx
== -1)
5329 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5330 goto error_free_vers
;
5333 else if (h
->dynindx
!= -1)
5334 /* If the symbol already has a dynamic index, but
5335 visibility says it should not be visible, turn it into
5337 switch (ELF_ST_VISIBILITY (h
->other
))
5341 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
5349 && h
->root
.type
!= bfd_link_hash_indirect
5351 && h
->ref_regular_nonweak
)
5353 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5354 && h
->ref_ir_nonweak
5355 && !info
->lto_all_symbols_read
)
5356 || (h
->ref_dynamic_nonweak
5357 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5358 && !on_needed_list (elf_dt_name (abfd
),
5359 htab
->needed
, NULL
))))
5361 const char *soname
= elf_dt_name (abfd
);
5363 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5364 h
->root
.root
.string
);
5366 /* A symbol from a library loaded via DT_NEEDED of some
5367 other library is referenced by a regular object.
5368 Add a DT_NEEDED entry for it. Issue an error if
5369 --no-add-needed is used and the reference was not
5372 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5375 /* xgettext:c-format */
5376 (_("%pB: undefined reference to symbol '%s'"),
5378 bfd_set_error (bfd_error_missing_dso
);
5379 goto error_free_vers
;
5382 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5383 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5385 /* Create dynamic sections for backends that require
5386 that be done before setup_gnu_properties. */
5387 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5394 if (info
->lto_plugin_active
5395 && !bfd_link_relocatable (info
)
5396 && (abfd
->flags
& BFD_PLUGIN
) == 0
5402 if (bed
->s
->arch_size
== 32)
5407 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5408 referenced in regular objects so that linker plugin will get
5409 the correct symbol resolution. */
5411 sym_hash
= elf_sym_hashes (abfd
);
5412 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5414 Elf_Internal_Rela
*internal_relocs
;
5415 Elf_Internal_Rela
*rel
, *relend
;
5417 /* Don't check relocations in excluded sections. */
5418 if ((s
->flags
& SEC_RELOC
) == 0
5419 || s
->reloc_count
== 0
5420 || (s
->flags
& SEC_EXCLUDE
) != 0
5421 || ((info
->strip
== strip_all
5422 || info
->strip
== strip_debugger
)
5423 && (s
->flags
& SEC_DEBUGGING
) != 0))
5426 internal_relocs
= _bfd_elf_link_info_read_relocs (abfd
, info
,
5429 _bfd_link_keep_memory (info
));
5430 if (internal_relocs
== NULL
)
5431 goto error_free_vers
;
5433 rel
= internal_relocs
;
5434 relend
= rel
+ s
->reloc_count
;
5435 for ( ; rel
< relend
; rel
++)
5437 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5438 struct elf_link_hash_entry
*h
;
5440 /* Skip local symbols. */
5441 if (r_symndx
< extsymoff
)
5444 h
= sym_hash
[r_symndx
- extsymoff
];
5446 h
->root
.non_ir_ref_regular
= 1;
5449 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5450 free (internal_relocs
);
5459 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5463 /* Restore the symbol table. */
5464 old_ent
= (char *) old_tab
+ tabsize
;
5465 memset (elf_sym_hashes (abfd
), 0,
5466 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5467 htab
->root
.table
.table
= old_table
;
5468 htab
->root
.table
.size
= old_size
;
5469 htab
->root
.table
.count
= old_count
;
5470 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5471 htab
->root
.undefs
= old_undefs
;
5472 htab
->root
.undefs_tail
= old_undefs_tail
;
5473 if (htab
->dynstr
!= NULL
)
5474 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5477 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5479 struct bfd_hash_entry
*p
;
5480 struct elf_link_hash_entry
*h
;
5481 unsigned int non_ir_ref_dynamic
;
5483 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5485 /* Preserve non_ir_ref_dynamic so that this symbol
5486 will be exported when the dynamic lib becomes needed
5487 in the second pass. */
5488 h
= (struct elf_link_hash_entry
*) p
;
5489 if (h
->root
.type
== bfd_link_hash_warning
)
5490 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5491 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5493 h
= (struct elf_link_hash_entry
*) p
;
5494 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5495 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5496 if (h
->root
.type
== bfd_link_hash_warning
)
5498 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5499 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5500 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5502 if (h
->root
.type
== bfd_link_hash_common
)
5504 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5505 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5507 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5511 /* Make a special call to the linker "notice" function to
5512 tell it that symbols added for crefs may need to be removed. */
5513 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5514 goto error_free_vers
;
5517 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5519 free (nondeflt_vers
);
5523 if (old_tab
!= NULL
)
5525 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5526 goto error_free_vers
;
5531 /* Now that all the symbols from this input file are created, if
5532 not performing a relocatable link, handle .symver foo, foo@BAR
5533 such that any relocs against foo become foo@BAR. */
5534 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5538 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5540 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5541 char *shortname
, *p
;
5544 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5546 || (h
->root
.type
!= bfd_link_hash_defined
5547 && h
->root
.type
!= bfd_link_hash_defweak
))
5550 amt
= p
- h
->root
.root
.string
;
5551 shortname
= (char *) bfd_malloc (amt
+ 1);
5553 goto error_free_vers
;
5554 memcpy (shortname
, h
->root
.root
.string
, amt
);
5555 shortname
[amt
] = '\0';
5557 hi
= (struct elf_link_hash_entry
*)
5558 bfd_link_hash_lookup (&htab
->root
, shortname
,
5559 false, false, false);
5561 && hi
->root
.type
== h
->root
.type
5562 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5563 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5565 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
5566 hi
->root
.type
= bfd_link_hash_indirect
;
5567 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5568 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5569 sym_hash
= elf_sym_hashes (abfd
);
5571 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5572 if (sym_hash
[symidx
] == hi
)
5574 sym_hash
[symidx
] = h
;
5580 free (nondeflt_vers
);
5581 nondeflt_vers
= NULL
;
5584 /* Now set the alias field correctly for all the weak defined
5585 symbols we found. The only way to do this is to search all the
5586 symbols. Since we only need the information for non functions in
5587 dynamic objects, that's the only time we actually put anything on
5588 the list WEAKS. We need this information so that if a regular
5589 object refers to a symbol defined weakly in a dynamic object, the
5590 real symbol in the dynamic object is also put in the dynamic
5591 symbols; we also must arrange for both symbols to point to the
5592 same memory location. We could handle the general case of symbol
5593 aliasing, but a general symbol alias can only be generated in
5594 assembler code, handling it correctly would be very time
5595 consuming, and other ELF linkers don't handle general aliasing
5599 struct elf_link_hash_entry
**hpp
;
5600 struct elf_link_hash_entry
**hppend
;
5601 struct elf_link_hash_entry
**sorted_sym_hash
;
5602 struct elf_link_hash_entry
*h
;
5603 size_t sym_count
, amt
;
5605 /* Since we have to search the whole symbol list for each weak
5606 defined symbol, search time for N weak defined symbols will be
5607 O(N^2). Binary search will cut it down to O(NlogN). */
5608 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5609 sorted_sym_hash
= bfd_malloc (amt
);
5610 if (sorted_sym_hash
== NULL
)
5612 sym_hash
= sorted_sym_hash
;
5613 hpp
= elf_sym_hashes (abfd
);
5614 hppend
= hpp
+ extsymcount
;
5616 for (; hpp
< hppend
; hpp
++)
5620 && h
->root
.type
== bfd_link_hash_defined
5621 && !bed
->is_function_type (h
->type
))
5629 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5632 while (weaks
!= NULL
)
5634 struct elf_link_hash_entry
*hlook
;
5637 size_t i
, j
, idx
= 0;
5640 weaks
= hlook
->u
.alias
;
5641 hlook
->u
.alias
= NULL
;
5643 if (hlook
->root
.type
!= bfd_link_hash_defined
5644 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5647 slook
= hlook
->root
.u
.def
.section
;
5648 vlook
= hlook
->root
.u
.def
.value
;
5654 bfd_signed_vma vdiff
;
5656 h
= sorted_sym_hash
[idx
];
5657 vdiff
= vlook
- h
->root
.u
.def
.value
;
5664 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5674 /* We didn't find a value/section match. */
5678 /* With multiple aliases, or when the weak symbol is already
5679 strongly defined, we have multiple matching symbols and
5680 the binary search above may land on any of them. Step
5681 one past the matching symbol(s). */
5684 h
= sorted_sym_hash
[idx
];
5685 if (h
->root
.u
.def
.section
!= slook
5686 || h
->root
.u
.def
.value
!= vlook
)
5690 /* Now look back over the aliases. Since we sorted by size
5691 as well as value and section, we'll choose the one with
5692 the largest size. */
5695 h
= sorted_sym_hash
[idx
];
5697 /* Stop if value or section doesn't match. */
5698 if (h
->root
.u
.def
.section
!= slook
5699 || h
->root
.u
.def
.value
!= vlook
)
5701 else if (h
!= hlook
)
5703 struct elf_link_hash_entry
*t
;
5706 hlook
->is_weakalias
= 1;
5708 if (t
->u
.alias
!= NULL
)
5709 while (t
->u
.alias
!= h
)
5713 /* If the weak definition is in the list of dynamic
5714 symbols, make sure the real definition is put
5716 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5718 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5721 free (sorted_sym_hash
);
5726 /* If the real definition is in the list of dynamic
5727 symbols, make sure the weak definition is put
5728 there as well. If we don't do this, then the
5729 dynamic loader might not merge the entries for the
5730 real definition and the weak definition. */
5731 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5733 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5734 goto err_free_sym_hash
;
5741 free (sorted_sym_hash
);
5744 if (bed
->check_directives
5745 && !(*bed
->check_directives
) (abfd
, info
))
5748 /* If this is a non-traditional link, try to optimize the handling
5749 of the .stab/.stabstr sections. */
5751 && ! info
->traditional_format
5752 && is_elf_hash_table (&htab
->root
)
5753 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5757 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5758 if (stabstr
!= NULL
)
5760 bfd_size_type string_offset
= 0;
5763 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5764 if (startswith (stab
->name
, ".stab")
5765 && (!stab
->name
[5] ||
5766 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5767 && (stab
->flags
& SEC_MERGE
) == 0
5768 && !bfd_is_abs_section (stab
->output_section
))
5770 struct bfd_elf_section_data
*secdata
;
5772 secdata
= elf_section_data (stab
);
5773 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5774 stabstr
, &secdata
->sec_info
,
5777 if (secdata
->sec_info
)
5778 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5783 if (dynamic
&& add_needed
)
5785 /* Add this bfd to the loaded list. */
5786 struct elf_link_loaded_list
*n
;
5788 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5792 n
->next
= htab
->dyn_loaded
;
5793 htab
->dyn_loaded
= n
;
5795 if (dynamic
&& !add_needed
5796 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
5797 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
5804 free (nondeflt_vers
);
5812 /* Return the linker hash table entry of a symbol that might be
5813 satisfied by an archive symbol. Return -1 on error. */
5815 struct bfd_link_hash_entry
*
5816 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5817 struct bfd_link_info
*info
,
5820 struct bfd_link_hash_entry
*h
;
5824 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
5828 /* If this is a default version (the name contains @@), look up the
5829 symbol again with only one `@' as well as without the version.
5830 The effect is that references to the symbol with and without the
5831 version will be matched by the default symbol in the archive. */
5833 p
= strchr (name
, ELF_VER_CHR
);
5834 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5837 /* First check with only one `@'. */
5838 len
= strlen (name
);
5839 copy
= (char *) bfd_alloc (abfd
, len
);
5841 return (struct bfd_link_hash_entry
*) -1;
5843 first
= p
- name
+ 1;
5844 memcpy (copy
, name
, first
);
5845 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5847 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5850 /* We also need to check references to the symbol without the
5852 copy
[first
- 1] = '\0';
5853 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
5856 bfd_release (abfd
, copy
);
5860 /* Add symbols from an ELF archive file to the linker hash table. We
5861 don't use _bfd_generic_link_add_archive_symbols because we need to
5862 handle versioned symbols.
5864 Fortunately, ELF archive handling is simpler than that done by
5865 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5866 oddities. In ELF, if we find a symbol in the archive map, and the
5867 symbol is currently undefined, we know that we must pull in that
5870 Unfortunately, we do have to make multiple passes over the symbol
5871 table until nothing further is resolved. */
5874 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5877 unsigned char *included
= NULL
;
5881 const struct elf_backend_data
*bed
;
5882 struct bfd_link_hash_entry
* (*archive_symbol_lookup
)
5883 (bfd
*, struct bfd_link_info
*, const char *);
5885 if (! bfd_has_map (abfd
))
5887 /* An empty archive is a special case. */
5888 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5890 bfd_set_error (bfd_error_no_armap
);
5894 /* Keep track of all symbols we know to be already defined, and all
5895 files we know to be already included. This is to speed up the
5896 second and subsequent passes. */
5897 c
= bfd_ardata (abfd
)->symdef_count
;
5900 amt
= c
* sizeof (*included
);
5901 included
= (unsigned char *) bfd_zmalloc (amt
);
5902 if (included
== NULL
)
5905 symdefs
= bfd_ardata (abfd
)->symdefs
;
5906 bed
= get_elf_backend_data (abfd
);
5907 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5920 symdefend
= symdef
+ c
;
5921 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5923 struct bfd_link_hash_entry
*h
;
5925 struct bfd_link_hash_entry
*undefs_tail
;
5930 if (symdef
->file_offset
== last
)
5936 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5937 if (h
== (struct bfd_link_hash_entry
*) -1)
5943 if (h
->type
== bfd_link_hash_undefined
)
5945 /* If the archive element has already been loaded then one
5946 of the symbols defined by that element might have been
5947 made undefined due to being in a discarded section. */
5948 if (is_elf_hash_table (info
->hash
)
5949 && ((struct elf_link_hash_entry
*) h
)->indx
== -3)
5952 else if (h
->type
== bfd_link_hash_common
)
5954 /* We currently have a common symbol. The archive map contains
5955 a reference to this symbol, so we may want to include it. We
5956 only want to include it however, if this archive element
5957 contains a definition of the symbol, not just another common
5960 Unfortunately some archivers (including GNU ar) will put
5961 declarations of common symbols into their archive maps, as
5962 well as real definitions, so we cannot just go by the archive
5963 map alone. Instead we must read in the element's symbol
5964 table and check that to see what kind of symbol definition
5966 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5971 if (h
->type
!= bfd_link_hash_undefweak
)
5972 /* Symbol must be defined. Don't check it again. */
5977 /* We need to include this archive member. */
5978 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
,
5980 if (element
== NULL
)
5983 if (! bfd_check_format (element
, bfd_object
))
5986 undefs_tail
= info
->hash
->undefs_tail
;
5988 if (!(*info
->callbacks
5989 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5991 if (!bfd_link_add_symbols (element
, info
))
5994 /* If there are any new undefined symbols, we need to make
5995 another pass through the archive in order to see whether
5996 they can be defined. FIXME: This isn't perfect, because
5997 common symbols wind up on undefs_tail and because an
5998 undefined symbol which is defined later on in this pass
5999 does not require another pass. This isn't a bug, but it
6000 does make the code less efficient than it could be. */
6001 if (undefs_tail
!= info
->hash
->undefs_tail
)
6004 /* Look backward to mark all symbols from this object file
6005 which we have already seen in this pass. */
6009 included
[mark
] = true;
6014 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
6016 /* We mark subsequent symbols from this object file as we go
6017 on through the loop. */
6018 last
= symdef
->file_offset
;
6031 /* Given an ELF BFD, add symbols to the global hash table as
6035 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6037 switch (bfd_get_format (abfd
))
6040 return elf_link_add_object_symbols (abfd
, info
);
6042 return elf_link_add_archive_symbols (abfd
, info
);
6044 bfd_set_error (bfd_error_wrong_format
);
6049 struct hash_codes_info
6051 unsigned long *hashcodes
;
6055 /* This function will be called though elf_link_hash_traverse to store
6056 all hash value of the exported symbols in an array. */
6059 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6061 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
6066 /* Ignore indirect symbols. These are added by the versioning code. */
6067 if (h
->dynindx
== -1)
6070 name
= h
->root
.root
.string
;
6071 if (h
->versioned
>= versioned
)
6073 char *p
= strchr (name
, ELF_VER_CHR
);
6076 alc
= (char *) bfd_malloc (p
- name
+ 1);
6082 memcpy (alc
, name
, p
- name
);
6083 alc
[p
- name
] = '\0';
6088 /* Compute the hash value. */
6089 ha
= bfd_elf_hash (name
);
6091 /* Store the found hash value in the array given as the argument. */
6092 *(inf
->hashcodes
)++ = ha
;
6094 /* And store it in the struct so that we can put it in the hash table
6096 h
->u
.elf_hash_value
= ha
;
6102 struct collect_gnu_hash_codes
6105 const struct elf_backend_data
*bed
;
6106 unsigned long int nsyms
;
6107 unsigned long int maskbits
;
6108 unsigned long int *hashcodes
;
6109 unsigned long int *hashval
;
6110 unsigned long int *indx
;
6111 unsigned long int *counts
;
6115 long int min_dynindx
;
6116 unsigned long int bucketcount
;
6117 unsigned long int symindx
;
6118 long int local_indx
;
6119 long int shift1
, shift2
;
6120 unsigned long int mask
;
6124 /* This function will be called though elf_link_hash_traverse to store
6125 all hash value of the exported symbols in an array. */
6128 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6130 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6135 /* Ignore indirect symbols. These are added by the versioning code. */
6136 if (h
->dynindx
== -1)
6139 /* Ignore also local symbols and undefined symbols. */
6140 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6143 name
= h
->root
.root
.string
;
6144 if (h
->versioned
>= versioned
)
6146 char *p
= strchr (name
, ELF_VER_CHR
);
6149 alc
= (char *) bfd_malloc (p
- name
+ 1);
6155 memcpy (alc
, name
, p
- name
);
6156 alc
[p
- name
] = '\0';
6161 /* Compute the hash value. */
6162 ha
= bfd_elf_gnu_hash (name
);
6164 /* Store the found hash value in the array for compute_bucket_count,
6165 and also for .dynsym reordering purposes. */
6166 s
->hashcodes
[s
->nsyms
] = ha
;
6167 s
->hashval
[h
->dynindx
] = ha
;
6169 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6170 s
->min_dynindx
= h
->dynindx
;
6176 /* This function will be called though elf_link_hash_traverse to do
6177 final dynamic symbol renumbering in case of .gnu.hash.
6178 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6179 to the translation table. */
6182 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6184 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6185 unsigned long int bucket
;
6186 unsigned long int val
;
6188 /* Ignore indirect symbols. */
6189 if (h
->dynindx
== -1)
6192 /* Ignore also local symbols and undefined symbols. */
6193 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6195 if (h
->dynindx
>= s
->min_dynindx
)
6197 if (s
->bed
->record_xhash_symbol
!= NULL
)
6199 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6203 h
->dynindx
= s
->local_indx
++;
6208 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6209 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6210 & ((s
->maskbits
>> s
->shift1
) - 1);
6211 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6213 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6214 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6215 if (s
->counts
[bucket
] == 1)
6216 /* Last element terminates the chain. */
6218 bfd_put_32 (s
->output_bfd
, val
,
6219 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6220 --s
->counts
[bucket
];
6221 if (s
->bed
->record_xhash_symbol
!= NULL
)
6223 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6225 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6228 h
->dynindx
= s
->indx
[bucket
]++;
6232 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6235 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6237 return !(h
->forced_local
6238 || h
->root
.type
== bfd_link_hash_undefined
6239 || h
->root
.type
== bfd_link_hash_undefweak
6240 || ((h
->root
.type
== bfd_link_hash_defined
6241 || h
->root
.type
== bfd_link_hash_defweak
)
6242 && h
->root
.u
.def
.section
->output_section
== NULL
));
6245 /* Array used to determine the number of hash table buckets to use
6246 based on the number of symbols there are. If there are fewer than
6247 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6248 fewer than 37 we use 17 buckets, and so forth. We never use more
6249 than 32771 buckets. */
6251 static const size_t elf_buckets
[] =
6253 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6257 /* Compute bucket count for hashing table. We do not use a static set
6258 of possible tables sizes anymore. Instead we determine for all
6259 possible reasonable sizes of the table the outcome (i.e., the
6260 number of collisions etc) and choose the best solution. The
6261 weighting functions are not too simple to allow the table to grow
6262 without bounds. Instead one of the weighting factors is the size.
6263 Therefore the result is always a good payoff between few collisions
6264 (= short chain lengths) and table size. */
6266 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6267 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6268 unsigned long int nsyms
,
6271 size_t best_size
= 0;
6272 unsigned long int i
;
6274 /* We have a problem here. The following code to optimize the table
6275 size requires an integer type with more the 32 bits. If
6276 BFD_HOST_U_64_BIT is set we know about such a type. */
6277 #ifdef BFD_HOST_U_64_BIT
6282 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
6283 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6284 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6285 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6286 unsigned long int *counts
;
6288 unsigned int no_improvement_count
= 0;
6290 /* Possible optimization parameters: if we have NSYMS symbols we say
6291 that the hashing table must at least have NSYMS/4 and at most
6293 minsize
= nsyms
/ 4;
6296 best_size
= maxsize
= nsyms
* 2;
6301 if ((best_size
& 31) == 0)
6305 /* Create array where we count the collisions in. We must use bfd_malloc
6306 since the size could be large. */
6308 amt
*= sizeof (unsigned long int);
6309 counts
= (unsigned long int *) bfd_malloc (amt
);
6313 /* Compute the "optimal" size for the hash table. The criteria is a
6314 minimal chain length. The minor criteria is (of course) the size
6316 for (i
= minsize
; i
< maxsize
; ++i
)
6318 /* Walk through the array of hashcodes and count the collisions. */
6319 BFD_HOST_U_64_BIT max
;
6320 unsigned long int j
;
6321 unsigned long int fact
;
6323 if (gnu_hash
&& (i
& 31) == 0)
6326 memset (counts
, '\0', i
* sizeof (unsigned long int));
6328 /* Determine how often each hash bucket is used. */
6329 for (j
= 0; j
< nsyms
; ++j
)
6330 ++counts
[hashcodes
[j
] % i
];
6332 /* For the weight function we need some information about the
6333 pagesize on the target. This is information need not be 100%
6334 accurate. Since this information is not available (so far) we
6335 define it here to a reasonable default value. If it is crucial
6336 to have a better value some day simply define this value. */
6337 # ifndef BFD_TARGET_PAGESIZE
6338 # define BFD_TARGET_PAGESIZE (4096)
6341 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6343 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6346 /* Variant 1: optimize for short chains. We add the squares
6347 of all the chain lengths (which favors many small chain
6348 over a few long chains). */
6349 for (j
= 0; j
< i
; ++j
)
6350 max
+= counts
[j
] * counts
[j
];
6352 /* This adds penalties for the overall size of the table. */
6353 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6356 /* Variant 2: Optimize a lot more for small table. Here we
6357 also add squares of the size but we also add penalties for
6358 empty slots (the +1 term). */
6359 for (j
= 0; j
< i
; ++j
)
6360 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6362 /* The overall size of the table is considered, but not as
6363 strong as in variant 1, where it is squared. */
6364 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6368 /* Compare with current best results. */
6369 if (max
< best_chlen
)
6373 no_improvement_count
= 0;
6375 /* PR 11843: Avoid futile long searches for the best bucket size
6376 when there are a large number of symbols. */
6377 else if (++no_improvement_count
== 100)
6384 #endif /* defined (BFD_HOST_U_64_BIT) */
6386 /* This is the fallback solution if no 64bit type is available or if we
6387 are not supposed to spend much time on optimizations. We select the
6388 bucket count using a fixed set of numbers. */
6389 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6391 best_size
= elf_buckets
[i
];
6392 if (nsyms
< elf_buckets
[i
+ 1])
6395 if (gnu_hash
&& best_size
< 2)
6402 /* Size any SHT_GROUP section for ld -r. */
6405 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6410 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6411 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6412 && (s
= ibfd
->sections
) != NULL
6413 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6414 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6419 /* Set a default stack segment size. The value in INFO wins. If it
6420 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6421 undefined it is initialized. */
6424 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6425 struct bfd_link_info
*info
,
6426 const char *legacy_symbol
,
6427 bfd_vma default_size
)
6429 struct elf_link_hash_entry
*h
= NULL
;
6431 /* Look for legacy symbol. */
6433 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6434 false, false, false);
6435 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6436 || h
->root
.type
== bfd_link_hash_defweak
)
6438 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6440 /* The symbol has no type if specified on the command line. */
6441 h
->type
= STT_OBJECT
;
6442 if (info
->stacksize
)
6443 /* xgettext:c-format */
6444 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6445 output_bfd
, legacy_symbol
);
6446 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6447 /* xgettext:c-format */
6448 _bfd_error_handler (_("%pB: %s not absolute"),
6449 output_bfd
, legacy_symbol
);
6451 info
->stacksize
= h
->root
.u
.def
.value
;
6454 if (!info
->stacksize
)
6455 /* If the user didn't set a size, or explicitly inhibit the
6456 size, set it now. */
6457 info
->stacksize
= default_size
;
6459 /* Provide the legacy symbol, if it is referenced. */
6460 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6461 || h
->root
.type
== bfd_link_hash_undefweak
))
6463 struct bfd_link_hash_entry
*bh
= NULL
;
6465 if (!(_bfd_generic_link_add_one_symbol
6466 (info
, output_bfd
, legacy_symbol
,
6467 BSF_GLOBAL
, bfd_abs_section_ptr
,
6468 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6469 NULL
, false, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6472 h
= (struct elf_link_hash_entry
*) bh
;
6474 h
->type
= STT_OBJECT
;
6480 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6482 struct elf_gc_sweep_symbol_info
6484 struct bfd_link_info
*info
;
6485 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6490 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6493 && (((h
->root
.type
== bfd_link_hash_defined
6494 || h
->root
.type
== bfd_link_hash_defweak
)
6495 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6496 && h
->root
.u
.def
.section
->gc_mark
))
6497 || h
->root
.type
== bfd_link_hash_undefined
6498 || h
->root
.type
== bfd_link_hash_undefweak
))
6500 struct elf_gc_sweep_symbol_info
*inf
;
6502 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6503 (*inf
->hide_symbol
) (inf
->info
, h
, true);
6506 h
->ref_regular_nonweak
= 0;
6512 /* Set up the sizes and contents of the ELF dynamic sections. This is
6513 called by the ELF linker emulation before_allocation routine. We
6514 must set the sizes of the sections before the linker sets the
6515 addresses of the various sections. */
6518 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6521 const char *filter_shlib
,
6523 const char *depaudit
,
6524 const char * const *auxiliary_filters
,
6525 struct bfd_link_info
*info
,
6526 asection
**sinterpptr
)
6529 const struct elf_backend_data
*bed
;
6533 if (!is_elf_hash_table (info
->hash
))
6536 /* Any syms created from now on start with -1 in
6537 got.refcount/offset and plt.refcount/offset. */
6538 elf_hash_table (info
)->init_got_refcount
6539 = elf_hash_table (info
)->init_got_offset
;
6540 elf_hash_table (info
)->init_plt_refcount
6541 = elf_hash_table (info
)->init_plt_offset
;
6543 bed
= get_elf_backend_data (output_bfd
);
6545 /* The backend may have to create some sections regardless of whether
6546 we're dynamic or not. */
6547 if (bed
->elf_backend_always_size_sections
6548 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
6551 dynobj
= elf_hash_table (info
)->dynobj
;
6553 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6555 struct bfd_elf_version_tree
*verdefs
;
6556 struct elf_info_failed asvinfo
;
6557 struct bfd_elf_version_tree
*t
;
6558 struct bfd_elf_version_expr
*d
;
6562 /* If we are supposed to export all symbols into the dynamic symbol
6563 table (this is not the normal case), then do so. */
6564 if (info
->export_dynamic
6565 || (bfd_link_executable (info
) && info
->dynamic
))
6567 struct elf_info_failed eif
;
6571 elf_link_hash_traverse (elf_hash_table (info
),
6572 _bfd_elf_export_symbol
,
6580 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6582 if (soname_indx
== (size_t) -1
6583 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6587 soname_indx
= (size_t) -1;
6589 /* Make all global versions with definition. */
6590 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6591 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6592 if (!d
->symver
&& d
->literal
)
6594 const char *verstr
, *name
;
6595 size_t namelen
, verlen
, newlen
;
6596 char *newname
, *p
, leading_char
;
6597 struct elf_link_hash_entry
*newh
;
6599 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6601 namelen
= strlen (name
) + (leading_char
!= '\0');
6603 verlen
= strlen (verstr
);
6604 newlen
= namelen
+ verlen
+ 3;
6606 newname
= (char *) bfd_malloc (newlen
);
6607 if (newname
== NULL
)
6609 newname
[0] = leading_char
;
6610 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6612 /* Check the hidden versioned definition. */
6613 p
= newname
+ namelen
;
6615 memcpy (p
, verstr
, verlen
+ 1);
6616 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6617 newname
, false, false,
6620 || (newh
->root
.type
!= bfd_link_hash_defined
6621 && newh
->root
.type
!= bfd_link_hash_defweak
))
6623 /* Check the default versioned definition. */
6625 memcpy (p
, verstr
, verlen
+ 1);
6626 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6627 newname
, false, false,
6632 /* Mark this version if there is a definition and it is
6633 not defined in a shared object. */
6635 && !newh
->def_dynamic
6636 && (newh
->root
.type
== bfd_link_hash_defined
6637 || newh
->root
.type
== bfd_link_hash_defweak
))
6641 /* Attach all the symbols to their version information. */
6642 asvinfo
.info
= info
;
6643 asvinfo
.failed
= false;
6645 elf_link_hash_traverse (elf_hash_table (info
),
6646 _bfd_elf_link_assign_sym_version
,
6651 if (!info
->allow_undefined_version
)
6653 /* Check if all global versions have a definition. */
6654 bool all_defined
= true;
6655 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6656 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6657 if (d
->literal
&& !d
->symver
&& !d
->script
)
6660 (_("%s: undefined version: %s"),
6661 d
->pattern
, t
->name
);
6662 all_defined
= false;
6667 bfd_set_error (bfd_error_bad_value
);
6672 /* Set up the version definition section. */
6673 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6674 BFD_ASSERT (s
!= NULL
);
6676 /* We may have created additional version definitions if we are
6677 just linking a regular application. */
6678 verdefs
= info
->version_info
;
6680 /* Skip anonymous version tag. */
6681 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6682 verdefs
= verdefs
->next
;
6684 if (verdefs
== NULL
&& !info
->create_default_symver
)
6685 s
->flags
|= SEC_EXCLUDE
;
6691 Elf_Internal_Verdef def
;
6692 Elf_Internal_Verdaux defaux
;
6693 struct bfd_link_hash_entry
*bh
;
6694 struct elf_link_hash_entry
*h
;
6700 /* Make space for the base version. */
6701 size
+= sizeof (Elf_External_Verdef
);
6702 size
+= sizeof (Elf_External_Verdaux
);
6705 /* Make space for the default version. */
6706 if (info
->create_default_symver
)
6708 size
+= sizeof (Elf_External_Verdef
);
6712 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6714 struct bfd_elf_version_deps
*n
;
6716 /* Don't emit base version twice. */
6720 size
+= sizeof (Elf_External_Verdef
);
6721 size
+= sizeof (Elf_External_Verdaux
);
6724 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6725 size
+= sizeof (Elf_External_Verdaux
);
6729 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6730 if (s
->contents
== NULL
&& s
->size
!= 0)
6733 /* Fill in the version definition section. */
6737 def
.vd_version
= VER_DEF_CURRENT
;
6738 def
.vd_flags
= VER_FLG_BASE
;
6741 if (info
->create_default_symver
)
6743 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6744 def
.vd_next
= sizeof (Elf_External_Verdef
);
6748 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6749 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6750 + sizeof (Elf_External_Verdaux
));
6753 if (soname_indx
!= (size_t) -1)
6755 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6757 def
.vd_hash
= bfd_elf_hash (soname
);
6758 defaux
.vda_name
= soname_indx
;
6765 name
= lbasename (bfd_get_filename (output_bfd
));
6766 def
.vd_hash
= bfd_elf_hash (name
);
6767 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6769 if (indx
== (size_t) -1)
6771 defaux
.vda_name
= indx
;
6773 defaux
.vda_next
= 0;
6775 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6776 (Elf_External_Verdef
*) p
);
6777 p
+= sizeof (Elf_External_Verdef
);
6778 if (info
->create_default_symver
)
6780 /* Add a symbol representing this version. */
6782 if (! (_bfd_generic_link_add_one_symbol
6783 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6785 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6787 h
= (struct elf_link_hash_entry
*) bh
;
6790 h
->type
= STT_OBJECT
;
6791 h
->verinfo
.vertree
= NULL
;
6793 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6796 /* Create a duplicate of the base version with the same
6797 aux block, but different flags. */
6800 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6802 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6803 + sizeof (Elf_External_Verdaux
));
6806 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6807 (Elf_External_Verdef
*) p
);
6808 p
+= sizeof (Elf_External_Verdef
);
6810 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6811 (Elf_External_Verdaux
*) p
);
6812 p
+= sizeof (Elf_External_Verdaux
);
6814 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6817 struct bfd_elf_version_deps
*n
;
6819 /* Don't emit the base version twice. */
6824 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6827 /* Add a symbol representing this version. */
6829 if (! (_bfd_generic_link_add_one_symbol
6830 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6832 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6834 h
= (struct elf_link_hash_entry
*) bh
;
6837 h
->type
= STT_OBJECT
;
6838 h
->verinfo
.vertree
= t
;
6840 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6843 def
.vd_version
= VER_DEF_CURRENT
;
6845 if (t
->globals
.list
== NULL
6846 && t
->locals
.list
== NULL
6848 def
.vd_flags
|= VER_FLG_WEAK
;
6849 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6850 def
.vd_cnt
= cdeps
+ 1;
6851 def
.vd_hash
= bfd_elf_hash (t
->name
);
6852 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6855 /* If a basever node is next, it *must* be the last node in
6856 the chain, otherwise Verdef construction breaks. */
6857 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6858 BFD_ASSERT (t
->next
->next
== NULL
);
6860 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6861 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6862 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6864 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6865 (Elf_External_Verdef
*) p
);
6866 p
+= sizeof (Elf_External_Verdef
);
6868 defaux
.vda_name
= h
->dynstr_index
;
6869 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6871 defaux
.vda_next
= 0;
6872 if (t
->deps
!= NULL
)
6873 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6874 t
->name_indx
= defaux
.vda_name
;
6876 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6877 (Elf_External_Verdaux
*) p
);
6878 p
+= sizeof (Elf_External_Verdaux
);
6880 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6882 if (n
->version_needed
== NULL
)
6884 /* This can happen if there was an error in the
6886 defaux
.vda_name
= 0;
6890 defaux
.vda_name
= n
->version_needed
->name_indx
;
6891 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6894 if (n
->next
== NULL
)
6895 defaux
.vda_next
= 0;
6897 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6899 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6900 (Elf_External_Verdaux
*) p
);
6901 p
+= sizeof (Elf_External_Verdaux
);
6905 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6909 if (info
->gc_sections
&& bed
->can_gc_sections
)
6911 struct elf_gc_sweep_symbol_info sweep_info
;
6913 /* Remove the symbols that were in the swept sections from the
6914 dynamic symbol table. */
6915 sweep_info
.info
= info
;
6916 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
6917 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
6921 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6924 struct elf_find_verdep_info sinfo
;
6926 /* Work out the size of the version reference section. */
6928 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6929 BFD_ASSERT (s
!= NULL
);
6932 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6933 if (sinfo
.vers
== 0)
6935 sinfo
.failed
= false;
6937 elf_link_hash_traverse (elf_hash_table (info
),
6938 _bfd_elf_link_find_version_dependencies
,
6943 if (elf_tdata (output_bfd
)->verref
== NULL
)
6944 s
->flags
|= SEC_EXCLUDE
;
6947 Elf_Internal_Verneed
*vn
;
6952 /* Build the version dependency section. */
6955 for (vn
= elf_tdata (output_bfd
)->verref
;
6957 vn
= vn
->vn_nextref
)
6959 Elf_Internal_Vernaux
*a
;
6961 size
+= sizeof (Elf_External_Verneed
);
6963 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6964 size
+= sizeof (Elf_External_Vernaux
);
6968 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6969 if (s
->contents
== NULL
)
6973 for (vn
= elf_tdata (output_bfd
)->verref
;
6975 vn
= vn
->vn_nextref
)
6978 Elf_Internal_Vernaux
*a
;
6982 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6985 vn
->vn_version
= VER_NEED_CURRENT
;
6987 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6988 elf_dt_name (vn
->vn_bfd
) != NULL
6989 ? elf_dt_name (vn
->vn_bfd
)
6990 : lbasename (bfd_get_filename
6993 if (indx
== (size_t) -1)
6996 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
6997 if (vn
->vn_nextref
== NULL
)
7000 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
7001 + caux
* sizeof (Elf_External_Vernaux
));
7003 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
7004 (Elf_External_Verneed
*) p
);
7005 p
+= sizeof (Elf_External_Verneed
);
7007 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7009 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
7010 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7011 a
->vna_nodename
, false);
7012 if (indx
== (size_t) -1)
7015 if (a
->vna_nextptr
== NULL
)
7018 a
->vna_next
= sizeof (Elf_External_Vernaux
);
7020 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
7021 (Elf_External_Vernaux
*) p
);
7022 p
+= sizeof (Elf_External_Vernaux
);
7026 elf_tdata (output_bfd
)->cverrefs
= crefs
;
7030 if (bfd_link_relocatable (info
)
7031 && !_bfd_elf_size_group_sections (info
))
7034 /* Determine any GNU_STACK segment requirements, after the backend
7035 has had a chance to set a default segment size. */
7036 if (info
->execstack
)
7037 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
7038 else if (info
->noexecstack
)
7039 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
7043 asection
*notesec
= NULL
;
7046 for (inputobj
= info
->input_bfds
;
7048 inputobj
= inputobj
->link
.next
)
7053 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
7055 s
= inputobj
->sections
;
7056 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
7059 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
7062 if (s
->flags
& SEC_CODE
)
7066 else if (bed
->default_execstack
)
7069 if (notesec
|| info
->stacksize
> 0)
7070 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
7071 if (notesec
&& exec
&& bfd_link_relocatable (info
)
7072 && notesec
->output_section
!= bfd_abs_section_ptr
)
7073 notesec
->output_section
->flags
|= SEC_CODE
;
7076 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7078 struct elf_info_failed eif
;
7079 struct elf_link_hash_entry
*h
;
7083 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7084 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7088 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7090 info
->flags
|= DF_SYMBOLIC
;
7098 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7100 if (indx
== (size_t) -1)
7103 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7104 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7108 if (filter_shlib
!= NULL
)
7112 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7113 filter_shlib
, true);
7114 if (indx
== (size_t) -1
7115 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7119 if (auxiliary_filters
!= NULL
)
7121 const char * const *p
;
7123 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7127 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7129 if (indx
== (size_t) -1
7130 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7139 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7141 if (indx
== (size_t) -1
7142 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7146 if (depaudit
!= NULL
)
7150 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7152 if (indx
== (size_t) -1
7153 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7160 /* Find all symbols which were defined in a dynamic object and make
7161 the backend pick a reasonable value for them. */
7162 elf_link_hash_traverse (elf_hash_table (info
),
7163 _bfd_elf_adjust_dynamic_symbol
,
7168 /* Add some entries to the .dynamic section. We fill in some of the
7169 values later, in bfd_elf_final_link, but we must add the entries
7170 now so that we know the final size of the .dynamic section. */
7172 /* If there are initialization and/or finalization functions to
7173 call then add the corresponding DT_INIT/DT_FINI entries. */
7174 h
= (info
->init_function
7175 ? elf_link_hash_lookup (elf_hash_table (info
),
7176 info
->init_function
, false,
7183 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7186 h
= (info
->fini_function
7187 ? elf_link_hash_lookup (elf_hash_table (info
),
7188 info
->fini_function
, false,
7195 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7199 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7200 if (s
!= NULL
&& s
->linker_has_input
)
7202 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7203 if (! bfd_link_executable (info
))
7208 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7209 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7210 && (o
= sub
->sections
) != NULL
7211 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7212 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7213 if (elf_section_data (o
)->this_hdr
.sh_type
7214 == SHT_PREINIT_ARRAY
)
7217 (_("%pB: .preinit_array section is not allowed in DSO"),
7222 bfd_set_error (bfd_error_nonrepresentable_section
);
7226 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7227 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7230 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7231 if (s
!= NULL
&& s
->linker_has_input
)
7233 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7234 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7237 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7238 if (s
!= NULL
&& s
->linker_has_input
)
7240 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7241 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7245 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7246 /* If .dynstr is excluded from the link, we don't want any of
7247 these tags. Strictly, we should be checking each section
7248 individually; This quick check covers for the case where
7249 someone does a /DISCARD/ : { *(*) }. */
7250 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7252 bfd_size_type strsize
;
7254 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7255 if ((info
->emit_hash
7256 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7257 || (info
->emit_gnu_hash
7258 && (bed
->record_xhash_symbol
== NULL
7259 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7260 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7261 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7262 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7263 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7265 || (info
->gnu_flags_1
7266 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_FLAGS_1
,
7267 info
->gnu_flags_1
)))
7272 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7275 /* The backend must work out the sizes of all the other dynamic
7278 && bed
->elf_backend_size_dynamic_sections
!= NULL
7279 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
7282 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7284 if (elf_tdata (output_bfd
)->cverdefs
)
7286 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7288 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7289 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7293 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7295 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7298 else if (info
->flags
& DF_BIND_NOW
)
7300 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7306 if (bfd_link_executable (info
))
7307 info
->flags_1
&= ~ (DF_1_INITFIRST
7310 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7314 if (elf_tdata (output_bfd
)->cverrefs
)
7316 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7318 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7319 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7323 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7324 && elf_tdata (output_bfd
)->cverdefs
== 0)
7325 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7329 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7330 s
->flags
|= SEC_EXCLUDE
;
7336 /* Find the first non-excluded output section. We'll use its
7337 section symbol for some emitted relocs. */
7339 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7342 asection
*found
= NULL
;
7344 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7345 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7346 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7349 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7352 elf_hash_table (info
)->text_index_section
= found
;
7355 /* Find two non-excluded output sections, one for code, one for data.
7356 We'll use their section symbols for some emitted relocs. */
7358 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7361 asection
*found
= NULL
;
7363 /* Data first, since setting text_index_section changes
7364 _bfd_elf_omit_section_dynsym_default. */
7365 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7366 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7367 && !(s
->flags
& SEC_READONLY
)
7368 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7371 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7374 elf_hash_table (info
)->data_index_section
= found
;
7376 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7377 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7378 && (s
->flags
& SEC_READONLY
)
7379 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7384 elf_hash_table (info
)->text_index_section
= found
;
7387 #define GNU_HASH_SECTION_NAME(bed) \
7388 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7391 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7393 const struct elf_backend_data
*bed
;
7394 unsigned long section_sym_count
;
7395 bfd_size_type dynsymcount
= 0;
7397 if (!is_elf_hash_table (info
->hash
))
7400 bed
= get_elf_backend_data (output_bfd
);
7401 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7403 /* Assign dynsym indices. In a shared library we generate a section
7404 symbol for each output section, which come first. Next come all
7405 of the back-end allocated local dynamic syms, followed by the rest
7406 of the global symbols.
7408 This is usually not needed for static binaries, however backends
7409 can request to always do it, e.g. the MIPS backend uses dynamic
7410 symbol counts to lay out GOT, which will be produced in the
7411 presence of GOT relocations even in static binaries (holding fixed
7412 data in that case, to satisfy those relocations). */
7414 if (elf_hash_table (info
)->dynamic_sections_created
7415 || bed
->always_renumber_dynsyms
)
7416 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7417 §ion_sym_count
);
7419 if (elf_hash_table (info
)->dynamic_sections_created
)
7423 unsigned int dtagcount
;
7425 dynobj
= elf_hash_table (info
)->dynobj
;
7427 /* Work out the size of the symbol version section. */
7428 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7429 BFD_ASSERT (s
!= NULL
);
7430 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7432 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7433 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7434 if (s
->contents
== NULL
)
7437 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7441 /* Set the size of the .dynsym and .hash sections. We counted
7442 the number of dynamic symbols in elf_link_add_object_symbols.
7443 We will build the contents of .dynsym and .hash when we build
7444 the final symbol table, because until then we do not know the
7445 correct value to give the symbols. We built the .dynstr
7446 section as we went along in elf_link_add_object_symbols. */
7447 s
= elf_hash_table (info
)->dynsym
;
7448 BFD_ASSERT (s
!= NULL
);
7449 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7451 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7452 if (s
->contents
== NULL
)
7455 /* The first entry in .dynsym is a dummy symbol. Clear all the
7456 section syms, in case we don't output them all. */
7457 ++section_sym_count
;
7458 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7460 elf_hash_table (info
)->bucketcount
= 0;
7462 /* Compute the size of the hashing table. As a side effect this
7463 computes the hash values for all the names we export. */
7464 if (info
->emit_hash
)
7466 unsigned long int *hashcodes
;
7467 struct hash_codes_info hashinf
;
7469 unsigned long int nsyms
;
7471 size_t hash_entry_size
;
7473 /* Compute the hash values for all exported symbols. At the same
7474 time store the values in an array so that we could use them for
7476 amt
= dynsymcount
* sizeof (unsigned long int);
7477 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7478 if (hashcodes
== NULL
)
7480 hashinf
.hashcodes
= hashcodes
;
7481 hashinf
.error
= false;
7483 /* Put all hash values in HASHCODES. */
7484 elf_link_hash_traverse (elf_hash_table (info
),
7485 elf_collect_hash_codes
, &hashinf
);
7492 nsyms
= hashinf
.hashcodes
- hashcodes
;
7494 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7497 if (bucketcount
== 0 && nsyms
> 0)
7500 elf_hash_table (info
)->bucketcount
= bucketcount
;
7502 s
= bfd_get_linker_section (dynobj
, ".hash");
7503 BFD_ASSERT (s
!= NULL
);
7504 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7505 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7506 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7507 if (s
->contents
== NULL
)
7510 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7511 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7512 s
->contents
+ hash_entry_size
);
7515 if (info
->emit_gnu_hash
)
7518 unsigned char *contents
;
7519 struct collect_gnu_hash_codes cinfo
;
7523 memset (&cinfo
, 0, sizeof (cinfo
));
7525 /* Compute the hash values for all exported symbols. At the same
7526 time store the values in an array so that we could use them for
7528 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7529 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7530 if (cinfo
.hashcodes
== NULL
)
7533 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7534 cinfo
.min_dynindx
= -1;
7535 cinfo
.output_bfd
= output_bfd
;
7538 /* Put all hash values in HASHCODES. */
7539 elf_link_hash_traverse (elf_hash_table (info
),
7540 elf_collect_gnu_hash_codes
, &cinfo
);
7543 free (cinfo
.hashcodes
);
7548 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7550 if (bucketcount
== 0)
7552 free (cinfo
.hashcodes
);
7556 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7557 BFD_ASSERT (s
!= NULL
);
7559 if (cinfo
.nsyms
== 0)
7561 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7562 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7563 free (cinfo
.hashcodes
);
7564 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7565 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7566 if (contents
== NULL
)
7568 s
->contents
= contents
;
7569 /* 1 empty bucket. */
7570 bfd_put_32 (output_bfd
, 1, contents
);
7571 /* SYMIDX above the special symbol 0. */
7572 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7573 /* Just one word for bitmask. */
7574 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7575 /* Only hash fn bloom filter. */
7576 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7577 /* No hashes are valid - empty bitmask. */
7578 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7579 /* No hashes in the only bucket. */
7580 bfd_put_32 (output_bfd
, 0,
7581 contents
+ 16 + bed
->s
->arch_size
/ 8);
7585 unsigned long int maskwords
, maskbitslog2
, x
;
7586 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7590 while ((x
>>= 1) != 0)
7592 if (maskbitslog2
< 3)
7594 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7595 maskbitslog2
= maskbitslog2
+ 3;
7597 maskbitslog2
= maskbitslog2
+ 2;
7598 if (bed
->s
->arch_size
== 64)
7600 if (maskbitslog2
== 5)
7606 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7607 cinfo
.shift2
= maskbitslog2
;
7608 cinfo
.maskbits
= 1 << maskbitslog2
;
7609 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7610 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7611 amt
+= maskwords
* sizeof (bfd_vma
);
7612 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7613 if (cinfo
.bitmask
== NULL
)
7615 free (cinfo
.hashcodes
);
7619 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7620 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7621 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7622 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7624 /* Determine how often each hash bucket is used. */
7625 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7626 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7627 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
7629 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
7630 if (cinfo
.counts
[i
] != 0)
7632 cinfo
.indx
[i
] = cnt
;
7633 cnt
+= cinfo
.counts
[i
];
7635 BFD_ASSERT (cnt
== dynsymcount
);
7636 cinfo
.bucketcount
= bucketcount
;
7637 cinfo
.local_indx
= cinfo
.min_dynindx
;
7639 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
7640 s
->size
+= cinfo
.maskbits
/ 8;
7641 if (bed
->record_xhash_symbol
!= NULL
)
7642 s
->size
+= cinfo
.nsyms
* 4;
7643 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7644 if (contents
== NULL
)
7646 free (cinfo
.bitmask
);
7647 free (cinfo
.hashcodes
);
7651 s
->contents
= contents
;
7652 bfd_put_32 (output_bfd
, bucketcount
, contents
);
7653 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
7654 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
7655 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
7656 contents
+= 16 + cinfo
.maskbits
/ 8;
7658 for (i
= 0; i
< bucketcount
; ++i
)
7660 if (cinfo
.counts
[i
] == 0)
7661 bfd_put_32 (output_bfd
, 0, contents
);
7663 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
7667 cinfo
.contents
= contents
;
7669 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
7670 /* Renumber dynamic symbols, if populating .gnu.hash section.
7671 If using .MIPS.xhash, populate the translation table. */
7672 elf_link_hash_traverse (elf_hash_table (info
),
7673 elf_gnu_hash_process_symidx
, &cinfo
);
7675 contents
= s
->contents
+ 16;
7676 for (i
= 0; i
< maskwords
; ++i
)
7678 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
7680 contents
+= bed
->s
->arch_size
/ 8;
7683 free (cinfo
.bitmask
);
7684 free (cinfo
.hashcodes
);
7688 s
= bfd_get_linker_section (dynobj
, ".dynstr");
7689 BFD_ASSERT (s
!= NULL
);
7691 elf_finalize_dynstr (output_bfd
, info
);
7693 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7695 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
7696 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
7703 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7706 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
7709 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
7710 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
7713 /* Finish SHF_MERGE section merging. */
7716 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
7721 if (!is_elf_hash_table (info
->hash
))
7724 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
7725 if ((ibfd
->flags
& DYNAMIC
) == 0
7726 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
7727 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
7728 == get_elf_backend_data (obfd
)->s
->elfclass
))
7729 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
7730 if ((sec
->flags
& SEC_MERGE
) != 0
7731 && !bfd_is_abs_section (sec
->output_section
))
7733 struct bfd_elf_section_data
*secdata
;
7735 secdata
= elf_section_data (sec
);
7736 if (! _bfd_add_merge_section (obfd
,
7737 &elf_hash_table (info
)->merge_info
,
7738 sec
, &secdata
->sec_info
))
7740 else if (secdata
->sec_info
)
7741 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
7744 if (elf_hash_table (info
)->merge_info
!= NULL
)
7745 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
7746 merge_sections_remove_hook
);
7750 /* Create an entry in an ELF linker hash table. */
7752 struct bfd_hash_entry
*
7753 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
7754 struct bfd_hash_table
*table
,
7757 /* Allocate the structure if it has not already been allocated by a
7761 entry
= (struct bfd_hash_entry
*)
7762 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
7767 /* Call the allocation method of the superclass. */
7768 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
7771 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
7772 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7774 /* Set local fields. */
7777 ret
->got
= htab
->init_got_refcount
;
7778 ret
->plt
= htab
->init_plt_refcount
;
7779 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7780 - offsetof (struct elf_link_hash_entry
, size
)));
7781 /* Assume that we have been called by a non-ELF symbol reader.
7782 This flag is then reset by the code which reads an ELF input
7783 file. This ensures that a symbol created by a non-ELF symbol
7784 reader will have the flag set correctly. */
7791 /* Copy data from an indirect symbol to its direct symbol, hiding the
7792 old indirect symbol. Also used for copying flags to a weakdef. */
7795 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7796 struct elf_link_hash_entry
*dir
,
7797 struct elf_link_hash_entry
*ind
)
7799 struct elf_link_hash_table
*htab
;
7801 if (ind
->dyn_relocs
!= NULL
)
7803 if (dir
->dyn_relocs
!= NULL
)
7805 struct elf_dyn_relocs
**pp
;
7806 struct elf_dyn_relocs
*p
;
7808 /* Add reloc counts against the indirect sym to the direct sym
7809 list. Merge any entries against the same section. */
7810 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
7812 struct elf_dyn_relocs
*q
;
7814 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
7815 if (q
->sec
== p
->sec
)
7817 q
->pc_count
+= p
->pc_count
;
7818 q
->count
+= p
->count
;
7825 *pp
= dir
->dyn_relocs
;
7828 dir
->dyn_relocs
= ind
->dyn_relocs
;
7829 ind
->dyn_relocs
= NULL
;
7832 /* Copy down any references that we may have already seen to the
7833 symbol which just became indirect. */
7835 if (dir
->versioned
!= versioned_hidden
)
7836 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7837 dir
->ref_regular
|= ind
->ref_regular
;
7838 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7839 dir
->non_got_ref
|= ind
->non_got_ref
;
7840 dir
->needs_plt
|= ind
->needs_plt
;
7841 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7843 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7846 /* Copy over the global and procedure linkage table refcount entries.
7847 These may have been already set up by a check_relocs routine. */
7848 htab
= elf_hash_table (info
);
7849 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7851 if (dir
->got
.refcount
< 0)
7852 dir
->got
.refcount
= 0;
7853 dir
->got
.refcount
+= ind
->got
.refcount
;
7854 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7857 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7859 if (dir
->plt
.refcount
< 0)
7860 dir
->plt
.refcount
= 0;
7861 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7862 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7865 if (ind
->dynindx
!= -1)
7867 if (dir
->dynindx
!= -1)
7868 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7869 dir
->dynindx
= ind
->dynindx
;
7870 dir
->dynstr_index
= ind
->dynstr_index
;
7872 ind
->dynstr_index
= 0;
7877 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7878 struct elf_link_hash_entry
*h
,
7881 /* STT_GNU_IFUNC symbol must go through PLT. */
7882 if (h
->type
!= STT_GNU_IFUNC
)
7884 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7889 h
->forced_local
= 1;
7890 if (h
->dynindx
!= -1)
7892 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7895 h
->dynstr_index
= 0;
7900 /* Hide a symbol. */
7903 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
7904 struct bfd_link_info
*info
,
7905 struct bfd_link_hash_entry
*h
)
7907 if (is_elf_hash_table (info
->hash
))
7909 const struct elf_backend_data
*bed
7910 = get_elf_backend_data (output_bfd
);
7911 struct elf_link_hash_entry
*eh
7912 = (struct elf_link_hash_entry
*) h
;
7913 bed
->elf_backend_hide_symbol (info
, eh
, true);
7914 eh
->def_dynamic
= 0;
7915 eh
->ref_dynamic
= 0;
7916 eh
->dynamic_def
= 0;
7920 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7924 _bfd_elf_link_hash_table_init
7925 (struct elf_link_hash_table
*table
,
7927 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7928 struct bfd_hash_table
*,
7930 unsigned int entsize
,
7931 enum elf_target_id target_id
)
7934 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7936 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7937 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7938 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7939 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7940 /* The first dynamic symbol is a dummy. */
7941 table
->dynsymcount
= 1;
7943 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7945 table
->root
.type
= bfd_link_elf_hash_table
;
7946 table
->hash_table_id
= target_id
;
7947 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
7952 /* Create an ELF linker hash table. */
7954 struct bfd_link_hash_table
*
7955 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7957 struct elf_link_hash_table
*ret
;
7958 size_t amt
= sizeof (struct elf_link_hash_table
);
7960 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7964 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7965 sizeof (struct elf_link_hash_entry
),
7971 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7976 /* Destroy an ELF linker hash table. */
7979 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7981 struct elf_link_hash_table
*htab
;
7983 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7984 if (htab
->dynstr
!= NULL
)
7985 _bfd_elf_strtab_free (htab
->dynstr
);
7986 _bfd_merge_sections_free (htab
->merge_info
);
7987 _bfd_generic_link_hash_table_free (obfd
);
7990 /* This is a hook for the ELF emulation code in the generic linker to
7991 tell the backend linker what file name to use for the DT_NEEDED
7992 entry for a dynamic object. */
7995 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7997 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7998 && bfd_get_format (abfd
) == bfd_object
)
7999 elf_dt_name (abfd
) = name
;
8003 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
8006 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8007 && bfd_get_format (abfd
) == bfd_object
)
8008 lib_class
= elf_dyn_lib_class (abfd
);
8015 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
8017 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8018 && bfd_get_format (abfd
) == bfd_object
)
8019 elf_dyn_lib_class (abfd
) = lib_class
;
8022 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8023 the linker ELF emulation code. */
8025 struct bfd_link_needed_list
*
8026 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8027 struct bfd_link_info
*info
)
8029 if (! is_elf_hash_table (info
->hash
))
8031 return elf_hash_table (info
)->needed
;
8034 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8035 hook for the linker ELF emulation code. */
8037 struct bfd_link_needed_list
*
8038 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8039 struct bfd_link_info
*info
)
8041 if (! is_elf_hash_table (info
->hash
))
8043 return elf_hash_table (info
)->runpath
;
8046 /* Get the name actually used for a dynamic object for a link. This
8047 is the SONAME entry if there is one. Otherwise, it is the string
8048 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8051 bfd_elf_get_dt_soname (bfd
*abfd
)
8053 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8054 && bfd_get_format (abfd
) == bfd_object
)
8055 return elf_dt_name (abfd
);
8059 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8060 the ELF linker emulation code. */
8063 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
8064 struct bfd_link_needed_list
**pneeded
)
8067 bfd_byte
*dynbuf
= NULL
;
8068 unsigned int elfsec
;
8069 unsigned long shlink
;
8070 bfd_byte
*extdyn
, *extdynend
;
8072 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
8076 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
8077 || bfd_get_format (abfd
) != bfd_object
)
8080 s
= bfd_get_section_by_name (abfd
, ".dynamic");
8081 if (s
== NULL
|| s
->size
== 0)
8084 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
8087 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8088 if (elfsec
== SHN_BAD
)
8091 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8093 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8094 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8097 extdynend
= extdyn
+ s
->size
;
8098 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
8100 Elf_Internal_Dyn dyn
;
8102 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8104 if (dyn
.d_tag
== DT_NULL
)
8107 if (dyn
.d_tag
== DT_NEEDED
)
8110 struct bfd_link_needed_list
*l
;
8111 unsigned int tagv
= dyn
.d_un
.d_val
;
8114 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8119 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8139 struct elf_symbuf_symbol
8141 unsigned long st_name
; /* Symbol name, index in string tbl */
8142 unsigned char st_info
; /* Type and binding attributes */
8143 unsigned char st_other
; /* Visibilty, and target specific */
8146 struct elf_symbuf_head
8148 struct elf_symbuf_symbol
*ssym
;
8150 unsigned int st_shndx
;
8157 Elf_Internal_Sym
*isym
;
8158 struct elf_symbuf_symbol
*ssym
;
8164 /* Sort references to symbols by ascending section number. */
8167 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8169 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8170 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8172 if (s1
->st_shndx
!= s2
->st_shndx
)
8173 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8174 /* Final sort by the address of the sym in the symbuf ensures
8177 return s1
> s2
? 1 : -1;
8182 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8184 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8185 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8186 int ret
= strcmp (s1
->name
, s2
->name
);
8189 if (s1
->u
.p
!= s2
->u
.p
)
8190 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8194 static struct elf_symbuf_head
*
8195 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8197 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8198 struct elf_symbuf_symbol
*ssym
;
8199 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8200 size_t i
, shndx_count
, total_size
, amt
;
8202 amt
= symcount
* sizeof (*indbuf
);
8203 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8207 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8208 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8209 *ind
++ = &isymbuf
[i
];
8212 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8213 elf_sort_elf_symbol
);
8216 if (indbufend
> indbuf
)
8217 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8218 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8221 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8222 + (indbufend
- indbuf
) * sizeof (*ssym
));
8223 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8224 if (ssymbuf
== NULL
)
8230 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8231 ssymbuf
->ssym
= NULL
;
8232 ssymbuf
->count
= shndx_count
;
8233 ssymbuf
->st_shndx
= 0;
8234 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8236 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8239 ssymhead
->ssym
= ssym
;
8240 ssymhead
->count
= 0;
8241 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8243 ssym
->st_name
= (*ind
)->st_name
;
8244 ssym
->st_info
= (*ind
)->st_info
;
8245 ssym
->st_other
= (*ind
)->st_other
;
8248 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8249 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
8256 /* Check if 2 sections define the same set of local and global
8260 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8261 struct bfd_link_info
*info
)
8264 const struct elf_backend_data
*bed1
, *bed2
;
8265 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8266 size_t symcount1
, symcount2
;
8267 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8268 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8269 Elf_Internal_Sym
*isym
, *isymend
;
8270 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8271 size_t count1
, count2
, sec_count1
, sec_count2
, i
;
8272 unsigned int shndx1
, shndx2
;
8274 bool ignore_section_symbol_p
;
8279 /* Both sections have to be in ELF. */
8280 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8281 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8284 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8287 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8288 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8289 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8292 bed1
= get_elf_backend_data (bfd1
);
8293 bed2
= get_elf_backend_data (bfd2
);
8294 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8295 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8296 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8297 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8299 if (symcount1
== 0 || symcount2
== 0)
8305 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8306 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8308 /* Ignore section symbols only when matching non-debugging sections
8309 or linkonce section with comdat section. */
8310 ignore_section_symbol_p
8311 = ((sec1
->flags
& SEC_DEBUGGING
) == 0
8312 || ((elf_section_flags (sec1
) & SHF_GROUP
)
8313 != (elf_section_flags (sec2
) & SHF_GROUP
)));
8315 if (ssymbuf1
== NULL
)
8317 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8319 if (isymbuf1
== NULL
)
8322 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8324 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8325 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8329 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8331 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8333 if (isymbuf2
== NULL
)
8336 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8338 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8339 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8343 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8345 /* Optimized faster version. */
8347 struct elf_symbol
*symp
;
8348 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8351 hi
= ssymbuf1
->count
;
8357 mid
= (lo
+ hi
) / 2;
8358 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8360 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8364 count1
= ssymbuf1
[mid
].count
;
8369 if (ignore_section_symbol_p
)
8371 for (i
= 0; i
< count1
; i
++)
8372 if (ELF_ST_TYPE (ssymbuf1
->ssym
[i
].st_info
) == STT_SECTION
)
8374 count1
-= sec_count1
;
8378 hi
= ssymbuf2
->count
;
8384 mid
= (lo
+ hi
) / 2;
8385 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8387 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8391 count2
= ssymbuf2
[mid
].count
;
8396 if (ignore_section_symbol_p
)
8398 for (i
= 0; i
< count2
; i
++)
8399 if (ELF_ST_TYPE (ssymbuf2
->ssym
[i
].st_info
) == STT_SECTION
)
8401 count2
-= sec_count2
;
8404 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8408 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8410 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8411 if (symtable1
== NULL
|| symtable2
== NULL
)
8415 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
+ sec_count1
;
8416 ssym
< ssymend
; ssym
++)
8418 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8420 symp
->u
.ssym
= ssym
;
8421 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8428 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
+ sec_count2
;
8429 ssym
< ssymend
; ssym
++)
8431 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8433 symp
->u
.ssym
= ssym
;
8434 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8440 /* Sort symbol by name. */
8441 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8442 elf_sym_name_compare
);
8443 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8444 elf_sym_name_compare
);
8446 for (i
= 0; i
< count1
; i
++)
8447 /* Two symbols must have the same binding, type and name. */
8448 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8449 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8450 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8457 symtable1
= (struct elf_symbol
*)
8458 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8459 symtable2
= (struct elf_symbol
*)
8460 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8461 if (symtable1
== NULL
|| symtable2
== NULL
)
8464 /* Count definitions in the section. */
8466 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8467 if (isym
->st_shndx
== shndx1
8468 && (!ignore_section_symbol_p
8469 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8470 symtable1
[count1
++].u
.isym
= isym
;
8473 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8474 if (isym
->st_shndx
== shndx2
8475 && (!ignore_section_symbol_p
8476 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8477 symtable2
[count2
++].u
.isym
= isym
;
8479 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8482 for (i
= 0; i
< count1
; i
++)
8484 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8485 symtable1
[i
].u
.isym
->st_name
);
8487 for (i
= 0; i
< count2
; i
++)
8489 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8490 symtable2
[i
].u
.isym
->st_name
);
8492 /* Sort symbol by name. */
8493 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8494 elf_sym_name_compare
);
8495 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8496 elf_sym_name_compare
);
8498 for (i
= 0; i
< count1
; i
++)
8499 /* Two symbols must have the same binding, type and name. */
8500 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8501 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8502 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8516 /* Return TRUE if 2 section types are compatible. */
8519 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8520 bfd
*bbfd
, const asection
*bsec
)
8524 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8525 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8528 return elf_section_type (asec
) == elf_section_type (bsec
);
8531 /* Final phase of ELF linker. */
8533 /* A structure we use to avoid passing large numbers of arguments. */
8535 struct elf_final_link_info
8537 /* General link information. */
8538 struct bfd_link_info
*info
;
8541 /* Symbol string table. */
8542 struct elf_strtab_hash
*symstrtab
;
8543 /* .hash section. */
8545 /* symbol version section (.gnu.version). */
8546 asection
*symver_sec
;
8547 /* Buffer large enough to hold contents of any section. */
8549 /* Buffer large enough to hold external relocs of any section. */
8550 void *external_relocs
;
8551 /* Buffer large enough to hold internal relocs of any section. */
8552 Elf_Internal_Rela
*internal_relocs
;
8553 /* Buffer large enough to hold external local symbols of any input
8555 bfd_byte
*external_syms
;
8556 /* And a buffer for symbol section indices. */
8557 Elf_External_Sym_Shndx
*locsym_shndx
;
8558 /* Buffer large enough to hold internal local symbols of any input
8560 Elf_Internal_Sym
*internal_syms
;
8561 /* Array large enough to hold a symbol index for each local symbol
8562 of any input BFD. */
8564 /* Array large enough to hold a section pointer for each local
8565 symbol of any input BFD. */
8566 asection
**sections
;
8567 /* Buffer for SHT_SYMTAB_SHNDX section. */
8568 Elf_External_Sym_Shndx
*symshndxbuf
;
8569 /* Number of STT_FILE syms seen. */
8570 size_t filesym_count
;
8571 /* Local symbol hash table. */
8572 struct bfd_hash_table local_hash_table
;
8575 struct local_hash_entry
8577 /* Base hash table entry structure. */
8578 struct bfd_hash_entry root
;
8579 /* Size of the local symbol name. */
8581 /* Number of the duplicated local symbol names. */
8585 /* Create an entry in the local symbol hash table. */
8587 static struct bfd_hash_entry
*
8588 local_hash_newfunc (struct bfd_hash_entry
*entry
,
8589 struct bfd_hash_table
*table
,
8593 /* Allocate the structure if it has not already been allocated by a
8597 entry
= bfd_hash_allocate (table
,
8598 sizeof (struct local_hash_entry
));
8603 /* Call the allocation method of the superclass. */
8604 entry
= bfd_hash_newfunc (entry
, table
, string
);
8607 ((struct local_hash_entry
*) entry
)->count
= 0;
8608 ((struct local_hash_entry
*) entry
)->size
= 0;
8614 /* This struct is used to pass information to elf_link_output_extsym. */
8616 struct elf_outext_info
8621 struct elf_final_link_info
*flinfo
;
8625 /* Support for evaluating a complex relocation.
8627 Complex relocations are generalized, self-describing relocations. The
8628 implementation of them consists of two parts: complex symbols, and the
8629 relocations themselves.
8631 The relocations use a reserved elf-wide relocation type code (R_RELC
8632 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8633 information (start bit, end bit, word width, etc) into the addend. This
8634 information is extracted from CGEN-generated operand tables within gas.
8636 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
8637 internal) representing prefix-notation expressions, including but not
8638 limited to those sorts of expressions normally encoded as addends in the
8639 addend field. The symbol mangling format is:
8642 | <unary-operator> ':' <node>
8643 | <binary-operator> ':' <node> ':' <node>
8646 <literal> := 's' <digits=N> ':' <N character symbol name>
8647 | 'S' <digits=N> ':' <N character section name>
8651 <binary-operator> := as in C
8652 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8655 set_symbol_value (bfd
*bfd_with_globals
,
8656 Elf_Internal_Sym
*isymbuf
,
8661 struct elf_link_hash_entry
**sym_hashes
;
8662 struct elf_link_hash_entry
*h
;
8663 size_t extsymoff
= locsymcount
;
8665 if (symidx
< locsymcount
)
8667 Elf_Internal_Sym
*sym
;
8669 sym
= isymbuf
+ symidx
;
8670 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
8672 /* It is a local symbol: move it to the
8673 "absolute" section and give it a value. */
8674 sym
->st_shndx
= SHN_ABS
;
8675 sym
->st_value
= val
;
8678 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
8682 /* It is a global symbol: set its link type
8683 to "defined" and give it a value. */
8685 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
8686 h
= sym_hashes
[symidx
- extsymoff
];
8687 while (h
->root
.type
== bfd_link_hash_indirect
8688 || h
->root
.type
== bfd_link_hash_warning
)
8689 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8690 h
->root
.type
= bfd_link_hash_defined
;
8691 h
->root
.u
.def
.value
= val
;
8692 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
8696 resolve_symbol (const char *name
,
8698 struct elf_final_link_info
*flinfo
,
8700 Elf_Internal_Sym
*isymbuf
,
8703 Elf_Internal_Sym
*sym
;
8704 struct bfd_link_hash_entry
*global_entry
;
8705 const char *candidate
= NULL
;
8706 Elf_Internal_Shdr
*symtab_hdr
;
8709 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
8711 for (i
= 0; i
< locsymcount
; ++ i
)
8715 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
8718 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
8719 symtab_hdr
->sh_link
,
8722 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8723 name
, candidate
, (unsigned long) sym
->st_value
);
8725 if (candidate
&& strcmp (candidate
, name
) == 0)
8727 asection
*sec
= flinfo
->sections
[i
];
8729 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
8730 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
8732 printf ("Found symbol with value %8.8lx\n",
8733 (unsigned long) *result
);
8739 /* Hmm, haven't found it yet. perhaps it is a global. */
8740 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
8741 false, false, true);
8745 if (global_entry
->type
== bfd_link_hash_defined
8746 || global_entry
->type
== bfd_link_hash_defweak
)
8748 *result
= (global_entry
->u
.def
.value
8749 + global_entry
->u
.def
.section
->output_section
->vma
8750 + global_entry
->u
.def
.section
->output_offset
);
8752 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8753 global_entry
->root
.string
, (unsigned long) *result
);
8761 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8762 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8763 names like "foo.end" which is the end address of section "foo". */
8766 resolve_section (const char *name
,
8774 for (curr
= sections
; curr
; curr
= curr
->next
)
8775 if (strcmp (curr
->name
, name
) == 0)
8777 *result
= curr
->vma
;
8781 /* Hmm. still haven't found it. try pseudo-section names. */
8782 /* FIXME: This could be coded more efficiently... */
8783 for (curr
= sections
; curr
; curr
= curr
->next
)
8785 len
= strlen (curr
->name
);
8786 if (len
> strlen (name
))
8789 if (strncmp (curr
->name
, name
, len
) == 0)
8791 if (startswith (name
+ len
, ".end"))
8793 *result
= (curr
->vma
8794 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
8798 /* Insert more pseudo-section names here, if you like. */
8806 undefined_reference (const char *reftype
, const char *name
)
8808 /* xgettext:c-format */
8809 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8811 bfd_set_error (bfd_error_bad_value
);
8815 eval_symbol (bfd_vma
*result
,
8818 struct elf_final_link_info
*flinfo
,
8820 Elf_Internal_Sym
*isymbuf
,
8829 const char *sym
= *symp
;
8831 bool symbol_is_section
= false;
8836 if (len
< 1 || len
> sizeof (symbuf
))
8838 bfd_set_error (bfd_error_invalid_operation
);
8851 *result
= strtoul (sym
, (char **) symp
, 16);
8855 symbol_is_section
= true;
8859 symlen
= strtol (sym
, (char **) symp
, 10);
8860 sym
= *symp
+ 1; /* Skip the trailing ':'. */
8862 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
8864 bfd_set_error (bfd_error_invalid_operation
);
8868 memcpy (symbuf
, sym
, symlen
);
8869 symbuf
[symlen
] = '\0';
8870 *symp
= sym
+ symlen
;
8872 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8873 the symbol as a section, or vice-versa. so we're pretty liberal in our
8874 interpretation here; section means "try section first", not "must be a
8875 section", and likewise with symbol. */
8877 if (symbol_is_section
)
8879 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
8880 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8881 isymbuf
, locsymcount
))
8883 undefined_reference ("section", symbuf
);
8889 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
8890 isymbuf
, locsymcount
)
8891 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
8894 undefined_reference ("symbol", symbuf
);
8901 /* All that remains are operators. */
8903 #define UNARY_OP(op) \
8904 if (startswith (sym, #op)) \
8906 sym += strlen (#op); \
8910 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8911 isymbuf, locsymcount, signed_p)) \
8914 *result = op ((bfd_signed_vma) a); \
8920 #define BINARY_OP_HEAD(op) \
8921 if (startswith (sym, #op)) \
8923 sym += strlen (#op); \
8927 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8928 isymbuf, locsymcount, signed_p)) \
8931 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8932 isymbuf, locsymcount, signed_p)) \
8934 #define BINARY_OP_TAIL(op) \
8936 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8941 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
8945 BINARY_OP_HEAD (<<);
8946 if (b
>= sizeof (a
) * CHAR_BIT
)
8952 BINARY_OP_TAIL (<<);
8953 BINARY_OP_HEAD (>>);
8954 if (b
>= sizeof (a
) * CHAR_BIT
)
8956 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
8959 BINARY_OP_TAIL (>>);
8972 _bfd_error_handler (_("division by zero"));
8973 bfd_set_error (bfd_error_bad_value
);
8980 _bfd_error_handler (_("division by zero"));
8981 bfd_set_error (bfd_error_bad_value
);
8994 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8995 bfd_set_error (bfd_error_invalid_operation
);
9001 put_value (bfd_vma size
,
9002 unsigned long chunksz
,
9007 location
+= (size
- chunksz
);
9009 for (; size
; size
-= chunksz
, location
-= chunksz
)
9014 bfd_put_8 (input_bfd
, x
, location
);
9018 bfd_put_16 (input_bfd
, x
, location
);
9022 bfd_put_32 (input_bfd
, x
, location
);
9023 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9029 bfd_put_64 (input_bfd
, x
, location
);
9030 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9043 get_value (bfd_vma size
,
9044 unsigned long chunksz
,
9051 /* Sanity checks. */
9052 BFD_ASSERT (chunksz
<= sizeof (x
)
9055 && (size
% chunksz
) == 0
9056 && input_bfd
!= NULL
9057 && location
!= NULL
);
9059 if (chunksz
== sizeof (x
))
9061 BFD_ASSERT (size
== chunksz
);
9063 /* Make sure that we do not perform an undefined shift operation.
9064 We know that size == chunksz so there will only be one iteration
9065 of the loop below. */
9069 shift
= 8 * chunksz
;
9071 for (; size
; size
-= chunksz
, location
+= chunksz
)
9076 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
9079 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
9082 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
9086 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
9097 decode_complex_addend (unsigned long *start
, /* in bits */
9098 unsigned long *oplen
, /* in bits */
9099 unsigned long *len
, /* in bits */
9100 unsigned long *wordsz
, /* in bytes */
9101 unsigned long *chunksz
, /* in bytes */
9102 unsigned long *lsb0_p
,
9103 unsigned long *signed_p
,
9104 unsigned long *trunc_p
,
9105 unsigned long encoded
)
9107 * start
= encoded
& 0x3F;
9108 * len
= (encoded
>> 6) & 0x3F;
9109 * oplen
= (encoded
>> 12) & 0x3F;
9110 * wordsz
= (encoded
>> 18) & 0xF;
9111 * chunksz
= (encoded
>> 22) & 0xF;
9112 * lsb0_p
= (encoded
>> 27) & 1;
9113 * signed_p
= (encoded
>> 28) & 1;
9114 * trunc_p
= (encoded
>> 29) & 1;
9117 bfd_reloc_status_type
9118 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
9119 asection
*input_section
,
9121 Elf_Internal_Rela
*rel
,
9124 bfd_vma shift
, x
, mask
;
9125 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9126 bfd_reloc_status_type r
;
9127 bfd_size_type octets
;
9129 /* Perform this reloc, since it is complex.
9130 (this is not to say that it necessarily refers to a complex
9131 symbol; merely that it is a self-describing CGEN based reloc.
9132 i.e. the addend has the complete reloc information (bit start, end,
9133 word size, etc) encoded within it.). */
9135 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9136 &chunksz
, &lsb0_p
, &signed_p
,
9137 &trunc_p
, rel
->r_addend
);
9139 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9142 shift
= (start
+ 1) - len
;
9144 shift
= (8 * wordsz
) - (start
+ len
);
9146 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9147 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9150 printf ("Doing complex reloc: "
9151 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9152 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9153 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9154 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9155 oplen
, (unsigned long) x
, (unsigned long) mask
,
9156 (unsigned long) relocation
);
9161 /* Now do an overflow check. */
9162 r
= bfd_check_overflow ((signed_p
9163 ? complain_overflow_signed
9164 : complain_overflow_unsigned
),
9165 len
, 0, (8 * wordsz
),
9169 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9172 printf (" relocation: %8.8lx\n"
9173 " shifted mask: %8.8lx\n"
9174 " shifted/masked reloc: %8.8lx\n"
9175 " result: %8.8lx\n",
9176 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9177 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9179 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9183 /* Functions to read r_offset from external (target order) reloc
9184 entry. Faster than bfd_getl32 et al, because we let the compiler
9185 know the value is aligned. */
9188 ext32l_r_offset (const void *p
)
9195 const union aligned32
*a
9196 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9198 uint32_t aval
= ( (uint32_t) a
->c
[0]
9199 | (uint32_t) a
->c
[1] << 8
9200 | (uint32_t) a
->c
[2] << 16
9201 | (uint32_t) a
->c
[3] << 24);
9206 ext32b_r_offset (const void *p
)
9213 const union aligned32
*a
9214 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9216 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9217 | (uint32_t) a
->c
[1] << 16
9218 | (uint32_t) a
->c
[2] << 8
9219 | (uint32_t) a
->c
[3]);
9223 #ifdef BFD_HOST_64_BIT
9225 ext64l_r_offset (const void *p
)
9232 const union aligned64
*a
9233 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9235 uint64_t aval
= ( (uint64_t) a
->c
[0]
9236 | (uint64_t) a
->c
[1] << 8
9237 | (uint64_t) a
->c
[2] << 16
9238 | (uint64_t) a
->c
[3] << 24
9239 | (uint64_t) a
->c
[4] << 32
9240 | (uint64_t) a
->c
[5] << 40
9241 | (uint64_t) a
->c
[6] << 48
9242 | (uint64_t) a
->c
[7] << 56);
9247 ext64b_r_offset (const void *p
)
9254 const union aligned64
*a
9255 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9257 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9258 | (uint64_t) a
->c
[1] << 48
9259 | (uint64_t) a
->c
[2] << 40
9260 | (uint64_t) a
->c
[3] << 32
9261 | (uint64_t) a
->c
[4] << 24
9262 | (uint64_t) a
->c
[5] << 16
9263 | (uint64_t) a
->c
[6] << 8
9264 | (uint64_t) a
->c
[7]);
9269 /* When performing a relocatable link, the input relocations are
9270 preserved. But, if they reference global symbols, the indices
9271 referenced must be updated. Update all the relocations found in
9275 elf_link_adjust_relocs (bfd
*abfd
,
9277 struct bfd_elf_section_reloc_data
*reldata
,
9279 struct bfd_link_info
*info
)
9282 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9284 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9285 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9286 bfd_vma r_type_mask
;
9288 unsigned int count
= reldata
->count
;
9289 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9291 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9293 swap_in
= bed
->s
->swap_reloc_in
;
9294 swap_out
= bed
->s
->swap_reloc_out
;
9296 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9298 swap_in
= bed
->s
->swap_reloca_in
;
9299 swap_out
= bed
->s
->swap_reloca_out
;
9304 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9307 if (bed
->s
->arch_size
== 32)
9314 r_type_mask
= 0xffffffff;
9318 erela
= reldata
->hdr
->contents
;
9319 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9321 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9324 if (*rel_hash
== NULL
)
9327 if ((*rel_hash
)->indx
== -2
9328 && info
->gc_sections
9329 && ! info
->gc_keep_exported
)
9331 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9332 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9334 (*rel_hash
)->root
.root
.string
);
9335 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9337 bfd_set_error (bfd_error_invalid_operation
);
9340 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9342 (*swap_in
) (abfd
, erela
, irela
);
9343 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9344 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9345 | (irela
[j
].r_info
& r_type_mask
));
9346 (*swap_out
) (abfd
, irela
, erela
);
9349 if (bed
->elf_backend_update_relocs
)
9350 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9352 if (sort
&& count
!= 0)
9354 bfd_vma (*ext_r_off
) (const void *);
9357 bfd_byte
*base
, *end
, *p
, *loc
;
9358 bfd_byte
*buf
= NULL
;
9360 if (bed
->s
->arch_size
== 32)
9362 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9363 ext_r_off
= ext32l_r_offset
;
9364 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9365 ext_r_off
= ext32b_r_offset
;
9371 #ifdef BFD_HOST_64_BIT
9372 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9373 ext_r_off
= ext64l_r_offset
;
9374 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9375 ext_r_off
= ext64b_r_offset
;
9381 /* Must use a stable sort here. A modified insertion sort,
9382 since the relocs are mostly sorted already. */
9383 elt_size
= reldata
->hdr
->sh_entsize
;
9384 base
= reldata
->hdr
->contents
;
9385 end
= base
+ count
* elt_size
;
9386 if (elt_size
> sizeof (Elf64_External_Rela
))
9389 /* Ensure the first element is lowest. This acts as a sentinel,
9390 speeding the main loop below. */
9391 r_off
= (*ext_r_off
) (base
);
9392 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9394 bfd_vma r_off2
= (*ext_r_off
) (p
);
9403 /* Don't just swap *base and *loc as that changes the order
9404 of the original base[0] and base[1] if they happen to
9405 have the same r_offset. */
9406 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9407 memcpy (onebuf
, loc
, elt_size
);
9408 memmove (base
+ elt_size
, base
, loc
- base
);
9409 memcpy (base
, onebuf
, elt_size
);
9412 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9414 /* base to p is sorted, *p is next to insert. */
9415 r_off
= (*ext_r_off
) (p
);
9416 /* Search the sorted region for location to insert. */
9418 while (r_off
< (*ext_r_off
) (loc
))
9423 /* Chances are there is a run of relocs to insert here,
9424 from one of more input files. Files are not always
9425 linked in order due to the way elf_link_input_bfd is
9426 called. See pr17666. */
9427 size_t sortlen
= p
- loc
;
9428 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9429 size_t runlen
= elt_size
;
9430 size_t buf_size
= 96 * 1024;
9431 while (p
+ runlen
< end
9432 && (sortlen
<= buf_size
9433 || runlen
+ elt_size
<= buf_size
)
9434 && r_off2
> (*ext_r_off
) (p
+ runlen
))
9438 buf
= bfd_malloc (buf_size
);
9442 if (runlen
< sortlen
)
9444 memcpy (buf
, p
, runlen
);
9445 memmove (loc
+ runlen
, loc
, sortlen
);
9446 memcpy (loc
, buf
, runlen
);
9450 memcpy (buf
, loc
, sortlen
);
9451 memmove (loc
, p
, runlen
);
9452 memcpy (loc
+ runlen
, buf
, sortlen
);
9454 p
+= runlen
- elt_size
;
9457 /* Hashes are no longer valid. */
9458 free (reldata
->hashes
);
9459 reldata
->hashes
= NULL
;
9465 struct elf_link_sort_rela
9471 enum elf_reloc_type_class type
;
9472 /* We use this as an array of size int_rels_per_ext_rel. */
9473 Elf_Internal_Rela rela
[1];
9476 /* qsort stability here and for cmp2 is only an issue if multiple
9477 dynamic relocations are emitted at the same address. But targets
9478 that apply a series of dynamic relocations each operating on the
9479 result of the prior relocation can't use -z combreloc as
9480 implemented anyway. Such schemes tend to be broken by sorting on
9481 symbol index. That leaves dynamic NONE relocs as the only other
9482 case where ld might emit multiple relocs at the same address, and
9483 those are only emitted due to target bugs. */
9486 elf_link_sort_cmp1 (const void *A
, const void *B
)
9488 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9489 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9490 int relativea
, relativeb
;
9492 relativea
= a
->type
== reloc_class_relative
;
9493 relativeb
= b
->type
== reloc_class_relative
;
9495 if (relativea
< relativeb
)
9497 if (relativea
> relativeb
)
9499 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9501 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9503 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9505 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9511 elf_link_sort_cmp2 (const void *A
, const void *B
)
9513 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9514 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9516 if (a
->type
< b
->type
)
9518 if (a
->type
> b
->type
)
9520 if (a
->u
.offset
< b
->u
.offset
)
9522 if (a
->u
.offset
> b
->u
.offset
)
9524 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9526 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9532 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9534 asection
*dynamic_relocs
;
9537 bfd_size_type count
, size
;
9538 size_t i
, ret
, sort_elt
, ext_size
;
9539 bfd_byte
*sort
, *s_non_relative
, *p
;
9540 struct elf_link_sort_rela
*sq
;
9541 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9542 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9543 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9544 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9545 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9546 struct bfd_link_order
*lo
;
9550 /* Find a dynamic reloc section. */
9551 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9552 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9553 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9554 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9556 bool use_rela_initialised
= false;
9558 /* This is just here to stop gcc from complaining.
9559 Its initialization checking code is not perfect. */
9562 /* Both sections are present. Examine the sizes
9563 of the indirect sections to help us choose. */
9564 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9565 if (lo
->type
== bfd_indirect_link_order
)
9567 asection
*o
= lo
->u
.indirect
.section
;
9569 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9571 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9572 /* Section size is divisible by both rel and rela sizes.
9573 It is of no help to us. */
9577 /* Section size is only divisible by rela. */
9578 if (use_rela_initialised
&& !use_rela
)
9580 _bfd_error_handler (_("%pB: unable to sort relocs - "
9581 "they are in more than one size"),
9583 bfd_set_error (bfd_error_invalid_operation
);
9589 use_rela_initialised
= true;
9593 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9595 /* Section size is only divisible by rel. */
9596 if (use_rela_initialised
&& use_rela
)
9598 _bfd_error_handler (_("%pB: unable to sort relocs - "
9599 "they are in more than one size"),
9601 bfd_set_error (bfd_error_invalid_operation
);
9607 use_rela_initialised
= true;
9612 /* The section size is not divisible by either -
9613 something is wrong. */
9614 _bfd_error_handler (_("%pB: unable to sort relocs - "
9615 "they are of an unknown size"), abfd
);
9616 bfd_set_error (bfd_error_invalid_operation
);
9621 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9622 if (lo
->type
== bfd_indirect_link_order
)
9624 asection
*o
= lo
->u
.indirect
.section
;
9626 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9628 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9629 /* Section size is divisible by both rel and rela sizes.
9630 It is of no help to us. */
9634 /* Section size is only divisible by rela. */
9635 if (use_rela_initialised
&& !use_rela
)
9637 _bfd_error_handler (_("%pB: unable to sort relocs - "
9638 "they are in more than one size"),
9640 bfd_set_error (bfd_error_invalid_operation
);
9646 use_rela_initialised
= true;
9650 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9652 /* Section size is only divisible by rel. */
9653 if (use_rela_initialised
&& use_rela
)
9655 _bfd_error_handler (_("%pB: unable to sort relocs - "
9656 "they are in more than one size"),
9658 bfd_set_error (bfd_error_invalid_operation
);
9664 use_rela_initialised
= true;
9669 /* The section size is not divisible by either -
9670 something is wrong. */
9671 _bfd_error_handler (_("%pB: unable to sort relocs - "
9672 "they are of an unknown size"), abfd
);
9673 bfd_set_error (bfd_error_invalid_operation
);
9678 if (! use_rela_initialised
)
9682 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
9684 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9691 dynamic_relocs
= rela_dyn
;
9692 ext_size
= bed
->s
->sizeof_rela
;
9693 swap_in
= bed
->s
->swap_reloca_in
;
9694 swap_out
= bed
->s
->swap_reloca_out
;
9698 dynamic_relocs
= rel_dyn
;
9699 ext_size
= bed
->s
->sizeof_rel
;
9700 swap_in
= bed
->s
->swap_reloc_in
;
9701 swap_out
= bed
->s
->swap_reloc_out
;
9705 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9706 if (lo
->type
== bfd_indirect_link_order
)
9707 size
+= lo
->u
.indirect
.section
->size
;
9709 if (size
!= dynamic_relocs
->size
)
9712 sort_elt
= (sizeof (struct elf_link_sort_rela
)
9713 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
9715 count
= dynamic_relocs
->size
/ ext_size
;
9718 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
9722 (*info
->callbacks
->warning
)
9723 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
9727 if (bed
->s
->arch_size
== 32)
9728 r_sym_mask
= ~(bfd_vma
) 0xff;
9730 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
9732 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9733 if (lo
->type
== bfd_indirect_link_order
)
9735 bfd_byte
*erel
, *erelend
;
9736 asection
*o
= lo
->u
.indirect
.section
;
9738 if (o
->contents
== NULL
&& o
->size
!= 0)
9740 /* This is a reloc section that is being handled as a normal
9741 section. See bfd_section_from_shdr. We can't combine
9742 relocs in this case. */
9747 erelend
= o
->contents
+ o
->size
;
9748 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
9750 while (erel
< erelend
)
9752 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9754 (*swap_in
) (abfd
, erel
, s
->rela
);
9755 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
9756 s
->u
.sym_mask
= r_sym_mask
;
9762 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
9764 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
9766 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9767 if (s
->type
!= reloc_class_relative
)
9773 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
9774 for (; i
< count
; i
++, p
+= sort_elt
)
9776 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
9777 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
9779 sp
->u
.offset
= sq
->rela
->r_offset
;
9782 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
9784 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
9785 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
9787 /* We have plt relocs in .rela.dyn. */
9788 sq
= (struct elf_link_sort_rela
*) sort
;
9789 for (i
= 0; i
< count
; i
++)
9790 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
9792 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
9794 struct bfd_link_order
**plo
;
9795 /* Put srelplt link_order last. This is so the output_offset
9796 set in the next loop is correct for DT_JMPREL. */
9797 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
9798 if ((*plo
)->type
== bfd_indirect_link_order
9799 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
9805 plo
= &(*plo
)->next
;
9808 dynamic_relocs
->map_tail
.link_order
= lo
;
9813 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9814 if (lo
->type
== bfd_indirect_link_order
)
9816 bfd_byte
*erel
, *erelend
;
9817 asection
*o
= lo
->u
.indirect
.section
;
9820 erelend
= o
->contents
+ o
->size
;
9821 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
9822 while (erel
< erelend
)
9824 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
9825 (*swap_out
) (abfd
, s
->rela
, erel
);
9832 *psec
= dynamic_relocs
;
9836 /* Add a symbol to the output symbol string table. */
9839 elf_link_output_symstrtab (void *finf
,
9841 Elf_Internal_Sym
*elfsym
,
9842 asection
*input_sec
,
9843 struct elf_link_hash_entry
*h
)
9845 struct elf_final_link_info
*flinfo
= finf
;
9846 int (*output_symbol_hook
)
9847 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
9848 struct elf_link_hash_entry
*);
9849 struct elf_link_hash_table
*hash_table
;
9850 const struct elf_backend_data
*bed
;
9851 bfd_size_type strtabsize
;
9853 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9855 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9856 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
9857 if (output_symbol_hook
!= NULL
)
9859 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
9864 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
9865 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
9866 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
9867 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
9871 || (input_sec
->flags
& SEC_EXCLUDE
))
9872 elfsym
->st_name
= (unsigned long) -1;
9875 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9876 to get the final offset for st_name. */
9877 char *versioned_name
= (char *) name
;
9880 if (h
->versioned
== versioned
&& h
->def_dynamic
)
9882 /* Keep only one '@' for versioned symbols defined in
9884 char *version
= strrchr (name
, ELF_VER_CHR
);
9885 char *base_end
= strchr (name
, ELF_VER_CHR
);
9886 if (version
!= base_end
)
9889 size_t len
= strlen (name
);
9890 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
9891 if (versioned_name
== NULL
)
9893 base_len
= base_end
- name
;
9894 memcpy (versioned_name
, name
, base_len
);
9895 memcpy (versioned_name
+ base_len
, version
,
9900 else if (flinfo
->info
->unique_symbol
9901 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
9903 struct local_hash_entry
*lh
;
9907 switch (ELF_ST_TYPE (elfsym
->st_info
))
9913 lh
= (struct local_hash_entry
*) bfd_hash_lookup
9914 (&flinfo
->local_hash_table
, name
, true, false);
9917 /* Always append ".COUNT" to local symbols to avoid
9918 potential conflicts with local symbol "XXX.COUNT". */
9919 sprintf (buf
, "%lx", lh
->count
);
9920 base_len
= lh
->size
;
9923 base_len
= strlen (name
);
9924 lh
->size
= base_len
;
9926 count_len
= strlen (buf
);
9927 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
9928 base_len
+ count_len
+ 2);
9929 if (versioned_name
== NULL
)
9931 memcpy (versioned_name
, name
, base_len
);
9932 versioned_name
[base_len
] = '.';
9933 memcpy (versioned_name
+ base_len
+ 1, buf
,
9940 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
9941 versioned_name
, false);
9942 if (elfsym
->st_name
== (unsigned long) -1)
9946 hash_table
= elf_hash_table (flinfo
->info
);
9947 strtabsize
= hash_table
->strtabsize
;
9948 if (strtabsize
<= flinfo
->output_bfd
->symcount
)
9950 strtabsize
+= strtabsize
;
9951 hash_table
->strtabsize
= strtabsize
;
9952 strtabsize
*= sizeof (*hash_table
->strtab
);
9954 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
9956 if (hash_table
->strtab
== NULL
)
9959 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].sym
= *elfsym
;
9960 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].dest_index
9961 = flinfo
->output_bfd
->symcount
;
9962 flinfo
->output_bfd
->symcount
+= 1;
9967 /* Swap symbols out to the symbol table and flush the output symbols to
9971 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
9973 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
9976 const struct elf_backend_data
*bed
;
9978 Elf_Internal_Shdr
*hdr
;
9982 if (flinfo
->output_bfd
->symcount
== 0)
9985 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
9987 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9989 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
9990 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
9994 if (flinfo
->symshndxbuf
)
9996 amt
= sizeof (Elf_External_Sym_Shndx
);
9997 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
9998 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
9999 if (flinfo
->symshndxbuf
== NULL
)
10006 /* Now swap out the symbols. */
10007 for (i
= 0; i
< flinfo
->output_bfd
->symcount
; i
++)
10009 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
10010 if (elfsym
->sym
.st_name
== (unsigned long) -1)
10011 elfsym
->sym
.st_name
= 0;
10013 elfsym
->sym
.st_name
10014 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
10015 elfsym
->sym
.st_name
);
10017 /* Inform the linker of the addition of this symbol. */
10019 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
10020 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
10023 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
10024 ((bfd_byte
*) symbuf
10025 + (elfsym
->dest_index
10026 * bed
->s
->sizeof_sym
)),
10027 NPTR_ADD (flinfo
->symshndxbuf
,
10028 elfsym
->dest_index
));
10031 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
10032 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
10033 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10034 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
10035 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
10037 hdr
->sh_size
+= amt
;
10045 free (hash_table
->strtab
);
10046 hash_table
->strtab
= NULL
;
10051 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10054 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
10056 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
10057 && sym
->st_shndx
< SHN_LORESERVE
)
10059 /* The gABI doesn't support dynamic symbols in output sections
10062 /* xgettext:c-format */
10063 (_("%pB: too many sections: %d (>= %d)"),
10064 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
10065 bfd_set_error (bfd_error_nonrepresentable_section
);
10071 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10072 allowing an unsatisfied unversioned symbol in the DSO to match a
10073 versioned symbol that would normally require an explicit version.
10074 We also handle the case that a DSO references a hidden symbol
10075 which may be satisfied by a versioned symbol in another DSO. */
10078 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
10079 const struct elf_backend_data
*bed
,
10080 struct elf_link_hash_entry
*h
)
10083 struct elf_link_loaded_list
*loaded
;
10085 if (!is_elf_hash_table (info
->hash
))
10088 /* Check indirect symbol. */
10089 while (h
->root
.type
== bfd_link_hash_indirect
)
10090 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10092 switch (h
->root
.type
)
10098 case bfd_link_hash_undefined
:
10099 case bfd_link_hash_undefweak
:
10100 abfd
= h
->root
.u
.undef
.abfd
;
10102 || (abfd
->flags
& DYNAMIC
) == 0
10103 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
10107 case bfd_link_hash_defined
:
10108 case bfd_link_hash_defweak
:
10109 abfd
= h
->root
.u
.def
.section
->owner
;
10112 case bfd_link_hash_common
:
10113 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10116 BFD_ASSERT (abfd
!= NULL
);
10118 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10120 loaded
= loaded
->next
)
10123 Elf_Internal_Shdr
*hdr
;
10125 size_t extsymcount
;
10127 Elf_Internal_Shdr
*versymhdr
;
10128 Elf_Internal_Sym
*isym
;
10129 Elf_Internal_Sym
*isymend
;
10130 Elf_Internal_Sym
*isymbuf
;
10131 Elf_External_Versym
*ever
;
10132 Elf_External_Versym
*extversym
;
10134 input
= loaded
->abfd
;
10136 /* We check each DSO for a possible hidden versioned definition. */
10138 || elf_dynversym (input
) == 0)
10141 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10143 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10144 if (elf_bad_symtab (input
))
10146 extsymcount
= symcount
;
10151 extsymcount
= symcount
- hdr
->sh_info
;
10152 extsymoff
= hdr
->sh_info
;
10155 if (extsymcount
== 0)
10158 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10160 if (isymbuf
== NULL
)
10163 /* Read in any version definitions. */
10164 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10165 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10166 || (extversym
= (Elf_External_Versym
*)
10167 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10168 versymhdr
->sh_size
)) == NULL
)
10174 ever
= extversym
+ extsymoff
;
10175 isymend
= isymbuf
+ extsymcount
;
10176 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10179 Elf_Internal_Versym iver
;
10180 unsigned short version_index
;
10182 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10183 || isym
->st_shndx
== SHN_UNDEF
)
10186 name
= bfd_elf_string_from_elf_section (input
,
10189 if (strcmp (name
, h
->root
.root
.string
) != 0)
10192 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10194 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10195 && !(h
->def_regular
10196 && h
->forced_local
))
10198 /* If we have a non-hidden versioned sym, then it should
10199 have provided a definition for the undefined sym unless
10200 it is defined in a non-shared object and forced local.
10205 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10206 if (version_index
== 1 || version_index
== 2)
10208 /* This is the base or first version. We can use it. */
10222 /* Convert ELF common symbol TYPE. */
10225 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10227 /* Commom symbol can only appear in relocatable link. */
10228 if (!bfd_link_relocatable (info
))
10230 switch (info
->elf_stt_common
)
10234 case elf_stt_common
:
10237 case no_elf_stt_common
:
10244 /* Add an external symbol to the symbol table. This is called from
10245 the hash table traversal routine. When generating a shared object,
10246 we go through the symbol table twice. The first time we output
10247 anything that might have been forced to local scope in a version
10248 script. The second time we output the symbols that are still
10252 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10254 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10255 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10256 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10258 Elf_Internal_Sym sym
;
10259 asection
*input_sec
;
10260 const struct elf_backend_data
*bed
;
10265 if (h
->root
.type
== bfd_link_hash_warning
)
10267 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10268 if (h
->root
.type
== bfd_link_hash_new
)
10272 /* Decide whether to output this symbol in this pass. */
10273 if (eoinfo
->localsyms
)
10275 if (!h
->forced_local
)
10280 if (h
->forced_local
)
10284 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10286 if (h
->root
.type
== bfd_link_hash_undefined
)
10288 /* If we have an undefined symbol reference here then it must have
10289 come from a shared library that is being linked in. (Undefined
10290 references in regular files have already been handled unless
10291 they are in unreferenced sections which are removed by garbage
10293 bool ignore_undef
= false;
10295 /* Some symbols may be special in that the fact that they're
10296 undefined can be safely ignored - let backend determine that. */
10297 if (bed
->elf_backend_ignore_undef_symbol
)
10298 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10300 /* If we are reporting errors for this situation then do so now. */
10302 && h
->ref_dynamic_nonweak
10303 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10304 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10305 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10307 flinfo
->info
->callbacks
->undefined_symbol
10308 (flinfo
->info
, h
->root
.root
.string
,
10309 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10310 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10311 && !flinfo
->info
->warn_unresolved_syms
);
10314 /* Strip a global symbol defined in a discarded section. */
10319 /* We should also warn if a forced local symbol is referenced from
10320 shared libraries. */
10321 if (bfd_link_executable (flinfo
->info
)
10326 && h
->ref_dynamic_nonweak
10327 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10331 struct elf_link_hash_entry
*hi
= h
;
10333 /* Check indirect symbol. */
10334 while (hi
->root
.type
== bfd_link_hash_indirect
)
10335 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10337 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10338 /* xgettext:c-format */
10339 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10340 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10341 /* xgettext:c-format */
10342 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10344 /* xgettext:c-format */
10345 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10346 def_bfd
= flinfo
->output_bfd
;
10347 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10348 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10349 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10350 h
->root
.root
.string
, def_bfd
);
10351 bfd_set_error (bfd_error_bad_value
);
10352 eoinfo
->failed
= true;
10356 /* We don't want to output symbols that have never been mentioned by
10357 a regular file, or that we have been told to strip. However, if
10358 h->indx is set to -2, the symbol is used by a reloc and we must
10363 else if ((h
->def_dynamic
10365 || h
->root
.type
== bfd_link_hash_new
)
10367 && !h
->ref_regular
)
10369 else if (flinfo
->info
->strip
== strip_all
)
10371 else if (flinfo
->info
->strip
== strip_some
10372 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10373 h
->root
.root
.string
, false, false) == NULL
)
10375 else if ((h
->root
.type
== bfd_link_hash_defined
10376 || h
->root
.type
== bfd_link_hash_defweak
)
10377 && ((flinfo
->info
->strip_discarded
10378 && discarded_section (h
->root
.u
.def
.section
))
10379 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10380 && h
->root
.u
.def
.section
->owner
!= NULL
10381 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10383 else if ((h
->root
.type
== bfd_link_hash_undefined
10384 || h
->root
.type
== bfd_link_hash_undefweak
)
10385 && h
->root
.u
.undef
.abfd
!= NULL
10386 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10391 /* If we're stripping it, and it's not a dynamic symbol, there's
10392 nothing else to do. However, if it is a forced local symbol or
10393 an ifunc symbol we need to give the backend finish_dynamic_symbol
10394 function a chance to make it dynamic. */
10396 && h
->dynindx
== -1
10397 && type
!= STT_GNU_IFUNC
10398 && !h
->forced_local
)
10402 sym
.st_size
= h
->size
;
10403 sym
.st_other
= h
->other
;
10404 switch (h
->root
.type
)
10407 case bfd_link_hash_new
:
10408 case bfd_link_hash_warning
:
10412 case bfd_link_hash_undefined
:
10413 case bfd_link_hash_undefweak
:
10414 input_sec
= bfd_und_section_ptr
;
10415 sym
.st_shndx
= SHN_UNDEF
;
10418 case bfd_link_hash_defined
:
10419 case bfd_link_hash_defweak
:
10421 input_sec
= h
->root
.u
.def
.section
;
10422 if (input_sec
->output_section
!= NULL
)
10425 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10426 input_sec
->output_section
);
10427 if (sym
.st_shndx
== SHN_BAD
)
10430 /* xgettext:c-format */
10431 (_("%pB: could not find output section %pA for input section %pA"),
10432 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10433 bfd_set_error (bfd_error_nonrepresentable_section
);
10434 eoinfo
->failed
= true;
10438 /* ELF symbols in relocatable files are section relative,
10439 but in nonrelocatable files they are virtual
10441 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10442 if (!bfd_link_relocatable (flinfo
->info
))
10444 sym
.st_value
+= input_sec
->output_section
->vma
;
10445 if (h
->type
== STT_TLS
)
10447 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10448 if (tls_sec
!= NULL
)
10449 sym
.st_value
-= tls_sec
->vma
;
10455 BFD_ASSERT (input_sec
->owner
== NULL
10456 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10457 sym
.st_shndx
= SHN_UNDEF
;
10458 input_sec
= bfd_und_section_ptr
;
10463 case bfd_link_hash_common
:
10464 input_sec
= h
->root
.u
.c
.p
->section
;
10465 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10466 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10469 case bfd_link_hash_indirect
:
10470 /* These symbols are created by symbol versioning. They point
10471 to the decorated version of the name. For example, if the
10472 symbol foo@@GNU_1.2 is the default, which should be used when
10473 foo is used with no version, then we add an indirect symbol
10474 foo which points to foo@@GNU_1.2. We ignore these symbols,
10475 since the indirected symbol is already in the hash table. */
10479 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10480 switch (h
->root
.type
)
10482 case bfd_link_hash_common
:
10483 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10485 case bfd_link_hash_defined
:
10486 case bfd_link_hash_defweak
:
10487 if (bed
->common_definition (&sym
))
10488 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10492 case bfd_link_hash_undefined
:
10493 case bfd_link_hash_undefweak
:
10499 if (h
->forced_local
)
10501 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10502 /* Turn off visibility on local symbol. */
10503 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10505 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10506 else if (h
->unique_global
&& h
->def_regular
)
10507 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10508 else if (h
->root
.type
== bfd_link_hash_undefweak
10509 || h
->root
.type
== bfd_link_hash_defweak
)
10510 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10512 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10513 sym
.st_target_internal
= h
->target_internal
;
10515 /* Give the processor backend a chance to tweak the symbol value,
10516 and also to finish up anything that needs to be done for this
10517 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10518 forced local syms when non-shared is due to a historical quirk.
10519 STT_GNU_IFUNC symbol must go through PLT. */
10520 if ((h
->type
== STT_GNU_IFUNC
10522 && !bfd_link_relocatable (flinfo
->info
))
10523 || ((h
->dynindx
!= -1
10524 || h
->forced_local
)
10525 && ((bfd_link_pic (flinfo
->info
)
10526 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10527 || h
->root
.type
!= bfd_link_hash_undefweak
))
10528 || !h
->forced_local
)
10529 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10531 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10532 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10534 eoinfo
->failed
= true;
10539 /* If we are marking the symbol as undefined, and there are no
10540 non-weak references to this symbol from a regular object, then
10541 mark the symbol as weak undefined; if there are non-weak
10542 references, mark the symbol as strong. We can't do this earlier,
10543 because it might not be marked as undefined until the
10544 finish_dynamic_symbol routine gets through with it. */
10545 if (sym
.st_shndx
== SHN_UNDEF
10547 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10548 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10551 type
= ELF_ST_TYPE (sym
.st_info
);
10553 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10554 if (type
== STT_GNU_IFUNC
)
10557 if (h
->ref_regular_nonweak
)
10558 bindtype
= STB_GLOBAL
;
10560 bindtype
= STB_WEAK
;
10561 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10564 /* If this is a symbol defined in a dynamic library, don't use the
10565 symbol size from the dynamic library. Relinking an executable
10566 against a new library may introduce gratuitous changes in the
10567 executable's symbols if we keep the size. */
10568 if (sym
.st_shndx
== SHN_UNDEF
10573 /* If a non-weak symbol with non-default visibility is not defined
10574 locally, it is a fatal error. */
10575 if (!bfd_link_relocatable (flinfo
->info
)
10576 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10577 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10578 && h
->root
.type
== bfd_link_hash_undefined
10579 && !h
->def_regular
)
10583 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10584 /* xgettext:c-format */
10585 msg
= _("%pB: protected symbol `%s' isn't defined");
10586 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10587 /* xgettext:c-format */
10588 msg
= _("%pB: internal symbol `%s' isn't defined");
10590 /* xgettext:c-format */
10591 msg
= _("%pB: hidden symbol `%s' isn't defined");
10592 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10593 bfd_set_error (bfd_error_bad_value
);
10594 eoinfo
->failed
= true;
10598 /* If this symbol should be put in the .dynsym section, then put it
10599 there now. We already know the symbol index. We also fill in
10600 the entry in the .hash section. */
10601 if (h
->dynindx
!= -1
10602 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10603 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10604 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10608 /* Since there is no version information in the dynamic string,
10609 if there is no version info in symbol version section, we will
10610 have a run-time problem if not linking executable, referenced
10611 by shared library, or not bound locally. */
10612 if (h
->verinfo
.verdef
== NULL
10613 && (!bfd_link_executable (flinfo
->info
)
10615 || !h
->def_regular
))
10617 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
10619 if (p
&& p
[1] != '\0')
10622 /* xgettext:c-format */
10623 (_("%pB: no symbol version section for versioned symbol `%s'"),
10624 flinfo
->output_bfd
, h
->root
.root
.string
);
10625 eoinfo
->failed
= true;
10630 sym
.st_name
= h
->dynstr_index
;
10631 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
10632 + h
->dynindx
* bed
->s
->sizeof_sym
);
10633 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
10635 eoinfo
->failed
= true;
10639 /* Inform the linker of the addition of this symbol. */
10641 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
10642 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
10644 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
10646 if (flinfo
->hash_sec
!= NULL
)
10648 size_t hash_entry_size
;
10649 bfd_byte
*bucketpos
;
10651 size_t bucketcount
;
10654 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
10655 bucket
= h
->u
.elf_hash_value
% bucketcount
;
10658 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
10659 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
10660 + (bucket
+ 2) * hash_entry_size
);
10661 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
10662 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
10664 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
10665 ((bfd_byte
*) flinfo
->hash_sec
->contents
10666 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
10669 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
10671 Elf_Internal_Versym iversym
;
10672 Elf_External_Versym
*eversym
;
10674 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
10676 if (h
->verinfo
.verdef
== NULL
10677 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
10678 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
10679 iversym
.vs_vers
= 1;
10681 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
10685 if (h
->verinfo
.vertree
== NULL
)
10686 iversym
.vs_vers
= 1;
10688 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
10689 if (flinfo
->info
->create_default_symver
)
10693 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10694 defined locally. */
10695 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
10696 iversym
.vs_vers
|= VERSYM_HIDDEN
;
10698 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
10699 eversym
+= h
->dynindx
;
10700 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
10704 /* If the symbol is undefined, and we didn't output it to .dynsym,
10705 strip it from .symtab too. Obviously we can't do this for
10706 relocatable output or when needed for --emit-relocs. */
10707 else if (input_sec
== bfd_und_section_ptr
10709 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10710 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
10711 && !bfd_link_relocatable (flinfo
->info
))
10714 /* Also strip others that we couldn't earlier due to dynamic symbol
10718 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
10721 /* Output a FILE symbol so that following locals are not associated
10722 with the wrong input file. We need one for forced local symbols
10723 if we've seen more than one FILE symbol or when we have exactly
10724 one FILE symbol but global symbols are present in a file other
10725 than the one with the FILE symbol. We also need one if linker
10726 defined symbols are present. In practice these conditions are
10727 always met, so just emit the FILE symbol unconditionally. */
10728 if (eoinfo
->localsyms
10729 && !eoinfo
->file_sym_done
10730 && eoinfo
->flinfo
->filesym_count
!= 0)
10732 Elf_Internal_Sym fsym
;
10734 memset (&fsym
, 0, sizeof (fsym
));
10735 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
10736 fsym
.st_shndx
= SHN_ABS
;
10737 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
10738 bfd_und_section_ptr
, NULL
))
10741 eoinfo
->file_sym_done
= true;
10744 indx
= bfd_get_symcount (flinfo
->output_bfd
);
10745 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
10749 eoinfo
->failed
= true;
10754 else if (h
->indx
== -2)
10760 /* Return TRUE if special handling is done for relocs in SEC against
10761 symbols defined in discarded sections. */
10764 elf_section_ignore_discarded_relocs (asection
*sec
)
10766 const struct elf_backend_data
*bed
;
10768 switch (sec
->sec_info_type
)
10770 case SEC_INFO_TYPE_STABS
:
10771 case SEC_INFO_TYPE_EH_FRAME
:
10772 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10778 bed
= get_elf_backend_data (sec
->owner
);
10779 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
10780 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
10786 /* Return a mask saying how ld should treat relocations in SEC against
10787 symbols defined in discarded sections. If this function returns
10788 COMPLAIN set, ld will issue a warning message. If this function
10789 returns PRETEND set, and the discarded section was link-once and the
10790 same size as the kept link-once section, ld will pretend that the
10791 symbol was actually defined in the kept section. Otherwise ld will
10792 zero the reloc (at least that is the intent, but some cooperation by
10793 the target dependent code is needed, particularly for REL targets). */
10796 _bfd_elf_default_action_discarded (asection
*sec
)
10798 if (sec
->flags
& SEC_DEBUGGING
)
10801 if (strcmp (".eh_frame", sec
->name
) == 0)
10804 if (strcmp (".gcc_except_table", sec
->name
) == 0)
10807 return COMPLAIN
| PRETEND
;
10810 /* Find a match between a section and a member of a section group. */
10813 match_group_member (asection
*sec
, asection
*group
,
10814 struct bfd_link_info
*info
)
10816 asection
*first
= elf_next_in_group (group
);
10817 asection
*s
= first
;
10821 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
10824 s
= elf_next_in_group (s
);
10832 /* Check if the kept section of a discarded section SEC can be used
10833 to replace it. Return the replacement if it is OK. Otherwise return
10837 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
10841 kept
= sec
->kept_section
;
10844 if ((kept
->flags
& SEC_GROUP
) != 0)
10845 kept
= match_group_member (sec
, kept
, info
);
10848 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
10849 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
10853 /* Get the real kept section. */
10855 for (next
= kept
->kept_section
;
10857 next
= next
->kept_section
)
10861 sec
->kept_section
= kept
;
10866 /* Link an input file into the linker output file. This function
10867 handles all the sections and relocations of the input file at once.
10868 This is so that we only have to read the local symbols once, and
10869 don't have to keep them in memory. */
10872 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
10874 int (*relocate_section
)
10875 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
10876 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
10878 Elf_Internal_Shdr
*symtab_hdr
;
10879 size_t locsymcount
;
10881 Elf_Internal_Sym
*isymbuf
;
10882 Elf_Internal_Sym
*isym
;
10883 Elf_Internal_Sym
*isymend
;
10885 asection
**ppsection
;
10887 const struct elf_backend_data
*bed
;
10888 struct elf_link_hash_entry
**sym_hashes
;
10889 bfd_size_type address_size
;
10890 bfd_vma r_type_mask
;
10892 bool have_file_sym
= false;
10894 output_bfd
= flinfo
->output_bfd
;
10895 bed
= get_elf_backend_data (output_bfd
);
10896 relocate_section
= bed
->elf_backend_relocate_section
;
10898 /* If this is a dynamic object, we don't want to do anything here:
10899 we don't want the local symbols, and we don't want the section
10901 if ((input_bfd
->flags
& DYNAMIC
) != 0)
10904 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
10905 if (elf_bad_symtab (input_bfd
))
10907 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10912 locsymcount
= symtab_hdr
->sh_info
;
10913 extsymoff
= symtab_hdr
->sh_info
;
10916 /* Enable GNU OSABI features in the output BFD that are used in the input
10918 if (bed
->elf_osabi
== ELFOSABI_NONE
10919 || bed
->elf_osabi
== ELFOSABI_GNU
10920 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
10921 elf_tdata (output_bfd
)->has_gnu_osabi
10922 |= (elf_tdata (input_bfd
)->has_gnu_osabi
10923 & (bfd_link_relocatable (flinfo
->info
)
10924 ? -1 : ~elf_gnu_osabi_retain
));
10926 /* Read the local symbols. */
10927 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
10928 if (isymbuf
== NULL
&& locsymcount
!= 0)
10930 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
10931 flinfo
->internal_syms
,
10932 flinfo
->external_syms
,
10933 flinfo
->locsym_shndx
);
10934 if (isymbuf
== NULL
)
10938 /* Find local symbol sections and adjust values of symbols in
10939 SEC_MERGE sections. Write out those local symbols we know are
10940 going into the output file. */
10941 isymend
= PTR_ADD (isymbuf
, locsymcount
);
10942 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
10944 isym
++, pindex
++, ppsection
++)
10948 Elf_Internal_Sym osym
;
10954 if (elf_bad_symtab (input_bfd
))
10956 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
10963 if (isym
->st_shndx
== SHN_UNDEF
)
10964 isec
= bfd_und_section_ptr
;
10965 else if (isym
->st_shndx
== SHN_ABS
)
10966 isec
= bfd_abs_section_ptr
;
10967 else if (isym
->st_shndx
== SHN_COMMON
)
10968 isec
= bfd_com_section_ptr
;
10971 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
10974 /* Don't attempt to output symbols with st_shnx in the
10975 reserved range other than SHN_ABS and SHN_COMMON. */
10976 isec
= bfd_und_section_ptr
;
10978 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
10979 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
10981 _bfd_merged_section_offset (output_bfd
, &isec
,
10982 elf_section_data (isec
)->sec_info
,
10988 /* Don't output the first, undefined, symbol. In fact, don't
10989 output any undefined local symbol. */
10990 if (isec
== bfd_und_section_ptr
)
10993 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
10995 /* We never output section symbols. Instead, we use the
10996 section symbol of the corresponding section in the output
11001 /* If we are stripping all symbols, we don't want to output this
11003 if (flinfo
->info
->strip
== strip_all
)
11006 /* If we are discarding all local symbols, we don't want to
11007 output this one. If we are generating a relocatable output
11008 file, then some of the local symbols may be required by
11009 relocs; we output them below as we discover that they are
11011 if (flinfo
->info
->discard
== discard_all
)
11014 /* If this symbol is defined in a section which we are
11015 discarding, we don't need to keep it. */
11016 if (isym
->st_shndx
!= SHN_UNDEF
11017 && isym
->st_shndx
< SHN_LORESERVE
11018 && isec
->output_section
== NULL
11019 && flinfo
->info
->non_contiguous_regions
11020 && flinfo
->info
->non_contiguous_regions_warnings
)
11022 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
11023 "discards section `%s' from '%s'\n"),
11024 isec
->name
, bfd_get_filename (isec
->owner
));
11028 if (isym
->st_shndx
!= SHN_UNDEF
11029 && isym
->st_shndx
< SHN_LORESERVE
11030 && bfd_section_removed_from_list (output_bfd
,
11031 isec
->output_section
))
11034 /* Get the name of the symbol. */
11035 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
11040 /* See if we are discarding symbols with this name. */
11041 if ((flinfo
->info
->strip
== strip_some
11042 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, false, false)
11044 || (((flinfo
->info
->discard
== discard_sec_merge
11045 && (isec
->flags
& SEC_MERGE
)
11046 && !bfd_link_relocatable (flinfo
->info
))
11047 || flinfo
->info
->discard
== discard_l
)
11048 && bfd_is_local_label_name (input_bfd
, name
)))
11051 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
11053 if (input_bfd
->lto_output
)
11054 /* -flto puts a temp file name here. This means builds
11055 are not reproducible. Discard the symbol. */
11057 have_file_sym
= true;
11058 flinfo
->filesym_count
+= 1;
11060 if (!have_file_sym
)
11062 /* In the absence of debug info, bfd_find_nearest_line uses
11063 FILE symbols to determine the source file for local
11064 function symbols. Provide a FILE symbol here if input
11065 files lack such, so that their symbols won't be
11066 associated with a previous input file. It's not the
11067 source file, but the best we can do. */
11068 const char *filename
;
11069 have_file_sym
= true;
11070 flinfo
->filesym_count
+= 1;
11071 memset (&osym
, 0, sizeof (osym
));
11072 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11073 osym
.st_shndx
= SHN_ABS
;
11074 if (input_bfd
->lto_output
)
11077 filename
= lbasename (bfd_get_filename (input_bfd
));
11078 if (!elf_link_output_symstrtab (flinfo
, filename
, &osym
,
11079 bfd_abs_section_ptr
, NULL
))
11085 /* Adjust the section index for the output file. */
11086 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11087 isec
->output_section
);
11088 if (osym
.st_shndx
== SHN_BAD
)
11091 /* ELF symbols in relocatable files are section relative, but
11092 in executable files they are virtual addresses. Note that
11093 this code assumes that all ELF sections have an associated
11094 BFD section with a reasonable value for output_offset; below
11095 we assume that they also have a reasonable value for
11096 output_section. Any special sections must be set up to meet
11097 these requirements. */
11098 osym
.st_value
+= isec
->output_offset
;
11099 if (!bfd_link_relocatable (flinfo
->info
))
11101 osym
.st_value
+= isec
->output_section
->vma
;
11102 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
11104 /* STT_TLS symbols are relative to PT_TLS segment base. */
11105 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
11106 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
11108 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
11113 indx
= bfd_get_symcount (output_bfd
);
11114 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11121 if (bed
->s
->arch_size
== 32)
11123 r_type_mask
= 0xff;
11129 r_type_mask
= 0xffffffff;
11134 /* Relocate the contents of each section. */
11135 sym_hashes
= elf_sym_hashes (input_bfd
);
11136 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11138 bfd_byte
*contents
;
11140 if (! o
->linker_mark
)
11142 /* This section was omitted from the link. */
11146 if (!flinfo
->info
->resolve_section_groups
11147 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11149 /* Deal with the group signature symbol. */
11150 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11151 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11152 asection
*osec
= o
->output_section
;
11154 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11155 if (symndx
>= locsymcount
11156 || (elf_bad_symtab (input_bfd
)
11157 && flinfo
->sections
[symndx
] == NULL
))
11159 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
11160 while (h
->root
.type
== bfd_link_hash_indirect
11161 || h
->root
.type
== bfd_link_hash_warning
)
11162 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11163 /* Arrange for symbol to be output. */
11165 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11167 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11169 /* We'll use the output section target_index. */
11170 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11171 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11175 if (flinfo
->indices
[symndx
] == -1)
11177 /* Otherwise output the local symbol now. */
11178 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11179 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11184 name
= bfd_elf_string_from_elf_section (input_bfd
,
11185 symtab_hdr
->sh_link
,
11190 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11192 if (sym
.st_shndx
== SHN_BAD
)
11195 sym
.st_value
+= o
->output_offset
;
11197 indx
= bfd_get_symcount (output_bfd
);
11198 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11203 flinfo
->indices
[symndx
] = indx
;
11207 elf_section_data (osec
)->this_hdr
.sh_info
11208 = flinfo
->indices
[symndx
];
11212 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11213 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11216 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11218 /* Section was created by _bfd_elf_link_create_dynamic_sections
11223 /* Get the contents of the section. They have been cached by a
11224 relaxation routine. Note that o is a section in an input
11225 file, so the contents field will not have been set by any of
11226 the routines which work on output files. */
11227 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11229 contents
= elf_section_data (o
)->this_hdr
.contents
;
11230 if (bed
->caches_rawsize
11232 && o
->rawsize
< o
->size
)
11234 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11235 contents
= flinfo
->contents
;
11240 contents
= flinfo
->contents
;
11241 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
11245 if ((o
->flags
& SEC_RELOC
) != 0)
11247 Elf_Internal_Rela
*internal_relocs
;
11248 Elf_Internal_Rela
*rel
, *relend
;
11249 int action_discarded
;
11252 /* Get the swapped relocs. */
11254 = _bfd_elf_link_info_read_relocs (input_bfd
, flinfo
->info
, o
,
11255 flinfo
->external_relocs
,
11256 flinfo
->internal_relocs
,
11258 if (internal_relocs
== NULL
11259 && o
->reloc_count
> 0)
11262 action_discarded
= -1;
11263 if (!elf_section_ignore_discarded_relocs (o
))
11264 action_discarded
= (*bed
->action_discarded
) (o
);
11266 /* Run through the relocs evaluating complex reloc symbols and
11267 looking for relocs against symbols from discarded sections
11268 or section symbols from removed link-once sections.
11269 Complain about relocs against discarded sections. Zero
11270 relocs against removed link-once sections. */
11272 rel
= internal_relocs
;
11273 relend
= rel
+ o
->reloc_count
;
11274 for ( ; rel
< relend
; rel
++)
11276 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11277 unsigned int s_type
;
11278 asection
**ps
, *sec
;
11279 struct elf_link_hash_entry
*h
= NULL
;
11280 const char *sym_name
;
11282 if (r_symndx
== STN_UNDEF
)
11285 if (r_symndx
>= locsymcount
11286 || (elf_bad_symtab (input_bfd
)
11287 && flinfo
->sections
[r_symndx
] == NULL
))
11289 h
= sym_hashes
[r_symndx
- extsymoff
];
11291 /* Badly formatted input files can contain relocs that
11292 reference non-existant symbols. Check here so that
11293 we do not seg fault. */
11297 /* xgettext:c-format */
11298 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11299 "that references a non-existent global symbol"),
11300 input_bfd
, (uint64_t) rel
->r_info
, o
);
11301 bfd_set_error (bfd_error_bad_value
);
11305 while (h
->root
.type
== bfd_link_hash_indirect
11306 || h
->root
.type
== bfd_link_hash_warning
)
11307 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11311 /* If a plugin symbol is referenced from a non-IR file,
11312 mark the symbol as undefined. Note that the
11313 linker may attach linker created dynamic sections
11314 to the plugin bfd. Symbols defined in linker
11315 created sections are not plugin symbols. */
11316 if ((h
->root
.non_ir_ref_regular
11317 || h
->root
.non_ir_ref_dynamic
)
11318 && (h
->root
.type
== bfd_link_hash_defined
11319 || h
->root
.type
== bfd_link_hash_defweak
)
11320 && (h
->root
.u
.def
.section
->flags
11321 & SEC_LINKER_CREATED
) == 0
11322 && h
->root
.u
.def
.section
->owner
!= NULL
11323 && (h
->root
.u
.def
.section
->owner
->flags
11324 & BFD_PLUGIN
) != 0)
11326 h
->root
.type
= bfd_link_hash_undefined
;
11327 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11331 if (h
->root
.type
== bfd_link_hash_defined
11332 || h
->root
.type
== bfd_link_hash_defweak
)
11333 ps
= &h
->root
.u
.def
.section
;
11335 sym_name
= h
->root
.root
.string
;
11339 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11341 s_type
= ELF_ST_TYPE (sym
->st_info
);
11342 ps
= &flinfo
->sections
[r_symndx
];
11343 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11347 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11348 && !bfd_link_relocatable (flinfo
->info
))
11351 bfd_vma dot
= (rel
->r_offset
11352 + o
->output_offset
+ o
->output_section
->vma
);
11354 printf ("Encountered a complex symbol!");
11355 printf (" (input_bfd %s, section %s, reloc %ld\n",
11356 bfd_get_filename (input_bfd
), o
->name
,
11357 (long) (rel
- internal_relocs
));
11358 printf (" symbol: idx %8.8lx, name %s\n",
11359 r_symndx
, sym_name
);
11360 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11361 (unsigned long) rel
->r_info
,
11362 (unsigned long) rel
->r_offset
);
11364 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11365 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11368 /* Symbol evaluated OK. Update to absolute value. */
11369 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11374 if (action_discarded
!= -1 && ps
!= NULL
)
11376 /* Complain if the definition comes from a
11377 discarded section. */
11378 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11380 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11381 if (action_discarded
& COMPLAIN
)
11382 (*flinfo
->info
->callbacks
->einfo
)
11383 /* xgettext:c-format */
11384 (_("%X`%s' referenced in section `%pA' of %pB: "
11385 "defined in discarded section `%pA' of %pB\n"),
11386 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11388 /* Try to do the best we can to support buggy old
11389 versions of gcc. Pretend that the symbol is
11390 really defined in the kept linkonce section.
11391 FIXME: This is quite broken. Modifying the
11392 symbol here means we will be changing all later
11393 uses of the symbol, not just in this section. */
11394 if (action_discarded
& PRETEND
)
11398 kept
= _bfd_elf_check_kept_section (sec
,
11410 /* Relocate the section by invoking a back end routine.
11412 The back end routine is responsible for adjusting the
11413 section contents as necessary, and (if using Rela relocs
11414 and generating a relocatable output file) adjusting the
11415 reloc addend as necessary.
11417 The back end routine does not have to worry about setting
11418 the reloc address or the reloc symbol index.
11420 The back end routine is given a pointer to the swapped in
11421 internal symbols, and can access the hash table entries
11422 for the external symbols via elf_sym_hashes (input_bfd).
11424 When generating relocatable output, the back end routine
11425 must handle STB_LOCAL/STT_SECTION symbols specially. The
11426 output symbol is going to be a section symbol
11427 corresponding to the output section, which will require
11428 the addend to be adjusted. */
11430 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11431 input_bfd
, o
, contents
,
11439 || bfd_link_relocatable (flinfo
->info
)
11440 || flinfo
->info
->emitrelocations
)
11442 Elf_Internal_Rela
*irela
;
11443 Elf_Internal_Rela
*irelaend
, *irelamid
;
11444 bfd_vma last_offset
;
11445 struct elf_link_hash_entry
**rel_hash
;
11446 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11447 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11448 unsigned int next_erel
;
11450 struct bfd_elf_section_data
*esdi
, *esdo
;
11452 esdi
= elf_section_data (o
);
11453 esdo
= elf_section_data (o
->output_section
);
11454 rela_normal
= false;
11456 /* Adjust the reloc addresses and symbol indices. */
11458 irela
= internal_relocs
;
11459 irelaend
= irela
+ o
->reloc_count
;
11460 rel_hash
= PTR_ADD (esdo
->rel
.hashes
, esdo
->rel
.count
);
11461 /* We start processing the REL relocs, if any. When we reach
11462 IRELAMID in the loop, we switch to the RELA relocs. */
11464 if (esdi
->rel
.hdr
!= NULL
)
11465 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11466 * bed
->s
->int_rels_per_ext_rel
);
11467 rel_hash_list
= rel_hash
;
11468 rela_hash_list
= NULL
;
11469 last_offset
= o
->output_offset
;
11470 if (!bfd_link_relocatable (flinfo
->info
))
11471 last_offset
+= o
->output_section
->vma
;
11472 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11474 unsigned long r_symndx
;
11476 Elf_Internal_Sym sym
;
11478 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11484 if (irela
== irelamid
)
11486 rel_hash
= PTR_ADD (esdo
->rela
.hashes
, esdo
->rela
.count
);
11487 rela_hash_list
= rel_hash
;
11488 rela_normal
= bed
->rela_normal
;
11491 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11494 if (irela
->r_offset
>= (bfd_vma
) -2)
11496 /* This is a reloc for a deleted entry or somesuch.
11497 Turn it into an R_*_NONE reloc, at the same
11498 offset as the last reloc. elf_eh_frame.c and
11499 bfd_elf_discard_info rely on reloc offsets
11501 irela
->r_offset
= last_offset
;
11503 irela
->r_addend
= 0;
11507 irela
->r_offset
+= o
->output_offset
;
11509 /* Relocs in an executable have to be virtual addresses. */
11510 if (!bfd_link_relocatable (flinfo
->info
))
11511 irela
->r_offset
+= o
->output_section
->vma
;
11513 last_offset
= irela
->r_offset
;
11515 r_symndx
= irela
->r_info
>> r_sym_shift
;
11516 if (r_symndx
== STN_UNDEF
)
11519 if (r_symndx
>= locsymcount
11520 || (elf_bad_symtab (input_bfd
)
11521 && flinfo
->sections
[r_symndx
] == NULL
))
11523 struct elf_link_hash_entry
*rh
;
11524 unsigned long indx
;
11526 /* This is a reloc against a global symbol. We
11527 have not yet output all the local symbols, so
11528 we do not know the symbol index of any global
11529 symbol. We set the rel_hash entry for this
11530 reloc to point to the global hash table entry
11531 for this symbol. The symbol index is then
11532 set at the end of bfd_elf_final_link. */
11533 indx
= r_symndx
- extsymoff
;
11534 rh
= elf_sym_hashes (input_bfd
)[indx
];
11535 while (rh
->root
.type
== bfd_link_hash_indirect
11536 || rh
->root
.type
== bfd_link_hash_warning
)
11537 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11539 /* Setting the index to -2 tells
11540 elf_link_output_extsym that this symbol is
11541 used by a reloc. */
11542 BFD_ASSERT (rh
->indx
< 0);
11549 /* This is a reloc against a local symbol. */
11552 sym
= isymbuf
[r_symndx
];
11553 sec
= flinfo
->sections
[r_symndx
];
11554 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11556 /* I suppose the backend ought to fill in the
11557 section of any STT_SECTION symbol against a
11558 processor specific section. */
11559 r_symndx
= STN_UNDEF
;
11560 if (bfd_is_abs_section (sec
))
11562 else if (sec
== NULL
|| sec
->owner
== NULL
)
11564 bfd_set_error (bfd_error_bad_value
);
11569 asection
*osec
= sec
->output_section
;
11571 /* If we have discarded a section, the output
11572 section will be the absolute section. In
11573 case of discarded SEC_MERGE sections, use
11574 the kept section. relocate_section should
11575 have already handled discarded linkonce
11577 if (bfd_is_abs_section (osec
)
11578 && sec
->kept_section
!= NULL
11579 && sec
->kept_section
->output_section
!= NULL
)
11581 osec
= sec
->kept_section
->output_section
;
11582 irela
->r_addend
-= osec
->vma
;
11585 if (!bfd_is_abs_section (osec
))
11587 r_symndx
= osec
->target_index
;
11588 if (r_symndx
== STN_UNDEF
)
11590 irela
->r_addend
+= osec
->vma
;
11591 osec
= _bfd_nearby_section (output_bfd
, osec
,
11593 irela
->r_addend
-= osec
->vma
;
11594 r_symndx
= osec
->target_index
;
11599 /* Adjust the addend according to where the
11600 section winds up in the output section. */
11602 irela
->r_addend
+= sec
->output_offset
;
11606 if (flinfo
->indices
[r_symndx
] == -1)
11608 unsigned long shlink
;
11613 if (flinfo
->info
->strip
== strip_all
)
11615 /* You can't do ld -r -s. */
11616 bfd_set_error (bfd_error_invalid_operation
);
11620 /* This symbol was skipped earlier, but
11621 since it is needed by a reloc, we
11622 must output it now. */
11623 shlink
= symtab_hdr
->sh_link
;
11624 name
= (bfd_elf_string_from_elf_section
11625 (input_bfd
, shlink
, sym
.st_name
));
11629 osec
= sec
->output_section
;
11631 _bfd_elf_section_from_bfd_section (output_bfd
,
11633 if (sym
.st_shndx
== SHN_BAD
)
11636 sym
.st_value
+= sec
->output_offset
;
11637 if (!bfd_link_relocatable (flinfo
->info
))
11639 sym
.st_value
+= osec
->vma
;
11640 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
11642 struct elf_link_hash_table
*htab
11643 = elf_hash_table (flinfo
->info
);
11645 /* STT_TLS symbols are relative to PT_TLS
11647 if (htab
->tls_sec
!= NULL
)
11648 sym
.st_value
-= htab
->tls_sec
->vma
;
11651 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
11656 indx
= bfd_get_symcount (output_bfd
);
11657 ret
= elf_link_output_symstrtab (flinfo
, name
,
11663 flinfo
->indices
[r_symndx
] = indx
;
11668 r_symndx
= flinfo
->indices
[r_symndx
];
11671 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
11672 | (irela
->r_info
& r_type_mask
));
11675 /* Swap out the relocs. */
11676 input_rel_hdr
= esdi
->rel
.hdr
;
11677 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
11679 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11684 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
11685 * bed
->s
->int_rels_per_ext_rel
);
11686 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
11689 input_rela_hdr
= esdi
->rela
.hdr
;
11690 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
11692 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
11701 /* Write out the modified section contents. */
11702 if (bed
->elf_backend_write_section
11703 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
11706 /* Section written out. */
11708 else switch (o
->sec_info_type
)
11710 case SEC_INFO_TYPE_STABS
:
11711 if (! (_bfd_write_section_stabs
11713 &elf_hash_table (flinfo
->info
)->stab_info
,
11714 o
, &elf_section_data (o
)->sec_info
, contents
)))
11717 case SEC_INFO_TYPE_MERGE
:
11718 if (! _bfd_write_merged_section (output_bfd
, o
,
11719 elf_section_data (o
)->sec_info
))
11722 case SEC_INFO_TYPE_EH_FRAME
:
11724 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
11729 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11731 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
11739 if (! (o
->flags
& SEC_EXCLUDE
))
11741 file_ptr offset
= (file_ptr
) o
->output_offset
;
11742 bfd_size_type todo
= o
->size
;
11744 offset
*= bfd_octets_per_byte (output_bfd
, o
);
11746 if ((o
->flags
& SEC_ELF_REVERSE_COPY
)
11747 && o
->size
> address_size
)
11749 /* Reverse-copy input section to output. */
11751 if (o
->reloc_count
!= 0
11752 && (o
->size
* bed
->s
->int_rels_per_ext_rel
11753 != o
->reloc_count
* address_size
))
11756 /* xgettext:c-format */
11757 (_("error: %pB: size of section %pA is not "
11758 "multiple of address size"),
11760 bfd_set_error (bfd_error_bad_value
);
11766 todo
-= address_size
;
11767 if (! bfd_set_section_contents (output_bfd
,
11775 offset
+= address_size
;
11779 else if (! bfd_set_section_contents (output_bfd
,
11793 /* Generate a reloc when linking an ELF file. This is a reloc
11794 requested by the linker, and does not come from any input file. This
11795 is used to build constructor and destructor tables when linking
11799 elf_reloc_link_order (bfd
*output_bfd
,
11800 struct bfd_link_info
*info
,
11801 asection
*output_section
,
11802 struct bfd_link_order
*link_order
)
11804 reloc_howto_type
*howto
;
11808 struct bfd_elf_section_reloc_data
*reldata
;
11809 struct elf_link_hash_entry
**rel_hash_ptr
;
11810 Elf_Internal_Shdr
*rel_hdr
;
11811 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
11812 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
11815 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
11817 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
11820 bfd_set_error (bfd_error_bad_value
);
11824 addend
= link_order
->u
.reloc
.p
->addend
;
11827 reldata
= &esdo
->rel
;
11828 else if (esdo
->rela
.hdr
)
11829 reldata
= &esdo
->rela
;
11836 /* Figure out the symbol index. */
11837 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
11838 if (link_order
->type
== bfd_section_reloc_link_order
)
11840 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
11841 BFD_ASSERT (indx
!= 0);
11842 *rel_hash_ptr
= NULL
;
11846 struct elf_link_hash_entry
*h
;
11848 /* Treat a reloc against a defined symbol as though it were
11849 actually against the section. */
11850 h
= ((struct elf_link_hash_entry
*)
11851 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
11852 link_order
->u
.reloc
.p
->u
.name
,
11853 false, false, true));
11855 && (h
->root
.type
== bfd_link_hash_defined
11856 || h
->root
.type
== bfd_link_hash_defweak
))
11860 section
= h
->root
.u
.def
.section
;
11861 indx
= section
->output_section
->target_index
;
11862 *rel_hash_ptr
= NULL
;
11863 /* It seems that we ought to add the symbol value to the
11864 addend here, but in practice it has already been added
11865 because it was passed to constructor_callback. */
11866 addend
+= section
->output_section
->vma
+ section
->output_offset
;
11868 else if (h
!= NULL
)
11870 /* Setting the index to -2 tells elf_link_output_extsym that
11871 this symbol is used by a reloc. */
11878 (*info
->callbacks
->unattached_reloc
)
11879 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
11884 /* If this is an inplace reloc, we must write the addend into the
11886 if (howto
->partial_inplace
&& addend
!= 0)
11888 bfd_size_type size
;
11889 bfd_reloc_status_type rstat
;
11892 const char *sym_name
;
11893 bfd_size_type octets
;
11895 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
11896 buf
= (bfd_byte
*) bfd_zmalloc (size
);
11897 if (buf
== NULL
&& size
!= 0)
11899 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
11906 case bfd_reloc_outofrange
:
11909 case bfd_reloc_overflow
:
11910 if (link_order
->type
== bfd_section_reloc_link_order
)
11911 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
11913 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
11914 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
11915 howto
->name
, addend
, NULL
, NULL
,
11920 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
11922 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
11929 /* The address of a reloc is relative to the section in a
11930 relocatable file, and is a virtual address in an executable
11932 offset
= link_order
->offset
;
11933 if (! bfd_link_relocatable (info
))
11934 offset
+= output_section
->vma
;
11936 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
11938 irel
[i
].r_offset
= offset
;
11939 irel
[i
].r_info
= 0;
11940 irel
[i
].r_addend
= 0;
11942 if (bed
->s
->arch_size
== 32)
11943 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
11945 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
11947 rel_hdr
= reldata
->hdr
;
11948 erel
= rel_hdr
->contents
;
11949 if (rel_hdr
->sh_type
== SHT_REL
)
11951 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
11952 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
11956 irel
[0].r_addend
= addend
;
11957 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
11958 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
11966 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11967 Returns TRUE upon success, FALSE otherwise. */
11970 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11974 const struct elf_backend_data
*bed
;
11976 enum bfd_architecture arch
;
11978 asymbol
**sympp
= NULL
;
11982 elf_symbol_type
*osymbuf
;
11985 implib_bfd
= info
->out_implib_bfd
;
11986 bed
= get_elf_backend_data (abfd
);
11988 if (!bfd_set_format (implib_bfd
, bfd_object
))
11991 /* Use flag from executable but make it a relocatable object. */
11992 flags
= bfd_get_file_flags (abfd
);
11993 flags
&= ~HAS_RELOC
;
11994 if (!bfd_set_start_address (implib_bfd
, 0)
11995 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
11998 /* Copy architecture of output file to import library file. */
11999 arch
= bfd_get_arch (abfd
);
12000 mach
= bfd_get_mach (abfd
);
12001 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
12002 && (abfd
->target_defaulted
12003 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
12006 /* Get symbol table size. */
12007 symsize
= bfd_get_symtab_upper_bound (abfd
);
12011 /* Read in the symbol table. */
12012 sympp
= (asymbol
**) bfd_malloc (symsize
);
12016 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
12020 /* Allow the BFD backend to copy any private header data it
12021 understands from the output BFD to the import library BFD. */
12022 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
12025 /* Filter symbols to appear in the import library. */
12026 if (bed
->elf_backend_filter_implib_symbols
)
12027 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
12030 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
12033 bfd_set_error (bfd_error_no_symbols
);
12034 _bfd_error_handler (_("%pB: no symbol found for import library"),
12040 /* Make symbols absolute. */
12041 amt
= symcount
* sizeof (*osymbuf
);
12042 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
12043 if (osymbuf
== NULL
)
12046 for (src_count
= 0; src_count
< symcount
; src_count
++)
12048 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
12049 sizeof (*osymbuf
));
12050 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
12051 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
12052 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
12053 osymbuf
[src_count
].internal_elf_sym
.st_value
=
12054 osymbuf
[src_count
].symbol
.value
;
12055 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
12058 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
12060 /* Allow the BFD backend to copy any private data it understands
12061 from the output BFD to the import library BFD. This is done last
12062 to permit the routine to look at the filtered symbol table. */
12063 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
12066 if (!bfd_close (implib_bfd
))
12077 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
12081 if (flinfo
->symstrtab
!= NULL
)
12082 _bfd_elf_strtab_free (flinfo
->symstrtab
);
12083 free (flinfo
->contents
);
12084 free (flinfo
->external_relocs
);
12085 free (flinfo
->internal_relocs
);
12086 free (flinfo
->external_syms
);
12087 free (flinfo
->locsym_shndx
);
12088 free (flinfo
->internal_syms
);
12089 free (flinfo
->indices
);
12090 free (flinfo
->sections
);
12091 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12092 free (flinfo
->symshndxbuf
);
12093 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12095 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12096 free (esdo
->rel
.hashes
);
12097 free (esdo
->rela
.hashes
);
12101 /* Do the final step of an ELF link. */
12104 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12109 struct elf_final_link_info flinfo
;
12111 struct bfd_link_order
*p
;
12113 bfd_size_type max_contents_size
;
12114 bfd_size_type max_external_reloc_size
;
12115 bfd_size_type max_internal_reloc_count
;
12116 bfd_size_type max_sym_count
;
12117 bfd_size_type max_sym_shndx_count
;
12118 Elf_Internal_Sym elfsym
;
12120 Elf_Internal_Shdr
*symtab_hdr
;
12121 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12122 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12123 struct elf_outext_info eoinfo
;
12125 size_t relativecount
= 0;
12126 asection
*reldyn
= 0;
12128 asection
*attr_section
= NULL
;
12129 bfd_vma attr_size
= 0;
12130 const char *std_attrs_section
;
12131 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12132 bool sections_removed
;
12135 if (!is_elf_hash_table (&htab
->root
))
12138 if (bfd_link_pic (info
))
12139 abfd
->flags
|= DYNAMIC
;
12141 dynamic
= htab
->dynamic_sections_created
;
12142 dynobj
= htab
->dynobj
;
12144 emit_relocs
= (bfd_link_relocatable (info
)
12145 || info
->emitrelocations
);
12147 memset (&flinfo
, 0, sizeof (flinfo
));
12148 flinfo
.info
= info
;
12149 flinfo
.output_bfd
= abfd
;
12150 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12151 if (flinfo
.symstrtab
== NULL
)
12156 flinfo
.hash_sec
= NULL
;
12157 flinfo
.symver_sec
= NULL
;
12161 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12162 /* Note that dynsym_sec can be NULL (on VMS). */
12163 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12164 /* Note that it is OK if symver_sec is NULL. */
12167 if (info
->unique_symbol
12168 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12169 local_hash_newfunc
,
12170 sizeof (struct local_hash_entry
)))
12173 /* The object attributes have been merged. Remove the input
12174 sections from the link, and set the contents of the output
12176 sections_removed
= false;
12177 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12178 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12180 bool remove_section
= false;
12182 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12183 || strcmp (o
->name
, ".gnu.attributes") == 0)
12185 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12187 asection
*input_section
;
12189 if (p
->type
!= bfd_indirect_link_order
)
12191 input_section
= p
->u
.indirect
.section
;
12192 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12193 elf_link_input_bfd ignores this section. */
12194 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12197 attr_size
= bfd_elf_obj_attr_size (abfd
);
12198 bfd_set_section_size (o
, attr_size
);
12199 /* Skip this section later on. */
12200 o
->map_head
.link_order
= NULL
;
12204 remove_section
= true;
12206 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12208 /* Remove empty group section from linker output. */
12209 remove_section
= true;
12211 if (remove_section
)
12213 o
->flags
|= SEC_EXCLUDE
;
12214 bfd_section_list_remove (abfd
, o
);
12215 abfd
->section_count
--;
12216 sections_removed
= true;
12219 if (sections_removed
)
12220 _bfd_fix_excluded_sec_syms (abfd
, info
);
12222 /* Count up the number of relocations we will output for each output
12223 section, so that we know the sizes of the reloc sections. We
12224 also figure out some maximum sizes. */
12225 max_contents_size
= 0;
12226 max_external_reloc_size
= 0;
12227 max_internal_reloc_count
= 0;
12229 max_sym_shndx_count
= 0;
12231 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12233 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12234 o
->reloc_count
= 0;
12236 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12238 unsigned int reloc_count
= 0;
12239 unsigned int additional_reloc_count
= 0;
12240 struct bfd_elf_section_data
*esdi
= NULL
;
12242 if (p
->type
== bfd_section_reloc_link_order
12243 || p
->type
== bfd_symbol_reloc_link_order
)
12245 else if (p
->type
== bfd_indirect_link_order
)
12249 sec
= p
->u
.indirect
.section
;
12251 /* Mark all sections which are to be included in the
12252 link. This will normally be every section. We need
12253 to do this so that we can identify any sections which
12254 the linker has decided to not include. */
12255 sec
->linker_mark
= true;
12257 if (sec
->flags
& SEC_MERGE
)
12260 if (sec
->rawsize
> max_contents_size
)
12261 max_contents_size
= sec
->rawsize
;
12262 if (sec
->size
> max_contents_size
)
12263 max_contents_size
= sec
->size
;
12265 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12266 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12270 /* We are interested in just local symbols, not all
12272 if (elf_bad_symtab (sec
->owner
))
12273 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12274 / bed
->s
->sizeof_sym
);
12276 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12278 if (sym_count
> max_sym_count
)
12279 max_sym_count
= sym_count
;
12281 if (sym_count
> max_sym_shndx_count
12282 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12283 max_sym_shndx_count
= sym_count
;
12285 if (esdo
->this_hdr
.sh_type
== SHT_REL
12286 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
12287 /* Some backends use reloc_count in relocation sections
12288 to count particular types of relocs. Of course,
12289 reloc sections themselves can't have relocations. */
12291 else if (emit_relocs
)
12293 reloc_count
= sec
->reloc_count
;
12294 if (bed
->elf_backend_count_additional_relocs
)
12297 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12298 additional_reloc_count
+= c
;
12301 else if (bed
->elf_backend_count_relocs
)
12302 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12304 esdi
= elf_section_data (sec
);
12306 if ((sec
->flags
& SEC_RELOC
) != 0)
12308 size_t ext_size
= 0;
12310 if (esdi
->rel
.hdr
!= NULL
)
12311 ext_size
= esdi
->rel
.hdr
->sh_size
;
12312 if (esdi
->rela
.hdr
!= NULL
)
12313 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12315 if (ext_size
> max_external_reloc_size
)
12316 max_external_reloc_size
= ext_size
;
12317 if (sec
->reloc_count
> max_internal_reloc_count
)
12318 max_internal_reloc_count
= sec
->reloc_count
;
12323 if (reloc_count
== 0)
12326 reloc_count
+= additional_reloc_count
;
12327 o
->reloc_count
+= reloc_count
;
12329 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12333 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12334 esdo
->rel
.count
+= additional_reloc_count
;
12336 if (esdi
->rela
.hdr
)
12338 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12339 esdo
->rela
.count
+= additional_reloc_count
;
12345 esdo
->rela
.count
+= reloc_count
;
12347 esdo
->rel
.count
+= reloc_count
;
12351 if (o
->reloc_count
> 0)
12352 o
->flags
|= SEC_RELOC
;
12355 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12356 set it (this is probably a bug) and if it is set
12357 assign_section_numbers will create a reloc section. */
12358 o
->flags
&=~ SEC_RELOC
;
12361 /* If the SEC_ALLOC flag is not set, force the section VMA to
12362 zero. This is done in elf_fake_sections as well, but forcing
12363 the VMA to 0 here will ensure that relocs against these
12364 sections are handled correctly. */
12365 if ((o
->flags
& SEC_ALLOC
) == 0
12366 && ! o
->user_set_vma
)
12370 if (! bfd_link_relocatable (info
) && merged
)
12371 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12373 /* Figure out the file positions for everything but the symbol table
12374 and the relocs. We set symcount to force assign_section_numbers
12375 to create a symbol table. */
12376 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12377 BFD_ASSERT (! abfd
->output_has_begun
);
12378 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12381 /* Set sizes, and assign file positions for reloc sections. */
12382 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12384 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12385 if ((o
->flags
& SEC_RELOC
) != 0)
12388 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12392 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12396 /* _bfd_elf_compute_section_file_positions makes temporary use
12397 of target_index. Reset it. */
12398 o
->target_index
= 0;
12400 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12401 to count upwards while actually outputting the relocations. */
12402 esdo
->rel
.count
= 0;
12403 esdo
->rela
.count
= 0;
12405 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12406 && !bfd_section_is_ctf (o
))
12408 /* Cache the section contents so that they can be compressed
12409 later. Use bfd_malloc since it will be freed by
12410 bfd_compress_section_contents. */
12411 unsigned char *contents
= esdo
->this_hdr
.contents
;
12412 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
12415 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12416 if (contents
== NULL
)
12418 esdo
->this_hdr
.contents
= contents
;
12422 /* We have now assigned file positions for all the sections except .symtab,
12423 .strtab, and non-loaded reloc and compressed debugging sections. We start
12424 the .symtab section at the current file position, and write directly to it.
12425 We build the .strtab section in memory. */
12426 abfd
->symcount
= 0;
12427 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12428 /* sh_name is set in prep_headers. */
12429 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12430 /* sh_flags, sh_addr and sh_size all start off zero. */
12431 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12432 /* sh_link is set in assign_section_numbers. */
12433 /* sh_info is set below. */
12434 /* sh_offset is set just below. */
12435 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12437 if (max_sym_count
< 20)
12438 max_sym_count
= 20;
12439 htab
->strtabsize
= max_sym_count
;
12440 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12441 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12442 if (htab
->strtab
== NULL
)
12444 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12446 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12447 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12449 if (info
->strip
!= strip_all
|| emit_relocs
)
12451 file_ptr off
= elf_next_file_pos (abfd
);
12453 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
12455 /* Note that at this point elf_next_file_pos (abfd) is
12456 incorrect. We do not yet know the size of the .symtab section.
12457 We correct next_file_pos below, after we do know the size. */
12459 /* Start writing out the symbol table. The first symbol is always a
12461 elfsym
.st_value
= 0;
12462 elfsym
.st_size
= 0;
12463 elfsym
.st_info
= 0;
12464 elfsym
.st_other
= 0;
12465 elfsym
.st_shndx
= SHN_UNDEF
;
12466 elfsym
.st_target_internal
= 0;
12467 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12468 bfd_und_section_ptr
, NULL
) != 1)
12471 /* Output a symbol for each section if asked or they are used for
12472 relocs. These symbols usually have no names. We store the
12473 index of each one in the index field of the section, so that
12474 we can find it again when outputting relocs. */
12476 if (bfd_keep_unused_section_symbols (abfd
) || emit_relocs
)
12478 bool name_local_sections
12479 = (bed
->elf_backend_name_local_section_symbols
12480 && bed
->elf_backend_name_local_section_symbols (abfd
));
12481 const char *name
= NULL
;
12483 elfsym
.st_size
= 0;
12484 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12485 elfsym
.st_other
= 0;
12486 elfsym
.st_value
= 0;
12487 elfsym
.st_target_internal
= 0;
12488 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12490 o
= bfd_section_from_elf_index (abfd
, i
);
12493 o
->target_index
= bfd_get_symcount (abfd
);
12494 elfsym
.st_shndx
= i
;
12495 if (!bfd_link_relocatable (info
))
12496 elfsym
.st_value
= o
->vma
;
12497 if (name_local_sections
)
12499 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12507 /* On some targets like Irix 5 the symbol split between local and global
12508 ones recorded in the sh_info field needs to be done between section
12509 and all other symbols. */
12510 if (bed
->elf_backend_elfsym_local_is_section
12511 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12512 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12514 /* Allocate some memory to hold information read in from the input
12516 if (max_contents_size
!= 0)
12518 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12519 if (flinfo
.contents
== NULL
)
12523 if (max_external_reloc_size
!= 0)
12525 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12526 if (flinfo
.external_relocs
== NULL
)
12530 if (max_internal_reloc_count
!= 0)
12532 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12533 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12534 if (flinfo
.internal_relocs
== NULL
)
12538 if (max_sym_count
!= 0)
12540 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12541 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12542 if (flinfo
.external_syms
== NULL
)
12545 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12546 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12547 if (flinfo
.internal_syms
== NULL
)
12550 amt
= max_sym_count
* sizeof (long);
12551 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12552 if (flinfo
.indices
== NULL
)
12555 amt
= max_sym_count
* sizeof (asection
*);
12556 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12557 if (flinfo
.sections
== NULL
)
12561 if (max_sym_shndx_count
!= 0)
12563 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
12564 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
12565 if (flinfo
.locsym_shndx
== NULL
)
12571 bfd_vma base
, end
= 0; /* Both bytes. */
12574 for (sec
= htab
->tls_sec
;
12575 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
12578 bfd_size_type size
= sec
->size
;
12579 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
12582 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
12584 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
12587 size
= ord
->offset
* opb
+ ord
->size
;
12589 end
= sec
->vma
+ size
/ opb
;
12591 base
= htab
->tls_sec
->vma
;
12592 /* Only align end of TLS section if static TLS doesn't have special
12593 alignment requirements. */
12594 if (bed
->static_tls_alignment
== 1)
12595 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
12596 htab
->tls_size
= end
- base
;
12599 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
12602 /* Since ELF permits relocations to be against local symbols, we
12603 must have the local symbols available when we do the relocations.
12604 Since we would rather only read the local symbols once, and we
12605 would rather not keep them in memory, we handle all the
12606 relocations for a single input file at the same time.
12608 Unfortunately, there is no way to know the total number of local
12609 symbols until we have seen all of them, and the local symbol
12610 indices precede the global symbol indices. This means that when
12611 we are generating relocatable output, and we see a reloc against
12612 a global symbol, we can not know the symbol index until we have
12613 finished examining all the local symbols to see which ones we are
12614 going to output. To deal with this, we keep the relocations in
12615 memory, and don't output them until the end of the link. This is
12616 an unfortunate waste of memory, but I don't see a good way around
12617 it. Fortunately, it only happens when performing a relocatable
12618 link, which is not the common case. FIXME: If keep_memory is set
12619 we could write the relocs out and then read them again; I don't
12620 know how bad the memory loss will be. */
12622 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12623 sub
->output_has_begun
= false;
12624 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12626 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12628 if (p
->type
== bfd_indirect_link_order
12629 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
12630 == bfd_target_elf_flavour
)
12631 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
12633 if (! sub
->output_has_begun
)
12635 if (! elf_link_input_bfd (&flinfo
, sub
))
12637 sub
->output_has_begun
= true;
12640 else if (p
->type
== bfd_section_reloc_link_order
12641 || p
->type
== bfd_symbol_reloc_link_order
)
12643 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
12648 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
12650 if (p
->type
== bfd_indirect_link_order
12651 && (bfd_get_flavour (sub
)
12652 == bfd_target_elf_flavour
)
12653 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
12654 != bed
->s
->elfclass
))
12656 const char *iclass
, *oclass
;
12658 switch (bed
->s
->elfclass
)
12660 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
12661 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
12662 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
12666 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
12668 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
12669 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
12670 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
12674 bfd_set_error (bfd_error_wrong_format
);
12676 /* xgettext:c-format */
12677 (_("%pB: file class %s incompatible with %s"),
12678 sub
, iclass
, oclass
);
12687 /* Free symbol buffer if needed. */
12688 if (!info
->reduce_memory_overheads
)
12690 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12691 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
12693 free (elf_tdata (sub
)->symbuf
);
12694 elf_tdata (sub
)->symbuf
= NULL
;
12700 /* Output any global symbols that got converted to local in a
12701 version script or due to symbol visibility. We do this in a
12702 separate step since ELF requires all local symbols to appear
12703 prior to any global symbols. FIXME: We should only do this if
12704 some global symbols were, in fact, converted to become local.
12705 FIXME: Will this work correctly with the Irix 5 linker? */
12706 eoinfo
.failed
= false;
12707 eoinfo
.flinfo
= &flinfo
;
12708 eoinfo
.localsyms
= true;
12709 eoinfo
.file_sym_done
= false;
12710 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12714 goto return_local_hash_table
;
12717 /* If backend needs to output some local symbols not present in the hash
12718 table, do it now. */
12719 if (bed
->elf_backend_output_arch_local_syms
12720 && (info
->strip
!= strip_all
|| emit_relocs
))
12722 if (! ((*bed
->elf_backend_output_arch_local_syms
)
12723 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
12726 goto return_local_hash_table
;
12730 /* That wrote out all the local symbols. Finish up the symbol table
12731 with the global symbols. Even if we want to strip everything we
12732 can, we still need to deal with those global symbols that got
12733 converted to local in a version script. */
12735 /* The sh_info field records the index of the first non local symbol. */
12736 if (!symtab_hdr
->sh_info
)
12737 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12740 && htab
->dynsym
!= NULL
12741 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
12743 Elf_Internal_Sym sym
;
12744 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
12746 o
= htab
->dynsym
->output_section
;
12747 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
12749 /* Write out the section symbols for the output sections. */
12750 if (bfd_link_pic (info
)
12751 || htab
->is_relocatable_executable
)
12757 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12759 sym
.st_target_internal
= 0;
12761 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
12767 dynindx
= elf_section_data (s
)->dynindx
;
12770 indx
= elf_section_data (s
)->this_idx
;
12771 BFD_ASSERT (indx
> 0);
12772 sym
.st_shndx
= indx
;
12773 if (! check_dynsym (abfd
, &sym
))
12776 goto return_local_hash_table
;
12778 sym
.st_value
= s
->vma
;
12779 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
12781 /* Inform the linker of the addition of this symbol. */
12783 if (info
->callbacks
->ctf_new_dynsym
)
12784 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
12786 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12790 /* Write out the local dynsyms. */
12791 if (htab
->dynlocal
)
12793 struct elf_link_local_dynamic_entry
*e
;
12794 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
12799 /* Copy the internal symbol and turn off visibility.
12800 Note that we saved a word of storage and overwrote
12801 the original st_name with the dynstr_index. */
12803 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
12804 sym
.st_shndx
= SHN_UNDEF
;
12806 s
= bfd_section_from_elf_index (e
->input_bfd
,
12809 && s
->output_section
!= NULL
12810 && elf_section_data (s
->output_section
) != NULL
)
12813 elf_section_data (s
->output_section
)->this_idx
;
12814 if (! check_dynsym (abfd
, &sym
))
12817 goto return_local_hash_table
;
12819 sym
.st_value
= (s
->output_section
->vma
12821 + e
->isym
.st_value
);
12824 /* Inform the linker of the addition of this symbol. */
12826 if (info
->callbacks
->ctf_new_dynsym
)
12827 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
12829 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
12830 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
12835 /* We get the global symbols from the hash table. */
12836 eoinfo
.failed
= false;
12837 eoinfo
.localsyms
= false;
12838 eoinfo
.flinfo
= &flinfo
;
12839 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
12843 goto return_local_hash_table
;
12846 /* If backend needs to output some symbols not present in the hash
12847 table, do it now. */
12848 if (bed
->elf_backend_output_arch_syms
12849 && (info
->strip
!= strip_all
|| emit_relocs
))
12851 if (! ((*bed
->elf_backend_output_arch_syms
)
12852 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
12855 goto return_local_hash_table
;
12859 /* Finalize the .strtab section. */
12860 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
12862 /* Swap out the .strtab section. */
12863 if (!elf_link_swap_symbols_out (&flinfo
))
12866 goto return_local_hash_table
;
12869 /* Now we know the size of the symtab section. */
12870 if (bfd_get_symcount (abfd
) > 0)
12872 /* Finish up and write out the symbol string table (.strtab)
12874 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
12875 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
12877 if (elf_symtab_shndx_list (abfd
))
12879 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
12881 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
12883 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
12884 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
12885 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
12886 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
12887 symtab_shndx_hdr
->sh_size
= amt
;
12889 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
12892 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
12893 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
12896 goto return_local_hash_table
;
12901 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
12902 /* sh_name was set in prep_headers. */
12903 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
12904 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
12905 symstrtab_hdr
->sh_addr
= 0;
12906 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
12907 symstrtab_hdr
->sh_entsize
= 0;
12908 symstrtab_hdr
->sh_link
= 0;
12909 symstrtab_hdr
->sh_info
= 0;
12910 /* sh_offset is set just below. */
12911 symstrtab_hdr
->sh_addralign
= 1;
12913 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
12915 elf_next_file_pos (abfd
) = off
;
12917 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
12918 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
12921 goto return_local_hash_table
;
12925 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
12927 _bfd_error_handler (_("%pB: failed to generate import library"),
12928 info
->out_implib_bfd
);
12930 goto return_local_hash_table
;
12933 /* Adjust the relocs to have the correct symbol indices. */
12934 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12936 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12939 if ((o
->flags
& SEC_RELOC
) == 0)
12942 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
12943 if (esdo
->rel
.hdr
!= NULL
12944 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
12947 goto return_local_hash_table
;
12949 if (esdo
->rela
.hdr
!= NULL
12950 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
12953 goto return_local_hash_table
;
12956 /* Set the reloc_count field to 0 to prevent write_relocs from
12957 trying to swap the relocs out itself. */
12958 o
->reloc_count
= 0;
12961 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
12962 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
12964 /* If we are linking against a dynamic object, or generating a
12965 shared library, finish up the dynamic linking information. */
12968 bfd_byte
*dyncon
, *dynconend
;
12970 /* Fix up .dynamic entries. */
12971 o
= bfd_get_linker_section (dynobj
, ".dynamic");
12972 BFD_ASSERT (o
!= NULL
);
12974 dyncon
= o
->contents
;
12975 dynconend
= PTR_ADD (o
->contents
, o
->size
);
12976 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12978 Elf_Internal_Dyn dyn
;
12981 bfd_size_type sh_size
;
12984 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12991 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
12993 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
12995 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
12996 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
12999 dyn
.d_un
.d_val
= relativecount
;
13006 name
= info
->init_function
;
13009 name
= info
->fini_function
;
13012 struct elf_link_hash_entry
*h
;
13014 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
13016 && (h
->root
.type
== bfd_link_hash_defined
13017 || h
->root
.type
== bfd_link_hash_defweak
))
13019 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
13020 o
= h
->root
.u
.def
.section
;
13021 if (o
->output_section
!= NULL
)
13022 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
13023 + o
->output_offset
);
13026 /* The symbol is imported from another shared
13027 library and does not apply to this one. */
13028 dyn
.d_un
.d_ptr
= 0;
13035 case DT_PREINIT_ARRAYSZ
:
13036 name
= ".preinit_array";
13038 case DT_INIT_ARRAYSZ
:
13039 name
= ".init_array";
13041 case DT_FINI_ARRAYSZ
:
13042 name
= ".fini_array";
13044 o
= bfd_get_section_by_name (abfd
, name
);
13048 (_("could not find section %s"), name
);
13053 (_("warning: %s section has zero size"), name
);
13054 dyn
.d_un
.d_val
= o
->size
;
13057 case DT_PREINIT_ARRAY
:
13058 name
= ".preinit_array";
13060 case DT_INIT_ARRAY
:
13061 name
= ".init_array";
13063 case DT_FINI_ARRAY
:
13064 name
= ".fini_array";
13066 o
= bfd_get_section_by_name (abfd
, name
);
13073 name
= ".gnu.hash";
13082 name
= ".gnu.version_d";
13085 name
= ".gnu.version_r";
13088 name
= ".gnu.version";
13090 o
= bfd_get_linker_section (dynobj
, name
);
13092 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13095 (_("could not find section %s"), name
);
13098 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13101 (_("warning: section '%s' is being made into a note"), name
);
13102 bfd_set_error (bfd_error_nonrepresentable_section
);
13105 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13112 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13118 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13120 Elf_Internal_Shdr
*hdr
;
13122 hdr
= elf_elfsections (abfd
)[i
];
13123 if (hdr
->sh_type
== type
13124 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13126 sh_size
+= hdr
->sh_size
;
13128 || sh_addr
> hdr
->sh_addr
)
13129 sh_addr
= hdr
->sh_addr
;
13133 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13135 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13137 /* Don't count procedure linkage table relocs in the
13138 overall reloc count. */
13139 sh_size
-= htab
->srelplt
->size
;
13141 /* If the size is zero, make the address zero too.
13142 This is to avoid a glibc bug. If the backend
13143 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13144 zero, then we'll put DT_RELA at the end of
13145 DT_JMPREL. glibc will interpret the end of
13146 DT_RELA matching the end of DT_JMPREL as the
13147 case where DT_RELA includes DT_JMPREL, and for
13148 LD_BIND_NOW will decide that processing DT_RELA
13149 will process the PLT relocs too. Net result:
13150 No PLT relocs applied. */
13153 /* If .rela.plt is the first .rela section, exclude
13154 it from DT_RELA. */
13155 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13156 + htab
->srelplt
->output_offset
) * opb
)
13157 sh_addr
+= htab
->srelplt
->size
;
13160 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13161 dyn
.d_un
.d_val
= sh_size
;
13163 dyn
.d_un
.d_ptr
= sh_addr
;
13166 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13170 /* If we have created any dynamic sections, then output them. */
13171 if (dynobj
!= NULL
)
13173 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13176 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13177 if (bfd_link_textrel_check (info
)
13178 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
13181 bfd_byte
*dyncon
, *dynconend
;
13183 dyncon
= o
->contents
;
13184 dynconend
= o
->contents
+ o
->size
;
13185 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13187 Elf_Internal_Dyn dyn
;
13189 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13191 if (dyn
.d_tag
== DT_TEXTREL
)
13193 if (info
->textrel_check
== textrel_check_error
)
13194 info
->callbacks
->einfo
13195 (_("%P%X: read-only segment has dynamic relocations\n"));
13196 else if (bfd_link_dll (info
))
13197 info
->callbacks
->einfo
13198 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13199 else if (bfd_link_pde (info
))
13200 info
->callbacks
->einfo
13201 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13203 info
->callbacks
->einfo
13204 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13210 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13212 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13214 || o
->output_section
== bfd_abs_section_ptr
)
13216 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13218 /* At this point, we are only interested in sections
13219 created by _bfd_elf_link_create_dynamic_sections. */
13222 if (htab
->stab_info
.stabstr
== o
)
13224 if (htab
->eh_info
.hdr_sec
== o
)
13226 if (strcmp (o
->name
, ".dynstr") != 0)
13228 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13229 * bfd_octets_per_byte (abfd
, o
));
13230 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13231 o
->contents
, octets
, o
->size
))
13236 /* The contents of the .dynstr section are actually in a
13240 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13241 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13242 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13248 if (!info
->resolve_section_groups
)
13250 bool failed
= false;
13252 BFD_ASSERT (bfd_link_relocatable (info
));
13253 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13258 /* If we have optimized stabs strings, output them. */
13259 if (htab
->stab_info
.stabstr
!= NULL
)
13261 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13265 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13268 if (info
->callbacks
->emit_ctf
)
13269 info
->callbacks
->emit_ctf ();
13271 elf_final_link_free (abfd
, &flinfo
);
13275 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13276 if (contents
== NULL
)
13278 /* Bail out and fail. */
13280 goto return_local_hash_table
;
13282 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13283 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13287 return_local_hash_table
:
13288 if (info
->unique_symbol
)
13289 bfd_hash_table_free (&flinfo
.local_hash_table
);
13293 elf_final_link_free (abfd
, &flinfo
);
13295 goto return_local_hash_table
;
13298 /* Initialize COOKIE for input bfd ABFD. */
13301 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13302 struct bfd_link_info
*info
, bfd
*abfd
)
13304 Elf_Internal_Shdr
*symtab_hdr
;
13305 const struct elf_backend_data
*bed
;
13307 bed
= get_elf_backend_data (abfd
);
13308 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13310 cookie
->abfd
= abfd
;
13311 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13312 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13313 if (cookie
->bad_symtab
)
13315 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13316 cookie
->extsymoff
= 0;
13320 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13321 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13324 if (bed
->s
->arch_size
== 32)
13325 cookie
->r_sym_shift
= 8;
13327 cookie
->r_sym_shift
= 32;
13329 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13330 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13332 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13333 cookie
->locsymcount
, 0,
13335 if (cookie
->locsyms
== NULL
)
13337 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13340 if (_bfd_link_keep_memory (info
) )
13342 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13343 info
->cache_size
+= (cookie
->locsymcount
13344 * sizeof (Elf_External_Sym_Shndx
));
13350 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13353 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13355 Elf_Internal_Shdr
*symtab_hdr
;
13357 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13358 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13359 free (cookie
->locsyms
);
13362 /* Initialize the relocation information in COOKIE for input section SEC
13363 of input bfd ABFD. */
13366 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13367 struct bfd_link_info
*info
, bfd
*abfd
,
13370 if (sec
->reloc_count
== 0)
13372 cookie
->rels
= NULL
;
13373 cookie
->relend
= NULL
;
13377 cookie
->rels
= _bfd_elf_link_info_read_relocs (abfd
, info
, sec
,
13379 _bfd_link_keep_memory (info
));
13380 if (cookie
->rels
== NULL
)
13382 cookie
->rel
= cookie
->rels
;
13383 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13385 cookie
->rel
= cookie
->rels
;
13389 /* Free the memory allocated by init_reloc_cookie_rels,
13393 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13396 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13397 free (cookie
->rels
);
13400 /* Initialize the whole of COOKIE for input section SEC. */
13403 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13404 struct bfd_link_info
*info
,
13407 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
13409 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
13414 fini_reloc_cookie (cookie
, sec
->owner
);
13419 /* Free the memory allocated by init_reloc_cookie_for_section,
13423 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13426 fini_reloc_cookie_rels (cookie
, sec
);
13427 fini_reloc_cookie (cookie
, sec
->owner
);
13430 /* Garbage collect unused sections. */
13432 /* Default gc_mark_hook. */
13435 _bfd_elf_gc_mark_hook (asection
*sec
,
13436 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13437 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13438 struct elf_link_hash_entry
*h
,
13439 Elf_Internal_Sym
*sym
)
13443 switch (h
->root
.type
)
13445 case bfd_link_hash_defined
:
13446 case bfd_link_hash_defweak
:
13447 return h
->root
.u
.def
.section
;
13449 case bfd_link_hash_common
:
13450 return h
->root
.u
.c
.p
->section
;
13457 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13462 /* Return the debug definition section. */
13465 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13466 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13467 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13468 struct elf_link_hash_entry
*h
,
13469 Elf_Internal_Sym
*sym
)
13473 /* Return the global debug definition section. */
13474 if ((h
->root
.type
== bfd_link_hash_defined
13475 || h
->root
.type
== bfd_link_hash_defweak
)
13476 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13477 return h
->root
.u
.def
.section
;
13481 /* Return the local debug definition section. */
13482 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13484 if ((isec
->flags
& SEC_DEBUGGING
) != 0)
13491 /* COOKIE->rel describes a relocation against section SEC, which is
13492 a section we've decided to keep. Return the section that contains
13493 the relocation symbol, or NULL if no section contains it. */
13496 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13497 elf_gc_mark_hook_fn gc_mark_hook
,
13498 struct elf_reloc_cookie
*cookie
,
13501 unsigned long r_symndx
;
13502 struct elf_link_hash_entry
*h
, *hw
;
13504 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13505 if (r_symndx
== STN_UNDEF
)
13508 if (r_symndx
>= cookie
->locsymcount
13509 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13513 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
13516 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
13520 while (h
->root
.type
== bfd_link_hash_indirect
13521 || h
->root
.type
== bfd_link_hash_warning
)
13522 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13524 was_marked
= h
->mark
;
13526 /* Keep all aliases of the symbol too. If an object symbol
13527 needs to be copied into .dynbss then all of its aliases
13528 should be present as dynamic symbols, not just the one used
13529 on the copy relocation. */
13531 while (hw
->is_weakalias
)
13537 if (!was_marked
&& h
->start_stop
&& !h
->root
.ldscript_def
)
13539 if (info
->start_stop_gc
)
13542 /* To work around a glibc bug, mark XXX input sections
13543 when there is a reference to __start_XXX or __stop_XXX
13545 else if (start_stop
!= NULL
)
13547 asection
*s
= h
->u2
.start_stop_section
;
13548 *start_stop
= true;
13553 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
13556 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
13557 &cookie
->locsyms
[r_symndx
]);
13560 /* COOKIE->rel describes a relocation against section SEC, which is
13561 a section we've decided to keep. Mark the section that contains
13562 the relocation symbol. */
13565 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
13567 elf_gc_mark_hook_fn gc_mark_hook
,
13568 struct elf_reloc_cookie
*cookie
)
13571 bool start_stop
= false;
13573 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
13574 while (rsec
!= NULL
)
13576 if (!rsec
->gc_mark
)
13578 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
13579 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
13581 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
13586 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
13591 /* The mark phase of garbage collection. For a given section, mark
13592 it and any sections in this section's group, and all the sections
13593 which define symbols to which it refers. */
13596 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
13598 elf_gc_mark_hook_fn gc_mark_hook
)
13601 asection
*group_sec
, *eh_frame
;
13605 /* Mark all the sections in the group. */
13606 group_sec
= elf_section_data (sec
)->next_in_group
;
13607 if (group_sec
&& !group_sec
->gc_mark
)
13608 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
13611 /* Look through the section relocs. */
13613 eh_frame
= elf_eh_frame_section (sec
->owner
);
13614 if ((sec
->flags
& SEC_RELOC
) != 0
13615 && sec
->reloc_count
> 0
13616 && sec
!= eh_frame
)
13618 struct elf_reloc_cookie cookie
;
13620 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
13624 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
13625 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
13630 fini_reloc_cookie_for_section (&cookie
, sec
);
13634 if (ret
&& eh_frame
&& elf_fde_list (sec
))
13636 struct elf_reloc_cookie cookie
;
13638 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
13642 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
13643 gc_mark_hook
, &cookie
))
13645 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
13649 eh_frame
= elf_section_eh_frame_entry (sec
);
13650 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
13651 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
13657 /* Scan and mark sections in a special or debug section group. */
13660 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
13662 /* Point to first section of section group. */
13664 /* Used to iterate the section group. */
13667 bool is_special_grp
= true;
13668 bool is_debug_grp
= true;
13670 /* First scan to see if group contains any section other than debug
13671 and special section. */
13672 ssec
= msec
= elf_next_in_group (grp
);
13675 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
13676 is_debug_grp
= false;
13678 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
13679 is_special_grp
= false;
13681 msec
= elf_next_in_group (msec
);
13683 while (msec
!= ssec
);
13685 /* If this is a pure debug section group or pure special section group,
13686 keep all sections in this group. */
13687 if (is_debug_grp
|| is_special_grp
)
13692 msec
= elf_next_in_group (msec
);
13694 while (msec
!= ssec
);
13698 /* Keep debug and special sections. */
13701 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
13702 elf_gc_mark_hook_fn mark_hook
)
13706 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13710 bool debug_frag_seen
;
13711 bool has_kept_debug_info
;
13713 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13715 isec
= ibfd
->sections
;
13716 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13719 /* Ensure all linker created sections are kept,
13720 see if any other section is already marked,
13721 and note if we have any fragmented debug sections. */
13722 debug_frag_seen
= some_kept
= has_kept_debug_info
= false;
13723 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13725 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
13727 else if (isec
->gc_mark
13728 && (isec
->flags
& SEC_ALLOC
) != 0
13729 && elf_section_type (isec
) != SHT_NOTE
)
13733 /* Since all sections, except for backend specific ones,
13734 have been garbage collected, call mark_hook on this
13735 section if any of its linked-to sections is marked. */
13736 asection
*linked_to_sec
;
13737 for (linked_to_sec
= elf_linked_to_section (isec
);
13738 linked_to_sec
!= NULL
&& !linked_to_sec
->linker_mark
;
13739 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13741 if (linked_to_sec
->gc_mark
)
13743 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
13747 linked_to_sec
->linker_mark
= 1;
13749 for (linked_to_sec
= elf_linked_to_section (isec
);
13750 linked_to_sec
!= NULL
&& linked_to_sec
->linker_mark
;
13751 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
13752 linked_to_sec
->linker_mark
= 0;
13755 if (!debug_frag_seen
13756 && (isec
->flags
& SEC_DEBUGGING
)
13757 && startswith (isec
->name
, ".debug_line."))
13758 debug_frag_seen
= true;
13759 else if (strcmp (bfd_section_name (isec
),
13760 "__patchable_function_entries") == 0
13761 && elf_linked_to_section (isec
) == NULL
)
13762 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
13763 "need linked-to section "
13764 "for --gc-sections\n"),
13765 isec
->owner
, isec
);
13768 /* If no non-note alloc section in this file will be kept, then
13769 we can toss out the debug and special sections. */
13773 /* Keep debug and special sections like .comment when they are
13774 not part of a group. Also keep section groups that contain
13775 just debug sections or special sections. NB: Sections with
13776 linked-to section has been handled above. */
13777 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13779 if ((isec
->flags
& SEC_GROUP
) != 0)
13780 _bfd_elf_gc_mark_debug_special_section_group (isec
);
13781 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
13782 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
13783 && elf_next_in_group (isec
) == NULL
13784 && elf_linked_to_section (isec
) == NULL
)
13786 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13787 has_kept_debug_info
= true;
13790 /* Look for CODE sections which are going to be discarded,
13791 and find and discard any fragmented debug sections which
13792 are associated with that code section. */
13793 if (debug_frag_seen
)
13794 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13795 if ((isec
->flags
& SEC_CODE
) != 0
13796 && isec
->gc_mark
== 0)
13801 ilen
= strlen (isec
->name
);
13803 /* Association is determined by the name of the debug
13804 section containing the name of the code section as
13805 a suffix. For example .debug_line.text.foo is a
13806 debug section associated with .text.foo. */
13807 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
13811 if (dsec
->gc_mark
== 0
13812 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
13815 dlen
= strlen (dsec
->name
);
13818 && strncmp (dsec
->name
+ (dlen
- ilen
),
13819 isec
->name
, ilen
) == 0)
13824 /* Mark debug sections referenced by kept debug sections. */
13825 if (has_kept_debug_info
)
13826 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
13828 && (isec
->flags
& SEC_DEBUGGING
) != 0)
13829 if (!_bfd_elf_gc_mark (info
, isec
,
13830 elf_gc_mark_debug_section
))
13837 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
13840 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13842 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13846 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13847 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
13848 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13851 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
13854 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13856 /* When any section in a section group is kept, we keep all
13857 sections in the section group. If the first member of
13858 the section group is excluded, we will also exclude the
13860 if (o
->flags
& SEC_GROUP
)
13862 asection
*first
= elf_next_in_group (o
);
13863 o
->gc_mark
= first
->gc_mark
;
13869 /* Skip sweeping sections already excluded. */
13870 if (o
->flags
& SEC_EXCLUDE
)
13873 /* Since this is early in the link process, it is simple
13874 to remove a section from the output. */
13875 o
->flags
|= SEC_EXCLUDE
;
13877 if (info
->print_gc_sections
&& o
->size
!= 0)
13878 /* xgettext:c-format */
13879 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13887 /* Propagate collected vtable information. This is called through
13888 elf_link_hash_traverse. */
13891 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
13893 /* Those that are not vtables. */
13895 || h
->u2
.vtable
== NULL
13896 || h
->u2
.vtable
->parent
== NULL
)
13899 /* Those vtables that do not have parents, we cannot merge. */
13900 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
13903 /* If we've already been done, exit. */
13904 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
13907 /* Make sure the parent's table is up to date. */
13908 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
13910 if (h
->u2
.vtable
->used
== NULL
)
13912 /* None of this table's entries were referenced. Re-use the
13914 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13915 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
13922 /* Or the parent's entries into ours. */
13923 cu
= h
->u2
.vtable
->used
;
13925 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
13928 const struct elf_backend_data
*bed
;
13929 unsigned int log_file_align
;
13931 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
13932 log_file_align
= bed
->s
->log_file_align
;
13933 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
13947 struct link_info_ok
13949 struct bfd_link_info
*info
;
13954 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
,
13958 bfd_vma hstart
, hend
;
13959 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
13960 const struct elf_backend_data
*bed
;
13961 unsigned int log_file_align
;
13962 struct link_info_ok
*info
= (struct link_info_ok
*) ptr
;
13964 /* Take care of both those symbols that do not describe vtables as
13965 well as those that are not loaded. */
13967 || h
->u2
.vtable
== NULL
13968 || h
->u2
.vtable
->parent
== NULL
)
13971 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
13972 || h
->root
.type
== bfd_link_hash_defweak
);
13974 sec
= h
->root
.u
.def
.section
;
13975 hstart
= h
->root
.u
.def
.value
;
13976 hend
= hstart
+ h
->size
;
13978 relstart
= _bfd_elf_link_info_read_relocs (sec
->owner
, info
->info
,
13979 sec
, NULL
, NULL
, true);
13981 return info
->ok
= false;
13982 bed
= get_elf_backend_data (sec
->owner
);
13983 log_file_align
= bed
->s
->log_file_align
;
13985 relend
= relstart
+ sec
->reloc_count
;
13987 for (rel
= relstart
; rel
< relend
; ++rel
)
13988 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
13990 /* If the entry is in use, do nothing. */
13991 if (h
->u2
.vtable
->used
13992 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
13994 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
13995 if (h
->u2
.vtable
->used
[entry
])
13998 /* Otherwise, kill it. */
13999 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
14005 /* Mark sections containing dynamically referenced symbols. When
14006 building shared libraries, we must assume that any visible symbol is
14010 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
14012 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
14013 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
14015 if ((h
->root
.type
== bfd_link_hash_defined
14016 || h
->root
.type
== bfd_link_hash_defweak
)
14018 || h
->root
.ldscript_def
14019 || !info
->start_stop_gc
)
14020 && ((h
->ref_dynamic
&& !h
->forced_local
)
14021 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
14022 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
14023 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
14024 && (!bfd_link_executable (info
)
14025 || info
->gc_keep_exported
14026 || info
->export_dynamic
14029 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
14030 && (h
->versioned
>= versioned
14031 || !bfd_hide_sym_by_version (info
->version_info
,
14032 h
->root
.root
.string
)))))
14033 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14038 /* Keep all sections containing symbols undefined on the command-line,
14039 and the section containing the entry symbol. */
14042 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
14044 struct bfd_sym_chain
*sym
;
14046 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
14048 struct elf_link_hash_entry
*h
;
14050 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
14051 false, false, false);
14054 && (h
->root
.type
== bfd_link_hash_defined
14055 || h
->root
.type
== bfd_link_hash_defweak
)
14056 && !bfd_is_const_section (h
->root
.u
.def
.section
))
14057 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14062 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
14063 struct bfd_link_info
*info
)
14065 bfd
*ibfd
= info
->input_bfds
;
14067 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14070 struct elf_reloc_cookie cookie
;
14072 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14074 sec
= ibfd
->sections
;
14075 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14078 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
14081 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
14083 if (startswith (bfd_section_name (sec
), ".eh_frame_entry")
14084 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
14086 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
14087 fini_reloc_cookie_rels (&cookie
, sec
);
14094 /* Do mark and sweep of unused sections. */
14097 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14101 elf_gc_mark_hook_fn gc_mark_hook
;
14102 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14103 struct elf_link_hash_table
*htab
;
14104 struct link_info_ok info_ok
;
14106 if (!bed
->can_gc_sections
14107 || !is_elf_hash_table (info
->hash
))
14109 _bfd_error_handler(_("warning: gc-sections option ignored"));
14113 bed
->gc_keep (info
);
14114 htab
= elf_hash_table (info
);
14116 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14117 at the .eh_frame section if we can mark the FDEs individually. */
14118 for (sub
= info
->input_bfds
;
14119 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14120 sub
= sub
->link
.next
)
14123 struct elf_reloc_cookie cookie
;
14125 sec
= sub
->sections
;
14126 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14128 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14129 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
14131 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14132 if (elf_section_data (sec
)->sec_info
14133 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14134 elf_eh_frame_section (sub
) = sec
;
14135 fini_reloc_cookie_for_section (&cookie
, sec
);
14136 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14140 /* Apply transitive closure to the vtable entry usage info. */
14141 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14145 /* Kill the vtable relocations that were not used. */
14146 info_ok
.info
= info
;
14148 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &info_ok
);
14152 /* Mark dynamically referenced symbols. */
14153 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14154 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14156 /* Grovel through relocs to find out who stays ... */
14157 gc_mark_hook
= bed
->gc_mark_hook
;
14158 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14162 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14163 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14164 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14168 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14171 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14172 Also treat note sections as a root, if the section is not part
14173 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14174 well as FINI_ARRAY sections for ld -r. */
14175 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14177 && (o
->flags
& SEC_EXCLUDE
) == 0
14178 && ((o
->flags
& SEC_KEEP
) != 0
14179 || (bfd_link_relocatable (info
)
14180 && ((elf_section_data (o
)->this_hdr
.sh_type
14181 == SHT_PREINIT_ARRAY
)
14182 || (elf_section_data (o
)->this_hdr
.sh_type
14184 || (elf_section_data (o
)->this_hdr
.sh_type
14185 == SHT_FINI_ARRAY
)))
14186 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14187 && elf_next_in_group (o
) == NULL
14188 && elf_linked_to_section (o
) == NULL
)
14189 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14190 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14192 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14197 /* Allow the backend to mark additional target specific sections. */
14198 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14200 /* ... and mark SEC_EXCLUDE for those that go. */
14201 return elf_gc_sweep (abfd
, info
);
14204 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14207 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14209 struct elf_link_hash_entry
*h
,
14212 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14213 struct elf_link_hash_entry
**search
, *child
;
14214 size_t extsymcount
;
14215 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14217 /* The sh_info field of the symtab header tells us where the
14218 external symbols start. We don't care about the local symbols at
14220 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14221 if (!elf_bad_symtab (abfd
))
14222 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14224 sym_hashes
= elf_sym_hashes (abfd
);
14225 sym_hashes_end
= PTR_ADD (sym_hashes
, extsymcount
);
14227 /* Hunt down the child symbol, which is in this section at the same
14228 offset as the relocation. */
14229 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14231 if ((child
= *search
) != NULL
14232 && (child
->root
.type
== bfd_link_hash_defined
14233 || child
->root
.type
== bfd_link_hash_defweak
)
14234 && child
->root
.u
.def
.section
== sec
14235 && child
->root
.u
.def
.value
== offset
)
14239 /* xgettext:c-format */
14240 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14241 abfd
, sec
, (uint64_t) offset
);
14242 bfd_set_error (bfd_error_invalid_operation
);
14246 if (!child
->u2
.vtable
)
14248 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14249 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14250 if (!child
->u2
.vtable
)
14255 /* This *should* only be the absolute section. It could potentially
14256 be that someone has defined a non-global vtable though, which
14257 would be bad. It isn't worth paging in the local symbols to be
14258 sure though; that case should simply be handled by the assembler. */
14260 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14263 child
->u2
.vtable
->parent
= h
;
14268 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14271 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14272 struct elf_link_hash_entry
*h
,
14275 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14276 unsigned int log_file_align
= bed
->s
->log_file_align
;
14280 /* xgettext:c-format */
14281 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14283 bfd_set_error (bfd_error_bad_value
);
14289 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14290 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14295 if (addend
>= h
->u2
.vtable
->size
)
14297 size_t size
, bytes
, file_align
;
14298 bool *ptr
= h
->u2
.vtable
->used
;
14300 /* While the symbol is undefined, we have to be prepared to handle
14302 file_align
= 1 << log_file_align
;
14303 if (h
->root
.type
== bfd_link_hash_undefined
)
14304 size
= addend
+ file_align
;
14308 if (addend
>= size
)
14310 /* Oops! We've got a reference past the defined end of
14311 the table. This is probably a bug -- shall we warn? */
14312 size
= addend
+ file_align
;
14315 size
= (size
+ file_align
- 1) & -file_align
;
14317 /* Allocate one extra entry for use as a "done" flag for the
14318 consolidation pass. */
14319 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bool);
14323 ptr
= (bool *) bfd_realloc (ptr
- 1, bytes
);
14329 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14331 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14335 ptr
= (bool *) bfd_zmalloc (bytes
);
14340 /* And arrange for that done flag to be at index -1. */
14341 h
->u2
.vtable
->used
= ptr
+ 1;
14342 h
->u2
.vtable
->size
= size
;
14345 h
->u2
.vtable
->used
[addend
>> log_file_align
] = true;
14350 /* Map an ELF section header flag to its corresponding string. */
14354 flagword flag_value
;
14355 } elf_flags_to_name_table
;
14357 static const elf_flags_to_name_table elf_flags_to_names
[] =
14359 { "SHF_WRITE", SHF_WRITE
},
14360 { "SHF_ALLOC", SHF_ALLOC
},
14361 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14362 { "SHF_MERGE", SHF_MERGE
},
14363 { "SHF_STRINGS", SHF_STRINGS
},
14364 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14365 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14366 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14367 { "SHF_GROUP", SHF_GROUP
},
14368 { "SHF_TLS", SHF_TLS
},
14369 { "SHF_MASKOS", SHF_MASKOS
},
14370 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14373 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14375 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14376 struct flag_info
*flaginfo
,
14379 const bfd_vma sh_flags
= elf_section_flags (section
);
14381 if (!flaginfo
->flags_initialized
)
14383 bfd
*obfd
= info
->output_bfd
;
14384 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14385 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14387 int without_hex
= 0;
14389 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14392 flagword (*lookup
) (char *);
14394 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14395 if (lookup
!= NULL
)
14397 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14401 if (tf
->with
== with_flags
)
14402 with_hex
|= hexval
;
14403 else if (tf
->with
== without_flags
)
14404 without_hex
|= hexval
;
14409 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14411 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14413 if (tf
->with
== with_flags
)
14414 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14415 else if (tf
->with
== without_flags
)
14416 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14423 info
->callbacks
->einfo
14424 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14428 flaginfo
->flags_initialized
= true;
14429 flaginfo
->only_with_flags
|= with_hex
;
14430 flaginfo
->not_with_flags
|= without_hex
;
14433 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14436 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14442 struct alloc_got_off_arg
{
14444 struct bfd_link_info
*info
;
14447 /* We need a special top-level link routine to convert got reference counts
14448 to real got offsets. */
14451 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14453 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14454 bfd
*obfd
= gofarg
->info
->output_bfd
;
14455 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14457 if (h
->got
.refcount
> 0)
14459 h
->got
.offset
= gofarg
->gotoff
;
14460 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14463 h
->got
.offset
= (bfd_vma
) -1;
14468 /* And an accompanying bit to work out final got entry offsets once
14469 we're done. Should be called from final_link. */
14472 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14473 struct bfd_link_info
*info
)
14476 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14478 struct alloc_got_off_arg gofarg
;
14480 BFD_ASSERT (abfd
== info
->output_bfd
);
14482 if (! is_elf_hash_table (info
->hash
))
14485 /* The GOT offset is relative to the .got section, but the GOT header is
14486 put into the .got.plt section, if the backend uses it. */
14487 if (bed
->want_got_plt
)
14490 gotoff
= bed
->got_header_size
;
14492 /* Do the local .got entries first. */
14493 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14495 bfd_signed_vma
*local_got
;
14496 size_t j
, locsymcount
;
14497 Elf_Internal_Shdr
*symtab_hdr
;
14499 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
14502 local_got
= elf_local_got_refcounts (i
);
14506 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
14507 if (elf_bad_symtab (i
))
14508 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
14510 locsymcount
= symtab_hdr
->sh_info
;
14512 for (j
= 0; j
< locsymcount
; ++j
)
14514 if (local_got
[j
] > 0)
14516 local_got
[j
] = gotoff
;
14517 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
14520 local_got
[j
] = (bfd_vma
) -1;
14524 /* Then the global .got entries. .plt refcounts are handled by
14525 adjust_dynamic_symbol */
14526 gofarg
.gotoff
= gotoff
;
14527 gofarg
.info
= info
;
14528 elf_link_hash_traverse (elf_hash_table (info
),
14529 elf_gc_allocate_got_offsets
,
14534 /* Many folk need no more in the way of final link than this, once
14535 got entry reference counting is enabled. */
14538 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
14540 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
14543 /* Invoke the regular ELF backend linker to do all the work. */
14544 return bfd_elf_final_link (abfd
, info
);
14548 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
14550 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
14552 if (rcookie
->bad_symtab
)
14553 rcookie
->rel
= rcookie
->rels
;
14555 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
14557 unsigned long r_symndx
;
14559 if (! rcookie
->bad_symtab
)
14560 if (rcookie
->rel
->r_offset
> offset
)
14562 if (rcookie
->rel
->r_offset
!= offset
)
14565 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
14566 if (r_symndx
== STN_UNDEF
)
14569 if (r_symndx
>= rcookie
->locsymcount
14570 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14572 struct elf_link_hash_entry
*h
;
14574 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
14576 while (h
->root
.type
== bfd_link_hash_indirect
14577 || h
->root
.type
== bfd_link_hash_warning
)
14578 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14580 if ((h
->root
.type
== bfd_link_hash_defined
14581 || h
->root
.type
== bfd_link_hash_defweak
)
14582 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
14583 || h
->root
.u
.def
.section
->kept_section
!= NULL
14584 || discarded_section (h
->root
.u
.def
.section
)))
14589 /* It's not a relocation against a global symbol,
14590 but it could be a relocation against a local
14591 symbol for a discarded section. */
14593 Elf_Internal_Sym
*isym
;
14595 /* Need to: get the symbol; get the section. */
14596 isym
= &rcookie
->locsyms
[r_symndx
];
14597 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
14599 && (isec
->kept_section
!= NULL
14600 || discarded_section (isec
)))
14608 /* Discard unneeded references to discarded sections.
14609 Returns -1 on error, 1 if any section's size was changed, 0 if
14610 nothing changed. This function assumes that the relocations are in
14611 sorted order, which is true for all known assemblers. */
14614 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
14616 struct elf_reloc_cookie cookie
;
14621 if (info
->traditional_format
14622 || !is_elf_hash_table (info
->hash
))
14625 o
= bfd_get_section_by_name (output_bfd
, ".stab");
14630 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14633 || i
->reloc_count
== 0
14634 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
14638 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14641 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14644 if (_bfd_discard_section_stabs (abfd
, i
,
14645 elf_section_data (i
)->sec_info
,
14646 bfd_elf_reloc_symbol_deleted_p
,
14650 fini_reloc_cookie_for_section (&cookie
, i
);
14655 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
14656 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
14660 int eh_changed
= 0;
14661 unsigned int eh_alignment
; /* Octets. */
14663 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
14669 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14672 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
14675 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
14676 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
14677 bfd_elf_reloc_symbol_deleted_p
,
14681 if (i
->size
!= i
->rawsize
)
14685 fini_reloc_cookie_for_section (&cookie
, i
);
14688 eh_alignment
= ((1 << o
->alignment_power
)
14689 * bfd_octets_per_byte (output_bfd
, o
));
14690 /* Skip over zero terminator, and prevent empty sections from
14691 adding alignment padding at the end. */
14692 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
14694 i
->flags
|= SEC_EXCLUDE
;
14695 else if (i
->size
> 4)
14697 /* The last non-empty eh_frame section doesn't need padding. */
14700 /* Any prior sections must pad the last FDE out to the output
14701 section alignment. Otherwise we might have zero padding
14702 between sections, which would be seen as a terminator. */
14703 for (; i
!= NULL
; i
= i
->map_tail
.s
)
14705 /* All but the last zero terminator should have been removed. */
14710 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
14711 if (i
->size
!= size
)
14719 elf_link_hash_traverse (elf_hash_table (info
),
14720 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
14723 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
14725 const struct elf_backend_data
*bed
;
14728 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
14730 s
= abfd
->sections
;
14731 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14734 bed
= get_elf_backend_data (abfd
);
14736 if (bed
->elf_backend_discard_info
!= NULL
)
14738 if (!init_reloc_cookie (&cookie
, info
, abfd
))
14741 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
14744 fini_reloc_cookie (&cookie
, abfd
);
14748 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
14749 _bfd_elf_end_eh_frame_parsing (info
);
14751 if (info
->eh_frame_hdr_type
14752 && !bfd_link_relocatable (info
)
14753 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
14760 _bfd_elf_section_already_linked (bfd
*abfd
,
14762 struct bfd_link_info
*info
)
14765 const char *name
, *key
;
14766 struct bfd_section_already_linked
*l
;
14767 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
14769 if (sec
->output_section
== bfd_abs_section_ptr
)
14772 flags
= sec
->flags
;
14774 /* Return if it isn't a linkonce section. A comdat group section
14775 also has SEC_LINK_ONCE set. */
14776 if ((flags
& SEC_LINK_ONCE
) == 0)
14779 /* Don't put group member sections on our list of already linked
14780 sections. They are handled as a group via their group section. */
14781 if (elf_sec_group (sec
) != NULL
)
14784 /* For a SHT_GROUP section, use the group signature as the key. */
14786 if ((flags
& SEC_GROUP
) != 0
14787 && elf_next_in_group (sec
) != NULL
14788 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
14789 key
= elf_group_name (elf_next_in_group (sec
));
14792 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14793 if (startswith (name
, ".gnu.linkonce.")
14794 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
14797 /* Must be a user linkonce section that doesn't follow gcc's
14798 naming convention. In this case we won't be matching
14799 single member groups. */
14803 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
14805 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14807 /* We may have 2 different types of sections on the list: group
14808 sections with a signature of <key> (<key> is some string),
14809 and linkonce sections named .gnu.linkonce.<type>.<key>.
14810 Match like sections. LTO plugin sections are an exception.
14811 They are always named .gnu.linkonce.t.<key> and match either
14812 type of section. */
14813 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
14814 && ((flags
& SEC_GROUP
) != 0
14815 || strcmp (name
, l
->sec
->name
) == 0))
14816 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
14817 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
14819 /* The section has already been linked. See if we should
14820 issue a warning. */
14821 if (!_bfd_handle_already_linked (sec
, l
, info
))
14824 if (flags
& SEC_GROUP
)
14826 asection
*first
= elf_next_in_group (sec
);
14827 asection
*s
= first
;
14831 s
->output_section
= bfd_abs_section_ptr
;
14832 /* Record which group discards it. */
14833 s
->kept_section
= l
->sec
;
14834 s
= elf_next_in_group (s
);
14835 /* These lists are circular. */
14845 /* A single member comdat group section may be discarded by a
14846 linkonce section and vice versa. */
14847 if ((flags
& SEC_GROUP
) != 0)
14849 asection
*first
= elf_next_in_group (sec
);
14851 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
14852 /* Check this single member group against linkonce sections. */
14853 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14854 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14855 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
14857 first
->output_section
= bfd_abs_section_ptr
;
14858 first
->kept_section
= l
->sec
;
14859 sec
->output_section
= bfd_abs_section_ptr
;
14864 /* Check this linkonce section against single member groups. */
14865 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14866 if (l
->sec
->flags
& SEC_GROUP
)
14868 asection
*first
= elf_next_in_group (l
->sec
);
14871 && elf_next_in_group (first
) == first
14872 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
14874 sec
->output_section
= bfd_abs_section_ptr
;
14875 sec
->kept_section
= first
;
14880 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14881 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14882 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14883 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14884 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14885 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14886 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14887 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14888 The reverse order cannot happen as there is never a bfd with only the
14889 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14890 matter as here were are looking only for cross-bfd sections. */
14892 if ((flags
& SEC_GROUP
) == 0 && startswith (name
, ".gnu.linkonce.r."))
14893 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
14894 if ((l
->sec
->flags
& SEC_GROUP
) == 0
14895 && startswith (l
->sec
->name
, ".gnu.linkonce.t."))
14897 if (abfd
!= l
->sec
->owner
)
14898 sec
->output_section
= bfd_abs_section_ptr
;
14902 /* This is the first section with this name. Record it. */
14903 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
14904 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
14905 return sec
->output_section
== bfd_abs_section_ptr
;
14909 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
14911 return sym
->st_shndx
== SHN_COMMON
;
14915 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
14921 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
14923 return bfd_com_section_ptr
;
14927 _bfd_elf_default_got_elt_size (bfd
*abfd
,
14928 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14929 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
14930 bfd
*ibfd ATTRIBUTE_UNUSED
,
14931 unsigned long symndx ATTRIBUTE_UNUSED
)
14933 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14934 return bed
->s
->arch_size
/ 8;
14937 /* Routines to support the creation of dynamic relocs. */
14939 /* Returns the name of the dynamic reloc section associated with SEC. */
14941 static const char *
14942 get_dynamic_reloc_section_name (bfd
* abfd
,
14947 const char *old_name
= bfd_section_name (sec
);
14948 const char *prefix
= is_rela
? ".rela" : ".rel";
14950 if (old_name
== NULL
)
14953 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
14954 sprintf (name
, "%s%s", prefix
, old_name
);
14959 /* Returns the dynamic reloc section associated with SEC.
14960 If necessary compute the name of the dynamic reloc section based
14961 on SEC's name (looked up in ABFD's string table) and the setting
14965 _bfd_elf_get_dynamic_reloc_section (bfd
*abfd
,
14969 asection
*reloc_sec
= elf_section_data (sec
)->sreloc
;
14971 if (reloc_sec
== NULL
)
14973 const char *name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
14977 reloc_sec
= bfd_get_linker_section (abfd
, name
);
14979 if (reloc_sec
!= NULL
)
14980 elf_section_data (sec
)->sreloc
= reloc_sec
;
14987 /* Returns the dynamic reloc section associated with SEC. If the
14988 section does not exist it is created and attached to the DYNOBJ
14989 bfd and stored in the SRELOC field of SEC's elf_section_data
14992 ALIGNMENT is the alignment for the newly created section and
14993 IS_RELA defines whether the name should be .rela.<SEC's name>
14994 or .rel.<SEC's name>. The section name is looked up in the
14995 string table associated with ABFD. */
14998 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
15000 unsigned int alignment
,
15004 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
15006 if (reloc_sec
== NULL
)
15008 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15013 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
15015 if (reloc_sec
== NULL
)
15017 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
15018 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
15019 if ((sec
->flags
& SEC_ALLOC
) != 0)
15020 flags
|= SEC_ALLOC
| SEC_LOAD
;
15022 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
15023 if (reloc_sec
!= NULL
)
15025 /* _bfd_elf_get_sec_type_attr chooses a section type by
15026 name. Override as it may be wrong, eg. for a user
15027 section named "auto" we'll get ".relauto" which is
15028 seen to be a .rela section. */
15029 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
15030 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
15035 elf_section_data (sec
)->sreloc
= reloc_sec
;
15041 /* Copy the ELF symbol type and other attributes for a linker script
15042 assignment from HSRC to HDEST. Generally this should be treated as
15043 if we found a strong non-dynamic definition for HDEST (except that
15044 ld ignores multiple definition errors). */
15046 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
15047 struct bfd_link_hash_entry
*hdest
,
15048 struct bfd_link_hash_entry
*hsrc
)
15050 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
15051 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
15052 Elf_Internal_Sym isym
;
15054 ehdest
->type
= ehsrc
->type
;
15055 ehdest
->target_internal
= ehsrc
->target_internal
;
15057 isym
.st_other
= ehsrc
->other
;
15058 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, true, false);
15061 /* Append a RELA relocation REL to section S in BFD. */
15064 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15066 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15067 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
15068 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
15069 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
15072 /* Append a REL relocation REL to section S in BFD. */
15075 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15077 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15078 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
15079 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
15080 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
15083 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15085 struct bfd_link_hash_entry
*
15086 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
15087 const char *symbol
, asection
*sec
)
15089 struct elf_link_hash_entry
*h
;
15091 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
15092 false, false, true);
15093 /* NB: Common symbols will be turned into definition later. */
15095 && !h
->root
.ldscript_def
15096 && (h
->root
.type
== bfd_link_hash_undefined
15097 || h
->root
.type
== bfd_link_hash_undefweak
15098 || ((h
->ref_regular
|| h
->def_dynamic
)
15100 && h
->root
.type
!= bfd_link_hash_common
)))
15102 bool was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15103 h
->verinfo
.verdef
= NULL
;
15104 h
->root
.type
= bfd_link_hash_defined
;
15105 h
->root
.u
.def
.section
= sec
;
15106 h
->root
.u
.def
.value
= 0;
15107 h
->def_regular
= 1;
15108 h
->def_dynamic
= 0;
15110 h
->u2
.start_stop_section
= sec
;
15111 if (symbol
[0] == '.')
15113 /* .startof. and .sizeof. symbols are local. */
15114 const struct elf_backend_data
*bed
;
15115 bed
= get_elf_backend_data (info
->output_bfd
);
15116 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
15120 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15121 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15122 | info
->start_stop_visibility
);
15124 bfd_elf_link_record_dynamic_symbol (info
, h
);
15131 /* Find dynamic relocs for H that apply to read-only sections. */
15134 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15136 struct elf_dyn_relocs
*p
;
15138 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15140 asection
*s
= p
->sec
->output_section
;
15142 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15148 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15149 read-only sections. */
15152 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15156 if (h
->root
.type
== bfd_link_hash_indirect
)
15159 sec
= _bfd_elf_readonly_dynrelocs (h
);
15162 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15164 info
->flags
|= DF_TEXTREL
;
15165 /* xgettext:c-format */
15166 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15167 "in read-only section `%pA'\n"),
15168 sec
->owner
, h
->root
.root
.string
, sec
);
15170 if (bfd_link_textrel_check (info
))
15171 /* xgettext:c-format */
15172 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15173 "in read-only section `%pA'\n"),
15174 sec
->owner
, h
->root
.root
.string
, sec
);
15176 /* Not an error, just cut short the traversal. */
15182 /* Add dynamic tags. */
15185 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15186 bool need_dynamic_reloc
)
15188 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15190 if (htab
->dynamic_sections_created
)
15192 /* Add some entries to the .dynamic section. We fill in the
15193 values later, in finish_dynamic_sections, but we must add
15194 the entries now so that we get the correct size for the
15195 .dynamic section. The DT_DEBUG entry is filled in by the
15196 dynamic linker and used by the debugger. */
15197 #define add_dynamic_entry(TAG, VAL) \
15198 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15200 const struct elf_backend_data
*bed
15201 = get_elf_backend_data (output_bfd
);
15203 if (bfd_link_executable (info
))
15205 if (!add_dynamic_entry (DT_DEBUG
, 0))
15209 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15211 /* DT_PLTGOT is used by prelink even if there is no PLT
15213 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15217 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15219 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15220 || !add_dynamic_entry (DT_PLTREL
,
15221 (bed
->rela_plts_and_copies_p
15222 ? DT_RELA
: DT_REL
))
15223 || !add_dynamic_entry (DT_JMPREL
, 0))
15227 if (htab
->tlsdesc_plt
15228 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15229 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15232 if (need_dynamic_reloc
)
15234 if (bed
->rela_plts_and_copies_p
)
15236 if (!add_dynamic_entry (DT_RELA
, 0)
15237 || !add_dynamic_entry (DT_RELASZ
, 0)
15238 || !add_dynamic_entry (DT_RELAENT
,
15239 bed
->s
->sizeof_rela
))
15244 if (!add_dynamic_entry (DT_REL
, 0)
15245 || !add_dynamic_entry (DT_RELSZ
, 0)
15246 || !add_dynamic_entry (DT_RELENT
,
15247 bed
->s
->sizeof_rel
))
15251 /* If any dynamic relocs apply to a read-only section,
15252 then we need a DT_TEXTREL entry. */
15253 if ((info
->flags
& DF_TEXTREL
) == 0)
15254 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15257 if ((info
->flags
& DF_TEXTREL
) != 0)
15259 if (htab
->ifunc_resolvers
)
15260 info
->callbacks
->einfo
15261 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15262 "may result in a segfault at runtime; recompile with %s\n"),
15263 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15265 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15270 #undef add_dynamic_entry