1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2025 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 static bool _bfd_elf_fix_symbol_flags
50 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
52 /* Return false if linker should avoid caching relocation information
53 and symbol tables of input files in memory. */
56 _bfd_elf_link_keep_memory (struct bfd_link_info
*info
)
59 /* Don't cache symbol nor relocation tables if they are mapped in.
60 NB: Since the --no-keep-memory linker option causes:
62 https://sourceware.org/bugzilla/show_bug.cgi?id=31458
64 this is opt-in by each backend. */
65 const struct elf_backend_data
*bed
66 = get_elf_backend_data (info
->output_bfd
);
67 if (bed
!= NULL
&& bed
->use_mmap
)
73 if (!info
->keep_memory
)
76 if (info
->max_cache_size
== (bfd_size_type
) -1)
79 abfd
= info
->input_bfds
;
80 size
= info
->cache_size
;
83 if (size
>= info
->max_cache_size
)
85 /* Over the limit. Reduce the memory usage. */
86 info
->keep_memory
= false;
91 size
+= abfd
->alloc_size
;
92 abfd
= abfd
->link
.next
;
99 static struct elf_link_hash_entry
*
100 get_link_hash_entry (struct elf_link_hash_entry
** sym_hashes
,
102 unsigned int ext_sym_start
)
104 if (sym_hashes
== NULL
105 /* Guard against corrupt input. See PR 32636 for an example. */
106 || symndx
< ext_sym_start
)
109 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- ext_sym_start
];
111 /* The hash might be empty. See PR 32641 for an example of this. */
115 while (h
->root
.type
== bfd_link_hash_indirect
116 || h
->root
.type
== bfd_link_hash_warning
)
117 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
122 struct elf_link_hash_entry
*
123 _bfd_elf_get_link_hash_entry (struct elf_link_hash_entry
** sym_hashes
,
125 Elf_Internal_Shdr
* symtab_hdr
)
127 if (symtab_hdr
== NULL
)
130 return get_link_hash_entry (sym_hashes
, symndx
, symtab_hdr
->sh_info
);
133 static struct elf_link_hash_entry
*
134 get_ext_sym_hash_from_cookie (struct elf_reloc_cookie
*cookie
, unsigned long r_symndx
)
136 if (cookie
== NULL
|| cookie
->sym_hashes
== NULL
)
139 if (r_symndx
>= cookie
->locsymcount
140 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
141 return get_link_hash_entry (cookie
->sym_hashes
, r_symndx
, cookie
->extsymoff
);
147 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
148 unsigned long r_symndx
,
151 struct elf_link_hash_entry
*h
;
153 h
= get_ext_sym_hash_from_cookie (cookie
, r_symndx
);
157 if ((h
->root
.type
== bfd_link_hash_defined
158 || h
->root
.type
== bfd_link_hash_defweak
)
159 && discarded_section (h
->root
.u
.def
.section
))
160 return h
->root
.u
.def
.section
;
165 /* It's not a relocation against a global symbol,
166 but it could be a relocation against a local
167 symbol for a discarded section. */
169 Elf_Internal_Sym
*isym
;
171 /* Need to: get the symbol; get the section. */
172 isym
= &cookie
->locsyms
[r_symndx
];
173 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
175 && discard
? discarded_section (isec
) : 1)
181 /* Define a symbol in a dynamic linkage section. */
183 struct elf_link_hash_entry
*
184 _bfd_elf_define_linkage_sym (bfd
*abfd
,
185 struct bfd_link_info
*info
,
189 struct elf_link_hash_entry
*h
;
190 struct bfd_link_hash_entry
*bh
;
191 const struct elf_backend_data
*bed
;
193 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, false, false, false);
196 /* Zap symbol defined in an as-needed lib that wasn't linked.
197 This is a symptom of a larger problem: Absolute symbols
198 defined in shared libraries can't be overridden, because we
199 lose the link to the bfd which is via the symbol section. */
200 h
->root
.type
= bfd_link_hash_new
;
206 bed
= get_elf_backend_data (abfd
);
207 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
208 sec
, 0, NULL
, false, bed
->collect
,
211 h
= (struct elf_link_hash_entry
*) bh
;
212 BFD_ASSERT (h
!= NULL
);
215 h
->root
.linker_def
= 1;
216 h
->type
= STT_OBJECT
;
217 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
218 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
220 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
225 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
229 struct elf_link_hash_entry
*h
;
230 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
231 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
233 /* This function may be called more than once. */
234 if (htab
->sgot
!= NULL
)
237 flags
= bed
->dynamic_sec_flags
;
239 s
= bfd_make_section_anyway_with_flags (abfd
,
240 (bed
->rela_plts_and_copies_p
241 ? ".rela.got" : ".rel.got"),
242 flags
| SEC_READONLY
);
244 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
248 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
250 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
254 if (bed
->want_got_plt
)
256 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
258 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
263 /* The first bit of the global offset table is the header. */
264 s
->size
+= bed
->got_header_size
;
266 if (bed
->want_got_sym
)
268 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
269 (or .got.plt) section. We don't do this in the linker script
270 because we don't want to define the symbol if we are not creating
271 a global offset table. */
272 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
273 "_GLOBAL_OFFSET_TABLE_");
274 elf_hash_table (info
)->hgot
= h
;
282 /* Create a strtab to hold the dynamic symbol names. */
284 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
286 struct elf_link_hash_table
*hash_table
;
288 hash_table
= elf_hash_table (info
);
289 if (hash_table
->dynobj
== NULL
)
291 /* We may not set dynobj, an input file holding linker created
292 dynamic sections to abfd, which may be a dynamic object with
293 its own dynamic sections. We need to find a normal input file
294 to hold linker created sections if possible. */
295 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
299 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
301 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0
302 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
303 && elf_object_id (ibfd
) == elf_hash_table_id (hash_table
)
304 && !((s
= ibfd
->sections
) != NULL
305 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
))
311 hash_table
->dynobj
= abfd
;
314 if (hash_table
->dynstr
== NULL
)
316 hash_table
->dynstr
= _bfd_elf_strtab_init ();
317 if (hash_table
->dynstr
== NULL
)
323 /* Create some sections which will be filled in with dynamic linking
324 information. ABFD is an input file which requires dynamic sections
325 to be created. The dynamic sections take up virtual memory space
326 when the final executable is run, so we need to create them before
327 addresses are assigned to the output sections. We work out the
328 actual contents and size of these sections later. */
331 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
335 const struct elf_backend_data
*bed
;
336 struct elf_link_hash_entry
*h
;
338 if (! is_elf_hash_table (info
->hash
))
341 if (elf_hash_table (info
)->dynamic_sections_created
)
344 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
347 abfd
= elf_hash_table (info
)->dynobj
;
348 bed
= get_elf_backend_data (abfd
);
350 flags
= bed
->dynamic_sec_flags
;
352 /* A dynamically linked executable has a .interp section, but a
353 shared library does not. */
354 if (bfd_link_executable (info
) && !info
->nointerp
)
356 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
357 flags
| SEC_READONLY
);
362 /* Create sections to hold version informations. These are removed
363 if they are not needed. */
364 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
365 flags
| SEC_READONLY
);
367 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
370 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
371 flags
| SEC_READONLY
);
373 || !bfd_set_section_alignment (s
, 1))
376 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
377 flags
| SEC_READONLY
);
379 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
382 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
383 flags
| SEC_READONLY
);
385 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
387 elf_hash_table (info
)->dynsym
= s
;
389 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
390 flags
| SEC_READONLY
);
394 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
396 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
398 elf_hash_table (info
)->dynamic
= s
;
400 /* The special symbol _DYNAMIC is always set to the start of the
401 .dynamic section. We could set _DYNAMIC in a linker script, but we
402 only want to define it if we are, in fact, creating a .dynamic
403 section. We don't want to define it if there is no .dynamic
404 section, since on some ELF platforms the start up code examines it
405 to decide how to initialize the process. */
406 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
407 elf_hash_table (info
)->hdynamic
= h
;
413 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
414 flags
| SEC_READONLY
);
416 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
418 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
421 if (info
->emit_gnu_hash
&& bed
->record_xhash_symbol
== NULL
)
423 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
424 flags
| SEC_READONLY
);
426 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
428 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
429 4 32-bit words followed by variable count of 64-bit words, then
430 variable count of 32-bit words. */
431 if (bed
->s
->arch_size
== 64)
432 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
434 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
437 if (info
->enable_dt_relr
)
439 s
= bfd_make_section_anyway_with_flags (abfd
, ".relr.dyn",
440 flags
| SEC_READONLY
);
442 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
444 elf_hash_table (info
)->srelrdyn
= s
;
447 /* Let the backend create the rest of the sections. This lets the
448 backend set the right flags. The backend will normally create
449 the .got and .plt sections. */
450 if (bed
->elf_backend_create_dynamic_sections
== NULL
451 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
454 elf_hash_table (info
)->dynamic_sections_created
= true;
459 /* Create dynamic sections when linking against a dynamic object. */
462 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
464 flagword flags
, pltflags
;
465 struct elf_link_hash_entry
*h
;
467 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
468 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
470 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
471 .rel[a].bss sections. */
472 flags
= bed
->dynamic_sec_flags
;
475 if (bed
->plt_not_loaded
)
476 /* We do not clear SEC_ALLOC here because we still want the OS to
477 allocate space for the section; it's just that there's nothing
478 to read in from the object file. */
479 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
481 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
482 if (bed
->plt_readonly
)
483 pltflags
|= SEC_READONLY
;
485 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
487 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
491 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
493 if (bed
->want_plt_sym
)
495 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
496 "_PROCEDURE_LINKAGE_TABLE_");
497 elf_hash_table (info
)->hplt
= h
;
502 s
= bfd_make_section_anyway_with_flags (abfd
,
503 (bed
->rela_plts_and_copies_p
504 ? ".rela.plt" : ".rel.plt"),
505 flags
| SEC_READONLY
);
507 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
511 if (! _bfd_elf_create_got_section (abfd
, info
))
514 if (bed
->want_dynbss
)
516 /* The .dynbss section is a place to put symbols which are defined
517 by dynamic objects, are referenced by regular objects, and are
518 not functions. We must allocate space for them in the process
519 image and use a R_*_COPY reloc to tell the dynamic linker to
520 initialize them at run time. The linker script puts the .dynbss
521 section into the .bss section of the final image. */
522 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
523 SEC_ALLOC
| SEC_LINKER_CREATED
);
528 if (bed
->want_dynrelro
)
530 /* Similarly, but for symbols that were originally in read-only
531 sections. This section doesn't really need to have contents,
532 but make it like other .data.rel.ro sections. */
533 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
540 /* The .rel[a].bss section holds copy relocs. This section is not
541 normally needed. We need to create it here, though, so that the
542 linker will map it to an output section. We can't just create it
543 only if we need it, because we will not know whether we need it
544 until we have seen all the input files, and the first time the
545 main linker code calls BFD after examining all the input files
546 (size_dynamic_sections) the input sections have already been
547 mapped to the output sections. If the section turns out not to
548 be needed, we can discard it later. We will never need this
549 section when generating a shared object, since they do not use
551 if (bfd_link_executable (info
))
553 s
= bfd_make_section_anyway_with_flags (abfd
,
554 (bed
->rela_plts_and_copies_p
555 ? ".rela.bss" : ".rel.bss"),
556 flags
| SEC_READONLY
);
558 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
562 if (bed
->want_dynrelro
)
564 s
= (bfd_make_section_anyway_with_flags
565 (abfd
, (bed
->rela_plts_and_copies_p
566 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
567 flags
| SEC_READONLY
));
569 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
571 htab
->sreldynrelro
= s
;
579 /* Record a new dynamic symbol. We record the dynamic symbols as we
580 read the input files, since we need to have a list of all of them
581 before we can determine the final sizes of the output sections.
582 Note that we may actually call this function even though we are not
583 going to output any dynamic symbols; in some cases we know that a
584 symbol should be in the dynamic symbol table, but only if there is
588 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
589 struct elf_link_hash_entry
*h
)
591 if (h
->dynindx
== -1)
593 struct elf_strtab_hash
*dynstr
;
598 if (h
->root
.type
== bfd_link_hash_defined
599 || h
->root
.type
== bfd_link_hash_defweak
)
601 /* An IR symbol should not be made dynamic. */
602 if (h
->root
.u
.def
.section
!= NULL
603 && h
->root
.u
.def
.section
->owner
!= NULL
604 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
608 /* XXX: The ABI draft says the linker must turn hidden and
609 internal symbols into STB_LOCAL symbols when producing the
610 DSO. However, if ld.so honors st_other in the dynamic table,
611 this would not be necessary. */
612 switch (ELF_ST_VISIBILITY (h
->other
))
616 if (h
->root
.type
!= bfd_link_hash_undefined
617 && h
->root
.type
!= bfd_link_hash_undefweak
)
627 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
628 ++elf_hash_table (info
)->dynsymcount
;
630 dynstr
= elf_hash_table (info
)->dynstr
;
633 /* Create a strtab to hold the dynamic symbol names. */
634 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
639 char *unversioned_name
= NULL
;
641 /* We don't put any version information in the dynamic string
643 name
= h
->root
.root
.string
;
644 p
= strchr (name
, ELF_VER_CHR
);
647 unversioned_name
= bfd_malloc (p
- name
+ 1);
648 memcpy (unversioned_name
, name
, p
- name
);
649 unversioned_name
[p
- name
] = 0;
650 name
= unversioned_name
;
653 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
656 free (unversioned_name
);
658 if (indx
== (size_t) -1)
660 h
->dynstr_index
= indx
;
666 /* Mark a symbol dynamic. */
669 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
670 struct elf_link_hash_entry
*h
,
671 Elf_Internal_Sym
*sym
)
673 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
675 /* It may be called more than once on the same H. */
676 if(h
->dynamic
|| bfd_link_relocatable (info
))
679 if ((info
->dynamic_data
680 && (h
->type
== STT_OBJECT
681 || h
->type
== STT_COMMON
683 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
684 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
687 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
690 /* NB: If a symbol is made dynamic by --dynamic-list, it has
692 h
->root
.non_ir_ref_dynamic
= 1;
696 /* Record an assignment to a symbol made by a linker script. We need
697 this in case some dynamic object refers to this symbol. */
700 bfd_elf_record_link_assignment (bfd
*output_bfd
,
701 struct bfd_link_info
*info
,
706 struct elf_link_hash_entry
*h
, *hv
;
707 struct elf_link_hash_table
*htab
;
708 const struct elf_backend_data
*bed
;
710 if (!is_elf_hash_table (info
->hash
))
713 htab
= elf_hash_table (info
);
714 h
= elf_link_hash_lookup (htab
, name
, !provide
, true, false);
718 if (h
->root
.type
== bfd_link_hash_warning
)
719 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
721 if (h
->versioned
== unknown
)
723 /* Set versioned if symbol version is unknown. */
724 char *version
= strrchr (name
, ELF_VER_CHR
);
727 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
728 h
->versioned
= versioned_hidden
;
730 h
->versioned
= versioned
;
734 /* Symbols defined in a linker script but not referenced anywhere
735 else will have non_elf set. */
738 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
742 switch (h
->root
.type
)
744 case bfd_link_hash_defined
:
745 case bfd_link_hash_defweak
:
746 case bfd_link_hash_common
:
748 case bfd_link_hash_undefweak
:
749 case bfd_link_hash_undefined
:
750 /* Since we're defining the symbol, don't let it seem to have not
751 been defined. record_dynamic_symbol and size_dynamic_sections
752 may depend on this. */
753 h
->root
.type
= bfd_link_hash_new
;
754 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
755 bfd_link_repair_undef_list (&htab
->root
);
757 case bfd_link_hash_new
:
759 case bfd_link_hash_indirect
:
760 /* We had a versioned symbol in a dynamic library. We make the
761 the versioned symbol point to this one. */
762 bed
= get_elf_backend_data (output_bfd
);
764 while (hv
->root
.type
== bfd_link_hash_indirect
765 || hv
->root
.type
== bfd_link_hash_warning
)
766 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
767 /* We don't need to update h->root.u since linker will set them
769 h
->root
.type
= bfd_link_hash_undefined
;
770 hv
->root
.type
= bfd_link_hash_indirect
;
771 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
772 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
779 /* If this symbol is being provided by the linker script, and it is
780 currently defined by a dynamic object, but not by a regular
781 object, then mark it as undefined so that the generic linker will
782 force the correct value. */
786 h
->root
.type
= bfd_link_hash_undefined
;
788 /* If this symbol is currently defined by a dynamic object, but not
789 by a regular object, then clear out any version information because
790 the symbol will not be associated with the dynamic object any
792 if (h
->def_dynamic
&& !h
->def_regular
)
793 h
->verinfo
.verdef
= NULL
;
795 /* Make sure this symbol is not garbage collected. */
802 bed
= get_elf_backend_data (output_bfd
);
803 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
804 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
805 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
808 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
810 if (!bfd_link_relocatable (info
)
812 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
813 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
818 || bfd_link_dll (info
))
822 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
825 /* If this is a weak defined symbol, and we know a corresponding
826 real symbol from the same dynamic object, make sure the real
827 symbol is also made into a dynamic symbol. */
830 struct elf_link_hash_entry
*def
= weakdef (h
);
832 if (def
->dynindx
== -1
833 && !bfd_elf_link_record_dynamic_symbol (info
, def
))
841 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
842 success, and 2 on a failure caused by attempting to record a symbol
843 in a discarded section, eg. a discarded link-once section symbol. */
846 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
851 struct elf_link_local_dynamic_entry
*entry
;
852 struct elf_link_hash_table
*eht
;
853 struct elf_strtab_hash
*dynstr
;
856 Elf_External_Sym_Shndx eshndx
;
857 char esym
[sizeof (Elf64_External_Sym
)];
859 if (! is_elf_hash_table (info
->hash
))
862 /* See if the entry exists already. */
863 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
864 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
867 amt
= sizeof (*entry
);
868 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
872 /* Go find the symbol, so that we can find it's name. */
873 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
874 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
876 bfd_release (input_bfd
, entry
);
880 if (entry
->isym
.st_shndx
!= SHN_UNDEF
881 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
885 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
886 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
888 /* We can still bfd_release here as nothing has done another
889 bfd_alloc. We can't do this later in this function. */
890 bfd_release (input_bfd
, entry
);
895 name
= (bfd_elf_string_from_elf_section
896 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
897 entry
->isym
.st_name
));
899 dynstr
= elf_hash_table (info
)->dynstr
;
902 /* Create a strtab to hold the dynamic symbol names. */
903 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
908 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, false);
909 if (dynstr_index
== (size_t) -1)
911 entry
->isym
.st_name
= dynstr_index
;
913 eht
= elf_hash_table (info
);
915 entry
->next
= eht
->dynlocal
;
916 eht
->dynlocal
= entry
;
917 entry
->input_bfd
= input_bfd
;
918 entry
->input_indx
= input_indx
;
921 /* Whatever binding the symbol had before, it's now local. */
923 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
925 /* The dynindx will be set at the end of size_dynamic_sections. */
930 /* Return the dynindex of a local dynamic symbol. */
933 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
937 struct elf_link_local_dynamic_entry
*e
;
939 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
940 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
945 /* This function is used to renumber the dynamic symbols, if some of
946 them are removed because they are marked as local. This is called
947 via elf_link_hash_traverse. */
950 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
953 size_t *count
= (size_t *) data
;
958 if (h
->dynindx
!= -1)
959 h
->dynindx
= ++(*count
);
965 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
966 STB_LOCAL binding. */
969 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
972 size_t *count
= (size_t *) data
;
974 if (!h
->forced_local
)
977 if (h
->dynindx
!= -1)
978 h
->dynindx
= ++(*count
);
983 /* Return true if the dynamic symbol for a given section should be
984 omitted when creating a shared library. */
986 _bfd_elf_omit_section_dynsym_default (bfd
*output_bfd ATTRIBUTE_UNUSED
,
987 struct bfd_link_info
*info
,
990 struct elf_link_hash_table
*htab
;
993 switch (elf_section_data (p
)->this_hdr
.sh_type
)
997 /* If sh_type is yet undecided, assume it could be
998 SHT_PROGBITS/SHT_NOBITS. */
1000 htab
= elf_hash_table (info
);
1001 if (htab
->text_index_section
!= NULL
)
1002 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
1004 return (htab
->dynobj
!= NULL
1005 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
1006 && ip
->output_section
== p
);
1008 /* There shouldn't be section relative relocations
1009 against any other section. */
1016 _bfd_elf_omit_section_dynsym_all
1017 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
1018 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1019 asection
*p ATTRIBUTE_UNUSED
)
1024 /* Assign dynsym indices. In a shared library we generate a section
1025 symbol for each output section, which come first. Next come symbols
1026 which have been forced to local binding. Then all of the back-end
1027 allocated local dynamic syms, followed by the rest of the global
1028 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
1029 (This prevents the early call before elf_backend_init_index_section
1030 and strip_excluded_output_sections setting dynindx for sections
1031 that are stripped.) */
1033 static unsigned long
1034 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
1035 struct bfd_link_info
*info
,
1036 unsigned long *section_sym_count
)
1038 unsigned long dynsymcount
= 0;
1039 bool do_sec
= section_sym_count
!= NULL
;
1041 if (bfd_link_pic (info
)
1042 || elf_hash_table (info
)->is_relocatable_executable
)
1044 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
1046 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
1047 if ((p
->flags
& SEC_EXCLUDE
) == 0
1048 && (p
->flags
& SEC_ALLOC
) != 0
1049 && elf_hash_table (info
)->dynamic_relocs
1050 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
1054 elf_section_data (p
)->dynindx
= dynsymcount
;
1057 elf_section_data (p
)->dynindx
= 0;
1060 *section_sym_count
= dynsymcount
;
1062 elf_link_hash_traverse (elf_hash_table (info
),
1063 elf_link_renumber_local_hash_table_dynsyms
,
1066 if (elf_hash_table (info
)->dynlocal
)
1068 struct elf_link_local_dynamic_entry
*p
;
1069 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
1070 p
->dynindx
= ++dynsymcount
;
1072 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
1074 elf_link_hash_traverse (elf_hash_table (info
),
1075 elf_link_renumber_hash_table_dynsyms
,
1078 /* There is an unused NULL entry at the head of the table which we
1079 must account for in our count even if the table is empty since it
1080 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1081 .dynamic section. */
1084 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
1088 /* Merge st_other field. */
1091 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
1092 unsigned int st_other
, asection
*sec
,
1093 bool definition
, bool dynamic
)
1095 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1097 /* If st_other has a processor-specific meaning, specific
1098 code might be needed here. */
1099 if (bed
->elf_backend_merge_symbol_attribute
)
1100 (*bed
->elf_backend_merge_symbol_attribute
) (h
, st_other
, definition
,
1105 unsigned symvis
= ELF_ST_VISIBILITY (st_other
);
1106 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
1108 /* Keep the most constraining visibility. Leave the remainder
1109 of the st_other field to elf_backend_merge_symbol_attribute. */
1110 if (symvis
- 1 < hvis
- 1)
1111 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
1114 && ELF_ST_VISIBILITY (st_other
) != STV_DEFAULT
1115 && (sec
->flags
& SEC_READONLY
) == 0)
1116 h
->protected_def
= 1;
1119 /* This function is called when we want to merge a new symbol with an
1120 existing symbol. It handles the various cases which arise when we
1121 find a definition in a dynamic object, or when there is already a
1122 definition in a dynamic object. The new symbol is described by
1123 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1124 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1125 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1126 of an old common symbol. We set OVERRIDE if the old symbol is
1127 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1128 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1129 to change. By OK to change, we mean that we shouldn't warn if the
1130 type or size does change. */
1133 _bfd_elf_merge_symbol (bfd
*abfd
,
1134 struct bfd_link_info
*info
,
1136 Elf_Internal_Sym
*sym
,
1139 struct elf_link_hash_entry
**sym_hash
,
1142 unsigned int *pold_alignment
,
1145 bool *type_change_ok
,
1146 bool *size_change_ok
,
1149 asection
*sec
, *oldsec
;
1150 struct elf_link_hash_entry
*h
;
1151 struct elf_link_hash_entry
*hi
;
1152 struct elf_link_hash_entry
*flip
;
1155 bool newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1156 bool newweak
, oldweak
, newfunc
, oldfunc
;
1157 const struct elf_backend_data
*bed
;
1159 bool default_sym
= *matched
;
1160 struct elf_link_hash_table
*htab
;
1166 bind
= ELF_ST_BIND (sym
->st_info
);
1168 if (! bfd_is_und_section (sec
))
1169 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
1171 h
= ((struct elf_link_hash_entry
*)
1172 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
1177 bed
= get_elf_backend_data (abfd
);
1179 /* NEW_VERSION is the symbol version of the new symbol. */
1180 if (h
->versioned
!= unversioned
)
1182 /* Symbol version is unknown or versioned. */
1183 new_version
= strrchr (name
, ELF_VER_CHR
);
1186 if (h
->versioned
== unknown
)
1188 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1189 h
->versioned
= versioned_hidden
;
1191 h
->versioned
= versioned
;
1194 if (new_version
[0] == '\0')
1198 h
->versioned
= unversioned
;
1203 /* For merging, we only care about real symbols. But we need to make
1204 sure that indirect symbol dynamic flags are updated. */
1206 while (h
->root
.type
== bfd_link_hash_indirect
1207 || h
->root
.type
== bfd_link_hash_warning
)
1208 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1212 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1216 /* OLD_HIDDEN is true if the existing symbol is only visible
1217 to the symbol with the same symbol version. NEW_HIDDEN is
1218 true if the new symbol is only visible to the symbol with
1219 the same symbol version. */
1220 bool old_hidden
= h
->versioned
== versioned_hidden
;
1221 bool new_hidden
= hi
->versioned
== versioned_hidden
;
1222 if (!old_hidden
&& !new_hidden
)
1223 /* The new symbol matches the existing symbol if both
1228 /* OLD_VERSION is the symbol version of the existing
1232 if (h
->versioned
>= versioned
)
1233 old_version
= strrchr (h
->root
.root
.string
,
1238 /* The new symbol matches the existing symbol if they
1239 have the same symbol version. */
1240 *matched
= (old_version
== new_version
1241 || (old_version
!= NULL
1242 && new_version
!= NULL
1243 && strcmp (old_version
, new_version
) == 0));
1248 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1253 switch (h
->root
.type
)
1258 case bfd_link_hash_undefined
:
1259 case bfd_link_hash_undefweak
:
1260 oldbfd
= h
->root
.u
.undef
.abfd
;
1263 case bfd_link_hash_defined
:
1264 case bfd_link_hash_defweak
:
1265 oldbfd
= h
->root
.u
.def
.section
->owner
;
1266 oldsec
= h
->root
.u
.def
.section
;
1269 case bfd_link_hash_common
:
1270 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1271 oldsec
= h
->root
.u
.c
.p
->section
;
1273 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1276 if (poldbfd
&& *poldbfd
== NULL
)
1279 /* Differentiate strong and weak symbols. */
1280 newweak
= bind
== STB_WEAK
;
1281 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1282 || h
->root
.type
== bfd_link_hash_undefweak
);
1284 *pold_weak
= oldweak
;
1286 /* We have to check it for every instance since the first few may be
1287 references and not all compilers emit symbol type for undefined
1289 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1291 htab
= elf_hash_table (info
);
1293 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1294 respectively, is from a dynamic object. */
1296 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1298 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1299 syms and defined syms in dynamic libraries respectively.
1300 ref_dynamic on the other hand can be set for a symbol defined in
1301 a dynamic library, and def_dynamic may not be set; When the
1302 definition in a dynamic lib is overridden by a definition in the
1303 executable use of the symbol in the dynamic lib becomes a
1304 reference to the executable symbol. */
1307 if (bfd_is_und_section (sec
))
1309 if (bind
!= STB_WEAK
)
1311 h
->ref_dynamic_nonweak
= 1;
1312 hi
->ref_dynamic_nonweak
= 1;
1317 /* Update the existing symbol only if they match. */
1320 hi
->dynamic_def
= 1;
1324 /* If we just created the symbol, mark it as being an ELF symbol.
1325 Other than that, there is nothing to do--there is no merge issue
1326 with a newly defined symbol--so we just return. */
1328 if (h
->root
.type
== bfd_link_hash_new
)
1334 /* In cases involving weak versioned symbols, we may wind up trying
1335 to merge a symbol with itself. Catch that here, to avoid the
1336 confusion that results if we try to override a symbol with
1337 itself. The additional tests catch cases like
1338 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1339 dynamic object, which we do want to handle here. */
1341 && (newweak
|| oldweak
)
1342 && ((abfd
->flags
& DYNAMIC
) == 0
1343 || !h
->def_regular
))
1348 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1349 else if (oldsec
!= NULL
)
1351 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1352 indices used by MIPS ELF. */
1353 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1356 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1357 if (!htab
->handling_dt_needed
1359 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
))
1361 if (newdyn
!= olddyn
)
1363 /* Handle a case where plugin_notice won't be called and thus
1364 won't set the non_ir_ref flags on the first pass over
1366 h
->root
.non_ir_ref_dynamic
= true;
1367 hi
->root
.non_ir_ref_dynamic
= true;
1369 else if ((oldbfd
->flags
& BFD_PLUGIN
) != 0
1370 && hi
->root
.type
== bfd_link_hash_indirect
)
1372 /* Change indirect symbol from IR to undefined. */
1373 hi
->root
.type
= bfd_link_hash_undefined
;
1374 hi
->root
.u
.undef
.abfd
= oldbfd
;
1378 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1379 respectively, appear to be a definition rather than reference. */
1381 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1383 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1384 && h
->root
.type
!= bfd_link_hash_undefweak
1385 && h
->root
.type
!= bfd_link_hash_common
);
1387 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1388 respectively, appear to be a function. */
1390 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1391 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1393 oldfunc
= (h
->type
!= STT_NOTYPE
1394 && bed
->is_function_type (h
->type
));
1396 if (!(newfunc
&& oldfunc
)
1397 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1398 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1399 && h
->type
!= STT_NOTYPE
1400 && (newdef
|| bfd_is_com_section (sec
))
1401 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1403 /* If creating a default indirect symbol ("foo" or "foo@") from
1404 a dynamic versioned definition ("foo@@") skip doing so if
1405 there is an existing regular definition with a different
1406 type. We don't want, for example, a "time" variable in the
1407 executable overriding a "time" function in a shared library. */
1415 /* When adding a symbol from a regular object file after we have
1416 created indirect symbols, undo the indirection and any
1423 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1424 h
->forced_local
= 0;
1428 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1430 h
->root
.type
= bfd_link_hash_undefined
;
1431 h
->root
.u
.undef
.abfd
= abfd
;
1435 h
->root
.type
= bfd_link_hash_new
;
1436 h
->root
.u
.undef
.abfd
= NULL
;
1442 /* Check TLS symbols. We don't check undefined symbols introduced
1443 by "ld -u" which have no type (and oldbfd NULL), and we don't
1444 check symbols from plugins because they also have no type. */
1446 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1447 && (abfd
->flags
& BFD_PLUGIN
) == 0
1448 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1449 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1453 asection
*ntsec
, *tsec
;
1455 if (h
->type
== STT_TLS
)
1476 /* xgettext:c-format */
1477 (_("%s: TLS definition in %pB section %pA "
1478 "mismatches non-TLS definition in %pB section %pA"),
1479 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1480 else if (!tdef
&& !ntdef
)
1482 /* xgettext:c-format */
1483 (_("%s: TLS reference in %pB "
1484 "mismatches non-TLS reference in %pB"),
1485 h
->root
.root
.string
, tbfd
, ntbfd
);
1488 /* xgettext:c-format */
1489 (_("%s: TLS definition in %pB section %pA "
1490 "mismatches non-TLS reference in %pB"),
1491 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1494 /* xgettext:c-format */
1495 (_("%s: TLS reference in %pB "
1496 "mismatches non-TLS definition in %pB section %pA"),
1497 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1499 bfd_set_error (bfd_error_bad_value
);
1503 /* If the old symbol has non-default visibility, we ignore the new
1504 definition from a dynamic object. */
1506 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1507 && !bfd_is_und_section (sec
))
1510 /* Make sure this symbol is dynamic. */
1512 hi
->ref_dynamic
= 1;
1513 /* A protected symbol has external availability. Make sure it is
1514 recorded as dynamic.
1516 FIXME: Should we check type and size for protected symbol? */
1517 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1518 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1523 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1526 /* If the new symbol with non-default visibility comes from a
1527 relocatable file and the old definition comes from a dynamic
1528 object, we remove the old definition. */
1529 if (hi
->root
.type
== bfd_link_hash_indirect
)
1531 /* Handle the case where the old dynamic definition is
1532 default versioned. We need to copy the symbol info from
1533 the symbol with default version to the normal one if it
1534 was referenced before. */
1537 hi
->root
.type
= h
->root
.type
;
1538 h
->root
.type
= bfd_link_hash_indirect
;
1539 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1541 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1542 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1544 /* If the new symbol is hidden or internal, completely undo
1545 any dynamic link state. */
1546 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1547 h
->forced_local
= 0;
1554 /* FIXME: Should we check type and size for protected symbol? */
1564 /* If the old symbol was undefined before, then it will still be
1565 on the undefs list. If the new symbol is undefined or
1566 common, we can't make it bfd_link_hash_new here, because new
1567 undefined or common symbols will be added to the undefs list
1568 by _bfd_generic_link_add_one_symbol. Symbols may not be
1569 added twice to the undefs list. Also, if the new symbol is
1570 undefweak then we don't want to lose the strong undef. */
1571 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1573 h
->root
.type
= bfd_link_hash_undefined
;
1574 h
->root
.u
.undef
.abfd
= abfd
;
1578 h
->root
.type
= bfd_link_hash_new
;
1579 h
->root
.u
.undef
.abfd
= NULL
;
1582 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1584 /* If the new symbol is hidden or internal, completely undo
1585 any dynamic link state. */
1586 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1587 h
->forced_local
= 0;
1593 /* FIXME: Should we check type and size for protected symbol? */
1599 /* If a new weak symbol definition comes from a regular file and the
1600 old symbol comes from a dynamic library, we treat the new one as
1601 strong. Similarly, an old weak symbol definition from a regular
1602 file is treated as strong when the new symbol comes from a dynamic
1603 library. Further, an old weak symbol from a dynamic library is
1604 treated as strong if the new symbol is from a dynamic library.
1605 This reflects the way glibc's ld.so works.
1607 Also allow a weak symbol to override a linker script symbol
1608 defined by an early pass over the script. This is done so the
1609 linker knows the symbol is defined in an object file, for the
1610 DEFINED script function.
1612 Do this before setting *type_change_ok or *size_change_ok so that
1613 we warn properly when dynamic library symbols are overridden. */
1615 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1617 if (olddef
&& newdyn
)
1620 /* Allow changes between different types of function symbol. */
1621 if (newfunc
&& oldfunc
)
1622 *type_change_ok
= true;
1624 /* It's OK to change the type if either the existing symbol or the
1625 new symbol is weak. A type change is also OK if the old symbol
1626 is undefined and the new symbol is defined. */
1631 && h
->root
.type
== bfd_link_hash_undefined
))
1632 *type_change_ok
= true;
1634 /* It's OK to change the size if either the existing symbol or the
1635 new symbol is weak, or if the old symbol is undefined. */
1638 || h
->root
.type
== bfd_link_hash_undefined
)
1639 *size_change_ok
= true;
1641 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1642 symbol, respectively, appears to be a common symbol in a dynamic
1643 object. If a symbol appears in an uninitialized section, and is
1644 not weak, and is not a function, then it may be a common symbol
1645 which was resolved when the dynamic object was created. We want
1646 to treat such symbols specially, because they raise special
1647 considerations when setting the symbol size: if the symbol
1648 appears as a common symbol in a regular object, and the size in
1649 the regular object is larger, we must make sure that we use the
1650 larger size. This problematic case can always be avoided in C,
1651 but it must be handled correctly when using Fortran shared
1654 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1655 likewise for OLDDYNCOMMON and OLDDEF.
1657 Note that this test is just a heuristic, and that it is quite
1658 possible to have an uninitialized symbol in a shared object which
1659 is really a definition, rather than a common symbol. This could
1660 lead to some minor confusion when the symbol really is a common
1661 symbol in some regular object. However, I think it will be
1667 && (sec
->flags
& SEC_ALLOC
) != 0
1668 && (sec
->flags
& SEC_LOAD
) == 0
1671 newdyncommon
= true;
1673 newdyncommon
= false;
1677 && h
->root
.type
== bfd_link_hash_defined
1679 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1680 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1683 olddyncommon
= true;
1685 olddyncommon
= false;
1687 /* We now know everything about the old and new symbols. We ask the
1688 backend to check if we can merge them. */
1689 if (bed
->merge_symbol
!= NULL
)
1691 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1696 /* There are multiple definitions of a normal symbol. Skip the
1697 default symbol as well as definition from an IR object. */
1698 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1699 && !default_sym
&& h
->def_regular
1701 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1702 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1704 /* Handle a multiple definition. */
1705 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1706 abfd
, sec
, *pvalue
);
1711 /* If both the old and the new symbols look like common symbols in a
1712 dynamic object, set the size of the symbol to the larger of the
1717 && sym
->st_size
!= h
->size
)
1719 /* Since we think we have two common symbols, issue a multiple
1720 common warning if desired. Note that we only warn if the
1721 size is different. If the size is the same, we simply let
1722 the old symbol override the new one as normally happens with
1723 symbols defined in dynamic objects. */
1725 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1726 bfd_link_hash_common
, sym
->st_size
);
1727 if (sym
->st_size
> h
->size
)
1728 h
->size
= sym
->st_size
;
1730 *size_change_ok
= true;
1733 /* If we are looking at a dynamic object, and we have found a
1734 definition, we need to see if the symbol was already defined by
1735 some other object. If so, we want to use the existing
1736 definition, and we do not want to report a multiple symbol
1737 definition error; we do this by clobbering *PSEC to be
1738 bfd_und_section_ptr.
1740 We treat a common symbol as a definition if the symbol in the
1741 shared library is a function, since common symbols always
1742 represent variables; this can cause confusion in principle, but
1743 any such confusion would seem to indicate an erroneous program or
1744 shared library. We also permit a common symbol in a regular
1745 object to override a weak symbol in a shared object. */
1750 || (h
->root
.type
== bfd_link_hash_common
1751 && (newweak
|| newfunc
))))
1755 newdyncommon
= false;
1757 *psec
= sec
= bfd_und_section_ptr
;
1758 *size_change_ok
= true;
1760 /* If we get here when the old symbol is a common symbol, then
1761 we are explicitly letting it override a weak symbol or
1762 function in a dynamic object, and we don't want to warn about
1763 a type change. If the old symbol is a defined symbol, a type
1764 change warning may still be appropriate. */
1766 if (h
->root
.type
== bfd_link_hash_common
)
1767 *type_change_ok
= true;
1770 /* Handle the special case of an old common symbol merging with a
1771 new symbol which looks like a common symbol in a shared object.
1772 We change *PSEC and *PVALUE to make the new symbol look like a
1773 common symbol, and let _bfd_generic_link_add_one_symbol do the
1777 && h
->root
.type
== bfd_link_hash_common
)
1781 newdyncommon
= false;
1782 *pvalue
= sym
->st_size
;
1783 *psec
= sec
= bed
->common_section (oldsec
);
1784 *size_change_ok
= true;
1787 /* Skip weak definitions of symbols that are already defined. */
1788 if (newdef
&& olddef
&& newweak
)
1790 /* Don't skip new non-IR weak syms. */
1791 if (!(oldbfd
!= NULL
1792 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1793 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1799 /* Merge st_other. If the symbol already has a dynamic index,
1800 but visibility says it should not be visible, turn it into a
1802 elf_merge_st_other (abfd
, h
, sym
->st_other
, sec
, newdef
, newdyn
);
1803 if (h
->dynindx
!= -1)
1804 switch (ELF_ST_VISIBILITY (h
->other
))
1808 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1813 /* If the old symbol is from a dynamic object, and the new symbol is
1814 a definition which is not from a dynamic object, then the new
1815 symbol overrides the old symbol. Symbols from regular files
1816 always take precedence over symbols from dynamic objects, even if
1817 they are defined after the dynamic object in the link.
1819 As above, we again permit a common symbol in a regular object to
1820 override a definition in a shared object if the shared object
1821 symbol is a function or is weak. */
1826 || (bfd_is_com_section (sec
)
1827 && (oldweak
|| oldfunc
)))
1832 /* Change the hash table entry to undefined, and let
1833 _bfd_generic_link_add_one_symbol do the right thing with the
1836 h
->root
.type
= bfd_link_hash_undefined
;
1837 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1838 *size_change_ok
= true;
1841 olddyncommon
= false;
1843 /* We again permit a type change when a common symbol may be
1844 overriding a function. */
1846 if (bfd_is_com_section (sec
))
1850 /* If a common symbol overrides a function, make sure
1851 that it isn't defined dynamically nor has type
1854 h
->type
= STT_NOTYPE
;
1856 *type_change_ok
= true;
1859 if (hi
->root
.type
== bfd_link_hash_indirect
)
1862 /* This union may have been set to be non-NULL when this symbol
1863 was seen in a dynamic object. We must force the union to be
1864 NULL, so that it is correct for a regular symbol. */
1865 h
->verinfo
.vertree
= NULL
;
1868 /* Handle the special case of a new common symbol merging with an
1869 old symbol that looks like it might be a common symbol defined in
1870 a shared object. Note that we have already handled the case in
1871 which a new common symbol should simply override the definition
1872 in the shared library. */
1875 && bfd_is_com_section (sec
)
1878 /* It would be best if we could set the hash table entry to a
1879 common symbol, but we don't know what to use for the section
1880 or the alignment. */
1881 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1882 bfd_link_hash_common
, sym
->st_size
);
1884 /* If the presumed common symbol in the dynamic object is
1885 larger, pretend that the new symbol has its size. */
1887 if (h
->size
> *pvalue
)
1890 /* We need to remember the alignment required by the symbol
1891 in the dynamic object. */
1892 BFD_ASSERT (pold_alignment
);
1893 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1896 olddyncommon
= false;
1898 h
->root
.type
= bfd_link_hash_undefined
;
1899 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1901 *size_change_ok
= true;
1902 *type_change_ok
= true;
1904 if (hi
->root
.type
== bfd_link_hash_indirect
)
1907 h
->verinfo
.vertree
= NULL
;
1912 /* Handle the case where we had a versioned symbol in a dynamic
1913 library and now find a definition in a normal object. In this
1914 case, we make the versioned symbol point to the normal one. */
1915 flip
->root
.type
= h
->root
.type
;
1916 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1917 h
->root
.type
= bfd_link_hash_indirect
;
1918 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1919 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1923 flip
->ref_dynamic
= 1;
1930 /* This function is called to create an indirect symbol from the
1931 default for the symbol with the default version if needed. The
1932 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1933 set DYNSYM if the new indirect symbol is dynamic. */
1936 _bfd_elf_add_default_symbol (bfd
*abfd
,
1937 struct bfd_link_info
*info
,
1938 struct elf_link_hash_entry
*h
,
1940 Elf_Internal_Sym
*sym
,
1946 bool type_change_ok
;
1947 bool size_change_ok
;
1950 struct elf_link_hash_entry
*hi
;
1951 struct bfd_link_hash_entry
*bh
;
1952 const struct elf_backend_data
*bed
;
1957 size_t len
, shortlen
;
1961 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1964 /* If this symbol has a version, and it is the default version, we
1965 create an indirect symbol from the default name to the fully
1966 decorated name. This will cause external references which do not
1967 specify a version to be bound to this version of the symbol. */
1968 p
= strchr (name
, ELF_VER_CHR
);
1969 if (h
->versioned
== unknown
)
1973 h
->versioned
= unversioned
;
1978 if (p
[1] != ELF_VER_CHR
)
1980 h
->versioned
= versioned_hidden
;
1984 h
->versioned
= versioned
;
1989 /* PR ld/19073: We may see an unversioned definition after the
1995 bed
= get_elf_backend_data (abfd
);
1996 collect
= bed
->collect
;
1997 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1999 shortlen
= p
- name
;
2000 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
2001 if (shortname
== NULL
)
2003 memcpy (shortname
, name
, shortlen
);
2004 shortname
[shortlen
] = '\0';
2006 /* We are going to create a new symbol. Merge it with any existing
2007 symbol with this name. For the purposes of the merge, act as
2008 though we were defining the symbol we just defined, although we
2009 actually going to define an indirect symbol. */
2010 type_change_ok
= false;
2011 size_change_ok
= false;
2014 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2015 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2016 &type_change_ok
, &size_change_ok
, &matched
))
2022 if (hi
->def_regular
|| ELF_COMMON_DEF_P (hi
))
2024 /* If the undecorated symbol will have a version added by a
2025 script different to H, then don't indirect to/from the
2026 undecorated symbol. This isn't ideal because we may not yet
2027 have seen symbol versions, if given by a script on the
2028 command line rather than via --version-script. */
2029 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2034 = bfd_find_version_for_sym (info
->version_info
,
2035 hi
->root
.root
.string
, &hide
);
2036 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
2038 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
2042 if (hi
->verinfo
.vertree
!= NULL
2043 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
2049 /* Add the default symbol if not performing a relocatable link. */
2050 if (! bfd_link_relocatable (info
))
2053 if (bh
->type
== bfd_link_hash_defined
2054 && bh
->u
.def
.section
->owner
!= NULL
2055 && (bh
->u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
2057 /* Mark the previous definition from IR object as
2058 undefined so that the generic linker will override
2060 bh
->type
= bfd_link_hash_undefined
;
2061 bh
->u
.undef
.abfd
= bh
->u
.def
.section
->owner
;
2063 if (! (_bfd_generic_link_add_one_symbol
2064 (info
, abfd
, shortname
, BSF_INDIRECT
,
2065 bfd_ind_section_ptr
,
2066 0, name
, false, collect
, &bh
)))
2068 hi
= (struct elf_link_hash_entry
*) bh
;
2073 /* In this case the symbol named SHORTNAME is overriding the
2074 indirect symbol we want to add. We were planning on making
2075 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2076 is the name without a version. NAME is the fully versioned
2077 name, and it is the default version.
2079 Overriding means that we already saw a definition for the
2080 symbol SHORTNAME in a regular object, and it is overriding
2081 the symbol defined in the dynamic object.
2083 When this happens, we actually want to change NAME, the
2084 symbol we just added, to refer to SHORTNAME. This will cause
2085 references to NAME in the shared object to become references
2086 to SHORTNAME in the regular object. This is what we expect
2087 when we override a function in a shared object: that the
2088 references in the shared object will be mapped to the
2089 definition in the regular object. */
2091 while (hi
->root
.type
== bfd_link_hash_indirect
2092 || hi
->root
.type
== bfd_link_hash_warning
)
2093 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2095 h
->root
.type
= bfd_link_hash_indirect
;
2096 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
2100 hi
->ref_dynamic
= 1;
2104 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
2109 /* Now set HI to H, so that the following code will set the
2110 other fields correctly. */
2114 /* Check if HI is a warning symbol. */
2115 if (hi
->root
.type
== bfd_link_hash_warning
)
2116 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2118 /* If there is a duplicate definition somewhere, then HI may not
2119 point to an indirect symbol. We will have reported an error to
2120 the user in that case. */
2122 if (hi
->root
.type
== bfd_link_hash_indirect
)
2124 struct elf_link_hash_entry
*ht
;
2126 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2127 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2129 /* If we first saw a reference to SHORTNAME with non-default
2130 visibility, merge that visibility to the @@VER symbol. */
2131 elf_merge_st_other (abfd
, ht
, hi
->other
, sec
, true, dynamic
);
2133 /* A reference to the SHORTNAME symbol from a dynamic library
2134 will be satisfied by the versioned symbol at runtime. In
2135 effect, we have a reference to the versioned symbol. */
2136 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2137 hi
->dynamic_def
|= ht
->dynamic_def
;
2139 /* See if the new flags lead us to realize that the symbol must
2145 if (! bfd_link_executable (info
)
2152 if (hi
->ref_regular
)
2158 /* We also need to define an indirection from the nondefault version
2162 len
= strlen (name
);
2163 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2164 if (shortname
== NULL
)
2166 memcpy (shortname
, name
, shortlen
);
2167 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2169 /* Once again, merge with any existing symbol. */
2170 type_change_ok
= false;
2171 size_change_ok
= false;
2173 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2174 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2175 &type_change_ok
, &size_change_ok
, &matched
))
2181 && h
->root
.type
== bfd_link_hash_defweak
2182 && hi
->root
.type
== bfd_link_hash_defined
)
2184 /* We are handling a weak sym@@ver and attempting to define
2185 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2186 new weak sym@ver because there is already a strong sym@ver.
2187 However, sym@ver and sym@@ver are really the same symbol.
2188 The existing strong sym@ver ought to override sym@@ver. */
2189 h
->root
.type
= bfd_link_hash_defined
;
2190 h
->root
.u
.def
.section
= hi
->root
.u
.def
.section
;
2191 h
->root
.u
.def
.value
= hi
->root
.u
.def
.value
;
2192 hi
->root
.type
= bfd_link_hash_indirect
;
2193 hi
->root
.u
.i
.link
= &h
->root
;
2200 /* Here SHORTNAME is a versioned name, so we don't expect to see
2201 the type of override we do in the case above unless it is
2202 overridden by a versioned definition. */
2203 if (hi
->root
.type
!= bfd_link_hash_defined
2204 && hi
->root
.type
!= bfd_link_hash_defweak
)
2206 /* xgettext:c-format */
2207 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2214 if (! (_bfd_generic_link_add_one_symbol
2215 (info
, abfd
, shortname
, BSF_INDIRECT
,
2216 bfd_ind_section_ptr
, 0, name
, false, collect
, &bh
)))
2218 hi
= (struct elf_link_hash_entry
*) bh
;
2221 /* If there is a duplicate definition somewhere, then HI may not
2222 point to an indirect symbol. We will have reported an error
2223 to the user in that case. */
2224 if (hi
->root
.type
== bfd_link_hash_indirect
)
2226 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2227 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2228 hi
->dynamic_def
|= h
->dynamic_def
;
2230 /* If we first saw a reference to @VER symbol with
2231 non-default visibility, merge that visibility to the
2233 elf_merge_st_other (abfd
, h
, hi
->other
, sec
, true, dynamic
);
2235 /* See if the new flags lead us to realize that the symbol
2241 if (! bfd_link_executable (info
)
2247 if (hi
->ref_regular
)
2256 /* This routine is used to export all defined symbols into the dynamic
2257 symbol table. It is called via elf_link_hash_traverse. */
2260 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2262 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2264 /* Ignore indirect symbols. These are added by the versioning code. */
2265 if (h
->root
.type
== bfd_link_hash_indirect
)
2268 /* Ignore this if we won't export it. */
2269 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2272 if (h
->dynindx
== -1
2273 && (h
->def_regular
|| h
->ref_regular
)
2274 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2275 h
->root
.root
.string
))
2277 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2287 /* Return the glibc version reference if VERSION_DEP is added to the
2288 list of glibc version dependencies successfully. VERSION_DEP will
2289 be put into the .gnu.version_r section. GLIBC_MINOR_BASE is the
2290 pointer to the glibc minor base version. */
2292 static Elf_Internal_Verneed
*
2293 elf_link_add_glibc_verneed (struct elf_find_verdep_info
*rinfo
,
2294 Elf_Internal_Verneed
*glibc_verref
,
2295 const char *version_dep
,
2296 int *glibc_minor_base
)
2298 Elf_Internal_Verneed
*t
;
2299 Elf_Internal_Vernaux
*a
;
2301 int minor_version
= -1;
2303 if (glibc_verref
!= NULL
)
2307 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2309 /* Return if VERSION_DEP dependency has been added. */
2310 if (a
->vna_nodename
== version_dep
2311 || strcmp (a
->vna_nodename
, version_dep
) == 0)
2317 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2321 const char *soname
= bfd_elf_get_dt_soname (t
->vn_bfd
);
2322 if (soname
!= NULL
&& startswith (soname
, "libc.so."))
2326 /* Skip the shared library if it isn't libc.so. */
2330 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2332 /* Return if VERSION_DEP dependency has been added. */
2333 if (a
->vna_nodename
== version_dep
2334 || strcmp (a
->vna_nodename
, version_dep
) == 0)
2337 /* Check if libc.so provides GLIBC_2.XX version. */
2338 if (startswith (a
->vna_nodename
, "GLIBC_2."))
2340 minor_version
= strtol (a
->vna_nodename
+ 8, NULL
, 10);
2341 if (minor_version
< *glibc_minor_base
)
2342 *glibc_minor_base
= minor_version
;
2346 /* Skip if it isn't linked against glibc. */
2347 if (minor_version
< 0)
2351 /* Skip if 2.GLIBC_MINOR_BASE includes VERSION_DEP. */
2352 if (startswith (version_dep
, "GLIBC_2."))
2354 minor_version
= strtol (version_dep
+ 8, NULL
, 10);
2355 if (minor_version
<= *glibc_minor_base
)
2360 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2363 rinfo
->failed
= true;
2367 a
->vna_nodename
= version_dep
;
2369 a
->vna_nextptr
= t
->vn_auxptr
;
2370 a
->vna_other
= rinfo
->vers
+ 1;
2378 /* Add VERSION_DEP to the list of version dependencies when linked
2382 _bfd_elf_link_add_glibc_version_dependency
2383 (struct elf_find_verdep_info
*rinfo
,
2384 const char *version_dep
[])
2386 Elf_Internal_Verneed
*t
= NULL
;
2387 int glibc_minor_base
= INT_MAX
;
2391 t
= elf_link_add_glibc_verneed (rinfo
, t
, *version_dep
,
2393 /* Return if there is no glibc version reference. */
2398 while (*version_dep
!= NULL
);
2401 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2402 linked against glibc. */
2405 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info
*rinfo
)
2407 if (rinfo
->info
->enable_dt_relr
)
2409 const char *version
[] =
2411 "GLIBC_ABI_DT_RELR",
2414 _bfd_elf_link_add_glibc_version_dependency (rinfo
, version
);
2418 /* Look through the symbols which are defined in other shared
2419 libraries and referenced here. Update the list of version
2420 dependencies. This will be put into the .gnu.version_r section.
2421 This function is called via elf_link_hash_traverse. */
2424 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2427 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2428 Elf_Internal_Verneed
*t
;
2429 Elf_Internal_Vernaux
*a
;
2432 /* We only care about symbols defined in shared objects with version
2437 || h
->verinfo
.verdef
== NULL
2438 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2439 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2442 /* See if we already know about this version. */
2443 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2447 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2450 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2451 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2457 /* This is a new version. Add it to tree we are building. */
2462 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2465 rinfo
->failed
= true;
2469 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2470 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2471 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2475 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2478 rinfo
->failed
= true;
2482 /* Note that we are copying a string pointer here, and testing it
2483 above. If bfd_elf_string_from_elf_section is ever changed to
2484 discard the string data when low in memory, this will have to be
2486 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2488 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2489 a
->vna_nextptr
= t
->vn_auxptr
;
2491 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2494 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2501 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2502 hidden. Set *T_P to NULL if there is no match. */
2505 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2506 struct elf_link_hash_entry
*h
,
2507 const char *version_p
,
2508 struct bfd_elf_version_tree
**t_p
,
2511 struct bfd_elf_version_tree
*t
;
2513 /* Look for the version. If we find it, it is no longer weak. */
2514 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2516 if (strcmp (t
->name
, version_p
) == 0)
2520 struct bfd_elf_version_expr
*d
;
2522 len
= version_p
- h
->root
.root
.string
;
2523 alc
= (char *) bfd_malloc (len
);
2526 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2527 alc
[len
- 1] = '\0';
2528 if (alc
[len
- 2] == ELF_VER_CHR
)
2529 alc
[len
- 2] = '\0';
2531 h
->verinfo
.vertree
= t
;
2535 if (t
->globals
.list
!= NULL
)
2536 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2538 /* See if there is anything to force this symbol to
2540 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2542 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2545 && ! info
->export_dynamic
)
2559 /* Return TRUE if the symbol H is hidden by version script. */
2562 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2563 struct elf_link_hash_entry
*h
)
2567 const struct elf_backend_data
*bed
2568 = get_elf_backend_data (info
->output_bfd
);
2570 /* Version script only hides symbols defined in regular objects. */
2571 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2574 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2575 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2577 struct bfd_elf_version_tree
*t
;
2580 if (*p
== ELF_VER_CHR
)
2584 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2588 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2593 /* If we don't have a version for this symbol, see if we can find
2595 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2598 = bfd_find_version_for_sym (info
->version_info
,
2599 h
->root
.root
.string
, &hide
);
2600 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2602 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2610 /* Figure out appropriate versions for all the symbols. We may not
2611 have the version number script until we have read all of the input
2612 files, so until that point we don't know which symbols should be
2613 local. This function is called via elf_link_hash_traverse. */
2616 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2618 struct elf_info_failed
*sinfo
;
2619 struct bfd_link_info
*info
;
2620 const struct elf_backend_data
*bed
;
2621 struct elf_info_failed eif
;
2625 sinfo
= (struct elf_info_failed
*) data
;
2628 /* Fix the symbol flags. */
2631 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2634 sinfo
->failed
= true;
2638 bed
= get_elf_backend_data (info
->output_bfd
);
2640 /* We only need version numbers for symbols defined in regular
2642 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2644 /* Hide symbols defined in discarded input sections. */
2645 if ((h
->root
.type
== bfd_link_hash_defined
2646 || h
->root
.type
== bfd_link_hash_defweak
)
2647 && discarded_section (h
->root
.u
.def
.section
))
2648 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2653 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2654 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2656 struct bfd_elf_version_tree
*t
;
2659 if (*p
== ELF_VER_CHR
)
2662 /* If there is no version string, we can just return out. */
2666 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2668 sinfo
->failed
= true;
2673 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2675 /* If we are building an application, we need to create a
2676 version node for this version. */
2677 if (t
== NULL
&& bfd_link_executable (info
))
2679 struct bfd_elf_version_tree
**pp
;
2682 /* If we aren't going to export this symbol, we don't need
2683 to worry about it. */
2684 if (h
->dynindx
== -1)
2687 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2691 sinfo
->failed
= true;
2696 t
->name_indx
= (unsigned int) -1;
2700 /* Don't count anonymous version tag. */
2701 if (sinfo
->info
->version_info
!= NULL
2702 && sinfo
->info
->version_info
->vernum
== 0)
2704 for (pp
= &sinfo
->info
->version_info
;
2708 t
->vernum
= version_index
;
2712 h
->verinfo
.vertree
= t
;
2716 /* We could not find the version for a symbol when
2717 generating a shared archive. Return an error. */
2719 /* xgettext:c-format */
2720 (_("%pB: version node not found for symbol %s"),
2721 info
->output_bfd
, h
->root
.root
.string
);
2722 bfd_set_error (bfd_error_bad_value
);
2723 sinfo
->failed
= true;
2728 /* If we don't have a version for this symbol, see if we can find
2731 && h
->verinfo
.vertree
== NULL
2732 && sinfo
->info
->version_info
!= NULL
)
2735 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2736 h
->root
.root
.string
, &hide
);
2737 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2738 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2744 /* Read and swap the relocs from the section indicated by SHDR. This
2745 may be either a REL or a RELA section. The relocations are
2746 translated into RELA relocations and stored in INTERNAL_RELOCS,
2747 which should have already been allocated to contain enough space.
2748 The *EXTERNAL_RELOCS_P are a buffer where the external form of the
2749 relocations should be stored. If *EXTERNAL_RELOCS_ADDR is NULL,
2750 *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
2751 memory address and size. Otherwise, *EXTERNAL_RELOCS_ADDR is
2752 unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
2754 Returns FALSE if something goes wrong. */
2757 elf_link_read_relocs_from_section (bfd
*abfd
,
2758 const asection
*sec
,
2759 Elf_Internal_Shdr
*shdr
,
2760 void **external_relocs_addr
,
2761 size_t *external_relocs_size
,
2762 Elf_Internal_Rela
*internal_relocs
)
2764 const struct elf_backend_data
*bed
;
2765 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2766 const bfd_byte
*erela
;
2767 const bfd_byte
*erelaend
;
2768 Elf_Internal_Rela
*irela
;
2769 Elf_Internal_Shdr
*symtab_hdr
;
2771 void *external_relocs
= *external_relocs_addr
;
2773 /* Position ourselves at the start of the section. */
2774 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2777 /* Read the relocations. */
2778 *external_relocs_size
= shdr
->sh_size
;
2779 if (!_bfd_mmap_read_temporary (&external_relocs
,
2780 external_relocs_size
,
2781 external_relocs_addr
, abfd
, true))
2784 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2785 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2787 bed
= get_elf_backend_data (abfd
);
2789 /* Convert the external relocations to the internal format. */
2790 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2791 swap_in
= bed
->s
->swap_reloc_in
;
2792 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2793 swap_in
= bed
->s
->swap_reloca_in
;
2796 bfd_set_error (bfd_error_wrong_format
);
2800 erela
= (const bfd_byte
*) external_relocs
;
2801 /* Setting erelaend like this and comparing with <= handles case of
2802 a fuzzed object with sh_size not a multiple of sh_entsize. */
2803 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2804 irela
= internal_relocs
;
2805 while (erela
<= erelaend
)
2809 (*swap_in
) (abfd
, erela
, irela
);
2810 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2811 if (bed
->s
->arch_size
== 64)
2815 if ((size_t) r_symndx
>= nsyms
)
2818 /* xgettext:c-format */
2819 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2820 " for offset %#" PRIx64
" in section `%pA'"),
2821 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2822 (uint64_t) irela
->r_offset
, sec
);
2823 bfd_set_error (bfd_error_bad_value
);
2827 else if (r_symndx
!= STN_UNDEF
)
2830 /* xgettext:c-format */
2831 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2832 " for offset %#" PRIx64
" in section `%pA'"
2833 " when the object file has no symbol table"),
2834 abfd
, (uint64_t) r_symndx
,
2835 (uint64_t) irela
->r_offset
, sec
);
2836 bfd_set_error (bfd_error_bad_value
);
2839 irela
+= bed
->s
->int_rels_per_ext_rel
;
2840 erela
+= shdr
->sh_entsize
;
2846 /* Read and swap the relocs for a section O. They may have been
2847 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2848 not NULL, they are used as buffers to read into. They are known to
2849 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2850 the return value is allocated using either malloc or bfd_alloc,
2851 according to the KEEP_MEMORY argument. If O has two relocation
2852 sections (both REL and RELA relocations), then the REL_HDR
2853 relocations will appear first in INTERNAL_RELOCS, followed by the
2854 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2855 update cache_size. */
2858 _bfd_elf_link_info_read_relocs (bfd
*abfd
,
2859 struct bfd_link_info
*info
,
2861 void *external_relocs
,
2862 Elf_Internal_Rela
*internal_relocs
,
2865 void *alloc1
= NULL
;
2867 Elf_Internal_Rela
*alloc2
= NULL
;
2868 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2869 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2870 Elf_Internal_Rela
*internal_rela_relocs
;
2872 if (esdo
->relocs
!= NULL
)
2873 return esdo
->relocs
;
2875 if (o
->reloc_count
== 0)
2878 if (internal_relocs
== NULL
)
2882 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2883 if (keep_memory
&& info
)
2884 info
->cache_size
+= size
;
2885 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2886 if (internal_relocs
== NULL
)
2890 alloc1
= external_relocs
;
2891 internal_rela_relocs
= internal_relocs
;
2894 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2895 &alloc1
, &alloc1_size
,
2898 external_relocs
= (((bfd_byte
*) external_relocs
)
2899 + esdo
->rel
.hdr
->sh_size
);
2900 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2901 * bed
->s
->int_rels_per_ext_rel
);
2905 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2906 &alloc1
, &alloc1_size
,
2907 internal_rela_relocs
)))
2910 /* Cache the results for next time, if we can. */
2912 esdo
->relocs
= internal_relocs
;
2914 _bfd_munmap_temporary (alloc1
, alloc1_size
);
2916 /* Don't free alloc2, since if it was allocated we are passing it
2917 back (under the name of internal_relocs). */
2919 return internal_relocs
;
2922 _bfd_munmap_temporary (alloc1
, alloc1_size
);
2927 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2928 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2929 struct bfd_link_info. */
2932 _bfd_elf_link_read_relocs (bfd
*abfd
,
2934 void *external_relocs
,
2935 Elf_Internal_Rela
*internal_relocs
,
2938 return _bfd_elf_link_info_read_relocs (abfd
, NULL
, o
, external_relocs
,
2939 internal_relocs
, keep_memory
);
2943 /* Compute the size of, and allocate space for, REL_HDR which is the
2944 section header for a section containing relocations for O. */
2947 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2948 struct bfd_elf_section_reloc_data
*reldata
)
2950 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2952 /* That allows us to calculate the size of the section. */
2953 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2955 /* The contents field must last into write_object_contents, so we
2956 allocate it with bfd_alloc rather than malloc. Also since we
2957 cannot be sure that the contents will actually be filled in,
2958 we zero the allocated space. */
2959 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2960 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2963 if (reldata
->hashes
== NULL
&& reldata
->count
)
2965 struct elf_link_hash_entry
**p
;
2967 p
= ((struct elf_link_hash_entry
**)
2968 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2972 reldata
->hashes
= p
;
2978 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2979 originated from the section given by INPUT_REL_HDR) to the
2983 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2984 asection
*input_section
,
2985 Elf_Internal_Shdr
*input_rel_hdr
,
2986 Elf_Internal_Rela
*internal_relocs
,
2987 struct elf_link_hash_entry
**rel_hash
)
2989 Elf_Internal_Rela
*irela
;
2990 Elf_Internal_Rela
*irelaend
;
2992 struct bfd_elf_section_reloc_data
*output_reldata
;
2993 asection
*output_section
;
2994 const struct elf_backend_data
*bed
;
2995 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2996 struct bfd_elf_section_data
*esdo
;
2998 output_section
= input_section
->output_section
;
3000 bed
= get_elf_backend_data (output_bfd
);
3001 esdo
= elf_section_data (output_section
);
3002 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
3004 output_reldata
= &esdo
->rel
;
3005 swap_out
= bed
->s
->swap_reloc_out
;
3007 else if (esdo
->rela
.hdr
3008 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
3010 output_reldata
= &esdo
->rela
;
3011 swap_out
= bed
->s
->swap_reloca_out
;
3016 /* xgettext:c-format */
3017 (_("%pB: relocation size mismatch in %pB section %pA"),
3018 output_bfd
, input_section
->owner
, input_section
);
3019 bfd_set_error (bfd_error_wrong_format
);
3023 erel
= output_reldata
->hdr
->contents
;
3024 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
3025 irela
= internal_relocs
;
3026 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
3027 * bed
->s
->int_rels_per_ext_rel
);
3028 while (irela
< irelaend
)
3030 if (rel_hash
&& *rel_hash
)
3031 (*rel_hash
)->has_reloc
= 1;
3032 (*swap_out
) (output_bfd
, irela
, erel
);
3033 irela
+= bed
->s
->int_rels_per_ext_rel
;
3034 erel
+= input_rel_hdr
->sh_entsize
;
3039 /* Bump the counter, so that we know where to add the next set of
3041 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
3046 /* Make weak undefined symbols in PIE dynamic. */
3049 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
3050 struct elf_link_hash_entry
*h
)
3052 if (bfd_link_pie (info
)
3054 && h
->root
.type
== bfd_link_hash_undefweak
)
3055 return bfd_elf_link_record_dynamic_symbol (info
, h
);
3060 /* Fix up the flags for a symbol. This handles various cases which
3061 can only be fixed after all the input files are seen. This is
3062 currently called by both adjust_dynamic_symbol and
3063 assign_sym_version, which is unnecessary but perhaps more robust in
3064 the face of future changes. */
3067 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
3068 struct elf_info_failed
*eif
)
3070 const struct elf_backend_data
*bed
;
3072 /* If this symbol was mentioned in a non-ELF file, try to set
3073 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3074 permit a non-ELF file to correctly refer to a symbol defined in
3075 an ELF dynamic object. */
3078 while (h
->root
.type
== bfd_link_hash_indirect
)
3079 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3081 if (h
->root
.type
!= bfd_link_hash_defined
3082 && h
->root
.type
!= bfd_link_hash_defweak
)
3085 h
->ref_regular_nonweak
= 1;
3089 if (h
->root
.u
.def
.section
->owner
!= NULL
3090 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3091 == bfd_target_elf_flavour
))
3094 h
->ref_regular_nonweak
= 1;
3100 if (h
->dynindx
== -1
3104 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3113 /* Unfortunately, NON_ELF is only correct if the symbol
3114 was first seen in a non-ELF file. Fortunately, if the symbol
3115 was first seen in an ELF file, we're probably OK unless the
3116 symbol was defined in a non-ELF file. Catch that case here.
3117 FIXME: We're still in trouble if the symbol was first seen in
3118 a dynamic object, and then later in a non-ELF regular object. */
3119 if ((h
->root
.type
== bfd_link_hash_defined
3120 || h
->root
.type
== bfd_link_hash_defweak
)
3122 && (h
->root
.u
.def
.section
->owner
!= NULL
3123 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3124 != bfd_target_elf_flavour
)
3125 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3126 && !h
->def_dynamic
)))
3130 /* Backend specific symbol fixup. */
3131 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3132 if (bed
->elf_backend_fixup_symbol
3133 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
3136 /* If this is a final link, and the symbol was defined as a common
3137 symbol in a regular object file, and there was no definition in
3138 any dynamic object, then the linker will have allocated space for
3139 the symbol in a common section but the DEF_REGULAR
3140 flag will not have been set. */
3141 if (h
->root
.type
== bfd_link_hash_defined
3145 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
3148 /* Symbols defined in discarded sections shouldn't be dynamic. */
3149 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
3150 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3152 /* If a weak undefined symbol has non-default visibility, we also
3153 hide it from the dynamic linker. */
3154 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
3155 && h
->root
.type
== bfd_link_hash_undefweak
)
3156 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3158 /* A hidden versioned symbol in executable should be forced local if
3159 it is is locally defined, not referenced by shared library and not
3161 else if (bfd_link_executable (eif
->info
)
3162 && h
->versioned
== versioned_hidden
3163 && !eif
->info
->export_dynamic
3167 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3169 /* If -Bsymbolic was used (which means to bind references to global
3170 symbols to the definition within the shared object), and this
3171 symbol was defined in a regular object, then it actually doesn't
3172 need a PLT entry. Likewise, if the symbol has non-default
3173 visibility. If the symbol has hidden or internal visibility, we
3174 will force it local. */
3175 else if (h
->needs_plt
3176 && bfd_link_pic (eif
->info
)
3177 && is_elf_hash_table (eif
->info
->hash
)
3178 && (SYMBOLIC_BIND (eif
->info
, h
)
3179 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
3184 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3185 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
3186 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3189 /* If this is a weak defined symbol in a dynamic object, and we know
3190 the real definition in the dynamic object, copy interesting flags
3191 over to the real definition. */
3192 if (h
->is_weakalias
)
3194 struct elf_link_hash_entry
*def
= weakdef (h
);
3196 /* If the real definition is defined by a regular object file,
3197 don't do anything special. See the longer description in
3198 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3199 bfd_link_hash_defined as it was when put on the alias list
3200 then it must have originally been a versioned symbol (for
3201 which a non-versioned indirect symbol is created) and later
3202 a definition for the non-versioned symbol is found. In that
3203 case the indirection is flipped with the versioned symbol
3204 becoming an indirect pointing at the non-versioned symbol.
3205 Thus, not an alias any more. */
3206 if (def
->def_regular
3207 || def
->root
.type
!= bfd_link_hash_defined
)
3210 while ((h
= h
->u
.alias
) != def
)
3211 h
->is_weakalias
= 0;
3215 while (h
->root
.type
== bfd_link_hash_indirect
)
3216 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3217 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3218 || h
->root
.type
== bfd_link_hash_defweak
);
3219 BFD_ASSERT (def
->def_dynamic
);
3220 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
3227 /* Make the backend pick a good value for a dynamic symbol. This is
3228 called via elf_link_hash_traverse, and also calls itself
3232 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
3234 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3235 struct elf_link_hash_table
*htab
;
3236 const struct elf_backend_data
*bed
;
3238 if (! is_elf_hash_table (eif
->info
->hash
))
3241 /* Ignore indirect symbols. These are added by the versioning code. */
3242 if (h
->root
.type
== bfd_link_hash_indirect
)
3245 /* Fix the symbol flags. */
3246 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
3249 htab
= elf_hash_table (eif
->info
);
3250 bed
= get_elf_backend_data (htab
->dynobj
);
3252 if (h
->root
.type
== bfd_link_hash_undefweak
)
3254 if (eif
->info
->dynamic_undefined_weak
== 0)
3255 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3256 else if (eif
->info
->dynamic_undefined_weak
> 0
3258 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3259 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
3260 h
->root
.root
.string
))
3262 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3270 /* If this symbol does not require a PLT entry, and it is not
3271 defined by a dynamic object, or is not referenced by a regular
3272 object, ignore it. We do have to handle a weak defined symbol,
3273 even if no regular object refers to it, if we decided to add it
3274 to the dynamic symbol table. FIXME: Do we normally need to worry
3275 about symbols which are defined by one dynamic object and
3276 referenced by another one? */
3278 && h
->type
!= STT_GNU_IFUNC
3282 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3284 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3288 /* If we've already adjusted this symbol, don't do it again. This
3289 can happen via a recursive call. */
3290 if (h
->dynamic_adjusted
)
3293 /* Don't look at this symbol again. Note that we must set this
3294 after checking the above conditions, because we may look at a
3295 symbol once, decide not to do anything, and then get called
3296 recursively later after REF_REGULAR is set below. */
3297 h
->dynamic_adjusted
= 1;
3299 /* If this is a weak definition, and we know a real definition, and
3300 the real symbol is not itself defined by a regular object file,
3301 then get a good value for the real definition. We handle the
3302 real symbol first, for the convenience of the backend routine.
3304 Note that there is a confusing case here. If the real definition
3305 is defined by a regular object file, we don't get the real symbol
3306 from the dynamic object, but we do get the weak symbol. If the
3307 processor backend uses a COPY reloc, then if some routine in the
3308 dynamic object changes the real symbol, we will not see that
3309 change in the corresponding weak symbol. This is the way other
3310 ELF linkers work as well, and seems to be a result of the shared
3313 I will clarify this issue. Most SVR4 shared libraries define the
3314 variable _timezone and define timezone as a weak synonym. The
3315 tzset call changes _timezone. If you write
3316 extern int timezone;
3318 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3319 you might expect that, since timezone is a synonym for _timezone,
3320 the same number will print both times. However, if the processor
3321 backend uses a COPY reloc, then actually timezone will be copied
3322 into your process image, and, since you define _timezone
3323 yourself, _timezone will not. Thus timezone and _timezone will
3324 wind up at different memory locations. The tzset call will set
3325 _timezone, leaving timezone unchanged. */
3327 if (h
->is_weakalias
)
3329 struct elf_link_hash_entry
*def
= weakdef (h
);
3331 /* If we get to this point, there is an implicit reference to
3332 the alias by a regular object file via the weak symbol H. */
3333 def
->ref_regular
= 1;
3335 /* Ensure that the backend adjust_dynamic_symbol function sees
3336 the strong alias before H by recursively calling ourselves. */
3337 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3341 /* If a symbol has no type and no size and does not require a PLT
3342 entry, then we are probably about to do the wrong thing here: we
3343 are probably going to create a COPY reloc for an empty object.
3344 This case can arise when a shared object is built with assembly
3345 code, and the assembly code fails to set the symbol type. */
3347 && h
->type
== STT_NOTYPE
3350 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3351 h
->root
.root
.string
);
3353 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3362 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3366 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3367 struct elf_link_hash_entry
*h
,
3370 unsigned int power_of_two
;
3372 asection
*sec
= h
->root
.u
.def
.section
;
3374 /* The section alignment of the definition is the maximum alignment
3375 requirement of symbols defined in the section. Since we don't
3376 know the symbol alignment requirement, we start with the
3377 maximum alignment and check low bits of the symbol address
3378 for the minimum alignment. */
3379 power_of_two
= bfd_section_alignment (sec
);
3380 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3381 while ((h
->root
.u
.def
.value
& mask
) != 0)
3387 /* Adjust the section alignment if needed. */
3388 if (!bfd_link_align_section (dynbss
, power_of_two
))
3391 /* We make sure that the symbol will be aligned properly. */
3392 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3394 /* Define the symbol as being at this point in DYNBSS. */
3395 h
->root
.u
.def
.section
= dynbss
;
3396 h
->root
.u
.def
.value
= dynbss
->size
;
3398 /* Increment the size of DYNBSS to make room for the symbol. */
3399 dynbss
->size
+= h
->size
;
3401 /* No error if extern_protected_data is true. */
3402 if (h
->protected_def
3403 && (!info
->extern_protected_data
3404 || (info
->extern_protected_data
< 0
3405 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3406 info
->callbacks
->einfo
3407 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3408 h
->root
.root
.string
);
3413 /* Adjust all external symbols pointing into SEC_MERGE sections
3414 to reflect the object merging within the sections. */
3417 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3421 if ((h
->root
.type
== bfd_link_hash_defined
3422 || h
->root
.type
== bfd_link_hash_defweak
)
3423 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3424 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3426 bfd
*output_bfd
= (bfd
*) data
;
3428 h
->root
.u
.def
.value
=
3429 _bfd_merged_section_offset (output_bfd
,
3430 &h
->root
.u
.def
.section
,
3431 elf_section_data (sec
)->sec_info
,
3432 h
->root
.u
.def
.value
);
3438 /* Returns false if the symbol referred to by H should be considered
3439 to resolve local to the current module, and true if it should be
3440 considered to bind dynamically. */
3443 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3444 struct bfd_link_info
*info
,
3445 bool not_local_protected
)
3447 bool binding_stays_local_p
;
3448 const struct elf_backend_data
*bed
;
3449 struct elf_link_hash_table
*hash_table
;
3454 while (h
->root
.type
== bfd_link_hash_indirect
3455 || h
->root
.type
== bfd_link_hash_warning
)
3456 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3458 /* If it was forced local, then clearly it's not dynamic. */
3459 if (h
->dynindx
== -1)
3461 if (h
->forced_local
)
3464 /* Identify the cases where name binding rules say that a
3465 visible symbol resolves locally. */
3466 binding_stays_local_p
= (bfd_link_executable (info
)
3467 || SYMBOLIC_BIND (info
, h
));
3469 switch (ELF_ST_VISIBILITY (h
->other
))
3476 hash_table
= elf_hash_table (info
);
3477 if (!is_elf_hash_table (&hash_table
->root
))
3480 bed
= get_elf_backend_data (hash_table
->dynobj
);
3482 /* Proper resolution for function pointer equality may require
3483 that these symbols perhaps be resolved dynamically, even though
3484 we should be resolving them to the current module. */
3485 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3486 binding_stays_local_p
= true;
3493 /* If it isn't defined locally, then clearly it's dynamic. */
3494 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3497 /* Otherwise, the symbol is dynamic if binding rules don't tell
3498 us that it remains local. */
3499 return !binding_stays_local_p
;
3502 /* Return true if the symbol referred to by H should be considered
3503 to resolve local to the current module, and false otherwise. Differs
3504 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3505 undefined symbols. The two functions are virtually identical except
3506 for the place where dynindx == -1 is tested. If that test is true,
3507 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3508 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3510 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3511 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3512 treatment of undefined weak symbols. For those that do not make
3513 undefined weak symbols dynamic, both functions may return false. */
3516 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3517 struct bfd_link_info
*info
,
3518 bool local_protected
)
3520 const struct elf_backend_data
*bed
;
3521 struct elf_link_hash_table
*hash_table
;
3523 /* If it's a local sym, of course we resolve locally. */
3527 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3528 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3529 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3532 /* Forced local symbols resolve locally. */
3533 if (h
->forced_local
)
3536 /* Common symbols that become definitions don't get the DEF_REGULAR
3537 flag set, so test it first, and don't bail out. */
3538 if (ELF_COMMON_DEF_P (h
))
3540 /* If we don't have a definition in a regular file, then we can't
3541 resolve locally. The sym is either undefined or dynamic. */
3542 else if (!h
->def_regular
)
3545 /* Non-dynamic symbols resolve locally. */
3546 if (h
->dynindx
== -1)
3549 /* At this point, we know the symbol is defined and dynamic. In an
3550 executable it must resolve locally, likewise when building symbolic
3551 shared libraries. */
3552 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3555 /* Now deal with defined dynamic symbols in shared libraries. Ones
3556 with default visibility might not resolve locally. */
3557 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3560 hash_table
= elf_hash_table (info
);
3561 if (!is_elf_hash_table (&hash_table
->root
))
3564 /* STV_PROTECTED symbols with indirect external access are local. */
3565 if (info
->indirect_extern_access
> 0)
3568 bed
= get_elf_backend_data (hash_table
->dynobj
);
3570 /* If extern_protected_data is false, STV_PROTECTED non-function
3571 symbols are local. */
3572 if ((!info
->extern_protected_data
3573 || (info
->extern_protected_data
< 0
3574 && !bed
->extern_protected_data
))
3575 && !bed
->is_function_type (h
->type
))
3578 /* Function pointer equality tests may require that STV_PROTECTED
3579 symbols be treated as dynamic symbols. If the address of a
3580 function not defined in an executable is set to that function's
3581 plt entry in the executable, then the address of the function in
3582 a shared library must also be the plt entry in the executable. */
3583 return local_protected
;
3586 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3587 aligned. Returns the first TLS output section. */
3589 struct bfd_section
*
3590 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3592 struct bfd_section
*sec
, *tls
;
3593 unsigned int align
= 0;
3595 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3596 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3600 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3601 if (sec
->alignment_power
> align
)
3602 align
= sec
->alignment_power
;
3604 elf_hash_table (info
)->tls_sec
= tls
;
3606 /* Ensure the alignment of the first section (usually .tdata) is the largest
3607 alignment, so that the tls segment starts aligned. */
3609 (void) bfd_link_align_section (tls
, align
);
3614 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3616 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3617 Elf_Internal_Sym
*sym
)
3619 const struct elf_backend_data
*bed
;
3621 /* Local symbols do not count, but target specific ones might. */
3622 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3623 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3626 bed
= get_elf_backend_data (abfd
);
3627 /* Function symbols do not count. */
3628 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3631 /* If the section is undefined, then so is the symbol. */
3632 if (sym
->st_shndx
== SHN_UNDEF
)
3635 /* If the symbol is defined in the common section, then
3636 it is a common definition and so does not count. */
3637 if (bed
->common_definition (sym
))
3640 /* If the symbol is in a target specific section then we
3641 must rely upon the backend to tell us what it is. */
3642 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3643 /* FIXME - this function is not coded yet:
3645 return _bfd_is_global_symbol_definition (abfd, sym);
3647 Instead for now assume that the definition is not global,
3648 Even if this is wrong, at least the linker will behave
3649 in the same way that it used to do. */
3655 /* Search the symbol table of the archive element of the archive ABFD
3656 whose archive map contains a mention of SYMDEF, and determine if
3657 the symbol is defined in this element. */
3659 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3661 Elf_Internal_Shdr
* hdr
;
3665 Elf_Internal_Sym
*isymbuf
;
3666 Elf_Internal_Sym
*isym
;
3667 Elf_Internal_Sym
*isymend
;
3670 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
, NULL
);
3674 if (! bfd_check_format (abfd
, bfd_object
))
3677 /* Select the appropriate symbol table. If we don't know if the
3678 object file is an IR object, give linker LTO plugin a chance to
3679 get the correct symbol table. */
3680 if (abfd
->plugin_format
== bfd_plugin_yes
3681 || abfd
->plugin_format
== bfd_plugin_yes_unused
3682 #if BFD_SUPPORTS_PLUGINS
3683 || (abfd
->plugin_format
== bfd_plugin_unknown
3684 && bfd_link_plugin_object_p (abfd
))
3688 /* Use the IR symbol table if the object has been claimed by
3690 abfd
= abfd
->plugin_dummy_bfd
;
3691 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3695 if (elf_use_dt_symtab_p (abfd
))
3697 bfd_set_error (bfd_error_wrong_format
);
3701 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3702 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3704 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3707 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3709 /* The sh_info field of the symtab header tells us where the
3710 external symbols start. We don't care about the local symbols. */
3711 if (elf_bad_symtab (abfd
))
3713 extsymcount
= symcount
;
3718 extsymcount
= symcount
- hdr
->sh_info
;
3719 extsymoff
= hdr
->sh_info
;
3722 if (extsymcount
== 0)
3725 /* Read in the symbol table. */
3726 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3728 if (isymbuf
== NULL
)
3731 /* Scan the symbol table looking for SYMDEF. */
3733 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3737 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3742 if (strcmp (name
, symdef
->name
) == 0)
3744 result
= is_global_data_symbol_definition (abfd
, isym
);
3754 /* Add an entry to the .dynamic table. */
3757 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3761 struct elf_link_hash_table
*hash_table
;
3762 const struct elf_backend_data
*bed
;
3764 bfd_size_type newsize
;
3765 bfd_byte
*newcontents
;
3766 Elf_Internal_Dyn dyn
;
3768 hash_table
= elf_hash_table (info
);
3769 if (! is_elf_hash_table (&hash_table
->root
))
3772 if (tag
== DT_RELA
|| tag
== DT_REL
)
3773 hash_table
->dynamic_relocs
= true;
3775 bed
= get_elf_backend_data (hash_table
->dynobj
);
3776 s
= hash_table
->dynamic
;
3777 BFD_ASSERT (s
!= NULL
);
3779 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3780 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3781 if (newcontents
== NULL
)
3785 dyn
.d_un
.d_val
= val
;
3786 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3789 s
->contents
= newcontents
;
3794 /* Strip zero-sized dynamic sections. */
3797 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3799 struct elf_link_hash_table
*hash_table
;
3800 const struct elf_backend_data
*bed
;
3801 asection
*s
, *sdynamic
, **pp
;
3802 asection
*rela_dyn
, *rel_dyn
;
3803 Elf_Internal_Dyn dyn
;
3804 bfd_byte
*extdyn
, *next
;
3805 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3806 bool strip_zero_sized
;
3807 bool strip_zero_sized_plt
;
3809 if (bfd_link_relocatable (info
))
3812 hash_table
= elf_hash_table (info
);
3813 if (!is_elf_hash_table (&hash_table
->root
))
3816 if (!hash_table
->dynobj
)
3819 sdynamic
= hash_table
->dynamic
;
3823 bed
= get_elf_backend_data (hash_table
->dynobj
);
3824 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3826 strip_zero_sized
= false;
3827 strip_zero_sized_plt
= false;
3829 /* Strip zero-sized dynamic sections. */
3830 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3831 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3832 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3836 || s
== hash_table
->srelplt
->output_section
3837 || s
== hash_table
->splt
->output_section
))
3840 info
->output_bfd
->section_count
--;
3841 strip_zero_sized
= true;
3846 else if (s
== hash_table
->splt
->output_section
)
3848 s
= hash_table
->splt
;
3849 strip_zero_sized_plt
= true;
3852 s
= hash_table
->srelplt
;
3853 s
->flags
|= SEC_EXCLUDE
;
3854 s
->output_section
= bfd_abs_section_ptr
;
3859 if (strip_zero_sized_plt
&& sdynamic
->size
!= 0)
3860 for (extdyn
= sdynamic
->contents
;
3861 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3864 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3865 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3873 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3874 the procedure linkage table (the .plt section) has been
3876 memmove (extdyn
, next
,
3877 sdynamic
->size
- (next
- sdynamic
->contents
));
3882 if (strip_zero_sized
)
3884 /* Regenerate program headers. */
3885 elf_seg_map (info
->output_bfd
) = NULL
;
3886 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
,
3893 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3894 1 if a DT_NEEDED tag already exists, and 0 on success. */
3897 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3899 struct elf_link_hash_table
*hash_table
;
3903 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3906 hash_table
= elf_hash_table (info
);
3907 soname
= elf_dt_name (abfd
);
3908 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, false);
3909 if (strindex
== (size_t) -1)
3912 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3915 const struct elf_backend_data
*bed
;
3918 bed
= get_elf_backend_data (hash_table
->dynobj
);
3919 sdyn
= hash_table
->dynamic
;
3920 if (sdyn
!= NULL
&& sdyn
->size
!= 0)
3921 for (extdyn
= sdyn
->contents
;
3922 extdyn
< sdyn
->contents
+ sdyn
->size
;
3923 extdyn
+= bed
->s
->sizeof_dyn
)
3925 Elf_Internal_Dyn dyn
;
3927 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3928 if (dyn
.d_tag
== DT_NEEDED
3929 && dyn
.d_un
.d_val
== strindex
)
3931 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3937 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3940 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3946 /* Return true if SONAME is on the needed list between NEEDED and STOP
3947 (or the end of list if STOP is NULL), and needed by a library that
3951 on_needed_list (const char *soname
,
3952 struct bfd_link_needed_list
*needed
,
3953 struct bfd_link_needed_list
*stop
)
3955 struct bfd_link_needed_list
*look
;
3956 for (look
= needed
; look
!= stop
; look
= look
->next
)
3957 if (strcmp (soname
, look
->name
) == 0
3958 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3959 /* If needed by a library that itself is not directly
3960 needed, recursively check whether that library is
3961 indirectly needed. Since we add DT_NEEDED entries to
3962 the end of the list, library dependencies appear after
3963 the library. Therefore search prior to the current
3964 LOOK, preventing possible infinite recursion. */
3965 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3971 /* Sort symbol by value, section, size, and type. */
3973 elf_sort_symbol (const void *arg1
, const void *arg2
)
3975 const struct elf_link_hash_entry
*h1
;
3976 const struct elf_link_hash_entry
*h2
;
3977 bfd_signed_vma vdiff
;
3982 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3983 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3984 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3986 return vdiff
> 0 ? 1 : -1;
3988 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3992 /* Sort so that sized symbols are selected over zero size symbols. */
3993 vdiff
= h1
->size
- h2
->size
;
3995 return vdiff
> 0 ? 1 : -1;
3997 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3998 if (h1
->type
!= h2
->type
)
3999 return h1
->type
- h2
->type
;
4001 /* If symbols are properly sized and typed, and multiple strong
4002 aliases are not defined in a shared library by the user we
4003 shouldn't get here. Unfortunately linker script symbols like
4004 __bss_start sometimes match a user symbol defined at the start of
4005 .bss without proper size and type. We'd like to preference the
4006 user symbol over reserved system symbols. Sort on leading
4008 n1
= h1
->root
.root
.string
;
4009 n2
= h2
->root
.root
.string
;
4022 /* Final sort on name selects user symbols like '_u' over reserved
4023 system symbols like '_Z' and also will avoid qsort instability. */
4027 /* This function is used to adjust offsets into .dynstr for
4028 dynamic symbols. This is called via elf_link_hash_traverse. */
4031 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
4033 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
4035 if (h
->dynindx
!= -1)
4036 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
4040 /* Assign string offsets in .dynstr, update all structures referencing
4044 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
4046 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
4047 struct elf_link_local_dynamic_entry
*entry
;
4048 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
4049 bfd
*dynobj
= hash_table
->dynobj
;
4052 const struct elf_backend_data
*bed
;
4055 _bfd_elf_strtab_finalize (dynstr
);
4056 size
= _bfd_elf_strtab_size (dynstr
);
4058 /* Allow the linker to examine the dynsymtab now it's fully populated. */
4060 if (info
->callbacks
->examine_strtab
)
4061 info
->callbacks
->examine_strtab (dynstr
);
4063 bed
= get_elf_backend_data (dynobj
);
4064 sdyn
= hash_table
->dynamic
;
4065 BFD_ASSERT (sdyn
!= NULL
);
4067 /* Update all .dynamic entries referencing .dynstr strings. */
4068 for (extdyn
= sdyn
->contents
;
4069 extdyn
< PTR_ADD (sdyn
->contents
, sdyn
->size
);
4070 extdyn
+= bed
->s
->sizeof_dyn
)
4072 Elf_Internal_Dyn dyn
;
4074 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
4078 dyn
.d_un
.d_val
= size
;
4088 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
4093 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
4096 /* Now update local dynamic symbols. */
4097 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
4098 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
4099 entry
->isym
.st_name
);
4101 /* And the rest of dynamic symbols. */
4102 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
4104 /* Adjust version definitions. */
4105 if (elf_tdata (output_bfd
)->cverdefs
)
4110 Elf_Internal_Verdef def
;
4111 Elf_Internal_Verdaux defaux
;
4113 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
4117 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
4119 p
+= sizeof (Elf_External_Verdef
);
4120 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
4122 for (i
= 0; i
< def
.vd_cnt
; ++i
)
4124 _bfd_elf_swap_verdaux_in (output_bfd
,
4125 (Elf_External_Verdaux
*) p
, &defaux
);
4126 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
4128 _bfd_elf_swap_verdaux_out (output_bfd
,
4129 &defaux
, (Elf_External_Verdaux
*) p
);
4130 p
+= sizeof (Elf_External_Verdaux
);
4133 while (def
.vd_next
);
4136 /* Adjust version references. */
4137 if (elf_tdata (output_bfd
)->verref
)
4142 Elf_Internal_Verneed need
;
4143 Elf_Internal_Vernaux needaux
;
4145 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
4149 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
4151 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
4152 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
4153 (Elf_External_Verneed
*) p
);
4154 p
+= sizeof (Elf_External_Verneed
);
4155 for (i
= 0; i
< need
.vn_cnt
; ++i
)
4157 _bfd_elf_swap_vernaux_in (output_bfd
,
4158 (Elf_External_Vernaux
*) p
, &needaux
);
4159 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
4161 _bfd_elf_swap_vernaux_out (output_bfd
,
4163 (Elf_External_Vernaux
*) p
);
4164 p
+= sizeof (Elf_External_Vernaux
);
4167 while (need
.vn_next
);
4173 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4174 The default is to only match when the INPUT and OUTPUT are exactly
4178 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
4179 const bfd_target
*output
)
4181 return input
== output
;
4184 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4185 This version is used when different targets for the same architecture
4186 are virtually identical. */
4189 _bfd_elf_relocs_compatible (const bfd_target
*input
,
4190 const bfd_target
*output
)
4192 const struct elf_backend_data
*obed
, *ibed
;
4194 if (input
== output
)
4197 ibed
= xvec_get_elf_backend_data (input
);
4198 obed
= xvec_get_elf_backend_data (output
);
4200 if (ibed
->arch
!= obed
->arch
)
4203 /* If both backends are using this function, deem them compatible. */
4204 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
4207 /* Make a special call to the linker "notice" function to tell it that
4208 we are about to handle an as-needed lib, or have finished
4209 processing the lib. */
4212 _bfd_elf_notice_as_needed (bfd
*ibfd
,
4213 struct bfd_link_info
*info
,
4214 enum notice_asneeded_action act
)
4216 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
4219 /* Call ACTION on each relocation in an ELF object file. */
4222 _bfd_elf_link_iterate_on_relocs
4223 (bfd
*abfd
, struct bfd_link_info
*info
,
4224 bool (*action
) (bfd
*, struct bfd_link_info
*, asection
*,
4225 const Elf_Internal_Rela
*))
4227 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4228 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4230 /* If this object is the same format as the output object, and it is
4231 not a shared library, then let the backend look through the
4234 This is required to build global offset table entries and to
4235 arrange for dynamic relocs. It is not required for the
4236 particular common case of linking non PIC code, even when linking
4237 against shared libraries, but unfortunately there is no way of
4238 knowing whether an object file has been compiled PIC or not.
4239 Looking through the relocs is not particularly time consuming.
4240 The problem is that we must either (1) keep the relocs in memory,
4241 which causes the linker to require additional runtime memory or
4242 (2) read the relocs twice from the input file, which wastes time.
4243 This would be a good case for using mmap.
4245 I have no idea how to handle linking PIC code into a file of a
4246 different format. It probably can't be done. */
4247 if ((abfd
->flags
& DYNAMIC
) == 0
4248 && is_elf_hash_table (&htab
->root
)
4249 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4250 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4254 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4256 Elf_Internal_Rela
*internal_relocs
;
4259 /* Don't check relocations in excluded sections. Don't do
4260 anything special with non-loaded, non-alloced sections.
4261 In particular, any relocs in such sections should not
4262 affect GOT and PLT reference counting (ie. we don't
4263 allow them to create GOT or PLT entries), there's no
4264 possibility or desire to optimize TLS relocs, and
4265 there's not much point in propagating relocs to shared
4266 libs that the dynamic linker won't relocate. */
4267 if ((o
->flags
& SEC_ALLOC
) == 0
4268 || (o
->flags
& SEC_RELOC
) == 0
4269 || (o
->flags
& SEC_EXCLUDE
) != 0
4270 || o
->reloc_count
== 0
4271 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4272 && (o
->flags
& SEC_DEBUGGING
) != 0)
4273 || bfd_is_abs_section (o
->output_section
))
4276 internal_relocs
= _bfd_elf_link_info_read_relocs
4277 (abfd
, info
, o
, NULL
, NULL
,
4278 _bfd_elf_link_keep_memory (info
));
4279 if (internal_relocs
== NULL
)
4282 ok
= action (abfd
, info
, o
, internal_relocs
);
4284 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4285 free (internal_relocs
);
4295 /* Check relocations in an ELF object file. This is called after
4296 all input files have been opened. */
4299 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
4301 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4302 if (bed
->check_relocs
!= NULL
)
4303 return _bfd_elf_link_iterate_on_relocs (abfd
, info
,
4308 /* An entry in the first definition hash table. */
4310 struct elf_link_first_hash_entry
4312 struct bfd_hash_entry root
;
4313 /* The object of the first definition. */
4317 /* The function to create a new entry in the first definition hash
4320 static struct bfd_hash_entry
*
4321 elf_link_first_hash_newfunc (struct bfd_hash_entry
*entry
,
4322 struct bfd_hash_table
*table
,
4325 struct elf_link_first_hash_entry
*ret
=
4326 (struct elf_link_first_hash_entry
*) entry
;
4328 /* Allocate the structure if it has not already been allocated by a
4331 ret
= (struct elf_link_first_hash_entry
*)
4332 bfd_hash_allocate (table
,
4333 sizeof (struct elf_link_first_hash_entry
));
4337 /* Call the allocation method of the superclass. */
4338 ret
= ((struct elf_link_first_hash_entry
*)
4339 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
,
4344 return (struct bfd_hash_entry
*) ret
;
4347 /* Add the symbol NAME from ABFD to first hash. */
4350 elf_link_add_to_first_hash (bfd
*abfd
, struct bfd_link_info
*info
,
4351 const char *name
, bool copy
)
4353 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4354 /* Skip if there is no first hash. */
4355 if (htab
->first_hash
== NULL
)
4358 struct elf_link_first_hash_entry
*e
4359 = ((struct elf_link_first_hash_entry
*)
4360 bfd_hash_lookup (htab
->first_hash
, name
, true, copy
));
4362 info
->callbacks
->fatal
4363 (_("%P: %pB: failed to add %s to first hash\n"), abfd
, name
);
4365 if (e
->abfd
== NULL
)
4366 /* Store ABFD in abfd. */
4370 /* Add symbols from an ELF object file to the linker hash table. */
4373 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4375 Elf_Internal_Ehdr
*ehdr
;
4376 Elf_Internal_Shdr
*hdr
;
4380 struct elf_link_hash_entry
**sym_hash
;
4382 Elf_External_Versym
*extversym
= NULL
;
4383 Elf_External_Versym
*extversym_end
= NULL
;
4384 Elf_External_Versym
*ever
;
4385 struct elf_link_hash_entry
*weaks
;
4386 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4387 size_t nondeflt_vers_cnt
= 0;
4388 Elf_Internal_Sym
*isymbuf
= NULL
;
4389 Elf_Internal_Sym
*isym
;
4390 Elf_Internal_Sym
*isymend
;
4391 const struct elf_backend_data
*bed
;
4393 struct elf_link_hash_table
*htab
;
4394 void *alloc_mark
= NULL
;
4395 struct bfd_hash_entry
**old_table
= NULL
;
4396 unsigned int old_size
= 0;
4397 unsigned int old_count
= 0;
4398 void *old_tab
= NULL
;
4400 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4401 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4402 void *old_strtab
= NULL
;
4407 htab
= elf_hash_table (info
);
4408 bed
= get_elf_backend_data (abfd
);
4410 if (elf_use_dt_symtab_p (abfd
))
4412 bfd_set_error (bfd_error_wrong_format
);
4416 if ((abfd
->flags
& DYNAMIC
) == 0)
4419 if ((abfd
->flags
& BFD_PLUGIN
) != 0
4420 && is_elf_hash_table (&htab
->root
)
4421 && htab
->first_hash
== NULL
)
4423 /* Initialize first_hash for an IR input. */
4424 htab
->first_hash
= (struct bfd_hash_table
*)
4425 bfd_malloc (sizeof (struct bfd_hash_table
));
4426 if (htab
->first_hash
== NULL
4427 || !bfd_hash_table_init
4428 (htab
->first_hash
, elf_link_first_hash_newfunc
,
4429 sizeof (struct elf_link_first_hash_entry
)))
4430 info
->callbacks
->fatal
4431 (_("%P: first_hash failed to create: %E\n"));
4438 /* You can't use -r against a dynamic object. Also, there's no
4439 hope of using a dynamic object which does not exactly match
4440 the format of the output file. */
4441 if (bfd_link_relocatable (info
)
4442 || !is_elf_hash_table (&htab
->root
)
4443 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4445 if (bfd_link_relocatable (info
))
4446 bfd_set_error (bfd_error_invalid_operation
);
4448 bfd_set_error (bfd_error_wrong_format
);
4453 ehdr
= elf_elfheader (abfd
);
4454 if (info
->warn_alternate_em
4455 && bed
->elf_machine_code
!= ehdr
->e_machine
4456 && ((bed
->elf_machine_alt1
!= 0
4457 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4458 || (bed
->elf_machine_alt2
!= 0
4459 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4461 /* xgettext:c-format */
4462 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4463 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4465 /* As a GNU extension, any input sections which are named
4466 .gnu.warning.SYMBOL are treated as warning symbols for the given
4467 symbol. This differs from .gnu.warning sections, which generate
4468 warnings when they are included in an output file. */
4469 /* PR 12761: Also generate this warning when building shared libraries. */
4470 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4474 name
= bfd_section_name (s
);
4475 if (startswith (name
, ".gnu.warning."))
4480 name
+= sizeof ".gnu.warning." - 1;
4482 /* If this is a shared object, then look up the symbol
4483 in the hash table. If it is there, and it is already
4484 been defined, then we will not be using the entry
4485 from this shared object, so we don't need to warn.
4486 FIXME: If we see the definition in a regular object
4487 later on, we will warn, but we shouldn't. The only
4488 fix is to keep track of what warnings we are supposed
4489 to emit, and then handle them all at the end of the
4493 struct elf_link_hash_entry
*h
;
4495 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
4497 /* FIXME: What about bfd_link_hash_common? */
4499 && (h
->root
.type
== bfd_link_hash_defined
4500 || h
->root
.type
== bfd_link_hash_defweak
))
4505 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4509 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4514 if (! (_bfd_generic_link_add_one_symbol
4515 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4516 false, bed
->collect
, NULL
)))
4519 if (bfd_link_executable (info
))
4521 /* Clobber the section size so that the warning does
4522 not get copied into the output file. */
4525 /* Also set SEC_EXCLUDE, so that symbols defined in
4526 the warning section don't get copied to the output. */
4527 s
->flags
|= SEC_EXCLUDE
;
4532 just_syms
= ((s
= abfd
->sections
) != NULL
4533 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4538 /* If we are creating a shared library, create all the dynamic
4539 sections immediately. We need to attach them to something,
4540 so we attach them to this BFD, provided it is the right
4541 format and is not from ld --just-symbols. Always create the
4542 dynamic sections for -E/--dynamic-list. FIXME: If there
4543 are no input BFD's of the same format as the output, we can't
4544 make a shared library. */
4546 && (bfd_link_pic (info
)
4547 || (!bfd_link_relocatable (info
)
4549 && (info
->export_dynamic
|| info
->dynamic
)))
4550 && is_elf_hash_table (&htab
->root
)
4551 && info
->output_bfd
->xvec
== abfd
->xvec
4552 && !htab
->dynamic_sections_created
)
4554 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4558 else if (!is_elf_hash_table (&htab
->root
))
4562 const char *soname
= NULL
;
4564 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4565 const Elf_Internal_Phdr
*phdr
;
4566 struct elf_link_loaded_list
*loaded_lib
;
4568 /* ld --just-symbols and dynamic objects don't mix very well.
4569 ld shouldn't allow it. */
4573 /* If this dynamic lib was specified on the command line with
4574 --as-needed in effect, then we don't want to add a DT_NEEDED
4575 tag unless the lib is actually used. Similary for libs brought
4576 in by another lib's DT_NEEDED. When --no-add-needed is used
4577 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4578 any dynamic library in DT_NEEDED tags in the dynamic lib at
4580 add_needed
= (elf_dyn_lib_class (abfd
)
4581 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4582 | DYN_NO_NEEDED
)) == 0;
4584 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4585 if (s
!= NULL
&& s
->size
!= 0 && (s
->flags
& SEC_HAS_CONTENTS
) != 0)
4589 unsigned int elfsec
;
4590 unsigned long shlink
;
4592 if (!_bfd_elf_mmap_section_contents (abfd
, s
, &dynbuf
))
4595 _bfd_elf_munmap_section_contents (s
, dynbuf
);
4599 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4600 if (elfsec
== SHN_BAD
)
4601 goto error_free_dyn
;
4602 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4604 for (extdyn
= dynbuf
;
4605 (size_t) (dynbuf
+ s
->size
- extdyn
) >= bed
->s
->sizeof_dyn
;
4606 extdyn
+= bed
->s
->sizeof_dyn
)
4608 Elf_Internal_Dyn dyn
;
4610 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4611 if (dyn
.d_tag
== DT_SONAME
)
4613 unsigned int tagv
= dyn
.d_un
.d_val
;
4614 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4616 goto error_free_dyn
;
4618 if (dyn
.d_tag
== DT_NEEDED
)
4620 struct bfd_link_needed_list
*n
, **pn
;
4622 unsigned int tagv
= dyn
.d_un
.d_val
;
4623 size_t amt
= sizeof (struct bfd_link_needed_list
);
4625 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4626 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4627 if (n
== NULL
|| fnm
== NULL
)
4628 goto error_free_dyn
;
4629 amt
= strlen (fnm
) + 1;
4630 anm
= (char *) bfd_alloc (abfd
, amt
);
4632 goto error_free_dyn
;
4633 memcpy (anm
, fnm
, amt
);
4637 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4641 if (dyn
.d_tag
== DT_RUNPATH
)
4643 struct bfd_link_needed_list
*n
, **pn
;
4645 unsigned int tagv
= dyn
.d_un
.d_val
;
4646 size_t amt
= sizeof (struct bfd_link_needed_list
);
4648 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4649 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4650 if (n
== NULL
|| fnm
== NULL
)
4651 goto error_free_dyn
;
4652 amt
= strlen (fnm
) + 1;
4653 anm
= (char *) bfd_alloc (abfd
, amt
);
4655 goto error_free_dyn
;
4656 memcpy (anm
, fnm
, amt
);
4660 for (pn
= & runpath
;
4666 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4667 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4669 struct bfd_link_needed_list
*n
, **pn
;
4671 unsigned int tagv
= dyn
.d_un
.d_val
;
4672 size_t amt
= sizeof (struct bfd_link_needed_list
);
4674 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4675 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4676 if (n
== NULL
|| fnm
== NULL
)
4677 goto error_free_dyn
;
4678 amt
= strlen (fnm
) + 1;
4679 anm
= (char *) bfd_alloc (abfd
, amt
);
4681 goto error_free_dyn
;
4682 memcpy (anm
, fnm
, amt
);
4692 if (dyn
.d_tag
== DT_AUDIT
)
4694 unsigned int tagv
= dyn
.d_un
.d_val
;
4695 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4697 if (dyn
.d_tag
== DT_FLAGS_1
)
4698 elf_tdata (abfd
)->is_pie
= (dyn
.d_un
.d_val
& DF_1_PIE
) != 0;
4701 _bfd_elf_munmap_section_contents (s
, dynbuf
);
4704 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4705 frees all more recently bfd_alloc'd blocks as well. */
4711 struct bfd_link_needed_list
**pn
;
4712 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4717 /* If we have a PT_GNU_RELRO program header, mark as read-only
4718 all sections contained fully therein. This makes relro
4719 shared library sections appear as they will at run-time. */
4720 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4721 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4722 if (phdr
->p_type
== PT_GNU_RELRO
)
4724 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4726 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4728 if ((s
->flags
& SEC_ALLOC
) != 0
4729 && s
->vma
* opb
>= phdr
->p_vaddr
4730 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4731 s
->flags
|= SEC_READONLY
;
4736 /* We do not want to include any of the sections in a dynamic
4737 object in the output file. We hack by simply clobbering the
4738 list of sections in the BFD. This could be handled more
4739 cleanly by, say, a new section flag; the existing
4740 SEC_NEVER_LOAD flag is not the one we want, because that one
4741 still implies that the section takes up space in the output
4743 bfd_section_list_clear (abfd
);
4745 /* Find the name to use in a DT_NEEDED entry that refers to this
4746 object. If the object has a DT_SONAME entry, we use it.
4747 Otherwise, if the generic linker stuck something in
4748 elf_dt_name, we use that. Otherwise, we just use the file
4750 if (soname
== NULL
|| *soname
== '\0')
4752 soname
= elf_dt_name (abfd
);
4753 if (soname
== NULL
|| *soname
== '\0')
4754 soname
= bfd_get_filename (abfd
);
4757 /* Save the SONAME because sometimes the linker emulation code
4758 will need to know it. */
4759 elf_dt_name (abfd
) = soname
;
4761 /* If we have already included this dynamic object in the
4762 link, just ignore it. There is no reason to include a
4763 particular dynamic object more than once. */
4764 for (loaded_lib
= htab
->dyn_loaded
;
4766 loaded_lib
= loaded_lib
->next
)
4768 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4772 /* Create dynamic sections for backends that require that be done
4773 before setup_gnu_properties. */
4775 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4778 /* Save the DT_AUDIT entry for the linker emulation code. */
4779 elf_dt_audit (abfd
) = audit
;
4782 /* If this is a dynamic object, we always link against the .dynsym
4783 symbol table, not the .symtab symbol table. The dynamic linker
4784 will only see the .dynsym symbol table, so there is no reason to
4785 look at .symtab for a dynamic object. */
4787 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4788 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4790 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4792 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4794 /* The sh_info field of the symtab header tells us where the
4795 external symbols start. We don't care about the local symbols at
4797 if (elf_bad_symtab (abfd
))
4799 extsymcount
= symcount
;
4804 extsymcount
= symcount
- hdr
->sh_info
;
4805 extsymoff
= hdr
->sh_info
;
4808 sym_hash
= elf_sym_hashes (abfd
);
4809 if (extsymcount
!= 0)
4811 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4813 if (isymbuf
== NULL
)
4816 if (sym_hash
== NULL
)
4818 /* We store a pointer to the hash table entry for each
4820 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4821 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4822 if (sym_hash
== NULL
)
4823 goto error_free_sym
;
4824 elf_sym_hashes (abfd
) = sym_hash
;
4830 /* Read in any version definitions. */
4831 if (!_bfd_elf_slurp_version_tables (abfd
,
4832 info
->default_imported_symver
))
4833 goto error_free_sym
;
4835 /* Read in the symbol versions, but don't bother to convert them
4836 to internal format. */
4837 if (elf_dynversym (abfd
) != 0)
4839 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4840 bfd_size_type amt
= versymhdr
->sh_size
;
4842 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4843 goto error_free_sym
;
4844 extversym
= (Elf_External_Versym
*)
4845 _bfd_malloc_and_read (abfd
, amt
, amt
);
4846 if (extversym
== NULL
)
4847 goto error_free_sym
;
4848 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4852 /* If we are loading an as-needed shared lib, save the symbol table
4853 state before we start adding symbols. If the lib turns out
4854 to be unneeded, restore the state. */
4855 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4860 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4862 struct bfd_hash_entry
*p
;
4863 struct elf_link_hash_entry
*h
;
4865 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4867 h
= (struct elf_link_hash_entry
*) p
;
4868 entsize
+= htab
->root
.table
.entsize
;
4869 if (h
->root
.type
== bfd_link_hash_warning
)
4871 entsize
+= htab
->root
.table
.entsize
;
4872 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4874 if (h
->root
.type
== bfd_link_hash_common
)
4875 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4879 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4880 old_tab
= bfd_malloc (tabsize
+ entsize
);
4881 if (old_tab
== NULL
)
4882 goto error_free_vers
;
4884 /* Remember the current objalloc pointer, so that all mem for
4885 symbols added can later be reclaimed. */
4886 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4887 if (alloc_mark
== NULL
)
4888 goto error_free_vers
;
4890 /* Make a special call to the linker "notice" function to
4891 tell it that we are about to handle an as-needed lib. */
4892 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4893 goto error_free_vers
;
4895 /* Clone the symbol table. Remember some pointers into the
4896 symbol table, and dynamic symbol count. */
4897 old_ent
= (char *) old_tab
+ tabsize
;
4898 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4899 old_undefs
= htab
->root
.undefs
;
4900 old_undefs_tail
= htab
->root
.undefs_tail
;
4901 old_table
= htab
->root
.table
.table
;
4902 old_size
= htab
->root
.table
.size
;
4903 old_count
= htab
->root
.table
.count
;
4905 if (htab
->dynstr
!= NULL
)
4907 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4908 if (old_strtab
== NULL
)
4909 goto error_free_vers
;
4912 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4914 struct bfd_hash_entry
*p
;
4915 struct elf_link_hash_entry
*h
;
4917 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4919 h
= (struct elf_link_hash_entry
*) p
;
4920 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4921 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4922 if (h
->root
.type
== bfd_link_hash_warning
)
4924 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4925 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4926 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4928 if (h
->root
.type
== bfd_link_hash_common
)
4930 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4931 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4938 if (extversym
== NULL
)
4940 else if (extversym
+ extsymoff
< extversym_end
)
4941 ever
= extversym
+ extsymoff
;
4944 /* xgettext:c-format */
4945 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4946 abfd
, (long) extsymoff
,
4947 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4948 bfd_set_error (bfd_error_bad_value
);
4949 goto error_free_vers
;
4952 if (!bfd_link_relocatable (info
)
4953 && bfd_get_lto_type (abfd
) == lto_slim_ir_object
)
4956 (_("%pB: plugin needed to handle lto object"), abfd
);
4959 for (isym
= isymbuf
, isymend
= PTR_ADD (isymbuf
, extsymcount
);
4961 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4965 asection
*sec
, *new_sec
;
4968 const char *defvername
;
4969 bool must_copy_name
= false;
4970 struct elf_link_hash_entry
*h
;
4971 struct elf_link_hash_entry
*hi
;
4973 bool size_change_ok
;
4974 bool type_change_ok
;
4980 unsigned int old_alignment
;
4981 unsigned int shindex
;
4987 flags
= BSF_NO_FLAGS
;
4989 value
= isym
->st_value
;
4990 common
= bed
->common_definition (isym
);
4991 if (common
&& info
->inhibit_common_definition
)
4993 /* Treat common symbol as undefined for --no-define-common. */
4994 isym
->st_shndx
= SHN_UNDEF
;
4999 bind
= ELF_ST_BIND (isym
->st_info
);
5003 /* This should be impossible, since ELF requires that all
5004 global symbols follow all local symbols, and that sh_info
5005 point to the first global symbol. Unfortunately, Irix 5
5007 if (elf_bad_symtab (abfd
))
5010 /* If we aren't prepared to handle locals within the globals
5011 then we'll likely segfault on a NULL symbol hash if the
5012 symbol is ever referenced in relocations. */
5013 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
5014 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
5015 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
5016 " (>= sh_info of %lu)"),
5017 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
5020 /* Dynamic object relocations are not processed by ld, so
5021 ld won't run into the problem mentioned above. */
5024 bfd_set_error (bfd_error_bad_value
);
5025 goto error_free_vers
;
5028 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
5036 case STB_GNU_UNIQUE
:
5037 flags
= BSF_GNU_UNIQUE
;
5041 /* Leave it up to the processor backend. */
5045 if (isym
->st_shndx
== SHN_UNDEF
)
5046 sec
= bfd_und_section_ptr
;
5047 else if (isym
->st_shndx
== SHN_ABS
)
5048 sec
= bfd_abs_section_ptr
;
5049 else if (isym
->st_shndx
== SHN_COMMON
)
5051 sec
= bfd_com_section_ptr
;
5052 /* What ELF calls the size we call the value. What ELF
5053 calls the value we call the alignment. */
5054 value
= isym
->st_size
;
5058 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
5060 sec
= bfd_abs_section_ptr
;
5061 else if (discarded_section (sec
))
5063 /* Symbols from discarded section are undefined. We keep
5065 sec
= bfd_und_section_ptr
;
5067 isym
->st_shndx
= SHN_UNDEF
;
5069 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
5073 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5076 goto error_free_vers
;
5078 if (isym
->st_shndx
== SHN_COMMON
5079 && (abfd
->flags
& BFD_PLUGIN
) != 0)
5081 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
5085 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
5087 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
5089 goto error_free_vers
;
5093 else if (isym
->st_shndx
== SHN_COMMON
5094 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
5095 && !bfd_link_relocatable (info
))
5097 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
5101 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
5102 | SEC_LINKER_CREATED
);
5103 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
5105 goto error_free_vers
;
5109 else if (bed
->elf_add_symbol_hook
)
5111 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
5113 goto error_free_vers
;
5115 /* The hook function sets the name to NULL if this symbol
5116 should be skipped for some reason. */
5121 /* Sanity check that all possibilities were handled. */
5125 /* Silently discard TLS symbols from --just-syms. There's
5126 no way to combine a static TLS block with a new TLS block
5127 for this executable. */
5128 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
5129 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
5132 if (bfd_is_und_section (sec
)
5133 || bfd_is_com_section (sec
))
5138 size_change_ok
= false;
5139 type_change_ok
= bed
->type_change_ok
;
5147 if (is_elf_hash_table (&htab
->root
))
5149 Elf_Internal_Versym iver
;
5150 unsigned int vernum
= 0;
5155 if (info
->default_imported_symver
)
5156 /* Use the default symbol version created earlier. */
5157 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
5161 else if (ever
>= extversym_end
)
5163 /* xgettext:c-format */
5164 _bfd_error_handler (_("%pB: not enough version information"),
5166 bfd_set_error (bfd_error_bad_value
);
5167 goto error_free_vers
;
5170 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
5172 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
5174 /* If this is a hidden symbol, or if it is not version
5175 1, we append the version name to the symbol name.
5176 However, we do not modify a non-hidden absolute symbol
5177 if it is not a function, because it might be the version
5178 symbol itself. FIXME: What if it isn't? */
5179 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
5181 && (!bfd_is_abs_section (sec
)
5182 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
5185 size_t namelen
, verlen
, newlen
;
5188 if (isym
->st_shndx
!= SHN_UNDEF
)
5190 if (vernum
> elf_tdata (abfd
)->cverdefs
)
5192 else if (vernum
> 1)
5194 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
5201 /* xgettext:c-format */
5202 (_("%pB: %s: invalid version %u (max %d)"),
5204 elf_tdata (abfd
)->cverdefs
);
5205 bfd_set_error (bfd_error_bad_value
);
5206 goto error_free_vers
;
5211 /* We cannot simply test for the number of
5212 entries in the VERNEED section since the
5213 numbers for the needed versions do not start
5215 Elf_Internal_Verneed
*t
;
5218 for (t
= elf_tdata (abfd
)->verref
;
5222 Elf_Internal_Vernaux
*a
;
5224 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5226 if (a
->vna_other
== vernum
)
5228 verstr
= a
->vna_nodename
;
5238 /* xgettext:c-format */
5239 (_("%pB: %s: invalid needed version %d"),
5240 abfd
, name
, vernum
);
5241 bfd_set_error (bfd_error_bad_value
);
5242 goto error_free_vers
;
5246 namelen
= strlen (name
);
5247 verlen
= strlen (verstr
);
5248 newlen
= namelen
+ verlen
+ 2;
5249 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5250 && isym
->st_shndx
!= SHN_UNDEF
)
5253 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
5254 if (newname
== NULL
)
5255 goto error_free_vers
;
5256 memcpy (newname
, name
, namelen
);
5257 p
= newname
+ namelen
;
5259 /* If this is a defined non-hidden version symbol,
5260 we add another @ to the name. This indicates the
5261 default version of the symbol. */
5262 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5263 && isym
->st_shndx
!= SHN_UNDEF
)
5264 *p
++ = ELF_VER_CHR
, defvername
= name
;
5265 memcpy (p
, verstr
, verlen
+ 1);
5268 /* Since bfd_hash_alloc is used for "name", the string
5269 must be copied if added to first_hash. The string
5270 memory can be freed when an --as-needed library is
5272 must_copy_name
= true;
5275 /* If this symbol has default visibility and the user has
5276 requested we not re-export it, then mark it as hidden. */
5277 if (!bfd_is_und_section (sec
)
5280 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
5281 isym
->st_other
= (STV_HIDDEN
5282 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
5284 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
5285 sym_hash
, &old_bfd
, &old_weak
,
5286 &old_alignment
, &skip
, &override
,
5287 &type_change_ok
, &size_change_ok
,
5289 goto error_free_vers
;
5295 while (h
->root
.type
== bfd_link_hash_indirect
5296 || h
->root
.type
== bfd_link_hash_warning
)
5297 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5299 /* Override a definition only if the new symbol matches the
5301 if (override
&& matched
)
5304 if (htab
->first_hash
!= NULL
5305 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5306 && h
->root
.non_ir_ref_regular
)
5308 /* When reloading --as-needed shared objects for new
5309 symbols added from IR inputs, if this shared object
5310 has the first definition, use it. */
5311 struct elf_link_first_hash_entry
*e
5312 = ((struct elf_link_first_hash_entry
*)
5313 bfd_hash_lookup (htab
->first_hash
, name
, false,
5315 if (e
!= NULL
&& e
->abfd
== abfd
)
5320 if (h
->versioned
!= unversioned
5321 && elf_tdata (abfd
)->verdef
!= NULL
5324 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
5327 if (! (_bfd_generic_link_add_one_symbol
5328 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
5329 NULL
, false, bed
->collect
,
5330 (struct bfd_link_hash_entry
**) sym_hash
)))
5331 goto error_free_vers
;
5334 /* We need to make sure that indirect symbol dynamic flags are
5337 while (h
->root
.type
== bfd_link_hash_indirect
5338 || h
->root
.type
== bfd_link_hash_warning
)
5339 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5343 /* Setting the index to -3 tells elf_link_output_extsym that
5344 this symbol is defined in a discarded section. */
5345 if (discarded
&& is_elf_hash_table (&htab
->root
))
5348 new_weak
= (flags
& BSF_WEAK
) != 0;
5352 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
5353 && is_elf_hash_table (&htab
->root
)
5354 && h
->u
.alias
== NULL
)
5356 /* Keep a list of all weak defined non function symbols from
5357 a dynamic object, using the alias field. Later in this
5358 function we will set the alias field to the correct
5359 value. We only put non-function symbols from dynamic
5360 objects on this list, because that happens to be the only
5361 time we need to know the normal symbol corresponding to a
5362 weak symbol, and the information is time consuming to
5363 figure out. If the alias field is not already NULL,
5364 then this symbol was already defined by some previous
5365 dynamic object, and we will be using that previous
5366 definition anyhow. */
5372 /* Set the alignment of a common symbol. */
5373 if ((common
|| bfd_is_com_section (sec
))
5374 && h
->root
.type
== bfd_link_hash_common
)
5379 align
= bfd_log2 (isym
->st_value
);
5382 /* The new symbol is a common symbol in a shared object.
5383 We need to get the alignment from the section. */
5384 align
= new_sec
->alignment_power
;
5386 if (align
> old_alignment
)
5387 h
->root
.u
.c
.p
->alignment_power
= align
;
5389 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5392 if (is_elf_hash_table (&htab
->root
))
5394 /* Set a flag in the hash table entry indicating the type of
5395 reference or definition we just found. A dynamic symbol
5396 is one which is referenced or defined by both a regular
5397 object and a shared object. */
5398 bool dynsym
= false;
5400 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5401 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5403 /* Except for this flag to track nonweak references. */
5405 && bind
!= STB_WEAK
)
5406 h
->ref_ir_nonweak
= 1;
5413 if (bind
!= STB_WEAK
)
5414 h
->ref_regular_nonweak
= 1;
5431 hi
->ref_dynamic
= 1;
5436 hi
->def_dynamic
= 1;
5440 /* If an indirect symbol has been forced local, don't
5441 make the real symbol dynamic. */
5442 if (h
!= hi
&& hi
->forced_local
)
5446 if (bfd_link_dll (info
)
5456 && weakdef (h
)->dynindx
!= -1))
5460 /* Check to see if we need to add an indirect symbol for
5461 the default name. */
5463 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5465 && hi
->versioned
== versioned_hidden
))
5466 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5467 sec
, value
, &old_bfd
, &dynsym
))
5468 goto error_free_vers
;
5470 /* Check the alignment when a common symbol is involved. This
5471 can change when a common symbol is overridden by a normal
5472 definition or a common symbol is ignored due to the old
5473 normal definition. We need to make sure the maximum
5474 alignment is maintained. */
5475 if ((old_alignment
|| common
)
5476 && h
->root
.type
!= bfd_link_hash_common
)
5478 unsigned int common_align
;
5479 unsigned int normal_align
;
5480 unsigned int symbol_align
;
5484 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5485 || h
->root
.type
== bfd_link_hash_defweak
);
5487 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5488 if (h
->root
.u
.def
.section
->owner
!= NULL
5489 && (h
->root
.u
.def
.section
->owner
->flags
5490 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5492 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5493 if (normal_align
> symbol_align
)
5494 normal_align
= symbol_align
;
5497 normal_align
= symbol_align
;
5501 common_align
= old_alignment
;
5502 common_bfd
= old_bfd
;
5507 common_align
= bfd_log2 (isym
->st_value
);
5509 normal_bfd
= old_bfd
;
5512 if (normal_align
< common_align
)
5514 /* PR binutils/2735 */
5515 if (normal_bfd
== NULL
)
5517 /* xgettext:c-format */
5518 (_("warning: alignment %u of common symbol `%s' in %pB is"
5519 " greater than the alignment (%u) of its section %pA"),
5520 1 << common_align
, name
, common_bfd
,
5521 1 << normal_align
, h
->root
.u
.def
.section
);
5524 /* xgettext:c-format */
5525 (_("warning: alignment %u of normal symbol `%s' in %pB"
5526 " is smaller than %u used by the common definition in %pB"),
5527 1 << normal_align
, name
, normal_bfd
,
5528 1 << common_align
, common_bfd
);
5530 /* PR 30499: make sure that users understand that this warning is serious. */
5532 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5536 /* Remember the symbol size if it isn't undefined. */
5537 if (isym
->st_size
!= 0
5538 && isym
->st_shndx
!= SHN_UNDEF
5539 && (definition
|| h
->size
== 0))
5542 && h
->size
!= isym
->st_size
5543 && ! size_change_ok
)
5546 /* xgettext:c-format */
5547 (_("warning: size of symbol `%s' changed"
5548 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5549 name
, (uint64_t) h
->size
, old_bfd
,
5550 (uint64_t) isym
->st_size
, abfd
);
5552 /* PR 30499: make sure that users understand that this warning is serious. */
5554 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5557 h
->size
= isym
->st_size
;
5560 /* If this is a common symbol, then we always want H->SIZE
5561 to be the size of the common symbol. The code just above
5562 won't fix the size if a common symbol becomes larger. We
5563 don't warn about a size change here, because that is
5564 covered by --warn-common. Allow changes between different
5566 if (h
->root
.type
== bfd_link_hash_common
)
5567 h
->size
= h
->root
.u
.c
.size
;
5569 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5570 && ((definition
&& !new_weak
)
5571 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5572 || h
->type
== STT_NOTYPE
))
5574 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5576 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5578 if (type
== STT_GNU_IFUNC
5579 && (abfd
->flags
& DYNAMIC
) != 0)
5582 if (h
->type
!= type
)
5584 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5585 /* xgettext:c-format */
5587 (_("warning: type of symbol `%s' changed"
5588 " from %d to %d in %pB"),
5589 name
, h
->type
, type
, abfd
);
5595 /* Merge st_other field. */
5596 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5597 definition
, dynamic
);
5599 /* We don't want to make debug symbol dynamic. */
5601 && (sec
->flags
& SEC_DEBUGGING
)
5602 && !bfd_link_relocatable (info
))
5605 /* Nor should we make plugin symbols dynamic. */
5606 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5611 h
->target_internal
= isym
->st_target_internal
;
5612 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5615 /* Don't add indirect symbols for .symver x, x@FOO aliases
5616 in IR. Since all data or text symbols in IR have the
5617 same type, value and section, we can't tell if a symbol
5618 is an alias of another symbol by their types, values and
5622 && (abfd
->flags
& BFD_PLUGIN
) == 0)
5624 char *p
= strchr (name
, ELF_VER_CHR
);
5625 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5627 /* Queue non-default versions so that .symver x, x@FOO
5628 aliases can be checked. */
5631 size_t amt
= ((isymend
- isym
+ 1)
5632 * sizeof (struct elf_link_hash_entry
*));
5634 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5636 goto error_free_vers
;
5638 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5642 if (dynsym
&& h
->dynindx
== -1)
5644 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5645 goto error_free_vers
;
5647 && weakdef (h
)->dynindx
== -1)
5649 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5650 goto error_free_vers
;
5653 else if (h
->dynindx
!= -1)
5654 /* If the symbol already has a dynamic index, but
5655 visibility says it should not be visible, turn it into
5657 switch (ELF_ST_VISIBILITY (h
->other
))
5661 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
5669 && h
->root
.type
!= bfd_link_hash_indirect
)
5672 && h
->ref_regular_nonweak
)
5674 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5675 && h
->ref_ir_nonweak
5676 && !info
->lto_all_symbols_read
)
5677 || (h
->ref_dynamic_nonweak
5678 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5679 && !on_needed_list (elf_dt_name (abfd
),
5680 htab
->needed
, NULL
)))
5682 const char *soname
= elf_dt_name (abfd
);
5684 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5685 h
->root
.root
.string
);
5687 /* A symbol from a library loaded via DT_NEEDED of some
5688 other library is referenced by a regular object.
5689 Add a DT_NEEDED entry for it. Issue an error if
5690 --no-add-needed is used and the reference was not
5693 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5696 /* xgettext:c-format */
5697 (_("%pB: undefined reference to symbol '%s'"),
5699 bfd_set_error (bfd_error_missing_dso
);
5700 goto error_free_vers
;
5703 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5704 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5706 /* Create dynamic sections for backends that require
5707 that be done before setup_gnu_properties. */
5708 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5709 goto error_free_vers
;
5713 && h
->root
.u
.def
.section
->owner
== abfd
)
5715 /* Add this symbol to first hash if this shared
5716 object has the first definition. */
5717 elf_link_add_to_first_hash (abfd
, info
, name
, must_copy_name
);
5718 /* And if it was the default symbol version definition,
5719 also add the short name. */
5721 elf_link_add_to_first_hash (abfd
, info
, defvername
, false);
5727 if (info
->lto_plugin_active
5728 && !bfd_link_relocatable (info
)
5729 && (abfd
->flags
& BFD_PLUGIN
) == 0
5732 && is_elf_hash_table (&htab
->root
))
5736 if (bed
->s
->arch_size
== 32)
5741 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5742 referenced in regular objects so that linker plugin will get
5743 the correct symbol resolution. */
5745 sym_hash
= elf_sym_hashes (abfd
);
5746 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5748 Elf_Internal_Rela
*internal_relocs
;
5749 Elf_Internal_Rela
*rel
, *relend
;
5751 /* Don't check relocations in excluded sections. */
5752 if ((s
->flags
& SEC_RELOC
) == 0
5753 || s
->reloc_count
== 0
5754 || (s
->flags
& SEC_EXCLUDE
) != 0
5755 || (s
->flags
& SEC_DEBUGGING
) != 0)
5758 internal_relocs
= _bfd_elf_link_info_read_relocs
5759 (abfd
, info
, s
, NULL
, NULL
,
5760 _bfd_elf_link_keep_memory (info
));
5761 if (internal_relocs
== NULL
)
5762 goto error_free_vers
;
5764 rel
= internal_relocs
;
5765 relend
= rel
+ s
->reloc_count
;
5766 for ( ; rel
< relend
; rel
++)
5768 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5769 struct elf_link_hash_entry
*h
;
5771 /* Skip local symbols. */
5772 if (r_symndx
< extsymoff
)
5775 h
= sym_hash
[r_symndx
- extsymoff
];
5777 h
->root
.non_ir_ref_regular
= 1;
5780 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5781 free (internal_relocs
);
5790 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5794 /* Restore the symbol table. */
5795 old_ent
= (char *) old_tab
+ tabsize
;
5796 memset (elf_sym_hashes (abfd
), 0,
5797 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5798 htab
->root
.table
.table
= old_table
;
5799 htab
->root
.table
.size
= old_size
;
5800 htab
->root
.table
.count
= old_count
;
5801 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5802 htab
->root
.undefs
= old_undefs
;
5803 htab
->root
.undefs_tail
= old_undefs_tail
;
5804 if (htab
->dynstr
!= NULL
)
5805 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5808 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5810 struct bfd_hash_entry
*p
;
5811 struct elf_link_hash_entry
*h
;
5812 unsigned int non_ir_ref_dynamic
;
5814 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5816 /* Preserve non_ir_ref_dynamic so that this symbol
5817 will be exported when the dynamic lib becomes needed
5818 in the second pass. */
5819 h
= (struct elf_link_hash_entry
*) p
;
5820 if (h
->root
.type
== bfd_link_hash_warning
)
5821 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5822 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5824 h
= (struct elf_link_hash_entry
*) p
;
5825 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5826 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5827 if (h
->root
.type
== bfd_link_hash_warning
)
5829 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5830 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5831 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5833 if (h
->root
.type
== bfd_link_hash_common
)
5835 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5836 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5838 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5842 /* Make a special call to the linker "notice" function to
5843 tell it that symbols added for crefs may need to be removed. */
5844 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5845 goto error_free_vers
;
5848 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5850 free (nondeflt_vers
);
5856 if (old_tab
!= NULL
)
5858 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5859 goto error_free_vers
;
5864 /* Now that all the symbols from this input file are created, if
5865 not performing a relocatable link, handle .symver foo, foo@BAR
5866 such that any relocs against foo become foo@BAR. */
5867 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5871 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5873 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5874 char *shortname
, *p
;
5877 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5879 || (h
->root
.type
!= bfd_link_hash_defined
5880 && h
->root
.type
!= bfd_link_hash_defweak
))
5883 amt
= p
- h
->root
.root
.string
;
5884 shortname
= (char *) bfd_malloc (amt
+ 1);
5886 goto error_free_vers
;
5887 memcpy (shortname
, h
->root
.root
.string
, amt
);
5888 shortname
[amt
] = '\0';
5890 hi
= (struct elf_link_hash_entry
*)
5891 bfd_link_hash_lookup (&htab
->root
, shortname
,
5892 false, false, false);
5894 && hi
->root
.type
== h
->root
.type
5895 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5896 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5898 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
5899 hi
->root
.type
= bfd_link_hash_indirect
;
5900 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5901 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5902 sym_hash
= elf_sym_hashes (abfd
);
5904 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5905 if (sym_hash
[symidx
] == hi
)
5907 sym_hash
[symidx
] = h
;
5914 free (nondeflt_vers
);
5915 nondeflt_vers
= NULL
;
5917 /* Now set the alias field correctly for all the weak defined
5918 symbols we found. The only way to do this is to search all the
5919 symbols. Since we only need the information for non functions in
5920 dynamic objects, that's the only time we actually put anything on
5921 the list WEAKS. We need this information so that if a regular
5922 object refers to a symbol defined weakly in a dynamic object, the
5923 real symbol in the dynamic object is also put in the dynamic
5924 symbols; we also must arrange for both symbols to point to the
5925 same memory location. We could handle the general case of symbol
5926 aliasing, but a general symbol alias can only be generated in
5927 assembler code, handling it correctly would be very time
5928 consuming, and other ELF linkers don't handle general aliasing
5932 struct elf_link_hash_entry
**hpp
;
5933 struct elf_link_hash_entry
**hppend
;
5934 struct elf_link_hash_entry
**sorted_sym_hash
;
5935 struct elf_link_hash_entry
*h
;
5936 size_t sym_count
, amt
;
5938 /* Since we have to search the whole symbol list for each weak
5939 defined symbol, search time for N weak defined symbols will be
5940 O(N^2). Binary search will cut it down to O(NlogN). */
5941 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5942 sorted_sym_hash
= bfd_malloc (amt
);
5943 if (sorted_sym_hash
== NULL
)
5945 sym_hash
= sorted_sym_hash
;
5946 hpp
= elf_sym_hashes (abfd
);
5947 hppend
= hpp
+ extsymcount
;
5949 for (; hpp
< hppend
; hpp
++)
5953 && h
->root
.type
== bfd_link_hash_defined
5954 && !bed
->is_function_type (h
->type
))
5962 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5965 while (weaks
!= NULL
)
5967 struct elf_link_hash_entry
*hlook
;
5970 size_t i
, j
, idx
= 0;
5973 weaks
= hlook
->u
.alias
;
5974 hlook
->u
.alias
= NULL
;
5976 if (hlook
->root
.type
!= bfd_link_hash_defined
5977 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5980 slook
= hlook
->root
.u
.def
.section
;
5981 vlook
= hlook
->root
.u
.def
.value
;
5987 bfd_signed_vma vdiff
;
5989 h
= sorted_sym_hash
[idx
];
5990 vdiff
= vlook
- h
->root
.u
.def
.value
;
5997 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
6007 /* We didn't find a value/section match. */
6011 /* With multiple aliases, or when the weak symbol is already
6012 strongly defined, we have multiple matching symbols and
6013 the binary search above may land on any of them. Step
6014 one past the matching symbol(s). */
6017 h
= sorted_sym_hash
[idx
];
6018 if (h
->root
.u
.def
.section
!= slook
6019 || h
->root
.u
.def
.value
!= vlook
)
6023 /* Now look back over the aliases. Since we sorted by size
6024 as well as value and section, we'll choose the one with
6025 the largest size. */
6028 h
= sorted_sym_hash
[idx
];
6030 /* Stop if value or section doesn't match. */
6031 if (h
->root
.u
.def
.section
!= slook
6032 || h
->root
.u
.def
.value
!= vlook
)
6034 else if (h
!= hlook
)
6036 struct elf_link_hash_entry
*t
;
6039 hlook
->is_weakalias
= 1;
6041 if (t
->u
.alias
!= NULL
)
6042 while (t
->u
.alias
!= h
)
6046 /* If the weak definition is in the list of dynamic
6047 symbols, make sure the real definition is put
6049 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
6051 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6054 free (sorted_sym_hash
);
6059 /* If the real definition is in the list of dynamic
6060 symbols, make sure the weak definition is put
6061 there as well. If we don't do this, then the
6062 dynamic loader might not merge the entries for the
6063 real definition and the weak definition. */
6064 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
6066 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
6067 goto err_free_sym_hash
;
6074 free (sorted_sym_hash
);
6077 if (bed
->check_directives
6078 && !(*bed
->check_directives
) (abfd
, info
))
6081 /* If this is a non-traditional link, try to optimize the handling
6082 of the .stab/.stabstr sections. */
6084 && ! info
->traditional_format
6085 && is_elf_hash_table (&htab
->root
)
6086 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
6090 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
6091 if (stabstr
!= NULL
)
6093 bfd_size_type string_offset
= 0;
6096 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
6097 if (startswith (stab
->name
, ".stab")
6098 && (!stab
->name
[5] ||
6099 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
6100 && (stab
->flags
& SEC_MERGE
) == 0
6101 && !bfd_is_abs_section (stab
->output_section
))
6103 struct bfd_elf_section_data
*secdata
;
6105 secdata
= elf_section_data (stab
);
6106 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
6107 stabstr
, &secdata
->sec_info
,
6110 if (secdata
->sec_info
)
6111 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
6116 if (dynamic
&& add_needed
)
6118 /* Add this bfd to the loaded list. */
6119 struct elf_link_loaded_list
*n
;
6121 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
6125 n
->next
= htab
->dyn_loaded
;
6126 htab
->dyn_loaded
= n
;
6128 if (dynamic
&& !add_needed
6129 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
6130 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
6137 free (nondeflt_vers
);
6145 /* Return the linker hash table entry of a symbol that might be
6146 satisfied by an archive symbol. Return -1 on error. */
6148 struct bfd_link_hash_entry
*
6149 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
6150 struct bfd_link_info
*info
,
6153 struct bfd_link_hash_entry
*h
;
6157 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
6161 /* If this is a default version (the name contains @@), look up the
6162 symbol again with only one `@' as well as without the version.
6163 The effect is that references to the symbol with and without the
6164 version will be matched by the default symbol in the archive. */
6166 p
= strchr (name
, ELF_VER_CHR
);
6167 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
6169 /* Add this symbol to first hash if this archive has the first
6171 if (is_elf_hash_table (info
->hash
))
6172 elf_link_add_to_first_hash (abfd
, info
, name
, false);
6176 /* First check with only one `@'. */
6177 len
= strlen (name
);
6178 copy
= (char *) bfd_alloc (abfd
, len
);
6180 return (struct bfd_link_hash_entry
*) -1;
6182 first
= p
- name
+ 1;
6183 memcpy (copy
, name
, first
);
6184 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
6186 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
6189 /* We also need to check references to the symbol without the
6191 copy
[first
- 1] = '\0';
6192 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
6195 bfd_release (abfd
, copy
);
6199 /* Add symbols from an ELF archive file to the linker hash table. We
6200 don't use _bfd_generic_link_add_archive_symbols because we need to
6201 handle versioned symbols.
6203 Fortunately, ELF archive handling is simpler than that done by
6204 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6205 oddities. In ELF, if we find a symbol in the archive map, and the
6206 symbol is currently undefined, we know that we must pull in that
6209 Unfortunately, we do have to make multiple passes over the symbol
6210 table until nothing further is resolved. */
6213 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6216 unsigned char *included
= NULL
;
6220 const struct elf_backend_data
*bed
;
6221 struct bfd_link_hash_entry
* (*archive_symbol_lookup
)
6222 (bfd
*, struct bfd_link_info
*, const char *);
6224 if (! bfd_has_map (abfd
))
6226 /* An empty archive is a special case. */
6227 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
6229 bfd_set_error (bfd_error_no_armap
);
6233 /* Keep track of all symbols we know to be already defined, and all
6234 files we know to be already included. This is to speed up the
6235 second and subsequent passes. */
6236 c
= bfd_ardata (abfd
)->symdef_count
;
6239 amt
= c
* sizeof (*included
);
6240 included
= (unsigned char *) bfd_zmalloc (amt
);
6241 if (included
== NULL
)
6244 symdefs
= bfd_ardata (abfd
)->symdefs
;
6245 bed
= get_elf_backend_data (abfd
);
6246 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
6259 symdefend
= symdef
+ c
;
6260 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
6262 struct bfd_link_hash_entry
*h
;
6264 struct bfd_link_hash_entry
*undefs_tail
;
6269 if (symdef
->file_offset
== last
)
6275 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
6276 if (h
== (struct bfd_link_hash_entry
*) -1)
6282 if (h
->type
== bfd_link_hash_undefined
)
6284 if (is_elf_hash_table (info
->hash
))
6286 /* If the archive element has already been loaded then one
6287 of the symbols defined by that element might have been
6288 made undefined due to being in a discarded section. */
6289 if (((struct elf_link_hash_entry
*) h
)->indx
== -3)
6292 /* In the pre-LTO-plugin pass we must not mistakenly
6293 include this archive member if an earlier shared
6294 library defined this symbol. */
6295 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
6296 if (htab
->first_hash
)
6298 struct elf_link_first_hash_entry
*e
6299 = ((struct elf_link_first_hash_entry
*)
6300 bfd_hash_lookup (htab
->first_hash
, symdef
->name
,
6303 && (e
->abfd
->flags
& DYNAMIC
) != 0
6309 else if (h
->type
== bfd_link_hash_common
)
6311 /* We currently have a common symbol. The archive map contains
6312 a reference to this symbol, so we may want to include it. We
6313 only want to include it however, if this archive element
6314 contains a definition of the symbol, not just another common
6317 Unfortunately some archivers (including GNU ar) will put
6318 declarations of common symbols into their archive maps, as
6319 well as real definitions, so we cannot just go by the archive
6320 map alone. Instead we must read in the element's symbol
6321 table and check that to see what kind of symbol definition
6323 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
6328 if (h
->type
!= bfd_link_hash_undefweak
)
6329 /* Symbol must be defined. Don't check it again. */
6332 if (!is_elf_hash_table (info
->hash
))
6334 struct elf_link_hash_entry
*eh
6335 = (struct elf_link_hash_entry
*) h
;
6336 /* Ignore the archive if the symbol isn't referenced by a
6337 regular object or isn't defined in a shared object. */
6338 if (!eh
->ref_regular
|| !eh
->def_dynamic
)
6340 /* Ignore the dynamic definition if symbol is first
6341 defined in this archive. */
6342 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
6343 if (htab
->first_hash
== NULL
)
6345 struct elf_link_first_hash_entry
*e
6346 = ((struct elf_link_first_hash_entry
*)
6347 bfd_hash_lookup (htab
->first_hash
, symdef
->name
,
6349 if (e
== NULL
|| e
->abfd
!= abfd
)
6353 /* We need to include this archive member. */
6354 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
,
6356 if (element
== NULL
)
6359 if (! bfd_check_format (element
, bfd_object
))
6362 undefs_tail
= info
->hash
->undefs_tail
;
6364 if (!(*info
->callbacks
6365 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
6367 if (!bfd_link_add_symbols (element
, info
))
6370 /* If there are any new undefined symbols, we need to make
6371 another pass through the archive in order to see whether
6372 they can be defined. FIXME: This isn't perfect, because
6373 common symbols wind up on undefs_tail and because an
6374 undefined symbol which is defined later on in this pass
6375 does not require another pass. This isn't a bug, but it
6376 does make the code less efficient than it could be. */
6377 if (undefs_tail
!= info
->hash
->undefs_tail
)
6380 /* Look backward to mark all symbols from this object file
6381 which we have already seen in this pass. */
6385 included
[mark
] = true;
6390 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
6392 /* We mark subsequent symbols from this object file as we go
6393 on through the loop. */
6394 last
= symdef
->file_offset
;
6407 /* Given an ELF BFD, add symbols to the global hash table as
6411 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6413 switch (bfd_get_format (abfd
))
6416 return elf_link_add_object_symbols (abfd
, info
);
6418 return elf_link_add_archive_symbols (abfd
, info
);
6420 bfd_set_error (bfd_error_wrong_format
);
6425 struct hash_codes_info
6427 unsigned long *hashcodes
;
6431 /* This function will be called though elf_link_hash_traverse to store
6432 all hash value of the exported symbols in an array. */
6435 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6437 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
6442 /* Ignore indirect symbols. These are added by the versioning code. */
6443 if (h
->dynindx
== -1)
6446 name
= h
->root
.root
.string
;
6447 if (h
->versioned
>= versioned
)
6449 char *p
= strchr (name
, ELF_VER_CHR
);
6452 alc
= (char *) bfd_malloc (p
- name
+ 1);
6458 memcpy (alc
, name
, p
- name
);
6459 alc
[p
- name
] = '\0';
6464 /* Compute the hash value. */
6465 ha
= bfd_elf_hash (name
);
6467 /* Store the found hash value in the array given as the argument. */
6468 *(inf
->hashcodes
)++ = ha
;
6470 /* And store it in the struct so that we can put it in the hash table
6472 h
->u
.elf_hash_value
= ha
;
6478 struct collect_gnu_hash_codes
6481 const struct elf_backend_data
*bed
;
6482 unsigned long int nsyms
;
6483 unsigned long int maskbits
;
6484 unsigned long int *hashcodes
;
6485 unsigned long int *hashval
;
6486 unsigned long int *indx
;
6487 unsigned long int *counts
;
6491 long int min_dynindx
;
6492 unsigned long int bucketcount
;
6493 unsigned long int symindx
;
6494 long int local_indx
;
6495 long int shift1
, shift2
;
6496 unsigned long int mask
;
6500 /* This function will be called though elf_link_hash_traverse to store
6501 all hash value of the exported symbols in an array. */
6504 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6506 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6511 /* Ignore indirect symbols. These are added by the versioning code. */
6512 if (h
->dynindx
== -1)
6515 /* Ignore also local symbols and undefined symbols. */
6516 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6519 name
= h
->root
.root
.string
;
6520 if (h
->versioned
>= versioned
)
6522 char *p
= strchr (name
, ELF_VER_CHR
);
6525 alc
= (char *) bfd_malloc (p
- name
+ 1);
6531 memcpy (alc
, name
, p
- name
);
6532 alc
[p
- name
] = '\0';
6537 /* Compute the hash value. */
6538 ha
= bfd_elf_gnu_hash (name
);
6540 /* Store the found hash value in the array for compute_bucket_count,
6541 and also for .dynsym reordering purposes. */
6542 s
->hashcodes
[s
->nsyms
] = ha
;
6543 s
->hashval
[h
->dynindx
] = ha
;
6545 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6546 s
->min_dynindx
= h
->dynindx
;
6552 /* This function will be called though elf_link_hash_traverse to do
6553 final dynamic symbol renumbering in case of .gnu.hash.
6554 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6555 to the translation table. */
6558 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6560 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6561 unsigned long int bucket
;
6562 unsigned long int val
;
6564 /* Ignore indirect symbols. */
6565 if (h
->dynindx
== -1)
6568 /* Ignore also local symbols and undefined symbols. */
6569 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6571 if (h
->dynindx
>= s
->min_dynindx
)
6573 if (s
->bed
->record_xhash_symbol
!= NULL
)
6575 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6579 h
->dynindx
= s
->local_indx
++;
6584 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6585 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6586 & ((s
->maskbits
>> s
->shift1
) - 1);
6587 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6589 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6590 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6591 if (s
->counts
[bucket
] == 1)
6592 /* Last element terminates the chain. */
6594 bfd_put_32 (s
->output_bfd
, val
,
6595 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6596 --s
->counts
[bucket
];
6597 if (s
->bed
->record_xhash_symbol
!= NULL
)
6599 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6601 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6604 h
->dynindx
= s
->indx
[bucket
]++;
6608 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6611 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6613 return !(h
->forced_local
6614 || h
->root
.type
== bfd_link_hash_undefined
6615 || h
->root
.type
== bfd_link_hash_undefweak
6616 || ((h
->root
.type
== bfd_link_hash_defined
6617 || h
->root
.type
== bfd_link_hash_defweak
)
6618 && h
->root
.u
.def
.section
->output_section
== NULL
));
6621 /* Array used to determine the number of hash table buckets to use
6622 based on the number of symbols there are. If there are fewer than
6623 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6624 fewer than 37 we use 17 buckets, and so forth. We never use more
6625 than 32771 buckets. */
6627 static const size_t elf_buckets
[] =
6629 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6633 /* Compute bucket count for hashing table. We do not use a static set
6634 of possible tables sizes anymore. Instead we determine for all
6635 possible reasonable sizes of the table the outcome (i.e., the
6636 number of collisions etc) and choose the best solution. The
6637 weighting functions are not too simple to allow the table to grow
6638 without bounds. Instead one of the weighting factors is the size.
6639 Therefore the result is always a good payoff between few collisions
6640 (= short chain lengths) and table size. */
6642 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6643 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6644 unsigned long int nsyms
,
6647 size_t best_size
= 0;
6648 unsigned long int i
;
6654 uint64_t best_chlen
= ~((uint64_t) 0);
6655 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6656 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6657 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6658 unsigned long int *counts
;
6660 unsigned int no_improvement_count
= 0;
6662 /* Possible optimization parameters: if we have NSYMS symbols we say
6663 that the hashing table must at least have NSYMS/4 and at most
6665 minsize
= nsyms
/ 4;
6668 best_size
= maxsize
= nsyms
* 2;
6673 if ((best_size
& 31) == 0)
6677 /* Create array where we count the collisions in. We must use bfd_malloc
6678 since the size could be large. */
6680 amt
*= sizeof (unsigned long int);
6681 counts
= (unsigned long int *) bfd_malloc (amt
);
6685 /* Compute the "optimal" size for the hash table. The criteria is a
6686 minimal chain length. The minor criteria is (of course) the size
6688 for (i
= minsize
; i
< maxsize
; ++i
)
6690 /* Walk through the array of hashcodes and count the collisions. */
6692 unsigned long int j
;
6693 unsigned long int fact
;
6695 if (gnu_hash
&& (i
& 31) == 0)
6698 memset (counts
, '\0', i
* sizeof (unsigned long int));
6700 /* Determine how often each hash bucket is used. */
6701 for (j
= 0; j
< nsyms
; ++j
)
6702 ++counts
[hashcodes
[j
] % i
];
6704 /* For the weight function we need some information about the
6705 pagesize on the target. This is information need not be 100%
6706 accurate. Since this information is not available (so far) we
6707 define it here to a reasonable default value. If it is crucial
6708 to have a better value some day simply define this value. */
6709 # ifndef BFD_TARGET_PAGESIZE
6710 # define BFD_TARGET_PAGESIZE (4096)
6713 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6715 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6718 /* Variant 1: optimize for short chains. We add the squares
6719 of all the chain lengths (which favors many small chain
6720 over a few long chains). */
6721 for (j
= 0; j
< i
; ++j
)
6722 max
+= counts
[j
] * counts
[j
];
6724 /* This adds penalties for the overall size of the table. */
6725 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6728 /* Variant 2: Optimize a lot more for small table. Here we
6729 also add squares of the size but we also add penalties for
6730 empty slots (the +1 term). */
6731 for (j
= 0; j
< i
; ++j
)
6732 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6734 /* The overall size of the table is considered, but not as
6735 strong as in variant 1, where it is squared. */
6736 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6740 /* Compare with current best results. */
6741 if (max
< best_chlen
)
6745 no_improvement_count
= 0;
6747 /* PR 11843: Avoid futile long searches for the best bucket size
6748 when there are a large number of symbols. */
6749 else if (++no_improvement_count
== 100)
6757 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6759 best_size
= elf_buckets
[i
];
6760 if (nsyms
< elf_buckets
[i
+ 1])
6763 if (gnu_hash
&& best_size
< 2)
6770 /* Size any SHT_GROUP section for ld -r. */
6773 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6778 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6779 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6780 && (s
= ibfd
->sections
) != NULL
6781 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6782 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6787 /* Set a default stack segment size. The value in INFO wins. If it
6788 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6789 undefined it is initialized. */
6792 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6793 struct bfd_link_info
*info
,
6794 const char *legacy_symbol
,
6795 bfd_vma default_size
)
6797 struct elf_link_hash_entry
*h
= NULL
;
6799 /* Look for legacy symbol. */
6801 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6802 false, false, false);
6803 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6804 || h
->root
.type
== bfd_link_hash_defweak
)
6806 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6808 /* The symbol has no type if specified on the command line. */
6809 h
->type
= STT_OBJECT
;
6810 if (info
->stacksize
)
6811 /* xgettext:c-format */
6812 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6813 output_bfd
, legacy_symbol
);
6814 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6815 /* xgettext:c-format */
6816 _bfd_error_handler (_("%pB: %s not absolute"),
6817 output_bfd
, legacy_symbol
);
6819 info
->stacksize
= h
->root
.u
.def
.value
;
6822 if (!info
->stacksize
)
6823 /* If the user didn't set a size, or explicitly inhibit the
6824 size, set it now. */
6825 info
->stacksize
= default_size
;
6827 /* Provide the legacy symbol, if it is referenced. */
6828 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6829 || h
->root
.type
== bfd_link_hash_undefweak
))
6831 struct bfd_link_hash_entry
*bh
= NULL
;
6833 if (!(_bfd_generic_link_add_one_symbol
6834 (info
, output_bfd
, legacy_symbol
,
6835 BSF_GLOBAL
, bfd_abs_section_ptr
,
6836 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6837 NULL
, false, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6840 h
= (struct elf_link_hash_entry
*) bh
;
6842 h
->type
= STT_OBJECT
;
6848 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6850 struct elf_gc_sweep_symbol_info
6852 struct bfd_link_info
*info
;
6853 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6858 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6861 && (((h
->root
.type
== bfd_link_hash_defined
6862 || h
->root
.type
== bfd_link_hash_defweak
)
6863 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6864 && h
->root
.u
.def
.section
->gc_mark
))
6865 || h
->root
.type
== bfd_link_hash_undefined
6866 || h
->root
.type
== bfd_link_hash_undefweak
))
6868 struct elf_gc_sweep_symbol_info
*inf
;
6870 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6871 (*inf
->hide_symbol
) (inf
->info
, h
, true);
6874 h
->ref_regular_nonweak
= 0;
6880 /* Set up the sizes and contents of the ELF dynamic sections. This is
6881 called by the ELF linker emulation before_allocation routine. We
6882 must set the sizes of the sections before the linker sets the
6883 addresses of the various sections. */
6886 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6889 const char *filter_shlib
,
6891 const char *depaudit
,
6892 const char * const *auxiliary_filters
,
6893 struct bfd_link_info
*info
,
6894 asection
**sinterpptr
)
6897 const struct elf_backend_data
*bed
;
6901 if (!is_elf_hash_table (info
->hash
))
6904 /* Any syms created from now on start with -1 in
6905 got.refcount/offset and plt.refcount/offset. */
6906 elf_hash_table (info
)->init_got_refcount
6907 = elf_hash_table (info
)->init_got_offset
;
6908 elf_hash_table (info
)->init_plt_refcount
6909 = elf_hash_table (info
)->init_plt_offset
;
6911 bed
= get_elf_backend_data (output_bfd
);
6913 /* The backend may have to create some sections regardless of whether
6914 we're dynamic or not. */
6915 if (bed
->elf_backend_early_size_sections
6916 && !bed
->elf_backend_early_size_sections (output_bfd
, info
))
6919 dynobj
= elf_hash_table (info
)->dynobj
;
6921 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6923 struct bfd_elf_version_tree
*verdefs
;
6924 struct elf_info_failed asvinfo
;
6925 struct bfd_elf_version_tree
*t
;
6926 struct bfd_elf_version_expr
*d
;
6930 /* If we are supposed to export all symbols into the dynamic symbol
6931 table (this is not the normal case), then do so. */
6932 if (info
->export_dynamic
6933 || (bfd_link_executable (info
) && info
->dynamic
))
6935 struct elf_info_failed eif
;
6939 elf_link_hash_traverse (elf_hash_table (info
),
6940 _bfd_elf_export_symbol
,
6948 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6950 if (soname_indx
== (size_t) -1
6951 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6955 soname_indx
= (size_t) -1;
6957 /* Make all global versions with definition. */
6958 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6959 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6960 if (!d
->symver
&& d
->literal
)
6962 const char *verstr
, *name
;
6963 size_t namelen
, verlen
, newlen
;
6964 char *newname
, *p
, leading_char
;
6965 struct elf_link_hash_entry
*newh
;
6967 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6969 namelen
= strlen (name
) + (leading_char
!= '\0');
6971 verlen
= strlen (verstr
);
6972 newlen
= namelen
+ verlen
+ 3;
6974 newname
= (char *) bfd_malloc (newlen
);
6975 if (newname
== NULL
)
6977 newname
[0] = leading_char
;
6978 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6980 /* Check the hidden versioned definition. */
6981 p
= newname
+ namelen
;
6983 memcpy (p
, verstr
, verlen
+ 1);
6984 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6985 newname
, false, false,
6988 || (newh
->root
.type
!= bfd_link_hash_defined
6989 && newh
->root
.type
!= bfd_link_hash_defweak
))
6991 /* Check the default versioned definition. */
6993 memcpy (p
, verstr
, verlen
+ 1);
6994 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6995 newname
, false, false,
7000 /* Mark this version if there is a definition and it is
7001 not defined in a shared object. */
7003 && !newh
->def_dynamic
7004 && (newh
->root
.type
== bfd_link_hash_defined
7005 || newh
->root
.type
== bfd_link_hash_defweak
))
7009 /* Attach all the symbols to their version information. */
7010 asvinfo
.info
= info
;
7011 asvinfo
.failed
= false;
7013 elf_link_hash_traverse (elf_hash_table (info
),
7014 _bfd_elf_link_assign_sym_version
,
7019 if (!info
->allow_undefined_version
)
7021 /* Check if all global versions have a definition. */
7022 bool all_defined
= true;
7023 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
7024 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
7025 if (d
->literal
&& !d
->symver
&& !d
->script
)
7028 (_("%s: undefined version: %s"),
7029 d
->pattern
, t
->name
);
7030 all_defined
= false;
7035 bfd_set_error (bfd_error_bad_value
);
7040 /* Set up the version definition section. */
7041 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
7042 BFD_ASSERT (s
!= NULL
);
7044 /* We may have created additional version definitions if we are
7045 just linking a regular application. */
7046 verdefs
= info
->version_info
;
7048 /* Skip anonymous version tag. */
7049 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
7050 verdefs
= verdefs
->next
;
7052 if (verdefs
== NULL
&& !info
->create_default_symver
)
7053 s
->flags
|= SEC_EXCLUDE
;
7059 Elf_Internal_Verdef def
;
7060 Elf_Internal_Verdaux defaux
;
7061 struct bfd_link_hash_entry
*bh
;
7062 struct elf_link_hash_entry
*h
;
7068 /* Make space for the base version. */
7069 size
+= sizeof (Elf_External_Verdef
);
7070 size
+= sizeof (Elf_External_Verdaux
);
7073 /* Make space for the default version. */
7074 if (info
->create_default_symver
)
7076 size
+= sizeof (Elf_External_Verdef
);
7080 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
7082 struct bfd_elf_version_deps
*n
;
7084 /* Don't emit base version twice. */
7088 size
+= sizeof (Elf_External_Verdef
);
7089 size
+= sizeof (Elf_External_Verdaux
);
7092 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
7093 size
+= sizeof (Elf_External_Verdaux
);
7097 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7098 if (s
->contents
== NULL
&& s
->size
!= 0)
7102 /* Fill in the version definition section. */
7106 def
.vd_version
= VER_DEF_CURRENT
;
7107 def
.vd_flags
= VER_FLG_BASE
;
7110 if (info
->create_default_symver
)
7112 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
7113 def
.vd_next
= sizeof (Elf_External_Verdef
);
7117 def
.vd_aux
= sizeof (Elf_External_Verdef
);
7118 def
.vd_next
= (sizeof (Elf_External_Verdef
)
7119 + sizeof (Elf_External_Verdaux
));
7122 if (soname_indx
!= (size_t) -1)
7124 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
7126 def
.vd_hash
= bfd_elf_hash (soname
);
7127 defaux
.vda_name
= soname_indx
;
7134 name
= lbasename (bfd_get_filename (output_bfd
));
7135 def
.vd_hash
= bfd_elf_hash (name
);
7136 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7138 if (indx
== (size_t) -1)
7140 defaux
.vda_name
= indx
;
7142 defaux
.vda_next
= 0;
7144 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
7145 (Elf_External_Verdef
*) p
);
7146 p
+= sizeof (Elf_External_Verdef
);
7147 if (info
->create_default_symver
)
7149 /* Add a symbol representing this version. */
7151 if (! (_bfd_generic_link_add_one_symbol
7152 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
7154 get_elf_backend_data (dynobj
)->collect
, &bh
)))
7156 h
= (struct elf_link_hash_entry
*) bh
;
7159 h
->type
= STT_OBJECT
;
7160 h
->verinfo
.vertree
= NULL
;
7162 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
7165 /* Create a duplicate of the base version with the same
7166 aux block, but different flags. */
7169 def
.vd_aux
= sizeof (Elf_External_Verdef
);
7171 def
.vd_next
= (sizeof (Elf_External_Verdef
)
7172 + sizeof (Elf_External_Verdaux
));
7175 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
7176 (Elf_External_Verdef
*) p
);
7177 p
+= sizeof (Elf_External_Verdef
);
7179 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7180 (Elf_External_Verdaux
*) p
);
7181 p
+= sizeof (Elf_External_Verdaux
);
7183 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
7186 struct bfd_elf_version_deps
*n
;
7188 /* Don't emit the base version twice. */
7193 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
7196 /* Add a symbol representing this version. */
7198 if (! (_bfd_generic_link_add_one_symbol
7199 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
7201 get_elf_backend_data (dynobj
)->collect
, &bh
)))
7203 h
= (struct elf_link_hash_entry
*) bh
;
7206 h
->type
= STT_OBJECT
;
7207 h
->verinfo
.vertree
= t
;
7209 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
7212 def
.vd_version
= VER_DEF_CURRENT
;
7214 if (t
->globals
.list
== NULL
7215 && t
->locals
.list
== NULL
7217 def
.vd_flags
|= VER_FLG_WEAK
;
7218 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
7219 def
.vd_cnt
= cdeps
+ 1;
7220 def
.vd_hash
= bfd_elf_hash (t
->name
);
7221 def
.vd_aux
= sizeof (Elf_External_Verdef
);
7224 /* If a basever node is next, it *must* be the last node in
7225 the chain, otherwise Verdef construction breaks. */
7226 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
7227 BFD_ASSERT (t
->next
->next
== NULL
);
7229 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
7230 def
.vd_next
= (sizeof (Elf_External_Verdef
)
7231 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
7233 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
7234 (Elf_External_Verdef
*) p
);
7235 p
+= sizeof (Elf_External_Verdef
);
7237 defaux
.vda_name
= h
->dynstr_index
;
7238 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
7240 defaux
.vda_next
= 0;
7241 if (t
->deps
!= NULL
)
7242 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
7243 t
->name_indx
= defaux
.vda_name
;
7245 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7246 (Elf_External_Verdaux
*) p
);
7247 p
+= sizeof (Elf_External_Verdaux
);
7249 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
7251 if (n
->version_needed
== NULL
)
7253 /* This can happen if there was an error in the
7255 defaux
.vda_name
= 0;
7259 defaux
.vda_name
= n
->version_needed
->name_indx
;
7260 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
7263 if (n
->next
== NULL
)
7264 defaux
.vda_next
= 0;
7266 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
7268 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7269 (Elf_External_Verdaux
*) p
);
7270 p
+= sizeof (Elf_External_Verdaux
);
7274 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
7278 if (info
->gc_sections
&& bed
->can_gc_sections
)
7280 struct elf_gc_sweep_symbol_info sweep_info
;
7282 /* Remove the symbols that were in the swept sections from the
7283 dynamic symbol table. */
7284 sweep_info
.info
= info
;
7285 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
7286 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
7290 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7293 struct elf_find_verdep_info sinfo
;
7295 /* Work out the size of the version reference section. */
7297 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
7298 BFD_ASSERT (s
!= NULL
);
7301 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
7302 if (sinfo
.vers
== 0)
7304 sinfo
.failed
= false;
7306 elf_link_hash_traverse (elf_hash_table (info
),
7307 _bfd_elf_link_find_version_dependencies
,
7312 bed
->elf_backend_add_glibc_version_dependency (&sinfo
);
7316 if (elf_tdata (output_bfd
)->verref
== NULL
)
7317 s
->flags
|= SEC_EXCLUDE
;
7320 Elf_Internal_Verneed
*vn
;
7325 /* Build the version dependency section. */
7328 for (vn
= elf_tdata (output_bfd
)->verref
;
7330 vn
= vn
->vn_nextref
)
7332 Elf_Internal_Vernaux
*a
;
7334 size
+= sizeof (Elf_External_Verneed
);
7336 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7337 size
+= sizeof (Elf_External_Vernaux
);
7341 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7342 if (s
->contents
== NULL
)
7347 for (vn
= elf_tdata (output_bfd
)->verref
;
7349 vn
= vn
->vn_nextref
)
7352 Elf_Internal_Vernaux
*a
;
7356 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7359 vn
->vn_version
= VER_NEED_CURRENT
;
7361 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7362 elf_dt_name (vn
->vn_bfd
) != NULL
7363 ? elf_dt_name (vn
->vn_bfd
)
7364 : lbasename (bfd_get_filename
7367 if (indx
== (size_t) -1)
7370 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
7371 if (vn
->vn_nextref
== NULL
)
7374 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
7375 + caux
* sizeof (Elf_External_Vernaux
));
7377 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
7378 (Elf_External_Verneed
*) p
);
7379 p
+= sizeof (Elf_External_Verneed
);
7381 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7383 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
7384 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7385 a
->vna_nodename
, false);
7386 if (indx
== (size_t) -1)
7389 if (a
->vna_nextptr
== NULL
)
7392 a
->vna_next
= sizeof (Elf_External_Vernaux
);
7394 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
7395 (Elf_External_Vernaux
*) p
);
7396 p
+= sizeof (Elf_External_Vernaux
);
7400 elf_tdata (output_bfd
)->cverrefs
= crefs
;
7404 if (bfd_link_relocatable (info
)
7405 && !_bfd_elf_size_group_sections (info
))
7408 /* Determine any GNU_STACK segment requirements, after the backend
7409 has had a chance to set a default segment size. */
7410 if (info
->execstack
)
7412 /* If the user has explicitly requested warnings, then generate one even
7413 though the choice is the result of another command line option. */
7414 if (info
->warn_execstack
== 1)
7416 if (info
->error_execstack
)
7420 error: creating an executable stack because of -z execstack command line option"));
7426 warning: enabling an executable stack because of -z execstack command line option"));
7429 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
7431 else if (info
->noexecstack
)
7432 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
7436 asection
*notesec
= NULL
;
7437 bfd
*noteobj
= NULL
;
7438 bfd
*emptyobj
= NULL
;
7441 for (inputobj
= info
->input_bfds
;
7443 inputobj
= inputobj
->link
.next
)
7448 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
7450 s
= inputobj
->sections
;
7451 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
7454 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
7458 if (s
->flags
& SEC_CODE
)
7462 /* There is no point in scanning the remaining bfds. */
7466 else if (bed
->default_execstack
&& info
->default_execstack
)
7469 emptyobj
= inputobj
;
7473 if (notesec
|| info
->stacksize
> 0)
7477 if (info
->warn_execstack
!= 0)
7479 /* PR 29072: Because an executable stack is a serious
7480 security risk, make sure that the user knows that it is
7481 being enabled despite the fact that it was not requested
7482 on the command line. */
7485 if (info
->error_execstack
)
7487 _bfd_error_handler (_("\
7488 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7489 bfd_get_filename (noteobj
));
7493 _bfd_error_handler (_("\
7494 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7495 bfd_get_filename (noteobj
));
7499 if (info
->error_execstack
)
7501 _bfd_error_handler (_("\
7502 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7503 bfd_get_filename (emptyobj
));
7507 _bfd_error_handler (_("\
7508 warning: %s: missing .note.GNU-stack section implies executable stack"),
7509 bfd_get_filename (emptyobj
));
7510 _bfd_error_handler (_("\
7511 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7515 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
7518 if (notesec
&& exec
&& bfd_link_relocatable (info
)
7519 && notesec
->output_section
!= bfd_abs_section_ptr
)
7520 notesec
->output_section
->flags
|= SEC_CODE
;
7523 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7525 struct elf_info_failed eif
;
7526 struct elf_link_hash_entry
*h
;
7530 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7531 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7535 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7537 info
->flags
|= DF_SYMBOLIC
;
7545 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7547 if (indx
== (size_t) -1)
7550 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7551 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7555 if (filter_shlib
!= NULL
)
7559 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7560 filter_shlib
, true);
7561 if (indx
== (size_t) -1
7562 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7566 if (auxiliary_filters
!= NULL
)
7568 const char * const *p
;
7570 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7574 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7576 if (indx
== (size_t) -1
7577 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7586 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7588 if (indx
== (size_t) -1
7589 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7593 if (depaudit
!= NULL
)
7597 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7599 if (indx
== (size_t) -1
7600 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7607 /* Find all symbols which were defined in a dynamic object and make
7608 the backend pick a reasonable value for them. */
7609 elf_link_hash_traverse (elf_hash_table (info
),
7610 _bfd_elf_adjust_dynamic_symbol
,
7615 /* Add some entries to the .dynamic section. We fill in some of the
7616 values later, in bfd_elf_final_link, but we must add the entries
7617 now so that we know the final size of the .dynamic section. */
7619 /* If there are initialization and/or finalization functions to
7620 call then add the corresponding DT_INIT/DT_FINI entries. */
7621 h
= (info
->init_function
7622 ? elf_link_hash_lookup (elf_hash_table (info
),
7623 info
->init_function
, false,
7630 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7633 h
= (info
->fini_function
7634 ? elf_link_hash_lookup (elf_hash_table (info
),
7635 info
->fini_function
, false,
7642 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7646 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7647 if (s
!= NULL
&& s
->linker_has_input
)
7649 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7650 if (! bfd_link_executable (info
))
7655 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7656 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7657 && (o
= sub
->sections
) != NULL
7658 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7659 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7660 if (elf_section_data (o
)->this_hdr
.sh_type
7661 == SHT_PREINIT_ARRAY
)
7664 (_("%pB: .preinit_array section is not allowed in DSO"),
7669 bfd_set_error (bfd_error_nonrepresentable_section
);
7673 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7674 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7677 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7678 if (s
!= NULL
&& s
->linker_has_input
)
7680 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7681 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7684 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7685 if (s
!= NULL
&& s
->linker_has_input
)
7687 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7688 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7692 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7693 /* If .dynstr is excluded from the link, we don't want any of
7694 these tags. Strictly, we should be checking each section
7695 individually; This quick check covers for the case where
7696 someone does a /DISCARD/ : { *(*) }. */
7697 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7699 bfd_size_type strsize
;
7701 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7702 if ((info
->emit_hash
7703 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7704 || (info
->emit_gnu_hash
7705 && (bed
->record_xhash_symbol
== NULL
7706 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7707 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7708 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7709 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7710 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7712 || (info
->gnu_flags_1
7713 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_FLAGS_1
,
7714 info
->gnu_flags_1
)))
7719 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7722 /* The backend must work out the sizes of all the other dynamic
7724 if (bed
->elf_backend_late_size_sections
!= NULL
7725 && !bed
->elf_backend_late_size_sections (output_bfd
, info
))
7728 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7730 if (elf_tdata (output_bfd
)->cverdefs
)
7732 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7734 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7735 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7739 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7741 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7744 else if (info
->flags
& DF_BIND_NOW
)
7746 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7752 if (bfd_link_executable (info
))
7753 info
->flags_1
&= ~ (DF_1_INITFIRST
7756 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7760 if (elf_tdata (output_bfd
)->cverrefs
)
7762 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7764 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7765 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7769 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7770 && elf_tdata (output_bfd
)->cverdefs
== 0)
7771 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7775 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7776 s
->flags
|= SEC_EXCLUDE
;
7782 /* Find the first non-excluded output section. We'll use its
7783 section symbol for some emitted relocs. */
7785 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7788 asection
*found
= NULL
;
7790 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7791 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7792 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7795 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7798 elf_hash_table (info
)->text_index_section
= found
;
7801 /* Find two non-excluded output sections, one for code, one for data.
7802 We'll use their section symbols for some emitted relocs. */
7804 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7807 asection
*found
= NULL
;
7809 /* Data first, since setting text_index_section changes
7810 _bfd_elf_omit_section_dynsym_default. */
7811 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7812 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7813 && !(s
->flags
& SEC_READONLY
)
7814 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7817 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7820 elf_hash_table (info
)->data_index_section
= found
;
7822 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7823 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7824 && (s
->flags
& SEC_READONLY
)
7825 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7830 elf_hash_table (info
)->text_index_section
= found
;
7833 #define GNU_HASH_SECTION_NAME(bed) \
7834 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7837 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7839 const struct elf_backend_data
*bed
;
7840 unsigned long section_sym_count
;
7841 bfd_size_type dynsymcount
= 0;
7843 if (!is_elf_hash_table (info
->hash
))
7846 bed
= get_elf_backend_data (output_bfd
);
7847 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7849 /* Assign dynsym indices. In a shared library we generate a section
7850 symbol for each output section, which come first. Next come all
7851 of the back-end allocated local dynamic syms, followed by the rest
7852 of the global symbols.
7854 This is usually not needed for static binaries, however backends
7855 can request to always do it, e.g. the MIPS backend uses dynamic
7856 symbol counts to lay out GOT, which will be produced in the
7857 presence of GOT relocations even in static binaries (holding fixed
7858 data in that case, to satisfy those relocations). */
7860 if (elf_hash_table (info
)->dynamic_sections_created
7861 || bed
->always_renumber_dynsyms
)
7862 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7863 §ion_sym_count
);
7865 if (elf_hash_table (info
)->dynamic_sections_created
)
7869 unsigned int dtagcount
;
7871 dynobj
= elf_hash_table (info
)->dynobj
;
7873 /* Work out the size of the symbol version section. */
7874 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7875 BFD_ASSERT (s
!= NULL
);
7876 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7878 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7879 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7880 if (s
->contents
== NULL
)
7884 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7888 /* Set the size of the .dynsym and .hash sections. We counted
7889 the number of dynamic symbols in elf_link_add_object_symbols.
7890 We will build the contents of .dynsym and .hash when we build
7891 the final symbol table, because until then we do not know the
7892 correct value to give the symbols. We built the .dynstr
7893 section as we went along in elf_link_add_object_symbols. */
7894 s
= elf_hash_table (info
)->dynsym
;
7895 BFD_ASSERT (s
!= NULL
);
7896 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7898 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7899 if (s
->contents
== NULL
)
7903 /* The first entry in .dynsym is a dummy symbol. Clear all the
7904 section syms, in case we don't output them all. */
7905 ++section_sym_count
;
7906 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7908 elf_hash_table (info
)->bucketcount
= 0;
7910 /* Compute the size of the hashing table. As a side effect this
7911 computes the hash values for all the names we export. */
7912 if (info
->emit_hash
)
7914 unsigned long int *hashcodes
;
7915 struct hash_codes_info hashinf
;
7917 unsigned long int nsyms
;
7919 size_t hash_entry_size
;
7921 /* Compute the hash values for all exported symbols. At the same
7922 time store the values in an array so that we could use them for
7924 amt
= dynsymcount
* sizeof (unsigned long int);
7925 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7926 if (hashcodes
== NULL
)
7928 hashinf
.hashcodes
= hashcodes
;
7929 hashinf
.error
= false;
7931 /* Put all hash values in HASHCODES. */
7932 elf_link_hash_traverse (elf_hash_table (info
),
7933 elf_collect_hash_codes
, &hashinf
);
7940 nsyms
= hashinf
.hashcodes
- hashcodes
;
7942 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7945 if (bucketcount
== 0 && nsyms
> 0)
7948 elf_hash_table (info
)->bucketcount
= bucketcount
;
7950 s
= bfd_get_linker_section (dynobj
, ".hash");
7951 BFD_ASSERT (s
!= NULL
);
7952 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7953 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7954 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7955 if (s
->contents
== NULL
)
7959 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7960 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7961 s
->contents
+ hash_entry_size
);
7964 if (info
->emit_gnu_hash
)
7967 unsigned char *contents
;
7968 struct collect_gnu_hash_codes cinfo
;
7972 memset (&cinfo
, 0, sizeof (cinfo
));
7974 /* Compute the hash values for all exported symbols. At the same
7975 time store the values in an array so that we could use them for
7977 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7978 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7979 if (cinfo
.hashcodes
== NULL
)
7982 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7983 cinfo
.min_dynindx
= -1;
7984 cinfo
.output_bfd
= output_bfd
;
7987 /* Put all hash values in HASHCODES. */
7988 elf_link_hash_traverse (elf_hash_table (info
),
7989 elf_collect_gnu_hash_codes
, &cinfo
);
7992 free (cinfo
.hashcodes
);
7997 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7999 if (bucketcount
== 0)
8001 free (cinfo
.hashcodes
);
8005 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
8006 BFD_ASSERT (s
!= NULL
);
8008 if (cinfo
.nsyms
== 0)
8010 /* Empty .gnu.hash or .MIPS.xhash section is special. */
8011 BFD_ASSERT (cinfo
.min_dynindx
== -1);
8012 free (cinfo
.hashcodes
);
8013 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
8014 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
8015 if (contents
== NULL
)
8017 s
->contents
= contents
;
8019 /* 1 empty bucket. */
8020 bfd_put_32 (output_bfd
, 1, contents
);
8021 /* SYMIDX above the special symbol 0. */
8022 bfd_put_32 (output_bfd
, 1, contents
+ 4);
8023 /* Just one word for bitmask. */
8024 bfd_put_32 (output_bfd
, 1, contents
+ 8);
8025 /* Only hash fn bloom filter. */
8026 bfd_put_32 (output_bfd
, 0, contents
+ 12);
8027 /* No hashes are valid - empty bitmask. */
8028 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
8029 /* No hashes in the only bucket. */
8030 bfd_put_32 (output_bfd
, 0,
8031 contents
+ 16 + bed
->s
->arch_size
/ 8);
8035 unsigned long int maskwords
, maskbitslog2
, x
;
8036 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
8040 while ((x
>>= 1) != 0)
8042 if (maskbitslog2
< 3)
8044 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
8045 maskbitslog2
= maskbitslog2
+ 3;
8047 maskbitslog2
= maskbitslog2
+ 2;
8048 if (bed
->s
->arch_size
== 64)
8050 if (maskbitslog2
== 5)
8056 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
8057 cinfo
.shift2
= maskbitslog2
;
8058 cinfo
.maskbits
= 1 << maskbitslog2
;
8059 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
8060 amt
= bucketcount
* sizeof (unsigned long int) * 2;
8061 amt
+= maskwords
* sizeof (bfd_vma
);
8062 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
8063 if (cinfo
.bitmask
== NULL
)
8065 free (cinfo
.hashcodes
);
8069 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
8070 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
8071 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
8072 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
8074 /* Determine how often each hash bucket is used. */
8075 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
8076 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
8077 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
8079 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
8080 if (cinfo
.counts
[i
] != 0)
8082 cinfo
.indx
[i
] = cnt
;
8083 cnt
+= cinfo
.counts
[i
];
8085 BFD_ASSERT (cnt
== dynsymcount
);
8086 cinfo
.bucketcount
= bucketcount
;
8087 cinfo
.local_indx
= cinfo
.min_dynindx
;
8089 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
8090 s
->size
+= cinfo
.maskbits
/ 8;
8091 if (bed
->record_xhash_symbol
!= NULL
)
8092 s
->size
+= cinfo
.nsyms
* 4;
8093 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
8094 if (contents
== NULL
)
8096 free (cinfo
.bitmask
);
8097 free (cinfo
.hashcodes
);
8101 s
->contents
= contents
;
8103 bfd_put_32 (output_bfd
, bucketcount
, contents
);
8104 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
8105 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
8106 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
8107 contents
+= 16 + cinfo
.maskbits
/ 8;
8109 for (i
= 0; i
< bucketcount
; ++i
)
8111 if (cinfo
.counts
[i
] == 0)
8112 bfd_put_32 (output_bfd
, 0, contents
);
8114 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
8118 cinfo
.contents
= contents
;
8120 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
8121 /* Renumber dynamic symbols, if populating .gnu.hash section.
8122 If using .MIPS.xhash, populate the translation table. */
8123 elf_link_hash_traverse (elf_hash_table (info
),
8124 elf_gnu_hash_process_symidx
, &cinfo
);
8126 contents
= s
->contents
+ 16;
8127 for (i
= 0; i
< maskwords
; ++i
)
8129 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
8131 contents
+= bed
->s
->arch_size
/ 8;
8134 free (cinfo
.bitmask
);
8135 free (cinfo
.hashcodes
);
8139 s
= bfd_get_linker_section (dynobj
, ".dynstr");
8140 BFD_ASSERT (s
!= NULL
);
8142 elf_finalize_dynstr (output_bfd
, info
);
8144 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
8146 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
8147 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
8154 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
8157 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
8160 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
8161 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
8164 /* Finish SHF_MERGE section merging. */
8167 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
8172 if (ENABLE_CHECKING
&& !is_elf_hash_table (info
->hash
))
8175 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
8176 if ((ibfd
->flags
& DYNAMIC
) == 0
8177 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
8178 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
8179 == get_elf_backend_data (obfd
)->s
->elfclass
))
8180 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
8181 if ((sec
->flags
& SEC_MERGE
) != 0
8182 && !bfd_is_abs_section (sec
->output_section
))
8184 struct bfd_elf_section_data
*secdata
;
8186 secdata
= elf_section_data (sec
);
8187 if (! _bfd_add_merge_section (obfd
,
8188 &elf_hash_table (info
)->merge_info
,
8189 sec
, &secdata
->sec_info
))
8191 else if (secdata
->sec_info
)
8192 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
8195 if (elf_hash_table (info
)->merge_info
!= NULL
)
8196 return _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
8197 merge_sections_remove_hook
);
8201 /* Create an entry in an ELF linker hash table. */
8203 struct bfd_hash_entry
*
8204 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
8205 struct bfd_hash_table
*table
,
8208 /* Allocate the structure if it has not already been allocated by a
8212 entry
= (struct bfd_hash_entry
*)
8213 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
8218 /* Call the allocation method of the superclass. */
8219 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
8222 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
8223 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
8225 /* Set local fields. */
8228 ret
->got
= htab
->init_got_refcount
;
8229 ret
->plt
= htab
->init_plt_refcount
;
8230 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
8231 - offsetof (struct elf_link_hash_entry
, size
)));
8232 /* Assume that we have been called by a non-ELF symbol reader.
8233 This flag is then reset by the code which reads an ELF input
8234 file. This ensures that a symbol created by a non-ELF symbol
8235 reader will have the flag set correctly. */
8242 /* Copy data from an indirect symbol to its direct symbol, hiding the
8243 old indirect symbol. Also used for copying flags to a weakdef. */
8246 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
8247 struct elf_link_hash_entry
*dir
,
8248 struct elf_link_hash_entry
*ind
)
8250 struct elf_link_hash_table
*htab
;
8252 if (ind
->dyn_relocs
!= NULL
)
8254 if (dir
->dyn_relocs
!= NULL
)
8256 struct elf_dyn_relocs
**pp
;
8257 struct elf_dyn_relocs
*p
;
8259 /* Add reloc counts against the indirect sym to the direct sym
8260 list. Merge any entries against the same section. */
8261 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
8263 struct elf_dyn_relocs
*q
;
8265 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
8266 if (q
->sec
== p
->sec
)
8268 q
->pc_count
+= p
->pc_count
;
8269 q
->count
+= p
->count
;
8276 *pp
= dir
->dyn_relocs
;
8279 dir
->dyn_relocs
= ind
->dyn_relocs
;
8280 ind
->dyn_relocs
= NULL
;
8283 /* Copy down any references that we may have already seen to the
8284 symbol which just became indirect. */
8286 if (dir
->versioned
!= versioned_hidden
)
8287 dir
->ref_dynamic
|= ind
->ref_dynamic
;
8288 dir
->ref_regular
|= ind
->ref_regular
;
8289 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
8290 dir
->non_got_ref
|= ind
->non_got_ref
;
8291 dir
->needs_plt
|= ind
->needs_plt
;
8292 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
8294 if (ind
->root
.type
!= bfd_link_hash_indirect
)
8297 /* Copy over the global and procedure linkage table refcount entries.
8298 These may have been already set up by a check_relocs routine. */
8299 htab
= elf_hash_table (info
);
8300 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
8302 if (dir
->got
.refcount
< 0)
8303 dir
->got
.refcount
= 0;
8304 dir
->got
.refcount
+= ind
->got
.refcount
;
8305 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
8308 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
8310 if (dir
->plt
.refcount
< 0)
8311 dir
->plt
.refcount
= 0;
8312 dir
->plt
.refcount
+= ind
->plt
.refcount
;
8313 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
8316 if (ind
->dynindx
!= -1)
8318 if (dir
->dynindx
!= -1)
8319 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
8320 dir
->dynindx
= ind
->dynindx
;
8321 dir
->dynstr_index
= ind
->dynstr_index
;
8323 ind
->dynstr_index
= 0;
8328 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
8329 struct elf_link_hash_entry
*h
,
8332 /* STT_GNU_IFUNC symbol must go through PLT. */
8333 if (h
->type
!= STT_GNU_IFUNC
)
8335 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
8340 h
->forced_local
= 1;
8341 if (h
->dynindx
!= -1)
8343 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
8346 h
->dynstr_index
= 0;
8351 /* Hide a symbol. */
8354 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
8355 struct bfd_link_info
*info
,
8356 struct bfd_link_hash_entry
*h
)
8358 if (is_elf_hash_table (info
->hash
))
8360 const struct elf_backend_data
*bed
8361 = get_elf_backend_data (output_bfd
);
8362 struct elf_link_hash_entry
*eh
8363 = (struct elf_link_hash_entry
*) h
;
8364 bed
->elf_backend_hide_symbol (info
, eh
, true);
8365 eh
->def_dynamic
= 0;
8366 eh
->ref_dynamic
= 0;
8367 eh
->dynamic_def
= 0;
8371 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8375 _bfd_elf_link_hash_table_init
8376 (struct elf_link_hash_table
*table
,
8378 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
8379 struct bfd_hash_table
*,
8381 unsigned int entsize
)
8384 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8385 int can_refcount
= bed
->can_refcount
;
8387 table
->init_got_refcount
.refcount
= can_refcount
- 1;
8388 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
8389 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
8390 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
8391 /* The first dynamic symbol is a dummy. */
8392 table
->dynsymcount
= 1;
8394 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
8396 table
->root
.type
= bfd_link_elf_hash_table
;
8397 table
->hash_table_id
= bed
->target_id
;
8398 table
->target_os
= bed
->target_os
;
8399 table
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
8404 /* Create an ELF linker hash table. */
8406 struct bfd_link_hash_table
*
8407 _bfd_elf_link_hash_table_create (bfd
*abfd
)
8409 struct elf_link_hash_table
*ret
;
8410 size_t amt
= sizeof (struct elf_link_hash_table
);
8412 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
8416 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
8417 sizeof (struct elf_link_hash_entry
)))
8426 /* Destroy an ELF linker hash table. */
8429 _bfd_elf_link_hash_table_free (bfd
*obfd
)
8431 struct elf_link_hash_table
*htab
;
8433 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
8434 if (htab
->dynstr
!= NULL
)
8435 _bfd_elf_strtab_free (htab
->dynstr
);
8436 _bfd_merge_sections_free (htab
->merge_info
);
8437 /* NB: htab->dynamic->contents is always allocated by bfd_realloc. */
8438 if (htab
->dynamic
!= NULL
)
8440 free (htab
->dynamic
->contents
);
8441 htab
->dynamic
->contents
= NULL
;
8443 if (htab
->first_hash
!= NULL
)
8445 bfd_hash_table_free (htab
->first_hash
);
8446 free (htab
->first_hash
);
8448 if (htab
->eh_info
.frame_hdr_is_compact
)
8449 free (htab
->eh_info
.u
.compact
.entries
);
8451 free (htab
->eh_info
.u
.dwarf
.array
);
8452 _bfd_generic_link_hash_table_free (obfd
);
8455 /* This is a hook for the ELF emulation code in the generic linker to
8456 tell the backend linker what file name to use for the DT_NEEDED
8457 entry for a dynamic object. */
8460 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
8462 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8463 && bfd_get_format (abfd
) == bfd_object
)
8464 elf_dt_name (abfd
) = name
;
8468 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
8471 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8472 && bfd_get_format (abfd
) == bfd_object
)
8473 lib_class
= elf_dyn_lib_class (abfd
);
8480 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
8482 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8483 && bfd_get_format (abfd
) == bfd_object
)
8484 elf_dyn_lib_class (abfd
) = lib_class
;
8487 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8488 the linker ELF emulation code. */
8490 struct bfd_link_needed_list
*
8491 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8492 struct bfd_link_info
*info
)
8494 if (! is_elf_hash_table (info
->hash
))
8496 return elf_hash_table (info
)->needed
;
8499 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8500 hook for the linker ELF emulation code. */
8502 struct bfd_link_needed_list
*
8503 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8504 struct bfd_link_info
*info
)
8506 if (! is_elf_hash_table (info
->hash
))
8508 return elf_hash_table (info
)->runpath
;
8511 /* Get the name actually used for a dynamic object for a link. This
8512 is the SONAME entry if there is one. Otherwise, it is the string
8513 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8516 bfd_elf_get_dt_soname (bfd
*abfd
)
8518 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8519 && bfd_get_format (abfd
) == bfd_object
)
8520 return elf_dt_name (abfd
);
8524 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8525 the ELF linker emulation code. */
8528 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
8529 struct bfd_link_needed_list
**pneeded
)
8532 bfd_byte
*dynbuf
= NULL
;
8533 unsigned int elfsec
;
8534 unsigned long shlink
;
8535 bfd_byte
*extdyn
, *extdynend
;
8537 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
8541 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
8542 || bfd_get_format (abfd
) != bfd_object
)
8545 s
= bfd_get_section_by_name (abfd
, ".dynamic");
8546 if (s
== NULL
|| s
->size
== 0 || (s
->flags
& SEC_HAS_CONTENTS
) == 0)
8549 if (!_bfd_elf_mmap_section_contents (abfd
, s
, &dynbuf
))
8552 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8553 if (elfsec
== SHN_BAD
)
8556 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8558 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8559 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8561 for (extdyn
= dynbuf
, extdynend
= dynbuf
+ s
->size
;
8562 (size_t) (extdynend
- extdyn
) >= extdynsize
;
8563 extdyn
+= extdynsize
)
8565 Elf_Internal_Dyn dyn
;
8567 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8569 if (dyn
.d_tag
== DT_NULL
)
8572 if (dyn
.d_tag
== DT_NEEDED
)
8575 struct bfd_link_needed_list
*l
;
8576 unsigned int tagv
= dyn
.d_un
.d_val
;
8579 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8584 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8595 _bfd_elf_munmap_section_contents (s
, dynbuf
);
8600 _bfd_elf_munmap_section_contents (s
, dynbuf
);
8604 struct elf_symbuf_symbol
8606 unsigned long st_name
; /* Symbol name, index in string tbl */
8607 unsigned char st_info
; /* Type and binding attributes */
8608 unsigned char st_other
; /* Visibilty, and target specific */
8611 struct elf_symbuf_head
8613 struct elf_symbuf_symbol
*ssym
;
8615 unsigned int st_shndx
;
8622 Elf_Internal_Sym
*isym
;
8623 struct elf_symbuf_symbol
*ssym
;
8629 /* Sort references to symbols by ascending section number. */
8632 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8634 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8635 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8637 if (s1
->st_shndx
!= s2
->st_shndx
)
8638 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8639 /* Final sort by the address of the sym in the symbuf ensures
8642 return s1
> s2
? 1 : -1;
8647 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8649 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8650 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8651 int ret
= strcmp (s1
->name
, s2
->name
);
8654 if (s1
->u
.p
!= s2
->u
.p
)
8655 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8659 static struct elf_symbuf_head
*
8660 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8662 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8663 struct elf_symbuf_symbol
*ssym
;
8664 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8665 size_t i
, shndx_count
, total_size
, amt
;
8667 amt
= symcount
* sizeof (*indbuf
);
8668 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8672 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8673 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8674 *ind
++ = &isymbuf
[i
];
8677 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8678 elf_sort_elf_symbol
);
8681 if (indbufend
> indbuf
)
8682 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8683 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8686 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8687 + (indbufend
- indbuf
) * sizeof (*ssym
));
8688 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8689 if (ssymbuf
== NULL
)
8695 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8696 ssymbuf
->ssym
= NULL
;
8697 ssymbuf
->count
= shndx_count
;
8698 ssymbuf
->st_shndx
= 0;
8699 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8701 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8704 ssymhead
->ssym
= ssym
;
8705 ssymhead
->count
= 0;
8706 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8708 ssym
->st_name
= (*ind
)->st_name
;
8709 ssym
->st_info
= (*ind
)->st_info
;
8710 ssym
->st_other
= (*ind
)->st_other
;
8713 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8714 && (uintptr_t) ssym
- (uintptr_t) ssymbuf
== total_size
);
8720 /* Check if 2 sections define the same set of local and global
8724 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8725 struct bfd_link_info
*info
)
8728 const struct elf_backend_data
*bed1
, *bed2
;
8729 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8730 size_t symcount1
, symcount2
;
8731 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8732 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8733 Elf_Internal_Sym
*isym
, *isymend
;
8734 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8735 size_t count1
, count2
, sec_count1
, sec_count2
, i
;
8736 unsigned int shndx1
, shndx2
;
8738 bool ignore_section_symbol_p
;
8743 /* Both sections have to be in ELF. */
8744 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8745 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8748 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8751 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8752 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8753 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8756 bed1
= get_elf_backend_data (bfd1
);
8757 bed2
= get_elf_backend_data (bfd2
);
8758 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8759 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8760 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8761 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8763 if (symcount1
== 0 || symcount2
== 0)
8769 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8770 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8772 /* Ignore section symbols only when matching non-debugging sections
8773 or linkonce section with comdat section. */
8774 ignore_section_symbol_p
8775 = ((sec1
->flags
& SEC_DEBUGGING
) == 0
8776 || ((elf_section_flags (sec1
) & SHF_GROUP
)
8777 != (elf_section_flags (sec2
) & SHF_GROUP
)));
8779 if (ssymbuf1
== NULL
)
8781 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8783 if (isymbuf1
== NULL
)
8786 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8788 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8789 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8793 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8795 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8797 if (isymbuf2
== NULL
)
8800 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8802 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8803 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8807 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8809 /* Optimized faster version. */
8811 struct elf_symbol
*symp
;
8812 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8815 hi
= ssymbuf1
->count
;
8821 mid
= (lo
+ hi
) / 2;
8822 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8824 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8828 count1
= ssymbuf1
[mid
].count
;
8833 if (ignore_section_symbol_p
)
8835 for (i
= 0; i
< count1
; i
++)
8836 if (ELF_ST_TYPE (ssymbuf1
->ssym
[i
].st_info
) == STT_SECTION
)
8838 count1
-= sec_count1
;
8842 hi
= ssymbuf2
->count
;
8848 mid
= (lo
+ hi
) / 2;
8849 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8851 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8855 count2
= ssymbuf2
[mid
].count
;
8860 if (ignore_section_symbol_p
)
8862 for (i
= 0; i
< count2
; i
++)
8863 if (ELF_ST_TYPE (ssymbuf2
->ssym
[i
].st_info
) == STT_SECTION
)
8865 count2
-= sec_count2
;
8868 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8872 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8874 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8875 if (symtable1
== NULL
|| symtable2
== NULL
)
8879 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
+ sec_count1
;
8880 ssym
< ssymend
; ssym
++)
8882 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8884 symp
->u
.ssym
= ssym
;
8885 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8888 if (symp
->name
== NULL
)
8894 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
+ sec_count2
;
8895 ssym
< ssymend
; ssym
++)
8897 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8899 symp
->u
.ssym
= ssym
;
8900 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8903 if (symp
->name
== NULL
)
8908 /* Sort symbol by name. */
8909 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8910 elf_sym_name_compare
);
8911 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8912 elf_sym_name_compare
);
8914 for (i
= 0; i
< count1
; i
++)
8915 /* Two symbols must have the same binding, type and name. */
8916 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8917 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8918 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8925 symtable1
= (struct elf_symbol
*)
8926 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8927 symtable2
= (struct elf_symbol
*)
8928 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8929 if (symtable1
== NULL
|| symtable2
== NULL
)
8932 /* Count definitions in the section. */
8934 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8935 if (isym
->st_shndx
== shndx1
8936 && (!ignore_section_symbol_p
8937 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8938 symtable1
[count1
++].u
.isym
= isym
;
8941 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8942 if (isym
->st_shndx
== shndx2
8943 && (!ignore_section_symbol_p
8944 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8945 symtable2
[count2
++].u
.isym
= isym
;
8947 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8950 for (i
= 0; i
< count1
; i
++)
8953 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8954 symtable1
[i
].u
.isym
->st_name
);
8955 if (symtable1
[i
].name
== NULL
)
8959 for (i
= 0; i
< count2
; i
++)
8962 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8963 symtable2
[i
].u
.isym
->st_name
);
8964 if (symtable2
[i
].name
== NULL
)
8968 /* Sort symbol by name. */
8969 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8970 elf_sym_name_compare
);
8971 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8972 elf_sym_name_compare
);
8974 for (i
= 0; i
< count1
; i
++)
8975 /* Two symbols must have the same binding, type and name. */
8976 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8977 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8978 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8992 /* Return TRUE if 2 section types are compatible. */
8995 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8996 bfd
*bbfd
, const asection
*bsec
)
9000 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
9001 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9004 return elf_section_type (asec
) == elf_section_type (bsec
);
9007 /* Final phase of ELF linker. */
9009 /* A structure we use to avoid passing large numbers of arguments. */
9011 struct elf_final_link_info
9013 /* General link information. */
9014 struct bfd_link_info
*info
;
9017 /* Symbol string table. */
9018 struct elf_strtab_hash
*symstrtab
;
9019 /* .hash section. */
9021 /* symbol version section (.gnu.version). */
9022 asection
*symver_sec
;
9023 /* Buffer large enough to hold contents of any section. */
9025 /* Buffer large enough to hold external relocs of any section. */
9026 void *external_relocs
;
9027 /* Buffer large enough to hold internal relocs of any section. */
9028 Elf_Internal_Rela
*internal_relocs
;
9029 /* Buffer large enough to hold external local symbols of any input
9031 bfd_byte
*external_syms
;
9032 /* And a buffer for symbol section indices. */
9033 Elf_External_Sym_Shndx
*locsym_shndx
;
9034 /* Buffer large enough to hold internal local symbols of any input
9036 Elf_Internal_Sym
*internal_syms
;
9037 /* Array large enough to hold a symbol index for each local symbol
9038 of any input BFD. */
9040 /* Array large enough to hold a section pointer for each local
9041 symbol of any input BFD. */
9042 asection
**sections
;
9043 /* Buffer for SHT_SYMTAB_SHNDX section. */
9044 Elf_External_Sym_Shndx
*symshndxbuf
;
9045 /* Number of STT_FILE syms seen. */
9046 size_t filesym_count
;
9047 /* Local symbol hash table. */
9048 struct bfd_hash_table local_hash_table
;
9051 struct local_hash_entry
9053 /* Base hash table entry structure. */
9054 struct bfd_hash_entry root
;
9055 /* Size of the local symbol name. */
9057 /* Number of the duplicated local symbol names. */
9061 /* Create an entry in the local symbol hash table. */
9063 static struct bfd_hash_entry
*
9064 local_hash_newfunc (struct bfd_hash_entry
*entry
,
9065 struct bfd_hash_table
*table
,
9069 /* Allocate the structure if it has not already been allocated by a
9073 entry
= bfd_hash_allocate (table
,
9074 sizeof (struct local_hash_entry
));
9079 /* Call the allocation method of the superclass. */
9080 entry
= bfd_hash_newfunc (entry
, table
, string
);
9083 ((struct local_hash_entry
*) entry
)->count
= 0;
9084 ((struct local_hash_entry
*) entry
)->size
= 0;
9090 /* This struct is used to pass information to elf_link_output_extsym. */
9092 struct elf_outext_info
9097 struct elf_final_link_info
*flinfo
;
9101 /* Support for evaluating a complex relocation.
9103 Complex relocations are generalized, self-describing relocations. The
9104 implementation of them consists of two parts: complex symbols, and the
9105 relocations themselves.
9107 The relocations use a reserved elf-wide relocation type code (R_RELC
9108 external / BFD_RELOC_RELC internal) and an encoding of relocation field
9109 information (start bit, end bit, word width, etc) into the addend. This
9110 information is extracted from CGEN-generated operand tables within gas.
9112 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9113 internal) representing prefix-notation expressions, including but not
9114 limited to those sorts of expressions normally encoded as addends in the
9115 addend field. The symbol mangling format is:
9118 | <unary-operator> ':' <node>
9119 | <binary-operator> ':' <node> ':' <node>
9122 <literal> := 's' <digits=N> ':' <N character symbol name>
9123 | 'S' <digits=N> ':' <N character section name>
9127 <binary-operator> := as in C
9128 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
9131 set_symbol_value (bfd
*bfd_with_globals
,
9132 Elf_Internal_Sym
*isymbuf
,
9137 struct elf_link_hash_entry
*h
;
9138 size_t extsymoff
= locsymcount
;
9140 if (symidx
< locsymcount
)
9142 Elf_Internal_Sym
*sym
;
9144 sym
= isymbuf
+ symidx
;
9145 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
9147 /* It is a local symbol: move it to the
9148 "absolute" section and give it a value. */
9149 sym
->st_shndx
= SHN_ABS
;
9150 sym
->st_value
= val
;
9153 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
9157 /* It is a global symbol: set its link type
9158 to "defined" and give it a value. */
9159 h
= get_link_hash_entry (elf_sym_hashes (bfd_with_globals
), symidx
, extsymoff
);
9162 /* FIXMEL What should we do ? */
9165 h
->root
.type
= bfd_link_hash_defined
;
9166 h
->root
.u
.def
.value
= val
;
9167 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
9171 resolve_symbol (const char *name
,
9173 struct elf_final_link_info
*flinfo
,
9175 Elf_Internal_Sym
*isymbuf
,
9178 Elf_Internal_Sym
*sym
;
9179 struct bfd_link_hash_entry
*global_entry
;
9180 const char *candidate
= NULL
;
9181 Elf_Internal_Shdr
*symtab_hdr
;
9184 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
9186 for (i
= 0; i
< locsymcount
; ++ i
)
9190 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
9193 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
9194 symtab_hdr
->sh_link
,
9197 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9198 name
, candidate
, (unsigned long) sym
->st_value
);
9200 if (candidate
&& strcmp (candidate
, name
) == 0)
9202 asection
*sec
= flinfo
->sections
[i
];
9204 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
9205 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
9207 printf ("Found symbol with value %8.8lx\n",
9208 (unsigned long) *result
);
9214 /* Hmm, haven't found it yet. perhaps it is a global. */
9215 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
9216 false, false, true);
9220 if (global_entry
->type
== bfd_link_hash_defined
9221 || global_entry
->type
== bfd_link_hash_defweak
)
9223 *result
= (global_entry
->u
.def
.value
9224 + global_entry
->u
.def
.section
->output_section
->vma
9225 + global_entry
->u
.def
.section
->output_offset
);
9227 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9228 global_entry
->root
.string
, (unsigned long) *result
);
9236 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
9237 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
9238 names like "foo.end" which is the end address of section "foo". */
9241 resolve_section (const char *name
,
9249 for (curr
= sections
; curr
; curr
= curr
->next
)
9250 if (strcmp (curr
->name
, name
) == 0)
9252 *result
= curr
->vma
;
9256 /* Hmm. still haven't found it. try pseudo-section names. */
9257 /* FIXME: This could be coded more efficiently... */
9258 for (curr
= sections
; curr
; curr
= curr
->next
)
9260 len
= strlen (curr
->name
);
9261 if (len
> strlen (name
))
9264 if (strncmp (curr
->name
, name
, len
) == 0)
9266 if (startswith (name
+ len
, ".end"))
9268 *result
= (curr
->vma
9269 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
9273 /* Insert more pseudo-section names here, if you like. */
9281 undefined_reference (const char *reftype
, const char *name
)
9283 /* xgettext:c-format */
9284 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9286 bfd_set_error (bfd_error_bad_value
);
9290 eval_symbol (bfd_vma
*result
,
9293 struct elf_final_link_info
*flinfo
,
9295 Elf_Internal_Sym
*isymbuf
,
9304 const char *sym
= *symp
;
9306 bool symbol_is_section
= false;
9311 if (len
< 1 || len
> sizeof (symbuf
))
9313 bfd_set_error (bfd_error_invalid_operation
);
9326 *result
= strtoul (sym
, (char **) symp
, 16);
9330 symbol_is_section
= true;
9334 symlen
= strtol (sym
, (char **) symp
, 10);
9335 sym
= *symp
+ 1; /* Skip the trailing ':'. */
9337 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
9339 bfd_set_error (bfd_error_invalid_operation
);
9343 memcpy (symbuf
, sym
, symlen
);
9344 symbuf
[symlen
] = '\0';
9345 *symp
= sym
+ symlen
;
9347 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9348 the symbol as a section, or vice-versa. so we're pretty liberal in our
9349 interpretation here; section means "try section first", not "must be a
9350 section", and likewise with symbol. */
9352 if (symbol_is_section
)
9354 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
9355 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
9356 isymbuf
, locsymcount
))
9358 undefined_reference ("section", symbuf
);
9364 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
9365 isymbuf
, locsymcount
)
9366 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
9369 undefined_reference ("symbol", symbuf
);
9376 /* All that remains are operators. */
9378 #define UNARY_OP(op) \
9379 if (startswith (sym, #op)) \
9381 sym += strlen (#op); \
9385 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9386 isymbuf, locsymcount, signed_p)) \
9389 *result = op ((bfd_signed_vma) a); \
9395 #define BINARY_OP_HEAD(op) \
9396 if (startswith (sym, #op)) \
9398 sym += strlen (#op); \
9402 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9403 isymbuf, locsymcount, signed_p)) \
9406 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9407 isymbuf, locsymcount, signed_p)) \
9409 #define BINARY_OP_TAIL(op) \
9411 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9416 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9420 BINARY_OP_HEAD (<<);
9421 if (b
>= sizeof (a
) * CHAR_BIT
)
9427 BINARY_OP_TAIL (<<);
9428 BINARY_OP_HEAD (>>);
9429 if (b
>= sizeof (a
) * CHAR_BIT
)
9431 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
9434 BINARY_OP_TAIL (>>);
9447 _bfd_error_handler (_("division by zero"));
9448 bfd_set_error (bfd_error_bad_value
);
9455 _bfd_error_handler (_("division by zero"));
9456 bfd_set_error (bfd_error_bad_value
);
9469 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
9470 bfd_set_error (bfd_error_invalid_operation
);
9476 put_value (bfd_vma size
,
9477 unsigned long chunksz
,
9482 location
+= (size
- chunksz
);
9484 for (; size
; size
-= chunksz
, location
-= chunksz
)
9489 bfd_put_8 (input_bfd
, x
, location
);
9493 bfd_put_16 (input_bfd
, x
, location
);
9497 bfd_put_32 (input_bfd
, x
, location
);
9498 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9504 bfd_put_64 (input_bfd
, x
, location
);
9505 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9518 get_value (bfd_vma size
,
9519 unsigned long chunksz
,
9526 /* Sanity checks. */
9527 BFD_ASSERT (chunksz
<= sizeof (x
)
9530 && (size
% chunksz
) == 0
9531 && input_bfd
!= NULL
9532 && location
!= NULL
);
9534 if (chunksz
== sizeof (x
))
9536 BFD_ASSERT (size
== chunksz
);
9538 /* Make sure that we do not perform an undefined shift operation.
9539 We know that size == chunksz so there will only be one iteration
9540 of the loop below. */
9544 shift
= 8 * chunksz
;
9546 for (; size
; size
-= chunksz
, location
+= chunksz
)
9551 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
9554 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
9557 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
9561 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
9572 decode_complex_addend (unsigned long *start
, /* in bits */
9573 unsigned long *oplen
, /* in bits */
9574 unsigned long *len
, /* in bits */
9575 unsigned long *wordsz
, /* in bytes */
9576 unsigned long *chunksz
, /* in bytes */
9577 unsigned long *lsb0_p
,
9578 unsigned long *signed_p
,
9579 unsigned long *trunc_p
,
9580 unsigned long encoded
)
9582 * start
= encoded
& 0x3F;
9583 * len
= (encoded
>> 6) & 0x3F;
9584 * oplen
= (encoded
>> 12) & 0x3F;
9585 * wordsz
= (encoded
>> 18) & 0xF;
9586 * chunksz
= (encoded
>> 22) & 0xF;
9587 * lsb0_p
= (encoded
>> 27) & 1;
9588 * signed_p
= (encoded
>> 28) & 1;
9589 * trunc_p
= (encoded
>> 29) & 1;
9592 bfd_reloc_status_type
9593 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
9594 asection
*input_section
,
9596 Elf_Internal_Rela
*rel
,
9599 bfd_vma shift
, x
, mask
;
9600 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9601 bfd_reloc_status_type r
;
9602 bfd_size_type octets
;
9604 /* Perform this reloc, since it is complex.
9605 (this is not to say that it necessarily refers to a complex
9606 symbol; merely that it is a self-describing CGEN based reloc.
9607 i.e. the addend has the complete reloc information (bit start, end,
9608 word size, etc) encoded within it.). */
9610 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9611 &chunksz
, &lsb0_p
, &signed_p
,
9612 &trunc_p
, rel
->r_addend
);
9614 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9617 shift
= (start
+ 1) - len
;
9619 shift
= (8 * wordsz
) - (start
+ len
);
9621 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9622 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9625 printf ("Doing complex reloc: "
9626 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9627 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9628 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9629 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9630 oplen
, (unsigned long) x
, (unsigned long) mask
,
9631 (unsigned long) relocation
);
9636 /* Now do an overflow check. */
9637 r
= bfd_check_overflow ((signed_p
9638 ? complain_overflow_signed
9639 : complain_overflow_unsigned
),
9640 len
, 0, (8 * wordsz
),
9644 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9647 printf (" relocation: %8.8lx\n"
9648 " shifted mask: %8.8lx\n"
9649 " shifted/masked reloc: %8.8lx\n"
9650 " result: %8.8lx\n",
9651 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9652 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9654 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9658 /* Functions to read r_offset from external (target order) reloc
9659 entry. Faster than bfd_getl32 et al, because we let the compiler
9660 know the value is aligned. */
9663 ext32l_r_offset (const void *p
)
9670 const union aligned32
*a
9671 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9673 uint32_t aval
= ( (uint32_t) a
->c
[0]
9674 | (uint32_t) a
->c
[1] << 8
9675 | (uint32_t) a
->c
[2] << 16
9676 | (uint32_t) a
->c
[3] << 24);
9681 ext32b_r_offset (const void *p
)
9688 const union aligned32
*a
9689 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9691 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9692 | (uint32_t) a
->c
[1] << 16
9693 | (uint32_t) a
->c
[2] << 8
9694 | (uint32_t) a
->c
[3]);
9699 ext64l_r_offset (const void *p
)
9706 const union aligned64
*a
9707 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9709 uint64_t aval
= ( (uint64_t) a
->c
[0]
9710 | (uint64_t) a
->c
[1] << 8
9711 | (uint64_t) a
->c
[2] << 16
9712 | (uint64_t) a
->c
[3] << 24
9713 | (uint64_t) a
->c
[4] << 32
9714 | (uint64_t) a
->c
[5] << 40
9715 | (uint64_t) a
->c
[6] << 48
9716 | (uint64_t) a
->c
[7] << 56);
9721 ext64b_r_offset (const void *p
)
9728 const union aligned64
*a
9729 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9731 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9732 | (uint64_t) a
->c
[1] << 48
9733 | (uint64_t) a
->c
[2] << 40
9734 | (uint64_t) a
->c
[3] << 32
9735 | (uint64_t) a
->c
[4] << 24
9736 | (uint64_t) a
->c
[5] << 16
9737 | (uint64_t) a
->c
[6] << 8
9738 | (uint64_t) a
->c
[7]);
9742 /* When performing a relocatable link, the input relocations are
9743 preserved. But, if they reference global symbols, the indices
9744 referenced must be updated. Update all the relocations found in
9748 elf_link_adjust_relocs (bfd
*abfd
,
9750 struct bfd_elf_section_reloc_data
*reldata
,
9752 struct bfd_link_info
*info
)
9755 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9757 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9758 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9759 bfd_vma r_type_mask
;
9761 unsigned int count
= reldata
->count
;
9762 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9764 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9766 swap_in
= bed
->s
->swap_reloc_in
;
9767 swap_out
= bed
->s
->swap_reloc_out
;
9769 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9771 swap_in
= bed
->s
->swap_reloca_in
;
9772 swap_out
= bed
->s
->swap_reloca_out
;
9777 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9780 if (bed
->s
->arch_size
== 32)
9787 r_type_mask
= 0xffffffff;
9791 erela
= reldata
->hdr
->contents
;
9792 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9794 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9797 if (*rel_hash
== NULL
)
9800 if ((*rel_hash
)->indx
== -2
9801 && info
->gc_sections
9802 && ! info
->gc_keep_exported
)
9804 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9805 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9807 (*rel_hash
)->root
.root
.string
);
9808 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9810 bfd_set_error (bfd_error_invalid_operation
);
9813 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9815 (*swap_in
) (abfd
, erela
, irela
);
9816 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9817 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9818 | (irela
[j
].r_info
& r_type_mask
));
9819 (*swap_out
) (abfd
, irela
, erela
);
9822 if (bed
->elf_backend_update_relocs
)
9823 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9825 if (sort
&& count
!= 0)
9827 bfd_vma (*ext_r_off
) (const void *);
9830 bfd_byte
*base
, *end
, *p
, *loc
;
9831 bfd_byte
*buf
= NULL
;
9833 if (bed
->s
->arch_size
== 32)
9835 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9836 ext_r_off
= ext32l_r_offset
;
9837 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9838 ext_r_off
= ext32b_r_offset
;
9844 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9845 ext_r_off
= ext64l_r_offset
;
9846 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9847 ext_r_off
= ext64b_r_offset
;
9852 /* Must use a stable sort here. A modified insertion sort,
9853 since the relocs are mostly sorted already. */
9854 elt_size
= reldata
->hdr
->sh_entsize
;
9855 base
= reldata
->hdr
->contents
;
9856 end
= base
+ count
* elt_size
;
9857 if (elt_size
> sizeof (Elf64_External_Rela
))
9860 /* Ensure the first element is lowest. This acts as a sentinel,
9861 speeding the main loop below. */
9862 r_off
= (*ext_r_off
) (base
);
9863 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9865 bfd_vma r_off2
= (*ext_r_off
) (p
);
9874 /* Don't just swap *base and *loc as that changes the order
9875 of the original base[0] and base[1] if they happen to
9876 have the same r_offset. */
9877 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9878 memcpy (onebuf
, loc
, elt_size
);
9879 memmove (base
+ elt_size
, base
, loc
- base
);
9880 memcpy (base
, onebuf
, elt_size
);
9883 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9885 /* base to p is sorted, *p is next to insert. */
9886 r_off
= (*ext_r_off
) (p
);
9887 /* Search the sorted region for location to insert. */
9889 while (r_off
< (*ext_r_off
) (loc
))
9894 /* Chances are there is a run of relocs to insert here,
9895 from one of more input files. Files are not always
9896 linked in order due to the way elf_link_input_bfd is
9897 called. See pr17666. */
9898 size_t sortlen
= p
- loc
;
9899 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9900 size_t runlen
= elt_size
;
9901 bfd_vma r_off_runend
= r_off
;
9902 bfd_vma r_off_runend_next
;
9903 size_t buf_size
= 96 * 1024;
9904 while (p
+ runlen
< end
9905 && (sortlen
<= buf_size
9906 || runlen
+ elt_size
<= buf_size
)
9907 /* run must not break the ordering of base..loc+1 */
9908 && r_off2
> (r_off_runend_next
= (*ext_r_off
) (p
+ runlen
))
9909 /* run must be already sorted */
9910 && r_off_runend_next
>= r_off_runend
)
9913 r_off_runend
= r_off_runend_next
;
9917 buf
= bfd_malloc (buf_size
);
9921 if (runlen
< sortlen
)
9923 memcpy (buf
, p
, runlen
);
9924 memmove (loc
+ runlen
, loc
, sortlen
);
9925 memcpy (loc
, buf
, runlen
);
9929 memcpy (buf
, loc
, sortlen
);
9930 memmove (loc
, p
, runlen
);
9931 memcpy (loc
+ runlen
, buf
, sortlen
);
9933 p
+= runlen
- elt_size
;
9936 /* Hashes are no longer valid. */
9937 free (reldata
->hashes
);
9938 reldata
->hashes
= NULL
;
9944 struct elf_link_sort_rela
9950 enum elf_reloc_type_class type
;
9951 /* We use this as an array of size int_rels_per_ext_rel. */
9952 Elf_Internal_Rela rela
[1];
9955 /* qsort stability here and for cmp2 is only an issue if multiple
9956 dynamic relocations are emitted at the same address. But targets
9957 that apply a series of dynamic relocations each operating on the
9958 result of the prior relocation can't use -z combreloc as
9959 implemented anyway. Such schemes tend to be broken by sorting on
9960 symbol index. That leaves dynamic NONE relocs as the only other
9961 case where ld might emit multiple relocs at the same address, and
9962 those are only emitted due to target bugs. */
9965 elf_link_sort_cmp1 (const void *A
, const void *B
)
9967 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9968 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9969 int relativea
, relativeb
;
9971 relativea
= a
->type
== reloc_class_relative
;
9972 relativeb
= b
->type
== reloc_class_relative
;
9974 if (relativea
< relativeb
)
9976 if (relativea
> relativeb
)
9978 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9980 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9982 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9984 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9990 elf_link_sort_cmp2 (const void *A
, const void *B
)
9992 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9993 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9995 if (a
->type
< b
->type
)
9997 if (a
->type
> b
->type
)
9999 if (a
->u
.offset
< b
->u
.offset
)
10001 if (a
->u
.offset
> b
->u
.offset
)
10003 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
10005 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
10011 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
10013 asection
*dynamic_relocs
;
10014 asection
*rela_dyn
;
10016 bfd_size_type count
, size
;
10017 size_t i
, ret
, sort_elt
, ext_size
;
10018 bfd_byte
*sort
, *s_non_relative
, *p
;
10019 struct elf_link_sort_rela
*sq
;
10020 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10021 int i2e
= bed
->s
->int_rels_per_ext_rel
;
10022 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
10023 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
10024 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
10025 struct bfd_link_order
*lo
;
10026 bfd_vma r_sym_mask
;
10029 /* Find a dynamic reloc section. */
10030 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
10031 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
10032 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
10033 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
10035 bool use_rela_initialised
= false;
10037 /* This is just here to stop gcc from complaining.
10038 Its initialization checking code is not perfect. */
10041 /* Both sections are present. Examine the sizes
10042 of the indirect sections to help us choose. */
10043 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10044 if (lo
->type
== bfd_indirect_link_order
)
10046 asection
*o
= lo
->u
.indirect
.section
;
10048 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
10050 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
10051 /* Section size is divisible by both rel and rela sizes.
10052 It is of no help to us. */
10056 /* Section size is only divisible by rela. */
10057 if (use_rela_initialised
&& !use_rela
)
10059 _bfd_error_handler (_("%pB: unable to sort relocs - "
10060 "they are in more than one size"),
10062 bfd_set_error (bfd_error_invalid_operation
);
10068 use_rela_initialised
= true;
10072 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
10074 /* Section size is only divisible by rel. */
10075 if (use_rela_initialised
&& use_rela
)
10077 _bfd_error_handler (_("%pB: unable to sort relocs - "
10078 "they are in more than one size"),
10080 bfd_set_error (bfd_error_invalid_operation
);
10086 use_rela_initialised
= true;
10091 /* The section size is not divisible by either -
10092 something is wrong. */
10093 _bfd_error_handler (_("%pB: unable to sort relocs - "
10094 "they are of an unknown size"), abfd
);
10095 bfd_set_error (bfd_error_invalid_operation
);
10100 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10101 if (lo
->type
== bfd_indirect_link_order
)
10103 asection
*o
= lo
->u
.indirect
.section
;
10105 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
10107 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
10108 /* Section size is divisible by both rel and rela sizes.
10109 It is of no help to us. */
10113 /* Section size is only divisible by rela. */
10114 if (use_rela_initialised
&& !use_rela
)
10116 _bfd_error_handler (_("%pB: unable to sort relocs - "
10117 "they are in more than one size"),
10119 bfd_set_error (bfd_error_invalid_operation
);
10125 use_rela_initialised
= true;
10129 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
10131 /* Section size is only divisible by rel. */
10132 if (use_rela_initialised
&& use_rela
)
10134 _bfd_error_handler (_("%pB: unable to sort relocs - "
10135 "they are in more than one size"),
10137 bfd_set_error (bfd_error_invalid_operation
);
10143 use_rela_initialised
= true;
10148 /* The section size is not divisible by either -
10149 something is wrong. */
10150 _bfd_error_handler (_("%pB: unable to sort relocs - "
10151 "they are of an unknown size"), abfd
);
10152 bfd_set_error (bfd_error_invalid_operation
);
10157 if (! use_rela_initialised
)
10158 /* Make a guess. */
10161 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
10163 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
10170 dynamic_relocs
= rela_dyn
;
10171 ext_size
= bed
->s
->sizeof_rela
;
10172 swap_in
= bed
->s
->swap_reloca_in
;
10173 swap_out
= bed
->s
->swap_reloca_out
;
10177 dynamic_relocs
= rel_dyn
;
10178 ext_size
= bed
->s
->sizeof_rel
;
10179 swap_in
= bed
->s
->swap_reloc_in
;
10180 swap_out
= bed
->s
->swap_reloc_out
;
10184 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10185 if (lo
->type
== bfd_indirect_link_order
)
10186 size
+= lo
->u
.indirect
.section
->size
;
10188 if (size
!= dynamic_relocs
->size
)
10191 sort_elt
= (sizeof (struct elf_link_sort_rela
)
10192 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
10194 count
= dynamic_relocs
->size
/ ext_size
;
10197 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
10201 (*info
->callbacks
->warning
)
10202 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
10206 if (bed
->s
->arch_size
== 32)
10207 r_sym_mask
= ~(bfd_vma
) 0xff;
10209 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
10211 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10212 if (lo
->type
== bfd_indirect_link_order
)
10214 bfd_byte
*erel
, *erelend
;
10215 asection
*o
= lo
->u
.indirect
.section
;
10217 if (o
->contents
== NULL
&& o
->size
!= 0)
10219 /* This is a reloc section that is being handled as a normal
10220 section. See bfd_section_from_shdr. We can't combine
10221 relocs in this case. */
10225 erel
= o
->contents
;
10226 erelend
= o
->contents
+ o
->size
;
10227 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
10229 while (erel
< erelend
)
10231 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
10233 (*swap_in
) (abfd
, erel
, s
->rela
);
10234 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
10235 s
->u
.sym_mask
= r_sym_mask
;
10241 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
10243 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
10245 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
10246 if (s
->type
!= reloc_class_relative
)
10250 s_non_relative
= p
;
10252 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
10253 for (; i
< count
; i
++, p
+= sort_elt
)
10255 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
10256 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
10258 sp
->u
.offset
= sq
->rela
->r_offset
;
10261 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
10263 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
10264 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
10266 /* We have plt relocs in .rela.dyn. */
10267 sq
= (struct elf_link_sort_rela
*) sort
;
10268 for (i
= 0; i
< count
; i
++)
10269 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
10271 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
10273 struct bfd_link_order
**plo
;
10274 /* Put srelplt link_order last. This is so the output_offset
10275 set in the next loop is correct for DT_JMPREL. */
10276 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
10277 if ((*plo
)->type
== bfd_indirect_link_order
10278 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
10284 plo
= &(*plo
)->next
;
10287 dynamic_relocs
->map_tail
.link_order
= lo
;
10292 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10293 if (lo
->type
== bfd_indirect_link_order
)
10295 bfd_byte
*erel
, *erelend
;
10296 asection
*o
= lo
->u
.indirect
.section
;
10298 erel
= o
->contents
;
10299 erelend
= o
->contents
+ o
->size
;
10300 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
10301 while (erel
< erelend
)
10303 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
10304 (*swap_out
) (abfd
, s
->rela
, erel
);
10311 *psec
= dynamic_relocs
;
10315 /* Add a symbol to the output symbol string table. */
10318 elf_link_output_symstrtab (void *finf
,
10320 Elf_Internal_Sym
*elfsym
,
10321 asection
*input_sec
,
10322 struct elf_link_hash_entry
*h
)
10324 struct elf_final_link_info
*flinfo
= finf
;
10325 int (*output_symbol_hook
)
10326 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
10327 struct elf_link_hash_entry
*);
10328 struct elf_link_hash_table
*hash_table
;
10329 const struct elf_backend_data
*bed
;
10330 bfd_size_type strtabsize
;
10332 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10334 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10335 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
10336 if (output_symbol_hook
!= NULL
)
10338 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
10343 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
10344 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
10345 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
10346 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
10348 if (name
== NULL
|| *name
== '\0')
10349 elfsym
->st_name
= (unsigned long) -1;
10352 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10353 to get the final offset for st_name. */
10354 char *versioned_name
= (char *) name
;
10357 if (h
->versioned
== versioned
&& h
->def_dynamic
)
10359 /* Keep only one '@' for versioned symbols defined in
10361 char *version
= strrchr (name
, ELF_VER_CHR
);
10362 char *base_end
= strchr (name
, ELF_VER_CHR
);
10363 if (version
!= base_end
)
10366 size_t len
= strlen (name
);
10367 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
10368 if (versioned_name
== NULL
)
10370 base_len
= base_end
- name
;
10371 memcpy (versioned_name
, name
, base_len
);
10372 memcpy (versioned_name
+ base_len
, version
,
10377 else if (flinfo
->info
->unique_symbol
10378 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
10380 struct local_hash_entry
*lh
;
10384 switch (ELF_ST_TYPE (elfsym
->st_info
))
10390 lh
= (struct local_hash_entry
*) bfd_hash_lookup
10391 (&flinfo
->local_hash_table
, name
, true, false);
10394 /* Always append ".COUNT" to local symbols to avoid
10395 potential conflicts with local symbol "XXX.COUNT". */
10396 sprintf (buf
, "%lx", lh
->count
);
10397 base_len
= lh
->size
;
10400 base_len
= strlen (name
);
10401 lh
->size
= base_len
;
10403 count_len
= strlen (buf
);
10404 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
10405 base_len
+ count_len
+ 2);
10406 if (versioned_name
== NULL
)
10408 memcpy (versioned_name
, name
, base_len
);
10409 versioned_name
[base_len
] = '.';
10410 memcpy (versioned_name
+ base_len
+ 1, buf
,
10417 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
10418 versioned_name
, false);
10419 if (elfsym
->st_name
== (unsigned long) -1)
10423 hash_table
= elf_hash_table (flinfo
->info
);
10424 strtabsize
= hash_table
->strtabsize
;
10425 if (strtabsize
<= flinfo
->output_bfd
->symcount
)
10427 strtabsize
+= strtabsize
;
10428 hash_table
->strtabsize
= strtabsize
;
10429 strtabsize
*= sizeof (*hash_table
->strtab
);
10431 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
10433 if (hash_table
->strtab
== NULL
)
10436 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].sym
= *elfsym
;
10437 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].dest_index
10438 = flinfo
->output_bfd
->symcount
;
10439 flinfo
->output_bfd
->symcount
+= 1;
10444 /* Swap symbols out to the symbol table and flush the output symbols to
10448 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
10450 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
10453 const struct elf_backend_data
*bed
;
10455 Elf_Internal_Shdr
*hdr
;
10459 if (flinfo
->output_bfd
->symcount
== 0)
10462 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10464 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10466 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10467 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
10468 if (symbuf
== NULL
)
10471 if (flinfo
->symshndxbuf
)
10473 amt
= sizeof (Elf_External_Sym_Shndx
);
10474 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
10475 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
10476 if (flinfo
->symshndxbuf
== NULL
)
10483 /* Now swap out the symbols. */
10484 for (i
= 0; i
< flinfo
->output_bfd
->symcount
; i
++)
10486 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
10487 if (elfsym
->sym
.st_name
== (unsigned long) -1)
10488 elfsym
->sym
.st_name
= 0;
10490 elfsym
->sym
.st_name
10491 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
10492 elfsym
->sym
.st_name
);
10494 /* Inform the linker of the addition of this symbol. */
10496 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
10497 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
10500 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
10501 ((bfd_byte
*) symbuf
10502 + (elfsym
->dest_index
10503 * bed
->s
->sizeof_sym
)),
10504 NPTR_ADD (flinfo
->symshndxbuf
,
10505 elfsym
->dest_index
));
10508 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
10509 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
10510 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10511 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
10512 && bfd_write (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
10514 hdr
->sh_size
+= amt
;
10524 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10527 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
10529 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
10530 && sym
->st_shndx
< SHN_LORESERVE
)
10532 /* The gABI doesn't support dynamic symbols in output sections
10535 /* xgettext:c-format */
10536 (_("%pB: too many sections: %d (>= %d)"),
10537 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
10538 bfd_set_error (bfd_error_nonrepresentable_section
);
10544 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10545 allowing an unsatisfied unversioned symbol in the DSO to match a
10546 versioned symbol that would normally require an explicit version.
10547 We also handle the case that a DSO references a hidden symbol
10548 which may be satisfied by a versioned symbol in another DSO. */
10551 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
10552 const struct elf_backend_data
*bed
,
10553 struct elf_link_hash_entry
*h
)
10556 struct elf_link_loaded_list
*loaded
;
10558 if (!is_elf_hash_table (info
->hash
))
10561 /* Check indirect symbol. */
10562 while (h
->root
.type
== bfd_link_hash_indirect
)
10563 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10565 switch (h
->root
.type
)
10571 case bfd_link_hash_undefined
:
10572 case bfd_link_hash_undefweak
:
10573 abfd
= h
->root
.u
.undef
.abfd
;
10575 || (abfd
->flags
& DYNAMIC
) == 0
10576 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
10580 case bfd_link_hash_defined
:
10581 case bfd_link_hash_defweak
:
10582 abfd
= h
->root
.u
.def
.section
->owner
;
10585 case bfd_link_hash_common
:
10586 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10589 BFD_ASSERT (abfd
!= NULL
);
10591 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10593 loaded
= loaded
->next
)
10596 Elf_Internal_Shdr
*hdr
;
10598 size_t extsymcount
;
10600 Elf_Internal_Shdr
*versymhdr
;
10601 Elf_Internal_Sym
*isym
;
10602 Elf_Internal_Sym
*isymend
;
10603 Elf_Internal_Sym
*isymbuf
;
10604 Elf_External_Versym
*ever
;
10605 Elf_External_Versym
*extversym
;
10607 input
= loaded
->abfd
;
10609 /* We check each DSO for a possible hidden versioned definition. */
10611 || elf_dynversym (input
) == 0)
10614 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10616 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10617 if (elf_bad_symtab (input
))
10619 extsymcount
= symcount
;
10624 extsymcount
= symcount
- hdr
->sh_info
;
10625 extsymoff
= hdr
->sh_info
;
10628 if (extsymcount
== 0)
10631 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10633 if (isymbuf
== NULL
)
10636 /* Read in any version definitions. */
10637 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10638 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10639 || (extversym
= (Elf_External_Versym
*)
10640 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10641 versymhdr
->sh_size
)) == NULL
)
10647 ever
= extversym
+ extsymoff
;
10648 isymend
= isymbuf
+ extsymcount
;
10649 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10652 Elf_Internal_Versym iver
;
10653 unsigned short version_index
;
10655 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10656 || isym
->st_shndx
== SHN_UNDEF
)
10659 name
= bfd_elf_string_from_elf_section (input
,
10662 if (strcmp (name
, h
->root
.root
.string
) != 0)
10665 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10667 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10668 && !(h
->def_regular
10669 && h
->forced_local
))
10671 /* If we have a non-hidden versioned sym, then it should
10672 have provided a definition for the undefined sym unless
10673 it is defined in a non-shared object and forced local.
10678 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10679 if (version_index
== 1 || version_index
== 2)
10681 /* This is the base or first version. We can use it. */
10695 /* Convert ELF common symbol TYPE. */
10698 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10700 /* Commom symbol can only appear in relocatable link. */
10701 if (!bfd_link_relocatable (info
))
10703 switch (info
->elf_stt_common
)
10707 case elf_stt_common
:
10710 case no_elf_stt_common
:
10717 /* Add an external symbol to the symbol table. This is called from
10718 the hash table traversal routine. When generating a shared object,
10719 we go through the symbol table twice. The first time we output
10720 anything that might have been forced to local scope in a version
10721 script. The second time we output the symbols that are still
10725 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10727 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10728 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10729 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10731 Elf_Internal_Sym sym
;
10732 asection
*input_sec
;
10733 const struct elf_backend_data
*bed
;
10738 if (h
->root
.type
== bfd_link_hash_warning
)
10740 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10741 if (h
->root
.type
== bfd_link_hash_new
)
10745 /* Decide whether to output this symbol in this pass. */
10746 if (eoinfo
->localsyms
)
10748 if (!h
->forced_local
)
10753 if (h
->forced_local
)
10757 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10759 if (h
->root
.type
== bfd_link_hash_undefined
)
10761 /* If we have an undefined symbol reference here then it must have
10762 come from a shared library that is being linked in. (Undefined
10763 references in regular files have already been handled unless
10764 they are in unreferenced sections which are removed by garbage
10766 bool ignore_undef
= false;
10768 /* Some symbols may be special in that the fact that they're
10769 undefined can be safely ignored - let backend determine that. */
10770 if (bed
->elf_backend_ignore_undef_symbol
)
10771 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10773 /* If we are reporting errors for this situation then do so now. */
10775 && h
->ref_dynamic_nonweak
10776 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10777 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10778 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10780 flinfo
->info
->callbacks
->undefined_symbol
10781 (flinfo
->info
, h
->root
.root
.string
,
10782 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10783 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10784 && !flinfo
->info
->warn_unresolved_syms
);
10787 /* Strip a global symbol defined in a discarded section. */
10792 /* We should also warn if a forced local symbol is referenced from
10793 shared libraries. */
10794 if (bfd_link_executable (flinfo
->info
)
10799 && h
->ref_dynamic_nonweak
10800 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10804 struct elf_link_hash_entry
*hi
= h
;
10806 /* Check indirect symbol. */
10807 while (hi
->root
.type
== bfd_link_hash_indirect
)
10808 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10810 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10811 /* xgettext:c-format */
10812 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10813 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10814 /* xgettext:c-format */
10815 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10817 /* xgettext:c-format */
10818 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10819 def_bfd
= flinfo
->output_bfd
;
10820 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10821 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10822 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10823 h
->root
.root
.string
, def_bfd
);
10824 bfd_set_error (bfd_error_bad_value
);
10825 eoinfo
->failed
= true;
10829 /* We don't want to output symbols that have never been mentioned by
10830 a regular file, or that we have been told to strip. However, if
10831 h->indx is set to -2, the symbol is used by a reloc and we must
10836 else if ((h
->def_dynamic
10838 || h
->root
.type
== bfd_link_hash_new
)
10840 && !h
->ref_regular
)
10842 else if (flinfo
->info
->strip
== strip_all
)
10844 else if (flinfo
->info
->strip
== strip_some
10845 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10846 h
->root
.root
.string
, false, false) == NULL
)
10848 else if ((h
->root
.type
== bfd_link_hash_defined
10849 || h
->root
.type
== bfd_link_hash_defweak
)
10850 && ((flinfo
->info
->strip_discarded
10851 && discarded_section (h
->root
.u
.def
.section
))
10852 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10853 && h
->root
.u
.def
.section
->owner
!= NULL
10854 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10856 else if ((h
->root
.type
== bfd_link_hash_undefined
10857 || h
->root
.type
== bfd_link_hash_undefweak
)
10858 && h
->root
.u
.undef
.abfd
!= NULL
10859 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10862 /* Remember if this symbol should be stripped. */
10863 bool should_strip
= strip
;
10865 /* Strip undefined weak symbols link if they don't have relocation. */
10867 strip
= !h
->has_reloc
&& h
->root
.type
== bfd_link_hash_undefweak
;
10871 /* If we're stripping it, and it's not a dynamic symbol, there's
10872 nothing else to do. However, if it is a forced local symbol or
10873 an ifunc symbol we need to give the backend finish_dynamic_symbol
10874 function a chance to make it dynamic. */
10876 && h
->dynindx
== -1
10877 && type
!= STT_GNU_IFUNC
10878 && !h
->forced_local
)
10882 sym
.st_size
= h
->size
;
10883 sym
.st_other
= h
->other
;
10884 switch (h
->root
.type
)
10887 case bfd_link_hash_new
:
10888 case bfd_link_hash_warning
:
10892 case bfd_link_hash_undefined
:
10893 case bfd_link_hash_undefweak
:
10894 input_sec
= bfd_und_section_ptr
;
10895 sym
.st_shndx
= SHN_UNDEF
;
10898 case bfd_link_hash_defined
:
10899 case bfd_link_hash_defweak
:
10901 input_sec
= h
->root
.u
.def
.section
;
10902 if (input_sec
->output_section
!= NULL
)
10905 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10906 input_sec
->output_section
);
10907 if (sym
.st_shndx
== SHN_BAD
)
10910 /* xgettext:c-format */
10911 (_("%pB: could not find output section %pA for input section %pA"),
10912 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10913 bfd_set_error (bfd_error_nonrepresentable_section
);
10914 eoinfo
->failed
= true;
10918 /* ELF symbols in relocatable files are section relative,
10919 but in nonrelocatable files they are virtual
10921 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10922 if (!bfd_link_relocatable (flinfo
->info
))
10924 sym
.st_value
+= input_sec
->output_section
->vma
;
10925 if (h
->type
== STT_TLS
)
10927 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10928 if (tls_sec
!= NULL
)
10929 sym
.st_value
-= tls_sec
->vma
;
10935 BFD_ASSERT (input_sec
->owner
== NULL
10936 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10937 sym
.st_shndx
= SHN_UNDEF
;
10938 input_sec
= bfd_und_section_ptr
;
10943 case bfd_link_hash_common
:
10944 input_sec
= h
->root
.u
.c
.p
->section
;
10945 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10946 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10949 case bfd_link_hash_indirect
:
10950 /* These symbols are created by symbol versioning. They point
10951 to the decorated version of the name. For example, if the
10952 symbol foo@@GNU_1.2 is the default, which should be used when
10953 foo is used with no version, then we add an indirect symbol
10954 foo which points to foo@@GNU_1.2. We ignore these symbols,
10955 since the indirected symbol is already in the hash table. */
10959 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10960 switch (h
->root
.type
)
10962 case bfd_link_hash_common
:
10963 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10965 case bfd_link_hash_defined
:
10966 case bfd_link_hash_defweak
:
10967 if (bed
->common_definition (&sym
))
10968 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10972 case bfd_link_hash_undefined
:
10973 case bfd_link_hash_undefweak
:
10979 if (h
->forced_local
)
10981 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10982 /* Turn off visibility on local symbol. */
10983 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10985 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10986 else if (h
->unique_global
&& h
->def_regular
)
10987 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10988 else if (h
->root
.type
== bfd_link_hash_undefweak
10989 || h
->root
.type
== bfd_link_hash_defweak
)
10990 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10992 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10993 sym
.st_target_internal
= h
->target_internal
;
10995 /* Give the processor backend a chance to tweak the symbol value,
10996 and also to finish up anything that needs to be done for this
10997 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10998 forced local syms when non-shared is due to a historical quirk.
10999 STT_GNU_IFUNC symbol must go through PLT. */
11000 if ((h
->type
== STT_GNU_IFUNC
11002 && !bfd_link_relocatable (flinfo
->info
))
11003 || ((h
->dynindx
!= -1
11004 || h
->forced_local
)
11005 && ((bfd_link_pic (flinfo
->info
)
11006 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
11007 || h
->root
.type
!= bfd_link_hash_undefweak
))
11008 || !h
->forced_local
)
11009 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
11011 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
11012 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
11014 eoinfo
->failed
= true;
11017 /* If a symbol is in the dynamic symbol table and isn't a
11018 should-strip symbol, also keep it in the symbol table. */
11023 /* If we are marking the symbol as undefined, and there are no
11024 non-weak references to this symbol from a regular object, then
11025 mark the symbol as weak undefined; if there are non-weak
11026 references, mark the symbol as strong. We can't do this earlier,
11027 because it might not be marked as undefined until the
11028 finish_dynamic_symbol routine gets through with it. */
11029 if (sym
.st_shndx
== SHN_UNDEF
11031 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
11032 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
11035 type
= ELF_ST_TYPE (sym
.st_info
);
11037 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
11038 if (type
== STT_GNU_IFUNC
)
11041 if (h
->ref_regular_nonweak
)
11042 bindtype
= STB_GLOBAL
;
11044 bindtype
= STB_WEAK
;
11045 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
11048 /* If this is a symbol defined in a dynamic library, don't use the
11049 symbol size from the dynamic library. Relinking an executable
11050 against a new library may introduce gratuitous changes in the
11051 executable's symbols if we keep the size. */
11052 if (sym
.st_shndx
== SHN_UNDEF
11057 /* If a non-weak symbol with non-default visibility is not defined
11058 locally, it is a fatal error. */
11059 if (!bfd_link_relocatable (flinfo
->info
)
11060 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
11061 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
11062 && h
->root
.type
== bfd_link_hash_undefined
11063 && !h
->def_regular
)
11067 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
11068 /* xgettext:c-format */
11069 msg
= _("%pB: protected symbol `%s' isn't defined");
11070 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
11071 /* xgettext:c-format */
11072 msg
= _("%pB: internal symbol `%s' isn't defined");
11074 /* xgettext:c-format */
11075 msg
= _("%pB: hidden symbol `%s' isn't defined");
11076 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
11077 bfd_set_error (bfd_error_bad_value
);
11078 eoinfo
->failed
= true;
11082 /* If this symbol should be put in the .dynsym section, then put it
11083 there now. We already know the symbol index. We also fill in
11084 the entry in the .hash section. */
11085 if (h
->dynindx
!= -1
11086 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
11087 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
11088 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
11092 /* Since there is no version information in the dynamic string,
11093 if there is no version info in symbol version section, we will
11094 have a run-time problem if not linking executable, referenced
11095 by shared library, or not bound locally. */
11096 if (h
->verinfo
.verdef
== NULL
11097 && (!bfd_link_executable (flinfo
->info
)
11099 || !h
->def_regular
))
11101 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
11103 if (p
&& p
[1] != '\0')
11106 /* xgettext:c-format */
11107 (_("%pB: no symbol version section for versioned symbol `%s'"),
11108 flinfo
->output_bfd
, h
->root
.root
.string
);
11109 eoinfo
->failed
= true;
11114 sym
.st_name
= h
->dynstr_index
;
11115 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
11116 + h
->dynindx
* bed
->s
->sizeof_sym
);
11117 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
11119 eoinfo
->failed
= true;
11123 /* Inform the linker of the addition of this symbol. */
11125 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
11126 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
11128 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
11130 if (flinfo
->hash_sec
!= NULL
)
11132 size_t hash_entry_size
;
11133 bfd_byte
*bucketpos
;
11135 size_t bucketcount
;
11138 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
11139 bucket
= h
->u
.elf_hash_value
% bucketcount
;
11142 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
11143 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
11144 + (bucket
+ 2) * hash_entry_size
);
11145 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
11146 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
11148 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
11149 ((bfd_byte
*) flinfo
->hash_sec
->contents
11150 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
11153 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
11155 Elf_Internal_Versym iversym
;
11156 Elf_External_Versym
*eversym
;
11158 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
11160 if (h
->verinfo
.verdef
== NULL
11161 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
11162 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
11163 iversym
.vs_vers
= 1;
11165 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
11169 if (h
->verinfo
.vertree
== NULL
)
11170 iversym
.vs_vers
= 1;
11172 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
11173 if (flinfo
->info
->create_default_symver
)
11177 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
11178 defined locally. */
11179 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
11180 iversym
.vs_vers
|= VERSYM_HIDDEN
;
11182 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
11183 eversym
+= h
->dynindx
;
11184 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
11188 /* If the symbol is undefined, and we didn't output it to .dynsym,
11189 strip it from .symtab too. Obviously we can't do this for
11190 relocatable output or when needed for --emit-relocs. */
11191 else if (input_sec
== bfd_und_section_ptr
11193 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
11194 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
11195 && !bfd_link_relocatable (flinfo
->info
))
11198 /* Also strip others that we couldn't earlier due to dynamic symbol
11202 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
11205 /* Output a FILE symbol so that following locals are not associated
11206 with the wrong input file. We need one for forced local symbols
11207 if we've seen more than one FILE symbol or when we have exactly
11208 one FILE symbol but global symbols are present in a file other
11209 than the one with the FILE symbol. We also need one if linker
11210 defined symbols are present. In practice these conditions are
11211 always met, so just emit the FILE symbol unconditionally. */
11212 if (eoinfo
->localsyms
11213 && !eoinfo
->file_sym_done
11214 && eoinfo
->flinfo
->filesym_count
!= 0)
11216 Elf_Internal_Sym fsym
;
11218 memset (&fsym
, 0, sizeof (fsym
));
11219 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11220 fsym
.st_shndx
= SHN_ABS
;
11221 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
11222 bfd_und_section_ptr
, NULL
))
11225 eoinfo
->file_sym_done
= true;
11228 indx
= bfd_get_symcount (flinfo
->output_bfd
);
11229 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
11233 eoinfo
->failed
= true;
11238 else if (h
->indx
== -2)
11244 /* Return TRUE if special handling is done for relocs in SEC against
11245 symbols defined in discarded sections. */
11248 elf_section_ignore_discarded_relocs (asection
*sec
)
11250 const struct elf_backend_data
*bed
;
11252 switch (sec
->sec_info_type
)
11254 case SEC_INFO_TYPE_STABS
:
11255 case SEC_INFO_TYPE_EH_FRAME
:
11256 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11257 case SEC_INFO_TYPE_SFRAME
:
11263 bed
= get_elf_backend_data (sec
->owner
);
11264 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
11265 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
11271 /* Return a mask saying how ld should treat relocations in SEC against
11272 symbols defined in discarded sections. If this function returns
11273 COMPLAIN set, ld will issue a warning message. If this function
11274 returns PRETEND set, and the discarded section was link-once and the
11275 same size as the kept link-once section, ld will pretend that the
11276 symbol was actually defined in the kept section. Otherwise ld will
11277 zero the reloc (at least that is the intent, but some cooperation by
11278 the target dependent code is needed, particularly for REL targets). */
11281 _bfd_elf_default_action_discarded (asection
*sec
)
11283 const struct elf_backend_data
*bed
;
11284 bed
= get_elf_backend_data (sec
->owner
);
11286 if (sec
->flags
& SEC_DEBUGGING
)
11289 if (strcmp (".eh_frame", sec
->name
) == 0)
11292 if (bed
->elf_backend_can_make_multiple_eh_frame
11293 && strncmp (sec
->name
, ".eh_frame.", 10) == 0)
11296 if (strcmp (".sframe", sec
->name
) == 0)
11299 if (strcmp (".gcc_except_table", sec
->name
) == 0)
11302 return COMPLAIN
| PRETEND
;
11305 /* Find a match between a section and a member of a section group. */
11308 match_group_member (asection
*sec
, asection
*group
,
11309 struct bfd_link_info
*info
)
11311 asection
*first
= elf_next_in_group (group
);
11312 asection
*s
= first
;
11316 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
11319 s
= elf_next_in_group (s
);
11327 /* Check if the kept section of a discarded section SEC can be used
11328 to replace it. Return the replacement if it is OK. Otherwise return
11332 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
11336 kept
= sec
->kept_section
;
11339 if ((kept
->flags
& SEC_GROUP
) != 0)
11340 kept
= match_group_member (sec
, kept
, info
);
11343 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
11344 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
11348 /* Get the real kept section. */
11350 for (next
= kept
->kept_section
;
11352 next
= next
->kept_section
)
11356 sec
->kept_section
= kept
;
11361 /* Link an input file into the linker output file. This function
11362 handles all the sections and relocations of the input file at once.
11363 This is so that we only have to read the local symbols once, and
11364 don't have to keep them in memory. */
11367 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
11369 int (*relocate_section
)
11370 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
11371 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
11373 Elf_Internal_Shdr
*symtab_hdr
;
11374 size_t locsymcount
;
11376 Elf_Internal_Sym
*isymbuf
;
11377 Elf_Internal_Sym
*isym
;
11378 Elf_Internal_Sym
*isymend
;
11380 asection
**ppsection
;
11382 const struct elf_backend_data
*bed
;
11383 struct elf_link_hash_entry
**sym_hashes
;
11384 bfd_size_type address_size
;
11385 bfd_vma r_type_mask
;
11387 bool have_file_sym
= false;
11389 output_bfd
= flinfo
->output_bfd
;
11390 bed
= get_elf_backend_data (output_bfd
);
11391 relocate_section
= bed
->elf_backend_relocate_section
;
11393 /* If this is a dynamic object, we don't want to do anything here:
11394 we don't want the local symbols, and we don't want the section
11396 if ((input_bfd
->flags
& DYNAMIC
) != 0)
11399 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
11400 if (elf_bad_symtab (input_bfd
))
11402 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11407 locsymcount
= symtab_hdr
->sh_info
;
11408 extsymoff
= symtab_hdr
->sh_info
;
11411 /* Enable GNU OSABI features in the output BFD that are used in the input
11413 if (bed
->elf_osabi
== ELFOSABI_NONE
11414 || bed
->elf_osabi
== ELFOSABI_GNU
11415 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
11416 elf_tdata (output_bfd
)->has_gnu_osabi
11417 |= (elf_tdata (input_bfd
)->has_gnu_osabi
11418 & (bfd_link_relocatable (flinfo
->info
)
11419 ? -1 : ~elf_gnu_osabi_retain
));
11421 /* Read the local symbols. */
11422 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11423 if (isymbuf
== NULL
&& locsymcount
!= 0)
11425 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
11426 flinfo
->internal_syms
,
11427 flinfo
->external_syms
,
11428 flinfo
->locsym_shndx
);
11429 if (isymbuf
== NULL
)
11433 /* Find local symbol sections and adjust values of symbols in
11434 SEC_MERGE sections. Write out those local symbols we know are
11435 going into the output file. */
11436 isymend
= PTR_ADD (isymbuf
, locsymcount
);
11437 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
11439 isym
++, pindex
++, ppsection
++)
11443 Elf_Internal_Sym osym
;
11449 if (elf_bad_symtab (input_bfd
))
11451 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
11458 if (isym
->st_shndx
== SHN_UNDEF
)
11459 isec
= bfd_und_section_ptr
;
11460 else if (isym
->st_shndx
== SHN_ABS
)
11461 isec
= bfd_abs_section_ptr
;
11462 else if (isym
->st_shndx
== SHN_COMMON
)
11463 isec
= bfd_com_section_ptr
;
11466 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
11469 /* Don't attempt to output symbols with st_shnx in the
11470 reserved range other than SHN_ABS and SHN_COMMON. */
11471 isec
= bfd_und_section_ptr
;
11473 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
11474 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
11476 _bfd_merged_section_offset (output_bfd
, &isec
,
11477 elf_section_data (isec
)->sec_info
,
11483 /* Don't output the first, undefined, symbol. In fact, don't
11484 output any undefined local symbol. */
11485 if (isec
== bfd_und_section_ptr
)
11488 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
11490 /* We never output section symbols. Instead, we use the
11491 section symbol of the corresponding section in the output
11496 /* If we are stripping all symbols, we don't want to output this
11498 if (flinfo
->info
->strip
== strip_all
)
11501 /* If we are discarding all local symbols, we don't want to
11502 output this one. If we are generating a relocatable output
11503 file, then some of the local symbols may be required by
11504 relocs; we output them below as we discover that they are
11506 if (flinfo
->info
->discard
== discard_all
)
11509 /* If this symbol is defined in a section which we are
11510 discarding, we don't need to keep it. */
11511 if (isym
->st_shndx
< SHN_LORESERVE
11512 && (isec
->output_section
== NULL
11513 || bfd_section_removed_from_list (output_bfd
,
11514 isec
->output_section
)))
11517 /* Get the name of the symbol. */
11518 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
11523 /* See if we are discarding symbols with this name. */
11524 if ((flinfo
->info
->strip
== strip_some
11525 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, false, false)
11527 || (((flinfo
->info
->discard
== discard_sec_merge
11528 && (isec
->flags
& SEC_MERGE
)
11529 && !bfd_link_relocatable (flinfo
->info
))
11530 || flinfo
->info
->discard
== discard_l
)
11531 && bfd_is_local_label_name (input_bfd
, name
)))
11534 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
11536 if (input_bfd
->lto_output
)
11537 /* -flto puts a temp file name here. This means builds
11538 are not reproducible. Discard the symbol. */
11540 have_file_sym
= true;
11541 flinfo
->filesym_count
+= 1;
11543 if (!have_file_sym
)
11545 /* In the absence of debug info, bfd_find_nearest_line uses
11546 FILE symbols to determine the source file for local
11547 function symbols. Provide a FILE symbol here if input
11548 files lack such, so that their symbols won't be
11549 associated with a previous input file. It's not the
11550 source file, but the best we can do. */
11551 const char *filename
;
11552 have_file_sym
= true;
11553 flinfo
->filesym_count
+= 1;
11554 memset (&osym
, 0, sizeof (osym
));
11555 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11556 osym
.st_shndx
= SHN_ABS
;
11557 if (input_bfd
->lto_output
)
11560 filename
= lbasename (bfd_get_filename (input_bfd
));
11561 if (!elf_link_output_symstrtab (flinfo
, filename
, &osym
,
11562 bfd_abs_section_ptr
, NULL
))
11568 /* Adjust the section index for the output file. */
11569 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11570 isec
->output_section
);
11571 if (osym
.st_shndx
== SHN_BAD
)
11574 /* ELF symbols in relocatable files are section relative, but
11575 in executable files they are virtual addresses. Note that
11576 this code assumes that all ELF sections have an associated
11577 BFD section with a reasonable value for output_offset; below
11578 we assume that they also have a reasonable value for
11579 output_section. Any special sections must be set up to meet
11580 these requirements. */
11581 osym
.st_value
+= isec
->output_offset
;
11582 if (!bfd_link_relocatable (flinfo
->info
))
11584 osym
.st_value
+= isec
->output_section
->vma
;
11585 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
11587 /* STT_TLS symbols are relative to PT_TLS segment base. */
11588 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
11589 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
11591 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
11596 indx
= bfd_get_symcount (output_bfd
);
11597 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11604 if (bed
->s
->arch_size
== 32)
11606 r_type_mask
= 0xff;
11612 r_type_mask
= 0xffffffff;
11617 /* Relocate the contents of each section. */
11618 sym_hashes
= elf_sym_hashes (input_bfd
);
11619 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11621 bfd_byte
*contents
;
11623 if (! o
->linker_mark
)
11625 /* This section was omitted from the link. */
11629 if (!flinfo
->info
->resolve_section_groups
11630 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11632 /* Deal with the group signature symbol. */
11633 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11634 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11635 asection
*osec
= o
->output_section
;
11637 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11638 if (symndx
>= locsymcount
11639 || (elf_bad_symtab (input_bfd
)
11640 && flinfo
->sections
[symndx
] == NULL
))
11642 struct elf_link_hash_entry
*h
;
11644 h
= get_link_hash_entry (sym_hashes
, symndx
, extsymoff
);
11648 /* xgettext:c-format */
11649 (_("error: %pB: unable to create group section symbol"),
11651 bfd_set_error (bfd_error_bad_value
);
11655 /* Arrange for symbol to be output. */
11657 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11659 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11661 /* We'll use the output section target_index. */
11662 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11663 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11667 if (flinfo
->indices
[symndx
] == -1)
11669 /* Otherwise output the local symbol now. */
11670 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11671 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11676 name
= bfd_elf_string_from_elf_section (input_bfd
,
11677 symtab_hdr
->sh_link
,
11682 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11684 if (sym
.st_shndx
== SHN_BAD
)
11687 sym
.st_value
+= o
->output_offset
;
11689 indx
= bfd_get_symcount (output_bfd
);
11690 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11695 flinfo
->indices
[symndx
] = indx
;
11699 elf_section_data (osec
)->this_hdr
.sh_info
11700 = flinfo
->indices
[symndx
];
11704 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11705 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11708 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11710 /* Section was created by _bfd_elf_link_create_dynamic_sections
11715 /* Get the contents of the section. They have been cached by a
11716 relaxation routine. Note that o is a section in an input
11717 file, so the contents field will not have been set by any of
11718 the routines which work on output files. */
11719 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11721 contents
= elf_section_data (o
)->this_hdr
.contents
;
11722 if (bed
->caches_rawsize
11724 && o
->rawsize
< o
->size
)
11726 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11727 contents
= flinfo
->contents
;
11730 else if (!(o
->flags
& SEC_RELOC
)
11731 && !bed
->elf_backend_write_section
11732 && o
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
11733 /* A MERGE section that has no relocations doesn't need the
11734 contents anymore, they have been recorded earlier. Except
11735 if the backend has special provisions for writing sections. */
11739 contents
= flinfo
->contents
;
11740 if (! _bfd_elf_link_mmap_section_contents (input_bfd
, o
,
11745 if ((o
->flags
& SEC_RELOC
) != 0)
11747 Elf_Internal_Rela
*internal_relocs
;
11748 Elf_Internal_Rela
*rel
, *relend
;
11749 int action_discarded
;
11752 /* Get the swapped relocs. */
11754 = _bfd_elf_link_info_read_relocs (input_bfd
, flinfo
->info
, o
,
11755 flinfo
->external_relocs
,
11756 flinfo
->internal_relocs
,
11758 if (internal_relocs
== NULL
11759 && o
->reloc_count
> 0)
11762 action_discarded
= -1;
11763 if (!elf_section_ignore_discarded_relocs (o
))
11764 action_discarded
= (*bed
->action_discarded
) (o
);
11766 /* Run through the relocs evaluating complex reloc symbols and
11767 looking for relocs against symbols from discarded sections
11768 or section symbols from removed link-once sections.
11769 Complain about relocs against discarded sections. Zero
11770 relocs against removed link-once sections. */
11772 rel
= internal_relocs
;
11773 relend
= rel
+ o
->reloc_count
;
11774 for ( ; rel
< relend
; rel
++)
11776 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11777 unsigned int s_type
;
11778 asection
**ps
, *sec
;
11779 struct elf_link_hash_entry
*h
= NULL
;
11780 const char *sym_name
;
11782 if (r_symndx
== STN_UNDEF
)
11785 if (r_symndx
>= locsymcount
11786 || (elf_bad_symtab (input_bfd
)
11787 && flinfo
->sections
[r_symndx
] == NULL
))
11789 h
= get_link_hash_entry (sym_hashes
, r_symndx
, extsymoff
);
11791 /* Badly formatted input files can contain relocs that
11792 reference non-existant symbols. Check here so that
11793 we do not seg fault. */
11797 /* xgettext:c-format */
11798 (_("error: %pB contains a reloc (%#" PRIx64
") for section '%pA' "
11799 "that references a non-existent global symbol"),
11800 input_bfd
, (uint64_t) rel
->r_info
, o
);
11801 bfd_set_error (bfd_error_bad_value
);
11807 /* If a plugin symbol is referenced from a non-IR file,
11808 mark the symbol as undefined. Note that the
11809 linker may attach linker created dynamic sections
11810 to the plugin bfd. Symbols defined in linker
11811 created sections are not plugin symbols. */
11812 if ((h
->root
.non_ir_ref_regular
11813 || h
->root
.non_ir_ref_dynamic
)
11814 && (h
->root
.type
== bfd_link_hash_defined
11815 || h
->root
.type
== bfd_link_hash_defweak
)
11816 && (h
->root
.u
.def
.section
->flags
11817 & SEC_LINKER_CREATED
) == 0
11818 && h
->root
.u
.def
.section
->owner
!= NULL
11819 && (h
->root
.u
.def
.section
->owner
->flags
11820 & BFD_PLUGIN
) != 0)
11822 h
->root
.type
= bfd_link_hash_undefined
;
11823 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11827 if (h
->root
.type
== bfd_link_hash_defined
11828 || h
->root
.type
== bfd_link_hash_defweak
)
11829 ps
= &h
->root
.u
.def
.section
;
11831 sym_name
= h
->root
.root
.string
;
11835 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11837 s_type
= ELF_ST_TYPE (sym
->st_info
);
11838 ps
= &flinfo
->sections
[r_symndx
];
11839 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11843 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11844 && !bfd_link_relocatable (flinfo
->info
))
11847 bfd_vma dot
= (rel
->r_offset
11848 + o
->output_offset
+ o
->output_section
->vma
);
11850 printf ("Encountered a complex symbol!");
11851 printf (" (input_bfd %s, section %s, reloc %ld\n",
11852 bfd_get_filename (input_bfd
), o
->name
,
11853 (long) (rel
- internal_relocs
));
11854 printf (" symbol: idx %8.8lx, name %s\n",
11855 r_symndx
, sym_name
);
11856 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11857 (unsigned long) rel
->r_info
,
11858 (unsigned long) rel
->r_offset
);
11860 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11861 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11864 /* Symbol evaluated OK. Update to absolute value. */
11865 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11870 if (action_discarded
!= -1 && ps
!= NULL
)
11872 /* Complain if the definition comes from a
11873 discarded section. */
11874 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11876 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11877 if (action_discarded
& COMPLAIN
)
11878 (*flinfo
->info
->callbacks
->einfo
)
11879 /* xgettext:c-format */
11880 (_("%X`%s' referenced in section `%pA' of %pB: "
11881 "defined in discarded section `%pA' of %pB\n"),
11882 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11884 /* Try to do the best we can to support buggy old
11885 versions of gcc. Pretend that the symbol is
11886 really defined in the kept linkonce section.
11887 FIXME: This is quite broken. Modifying the
11888 symbol here means we will be changing all later
11889 uses of the symbol, not just in this section. */
11890 if (action_discarded
& PRETEND
)
11894 kept
= _bfd_elf_check_kept_section (sec
,
11906 /* Relocate the section by invoking a back end routine.
11908 The back end routine is responsible for adjusting the
11909 section contents as necessary, and (if using Rela relocs
11910 and generating a relocatable output file) adjusting the
11911 reloc addend as necessary.
11913 The back end routine does not have to worry about setting
11914 the reloc address or the reloc symbol index.
11916 The back end routine is given a pointer to the swapped in
11917 internal symbols, and can access the hash table entries
11918 for the external symbols via elf_sym_hashes (input_bfd).
11920 When generating relocatable output, the back end routine
11921 must handle STB_LOCAL/STT_SECTION symbols specially. The
11922 output symbol is going to be a section symbol
11923 corresponding to the output section, which will require
11924 the addend to be adjusted. */
11926 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11927 input_bfd
, o
, contents
,
11935 || bfd_link_relocatable (flinfo
->info
)
11936 || flinfo
->info
->emitrelocations
)
11938 Elf_Internal_Rela
*irela
;
11939 Elf_Internal_Rela
*irelaend
, *irelamid
;
11940 bfd_vma last_offset
;
11941 struct elf_link_hash_entry
**rel_hash
;
11942 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11943 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11944 unsigned int next_erel
;
11946 struct bfd_elf_section_data
*esdi
, *esdo
;
11948 esdi
= elf_section_data (o
);
11949 esdo
= elf_section_data (o
->output_section
);
11950 rela_normal
= false;
11952 /* Adjust the reloc addresses and symbol indices. */
11954 irela
= internal_relocs
;
11955 irelaend
= irela
+ o
->reloc_count
;
11956 rel_hash
= PTR_ADD (esdo
->rel
.hashes
, esdo
->rel
.count
);
11957 /* We start processing the REL relocs, if any. When we reach
11958 IRELAMID in the loop, we switch to the RELA relocs. */
11960 if (esdi
->rel
.hdr
!= NULL
)
11961 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11962 * bed
->s
->int_rels_per_ext_rel
);
11963 rel_hash_list
= rel_hash
;
11964 rela_hash_list
= NULL
;
11965 last_offset
= o
->output_offset
;
11966 if (!bfd_link_relocatable (flinfo
->info
))
11967 last_offset
+= o
->output_section
->vma
;
11968 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11970 unsigned long r_symndx
;
11972 Elf_Internal_Sym sym
;
11974 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11980 if (irela
== irelamid
)
11982 rel_hash
= PTR_ADD (esdo
->rela
.hashes
, esdo
->rela
.count
);
11983 rela_hash_list
= rel_hash
;
11984 rela_normal
= bed
->rela_normal
;
11987 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11990 if (irela
->r_offset
>= (bfd_vma
) -2)
11992 /* This is a reloc for a deleted entry or somesuch.
11993 Turn it into an R_*_NONE reloc, at the same
11994 offset as the last reloc. elf_eh_frame.c and
11995 bfd_elf_discard_info rely on reloc offsets
11997 irela
->r_offset
= last_offset
;
11999 irela
->r_addend
= 0;
12003 irela
->r_offset
+= o
->output_offset
;
12005 /* Relocs in an executable have to be virtual addresses. */
12006 if (!bfd_link_relocatable (flinfo
->info
))
12007 irela
->r_offset
+= o
->output_section
->vma
;
12009 last_offset
= irela
->r_offset
;
12011 r_symndx
= irela
->r_info
>> r_sym_shift
;
12012 if (r_symndx
== STN_UNDEF
)
12015 if (r_symndx
>= locsymcount
12016 || (elf_bad_symtab (input_bfd
)
12017 && flinfo
->sections
[r_symndx
] == NULL
))
12019 struct elf_link_hash_entry
*rh
;
12021 /* This is a reloc against a global symbol. We
12022 have not yet output all the local symbols, so
12023 we do not know the symbol index of any global
12024 symbol. We set the rel_hash entry for this
12025 reloc to point to the global hash table entry
12026 for this symbol. The symbol index is then
12027 set at the end of bfd_elf_final_link. */
12028 rh
= get_link_hash_entry (elf_sym_hashes (input_bfd
),
12029 r_symndx
, extsymoff
);
12032 /* FIXME: Generate an error ? */
12036 /* Setting the index to -2 tells elf_link_output_extsym
12037 that this symbol is used by a reloc. */
12038 BFD_ASSERT (rh
->indx
< 0);
12045 /* This is a reloc against a local symbol. */
12048 sym
= isymbuf
[r_symndx
];
12049 sec
= flinfo
->sections
[r_symndx
];
12050 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
12052 /* I suppose the backend ought to fill in the
12053 section of any STT_SECTION symbol against a
12054 processor specific section. */
12055 r_symndx
= STN_UNDEF
;
12056 if (bfd_is_abs_section (sec
))
12058 else if (sec
== NULL
|| sec
->owner
== NULL
)
12060 bfd_set_error (bfd_error_bad_value
);
12065 asection
*osec
= sec
->output_section
;
12067 /* If we have discarded a section, the output
12068 section will be the absolute section. In
12069 case of discarded SEC_MERGE sections, use
12070 the kept section. relocate_section should
12071 have already handled discarded linkonce
12073 if (bfd_is_abs_section (osec
)
12074 && sec
->kept_section
!= NULL
12075 && sec
->kept_section
->output_section
!= NULL
)
12077 osec
= sec
->kept_section
->output_section
;
12078 irela
->r_addend
-= osec
->vma
;
12081 if (!bfd_is_abs_section (osec
))
12083 r_symndx
= osec
->target_index
;
12084 if (r_symndx
== STN_UNDEF
)
12086 irela
->r_addend
+= osec
->vma
;
12087 osec
= _bfd_nearby_section (output_bfd
, osec
,
12089 irela
->r_addend
-= osec
->vma
;
12090 r_symndx
= osec
->target_index
;
12095 /* Adjust the addend according to where the
12096 section winds up in the output section. */
12098 irela
->r_addend
+= sec
->output_offset
;
12102 if (flinfo
->indices
[r_symndx
] == -1)
12104 unsigned long shlink
;
12109 if (flinfo
->info
->strip
== strip_all
)
12111 /* You can't do ld -r -s. */
12112 bfd_set_error (bfd_error_invalid_operation
);
12116 /* This symbol was skipped earlier, but
12117 since it is needed by a reloc, we
12118 must output it now. */
12119 shlink
= symtab_hdr
->sh_link
;
12120 name
= (bfd_elf_string_from_elf_section
12121 (input_bfd
, shlink
, sym
.st_name
));
12125 osec
= sec
->output_section
;
12127 _bfd_elf_section_from_bfd_section (output_bfd
,
12129 if (sym
.st_shndx
== SHN_BAD
)
12132 sym
.st_value
+= sec
->output_offset
;
12133 if (!bfd_link_relocatable (flinfo
->info
))
12135 sym
.st_value
+= osec
->vma
;
12136 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
12138 struct elf_link_hash_table
*htab
12139 = elf_hash_table (flinfo
->info
);
12141 /* STT_TLS symbols are relative to PT_TLS
12143 if (htab
->tls_sec
!= NULL
)
12144 sym
.st_value
-= htab
->tls_sec
->vma
;
12147 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
12152 indx
= bfd_get_symcount (output_bfd
);
12153 ret
= elf_link_output_symstrtab (flinfo
, name
,
12159 flinfo
->indices
[r_symndx
] = indx
;
12164 r_symndx
= flinfo
->indices
[r_symndx
];
12167 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
12168 | (irela
->r_info
& r_type_mask
));
12171 /* Swap out the relocs. */
12172 input_rel_hdr
= esdi
->rel
.hdr
;
12173 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
12175 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
12180 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
12181 * bed
->s
->int_rels_per_ext_rel
);
12182 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
12185 input_rela_hdr
= esdi
->rela
.hdr
;
12186 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
12188 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
12197 /* Write out the modified section contents. */
12198 if (bed
->elf_backend_write_section
12199 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
12202 /* Section written out. */
12204 else switch (o
->sec_info_type
)
12206 case SEC_INFO_TYPE_STABS
:
12207 if (! (_bfd_write_section_stabs
12209 &elf_hash_table (flinfo
->info
)->stab_info
,
12210 o
, &elf_section_data (o
)->sec_info
, contents
)))
12213 case SEC_INFO_TYPE_MERGE
:
12214 if (! _bfd_write_merged_section (output_bfd
, o
,
12215 elf_section_data (o
)->sec_info
))
12218 case SEC_INFO_TYPE_EH_FRAME
:
12220 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
12225 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
12227 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
12233 case SEC_INFO_TYPE_SFRAME
:
12235 /* Merge SFrame section into the SFrame encoder context of the
12236 output_bfd's section. The final .sframe output section will
12237 be written out later. */
12238 if (!_bfd_elf_merge_section_sframe (output_bfd
, flinfo
->info
,
12245 if (! (o
->flags
& SEC_EXCLUDE
))
12247 file_ptr offset
= (file_ptr
) o
->output_offset
;
12248 bfd_size_type todo
= o
->size
;
12250 offset
*= bfd_octets_per_byte (output_bfd
, o
);
12252 if ((o
->flags
& SEC_ELF_REVERSE_COPY
)
12253 && o
->size
> address_size
)
12255 /* Reverse-copy input section to output. */
12257 if ((o
->size
& (address_size
- 1)) != 0
12258 || (o
->reloc_count
!= 0
12259 && (o
->size
* bed
->s
->int_rels_per_ext_rel
12260 != o
->reloc_count
* address_size
)))
12263 /* xgettext:c-format */
12264 (_("error: %pB: size of section %pA is not "
12265 "multiple of address size"),
12267 bfd_set_error (bfd_error_bad_value
);
12273 todo
-= address_size
;
12274 if (! bfd_set_section_contents (output_bfd
,
12282 offset
+= address_size
;
12286 else if (! bfd_set_section_contents (output_bfd
,
12296 /* Munmap the section contents for each input section. */
12297 _bfd_elf_link_munmap_section_contents (o
);
12303 /* Generate a reloc when linking an ELF file. This is a reloc
12304 requested by the linker, and does not come from any input file. This
12305 is used to build constructor and destructor tables when linking
12309 elf_reloc_link_order (bfd
*output_bfd
,
12310 struct bfd_link_info
*info
,
12311 asection
*output_section
,
12312 struct bfd_link_order
*link_order
)
12314 reloc_howto_type
*howto
;
12318 struct bfd_elf_section_reloc_data
*reldata
;
12319 struct elf_link_hash_entry
**rel_hash_ptr
;
12320 Elf_Internal_Shdr
*rel_hdr
;
12321 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
12322 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
12325 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
12327 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
12330 bfd_set_error (bfd_error_bad_value
);
12334 addend
= link_order
->u
.reloc
.p
->addend
;
12337 reldata
= &esdo
->rel
;
12338 else if (esdo
->rela
.hdr
)
12339 reldata
= &esdo
->rela
;
12346 /* Figure out the symbol index. */
12347 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
12348 if (link_order
->type
== bfd_section_reloc_link_order
)
12350 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
12351 BFD_ASSERT (indx
!= 0);
12352 *rel_hash_ptr
= NULL
;
12356 struct elf_link_hash_entry
*h
;
12358 /* Treat a reloc against a defined symbol as though it were
12359 actually against the section. */
12360 h
= ((struct elf_link_hash_entry
*)
12361 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
12362 link_order
->u
.reloc
.p
->u
.name
,
12363 false, false, true));
12365 && (h
->root
.type
== bfd_link_hash_defined
12366 || h
->root
.type
== bfd_link_hash_defweak
))
12370 section
= h
->root
.u
.def
.section
;
12371 indx
= section
->output_section
->target_index
;
12372 *rel_hash_ptr
= NULL
;
12373 /* It seems that we ought to add the symbol value to the
12374 addend here, but in practice it has already been added
12375 because it was passed to constructor_callback. */
12376 addend
+= section
->output_section
->vma
+ section
->output_offset
;
12378 else if (h
!= NULL
)
12380 /* Setting the index to -2 tells elf_link_output_extsym that
12381 this symbol is used by a reloc. */
12388 (*info
->callbacks
->unattached_reloc
)
12389 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
12394 /* If this is an inplace reloc, we must write the addend into the
12396 if (howto
->partial_inplace
&& addend
!= 0)
12398 bfd_size_type size
;
12399 bfd_reloc_status_type rstat
;
12402 const char *sym_name
;
12403 bfd_size_type octets
;
12405 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
12406 buf
= (bfd_byte
*) bfd_zmalloc (size
);
12407 if (buf
== NULL
&& size
!= 0)
12409 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
12416 case bfd_reloc_outofrange
:
12419 case bfd_reloc_overflow
:
12420 if (link_order
->type
== bfd_section_reloc_link_order
)
12421 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
12423 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
12424 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
12425 howto
->name
, addend
, NULL
, NULL
,
12430 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
12432 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
12439 /* The address of a reloc is relative to the section in a
12440 relocatable file, and is a virtual address in an executable
12442 offset
= link_order
->offset
;
12443 if (! bfd_link_relocatable (info
))
12444 offset
+= output_section
->vma
;
12446 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
12448 irel
[i
].r_offset
= offset
;
12449 irel
[i
].r_info
= 0;
12450 irel
[i
].r_addend
= 0;
12452 if (bed
->s
->arch_size
== 32)
12453 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
12455 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
12457 rel_hdr
= reldata
->hdr
;
12458 erel
= rel_hdr
->contents
;
12459 if (rel_hdr
->sh_type
== SHT_REL
)
12461 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
12462 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
12466 irel
[0].r_addend
= addend
;
12467 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
12468 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
12476 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12477 Returns TRUE upon success, FALSE otherwise. */
12480 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
12484 const struct elf_backend_data
*bed
;
12486 enum bfd_architecture arch
;
12488 asymbol
**sympp
= NULL
;
12492 elf_symbol_type
*osymbuf
;
12495 implib_bfd
= info
->out_implib_bfd
;
12496 bed
= get_elf_backend_data (abfd
);
12498 if (!bfd_set_format (implib_bfd
, bfd_object
))
12501 /* Use flag from executable but make it a relocatable object. */
12502 flags
= bfd_get_file_flags (abfd
);
12503 flags
&= ~HAS_RELOC
;
12504 if (!bfd_set_start_address (implib_bfd
, 0)
12505 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
12508 /* Copy architecture of output file to import library file. */
12509 arch
= bfd_get_arch (abfd
);
12510 mach
= bfd_get_mach (abfd
);
12511 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
12512 && (abfd
->target_defaulted
12513 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
12516 /* Get symbol table size. */
12517 symsize
= bfd_get_symtab_upper_bound (abfd
);
12521 /* Read in the symbol table. */
12522 sympp
= (asymbol
**) bfd_malloc (symsize
);
12526 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
12530 /* Allow the BFD backend to copy any private header data it
12531 understands from the output BFD to the import library BFD. */
12532 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
12535 /* Filter symbols to appear in the import library. */
12536 if (bed
->elf_backend_filter_implib_symbols
)
12537 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
12540 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
12543 bfd_set_error (bfd_error_no_symbols
);
12544 _bfd_error_handler (_("%pB: no symbol found for import library"),
12550 /* Make symbols absolute. */
12551 amt
= symcount
* sizeof (*osymbuf
);
12552 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
12553 if (osymbuf
== NULL
)
12556 for (src_count
= 0; src_count
< symcount
; src_count
++)
12558 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
12559 sizeof (*osymbuf
));
12560 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
12561 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
12562 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
12563 osymbuf
[src_count
].internal_elf_sym
.st_value
=
12564 osymbuf
[src_count
].symbol
.value
;
12565 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
12568 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
12570 /* Allow the BFD backend to copy any private data it understands
12571 from the output BFD to the import library BFD. This is done last
12572 to permit the routine to look at the filtered symbol table. */
12573 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
12576 if (!bfd_close (implib_bfd
))
12587 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
12591 if (flinfo
->symstrtab
!= NULL
)
12592 _bfd_elf_strtab_free (flinfo
->symstrtab
);
12593 free (flinfo
->contents
);
12594 free (flinfo
->external_relocs
);
12595 free (flinfo
->internal_relocs
);
12596 free (flinfo
->external_syms
);
12597 free (flinfo
->locsym_shndx
);
12598 free (flinfo
->internal_syms
);
12599 free (flinfo
->indices
);
12600 free (flinfo
->sections
);
12601 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12602 free (flinfo
->symshndxbuf
);
12603 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12605 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12606 free (esdo
->rel
.hashes
);
12607 free (esdo
->rela
.hashes
);
12611 /* Do the final step of an ELF link. */
12614 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12619 struct elf_final_link_info flinfo
;
12621 struct bfd_link_order
*p
;
12623 bfd_size_type max_contents_size
;
12624 bfd_size_type max_external_reloc_size
;
12625 bfd_size_type max_internal_reloc_count
;
12626 bfd_size_type max_sym_count
;
12627 bfd_size_type max_sym_shndx_count
;
12628 Elf_Internal_Sym elfsym
;
12630 Elf_Internal_Shdr
*symtab_hdr
;
12631 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12632 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12633 struct elf_outext_info eoinfo
;
12635 size_t relativecount
;
12636 size_t relr_entsize
;
12637 asection
*reldyn
= 0;
12639 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12640 bool sections_removed
;
12642 if (!is_elf_hash_table (&htab
->root
))
12645 if (bfd_link_pic (info
))
12646 abfd
->flags
|= DYNAMIC
;
12648 dynamic
= htab
->dynamic_sections_created
;
12649 dynobj
= htab
->dynobj
;
12651 emit_relocs
= (bfd_link_relocatable (info
)
12652 || info
->emitrelocations
);
12654 memset (&flinfo
, 0, sizeof (flinfo
));
12655 flinfo
.info
= info
;
12656 flinfo
.output_bfd
= abfd
;
12657 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12658 if (flinfo
.symstrtab
== NULL
)
12663 flinfo
.hash_sec
= NULL
;
12664 flinfo
.symver_sec
= NULL
;
12668 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12669 /* Note that dynsym_sec can be NULL (on VMS). */
12670 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12671 /* Note that it is OK if symver_sec is NULL. */
12674 if (info
->unique_symbol
12675 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12676 local_hash_newfunc
,
12677 sizeof (struct local_hash_entry
)))
12680 /* The object attributes have been merged. Remove the input
12681 sections from the link, and set the contents of the output
12683 sections_removed
= false;
12684 const char *obj_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12685 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12687 bool remove_section
= false;
12689 if ((obj_attrs_section
&& strcmp (o
->name
, obj_attrs_section
) == 0)
12690 || strcmp (o
->name
, ".gnu.attributes") == 0)
12692 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12694 asection
*input_section
;
12696 if (p
->type
!= bfd_indirect_link_order
)
12698 input_section
= p
->u
.indirect
.section
;
12699 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12700 elf_link_input_bfd ignores this section. */
12701 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12704 /* Skip this section later on. */
12705 o
->map_head
.link_order
= NULL
;
12707 bfd_vma attr_size
= bfd_elf_obj_attr_size (abfd
);
12708 /* Once ELF headers have been written, the size of a section is
12709 frozen. We need to set the size of the attribute section before
12710 _bfd_elf_compute_section_file_positions. */
12711 bfd_set_section_size (o
, attr_size
);
12713 elf_obj_build_attributes (abfd
) = o
;
12715 remove_section
= true;
12717 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12719 /* Remove empty group section from linker output. */
12720 remove_section
= true;
12722 if (remove_section
)
12724 o
->flags
|= SEC_EXCLUDE
;
12725 bfd_section_list_remove (abfd
, o
);
12726 abfd
->section_count
--;
12727 sections_removed
= true;
12730 if (sections_removed
)
12731 _bfd_fix_excluded_sec_syms (abfd
, info
);
12733 /* Count up the number of relocations we will output for each output
12734 section, so that we know the sizes of the reloc sections. We
12735 also figure out some maximum sizes. */
12739 /* Mmap is used only if section size >= the minimum mmap section
12740 size. The initial max_contents_size value covers all sections
12741 smaller than the minimum mmap section size. It may be increased
12742 for compressed or linker created sections or sections whose
12743 rawsize != size. max_external_reloc_size covers all relocation
12744 sections smaller than the minimum mmap section size. */
12745 max_contents_size
= _bfd_minimum_mmap_size
;
12746 max_external_reloc_size
= _bfd_minimum_mmap_size
;
12751 max_contents_size
= 0;
12752 max_external_reloc_size
= 0;
12754 max_internal_reloc_count
= 0;
12756 max_sym_shndx_count
= 0;
12758 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12760 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12761 o
->reloc_count
= 0;
12763 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12765 unsigned int reloc_count
= 0;
12766 unsigned int additional_reloc_count
= 0;
12767 struct bfd_elf_section_data
*esdi
= NULL
;
12769 if (p
->type
== bfd_section_reloc_link_order
12770 || p
->type
== bfd_symbol_reloc_link_order
)
12772 else if (p
->type
== bfd_indirect_link_order
)
12776 sec
= p
->u
.indirect
.section
;
12778 /* Mark all sections which are to be included in the
12779 link. This will normally be every section. We need
12780 to do this so that we can identify any sections which
12781 the linker has decided to not include. */
12782 sec
->linker_mark
= true;
12784 if (sec
->flags
& SEC_MERGE
)
12788 /* Mmap is used only on non-compressed, non-linker created
12789 sections whose rawsize == size. */
12791 || sec
->compress_status
!= COMPRESS_SECTION_NONE
12792 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
12793 || sec
->rawsize
!= sec
->size
)
12796 if (sec
->rawsize
> max_contents_size
)
12797 max_contents_size
= sec
->rawsize
;
12798 if (sec
->size
> max_contents_size
)
12799 max_contents_size
= sec
->size
;
12802 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12803 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12807 /* We are interested in just local symbols, not all
12809 if (elf_bad_symtab (sec
->owner
))
12810 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12811 / bed
->s
->sizeof_sym
);
12813 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12815 if (sym_count
> max_sym_count
)
12816 max_sym_count
= sym_count
;
12818 if (sym_count
> max_sym_shndx_count
12819 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12820 max_sym_shndx_count
= sym_count
;
12822 esdi
= elf_section_data (sec
);
12824 if (esdi
->this_hdr
.sh_type
== SHT_REL
12825 || esdi
->this_hdr
.sh_type
== SHT_RELA
)
12826 /* Some backends use reloc_count in relocation sections
12827 to count particular types of relocs. Of course,
12828 reloc sections themselves can't have relocations. */
12830 else if (emit_relocs
)
12832 reloc_count
= sec
->reloc_count
;
12833 if (bed
->elf_backend_count_additional_relocs
)
12836 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12837 additional_reloc_count
+= c
;
12840 else if (bed
->elf_backend_count_relocs
)
12841 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12843 if ((sec
->flags
& SEC_RELOC
) != 0)
12846 if (!bed
->use_mmap
)
12849 size_t ext_size
= 0;
12851 if (esdi
->rel
.hdr
!= NULL
)
12852 ext_size
= esdi
->rel
.hdr
->sh_size
;
12853 if (esdi
->rela
.hdr
!= NULL
)
12854 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12856 if (ext_size
> max_external_reloc_size
)
12857 max_external_reloc_size
= ext_size
;
12859 if (sec
->reloc_count
> max_internal_reloc_count
)
12860 max_internal_reloc_count
= sec
->reloc_count
;
12865 if (reloc_count
== 0)
12868 reloc_count
+= additional_reloc_count
;
12869 o
->reloc_count
+= reloc_count
;
12871 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12875 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12876 esdo
->rel
.count
+= additional_reloc_count
;
12878 if (esdi
->rela
.hdr
)
12880 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12881 esdo
->rela
.count
+= additional_reloc_count
;
12887 esdo
->rela
.count
+= reloc_count
;
12889 esdo
->rel
.count
+= reloc_count
;
12893 if (o
->reloc_count
> 0)
12894 o
->flags
|= SEC_RELOC
;
12897 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12898 set it (this is probably a bug) and if it is set
12899 assign_section_numbers will create a reloc section. */
12900 o
->flags
&=~ SEC_RELOC
;
12903 /* If the SEC_ALLOC flag is not set, force the section VMA to
12904 zero. This is done in elf_fake_sections as well, but forcing
12905 the VMA to 0 here will ensure that relocs against these
12906 sections are handled correctly. */
12907 if ((o
->flags
& SEC_ALLOC
) == 0
12908 && ! o
->user_set_vma
)
12912 if (! bfd_link_relocatable (info
) && merged
)
12913 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12915 /* Figure out the file positions for everything but the symbol table
12916 and the relocs. We set symcount to force assign_section_numbers
12917 to create a symbol table. */
12918 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12919 BFD_ASSERT (! abfd
->output_has_begun
);
12920 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12923 /* Set sizes, and assign file positions for reloc sections. */
12924 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12926 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12927 if ((o
->flags
& SEC_RELOC
) != 0)
12930 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12934 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12938 /* _bfd_elf_compute_section_file_positions makes temporary use
12939 of target_index. Reset it. */
12940 o
->target_index
= 0;
12942 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12943 to count upwards while actually outputting the relocations. */
12944 esdo
->rel
.count
= 0;
12945 esdo
->rela
.count
= 0;
12947 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12948 && !bfd_section_is_ctf (o
))
12950 /* Cache the section contents so that they can be compressed
12951 later. Use bfd_malloc since it will be freed by
12952 bfd_compress_section_contents. */
12953 unsigned char *contents
= esdo
->this_hdr
.contents
;
12954 if (contents
!= NULL
)
12957 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12958 if (contents
== NULL
)
12960 esdo
->this_hdr
.contents
= contents
;
12964 /* We have now assigned file positions for all the sections except .symtab,
12965 .strtab, and non-loaded reloc and compressed debugging sections. We start
12966 the .symtab section at the current file position, and write directly to it.
12967 We build the .strtab section in memory. */
12968 abfd
->symcount
= 0;
12969 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12970 /* sh_name is set in prep_headers. */
12971 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12972 /* sh_flags, sh_addr and sh_size all start off zero. */
12973 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12974 /* sh_link is set in assign_section_numbers. */
12975 /* sh_info is set below. */
12976 /* sh_offset is set just below. */
12977 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12979 if (max_sym_count
< 20)
12980 max_sym_count
= 20;
12981 htab
->strtabsize
= max_sym_count
;
12982 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12983 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12984 if (htab
->strtab
== NULL
)
12986 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12988 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12989 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12991 if (info
->strip
!= strip_all
|| emit_relocs
)
12993 file_ptr off
= elf_next_file_pos (abfd
);
12995 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true, 0);
12997 /* Note that at this point elf_next_file_pos (abfd) is
12998 incorrect. We do not yet know the size of the .symtab section.
12999 We correct next_file_pos below, after we do know the size. */
13001 /* Start writing out the symbol table. The first symbol is always a
13003 elfsym
.st_value
= 0;
13004 elfsym
.st_size
= 0;
13005 elfsym
.st_info
= 0;
13006 elfsym
.st_other
= 0;
13007 elfsym
.st_shndx
= SHN_UNDEF
;
13008 elfsym
.st_target_internal
= 0;
13009 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
13010 bfd_und_section_ptr
, NULL
) != 1)
13013 /* Output a symbol for each section if asked or they are used for
13014 relocs. These symbols usually have no names. We store the
13015 index of each one in the index field of the section, so that
13016 we can find it again when outputting relocs. */
13018 if (bfd_keep_unused_section_symbols (abfd
) || emit_relocs
)
13020 bool name_local_sections
13021 = (bed
->elf_backend_name_local_section_symbols
13022 && bed
->elf_backend_name_local_section_symbols (abfd
));
13023 const char *name
= NULL
;
13025 elfsym
.st_size
= 0;
13026 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
13027 elfsym
.st_other
= 0;
13028 elfsym
.st_value
= 0;
13029 elfsym
.st_target_internal
= 0;
13030 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13032 o
= bfd_section_from_elf_index (abfd
, i
);
13035 o
->target_index
= bfd_get_symcount (abfd
);
13036 elfsym
.st_shndx
= i
;
13037 if (!bfd_link_relocatable (info
))
13038 elfsym
.st_value
= o
->vma
;
13039 if (name_local_sections
)
13041 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
13049 /* On some targets like Irix 5 the symbol split between local and global
13050 ones recorded in the sh_info field needs to be done between section
13051 and all other symbols. */
13052 if (bed
->elf_backend_elfsym_local_is_section
13053 && bed
->elf_backend_elfsym_local_is_section (abfd
))
13054 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
13056 /* Allocate some memory to hold information read in from the input
13058 if (max_contents_size
!= 0)
13060 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
13061 if (flinfo
.contents
== NULL
)
13065 if (max_external_reloc_size
!= 0)
13067 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
13068 if (flinfo
.external_relocs
== NULL
)
13072 if (max_internal_reloc_count
!= 0)
13074 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
13075 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
13076 if (flinfo
.internal_relocs
== NULL
)
13080 if (max_sym_count
!= 0)
13082 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
13083 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
13084 if (flinfo
.external_syms
== NULL
)
13087 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
13088 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
13089 if (flinfo
.internal_syms
== NULL
)
13092 amt
= max_sym_count
* sizeof (long);
13093 flinfo
.indices
= (long int *) bfd_malloc (amt
);
13094 if (flinfo
.indices
== NULL
)
13097 amt
= max_sym_count
* sizeof (asection
*);
13098 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
13099 if (flinfo
.sections
== NULL
)
13103 if (max_sym_shndx_count
!= 0)
13105 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
13106 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
13107 if (flinfo
.locsym_shndx
== NULL
)
13113 bfd_vma base
, end
= 0; /* Both bytes. */
13116 for (sec
= htab
->tls_sec
;
13117 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
13120 bfd_size_type size
= sec
->size
;
13121 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
13124 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
13126 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
13129 size
= ord
->offset
* opb
+ ord
->size
;
13131 end
= sec
->vma
+ size
/ opb
;
13133 base
= htab
->tls_sec
->vma
;
13134 /* Only align end of TLS section if static TLS doesn't have special
13135 alignment requirements. */
13136 if (bed
->static_tls_alignment
== 1)
13137 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
13138 htab
->tls_size
= end
- base
;
13141 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
13144 /* Finish relative relocations here after regular symbol processing
13145 is finished if DT_RELR is enabled. */
13146 if (info
->enable_dt_relr
13147 && bed
->finish_relative_relocs
13148 && !bed
->finish_relative_relocs (info
))
13149 info
->callbacks
->fatal
13150 (_("%P: %pB: failed to finish relative relocations\n"), abfd
);
13152 /* Since ELF permits relocations to be against local symbols, we
13153 must have the local symbols available when we do the relocations.
13154 Since we would rather only read the local symbols once, and we
13155 would rather not keep them in memory, we handle all the
13156 relocations for a single input file at the same time.
13158 Unfortunately, there is no way to know the total number of local
13159 symbols until we have seen all of them, and the local symbol
13160 indices precede the global symbol indices. This means that when
13161 we are generating relocatable output, and we see a reloc against
13162 a global symbol, we can not know the symbol index until we have
13163 finished examining all the local symbols to see which ones we are
13164 going to output. To deal with this, we keep the relocations in
13165 memory, and don't output them until the end of the link. This is
13166 an unfortunate waste of memory, but I don't see a good way around
13167 it. Fortunately, it only happens when performing a relocatable
13168 link, which is not the common case. FIXME: If keep_memory is set
13169 we could write the relocs out and then read them again; I don't
13170 know how bad the memory loss will be. */
13172 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13173 sub
->output_has_begun
= false;
13174 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
13176 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
13178 if (p
->type
== bfd_indirect_link_order
13179 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
13180 == bfd_target_elf_flavour
)
13181 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
13183 if (! sub
->output_has_begun
)
13185 if (! elf_link_input_bfd (&flinfo
, sub
))
13187 sub
->output_has_begun
= true;
13190 else if (p
->type
== bfd_section_reloc_link_order
13191 || p
->type
== bfd_symbol_reloc_link_order
)
13193 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
13198 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
13200 if (p
->type
== bfd_indirect_link_order
13201 && (bfd_get_flavour (sub
)
13202 == bfd_target_elf_flavour
)
13203 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
13204 != bed
->s
->elfclass
))
13206 const char *iclass
, *oclass
;
13208 switch (bed
->s
->elfclass
)
13210 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
13211 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
13212 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
13216 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
13218 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
13219 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
13220 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
13224 bfd_set_error (bfd_error_wrong_format
);
13226 /* xgettext:c-format */
13227 (_("%pB: file class %s incompatible with %s"),
13228 sub
, iclass
, oclass
);
13237 /* Free symbol buffer if needed. */
13238 if (!info
->reduce_memory_overheads
)
13240 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13241 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
13243 free (elf_tdata (sub
)->symbuf
);
13244 elf_tdata (sub
)->symbuf
= NULL
;
13248 /* Output any global symbols that got converted to local in a
13249 version script or due to symbol visibility. We do this in a
13250 separate step since ELF requires all local symbols to appear
13251 prior to any global symbols. FIXME: We should only do this if
13252 some global symbols were, in fact, converted to become local.
13253 FIXME: Will this work correctly with the Irix 5 linker? */
13254 eoinfo
.failed
= false;
13255 eoinfo
.flinfo
= &flinfo
;
13256 eoinfo
.localsyms
= true;
13257 eoinfo
.file_sym_done
= false;
13258 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
13262 /* If backend needs to output some local symbols not present in the hash
13263 table, do it now. */
13264 if (bed
->elf_backend_output_arch_local_syms
)
13266 if (! ((*bed
->elf_backend_output_arch_local_syms
)
13267 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
13271 /* That wrote out all the local symbols. Finish up the symbol table
13272 with the global symbols. Even if we want to strip everything we
13273 can, we still need to deal with those global symbols that got
13274 converted to local in a version script. */
13276 /* The sh_info field records the index of the first non local symbol. */
13277 if (!symtab_hdr
->sh_info
)
13278 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
13281 && htab
->dynsym
!= NULL
13282 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
13284 Elf_Internal_Sym sym
;
13285 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
13287 o
= htab
->dynsym
->output_section
;
13288 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
13290 /* Write out the section symbols for the output sections. */
13291 if (bfd_link_pic (info
)
13292 || htab
->is_relocatable_executable
)
13298 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
13300 sym
.st_target_internal
= 0;
13302 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
13308 dynindx
= elf_section_data (s
)->dynindx
;
13311 indx
= elf_section_data (s
)->this_idx
;
13312 BFD_ASSERT (indx
> 0);
13313 sym
.st_shndx
= indx
;
13314 if (! check_dynsym (abfd
, &sym
))
13316 sym
.st_value
= s
->vma
;
13317 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
13319 /* Inform the linker of the addition of this symbol. */
13321 if (info
->callbacks
->ctf_new_dynsym
)
13322 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
13324 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
13328 /* Write out the local dynsyms. */
13329 if (htab
->dynlocal
)
13331 struct elf_link_local_dynamic_entry
*e
;
13332 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
13337 /* Copy the internal symbol and turn off visibility.
13338 Note that we saved a word of storage and overwrote
13339 the original st_name with the dynstr_index. */
13341 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
13342 sym
.st_shndx
= SHN_UNDEF
;
13344 s
= bfd_section_from_elf_index (e
->input_bfd
,
13347 && s
->output_section
!= NULL
13348 && elf_section_data (s
->output_section
) != NULL
)
13351 elf_section_data (s
->output_section
)->this_idx
;
13352 if (! check_dynsym (abfd
, &sym
))
13354 sym
.st_value
= (s
->output_section
->vma
13356 + e
->isym
.st_value
);
13359 /* Inform the linker of the addition of this symbol. */
13361 if (info
->callbacks
->ctf_new_dynsym
)
13362 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
13364 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
13365 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
13370 /* We get the global symbols from the hash table. */
13371 eoinfo
.failed
= false;
13372 eoinfo
.localsyms
= false;
13373 eoinfo
.flinfo
= &flinfo
;
13374 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
13378 /* If backend needs to output some symbols not present in the hash
13379 table, do it now. */
13380 if (bed
->elf_backend_output_arch_syms
13381 && (info
->strip
!= strip_all
|| emit_relocs
))
13383 if (! ((*bed
->elf_backend_output_arch_syms
)
13384 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
13388 /* Finalize the .strtab section. */
13389 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
13391 /* Swap out the .strtab section. */
13392 if (!elf_link_swap_symbols_out (&flinfo
))
13394 free (htab
->strtab
);
13395 htab
->strtab
= NULL
;
13397 /* Now we know the size of the symtab section. */
13398 if (bfd_get_symcount (abfd
) > 0)
13400 /* Finish up and write out the symbol string table (.strtab)
13402 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
13403 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
13405 if (elf_symtab_shndx_list (abfd
))
13407 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
13409 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
13411 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
13412 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
13413 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
13414 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
13415 symtab_shndx_hdr
->sh_size
= amt
;
13417 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
13420 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
13421 || (bfd_write (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
13426 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
13427 /* sh_name was set in prep_headers. */
13428 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
13429 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
13430 symstrtab_hdr
->sh_addr
= 0;
13431 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
13432 symstrtab_hdr
->sh_entsize
= 0;
13433 symstrtab_hdr
->sh_link
= 0;
13434 symstrtab_hdr
->sh_info
= 0;
13435 /* sh_offset is set just below. */
13436 symstrtab_hdr
->sh_addralign
= 1;
13438 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
13440 elf_next_file_pos (abfd
) = off
;
13442 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
13443 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
13447 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
13449 _bfd_error_handler (_("%pB: failed to generate import library"),
13450 info
->out_implib_bfd
);
13454 /* Adjust the relocs to have the correct symbol indices. */
13455 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
13457 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
13460 if ((o
->flags
& SEC_RELOC
) == 0)
13463 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
13464 if (esdo
->rel
.hdr
!= NULL
13465 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
13467 if (esdo
->rela
.hdr
!= NULL
13468 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
13471 /* Set the reloc_count field to 0 to prevent write_relocs from
13472 trying to swap the relocs out itself. */
13473 o
->reloc_count
= 0;
13477 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
13478 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
13481 if (htab
->srelrdyn
!= NULL
13482 && htab
->srelrdyn
->output_section
!= NULL
13483 && htab
->srelrdyn
->size
!= 0)
13485 asection
*s
= htab
->srelrdyn
->output_section
;
13486 relr_entsize
= elf_section_data (s
)->this_hdr
.sh_entsize
;
13487 if (relr_entsize
== 0)
13489 relr_entsize
= bed
->s
->arch_size
/ 8;
13490 elf_section_data (s
)->this_hdr
.sh_entsize
= relr_entsize
;
13494 /* If we are linking against a dynamic object, or generating a
13495 shared library, finish up the dynamic linking information. */
13498 bfd_byte
*dyncon
, *dynconend
;
13500 /* Fix up .dynamic entries. */
13502 BFD_ASSERT (o
!= NULL
);
13504 dyncon
= o
->contents
;
13505 dynconend
= PTR_ADD (o
->contents
, o
->size
);
13506 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13508 Elf_Internal_Dyn dyn
;
13511 bfd_size_type sh_size
;
13514 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13521 if (relativecount
!= 0)
13523 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
13525 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
13526 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
13528 if (dyn
.d_tag
!= DT_NULL
13529 && dynconend
- dyncon
>= bed
->s
->sizeof_dyn
)
13531 dyn
.d_un
.d_val
= relativecount
;
13537 if (relr_entsize
!= 0)
13539 if (dynconend
- dyncon
>= 3 * bed
->s
->sizeof_dyn
)
13541 asection
*s
= htab
->srelrdyn
;
13542 dyn
.d_tag
= DT_RELR
;
13544 = s
->output_section
->vma
+ s
->output_offset
;
13545 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13546 dyncon
+= bed
->s
->sizeof_dyn
;
13548 dyn
.d_tag
= DT_RELRSZ
;
13549 dyn
.d_un
.d_val
= s
->size
;
13550 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13551 dyncon
+= bed
->s
->sizeof_dyn
;
13553 dyn
.d_tag
= DT_RELRENT
;
13554 dyn
.d_un
.d_val
= relr_entsize
;
13563 name
= info
->init_function
;
13566 name
= info
->fini_function
;
13569 struct elf_link_hash_entry
*h
;
13571 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
13573 && (h
->root
.type
== bfd_link_hash_defined
13574 || h
->root
.type
== bfd_link_hash_defweak
))
13576 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
13577 o
= h
->root
.u
.def
.section
;
13578 if (o
->output_section
!= NULL
)
13579 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
13580 + o
->output_offset
);
13583 /* The symbol is imported from another shared
13584 library and does not apply to this one. */
13585 dyn
.d_un
.d_ptr
= 0;
13592 case DT_PREINIT_ARRAYSZ
:
13593 name
= ".preinit_array";
13595 case DT_INIT_ARRAYSZ
:
13596 name
= ".init_array";
13598 case DT_FINI_ARRAYSZ
:
13599 name
= ".fini_array";
13601 o
= bfd_get_section_by_name (abfd
, name
);
13605 (_("could not find section %s"), name
);
13610 (_("warning: %s section has zero size"), name
);
13611 dyn
.d_un
.d_val
= o
->size
;
13614 case DT_PREINIT_ARRAY
:
13615 name
= ".preinit_array";
13617 case DT_INIT_ARRAY
:
13618 name
= ".init_array";
13620 case DT_FINI_ARRAY
:
13621 name
= ".fini_array";
13623 o
= bfd_get_section_by_name (abfd
, name
);
13630 name
= ".gnu.hash";
13639 name
= ".gnu.version_d";
13642 name
= ".gnu.version_r";
13645 name
= ".gnu.version";
13647 o
= bfd_get_linker_section (dynobj
, name
);
13649 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13652 (_("could not find section %s"), name
);
13655 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13658 (_("warning: section '%s' is being made into a note"), name
);
13659 bfd_set_error (bfd_error_nonrepresentable_section
);
13662 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13669 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13675 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13677 Elf_Internal_Shdr
*hdr
;
13679 hdr
= elf_elfsections (abfd
)[i
];
13680 if (hdr
->sh_type
== type
13681 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13683 sh_size
+= hdr
->sh_size
;
13685 || sh_addr
> hdr
->sh_addr
)
13686 sh_addr
= hdr
->sh_addr
;
13690 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13692 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13694 /* Don't count procedure linkage table relocs in the
13695 overall reloc count. */
13696 sh_size
-= htab
->srelplt
->size
;
13698 /* If the size is zero, make the address zero too.
13699 This is to avoid a glibc bug. If the backend
13700 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13701 zero, then we'll put DT_RELA at the end of
13702 DT_JMPREL. glibc will interpret the end of
13703 DT_RELA matching the end of DT_JMPREL as the
13704 case where DT_RELA includes DT_JMPREL, and for
13705 LD_BIND_NOW will decide that processing DT_RELA
13706 will process the PLT relocs too. Net result:
13707 No PLT relocs applied. */
13710 /* If .rela.plt is the first .rela section, exclude
13711 it from DT_RELA. */
13712 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13713 + htab
->srelplt
->output_offset
) * opb
)
13714 sh_addr
+= htab
->srelplt
->size
;
13717 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13718 dyn
.d_un
.d_val
= sh_size
;
13720 dyn
.d_un
.d_ptr
= sh_addr
;
13723 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13727 /* If we have created any dynamic sections, then output them. */
13728 if (dynobj
!= NULL
)
13730 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13733 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13734 if (bfd_link_textrel_check (info
)
13735 && (o
= htab
->dynamic
) != NULL
13738 bfd_byte
*dyncon
, *dynconend
;
13740 dyncon
= o
->contents
;
13741 dynconend
= o
->contents
+ o
->size
;
13742 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13744 Elf_Internal_Dyn dyn
;
13746 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13748 if (dyn
.d_tag
== DT_TEXTREL
)
13750 if (info
->textrel_check
== textrel_check_error
)
13751 info
->callbacks
->einfo
13752 (_("%P%X: read-only segment has dynamic relocations\n"));
13753 else if (bfd_link_dll (info
))
13754 info
->callbacks
->einfo
13755 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13756 else if (bfd_link_pde (info
))
13757 info
->callbacks
->einfo
13758 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13760 info
->callbacks
->einfo
13761 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13767 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13769 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13771 || o
->output_section
== bfd_abs_section_ptr
)
13773 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13775 /* At this point, we are only interested in sections
13776 created by _bfd_elf_link_create_dynamic_sections. */
13779 if (htab
->stab_info
.stabstr
== o
)
13781 if (htab
->eh_info
.hdr_sec
== o
)
13783 if (strcmp (o
->name
, ".dynstr") != 0)
13785 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13786 * bfd_octets_per_byte (abfd
, o
));
13787 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13788 o
->contents
, octets
, o
->size
))
13793 /* The contents of the .dynstr section are actually in a
13797 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13798 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13799 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13805 if (!info
->resolve_section_groups
)
13807 bool failed
= false;
13809 BFD_ASSERT (bfd_link_relocatable (info
));
13810 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13815 /* If we have optimized stabs strings, output them. */
13816 if (htab
->stab_info
.stabstr
!= NULL
)
13818 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13822 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13825 if (! _bfd_elf_write_section_sframe (abfd
, info
))
13828 if (! _bfd_elf_write_section_build_attributes (abfd
, info
))
13831 if (info
->callbacks
->emit_ctf
)
13832 info
->callbacks
->emit_ctf ();
13834 elf_final_link_free (abfd
, &flinfo
);
13836 if (info
->unique_symbol
)
13837 bfd_hash_table_free (&flinfo
.local_hash_table
);
13841 free (htab
->strtab
);
13842 htab
->strtab
= NULL
;
13843 elf_final_link_free (abfd
, &flinfo
);
13845 if (info
->unique_symbol
)
13846 bfd_hash_table_free (&flinfo
.local_hash_table
);
13850 /* Initialize COOKIE for input bfd ABFD. */
13853 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13854 struct bfd_link_info
*info
, bfd
*abfd
,
13857 Elf_Internal_Shdr
*symtab_hdr
;
13858 const struct elf_backend_data
*bed
;
13860 bed
= get_elf_backend_data (abfd
);
13861 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13863 cookie
->abfd
= abfd
;
13864 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13865 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13866 if (cookie
->bad_symtab
)
13868 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13869 cookie
->extsymoff
= 0;
13873 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13874 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13877 if (bed
->s
->arch_size
== 32)
13878 cookie
->r_sym_shift
= 8;
13880 cookie
->r_sym_shift
= 32;
13882 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13883 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13885 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13886 cookie
->locsymcount
, 0,
13888 if (cookie
->locsyms
== NULL
)
13890 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13893 if (keep_memory
|| _bfd_elf_link_keep_memory (info
))
13895 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13896 info
->cache_size
+= (cookie
->locsymcount
13897 * sizeof (Elf_Internal_Sym
));
13903 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13906 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13908 Elf_Internal_Shdr
*symtab_hdr
;
13910 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13911 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13912 free (cookie
->locsyms
);
13915 /* Initialize the relocation information in COOKIE for input section SEC
13916 of input bfd ABFD. */
13919 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13920 struct bfd_link_info
*info
, bfd
*abfd
,
13921 asection
*sec
, bool keep_memory
)
13923 if (sec
->reloc_count
== 0)
13925 cookie
->rels
= NULL
;
13926 cookie
->relend
= NULL
;
13930 cookie
->rels
= _bfd_elf_link_info_read_relocs
13931 (abfd
, info
, sec
, NULL
, NULL
,
13932 keep_memory
|| _bfd_elf_link_keep_memory (info
));
13933 if (cookie
->rels
== NULL
)
13935 cookie
->rel
= cookie
->rels
;
13936 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13938 cookie
->rel
= cookie
->rels
;
13942 /* Free the memory allocated by init_reloc_cookie_rels,
13946 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13949 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13950 free (cookie
->rels
);
13953 /* Initialize the whole of COOKIE for input section SEC. */
13956 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13957 struct bfd_link_info
*info
,
13958 asection
*sec
, bool keep_memory
)
13960 if (!init_reloc_cookie (cookie
, info
, sec
->owner
, keep_memory
))
13962 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
,
13968 fini_reloc_cookie (cookie
, sec
->owner
);
13973 /* Free the memory allocated by init_reloc_cookie_for_section,
13977 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13980 fini_reloc_cookie_rels (cookie
, sec
);
13981 fini_reloc_cookie (cookie
, sec
->owner
);
13984 /* Garbage collect unused sections. */
13986 /* Default gc_mark_hook. */
13989 _bfd_elf_gc_mark_hook (asection
*sec
,
13990 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13991 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13992 struct elf_link_hash_entry
*h
,
13993 Elf_Internal_Sym
*sym
)
13996 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13998 switch (h
->root
.type
)
14000 case bfd_link_hash_defined
:
14001 case bfd_link_hash_defweak
:
14002 return h
->root
.u
.def
.section
;
14004 case bfd_link_hash_common
:
14005 return h
->root
.u
.c
.p
->section
;
14012 /* Return the debug definition section. */
14015 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
14016 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
14017 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
14018 struct elf_link_hash_entry
*h
,
14019 Elf_Internal_Sym
*sym
)
14023 /* Return the global debug definition section. */
14024 if ((h
->root
.type
== bfd_link_hash_defined
14025 || h
->root
.type
== bfd_link_hash_defweak
)
14026 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
14027 return h
->root
.u
.def
.section
;
14031 /* Return the local debug definition section. */
14032 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
14034 if (isec
!= NULL
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
14041 /* COOKIE->rel describes a relocation against section SEC, which is
14042 a section we've decided to keep. Return the section that contains
14043 the relocation symbol, or NULL if no section contains it. */
14046 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
14047 elf_gc_mark_hook_fn gc_mark_hook
,
14048 struct elf_reloc_cookie
*cookie
,
14051 unsigned long r_symndx
;
14052 struct elf_link_hash_entry
*h
, *hw
;
14054 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
14055 if (r_symndx
== STN_UNDEF
)
14058 h
= get_ext_sym_hash_from_cookie (cookie
, r_symndx
);
14061 /* A corrupt input file can lead to a situation where the index
14062 does not reference either a local or an external symbol. */
14063 if (r_symndx
>= cookie
->locsymcount
)
14066 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
14067 &cookie
->locsyms
[r_symndx
]);
14070 bool was_marked
= h
->mark
;
14073 /* Keep all aliases of the symbol too. If an object symbol
14074 needs to be copied into .dynbss then all of its aliases
14075 should be present as dynamic symbols, not just the one used
14076 on the copy relocation. */
14078 while (hw
->is_weakalias
)
14084 if (!was_marked
&& h
->start_stop
&& !h
->root
.ldscript_def
)
14086 if (info
->start_stop_gc
)
14089 /* To work around a glibc bug, mark XXX input sections
14090 when there is a reference to __start_XXX or __stop_XXX
14092 else if (start_stop
!= NULL
)
14094 asection
*s
= h
->u2
.start_stop_section
;
14095 *start_stop
= true;
14100 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
14103 /* COOKIE->rel describes a relocation against section SEC, which is
14104 a section we've decided to keep. Mark the section that contains
14105 the relocation symbol. */
14108 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
14110 elf_gc_mark_hook_fn gc_mark_hook
,
14111 struct elf_reloc_cookie
*cookie
)
14114 bool start_stop
= false;
14116 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
14117 while (rsec
!= NULL
)
14119 if (!rsec
->gc_mark
)
14121 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
14122 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
14124 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
14129 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
14134 /* The mark phase of garbage collection. For a given section, mark
14135 it and any sections in this section's group, and all the sections
14136 which define symbols to which it refers. */
14139 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
14141 elf_gc_mark_hook_fn gc_mark_hook
)
14144 asection
*group_sec
, *eh_frame
;
14148 /* Mark all the sections in the group. */
14149 group_sec
= elf_section_data (sec
)->next_in_group
;
14150 if (group_sec
&& !group_sec
->gc_mark
)
14151 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
14154 /* Look through the section relocs. */
14156 eh_frame
= elf_eh_frame_section (sec
->owner
);
14157 if ((sec
->flags
& SEC_RELOC
) != 0
14158 && sec
->reloc_count
> 0
14159 && sec
!= eh_frame
)
14161 struct elf_reloc_cookie cookie
;
14163 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
, false))
14167 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
14168 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
14173 fini_reloc_cookie_for_section (&cookie
, sec
);
14177 if (ret
&& eh_frame
&& elf_fde_list (sec
))
14179 struct elf_reloc_cookie cookie
;
14181 /* NB: When --no-keep-memory is used, the symbol table and
14182 relocation info for eh_frame are freed after they are retrieved
14183 for each text section in the input object. If an input object
14184 has many text sections, the same data is retrieved and freed
14185 many times which can take a very long time. Always keep the
14186 symbol table and relocation info for eh_frame to avoid it. */
14187 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
,
14192 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
14193 gc_mark_hook
, &cookie
))
14195 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
14199 eh_frame
= elf_section_eh_frame_entry (sec
);
14200 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
14201 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
14207 /* Scan and mark sections in a special or debug section group. */
14210 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
14212 /* Point to first section of section group. */
14214 /* Used to iterate the section group. */
14217 bool is_special_grp
= true;
14218 bool is_debug_grp
= true;
14220 /* First scan to see if group contains any section other than debug
14221 and special section. */
14222 ssec
= msec
= elf_next_in_group (grp
);
14225 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
14226 is_debug_grp
= false;
14228 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
14229 is_special_grp
= false;
14231 msec
= elf_next_in_group (msec
);
14233 while (msec
!= ssec
);
14235 /* If this is a pure debug section group or pure special section group,
14236 keep all sections in this group. */
14237 if (is_debug_grp
|| is_special_grp
)
14242 msec
= elf_next_in_group (msec
);
14244 while (msec
!= ssec
);
14248 /* Keep debug and special sections. */
14251 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
14252 elf_gc_mark_hook_fn mark_hook
)
14256 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14260 bool debug_frag_seen
;
14261 bool has_kept_debug_info
;
14263 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14265 isec
= ibfd
->sections
;
14266 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14269 /* Ensure all linker created sections are kept,
14270 see if any other section is already marked,
14271 and note if we have any fragmented debug sections. */
14272 debug_frag_seen
= some_kept
= has_kept_debug_info
= false;
14273 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14275 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
14277 else if (isec
->gc_mark
14278 && (isec
->flags
& SEC_ALLOC
) != 0
14279 && elf_section_type (isec
) != SHT_NOTE
)
14283 /* Since all sections, except for backend specific ones,
14284 have been garbage collected, call mark_hook on this
14285 section if any of its linked-to sections is marked. */
14286 asection
*linked_to_sec
;
14287 for (linked_to_sec
= elf_linked_to_section (isec
);
14288 linked_to_sec
!= NULL
&& !linked_to_sec
->linker_mark
;
14289 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
14291 if (linked_to_sec
->gc_mark
)
14293 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
14297 linked_to_sec
->linker_mark
= 1;
14299 for (linked_to_sec
= elf_linked_to_section (isec
);
14300 linked_to_sec
!= NULL
&& linked_to_sec
->linker_mark
;
14301 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
14302 linked_to_sec
->linker_mark
= 0;
14305 if (!debug_frag_seen
14306 && (isec
->flags
& SEC_DEBUGGING
)
14307 && startswith (isec
->name
, ".debug_line."))
14308 debug_frag_seen
= true;
14309 else if (strcmp (bfd_section_name (isec
),
14310 "__patchable_function_entries") == 0
14311 && elf_linked_to_section (isec
) == NULL
)
14312 info
->callbacks
->fatal (_("%P: %pB(%pA): error: "
14313 "need linked-to section "
14314 "for --gc-sections\n"),
14315 isec
->owner
, isec
);
14318 /* If no non-note alloc section in this file will be kept, then
14319 we can toss out the debug and special sections. */
14323 /* Keep debug and special sections like .comment when they are
14324 not part of a group. Also keep section groups that contain
14325 just debug sections or special sections. NB: Sections with
14326 linked-to section has been handled above. */
14327 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14329 if ((isec
->flags
& SEC_GROUP
) != 0)
14330 _bfd_elf_gc_mark_debug_special_section_group (isec
);
14331 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
14332 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
14333 && elf_next_in_group (isec
) == NULL
14334 && elf_linked_to_section (isec
) == NULL
)
14336 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
14337 has_kept_debug_info
= true;
14340 /* Look for CODE sections which are going to be discarded,
14341 and find and discard any fragmented debug sections which
14342 are associated with that code section. */
14343 if (debug_frag_seen
)
14344 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14345 if ((isec
->flags
& SEC_CODE
) != 0
14346 && isec
->gc_mark
== 0)
14351 ilen
= strlen (isec
->name
);
14353 /* Association is determined by the name of the debug
14354 section containing the name of the code section as
14355 a suffix. For example .debug_line.text.foo is a
14356 debug section associated with .text.foo. */
14357 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
14361 if (dsec
->gc_mark
== 0
14362 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
14365 dlen
= strlen (dsec
->name
);
14368 && strncmp (dsec
->name
+ (dlen
- ilen
),
14369 isec
->name
, ilen
) == 0)
14374 /* Mark debug sections referenced by kept debug sections. */
14375 if (has_kept_debug_info
)
14376 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14378 && (isec
->flags
& SEC_DEBUGGING
) != 0)
14379 if (!_bfd_elf_gc_mark (info
, isec
,
14380 elf_gc_mark_debug_section
))
14387 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
14390 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14392 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14396 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14397 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
14398 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14401 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14404 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14406 /* When any section in a section group is kept, we keep all
14407 sections in the section group. If the first member of
14408 the section group is excluded, we will also exclude the
14410 if (o
->flags
& SEC_GROUP
)
14412 asection
*first
= elf_next_in_group (o
);
14414 o
->gc_mark
= first
->gc_mark
;
14420 /* Skip sweeping sections already excluded. */
14421 if (o
->flags
& SEC_EXCLUDE
)
14424 /* Since this is early in the link process, it is simple
14425 to remove a section from the output. */
14426 o
->flags
|= SEC_EXCLUDE
;
14428 if (info
->print_gc_sections
&& o
->size
!= 0)
14429 /* xgettext:c-format */
14430 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14438 /* Propagate collected vtable information. This is called through
14439 elf_link_hash_traverse. */
14442 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
14444 /* Those that are not vtables. */
14446 || h
->u2
.vtable
== NULL
14447 || h
->u2
.vtable
->parent
== NULL
)
14450 /* Those vtables that do not have parents, we cannot merge. */
14451 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
14454 /* If we've already been done, exit. */
14455 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
14458 /* Make sure the parent's table is up to date. */
14459 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
14461 if (h
->u2
.vtable
->used
== NULL
)
14463 /* None of this table's entries were referenced. Re-use the
14465 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14466 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
14473 /* Or the parent's entries into ours. */
14474 cu
= h
->u2
.vtable
->used
;
14476 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14479 const struct elf_backend_data
*bed
;
14480 unsigned int log_file_align
;
14482 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
14483 log_file_align
= bed
->s
->log_file_align
;
14484 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
14498 struct link_info_ok
14500 struct bfd_link_info
*info
;
14505 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
,
14509 bfd_vma hstart
, hend
;
14510 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
14511 const struct elf_backend_data
*bed
;
14512 unsigned int log_file_align
;
14513 struct link_info_ok
*info
= (struct link_info_ok
*) ptr
;
14515 /* Take care of both those symbols that do not describe vtables as
14516 well as those that are not loaded. */
14518 || h
->u2
.vtable
== NULL
14519 || h
->u2
.vtable
->parent
== NULL
)
14522 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
14523 || h
->root
.type
== bfd_link_hash_defweak
);
14525 sec
= h
->root
.u
.def
.section
;
14526 hstart
= h
->root
.u
.def
.value
;
14527 hend
= hstart
+ h
->size
;
14529 relstart
= _bfd_elf_link_info_read_relocs (sec
->owner
, info
->info
,
14530 sec
, NULL
, NULL
, true);
14532 return info
->ok
= false;
14533 bed
= get_elf_backend_data (sec
->owner
);
14534 log_file_align
= bed
->s
->log_file_align
;
14536 relend
= relstart
+ sec
->reloc_count
;
14538 for (rel
= relstart
; rel
< relend
; ++rel
)
14539 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
14541 /* If the entry is in use, do nothing. */
14542 if (h
->u2
.vtable
->used
14543 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
14545 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
14546 if (h
->u2
.vtable
->used
[entry
])
14549 /* Otherwise, kill it. */
14550 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
14556 /* Mark sections containing dynamically referenced symbols. When
14557 building shared libraries, we must assume that any visible symbol is
14561 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
14563 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
14564 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
14566 if ((h
->root
.type
== bfd_link_hash_defined
14567 || h
->root
.type
== bfd_link_hash_defweak
)
14569 || h
->root
.ldscript_def
14570 || !info
->start_stop_gc
)
14571 && ((h
->ref_dynamic
&& !h
->forced_local
)
14572 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
14573 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
14574 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
14575 && (!bfd_link_executable (info
)
14576 || info
->gc_keep_exported
14577 || info
->export_dynamic
14580 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
14581 && (h
->versioned
>= versioned
14582 || !bfd_hide_sym_by_version (info
->version_info
,
14583 h
->root
.root
.string
)))))
14584 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14589 /* Keep all sections containing symbols undefined on the command-line,
14590 and the section containing the entry symbol. */
14593 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
14595 struct bfd_sym_chain
*sym
;
14597 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
14599 struct elf_link_hash_entry
*h
;
14601 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
14602 false, false, false);
14605 && (h
->root
.type
== bfd_link_hash_defined
14606 || h
->root
.type
== bfd_link_hash_defweak
)
14607 && !bfd_is_const_section (h
->root
.u
.def
.section
))
14608 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14613 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
14614 struct bfd_link_info
*info
)
14616 bfd
*ibfd
= info
->input_bfds
;
14618 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14621 struct elf_reloc_cookie cookie
;
14623 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14625 sec
= ibfd
->sections
;
14626 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14629 if (!init_reloc_cookie (&cookie
, info
, ibfd
, false))
14632 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
14634 if (startswith (bfd_section_name (sec
), ".eh_frame_entry")
14635 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
,
14638 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
14639 fini_reloc_cookie_rels (&cookie
, sec
);
14646 /* Do mark and sweep of unused sections. */
14649 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14653 elf_gc_mark_hook_fn gc_mark_hook
;
14654 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14655 struct elf_link_hash_table
*htab
;
14656 struct link_info_ok info_ok
;
14658 if (!bed
->can_gc_sections
14659 || !is_elf_hash_table (info
->hash
))
14661 _bfd_error_handler(_("warning: gc-sections option ignored"));
14665 bed
->gc_keep (info
);
14666 htab
= elf_hash_table (info
);
14668 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14669 at the .eh_frame section if we can mark the FDEs individually. */
14670 for (sub
= info
->input_bfds
;
14671 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14672 sub
= sub
->link
.next
)
14675 struct elf_reloc_cookie cookie
;
14677 sec
= sub
->sections
;
14678 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14680 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14681 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
,
14684 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14685 if (elf_section_data (sec
)->sec_info
14686 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14687 elf_eh_frame_section (sub
) = sec
;
14688 fini_reloc_cookie_for_section (&cookie
, sec
);
14689 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14693 /* Apply transitive closure to the vtable entry usage info. */
14694 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14698 /* Kill the vtable relocations that were not used. */
14699 info_ok
.info
= info
;
14701 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &info_ok
);
14705 /* Mark dynamically referenced symbols. */
14706 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14707 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14709 /* Grovel through relocs to find out who stays ... */
14710 gc_mark_hook
= bed
->gc_mark_hook
;
14711 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14715 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14716 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14717 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14721 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14724 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14725 Also treat note sections as a root, if the section is not part
14726 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14727 well as FINI_ARRAY sections for ld -r. */
14728 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14730 && (o
->flags
& SEC_EXCLUDE
) == 0
14731 && ((o
->flags
& SEC_KEEP
) != 0
14732 || (bfd_link_relocatable (info
)
14733 && ((elf_section_data (o
)->this_hdr
.sh_type
14734 == SHT_PREINIT_ARRAY
)
14735 || (elf_section_data (o
)->this_hdr
.sh_type
14737 || (elf_section_data (o
)->this_hdr
.sh_type
14738 == SHT_FINI_ARRAY
)))
14739 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14740 && elf_next_in_group (o
) == NULL
14741 && elf_linked_to_section (o
) == NULL
)
14742 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14743 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14745 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14750 /* Allow the backend to mark additional target specific sections. */
14751 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14753 /* ... and mark SEC_EXCLUDE for those that go. */
14754 return elf_gc_sweep (abfd
, info
);
14757 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14760 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14762 struct elf_link_hash_entry
*h
,
14765 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14766 struct elf_link_hash_entry
**search
, *child
;
14767 size_t extsymcount
;
14768 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14770 /* The sh_info field of the symtab header tells us where the
14771 external symbols start. We don't care about the local symbols at
14773 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14774 if (!elf_bad_symtab (abfd
))
14775 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14777 sym_hashes
= elf_sym_hashes (abfd
);
14778 sym_hashes_end
= PTR_ADD (sym_hashes
, extsymcount
);
14780 /* Hunt down the child symbol, which is in this section at the same
14781 offset as the relocation. */
14782 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14784 if ((child
= *search
) != NULL
14785 && (child
->root
.type
== bfd_link_hash_defined
14786 || child
->root
.type
== bfd_link_hash_defweak
)
14787 && child
->root
.u
.def
.section
== sec
14788 && child
->root
.u
.def
.value
== offset
)
14792 /* xgettext:c-format */
14793 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14794 abfd
, sec
, (uint64_t) offset
);
14795 bfd_set_error (bfd_error_invalid_operation
);
14799 if (!child
->u2
.vtable
)
14801 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14802 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14803 if (!child
->u2
.vtable
)
14808 /* This *should* only be the absolute section. It could potentially
14809 be that someone has defined a non-global vtable though, which
14810 would be bad. It isn't worth paging in the local symbols to be
14811 sure though; that case should simply be handled by the assembler. */
14813 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14816 child
->u2
.vtable
->parent
= h
;
14821 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14824 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14825 struct elf_link_hash_entry
*h
,
14828 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14829 unsigned int log_file_align
= bed
->s
->log_file_align
;
14833 /* xgettext:c-format */
14834 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14836 bfd_set_error (bfd_error_bad_value
);
14842 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14843 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14848 if (addend
>= h
->u2
.vtable
->size
)
14850 size_t size
, bytes
, file_align
;
14851 bool *ptr
= h
->u2
.vtable
->used
;
14853 /* While the symbol is undefined, we have to be prepared to handle
14855 file_align
= 1 << log_file_align
;
14856 if (h
->root
.type
== bfd_link_hash_undefined
)
14857 size
= addend
+ file_align
;
14861 if (addend
>= size
)
14863 /* Oops! We've got a reference past the defined end of
14864 the table. This is probably a bug -- shall we warn? */
14865 size
= addend
+ file_align
;
14868 size
= (size
+ file_align
- 1) & -file_align
;
14870 /* Allocate one extra entry for use as a "done" flag for the
14871 consolidation pass. */
14872 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bool);
14876 ptr
= (bool *) bfd_realloc (ptr
- 1, bytes
);
14882 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14884 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14888 ptr
= (bool *) bfd_zmalloc (bytes
);
14893 /* And arrange for that done flag to be at index -1. */
14894 h
->u2
.vtable
->used
= ptr
+ 1;
14895 h
->u2
.vtable
->size
= size
;
14898 h
->u2
.vtable
->used
[addend
>> log_file_align
] = true;
14903 /* Map an ELF section header flag to its corresponding string. */
14907 flagword flag_value
;
14908 } elf_flags_to_name_table
;
14910 static const elf_flags_to_name_table elf_flags_to_names
[] =
14912 { "SHF_WRITE", SHF_WRITE
},
14913 { "SHF_ALLOC", SHF_ALLOC
},
14914 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14915 { "SHF_MERGE", SHF_MERGE
},
14916 { "SHF_STRINGS", SHF_STRINGS
},
14917 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14918 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14919 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14920 { "SHF_GROUP", SHF_GROUP
},
14921 { "SHF_TLS", SHF_TLS
},
14922 { "SHF_MASKOS", SHF_MASKOS
},
14923 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14926 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14928 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14929 struct flag_info
*flaginfo
,
14932 const bfd_vma sh_flags
= elf_section_flags (section
);
14934 if (!flaginfo
->flags_initialized
)
14936 bfd
*obfd
= info
->output_bfd
;
14937 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14938 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14940 int without_hex
= 0;
14942 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14945 flagword (*lookup
) (char *);
14947 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14948 if (lookup
!= NULL
)
14950 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14954 if (tf
->with
== with_flags
)
14955 with_hex
|= hexval
;
14956 else if (tf
->with
== without_flags
)
14957 without_hex
|= hexval
;
14962 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14964 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14966 if (tf
->with
== with_flags
)
14967 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14968 else if (tf
->with
== without_flags
)
14969 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14976 info
->callbacks
->einfo
14977 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14981 flaginfo
->flags_initialized
= true;
14982 flaginfo
->only_with_flags
|= with_hex
;
14983 flaginfo
->not_with_flags
|= without_hex
;
14986 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14989 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14995 struct alloc_got_off_arg
{
14997 struct bfd_link_info
*info
;
15000 /* We need a special top-level link routine to convert got reference counts
15001 to real got offsets. */
15004 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
15006 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
15007 bfd
*obfd
= gofarg
->info
->output_bfd
;
15008 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
15010 if (h
->got
.refcount
> 0)
15012 h
->got
.offset
= gofarg
->gotoff
;
15013 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
15016 h
->got
.offset
= (bfd_vma
) -1;
15021 /* And an accompanying bit to work out final got entry offsets once
15022 we're done. Should be called from final_link. */
15025 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
15026 struct bfd_link_info
*info
)
15029 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15031 struct alloc_got_off_arg gofarg
;
15033 BFD_ASSERT (abfd
== info
->output_bfd
);
15035 if (! is_elf_hash_table (info
->hash
))
15038 /* The GOT offset is relative to the .got section, but the GOT header is
15039 put into the .got.plt section, if the backend uses it. */
15040 if (bed
->want_got_plt
)
15043 gotoff
= bed
->got_header_size
;
15045 /* Do the local .got entries first. */
15046 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
15048 bfd_signed_vma
*local_got
;
15049 size_t j
, locsymcount
;
15050 Elf_Internal_Shdr
*symtab_hdr
;
15052 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
15055 local_got
= elf_local_got_refcounts (i
);
15059 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
15060 if (elf_bad_symtab (i
))
15061 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
15063 locsymcount
= symtab_hdr
->sh_info
;
15065 for (j
= 0; j
< locsymcount
; ++j
)
15067 if (local_got
[j
] > 0)
15069 local_got
[j
] = gotoff
;
15070 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
15073 local_got
[j
] = (bfd_vma
) -1;
15077 /* Then the global .got entries. .plt refcounts are handled by
15078 adjust_dynamic_symbol */
15079 gofarg
.gotoff
= gotoff
;
15080 gofarg
.info
= info
;
15081 elf_link_hash_traverse (elf_hash_table (info
),
15082 elf_gc_allocate_got_offsets
,
15087 /* Many folk need no more in the way of final link than this, once
15088 got entry reference counting is enabled. */
15091 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
15093 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
15096 /* Invoke the regular ELF backend linker to do all the work. */
15097 return bfd_elf_final_link (abfd
, info
);
15101 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
15103 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
15105 if (rcookie
->bad_symtab
)
15106 rcookie
->rel
= rcookie
->rels
;
15108 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
15110 unsigned long r_symndx
;
15112 if (! rcookie
->bad_symtab
)
15113 if (rcookie
->rel
->r_offset
> offset
)
15115 if (rcookie
->rel
->r_offset
!= offset
)
15118 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
15119 if (r_symndx
== STN_UNDEF
)
15122 struct elf_link_hash_entry
*h
;
15124 h
= get_ext_sym_hash_from_cookie (rcookie
, r_symndx
);
15128 if ((h
->root
.type
== bfd_link_hash_defined
15129 || h
->root
.type
== bfd_link_hash_defweak
)
15130 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
15131 || h
->root
.u
.def
.section
->kept_section
!= NULL
15132 || discarded_section (h
->root
.u
.def
.section
)))
15137 if (r_symndx
>= rcookie
->locsymcount
)
15138 /* This can happen with corrupt input. */
15141 /* It's not a relocation against a global symbol,
15142 but it could be a relocation against a local
15143 symbol for a discarded section. */
15145 Elf_Internal_Sym
*isym
;
15147 /* Need to: get the symbol; get the section. */
15148 isym
= &rcookie
->locsyms
[r_symndx
];
15149 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
15151 && (isec
->kept_section
!= NULL
15152 || discarded_section (isec
)))
15161 /* Discard unneeded references to discarded sections.
15162 Returns -1 on error, 1 if any section's size was changed, 0 if
15163 nothing changed. This function assumes that the relocations are in
15164 sorted order, which is true for all known assemblers. */
15167 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
15169 struct elf_reloc_cookie cookie
;
15174 if (info
->traditional_format
15175 || !is_elf_hash_table (info
->hash
))
15178 o
= bfd_get_section_by_name (output_bfd
, ".stab");
15183 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
15186 || i
->reloc_count
== 0
15187 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
15191 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15194 if (!init_reloc_cookie_for_section (&cookie
, info
, i
, false))
15197 if (_bfd_discard_section_stabs (abfd
, i
,
15198 elf_section_data (i
)->sec_info
,
15199 bfd_elf_reloc_symbol_deleted_p
,
15203 fini_reloc_cookie_for_section (&cookie
, i
);
15208 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
15209 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
15213 int eh_changed
= 0;
15214 unsigned int eh_alignment
; /* Octets. */
15216 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
15222 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15225 if (!init_reloc_cookie_for_section (&cookie
, info
, i
, false))
15228 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
15229 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
15230 bfd_elf_reloc_symbol_deleted_p
,
15234 if (i
->size
!= i
->rawsize
)
15238 fini_reloc_cookie_for_section (&cookie
, i
);
15241 eh_alignment
= ((1 << o
->alignment_power
)
15242 * bfd_octets_per_byte (output_bfd
, o
));
15243 /* Skip over zero terminator, and prevent empty sections from
15244 adding alignment padding at the end. */
15245 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
15247 i
->flags
|= SEC_EXCLUDE
;
15248 else if (i
->size
> 4)
15250 /* The last non-empty eh_frame section doesn't need padding. */
15253 /* Any prior sections must pad the last FDE out to the output
15254 section alignment. Otherwise we might have zero padding
15255 between sections, which would be seen as a terminator. */
15256 for (; i
!= NULL
; i
= i
->map_tail
.s
)
15258 /* All but the last zero terminator should have been removed. */
15263 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
15264 if (i
->size
!= size
)
15272 elf_link_hash_traverse (elf_hash_table (info
),
15273 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
15276 o
= bfd_get_section_by_name (output_bfd
, ".sframe");
15281 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
15287 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15290 if (!init_reloc_cookie_for_section (&cookie
, info
, i
, false))
15293 if (_bfd_elf_parse_sframe (abfd
, info
, i
, &cookie
))
15295 if (_bfd_elf_discard_section_sframe (i
,
15296 bfd_elf_reloc_symbol_deleted_p
,
15299 if (i
->size
!= i
->rawsize
)
15303 fini_reloc_cookie_for_section (&cookie
, i
);
15305 /* Update the reference to the output .sframe section. Used to
15306 determine later if PT_GNU_SFRAME segment is to be generated. */
15307 if (!_bfd_elf_set_section_sframe (output_bfd
, info
))
15311 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
15313 const struct elf_backend_data
*bed
;
15316 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15318 s
= abfd
->sections
;
15319 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
15322 bed
= get_elf_backend_data (abfd
);
15324 if (bed
->elf_backend_discard_info
!= NULL
)
15326 if (!init_reloc_cookie (&cookie
, info
, abfd
, false))
15329 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
15332 fini_reloc_cookie (&cookie
, abfd
);
15336 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
15337 _bfd_elf_end_eh_frame_parsing (info
);
15339 if (_bfd_elf_discard_section_eh_frame_hdr (info
))
15346 _bfd_elf_section_already_linked (bfd
*abfd
,
15348 struct bfd_link_info
*info
)
15351 const char *name
, *key
;
15352 struct bfd_section_already_linked
*l
;
15353 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
15355 if (sec
->output_section
== bfd_abs_section_ptr
)
15358 flags
= sec
->flags
;
15360 /* Return if it isn't a linkonce section. A comdat group section
15361 also has SEC_LINK_ONCE set. */
15362 if ((flags
& SEC_LINK_ONCE
) == 0)
15365 /* Don't put group member sections on our list of already linked
15366 sections. They are handled as a group via their group section. */
15367 if (elf_sec_group (sec
) != NULL
)
15370 /* For a SHT_GROUP section, use the group signature as the key. */
15372 if ((flags
& SEC_GROUP
) != 0
15373 && elf_next_in_group (sec
) != NULL
15374 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
15375 key
= elf_group_name (elf_next_in_group (sec
));
15378 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15379 if (startswith (name
, ".gnu.linkonce.")
15380 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
15383 /* Must be a user linkonce section that doesn't follow gcc's
15384 naming convention. In this case we won't be matching
15385 single member groups. */
15389 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
15391 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15393 /* We may have 2 different types of sections on the list: group
15394 sections with a signature of <key> (<key> is some string),
15395 and linkonce sections named .gnu.linkonce.<type>.<key>.
15396 Match like sections. LTO plugin sections are an exception.
15397 They are always named .gnu.linkonce.t.<key> and match either
15398 type of section. */
15399 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
15400 && ((flags
& SEC_GROUP
) != 0
15401 || strcmp (name
, l
->sec
->name
) == 0))
15402 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
15403 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
15405 /* The section has already been linked. See if we should
15406 issue a warning. */
15407 if (!_bfd_handle_already_linked (sec
, l
, info
))
15410 if (flags
& SEC_GROUP
)
15412 asection
*first
= elf_next_in_group (sec
);
15413 asection
*s
= first
;
15417 s
->output_section
= bfd_abs_section_ptr
;
15418 /* Record which group discards it. */
15419 s
->kept_section
= l
->sec
;
15420 s
= elf_next_in_group (s
);
15421 /* These lists are circular. */
15431 /* A single member comdat group section may be discarded by a
15432 linkonce section and vice versa. */
15433 if ((flags
& SEC_GROUP
) != 0)
15435 asection
*first
= elf_next_in_group (sec
);
15437 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
15438 /* Check this single member group against linkonce sections. */
15439 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15440 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15441 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
15443 first
->output_section
= bfd_abs_section_ptr
;
15444 first
->kept_section
= l
->sec
;
15445 sec
->output_section
= bfd_abs_section_ptr
;
15450 /* Check this linkonce section against single member groups. */
15451 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15452 if (l
->sec
->flags
& SEC_GROUP
)
15454 asection
*first
= elf_next_in_group (l
->sec
);
15457 && elf_next_in_group (first
) == first
15458 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
15460 sec
->output_section
= bfd_abs_section_ptr
;
15461 sec
->kept_section
= first
;
15466 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15467 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15468 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15469 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15470 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15471 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15472 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15473 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15474 The reverse order cannot happen as there is never a bfd with only the
15475 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15476 matter as here were are looking only for cross-bfd sections. */
15478 if ((flags
& SEC_GROUP
) == 0 && startswith (name
, ".gnu.linkonce.r."))
15479 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15480 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15481 && startswith (l
->sec
->name
, ".gnu.linkonce.t."))
15483 if (abfd
!= l
->sec
->owner
)
15484 sec
->output_section
= bfd_abs_section_ptr
;
15488 /* This is the first section with this name. Record it. */
15489 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
15490 info
->callbacks
->fatal (_("%P: already_linked_table: %E\n"));
15491 return sec
->output_section
== bfd_abs_section_ptr
;
15495 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
15497 return sym
->st_shndx
== SHN_COMMON
;
15501 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
15507 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
15509 return bfd_com_section_ptr
;
15513 _bfd_elf_default_got_elt_size (bfd
*abfd
,
15514 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
15515 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
15516 bfd
*ibfd ATTRIBUTE_UNUSED
,
15517 unsigned long symndx ATTRIBUTE_UNUSED
)
15519 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15520 return bed
->s
->arch_size
/ 8;
15523 /* Routines to support the creation of dynamic relocs. */
15525 /* Returns the name of the dynamic reloc section associated with SEC. */
15527 static const char *
15528 get_dynamic_reloc_section_name (bfd
* abfd
,
15533 const char *old_name
= bfd_section_name (sec
);
15534 const char *prefix
= is_rela
? ".rela" : ".rel";
15536 if (old_name
== NULL
)
15539 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
15540 sprintf (name
, "%s%s", prefix
, old_name
);
15545 /* Returns the dynamic reloc section associated with SEC.
15546 If necessary compute the name of the dynamic reloc section based
15547 on SEC's name (looked up in ABFD's string table) and the setting
15551 _bfd_elf_get_dynamic_reloc_section (bfd
*abfd
,
15555 asection
*reloc_sec
= elf_section_data (sec
)->sreloc
;
15557 if (reloc_sec
== NULL
)
15559 const char *name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15563 reloc_sec
= bfd_get_linker_section (abfd
, name
);
15565 if (reloc_sec
!= NULL
)
15566 elf_section_data (sec
)->sreloc
= reloc_sec
;
15573 /* Returns the dynamic reloc section associated with SEC. If the
15574 section does not exist it is created and attached to the DYNOBJ
15575 bfd and stored in the SRELOC field of SEC's elf_section_data
15578 ALIGNMENT is the alignment for the newly created section and
15579 IS_RELA defines whether the name should be .rela.<SEC's name>
15580 or .rel.<SEC's name>. The section name is looked up in the
15581 string table associated with ABFD. */
15584 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
15586 unsigned int alignment
,
15590 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
15592 if (reloc_sec
== NULL
)
15594 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15599 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
15601 if (reloc_sec
== NULL
)
15603 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
15604 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
15605 if ((sec
->flags
& SEC_ALLOC
) != 0)
15606 flags
|= SEC_ALLOC
| SEC_LOAD
;
15608 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
15609 if (reloc_sec
!= NULL
)
15611 /* _bfd_elf_get_sec_type_attr chooses a section type by
15612 name. Override as it may be wrong, eg. for a user
15613 section named "auto" we'll get ".relauto" which is
15614 seen to be a .rela section. */
15615 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
15616 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
15621 elf_section_data (sec
)->sreloc
= reloc_sec
;
15627 /* Copy the ELF symbol type and other attributes for a linker script
15628 assignment from HSRC to HDEST. Generally this should be treated as
15629 if we found a strong non-dynamic definition for HDEST (except that
15630 ld ignores multiple definition errors). */
15632 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
15633 struct bfd_link_hash_entry
*hdest
,
15634 struct bfd_link_hash_entry
*hsrc
)
15636 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
15637 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
15638 Elf_Internal_Sym isym
;
15640 ehdest
->type
= ehsrc
->type
;
15641 ehdest
->target_internal
= ehsrc
->target_internal
;
15643 isym
.st_other
= ehsrc
->other
;
15644 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, true, false);
15647 /* Append a RELA relocation REL to section S in BFD. */
15650 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15652 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15653 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
15654 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
15655 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
15658 /* Append a REL relocation REL to section S in BFD. */
15661 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15663 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15664 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
15665 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
15666 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
15669 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15671 struct bfd_link_hash_entry
*
15672 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
15673 const char *symbol
, asection
*sec
)
15675 struct elf_link_hash_entry
*h
;
15677 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
15678 false, false, true);
15679 /* NB: Common symbols will be turned into definition later. */
15681 && !h
->root
.ldscript_def
15682 && (h
->root
.type
== bfd_link_hash_undefined
15683 || h
->root
.type
== bfd_link_hash_undefweak
15684 || ((h
->ref_regular
|| h
->def_dynamic
)
15686 && h
->root
.type
!= bfd_link_hash_common
)))
15688 bool was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15689 h
->verinfo
.verdef
= NULL
;
15690 h
->root
.type
= bfd_link_hash_defined
;
15691 h
->root
.u
.def
.section
= sec
;
15692 h
->root
.u
.def
.value
= 0;
15693 h
->def_regular
= 1;
15694 h
->def_dynamic
= 0;
15696 h
->u2
.start_stop_section
= sec
;
15697 if (symbol
[0] == '.')
15699 /* .startof. and .sizeof. symbols are local. */
15700 const struct elf_backend_data
*bed
;
15701 bed
= get_elf_backend_data (info
->output_bfd
);
15702 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
15706 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15707 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15708 | info
->start_stop_visibility
);
15710 bfd_elf_link_record_dynamic_symbol (info
, h
);
15717 /* Find dynamic relocs for H that apply to read-only sections. */
15720 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15722 struct elf_dyn_relocs
*p
;
15724 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15726 asection
*s
= p
->sec
->output_section
;
15728 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15734 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15735 read-only sections. */
15738 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15742 if (h
->root
.type
== bfd_link_hash_indirect
)
15745 sec
= _bfd_elf_readonly_dynrelocs (h
);
15748 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15750 info
->flags
|= DF_TEXTREL
;
15751 /* xgettext:c-format */
15752 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15753 "in read-only section `%pA'\n"),
15754 sec
->owner
, h
->root
.root
.string
, sec
);
15756 if (bfd_link_textrel_check (info
))
15757 /* xgettext:c-format */
15758 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15759 "in read-only section `%pA'\n"),
15760 sec
->owner
, h
->root
.root
.string
, sec
);
15762 /* Not an error, just cut short the traversal. */
15768 /* Add dynamic tags. */
15771 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15772 bool need_dynamic_reloc
)
15774 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15776 if (htab
->dynamic_sections_created
)
15778 /* Add some entries to the .dynamic section. We fill in the
15779 values later, in finish_dynamic_sections, but we must add
15780 the entries now so that we get the correct size for the
15781 .dynamic section. The DT_DEBUG entry is filled in by the
15782 dynamic linker and used by the debugger. */
15783 #define add_dynamic_entry(TAG, VAL) \
15784 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15786 const struct elf_backend_data
*bed
15787 = get_elf_backend_data (output_bfd
);
15789 if (bfd_link_executable (info
))
15791 if (!add_dynamic_entry (DT_DEBUG
, 0))
15795 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15797 /* DT_PLTGOT is used by prelink even if there is no PLT
15799 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15803 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15805 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15806 || !add_dynamic_entry (DT_PLTREL
,
15807 (bed
->rela_plts_and_copies_p
15808 ? DT_RELA
: DT_REL
))
15809 || !add_dynamic_entry (DT_JMPREL
, 0))
15813 if (htab
->tlsdesc_plt
15814 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15815 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15818 if (need_dynamic_reloc
)
15820 if (bed
->rela_plts_and_copies_p
)
15822 if (!add_dynamic_entry (DT_RELA
, 0)
15823 || !add_dynamic_entry (DT_RELASZ
, 0)
15824 || !add_dynamic_entry (DT_RELAENT
,
15825 bed
->s
->sizeof_rela
))
15830 if (!add_dynamic_entry (DT_REL
, 0)
15831 || !add_dynamic_entry (DT_RELSZ
, 0)
15832 || !add_dynamic_entry (DT_RELENT
,
15833 bed
->s
->sizeof_rel
))
15837 /* If any dynamic relocs apply to a read-only section,
15838 then we need a DT_TEXTREL entry. */
15839 if ((info
->flags
& DF_TEXTREL
) == 0)
15840 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15843 if ((info
->flags
& DF_TEXTREL
) != 0)
15845 if (htab
->ifunc_resolvers
)
15846 info
->callbacks
->einfo
15847 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15848 "may result in a segfault at runtime; recompile with %s\n"),
15849 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15851 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15856 #undef add_dynamic_entry