1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2024 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
);
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
;
100 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
101 unsigned long r_symndx
,
104 if (r_symndx
>= cookie
->locsymcount
105 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
107 struct elf_link_hash_entry
*h
;
109 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
111 while (h
->root
.type
== bfd_link_hash_indirect
112 || h
->root
.type
== bfd_link_hash_warning
)
113 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
115 if ((h
->root
.type
== bfd_link_hash_defined
116 || h
->root
.type
== bfd_link_hash_defweak
)
117 && discarded_section (h
->root
.u
.def
.section
))
118 return h
->root
.u
.def
.section
;
124 /* It's not a relocation against a global symbol,
125 but it could be a relocation against a local
126 symbol for a discarded section. */
128 Elf_Internal_Sym
*isym
;
130 /* Need to: get the symbol; get the section. */
131 isym
= &cookie
->locsyms
[r_symndx
];
132 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
134 && discard
? discarded_section (isec
) : 1)
140 /* Define a symbol in a dynamic linkage section. */
142 struct elf_link_hash_entry
*
143 _bfd_elf_define_linkage_sym (bfd
*abfd
,
144 struct bfd_link_info
*info
,
148 struct elf_link_hash_entry
*h
;
149 struct bfd_link_hash_entry
*bh
;
150 const struct elf_backend_data
*bed
;
152 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, false, false, false);
155 /* Zap symbol defined in an as-needed lib that wasn't linked.
156 This is a symptom of a larger problem: Absolute symbols
157 defined in shared libraries can't be overridden, because we
158 lose the link to the bfd which is via the symbol section. */
159 h
->root
.type
= bfd_link_hash_new
;
165 bed
= get_elf_backend_data (abfd
);
166 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
167 sec
, 0, NULL
, false, bed
->collect
,
170 h
= (struct elf_link_hash_entry
*) bh
;
171 BFD_ASSERT (h
!= NULL
);
174 h
->root
.linker_def
= 1;
175 h
->type
= STT_OBJECT
;
176 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
177 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
179 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
184 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
188 struct elf_link_hash_entry
*h
;
189 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
190 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
192 /* This function may be called more than once. */
193 if (htab
->sgot
!= NULL
)
196 flags
= bed
->dynamic_sec_flags
;
198 s
= bfd_make_section_anyway_with_flags (abfd
,
199 (bed
->rela_plts_and_copies_p
200 ? ".rela.got" : ".rel.got"),
201 (bed
->dynamic_sec_flags
204 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
208 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
210 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
214 if (bed
->want_got_plt
)
216 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
218 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
223 /* The first bit of the global offset table is the header. */
224 s
->size
+= bed
->got_header_size
;
226 if (bed
->want_got_sym
)
228 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
229 (or .got.plt) section. We don't do this in the linker script
230 because we don't want to define the symbol if we are not creating
231 a global offset table. */
232 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
233 "_GLOBAL_OFFSET_TABLE_");
234 elf_hash_table (info
)->hgot
= h
;
242 /* Create a strtab to hold the dynamic symbol names. */
244 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
246 struct elf_link_hash_table
*hash_table
;
248 hash_table
= elf_hash_table (info
);
249 if (hash_table
->dynobj
== NULL
)
251 /* We may not set dynobj, an input file holding linker created
252 dynamic sections to abfd, which may be a dynamic object with
253 its own dynamic sections. We need to find a normal input file
254 to hold linker created sections if possible. */
255 if ((abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
)) != 0)
259 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
261 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0
262 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
263 && elf_object_id (ibfd
) == elf_hash_table_id (hash_table
)
264 && !((s
= ibfd
->sections
) != NULL
265 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
))
271 hash_table
->dynobj
= abfd
;
274 if (hash_table
->dynstr
== NULL
)
276 hash_table
->dynstr
= _bfd_elf_strtab_init ();
277 if (hash_table
->dynstr
== NULL
)
283 /* Create some sections which will be filled in with dynamic linking
284 information. ABFD is an input file which requires dynamic sections
285 to be created. The dynamic sections take up virtual memory space
286 when the final executable is run, so we need to create them before
287 addresses are assigned to the output sections. We work out the
288 actual contents and size of these sections later. */
291 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
295 const struct elf_backend_data
*bed
;
296 struct elf_link_hash_entry
*h
;
298 if (! is_elf_hash_table (info
->hash
))
301 if (elf_hash_table (info
)->dynamic_sections_created
)
304 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
307 abfd
= elf_hash_table (info
)->dynobj
;
308 bed
= get_elf_backend_data (abfd
);
310 flags
= bed
->dynamic_sec_flags
;
312 /* A dynamically linked executable has a .interp section, but a
313 shared library does not. */
314 if (bfd_link_executable (info
) && !info
->nointerp
)
316 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
317 flags
| SEC_READONLY
);
322 /* Create sections to hold version informations. These are removed
323 if they are not needed. */
324 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
325 flags
| SEC_READONLY
);
327 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
330 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
331 flags
| SEC_READONLY
);
333 || !bfd_set_section_alignment (s
, 1))
336 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
337 flags
| SEC_READONLY
);
339 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
342 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
343 flags
| SEC_READONLY
);
345 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
347 elf_hash_table (info
)->dynsym
= s
;
349 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
350 flags
| SEC_READONLY
);
354 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
356 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
358 elf_hash_table (info
)->dynamic
= s
;
360 /* The special symbol _DYNAMIC is always set to the start of the
361 .dynamic section. We could set _DYNAMIC in a linker script, but we
362 only want to define it if we are, in fact, creating a .dynamic
363 section. We don't want to define it if there is no .dynamic
364 section, since on some ELF platforms the start up code examines it
365 to decide how to initialize the process. */
366 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
367 elf_hash_table (info
)->hdynamic
= h
;
373 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
374 flags
| SEC_READONLY
);
376 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
378 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
381 if (info
->emit_gnu_hash
&& bed
->record_xhash_symbol
== NULL
)
383 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
384 flags
| SEC_READONLY
);
386 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
388 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
389 4 32-bit words followed by variable count of 64-bit words, then
390 variable count of 32-bit words. */
391 if (bed
->s
->arch_size
== 64)
392 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
394 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
397 if (info
->enable_dt_relr
)
399 s
= bfd_make_section_anyway_with_flags (abfd
, ".relr.dyn",
400 (bed
->dynamic_sec_flags
403 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
405 elf_hash_table (info
)->srelrdyn
= s
;
408 /* Let the backend create the rest of the sections. This lets the
409 backend set the right flags. The backend will normally create
410 the .got and .plt sections. */
411 if (bed
->elf_backend_create_dynamic_sections
== NULL
412 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
415 elf_hash_table (info
)->dynamic_sections_created
= true;
420 /* Create dynamic sections when linking against a dynamic object. */
423 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
425 flagword flags
, pltflags
;
426 struct elf_link_hash_entry
*h
;
428 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
429 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
431 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
432 .rel[a].bss sections. */
433 flags
= bed
->dynamic_sec_flags
;
436 if (bed
->plt_not_loaded
)
437 /* We do not clear SEC_ALLOC here because we still want the OS to
438 allocate space for the section; it's just that there's nothing
439 to read in from the object file. */
440 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
442 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
443 if (bed
->plt_readonly
)
444 pltflags
|= SEC_READONLY
;
446 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
448 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
452 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
454 if (bed
->want_plt_sym
)
456 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
457 "_PROCEDURE_LINKAGE_TABLE_");
458 elf_hash_table (info
)->hplt
= h
;
463 s
= bfd_make_section_anyway_with_flags (abfd
,
464 (bed
->rela_plts_and_copies_p
465 ? ".rela.plt" : ".rel.plt"),
466 flags
| SEC_READONLY
);
468 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
472 if (! _bfd_elf_create_got_section (abfd
, info
))
475 if (bed
->want_dynbss
)
477 /* The .dynbss section is a place to put symbols which are defined
478 by dynamic objects, are referenced by regular objects, and are
479 not functions. We must allocate space for them in the process
480 image and use a R_*_COPY reloc to tell the dynamic linker to
481 initialize them at run time. The linker script puts the .dynbss
482 section into the .bss section of the final image. */
483 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
484 SEC_ALLOC
| SEC_LINKER_CREATED
);
489 if (bed
->want_dynrelro
)
491 /* Similarly, but for symbols that were originally in read-only
492 sections. This section doesn't really need to have contents,
493 but make it like other .data.rel.ro sections. */
494 s
= bfd_make_section_anyway_with_flags (abfd
, ".data.rel.ro",
501 /* The .rel[a].bss section holds copy relocs. This section is not
502 normally needed. We need to create it here, though, so that the
503 linker will map it to an output section. We can't just create it
504 only if we need it, because we will not know whether we need it
505 until we have seen all the input files, and the first time the
506 main linker code calls BFD after examining all the input files
507 (size_dynamic_sections) the input sections have already been
508 mapped to the output sections. If the section turns out not to
509 be needed, we can discard it later. We will never need this
510 section when generating a shared object, since they do not use
512 if (bfd_link_executable (info
))
514 s
= bfd_make_section_anyway_with_flags (abfd
,
515 (bed
->rela_plts_and_copies_p
516 ? ".rela.bss" : ".rel.bss"),
517 flags
| SEC_READONLY
);
519 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
523 if (bed
->want_dynrelro
)
525 s
= (bfd_make_section_anyway_with_flags
526 (abfd
, (bed
->rela_plts_and_copies_p
527 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
528 flags
| SEC_READONLY
));
530 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
532 htab
->sreldynrelro
= s
;
540 /* Record a new dynamic symbol. We record the dynamic symbols as we
541 read the input files, since we need to have a list of all of them
542 before we can determine the final sizes of the output sections.
543 Note that we may actually call this function even though we are not
544 going to output any dynamic symbols; in some cases we know that a
545 symbol should be in the dynamic symbol table, but only if there is
549 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
550 struct elf_link_hash_entry
*h
)
552 if (h
->dynindx
== -1)
554 struct elf_strtab_hash
*dynstr
;
559 if (h
->root
.type
== bfd_link_hash_defined
560 || h
->root
.type
== bfd_link_hash_defweak
)
562 /* An IR symbol should not be made dynamic. */
563 if (h
->root
.u
.def
.section
!= NULL
564 && h
->root
.u
.def
.section
->owner
!= NULL
565 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
569 /* XXX: The ABI draft says the linker must turn hidden and
570 internal symbols into STB_LOCAL symbols when producing the
571 DSO. However, if ld.so honors st_other in the dynamic table,
572 this would not be necessary. */
573 switch (ELF_ST_VISIBILITY (h
->other
))
577 if (h
->root
.type
!= bfd_link_hash_undefined
578 && h
->root
.type
!= bfd_link_hash_undefweak
)
588 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
589 ++elf_hash_table (info
)->dynsymcount
;
591 dynstr
= elf_hash_table (info
)->dynstr
;
594 /* Create a strtab to hold the dynamic symbol names. */
595 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
600 char *unversioned_name
= NULL
;
602 /* We don't put any version information in the dynamic string
604 name
= h
->root
.root
.string
;
605 p
= strchr (name
, ELF_VER_CHR
);
608 unversioned_name
= bfd_malloc (p
- name
+ 1);
609 memcpy (unversioned_name
, name
, p
- name
);
610 unversioned_name
[p
- name
] = 0;
611 name
= unversioned_name
;
614 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
617 free (unversioned_name
);
619 if (indx
== (size_t) -1)
621 h
->dynstr_index
= indx
;
627 /* Mark a symbol dynamic. */
630 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
631 struct elf_link_hash_entry
*h
,
632 Elf_Internal_Sym
*sym
)
634 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
636 /* It may be called more than once on the same H. */
637 if(h
->dynamic
|| bfd_link_relocatable (info
))
640 if ((info
->dynamic_data
641 && (h
->type
== STT_OBJECT
642 || h
->type
== STT_COMMON
644 && (ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
645 || ELF_ST_TYPE (sym
->st_info
) == STT_COMMON
))))
648 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
651 /* NB: If a symbol is made dynamic by --dynamic-list, it has
653 h
->root
.non_ir_ref_dynamic
= 1;
657 /* Record an assignment to a symbol made by a linker script. We need
658 this in case some dynamic object refers to this symbol. */
661 bfd_elf_record_link_assignment (bfd
*output_bfd
,
662 struct bfd_link_info
*info
,
667 struct elf_link_hash_entry
*h
, *hv
;
668 struct elf_link_hash_table
*htab
;
669 const struct elf_backend_data
*bed
;
671 if (!is_elf_hash_table (info
->hash
))
674 htab
= elf_hash_table (info
);
675 h
= elf_link_hash_lookup (htab
, name
, !provide
, true, false);
679 if (h
->root
.type
== bfd_link_hash_warning
)
680 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
682 if (h
->versioned
== unknown
)
684 /* Set versioned if symbol version is unknown. */
685 char *version
= strrchr (name
, ELF_VER_CHR
);
688 if (version
> name
&& version
[-1] != ELF_VER_CHR
)
689 h
->versioned
= versioned_hidden
;
691 h
->versioned
= versioned
;
695 /* Symbols defined in a linker script but not referenced anywhere
696 else will have non_elf set. */
699 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
703 switch (h
->root
.type
)
705 case bfd_link_hash_defined
:
706 case bfd_link_hash_defweak
:
707 case bfd_link_hash_common
:
709 case bfd_link_hash_undefweak
:
710 case bfd_link_hash_undefined
:
711 /* Since we're defining the symbol, don't let it seem to have not
712 been defined. record_dynamic_symbol and size_dynamic_sections
713 may depend on this. */
714 h
->root
.type
= bfd_link_hash_new
;
715 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
716 bfd_link_repair_undef_list (&htab
->root
);
718 case bfd_link_hash_new
:
720 case bfd_link_hash_indirect
:
721 /* We had a versioned symbol in a dynamic library. We make the
722 the versioned symbol point to this one. */
723 bed
= get_elf_backend_data (output_bfd
);
725 while (hv
->root
.type
== bfd_link_hash_indirect
726 || hv
->root
.type
== bfd_link_hash_warning
)
727 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
728 /* We don't need to update h->root.u since linker will set them
730 h
->root
.type
= bfd_link_hash_undefined
;
731 hv
->root
.type
= bfd_link_hash_indirect
;
732 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
733 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
740 /* If this symbol is being provided by the linker script, and it is
741 currently defined by a dynamic object, but not by a regular
742 object, then mark it as undefined so that the generic linker will
743 force the correct value. */
747 h
->root
.type
= bfd_link_hash_undefined
;
749 /* If this symbol is currently defined by a dynamic object, but not
750 by a regular object, then clear out any version information because
751 the symbol will not be associated with the dynamic object any
753 if (h
->def_dynamic
&& !h
->def_regular
)
754 h
->verinfo
.verdef
= NULL
;
756 /* Make sure this symbol is not garbage collected. */
763 bed
= get_elf_backend_data (output_bfd
);
764 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
765 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
766 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
769 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
771 if (!bfd_link_relocatable (info
)
773 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
774 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
779 || bfd_link_dll (info
))
783 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
786 /* If this is a weak defined symbol, and we know a corresponding
787 real symbol from the same dynamic object, make sure the real
788 symbol is also made into a dynamic symbol. */
791 struct elf_link_hash_entry
*def
= weakdef (h
);
793 if (def
->dynindx
== -1
794 && !bfd_elf_link_record_dynamic_symbol (info
, def
))
802 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
803 success, and 2 on a failure caused by attempting to record a symbol
804 in a discarded section, eg. a discarded link-once section symbol. */
807 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
812 struct elf_link_local_dynamic_entry
*entry
;
813 struct elf_link_hash_table
*eht
;
814 struct elf_strtab_hash
*dynstr
;
817 Elf_External_Sym_Shndx eshndx
;
818 char esym
[sizeof (Elf64_External_Sym
)];
820 if (! is_elf_hash_table (info
->hash
))
823 /* See if the entry exists already. */
824 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
825 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
828 amt
= sizeof (*entry
);
829 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
833 /* Go find the symbol, so that we can find it's name. */
834 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
835 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
837 bfd_release (input_bfd
, entry
);
841 if (entry
->isym
.st_shndx
!= SHN_UNDEF
842 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
846 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
847 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
849 /* We can still bfd_release here as nothing has done another
850 bfd_alloc. We can't do this later in this function. */
851 bfd_release (input_bfd
, entry
);
856 name
= (bfd_elf_string_from_elf_section
857 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
858 entry
->isym
.st_name
));
860 dynstr
= elf_hash_table (info
)->dynstr
;
863 /* Create a strtab to hold the dynamic symbol names. */
864 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
869 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, false);
870 if (dynstr_index
== (size_t) -1)
872 entry
->isym
.st_name
= dynstr_index
;
874 eht
= elf_hash_table (info
);
876 entry
->next
= eht
->dynlocal
;
877 eht
->dynlocal
= entry
;
878 entry
->input_bfd
= input_bfd
;
879 entry
->input_indx
= input_indx
;
882 /* Whatever binding the symbol had before, it's now local. */
884 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
886 /* The dynindx will be set at the end of size_dynamic_sections. */
891 /* Return the dynindex of a local dynamic symbol. */
894 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
898 struct elf_link_local_dynamic_entry
*e
;
900 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
901 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
906 /* This function is used to renumber the dynamic symbols, if some of
907 them are removed because they are marked as local. This is called
908 via elf_link_hash_traverse. */
911 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
914 size_t *count
= (size_t *) data
;
919 if (h
->dynindx
!= -1)
920 h
->dynindx
= ++(*count
);
926 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
927 STB_LOCAL binding. */
930 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
933 size_t *count
= (size_t *) data
;
935 if (!h
->forced_local
)
938 if (h
->dynindx
!= -1)
939 h
->dynindx
= ++(*count
);
944 /* Return true if the dynamic symbol for a given section should be
945 omitted when creating a shared library. */
947 _bfd_elf_omit_section_dynsym_default (bfd
*output_bfd ATTRIBUTE_UNUSED
,
948 struct bfd_link_info
*info
,
951 struct elf_link_hash_table
*htab
;
954 switch (elf_section_data (p
)->this_hdr
.sh_type
)
958 /* If sh_type is yet undecided, assume it could be
959 SHT_PROGBITS/SHT_NOBITS. */
961 htab
= elf_hash_table (info
);
962 if (htab
->text_index_section
!= NULL
)
963 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
965 return (htab
->dynobj
!= NULL
966 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
967 && ip
->output_section
== p
);
969 /* There shouldn't be section relative relocations
970 against any other section. */
977 _bfd_elf_omit_section_dynsym_all
978 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
979 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
980 asection
*p ATTRIBUTE_UNUSED
)
985 /* Assign dynsym indices. In a shared library we generate a section
986 symbol for each output section, which come first. Next come symbols
987 which have been forced to local binding. Then all of the back-end
988 allocated local dynamic syms, followed by the rest of the global
989 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
990 (This prevents the early call before elf_backend_init_index_section
991 and strip_excluded_output_sections setting dynindx for sections
992 that are stripped.) */
995 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
996 struct bfd_link_info
*info
,
997 unsigned long *section_sym_count
)
999 unsigned long dynsymcount
= 0;
1000 bool do_sec
= section_sym_count
!= NULL
;
1002 if (bfd_link_pic (info
)
1003 || elf_hash_table (info
)->is_relocatable_executable
)
1005 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
1007 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
1008 if ((p
->flags
& SEC_EXCLUDE
) == 0
1009 && (p
->flags
& SEC_ALLOC
) != 0
1010 && elf_hash_table (info
)->dynamic_relocs
1011 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
1015 elf_section_data (p
)->dynindx
= dynsymcount
;
1018 elf_section_data (p
)->dynindx
= 0;
1021 *section_sym_count
= dynsymcount
;
1023 elf_link_hash_traverse (elf_hash_table (info
),
1024 elf_link_renumber_local_hash_table_dynsyms
,
1027 if (elf_hash_table (info
)->dynlocal
)
1029 struct elf_link_local_dynamic_entry
*p
;
1030 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
1031 p
->dynindx
= ++dynsymcount
;
1033 elf_hash_table (info
)->local_dynsymcount
= dynsymcount
;
1035 elf_link_hash_traverse (elf_hash_table (info
),
1036 elf_link_renumber_hash_table_dynsyms
,
1039 /* There is an unused NULL entry at the head of the table which we
1040 must account for in our count even if the table is empty since it
1041 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1042 .dynamic section. */
1045 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
1049 /* Merge st_other field. */
1052 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
1053 unsigned int st_other
, asection
*sec
,
1054 bool definition
, bool dynamic
)
1056 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1058 /* If st_other has a processor-specific meaning, specific
1059 code might be needed here. */
1060 if (bed
->elf_backend_merge_symbol_attribute
)
1061 (*bed
->elf_backend_merge_symbol_attribute
) (h
, st_other
, definition
,
1066 unsigned symvis
= ELF_ST_VISIBILITY (st_other
);
1067 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
1069 /* Keep the most constraining visibility. Leave the remainder
1070 of the st_other field to elf_backend_merge_symbol_attribute. */
1071 if (symvis
- 1 < hvis
- 1)
1072 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
1075 && ELF_ST_VISIBILITY (st_other
) != STV_DEFAULT
1076 && (sec
->flags
& SEC_READONLY
) == 0)
1077 h
->protected_def
= 1;
1080 /* This function is called when we want to merge a new symbol with an
1081 existing symbol. It handles the various cases which arise when we
1082 find a definition in a dynamic object, or when there is already a
1083 definition in a dynamic object. The new symbol is described by
1084 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1085 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1086 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1087 of an old common symbol. We set OVERRIDE if the old symbol is
1088 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1089 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1090 to change. By OK to change, we mean that we shouldn't warn if the
1091 type or size does change. */
1094 _bfd_elf_merge_symbol (bfd
*abfd
,
1095 struct bfd_link_info
*info
,
1097 Elf_Internal_Sym
*sym
,
1100 struct elf_link_hash_entry
**sym_hash
,
1103 unsigned int *pold_alignment
,
1106 bool *type_change_ok
,
1107 bool *size_change_ok
,
1110 asection
*sec
, *oldsec
;
1111 struct elf_link_hash_entry
*h
;
1112 struct elf_link_hash_entry
*hi
;
1113 struct elf_link_hash_entry
*flip
;
1116 bool newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
1117 bool newweak
, oldweak
, newfunc
, oldfunc
;
1118 const struct elf_backend_data
*bed
;
1120 bool default_sym
= *matched
;
1121 struct elf_link_hash_table
*htab
;
1127 bind
= ELF_ST_BIND (sym
->st_info
);
1129 if (! bfd_is_und_section (sec
))
1130 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, true, false, false);
1132 h
= ((struct elf_link_hash_entry
*)
1133 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, false, false));
1138 bed
= get_elf_backend_data (abfd
);
1140 /* NEW_VERSION is the symbol version of the new symbol. */
1141 if (h
->versioned
!= unversioned
)
1143 /* Symbol version is unknown or versioned. */
1144 new_version
= strrchr (name
, ELF_VER_CHR
);
1147 if (h
->versioned
== unknown
)
1149 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1150 h
->versioned
= versioned_hidden
;
1152 h
->versioned
= versioned
;
1155 if (new_version
[0] == '\0')
1159 h
->versioned
= unversioned
;
1164 /* For merging, we only care about real symbols. But we need to make
1165 sure that indirect symbol dynamic flags are updated. */
1167 while (h
->root
.type
== bfd_link_hash_indirect
1168 || h
->root
.type
== bfd_link_hash_warning
)
1169 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1173 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1177 /* OLD_HIDDEN is true if the existing symbol is only visible
1178 to the symbol with the same symbol version. NEW_HIDDEN is
1179 true if the new symbol is only visible to the symbol with
1180 the same symbol version. */
1181 bool old_hidden
= h
->versioned
== versioned_hidden
;
1182 bool new_hidden
= hi
->versioned
== versioned_hidden
;
1183 if (!old_hidden
&& !new_hidden
)
1184 /* The new symbol matches the existing symbol if both
1189 /* OLD_VERSION is the symbol version of the existing
1193 if (h
->versioned
>= versioned
)
1194 old_version
= strrchr (h
->root
.root
.string
,
1199 /* The new symbol matches the existing symbol if they
1200 have the same symbol version. */
1201 *matched
= (old_version
== new_version
1202 || (old_version
!= NULL
1203 && new_version
!= NULL
1204 && strcmp (old_version
, new_version
) == 0));
1209 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1214 switch (h
->root
.type
)
1219 case bfd_link_hash_undefined
:
1220 case bfd_link_hash_undefweak
:
1221 oldbfd
= h
->root
.u
.undef
.abfd
;
1224 case bfd_link_hash_defined
:
1225 case bfd_link_hash_defweak
:
1226 oldbfd
= h
->root
.u
.def
.section
->owner
;
1227 oldsec
= h
->root
.u
.def
.section
;
1230 case bfd_link_hash_common
:
1231 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1232 oldsec
= h
->root
.u
.c
.p
->section
;
1234 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1237 if (poldbfd
&& *poldbfd
== NULL
)
1240 /* Differentiate strong and weak symbols. */
1241 newweak
= bind
== STB_WEAK
;
1242 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1243 || h
->root
.type
== bfd_link_hash_undefweak
);
1245 *pold_weak
= oldweak
;
1247 /* We have to check it for every instance since the first few may be
1248 references and not all compilers emit symbol type for undefined
1250 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1252 htab
= elf_hash_table (info
);
1254 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1255 respectively, is from a dynamic object. */
1257 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1259 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1260 syms and defined syms in dynamic libraries respectively.
1261 ref_dynamic on the other hand can be set for a symbol defined in
1262 a dynamic library, and def_dynamic may not be set; When the
1263 definition in a dynamic lib is overridden by a definition in the
1264 executable use of the symbol in the dynamic lib becomes a
1265 reference to the executable symbol. */
1268 if (bfd_is_und_section (sec
))
1270 if (bind
!= STB_WEAK
)
1272 h
->ref_dynamic_nonweak
= 1;
1273 hi
->ref_dynamic_nonweak
= 1;
1278 /* Update the existing symbol only if they match. */
1281 hi
->dynamic_def
= 1;
1285 /* If we just created the symbol, mark it as being an ELF symbol.
1286 Other than that, there is nothing to do--there is no merge issue
1287 with a newly defined symbol--so we just return. */
1289 if (h
->root
.type
== bfd_link_hash_new
)
1295 /* In cases involving weak versioned symbols, we may wind up trying
1296 to merge a symbol with itself. Catch that here, to avoid the
1297 confusion that results if we try to override a symbol with
1298 itself. The additional tests catch cases like
1299 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1300 dynamic object, which we do want to handle here. */
1302 && (newweak
|| oldweak
)
1303 && ((abfd
->flags
& DYNAMIC
) == 0
1304 || !h
->def_regular
))
1309 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1310 else if (oldsec
!= NULL
)
1312 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1313 indices used by MIPS ELF. */
1314 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1317 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1318 if (!htab
->handling_dt_needed
1320 && (oldbfd
->flags
& BFD_PLUGIN
) != (abfd
->flags
& BFD_PLUGIN
))
1322 if (newdyn
!= olddyn
)
1324 /* Handle a case where plugin_notice won't be called and thus
1325 won't set the non_ir_ref flags on the first pass over
1327 h
->root
.non_ir_ref_dynamic
= true;
1328 hi
->root
.non_ir_ref_dynamic
= true;
1330 else if ((oldbfd
->flags
& BFD_PLUGIN
) != 0
1331 && hi
->root
.type
== bfd_link_hash_indirect
)
1333 /* Change indirect symbol from IR to undefined. */
1334 hi
->root
.type
= bfd_link_hash_undefined
;
1335 hi
->root
.u
.undef
.abfd
= oldbfd
;
1339 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1340 respectively, appear to be a definition rather than reference. */
1342 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1344 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1345 && h
->root
.type
!= bfd_link_hash_undefweak
1346 && h
->root
.type
!= bfd_link_hash_common
);
1348 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1349 respectively, appear to be a function. */
1351 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1352 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1354 oldfunc
= (h
->type
!= STT_NOTYPE
1355 && bed
->is_function_type (h
->type
));
1357 if (!(newfunc
&& oldfunc
)
1358 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1359 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1360 && h
->type
!= STT_NOTYPE
1361 && (newdef
|| bfd_is_com_section (sec
))
1362 && (olddef
|| h
->root
.type
== bfd_link_hash_common
))
1364 /* If creating a default indirect symbol ("foo" or "foo@") from
1365 a dynamic versioned definition ("foo@@") skip doing so if
1366 there is an existing regular definition with a different
1367 type. We don't want, for example, a "time" variable in the
1368 executable overriding a "time" function in a shared library. */
1376 /* When adding a symbol from a regular object file after we have
1377 created indirect symbols, undo the indirection and any
1384 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1385 h
->forced_local
= 0;
1389 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1391 h
->root
.type
= bfd_link_hash_undefined
;
1392 h
->root
.u
.undef
.abfd
= abfd
;
1396 h
->root
.type
= bfd_link_hash_new
;
1397 h
->root
.u
.undef
.abfd
= NULL
;
1403 /* Check TLS symbols. We don't check undefined symbols introduced
1404 by "ld -u" which have no type (and oldbfd NULL), and we don't
1405 check symbols from plugins because they also have no type. */
1407 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1408 && (abfd
->flags
& BFD_PLUGIN
) == 0
1409 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1410 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1414 asection
*ntsec
, *tsec
;
1416 if (h
->type
== STT_TLS
)
1437 /* xgettext:c-format */
1438 (_("%s: TLS definition in %pB section %pA "
1439 "mismatches non-TLS definition in %pB section %pA"),
1440 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
, ntsec
);
1441 else if (!tdef
&& !ntdef
)
1443 /* xgettext:c-format */
1444 (_("%s: TLS reference in %pB "
1445 "mismatches non-TLS reference in %pB"),
1446 h
->root
.root
.string
, tbfd
, ntbfd
);
1449 /* xgettext:c-format */
1450 (_("%s: TLS definition in %pB section %pA "
1451 "mismatches non-TLS reference in %pB"),
1452 h
->root
.root
.string
, tbfd
, tsec
, ntbfd
);
1455 /* xgettext:c-format */
1456 (_("%s: TLS reference in %pB "
1457 "mismatches non-TLS definition in %pB section %pA"),
1458 h
->root
.root
.string
, tbfd
, ntbfd
, ntsec
);
1460 bfd_set_error (bfd_error_bad_value
);
1464 /* If the old symbol has non-default visibility, we ignore the new
1465 definition from a dynamic object. */
1467 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1468 && !bfd_is_und_section (sec
))
1471 /* Make sure this symbol is dynamic. */
1473 hi
->ref_dynamic
= 1;
1474 /* A protected symbol has external availability. Make sure it is
1475 recorded as dynamic.
1477 FIXME: Should we check type and size for protected symbol? */
1478 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1479 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1484 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1487 /* If the new symbol with non-default visibility comes from a
1488 relocatable file and the old definition comes from a dynamic
1489 object, we remove the old definition. */
1490 if (hi
->root
.type
== bfd_link_hash_indirect
)
1492 /* Handle the case where the old dynamic definition is
1493 default versioned. We need to copy the symbol info from
1494 the symbol with default version to the normal one if it
1495 was referenced before. */
1498 hi
->root
.type
= h
->root
.type
;
1499 h
->root
.type
= bfd_link_hash_indirect
;
1500 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1502 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1503 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1505 /* If the new symbol is hidden or internal, completely undo
1506 any dynamic link state. */
1507 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1508 h
->forced_local
= 0;
1515 /* FIXME: Should we check type and size for protected symbol? */
1525 /* If the old symbol was undefined before, then it will still be
1526 on the undefs list. If the new symbol is undefined or
1527 common, we can't make it bfd_link_hash_new here, because new
1528 undefined or common symbols will be added to the undefs list
1529 by _bfd_generic_link_add_one_symbol. Symbols may not be
1530 added twice to the undefs list. Also, if the new symbol is
1531 undefweak then we don't want to lose the strong undef. */
1532 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1534 h
->root
.type
= bfd_link_hash_undefined
;
1535 h
->root
.u
.undef
.abfd
= abfd
;
1539 h
->root
.type
= bfd_link_hash_new
;
1540 h
->root
.u
.undef
.abfd
= NULL
;
1543 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1545 /* If the new symbol is hidden or internal, completely undo
1546 any dynamic link state. */
1547 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1548 h
->forced_local
= 0;
1554 /* FIXME: Should we check type and size for protected symbol? */
1560 /* If a new weak symbol definition comes from a regular file and the
1561 old symbol comes from a dynamic library, we treat the new one as
1562 strong. Similarly, an old weak symbol definition from a regular
1563 file is treated as strong when the new symbol comes from a dynamic
1564 library. Further, an old weak symbol from a dynamic library is
1565 treated as strong if the new symbol is from a dynamic library.
1566 This reflects the way glibc's ld.so works.
1568 Also allow a weak symbol to override a linker script symbol
1569 defined by an early pass over the script. This is done so the
1570 linker knows the symbol is defined in an object file, for the
1571 DEFINED script function.
1573 Do this before setting *type_change_ok or *size_change_ok so that
1574 we warn properly when dynamic library symbols are overridden. */
1576 if (newdef
&& !newdyn
&& (olddyn
|| h
->root
.ldscript_def
))
1578 if (olddef
&& newdyn
)
1581 /* Allow changes between different types of function symbol. */
1582 if (newfunc
&& oldfunc
)
1583 *type_change_ok
= true;
1585 /* It's OK to change the type if either the existing symbol or the
1586 new symbol is weak. A type change is also OK if the old symbol
1587 is undefined and the new symbol is defined. */
1592 && h
->root
.type
== bfd_link_hash_undefined
))
1593 *type_change_ok
= true;
1595 /* It's OK to change the size if either the existing symbol or the
1596 new symbol is weak, or if the old symbol is undefined. */
1599 || h
->root
.type
== bfd_link_hash_undefined
)
1600 *size_change_ok
= true;
1602 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1603 symbol, respectively, appears to be a common symbol in a dynamic
1604 object. If a symbol appears in an uninitialized section, and is
1605 not weak, and is not a function, then it may be a common symbol
1606 which was resolved when the dynamic object was created. We want
1607 to treat such symbols specially, because they raise special
1608 considerations when setting the symbol size: if the symbol
1609 appears as a common symbol in a regular object, and the size in
1610 the regular object is larger, we must make sure that we use the
1611 larger size. This problematic case can always be avoided in C,
1612 but it must be handled correctly when using Fortran shared
1615 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1616 likewise for OLDDYNCOMMON and OLDDEF.
1618 Note that this test is just a heuristic, and that it is quite
1619 possible to have an uninitialized symbol in a shared object which
1620 is really a definition, rather than a common symbol. This could
1621 lead to some minor confusion when the symbol really is a common
1622 symbol in some regular object. However, I think it will be
1628 && (sec
->flags
& SEC_ALLOC
) != 0
1629 && (sec
->flags
& SEC_LOAD
) == 0
1632 newdyncommon
= true;
1634 newdyncommon
= false;
1638 && h
->root
.type
== bfd_link_hash_defined
1640 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1641 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1644 olddyncommon
= true;
1646 olddyncommon
= false;
1648 /* We now know everything about the old and new symbols. We ask the
1649 backend to check if we can merge them. */
1650 if (bed
->merge_symbol
!= NULL
)
1652 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1657 /* There are multiple definitions of a normal symbol. Skip the
1658 default symbol as well as definition from an IR object. */
1659 if (olddef
&& !olddyn
&& !oldweak
&& newdef
&& !newdyn
&& !newweak
1660 && !default_sym
&& h
->def_regular
1662 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1663 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1665 /* Handle a multiple definition. */
1666 (*info
->callbacks
->multiple_definition
) (info
, &h
->root
,
1667 abfd
, sec
, *pvalue
);
1672 /* If both the old and the new symbols look like common symbols in a
1673 dynamic object, set the size of the symbol to the larger of the
1678 && sym
->st_size
!= h
->size
)
1680 /* Since we think we have two common symbols, issue a multiple
1681 common warning if desired. Note that we only warn if the
1682 size is different. If the size is the same, we simply let
1683 the old symbol override the new one as normally happens with
1684 symbols defined in dynamic objects. */
1686 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1687 bfd_link_hash_common
, sym
->st_size
);
1688 if (sym
->st_size
> h
->size
)
1689 h
->size
= sym
->st_size
;
1691 *size_change_ok
= true;
1694 /* If we are looking at a dynamic object, and we have found a
1695 definition, we need to see if the symbol was already defined by
1696 some other object. If so, we want to use the existing
1697 definition, and we do not want to report a multiple symbol
1698 definition error; we do this by clobbering *PSEC to be
1699 bfd_und_section_ptr.
1701 We treat a common symbol as a definition if the symbol in the
1702 shared library is a function, since common symbols always
1703 represent variables; this can cause confusion in principle, but
1704 any such confusion would seem to indicate an erroneous program or
1705 shared library. We also permit a common symbol in a regular
1706 object to override a weak symbol in a shared object. */
1711 || (h
->root
.type
== bfd_link_hash_common
1712 && (newweak
|| newfunc
))))
1716 newdyncommon
= false;
1718 *psec
= sec
= bfd_und_section_ptr
;
1719 *size_change_ok
= true;
1721 /* If we get here when the old symbol is a common symbol, then
1722 we are explicitly letting it override a weak symbol or
1723 function in a dynamic object, and we don't want to warn about
1724 a type change. If the old symbol is a defined symbol, a type
1725 change warning may still be appropriate. */
1727 if (h
->root
.type
== bfd_link_hash_common
)
1728 *type_change_ok
= true;
1731 /* Handle the special case of an old common symbol merging with a
1732 new symbol which looks like a common symbol in a shared object.
1733 We change *PSEC and *PVALUE to make the new symbol look like a
1734 common symbol, and let _bfd_generic_link_add_one_symbol do the
1738 && h
->root
.type
== bfd_link_hash_common
)
1742 newdyncommon
= false;
1743 *pvalue
= sym
->st_size
;
1744 *psec
= sec
= bed
->common_section (oldsec
);
1745 *size_change_ok
= true;
1748 /* Skip weak definitions of symbols that are already defined. */
1749 if (newdef
&& olddef
&& newweak
)
1751 /* Don't skip new non-IR weak syms. */
1752 if (!(oldbfd
!= NULL
1753 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1754 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1760 /* Merge st_other. If the symbol already has a dynamic index,
1761 but visibility says it should not be visible, turn it into a
1763 elf_merge_st_other (abfd
, h
, sym
->st_other
, sec
, newdef
, newdyn
);
1764 if (h
->dynindx
!= -1)
1765 switch (ELF_ST_VISIBILITY (h
->other
))
1769 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
1774 /* If the old symbol is from a dynamic object, and the new symbol is
1775 a definition which is not from a dynamic object, then the new
1776 symbol overrides the old symbol. Symbols from regular files
1777 always take precedence over symbols from dynamic objects, even if
1778 they are defined after the dynamic object in the link.
1780 As above, we again permit a common symbol in a regular object to
1781 override a definition in a shared object if the shared object
1782 symbol is a function or is weak. */
1787 || (bfd_is_com_section (sec
)
1788 && (oldweak
|| oldfunc
)))
1793 /* Change the hash table entry to undefined, and let
1794 _bfd_generic_link_add_one_symbol do the right thing with the
1797 h
->root
.type
= bfd_link_hash_undefined
;
1798 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1799 *size_change_ok
= true;
1802 olddyncommon
= false;
1804 /* We again permit a type change when a common symbol may be
1805 overriding a function. */
1807 if (bfd_is_com_section (sec
))
1811 /* If a common symbol overrides a function, make sure
1812 that it isn't defined dynamically nor has type
1815 h
->type
= STT_NOTYPE
;
1817 *type_change_ok
= true;
1820 if (hi
->root
.type
== bfd_link_hash_indirect
)
1823 /* This union may have been set to be non-NULL when this symbol
1824 was seen in a dynamic object. We must force the union to be
1825 NULL, so that it is correct for a regular symbol. */
1826 h
->verinfo
.vertree
= NULL
;
1829 /* Handle the special case of a new common symbol merging with an
1830 old symbol that looks like it might be a common symbol defined in
1831 a shared object. Note that we have already handled the case in
1832 which a new common symbol should simply override the definition
1833 in the shared library. */
1836 && bfd_is_com_section (sec
)
1839 /* It would be best if we could set the hash table entry to a
1840 common symbol, but we don't know what to use for the section
1841 or the alignment. */
1842 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1843 bfd_link_hash_common
, sym
->st_size
);
1845 /* If the presumed common symbol in the dynamic object is
1846 larger, pretend that the new symbol has its size. */
1848 if (h
->size
> *pvalue
)
1851 /* We need to remember the alignment required by the symbol
1852 in the dynamic object. */
1853 BFD_ASSERT (pold_alignment
);
1854 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1857 olddyncommon
= false;
1859 h
->root
.type
= bfd_link_hash_undefined
;
1860 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1862 *size_change_ok
= true;
1863 *type_change_ok
= true;
1865 if (hi
->root
.type
== bfd_link_hash_indirect
)
1868 h
->verinfo
.vertree
= NULL
;
1873 /* Handle the case where we had a versioned symbol in a dynamic
1874 library and now find a definition in a normal object. In this
1875 case, we make the versioned symbol point to the normal one. */
1876 flip
->root
.type
= h
->root
.type
;
1877 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1878 h
->root
.type
= bfd_link_hash_indirect
;
1879 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1880 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1884 flip
->ref_dynamic
= 1;
1891 /* This function is called to create an indirect symbol from the
1892 default for the symbol with the default version if needed. The
1893 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1894 set DYNSYM if the new indirect symbol is dynamic. */
1897 _bfd_elf_add_default_symbol (bfd
*abfd
,
1898 struct bfd_link_info
*info
,
1899 struct elf_link_hash_entry
*h
,
1901 Elf_Internal_Sym
*sym
,
1907 bool type_change_ok
;
1908 bool size_change_ok
;
1911 struct elf_link_hash_entry
*hi
;
1912 struct bfd_link_hash_entry
*bh
;
1913 const struct elf_backend_data
*bed
;
1918 size_t len
, shortlen
;
1922 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1925 /* If this symbol has a version, and it is the default version, we
1926 create an indirect symbol from the default name to the fully
1927 decorated name. This will cause external references which do not
1928 specify a version to be bound to this version of the symbol. */
1929 p
= strchr (name
, ELF_VER_CHR
);
1930 if (h
->versioned
== unknown
)
1934 h
->versioned
= unversioned
;
1939 if (p
[1] != ELF_VER_CHR
)
1941 h
->versioned
= versioned_hidden
;
1945 h
->versioned
= versioned
;
1950 /* PR ld/19073: We may see an unversioned definition after the
1956 bed
= get_elf_backend_data (abfd
);
1957 collect
= bed
->collect
;
1958 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1960 shortlen
= p
- name
;
1961 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1962 if (shortname
== NULL
)
1964 memcpy (shortname
, name
, shortlen
);
1965 shortname
[shortlen
] = '\0';
1967 /* We are going to create a new symbol. Merge it with any existing
1968 symbol with this name. For the purposes of the merge, act as
1969 though we were defining the symbol we just defined, although we
1970 actually going to define an indirect symbol. */
1971 type_change_ok
= false;
1972 size_change_ok
= false;
1975 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1976 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1977 &type_change_ok
, &size_change_ok
, &matched
))
1983 if (hi
->def_regular
|| ELF_COMMON_DEF_P (hi
))
1985 /* If the undecorated symbol will have a version added by a
1986 script different to H, then don't indirect to/from the
1987 undecorated symbol. This isn't ideal because we may not yet
1988 have seen symbol versions, if given by a script on the
1989 command line rather than via --version-script. */
1990 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1995 = bfd_find_version_for_sym (info
->version_info
,
1996 hi
->root
.root
.string
, &hide
);
1997 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1999 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
2003 if (hi
->verinfo
.vertree
!= NULL
2004 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
2010 /* Add the default symbol if not performing a relocatable link. */
2011 if (! bfd_link_relocatable (info
))
2014 if (bh
->type
== bfd_link_hash_defined
2015 && bh
->u
.def
.section
->owner
!= NULL
2016 && (bh
->u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)
2018 /* Mark the previous definition from IR object as
2019 undefined so that the generic linker will override
2021 bh
->type
= bfd_link_hash_undefined
;
2022 bh
->u
.undef
.abfd
= bh
->u
.def
.section
->owner
;
2024 if (! (_bfd_generic_link_add_one_symbol
2025 (info
, abfd
, shortname
, BSF_INDIRECT
,
2026 bfd_ind_section_ptr
,
2027 0, name
, false, collect
, &bh
)))
2029 hi
= (struct elf_link_hash_entry
*) bh
;
2034 /* In this case the symbol named SHORTNAME is overriding the
2035 indirect symbol we want to add. We were planning on making
2036 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2037 is the name without a version. NAME is the fully versioned
2038 name, and it is the default version.
2040 Overriding means that we already saw a definition for the
2041 symbol SHORTNAME in a regular object, and it is overriding
2042 the symbol defined in the dynamic object.
2044 When this happens, we actually want to change NAME, the
2045 symbol we just added, to refer to SHORTNAME. This will cause
2046 references to NAME in the shared object to become references
2047 to SHORTNAME in the regular object. This is what we expect
2048 when we override a function in a shared object: that the
2049 references in the shared object will be mapped to the
2050 definition in the regular object. */
2052 while (hi
->root
.type
== bfd_link_hash_indirect
2053 || hi
->root
.type
== bfd_link_hash_warning
)
2054 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2056 h
->root
.type
= bfd_link_hash_indirect
;
2057 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
2061 hi
->ref_dynamic
= 1;
2065 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
2070 /* Now set HI to H, so that the following code will set the
2071 other fields correctly. */
2075 /* Check if HI is a warning symbol. */
2076 if (hi
->root
.type
== bfd_link_hash_warning
)
2077 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2079 /* If there is a duplicate definition somewhere, then HI may not
2080 point to an indirect symbol. We will have reported an error to
2081 the user in that case. */
2083 if (hi
->root
.type
== bfd_link_hash_indirect
)
2085 struct elf_link_hash_entry
*ht
;
2087 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
2088 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
2090 /* If we first saw a reference to SHORTNAME with non-default
2091 visibility, merge that visibility to the @@VER symbol. */
2092 elf_merge_st_other (abfd
, ht
, hi
->other
, sec
, true, dynamic
);
2094 /* A reference to the SHORTNAME symbol from a dynamic library
2095 will be satisfied by the versioned symbol at runtime. In
2096 effect, we have a reference to the versioned symbol. */
2097 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2098 hi
->dynamic_def
|= ht
->dynamic_def
;
2100 /* See if the new flags lead us to realize that the symbol must
2106 if (! bfd_link_executable (info
)
2113 if (hi
->ref_regular
)
2119 /* We also need to define an indirection from the nondefault version
2123 len
= strlen (name
);
2124 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
2125 if (shortname
== NULL
)
2127 memcpy (shortname
, name
, shortlen
);
2128 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
2130 /* Once again, merge with any existing symbol. */
2131 type_change_ok
= false;
2132 size_change_ok
= false;
2134 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
2135 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
2136 &type_change_ok
, &size_change_ok
, &matched
))
2142 && h
->root
.type
== bfd_link_hash_defweak
2143 && hi
->root
.type
== bfd_link_hash_defined
)
2145 /* We are handling a weak sym@@ver and attempting to define
2146 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2147 new weak sym@ver because there is already a strong sym@ver.
2148 However, sym@ver and sym@@ver are really the same symbol.
2149 The existing strong sym@ver ought to override sym@@ver. */
2150 h
->root
.type
= bfd_link_hash_defined
;
2151 h
->root
.u
.def
.section
= hi
->root
.u
.def
.section
;
2152 h
->root
.u
.def
.value
= hi
->root
.u
.def
.value
;
2153 hi
->root
.type
= bfd_link_hash_indirect
;
2154 hi
->root
.u
.i
.link
= &h
->root
;
2161 /* Here SHORTNAME is a versioned name, so we don't expect to see
2162 the type of override we do in the case above unless it is
2163 overridden by a versioned definition. */
2164 if (hi
->root
.type
!= bfd_link_hash_defined
2165 && hi
->root
.type
!= bfd_link_hash_defweak
)
2167 /* xgettext:c-format */
2168 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2175 if (! (_bfd_generic_link_add_one_symbol
2176 (info
, abfd
, shortname
, BSF_INDIRECT
,
2177 bfd_ind_section_ptr
, 0, name
, false, collect
, &bh
)))
2179 hi
= (struct elf_link_hash_entry
*) bh
;
2182 /* If there is a duplicate definition somewhere, then HI may not
2183 point to an indirect symbol. We will have reported an error
2184 to the user in that case. */
2185 if (hi
->root
.type
== bfd_link_hash_indirect
)
2187 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
2188 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
2189 hi
->dynamic_def
|= h
->dynamic_def
;
2191 /* If we first saw a reference to @VER symbol with
2192 non-default visibility, merge that visibility to the
2194 elf_merge_st_other (abfd
, h
, hi
->other
, sec
, true, dynamic
);
2196 /* See if the new flags lead us to realize that the symbol
2202 if (! bfd_link_executable (info
)
2208 if (hi
->ref_regular
)
2217 /* This routine is used to export all defined symbols into the dynamic
2218 symbol table. It is called via elf_link_hash_traverse. */
2221 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
2223 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2225 /* Ignore indirect symbols. These are added by the versioning code. */
2226 if (h
->root
.type
== bfd_link_hash_indirect
)
2229 /* Ignore this if we won't export it. */
2230 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2233 if (h
->dynindx
== -1
2234 && (h
->def_regular
|| h
->ref_regular
)
2235 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2236 h
->root
.root
.string
))
2238 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2248 /* Return the glibc version reference if VERSION_DEP is added to the
2249 list of glibc version dependencies successfully. VERSION_DEP will
2250 be put into the .gnu.version_r section. */
2252 static Elf_Internal_Verneed
*
2253 elf_link_add_glibc_verneed (struct elf_find_verdep_info
*rinfo
,
2254 Elf_Internal_Verneed
*glibc_verref
,
2255 const char *version_dep
)
2257 Elf_Internal_Verneed
*t
;
2258 Elf_Internal_Vernaux
*a
;
2261 if (glibc_verref
!= NULL
)
2265 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2267 /* Return if VERSION_DEP dependency has been added. */
2268 if (a
->vna_nodename
== version_dep
2269 || strcmp (a
->vna_nodename
, version_dep
) == 0)
2277 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2281 const char *soname
= bfd_elf_get_dt_soname (t
->vn_bfd
);
2282 if (soname
!= NULL
&& startswith (soname
, "libc.so."))
2286 /* Skip the shared library if it isn't libc.so. */
2291 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2293 /* Return if VERSION_DEP dependency has been added. */
2294 if (a
->vna_nodename
== version_dep
2295 || strcmp (a
->vna_nodename
, version_dep
) == 0)
2298 /* Check if libc.so provides GLIBC_2.XX version. */
2299 if (!is_glibc
&& startswith (a
->vna_nodename
, "GLIBC_2."))
2303 /* Skip if it isn't linked against glibc. */
2309 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2312 rinfo
->failed
= true;
2316 a
->vna_nodename
= version_dep
;
2318 a
->vna_nextptr
= t
->vn_auxptr
;
2319 a
->vna_other
= rinfo
->vers
+ 1;
2327 /* Add VERSION_DEP to the list of version dependencies when linked
2331 _bfd_elf_link_add_glibc_version_dependency
2332 (struct elf_find_verdep_info
*rinfo
,
2333 const char *version_dep
[])
2335 Elf_Internal_Verneed
*t
= NULL
;
2339 t
= elf_link_add_glibc_verneed (rinfo
, t
, *version_dep
);
2340 /* Return if there is no glibc version reference. */
2345 while (*version_dep
!= NULL
);
2348 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2349 linked against glibc. */
2352 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info
*rinfo
)
2354 if (rinfo
->info
->enable_dt_relr
)
2356 const char *version
[] =
2358 "GLIBC_ABI_DT_RELR",
2361 _bfd_elf_link_add_glibc_version_dependency (rinfo
, version
);
2365 /* Look through the symbols which are defined in other shared
2366 libraries and referenced here. Update the list of version
2367 dependencies. This will be put into the .gnu.version_r section.
2368 This function is called via elf_link_hash_traverse. */
2371 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2374 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2375 Elf_Internal_Verneed
*t
;
2376 Elf_Internal_Vernaux
*a
;
2379 /* We only care about symbols defined in shared objects with version
2384 || h
->verinfo
.verdef
== NULL
2385 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2386 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2389 /* See if we already know about this version. */
2390 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2394 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2397 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2398 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2404 /* This is a new version. Add it to tree we are building. */
2409 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2412 rinfo
->failed
= true;
2416 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2417 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2418 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2422 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2425 rinfo
->failed
= true;
2429 /* Note that we are copying a string pointer here, and testing it
2430 above. If bfd_elf_string_from_elf_section is ever changed to
2431 discard the string data when low in memory, this will have to be
2433 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2435 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2436 a
->vna_nextptr
= t
->vn_auxptr
;
2438 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2441 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2448 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2449 hidden. Set *T_P to NULL if there is no match. */
2452 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2453 struct elf_link_hash_entry
*h
,
2454 const char *version_p
,
2455 struct bfd_elf_version_tree
**t_p
,
2458 struct bfd_elf_version_tree
*t
;
2460 /* Look for the version. If we find it, it is no longer weak. */
2461 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2463 if (strcmp (t
->name
, version_p
) == 0)
2467 struct bfd_elf_version_expr
*d
;
2469 len
= version_p
- h
->root
.root
.string
;
2470 alc
= (char *) bfd_malloc (len
);
2473 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2474 alc
[len
- 1] = '\0';
2475 if (alc
[len
- 2] == ELF_VER_CHR
)
2476 alc
[len
- 2] = '\0';
2478 h
->verinfo
.vertree
= t
;
2482 if (t
->globals
.list
!= NULL
)
2483 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2485 /* See if there is anything to force this symbol to
2487 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2489 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2492 && ! info
->export_dynamic
)
2506 /* Return TRUE if the symbol H is hidden by version script. */
2509 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2510 struct elf_link_hash_entry
*h
)
2514 const struct elf_backend_data
*bed
2515 = get_elf_backend_data (info
->output_bfd
);
2517 /* Version script only hides symbols defined in regular objects. */
2518 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2521 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2522 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2524 struct bfd_elf_version_tree
*t
;
2527 if (*p
== ELF_VER_CHR
)
2531 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2535 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2540 /* If we don't have a version for this symbol, see if we can find
2542 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2545 = bfd_find_version_for_sym (info
->version_info
,
2546 h
->root
.root
.string
, &hide
);
2547 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2549 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2557 /* Figure out appropriate versions for all the symbols. We may not
2558 have the version number script until we have read all of the input
2559 files, so until that point we don't know which symbols should be
2560 local. This function is called via elf_link_hash_traverse. */
2563 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2565 struct elf_info_failed
*sinfo
;
2566 struct bfd_link_info
*info
;
2567 const struct elf_backend_data
*bed
;
2568 struct elf_info_failed eif
;
2572 sinfo
= (struct elf_info_failed
*) data
;
2575 /* Fix the symbol flags. */
2578 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2581 sinfo
->failed
= true;
2585 bed
= get_elf_backend_data (info
->output_bfd
);
2587 /* We only need version numbers for symbols defined in regular
2589 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2591 /* Hide symbols defined in discarded input sections. */
2592 if ((h
->root
.type
== bfd_link_hash_defined
2593 || h
->root
.type
== bfd_link_hash_defweak
)
2594 && discarded_section (h
->root
.u
.def
.section
))
2595 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2600 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2601 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2603 struct bfd_elf_version_tree
*t
;
2606 if (*p
== ELF_VER_CHR
)
2609 /* If there is no version string, we can just return out. */
2613 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2615 sinfo
->failed
= true;
2620 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2622 /* If we are building an application, we need to create a
2623 version node for this version. */
2624 if (t
== NULL
&& bfd_link_executable (info
))
2626 struct bfd_elf_version_tree
**pp
;
2629 /* If we aren't going to export this symbol, we don't need
2630 to worry about it. */
2631 if (h
->dynindx
== -1)
2634 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2638 sinfo
->failed
= true;
2643 t
->name_indx
= (unsigned int) -1;
2647 /* Don't count anonymous version tag. */
2648 if (sinfo
->info
->version_info
!= NULL
2649 && sinfo
->info
->version_info
->vernum
== 0)
2651 for (pp
= &sinfo
->info
->version_info
;
2655 t
->vernum
= version_index
;
2659 h
->verinfo
.vertree
= t
;
2663 /* We could not find the version for a symbol when
2664 generating a shared archive. Return an error. */
2666 /* xgettext:c-format */
2667 (_("%pB: version node not found for symbol %s"),
2668 info
->output_bfd
, h
->root
.root
.string
);
2669 bfd_set_error (bfd_error_bad_value
);
2670 sinfo
->failed
= true;
2675 /* If we don't have a version for this symbol, see if we can find
2678 && h
->verinfo
.vertree
== NULL
2679 && sinfo
->info
->version_info
!= NULL
)
2682 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2683 h
->root
.root
.string
, &hide
);
2684 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2685 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2691 /* Read and swap the relocs from the section indicated by SHDR. This
2692 may be either a REL or a RELA section. The relocations are
2693 translated into RELA relocations and stored in INTERNAL_RELOCS,
2694 which should have already been allocated to contain enough space.
2695 The *EXTERNAL_RELOCS_P are a buffer where the external form of the
2696 relocations should be stored. If *EXTERNAL_RELOCS_ADDR is NULL,
2697 *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
2698 memory address and size. Otherwise, *EXTERNAL_RELOCS_ADDR is
2699 unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
2701 Returns FALSE if something goes wrong. */
2704 elf_link_read_relocs_from_section (bfd
*abfd
,
2705 const asection
*sec
,
2706 Elf_Internal_Shdr
*shdr
,
2707 void **external_relocs_addr
,
2708 size_t *external_relocs_size
,
2709 Elf_Internal_Rela
*internal_relocs
)
2711 const struct elf_backend_data
*bed
;
2712 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2713 const bfd_byte
*erela
;
2714 const bfd_byte
*erelaend
;
2715 Elf_Internal_Rela
*irela
;
2716 Elf_Internal_Shdr
*symtab_hdr
;
2718 void *external_relocs
= *external_relocs_addr
;
2720 /* Position ourselves at the start of the section. */
2721 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2724 /* Read the relocations. */
2725 *external_relocs_size
= shdr
->sh_size
;
2726 if (!_bfd_mmap_read_temporary (&external_relocs
,
2727 external_relocs_size
,
2728 external_relocs_addr
, abfd
, true))
2731 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2732 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2734 bed
= get_elf_backend_data (abfd
);
2736 /* Convert the external relocations to the internal format. */
2737 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2738 swap_in
= bed
->s
->swap_reloc_in
;
2739 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2740 swap_in
= bed
->s
->swap_reloca_in
;
2743 bfd_set_error (bfd_error_wrong_format
);
2747 erela
= (const bfd_byte
*) external_relocs
;
2748 /* Setting erelaend like this and comparing with <= handles case of
2749 a fuzzed object with sh_size not a multiple of sh_entsize. */
2750 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2751 irela
= internal_relocs
;
2752 while (erela
<= erelaend
)
2756 (*swap_in
) (abfd
, erela
, irela
);
2757 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2758 if (bed
->s
->arch_size
== 64)
2762 if ((size_t) r_symndx
>= nsyms
)
2765 /* xgettext:c-format */
2766 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2767 " for offset %#" PRIx64
" in section `%pA'"),
2768 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2769 (uint64_t) irela
->r_offset
, sec
);
2770 bfd_set_error (bfd_error_bad_value
);
2774 else if (r_symndx
!= STN_UNDEF
)
2777 /* xgettext:c-format */
2778 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2779 " for offset %#" PRIx64
" in section `%pA'"
2780 " when the object file has no symbol table"),
2781 abfd
, (uint64_t) r_symndx
,
2782 (uint64_t) irela
->r_offset
, sec
);
2783 bfd_set_error (bfd_error_bad_value
);
2786 irela
+= bed
->s
->int_rels_per_ext_rel
;
2787 erela
+= shdr
->sh_entsize
;
2793 /* Read and swap the relocs for a section O. They may have been
2794 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2795 not NULL, they are used as buffers to read into. They are known to
2796 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2797 the return value is allocated using either malloc or bfd_alloc,
2798 according to the KEEP_MEMORY argument. If O has two relocation
2799 sections (both REL and RELA relocations), then the REL_HDR
2800 relocations will appear first in INTERNAL_RELOCS, followed by the
2801 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2802 update cache_size. */
2805 _bfd_elf_link_info_read_relocs (bfd
*abfd
,
2806 struct bfd_link_info
*info
,
2808 void *external_relocs
,
2809 Elf_Internal_Rela
*internal_relocs
,
2812 void *alloc1
= NULL
;
2814 Elf_Internal_Rela
*alloc2
= NULL
;
2815 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2816 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2817 Elf_Internal_Rela
*internal_rela_relocs
;
2819 if (esdo
->relocs
!= NULL
)
2820 return esdo
->relocs
;
2822 if (o
->reloc_count
== 0)
2825 if (internal_relocs
== NULL
)
2829 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2832 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2834 info
->cache_size
+= size
;
2837 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2838 if (internal_relocs
== NULL
)
2842 alloc1
= external_relocs
;
2843 internal_rela_relocs
= internal_relocs
;
2846 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2847 &alloc1
, &alloc1_size
,
2850 external_relocs
= (((bfd_byte
*) external_relocs
)
2851 + esdo
->rel
.hdr
->sh_size
);
2852 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2853 * bed
->s
->int_rels_per_ext_rel
);
2857 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2858 &alloc1
, &alloc1_size
,
2859 internal_rela_relocs
)))
2862 /* Cache the results for next time, if we can. */
2864 esdo
->relocs
= internal_relocs
;
2866 _bfd_munmap_readonly_temporary (alloc1
, alloc1_size
);
2868 /* Don't free alloc2, since if it was allocated we are passing it
2869 back (under the name of internal_relocs). */
2871 return internal_relocs
;
2874 _bfd_munmap_readonly_temporary (alloc1
, alloc1_size
);
2878 bfd_release (abfd
, alloc2
);
2885 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2886 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2887 struct bfd_link_info. */
2890 _bfd_elf_link_read_relocs (bfd
*abfd
,
2892 void *external_relocs
,
2893 Elf_Internal_Rela
*internal_relocs
,
2896 return _bfd_elf_link_info_read_relocs (abfd
, NULL
, o
, external_relocs
,
2897 internal_relocs
, keep_memory
);
2901 /* Compute the size of, and allocate space for, REL_HDR which is the
2902 section header for a section containing relocations for O. */
2905 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2906 struct bfd_elf_section_reloc_data
*reldata
)
2908 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2910 /* That allows us to calculate the size of the section. */
2911 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2913 /* The contents field must last into write_object_contents, so we
2914 allocate it with bfd_alloc rather than malloc. Also since we
2915 cannot be sure that the contents will actually be filled in,
2916 we zero the allocated space. */
2917 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2918 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2921 if (reldata
->hashes
== NULL
&& reldata
->count
)
2923 struct elf_link_hash_entry
**p
;
2925 p
= ((struct elf_link_hash_entry
**)
2926 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2930 reldata
->hashes
= p
;
2936 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2937 originated from the section given by INPUT_REL_HDR) to the
2941 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2942 asection
*input_section
,
2943 Elf_Internal_Shdr
*input_rel_hdr
,
2944 Elf_Internal_Rela
*internal_relocs
,
2945 struct elf_link_hash_entry
**rel_hash
)
2947 Elf_Internal_Rela
*irela
;
2948 Elf_Internal_Rela
*irelaend
;
2950 struct bfd_elf_section_reloc_data
*output_reldata
;
2951 asection
*output_section
;
2952 const struct elf_backend_data
*bed
;
2953 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2954 struct bfd_elf_section_data
*esdo
;
2956 output_section
= input_section
->output_section
;
2958 bed
= get_elf_backend_data (output_bfd
);
2959 esdo
= elf_section_data (output_section
);
2960 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2962 output_reldata
= &esdo
->rel
;
2963 swap_out
= bed
->s
->swap_reloc_out
;
2965 else if (esdo
->rela
.hdr
2966 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2968 output_reldata
= &esdo
->rela
;
2969 swap_out
= bed
->s
->swap_reloca_out
;
2974 /* xgettext:c-format */
2975 (_("%pB: relocation size mismatch in %pB section %pA"),
2976 output_bfd
, input_section
->owner
, input_section
);
2977 bfd_set_error (bfd_error_wrong_format
);
2981 erel
= output_reldata
->hdr
->contents
;
2982 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2983 irela
= internal_relocs
;
2984 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2985 * bed
->s
->int_rels_per_ext_rel
);
2986 while (irela
< irelaend
)
2988 if (rel_hash
&& *rel_hash
)
2989 (*rel_hash
)->has_reloc
= 1;
2990 (*swap_out
) (output_bfd
, irela
, erel
);
2991 irela
+= bed
->s
->int_rels_per_ext_rel
;
2992 erel
+= input_rel_hdr
->sh_entsize
;
2997 /* Bump the counter, so that we know where to add the next set of
2999 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
3004 /* Make weak undefined symbols in PIE dynamic. */
3007 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
3008 struct elf_link_hash_entry
*h
)
3010 if (bfd_link_pie (info
)
3012 && h
->root
.type
== bfd_link_hash_undefweak
)
3013 return bfd_elf_link_record_dynamic_symbol (info
, h
);
3018 /* Fix up the flags for a symbol. This handles various cases which
3019 can only be fixed after all the input files are seen. This is
3020 currently called by both adjust_dynamic_symbol and
3021 assign_sym_version, which is unnecessary but perhaps more robust in
3022 the face of future changes. */
3025 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
3026 struct elf_info_failed
*eif
)
3028 const struct elf_backend_data
*bed
;
3030 /* If this symbol was mentioned in a non-ELF file, try to set
3031 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3032 permit a non-ELF file to correctly refer to a symbol defined in
3033 an ELF dynamic object. */
3036 while (h
->root
.type
== bfd_link_hash_indirect
)
3037 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3039 if (h
->root
.type
!= bfd_link_hash_defined
3040 && h
->root
.type
!= bfd_link_hash_defweak
)
3043 h
->ref_regular_nonweak
= 1;
3047 if (h
->root
.u
.def
.section
->owner
!= NULL
3048 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3049 == bfd_target_elf_flavour
))
3052 h
->ref_regular_nonweak
= 1;
3058 if (h
->dynindx
== -1
3062 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3071 /* Unfortunately, NON_ELF is only correct if the symbol
3072 was first seen in a non-ELF file. Fortunately, if the symbol
3073 was first seen in an ELF file, we're probably OK unless the
3074 symbol was defined in a non-ELF file. Catch that case here.
3075 FIXME: We're still in trouble if the symbol was first seen in
3076 a dynamic object, and then later in a non-ELF regular object. */
3077 if ((h
->root
.type
== bfd_link_hash_defined
3078 || h
->root
.type
== bfd_link_hash_defweak
)
3080 && (h
->root
.u
.def
.section
->owner
!= NULL
3081 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3082 != bfd_target_elf_flavour
)
3083 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3084 && !h
->def_dynamic
)))
3088 /* Backend specific symbol fixup. */
3089 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3090 if (bed
->elf_backend_fixup_symbol
3091 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
3094 /* If this is a final link, and the symbol was defined as a common
3095 symbol in a regular object file, and there was no definition in
3096 any dynamic object, then the linker will have allocated space for
3097 the symbol in a common section but the DEF_REGULAR
3098 flag will not have been set. */
3099 if (h
->root
.type
== bfd_link_hash_defined
3103 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
3106 /* Symbols defined in discarded sections shouldn't be dynamic. */
3107 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
3108 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3110 /* If a weak undefined symbol has non-default visibility, we also
3111 hide it from the dynamic linker. */
3112 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
3113 && h
->root
.type
== bfd_link_hash_undefweak
)
3114 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3116 /* A hidden versioned symbol in executable should be forced local if
3117 it is is locally defined, not referenced by shared library and not
3119 else if (bfd_link_executable (eif
->info
)
3120 && h
->versioned
== versioned_hidden
3121 && !eif
->info
->export_dynamic
3125 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3127 /* If -Bsymbolic was used (which means to bind references to global
3128 symbols to the definition within the shared object), and this
3129 symbol was defined in a regular object, then it actually doesn't
3130 need a PLT entry. Likewise, if the symbol has non-default
3131 visibility. If the symbol has hidden or internal visibility, we
3132 will force it local. */
3133 else if (h
->needs_plt
3134 && bfd_link_pic (eif
->info
)
3135 && is_elf_hash_table (eif
->info
->hash
)
3136 && (SYMBOLIC_BIND (eif
->info
, h
)
3137 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
3142 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3143 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
3144 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3147 /* If this is a weak defined symbol in a dynamic object, and we know
3148 the real definition in the dynamic object, copy interesting flags
3149 over to the real definition. */
3150 if (h
->is_weakalias
)
3152 struct elf_link_hash_entry
*def
= weakdef (h
);
3154 /* If the real definition is defined by a regular object file,
3155 don't do anything special. See the longer description in
3156 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3157 bfd_link_hash_defined as it was when put on the alias list
3158 then it must have originally been a versioned symbol (for
3159 which a non-versioned indirect symbol is created) and later
3160 a definition for the non-versioned symbol is found. In that
3161 case the indirection is flipped with the versioned symbol
3162 becoming an indirect pointing at the non-versioned symbol.
3163 Thus, not an alias any more. */
3164 if (def
->def_regular
3165 || def
->root
.type
!= bfd_link_hash_defined
)
3168 while ((h
= h
->u
.alias
) != def
)
3169 h
->is_weakalias
= 0;
3173 while (h
->root
.type
== bfd_link_hash_indirect
)
3174 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3175 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3176 || h
->root
.type
== bfd_link_hash_defweak
);
3177 BFD_ASSERT (def
->def_dynamic
);
3178 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
3185 /* Make the backend pick a good value for a dynamic symbol. This is
3186 called via elf_link_hash_traverse, and also calls itself
3190 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
3192 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3193 struct elf_link_hash_table
*htab
;
3194 const struct elf_backend_data
*bed
;
3196 if (! is_elf_hash_table (eif
->info
->hash
))
3199 /* Ignore indirect symbols. These are added by the versioning code. */
3200 if (h
->root
.type
== bfd_link_hash_indirect
)
3203 /* Fix the symbol flags. */
3204 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
3207 htab
= elf_hash_table (eif
->info
);
3208 bed
= get_elf_backend_data (htab
->dynobj
);
3210 if (h
->root
.type
== bfd_link_hash_undefweak
)
3212 if (eif
->info
->dynamic_undefined_weak
== 0)
3213 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3214 else if (eif
->info
->dynamic_undefined_weak
> 0
3216 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3217 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
3218 h
->root
.root
.string
))
3220 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3228 /* If this symbol does not require a PLT entry, and it is not
3229 defined by a dynamic object, or is not referenced by a regular
3230 object, ignore it. We do have to handle a weak defined symbol,
3231 even if no regular object refers to it, if we decided to add it
3232 to the dynamic symbol table. FIXME: Do we normally need to worry
3233 about symbols which are defined by one dynamic object and
3234 referenced by another one? */
3236 && h
->type
!= STT_GNU_IFUNC
3240 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3242 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3246 /* If we've already adjusted this symbol, don't do it again. This
3247 can happen via a recursive call. */
3248 if (h
->dynamic_adjusted
)
3251 /* Don't look at this symbol again. Note that we must set this
3252 after checking the above conditions, because we may look at a
3253 symbol once, decide not to do anything, and then get called
3254 recursively later after REF_REGULAR is set below. */
3255 h
->dynamic_adjusted
= 1;
3257 /* If this is a weak definition, and we know a real definition, and
3258 the real symbol is not itself defined by a regular object file,
3259 then get a good value for the real definition. We handle the
3260 real symbol first, for the convenience of the backend routine.
3262 Note that there is a confusing case here. If the real definition
3263 is defined by a regular object file, we don't get the real symbol
3264 from the dynamic object, but we do get the weak symbol. If the
3265 processor backend uses a COPY reloc, then if some routine in the
3266 dynamic object changes the real symbol, we will not see that
3267 change in the corresponding weak symbol. This is the way other
3268 ELF linkers work as well, and seems to be a result of the shared
3271 I will clarify this issue. Most SVR4 shared libraries define the
3272 variable _timezone and define timezone as a weak synonym. The
3273 tzset call changes _timezone. If you write
3274 extern int timezone;
3276 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3277 you might expect that, since timezone is a synonym for _timezone,
3278 the same number will print both times. However, if the processor
3279 backend uses a COPY reloc, then actually timezone will be copied
3280 into your process image, and, since you define _timezone
3281 yourself, _timezone will not. Thus timezone and _timezone will
3282 wind up at different memory locations. The tzset call will set
3283 _timezone, leaving timezone unchanged. */
3285 if (h
->is_weakalias
)
3287 struct elf_link_hash_entry
*def
= weakdef (h
);
3289 /* If we get to this point, there is an implicit reference to
3290 the alias by a regular object file via the weak symbol H. */
3291 def
->ref_regular
= 1;
3293 /* Ensure that the backend adjust_dynamic_symbol function sees
3294 the strong alias before H by recursively calling ourselves. */
3295 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3299 /* If a symbol has no type and no size and does not require a PLT
3300 entry, then we are probably about to do the wrong thing here: we
3301 are probably going to create a COPY reloc for an empty object.
3302 This case can arise when a shared object is built with assembly
3303 code, and the assembly code fails to set the symbol type. */
3305 && h
->type
== STT_NOTYPE
3308 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3309 h
->root
.root
.string
);
3311 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3320 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3324 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3325 struct elf_link_hash_entry
*h
,
3328 unsigned int power_of_two
;
3330 asection
*sec
= h
->root
.u
.def
.section
;
3332 /* The section alignment of the definition is the maximum alignment
3333 requirement of symbols defined in the section. Since we don't
3334 know the symbol alignment requirement, we start with the
3335 maximum alignment and check low bits of the symbol address
3336 for the minimum alignment. */
3337 power_of_two
= bfd_section_alignment (sec
);
3338 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3339 while ((h
->root
.u
.def
.value
& mask
) != 0)
3345 if (power_of_two
> bfd_section_alignment (dynbss
))
3347 /* Adjust the section alignment if needed. */
3348 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3352 /* We make sure that the symbol will be aligned properly. */
3353 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3355 /* Define the symbol as being at this point in DYNBSS. */
3356 h
->root
.u
.def
.section
= dynbss
;
3357 h
->root
.u
.def
.value
= dynbss
->size
;
3359 /* Increment the size of DYNBSS to make room for the symbol. */
3360 dynbss
->size
+= h
->size
;
3362 /* No error if extern_protected_data is true. */
3363 if (h
->protected_def
3364 && (!info
->extern_protected_data
3365 || (info
->extern_protected_data
< 0
3366 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3367 info
->callbacks
->einfo
3368 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3369 h
->root
.root
.string
);
3374 /* Adjust all external symbols pointing into SEC_MERGE sections
3375 to reflect the object merging within the sections. */
3378 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3382 if ((h
->root
.type
== bfd_link_hash_defined
3383 || h
->root
.type
== bfd_link_hash_defweak
)
3384 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3385 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3387 bfd
*output_bfd
= (bfd
*) data
;
3389 h
->root
.u
.def
.value
=
3390 _bfd_merged_section_offset (output_bfd
,
3391 &h
->root
.u
.def
.section
,
3392 elf_section_data (sec
)->sec_info
,
3393 h
->root
.u
.def
.value
);
3399 /* Returns false if the symbol referred to by H should be considered
3400 to resolve local to the current module, and true if it should be
3401 considered to bind dynamically. */
3404 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3405 struct bfd_link_info
*info
,
3406 bool not_local_protected
)
3408 bool binding_stays_local_p
;
3409 const struct elf_backend_data
*bed
;
3410 struct elf_link_hash_table
*hash_table
;
3415 while (h
->root
.type
== bfd_link_hash_indirect
3416 || h
->root
.type
== bfd_link_hash_warning
)
3417 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3419 /* If it was forced local, then clearly it's not dynamic. */
3420 if (h
->dynindx
== -1)
3422 if (h
->forced_local
)
3425 /* Identify the cases where name binding rules say that a
3426 visible symbol resolves locally. */
3427 binding_stays_local_p
= (bfd_link_executable (info
)
3428 || SYMBOLIC_BIND (info
, h
));
3430 switch (ELF_ST_VISIBILITY (h
->other
))
3437 hash_table
= elf_hash_table (info
);
3438 if (!is_elf_hash_table (&hash_table
->root
))
3441 bed
= get_elf_backend_data (hash_table
->dynobj
);
3443 /* Proper resolution for function pointer equality may require
3444 that these symbols perhaps be resolved dynamically, even though
3445 we should be resolving them to the current module. */
3446 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3447 binding_stays_local_p
= true;
3454 /* If it isn't defined locally, then clearly it's dynamic. */
3455 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3458 /* Otherwise, the symbol is dynamic if binding rules don't tell
3459 us that it remains local. */
3460 return !binding_stays_local_p
;
3463 /* Return true if the symbol referred to by H should be considered
3464 to resolve local to the current module, and false otherwise. Differs
3465 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3466 undefined symbols. The two functions are virtually identical except
3467 for the place where dynindx == -1 is tested. If that test is true,
3468 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3469 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3471 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3472 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3473 treatment of undefined weak symbols. For those that do not make
3474 undefined weak symbols dynamic, both functions may return false. */
3477 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3478 struct bfd_link_info
*info
,
3479 bool local_protected
)
3481 const struct elf_backend_data
*bed
;
3482 struct elf_link_hash_table
*hash_table
;
3484 /* If it's a local sym, of course we resolve locally. */
3488 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3489 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3490 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3493 /* Forced local symbols resolve locally. */
3494 if (h
->forced_local
)
3497 /* Common symbols that become definitions don't get the DEF_REGULAR
3498 flag set, so test it first, and don't bail out. */
3499 if (ELF_COMMON_DEF_P (h
))
3501 /* If we don't have a definition in a regular file, then we can't
3502 resolve locally. The sym is either undefined or dynamic. */
3503 else if (!h
->def_regular
)
3506 /* Non-dynamic symbols resolve locally. */
3507 if (h
->dynindx
== -1)
3510 /* At this point, we know the symbol is defined and dynamic. In an
3511 executable it must resolve locally, likewise when building symbolic
3512 shared libraries. */
3513 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3516 /* Now deal with defined dynamic symbols in shared libraries. Ones
3517 with default visibility might not resolve locally. */
3518 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3521 hash_table
= elf_hash_table (info
);
3522 if (!is_elf_hash_table (&hash_table
->root
))
3525 /* STV_PROTECTED symbols with indirect external access are local. */
3526 if (info
->indirect_extern_access
> 0)
3529 bed
= get_elf_backend_data (hash_table
->dynobj
);
3531 /* If extern_protected_data is false, STV_PROTECTED non-function
3532 symbols are local. */
3533 if ((!info
->extern_protected_data
3534 || (info
->extern_protected_data
< 0
3535 && !bed
->extern_protected_data
))
3536 && !bed
->is_function_type (h
->type
))
3539 /* Function pointer equality tests may require that STV_PROTECTED
3540 symbols be treated as dynamic symbols. If the address of a
3541 function not defined in an executable is set to that function's
3542 plt entry in the executable, then the address of the function in
3543 a shared library must also be the plt entry in the executable. */
3544 return local_protected
;
3547 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3548 aligned. Returns the first TLS output section. */
3550 struct bfd_section
*
3551 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3553 struct bfd_section
*sec
, *tls
;
3554 unsigned int align
= 0;
3556 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3557 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3561 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3562 if (sec
->alignment_power
> align
)
3563 align
= sec
->alignment_power
;
3565 elf_hash_table (info
)->tls_sec
= tls
;
3567 /* Ensure the alignment of the first section (usually .tdata) is the largest
3568 alignment, so that the tls segment starts aligned. */
3570 tls
->alignment_power
= align
;
3575 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3577 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3578 Elf_Internal_Sym
*sym
)
3580 const struct elf_backend_data
*bed
;
3582 /* Local symbols do not count, but target specific ones might. */
3583 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3584 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3587 bed
= get_elf_backend_data (abfd
);
3588 /* Function symbols do not count. */
3589 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3592 /* If the section is undefined, then so is the symbol. */
3593 if (sym
->st_shndx
== SHN_UNDEF
)
3596 /* If the symbol is defined in the common section, then
3597 it is a common definition and so does not count. */
3598 if (bed
->common_definition (sym
))
3601 /* If the symbol is in a target specific section then we
3602 must rely upon the backend to tell us what it is. */
3603 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3604 /* FIXME - this function is not coded yet:
3606 return _bfd_is_global_symbol_definition (abfd, sym);
3608 Instead for now assume that the definition is not global,
3609 Even if this is wrong, at least the linker will behave
3610 in the same way that it used to do. */
3616 /* Search the symbol table of the archive element of the archive ABFD
3617 whose archive map contains a mention of SYMDEF, and determine if
3618 the symbol is defined in this element. */
3620 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3622 Elf_Internal_Shdr
* hdr
;
3626 Elf_Internal_Sym
*isymbuf
;
3627 Elf_Internal_Sym
*isym
;
3628 Elf_Internal_Sym
*isymend
;
3631 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
, NULL
);
3635 if (! bfd_check_format (abfd
, bfd_object
))
3638 /* Select the appropriate symbol table. If we don't know if the
3639 object file is an IR object, give linker LTO plugin a chance to
3640 get the correct symbol table. */
3641 if (abfd
->plugin_format
== bfd_plugin_yes
3642 #if BFD_SUPPORTS_PLUGINS
3643 || (abfd
->plugin_format
== bfd_plugin_unknown
3644 && bfd_link_plugin_object_p (abfd
))
3648 /* Use the IR symbol table if the object has been claimed by
3650 abfd
= abfd
->plugin_dummy_bfd
;
3651 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3655 if (elf_use_dt_symtab_p (abfd
))
3657 bfd_set_error (bfd_error_wrong_format
);
3661 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3662 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3664 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3667 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3669 /* The sh_info field of the symtab header tells us where the
3670 external symbols start. We don't care about the local symbols. */
3671 if (elf_bad_symtab (abfd
))
3673 extsymcount
= symcount
;
3678 extsymcount
= symcount
- hdr
->sh_info
;
3679 extsymoff
= hdr
->sh_info
;
3682 if (extsymcount
== 0)
3685 /* Read in the symbol table. */
3686 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3688 if (isymbuf
== NULL
)
3691 /* Scan the symbol table looking for SYMDEF. */
3693 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3697 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3702 if (strcmp (name
, symdef
->name
) == 0)
3704 result
= is_global_data_symbol_definition (abfd
, isym
);
3714 /* Add an entry to the .dynamic table. */
3717 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3721 struct elf_link_hash_table
*hash_table
;
3722 const struct elf_backend_data
*bed
;
3724 bfd_size_type newsize
;
3725 bfd_byte
*newcontents
;
3726 Elf_Internal_Dyn dyn
;
3728 hash_table
= elf_hash_table (info
);
3729 if (! is_elf_hash_table (&hash_table
->root
))
3732 if (tag
== DT_RELA
|| tag
== DT_REL
)
3733 hash_table
->dynamic_relocs
= true;
3735 bed
= get_elf_backend_data (hash_table
->dynobj
);
3736 s
= hash_table
->dynamic
;
3737 BFD_ASSERT (s
!= NULL
);
3739 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3740 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3741 if (newcontents
== NULL
)
3745 dyn
.d_un
.d_val
= val
;
3746 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3749 s
->contents
= newcontents
;
3754 /* Strip zero-sized dynamic sections. */
3757 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3759 struct elf_link_hash_table
*hash_table
;
3760 const struct elf_backend_data
*bed
;
3761 asection
*s
, *sdynamic
, **pp
;
3762 asection
*rela_dyn
, *rel_dyn
;
3763 Elf_Internal_Dyn dyn
;
3764 bfd_byte
*extdyn
, *next
;
3765 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3766 bool strip_zero_sized
;
3767 bool strip_zero_sized_plt
;
3769 if (bfd_link_relocatable (info
))
3772 hash_table
= elf_hash_table (info
);
3773 if (!is_elf_hash_table (&hash_table
->root
))
3776 if (!hash_table
->dynobj
)
3779 sdynamic
= hash_table
->dynamic
;
3783 bed
= get_elf_backend_data (hash_table
->dynobj
);
3784 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3786 strip_zero_sized
= false;
3787 strip_zero_sized_plt
= false;
3789 /* Strip zero-sized dynamic sections. */
3790 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3791 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3792 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3796 || s
== hash_table
->srelplt
->output_section
3797 || s
== hash_table
->splt
->output_section
))
3800 info
->output_bfd
->section_count
--;
3801 strip_zero_sized
= true;
3806 else if (s
== hash_table
->splt
->output_section
)
3808 s
= hash_table
->splt
;
3809 strip_zero_sized_plt
= true;
3812 s
= hash_table
->srelplt
;
3813 s
->flags
|= SEC_EXCLUDE
;
3814 s
->output_section
= bfd_abs_section_ptr
;
3819 if (strip_zero_sized_plt
&& sdynamic
->size
!= 0)
3820 for (extdyn
= sdynamic
->contents
;
3821 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3824 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3825 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3833 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3834 the procedure linkage table (the .plt section) has been
3836 memmove (extdyn
, next
,
3837 sdynamic
->size
- (next
- sdynamic
->contents
));
3842 if (strip_zero_sized
)
3844 /* Regenerate program headers. */
3845 elf_seg_map (info
->output_bfd
) = NULL
;
3846 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
,
3853 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3854 1 if a DT_NEEDED tag already exists, and 0 on success. */
3857 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3859 struct elf_link_hash_table
*hash_table
;
3863 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3866 hash_table
= elf_hash_table (info
);
3867 soname
= elf_dt_name (abfd
);
3868 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, false);
3869 if (strindex
== (size_t) -1)
3872 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3875 const struct elf_backend_data
*bed
;
3878 bed
= get_elf_backend_data (hash_table
->dynobj
);
3879 sdyn
= hash_table
->dynamic
;
3880 if (sdyn
!= NULL
&& sdyn
->size
!= 0)
3881 for (extdyn
= sdyn
->contents
;
3882 extdyn
< sdyn
->contents
+ sdyn
->size
;
3883 extdyn
+= bed
->s
->sizeof_dyn
)
3885 Elf_Internal_Dyn dyn
;
3887 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3888 if (dyn
.d_tag
== DT_NEEDED
3889 && dyn
.d_un
.d_val
== strindex
)
3891 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3897 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3900 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3906 /* Return true if SONAME is on the needed list between NEEDED and STOP
3907 (or the end of list if STOP is NULL), and needed by a library that
3911 on_needed_list (const char *soname
,
3912 struct bfd_link_needed_list
*needed
,
3913 struct bfd_link_needed_list
*stop
)
3915 struct bfd_link_needed_list
*look
;
3916 for (look
= needed
; look
!= stop
; look
= look
->next
)
3917 if (strcmp (soname
, look
->name
) == 0
3918 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3919 /* If needed by a library that itself is not directly
3920 needed, recursively check whether that library is
3921 indirectly needed. Since we add DT_NEEDED entries to
3922 the end of the list, library dependencies appear after
3923 the library. Therefore search prior to the current
3924 LOOK, preventing possible infinite recursion. */
3925 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3931 /* Sort symbol by value, section, size, and type. */
3933 elf_sort_symbol (const void *arg1
, const void *arg2
)
3935 const struct elf_link_hash_entry
*h1
;
3936 const struct elf_link_hash_entry
*h2
;
3937 bfd_signed_vma vdiff
;
3942 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3943 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3944 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3946 return vdiff
> 0 ? 1 : -1;
3948 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3952 /* Sort so that sized symbols are selected over zero size symbols. */
3953 vdiff
= h1
->size
- h2
->size
;
3955 return vdiff
> 0 ? 1 : -1;
3957 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3958 if (h1
->type
!= h2
->type
)
3959 return h1
->type
- h2
->type
;
3961 /* If symbols are properly sized and typed, and multiple strong
3962 aliases are not defined in a shared library by the user we
3963 shouldn't get here. Unfortunately linker script symbols like
3964 __bss_start sometimes match a user symbol defined at the start of
3965 .bss without proper size and type. We'd like to preference the
3966 user symbol over reserved system symbols. Sort on leading
3968 n1
= h1
->root
.root
.string
;
3969 n2
= h2
->root
.root
.string
;
3982 /* Final sort on name selects user symbols like '_u' over reserved
3983 system symbols like '_Z' and also will avoid qsort instability. */
3987 /* This function is used to adjust offsets into .dynstr for
3988 dynamic symbols. This is called via elf_link_hash_traverse. */
3991 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3993 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3995 if (h
->dynindx
!= -1)
3996 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
4000 /* Assign string offsets in .dynstr, update all structures referencing
4004 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
4006 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
4007 struct elf_link_local_dynamic_entry
*entry
;
4008 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
4009 bfd
*dynobj
= hash_table
->dynobj
;
4012 const struct elf_backend_data
*bed
;
4015 _bfd_elf_strtab_finalize (dynstr
);
4016 size
= _bfd_elf_strtab_size (dynstr
);
4018 /* Allow the linker to examine the dynsymtab now it's fully populated. */
4020 if (info
->callbacks
->examine_strtab
)
4021 info
->callbacks
->examine_strtab (dynstr
);
4023 bed
= get_elf_backend_data (dynobj
);
4024 sdyn
= hash_table
->dynamic
;
4025 BFD_ASSERT (sdyn
!= NULL
);
4027 /* Update all .dynamic entries referencing .dynstr strings. */
4028 for (extdyn
= sdyn
->contents
;
4029 extdyn
< PTR_ADD (sdyn
->contents
, sdyn
->size
);
4030 extdyn
+= bed
->s
->sizeof_dyn
)
4032 Elf_Internal_Dyn dyn
;
4034 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
4038 dyn
.d_un
.d_val
= size
;
4048 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
4053 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
4056 /* Now update local dynamic symbols. */
4057 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
4058 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
4059 entry
->isym
.st_name
);
4061 /* And the rest of dynamic symbols. */
4062 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
4064 /* Adjust version definitions. */
4065 if (elf_tdata (output_bfd
)->cverdefs
)
4070 Elf_Internal_Verdef def
;
4071 Elf_Internal_Verdaux defaux
;
4073 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
4077 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
4079 p
+= sizeof (Elf_External_Verdef
);
4080 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
4082 for (i
= 0; i
< def
.vd_cnt
; ++i
)
4084 _bfd_elf_swap_verdaux_in (output_bfd
,
4085 (Elf_External_Verdaux
*) p
, &defaux
);
4086 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
4088 _bfd_elf_swap_verdaux_out (output_bfd
,
4089 &defaux
, (Elf_External_Verdaux
*) p
);
4090 p
+= sizeof (Elf_External_Verdaux
);
4093 while (def
.vd_next
);
4096 /* Adjust version references. */
4097 if (elf_tdata (output_bfd
)->verref
)
4102 Elf_Internal_Verneed need
;
4103 Elf_Internal_Vernaux needaux
;
4105 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
4109 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
4111 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
4112 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
4113 (Elf_External_Verneed
*) p
);
4114 p
+= sizeof (Elf_External_Verneed
);
4115 for (i
= 0; i
< need
.vn_cnt
; ++i
)
4117 _bfd_elf_swap_vernaux_in (output_bfd
,
4118 (Elf_External_Vernaux
*) p
, &needaux
);
4119 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
4121 _bfd_elf_swap_vernaux_out (output_bfd
,
4123 (Elf_External_Vernaux
*) p
);
4124 p
+= sizeof (Elf_External_Vernaux
);
4127 while (need
.vn_next
);
4133 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4134 The default is to only match when the INPUT and OUTPUT are exactly
4138 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
4139 const bfd_target
*output
)
4141 return input
== output
;
4144 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4145 This version is used when different targets for the same architecture
4146 are virtually identical. */
4149 _bfd_elf_relocs_compatible (const bfd_target
*input
,
4150 const bfd_target
*output
)
4152 const struct elf_backend_data
*obed
, *ibed
;
4154 if (input
== output
)
4157 ibed
= xvec_get_elf_backend_data (input
);
4158 obed
= xvec_get_elf_backend_data (output
);
4160 if (ibed
->arch
!= obed
->arch
)
4163 /* If both backends are using this function, deem them compatible. */
4164 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
4167 /* Make a special call to the linker "notice" function to tell it that
4168 we are about to handle an as-needed lib, or have finished
4169 processing the lib. */
4172 _bfd_elf_notice_as_needed (bfd
*ibfd
,
4173 struct bfd_link_info
*info
,
4174 enum notice_asneeded_action act
)
4176 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
4179 /* Call ACTION on each relocation in an ELF object file. */
4182 _bfd_elf_link_iterate_on_relocs
4183 (bfd
*abfd
, struct bfd_link_info
*info
,
4184 bool (*action
) (bfd
*, struct bfd_link_info
*, asection
*,
4185 const Elf_Internal_Rela
*))
4187 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4188 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4190 /* If this object is the same format as the output object, and it is
4191 not a shared library, then let the backend look through the
4194 This is required to build global offset table entries and to
4195 arrange for dynamic relocs. It is not required for the
4196 particular common case of linking non PIC code, even when linking
4197 against shared libraries, but unfortunately there is no way of
4198 knowing whether an object file has been compiled PIC or not.
4199 Looking through the relocs is not particularly time consuming.
4200 The problem is that we must either (1) keep the relocs in memory,
4201 which causes the linker to require additional runtime memory or
4202 (2) read the relocs twice from the input file, which wastes time.
4203 This would be a good case for using mmap.
4205 I have no idea how to handle linking PIC code into a file of a
4206 different format. It probably can't be done. */
4207 if ((abfd
->flags
& DYNAMIC
) == 0
4208 && is_elf_hash_table (&htab
->root
)
4209 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4210 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4214 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4216 Elf_Internal_Rela
*internal_relocs
;
4219 /* Don't check relocations in excluded sections. Don't do
4220 anything special with non-loaded, non-alloced sections.
4221 In particular, any relocs in such sections should not
4222 affect GOT and PLT reference counting (ie. we don't
4223 allow them to create GOT or PLT entries), there's no
4224 possibility or desire to optimize TLS relocs, and
4225 there's not much point in propagating relocs to shared
4226 libs that the dynamic linker won't relocate. */
4227 if ((o
->flags
& SEC_ALLOC
) == 0
4228 || (o
->flags
& SEC_RELOC
) == 0
4229 || (o
->flags
& SEC_EXCLUDE
) != 0
4230 || o
->reloc_count
== 0
4231 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4232 && (o
->flags
& SEC_DEBUGGING
) != 0)
4233 || bfd_is_abs_section (o
->output_section
))
4236 internal_relocs
= _bfd_elf_link_info_read_relocs
4237 (abfd
, info
, o
, NULL
, NULL
,
4238 _bfd_elf_link_keep_memory (info
));
4239 if (internal_relocs
== NULL
)
4242 ok
= action (abfd
, info
, o
, internal_relocs
);
4244 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4245 free (internal_relocs
);
4255 /* Check relocations in an ELF object file. This is called after
4256 all input files have been opened. */
4259 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
4261 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4262 if (bed
->check_relocs
!= NULL
)
4263 return _bfd_elf_link_iterate_on_relocs (abfd
, info
,
4268 /* An entry in the first definition hash table. */
4270 struct elf_link_first_hash_entry
4272 struct bfd_hash_entry root
;
4273 /* The object of the first definition. */
4277 /* The function to create a new entry in the first definition hash
4280 static struct bfd_hash_entry
*
4281 elf_link_first_hash_newfunc (struct bfd_hash_entry
*entry
,
4282 struct bfd_hash_table
*table
,
4285 struct elf_link_first_hash_entry
*ret
=
4286 (struct elf_link_first_hash_entry
*) entry
;
4288 /* Allocate the structure if it has not already been allocated by a
4291 ret
= (struct elf_link_first_hash_entry
*)
4292 bfd_hash_allocate (table
,
4293 sizeof (struct elf_link_first_hash_entry
));
4297 /* Call the allocation method of the superclass. */
4298 ret
= ((struct elf_link_first_hash_entry
*)
4299 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
,
4304 return (struct bfd_hash_entry
*) ret
;
4307 /* Add the symbol NAME from ABFD to first hash. */
4310 elf_link_add_to_first_hash (bfd
*abfd
, struct bfd_link_info
*info
,
4311 const char *name
, bool copy
)
4313 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4314 /* Skip if there is no first hash. */
4315 if (htab
->first_hash
== NULL
)
4318 struct elf_link_first_hash_entry
*e
4319 = ((struct elf_link_first_hash_entry
*)
4320 bfd_hash_lookup (htab
->first_hash
, name
, true, copy
));
4322 info
->callbacks
->einfo
4323 (_("%F%P: %pB: failed to add %s to first hash\n"), abfd
, name
);
4325 if (e
->abfd
== NULL
)
4326 /* Store ABFD in abfd. */
4330 /* Add symbols from an ELF object file to the linker hash table. */
4333 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4335 Elf_Internal_Ehdr
*ehdr
;
4336 Elf_Internal_Shdr
*hdr
;
4340 struct elf_link_hash_entry
**sym_hash
;
4342 Elf_External_Versym
*extversym
= NULL
;
4343 Elf_External_Versym
*extversym_end
= NULL
;
4344 Elf_External_Versym
*ever
;
4345 struct elf_link_hash_entry
*weaks
;
4346 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4347 size_t nondeflt_vers_cnt
= 0;
4348 Elf_Internal_Sym
*isymbuf
= NULL
;
4349 Elf_Internal_Sym
*isym
;
4350 Elf_Internal_Sym
*isymend
;
4351 const struct elf_backend_data
*bed
;
4353 struct elf_link_hash_table
*htab
;
4354 void *alloc_mark
= NULL
;
4355 struct bfd_hash_entry
**old_table
= NULL
;
4356 unsigned int old_size
= 0;
4357 unsigned int old_count
= 0;
4358 void *old_tab
= NULL
;
4360 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4361 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4362 void *old_strtab
= NULL
;
4367 htab
= elf_hash_table (info
);
4368 bed
= get_elf_backend_data (abfd
);
4370 if (elf_use_dt_symtab_p (abfd
))
4372 bfd_set_error (bfd_error_wrong_format
);
4376 if ((abfd
->flags
& DYNAMIC
) == 0)
4379 if ((abfd
->flags
& BFD_PLUGIN
) != 0
4380 && is_elf_hash_table (&htab
->root
)
4381 && htab
->first_hash
== NULL
)
4383 /* Initialize first_hash for an IR input. */
4384 htab
->first_hash
= (struct bfd_hash_table
*)
4385 bfd_malloc (sizeof (struct bfd_hash_table
));
4386 if (htab
->first_hash
== NULL
4387 || !bfd_hash_table_init
4388 (htab
->first_hash
, elf_link_first_hash_newfunc
,
4389 sizeof (struct elf_link_first_hash_entry
)))
4390 info
->callbacks
->einfo
4391 (_("%F%P: first_hash failed to create: %E\n"));
4398 /* You can't use -r against a dynamic object. Also, there's no
4399 hope of using a dynamic object which does not exactly match
4400 the format of the output file. */
4401 if (bfd_link_relocatable (info
)
4402 || !is_elf_hash_table (&htab
->root
)
4403 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4405 if (bfd_link_relocatable (info
))
4406 bfd_set_error (bfd_error_invalid_operation
);
4408 bfd_set_error (bfd_error_wrong_format
);
4413 ehdr
= elf_elfheader (abfd
);
4414 if (info
->warn_alternate_em
4415 && bed
->elf_machine_code
!= ehdr
->e_machine
4416 && ((bed
->elf_machine_alt1
!= 0
4417 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4418 || (bed
->elf_machine_alt2
!= 0
4419 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4421 /* xgettext:c-format */
4422 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4423 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4425 /* As a GNU extension, any input sections which are named
4426 .gnu.warning.SYMBOL are treated as warning symbols for the given
4427 symbol. This differs from .gnu.warning sections, which generate
4428 warnings when they are included in an output file. */
4429 /* PR 12761: Also generate this warning when building shared libraries. */
4430 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4434 name
= bfd_section_name (s
);
4435 if (startswith (name
, ".gnu.warning."))
4440 name
+= sizeof ".gnu.warning." - 1;
4442 /* If this is a shared object, then look up the symbol
4443 in the hash table. If it is there, and it is already
4444 been defined, then we will not be using the entry
4445 from this shared object, so we don't need to warn.
4446 FIXME: If we see the definition in a regular object
4447 later on, we will warn, but we shouldn't. The only
4448 fix is to keep track of what warnings we are supposed
4449 to emit, and then handle them all at the end of the
4453 struct elf_link_hash_entry
*h
;
4455 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
4457 /* FIXME: What about bfd_link_hash_common? */
4459 && (h
->root
.type
== bfd_link_hash_defined
4460 || h
->root
.type
== bfd_link_hash_defweak
))
4465 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4469 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4474 if (! (_bfd_generic_link_add_one_symbol
4475 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4476 false, bed
->collect
, NULL
)))
4479 if (bfd_link_executable (info
))
4481 /* Clobber the section size so that the warning does
4482 not get copied into the output file. */
4485 /* Also set SEC_EXCLUDE, so that symbols defined in
4486 the warning section don't get copied to the output. */
4487 s
->flags
|= SEC_EXCLUDE
;
4492 just_syms
= ((s
= abfd
->sections
) != NULL
4493 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4498 /* If we are creating a shared library, create all the dynamic
4499 sections immediately. We need to attach them to something,
4500 so we attach them to this BFD, provided it is the right
4501 format and is not from ld --just-symbols. Always create the
4502 dynamic sections for -E/--dynamic-list. FIXME: If there
4503 are no input BFD's of the same format as the output, we can't
4504 make a shared library. */
4506 && (bfd_link_pic (info
)
4507 || (!bfd_link_relocatable (info
)
4509 && (info
->export_dynamic
|| info
->dynamic
)))
4510 && is_elf_hash_table (&htab
->root
)
4511 && info
->output_bfd
->xvec
== abfd
->xvec
4512 && !htab
->dynamic_sections_created
)
4514 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4518 else if (!is_elf_hash_table (&htab
->root
))
4522 const char *soname
= NULL
;
4524 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4525 const Elf_Internal_Phdr
*phdr
;
4526 struct elf_link_loaded_list
*loaded_lib
;
4528 /* ld --just-symbols and dynamic objects don't mix very well.
4529 ld shouldn't allow it. */
4533 /* If this dynamic lib was specified on the command line with
4534 --as-needed in effect, then we don't want to add a DT_NEEDED
4535 tag unless the lib is actually used. Similary for libs brought
4536 in by another lib's DT_NEEDED. When --no-add-needed is used
4537 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4538 any dynamic library in DT_NEEDED tags in the dynamic lib at
4540 add_needed
= (elf_dyn_lib_class (abfd
)
4541 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4542 | DYN_NO_NEEDED
)) == 0;
4544 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4545 if (s
!= NULL
&& s
->size
!= 0 && (s
->flags
& SEC_HAS_CONTENTS
) != 0)
4549 unsigned int elfsec
;
4550 unsigned long shlink
;
4552 if (!_bfd_elf_mmap_section_contents (abfd
, s
, &dynbuf
))
4555 _bfd_elf_munmap_section_contents (s
, dynbuf
);
4559 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4560 if (elfsec
== SHN_BAD
)
4561 goto error_free_dyn
;
4562 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4564 for (extdyn
= dynbuf
;
4565 (size_t) (dynbuf
+ s
->size
- extdyn
) >= bed
->s
->sizeof_dyn
;
4566 extdyn
+= bed
->s
->sizeof_dyn
)
4568 Elf_Internal_Dyn dyn
;
4570 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4571 if (dyn
.d_tag
== DT_SONAME
)
4573 unsigned int tagv
= dyn
.d_un
.d_val
;
4574 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4576 goto error_free_dyn
;
4578 if (dyn
.d_tag
== DT_NEEDED
)
4580 struct bfd_link_needed_list
*n
, **pn
;
4582 unsigned int tagv
= dyn
.d_un
.d_val
;
4583 size_t amt
= sizeof (struct bfd_link_needed_list
);
4585 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4586 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4587 if (n
== NULL
|| fnm
== NULL
)
4588 goto error_free_dyn
;
4589 amt
= strlen (fnm
) + 1;
4590 anm
= (char *) bfd_alloc (abfd
, amt
);
4592 goto error_free_dyn
;
4593 memcpy (anm
, fnm
, amt
);
4597 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4601 if (dyn
.d_tag
== DT_RUNPATH
)
4603 struct bfd_link_needed_list
*n
, **pn
;
4605 unsigned int tagv
= dyn
.d_un
.d_val
;
4606 size_t amt
= sizeof (struct bfd_link_needed_list
);
4608 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4609 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4610 if (n
== NULL
|| fnm
== NULL
)
4611 goto error_free_dyn
;
4612 amt
= strlen (fnm
) + 1;
4613 anm
= (char *) bfd_alloc (abfd
, amt
);
4615 goto error_free_dyn
;
4616 memcpy (anm
, fnm
, amt
);
4620 for (pn
= & runpath
;
4626 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4627 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4629 struct bfd_link_needed_list
*n
, **pn
;
4631 unsigned int tagv
= dyn
.d_un
.d_val
;
4632 size_t amt
= sizeof (struct bfd_link_needed_list
);
4634 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4635 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4636 if (n
== NULL
|| fnm
== NULL
)
4637 goto error_free_dyn
;
4638 amt
= strlen (fnm
) + 1;
4639 anm
= (char *) bfd_alloc (abfd
, amt
);
4641 goto error_free_dyn
;
4642 memcpy (anm
, fnm
, amt
);
4652 if (dyn
.d_tag
== DT_AUDIT
)
4654 unsigned int tagv
= dyn
.d_un
.d_val
;
4655 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4657 if (dyn
.d_tag
== DT_FLAGS_1
)
4658 elf_tdata (abfd
)->is_pie
= (dyn
.d_un
.d_val
& DF_1_PIE
) != 0;
4661 _bfd_elf_munmap_section_contents (s
, dynbuf
);
4664 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4665 frees all more recently bfd_alloc'd blocks as well. */
4671 struct bfd_link_needed_list
**pn
;
4672 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4677 /* If we have a PT_GNU_RELRO program header, mark as read-only
4678 all sections contained fully therein. This makes relro
4679 shared library sections appear as they will at run-time. */
4680 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4681 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4682 if (phdr
->p_type
== PT_GNU_RELRO
)
4684 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4686 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4688 if ((s
->flags
& SEC_ALLOC
) != 0
4689 && s
->vma
* opb
>= phdr
->p_vaddr
4690 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4691 s
->flags
|= SEC_READONLY
;
4696 /* We do not want to include any of the sections in a dynamic
4697 object in the output file. We hack by simply clobbering the
4698 list of sections in the BFD. This could be handled more
4699 cleanly by, say, a new section flag; the existing
4700 SEC_NEVER_LOAD flag is not the one we want, because that one
4701 still implies that the section takes up space in the output
4703 bfd_section_list_clear (abfd
);
4705 /* Find the name to use in a DT_NEEDED entry that refers to this
4706 object. If the object has a DT_SONAME entry, we use it.
4707 Otherwise, if the generic linker stuck something in
4708 elf_dt_name, we use that. Otherwise, we just use the file
4710 if (soname
== NULL
|| *soname
== '\0')
4712 soname
= elf_dt_name (abfd
);
4713 if (soname
== NULL
|| *soname
== '\0')
4714 soname
= bfd_get_filename (abfd
);
4717 /* Save the SONAME because sometimes the linker emulation code
4718 will need to know it. */
4719 elf_dt_name (abfd
) = soname
;
4721 /* If we have already included this dynamic object in the
4722 link, just ignore it. There is no reason to include a
4723 particular dynamic object more than once. */
4724 for (loaded_lib
= htab
->dyn_loaded
;
4726 loaded_lib
= loaded_lib
->next
)
4728 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4732 /* Create dynamic sections for backends that require that be done
4733 before setup_gnu_properties. */
4735 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4738 /* Save the DT_AUDIT entry for the linker emulation code. */
4739 elf_dt_audit (abfd
) = audit
;
4742 /* If this is a dynamic object, we always link against the .dynsym
4743 symbol table, not the .symtab symbol table. The dynamic linker
4744 will only see the .dynsym symbol table, so there is no reason to
4745 look at .symtab for a dynamic object. */
4747 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4748 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4750 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4752 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4754 /* The sh_info field of the symtab header tells us where the
4755 external symbols start. We don't care about the local symbols at
4757 if (elf_bad_symtab (abfd
))
4759 extsymcount
= symcount
;
4764 extsymcount
= symcount
- hdr
->sh_info
;
4765 extsymoff
= hdr
->sh_info
;
4768 sym_hash
= elf_sym_hashes (abfd
);
4769 if (extsymcount
!= 0)
4771 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4773 if (isymbuf
== NULL
)
4776 if (sym_hash
== NULL
)
4778 /* We store a pointer to the hash table entry for each
4780 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4781 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4782 if (sym_hash
== NULL
)
4783 goto error_free_sym
;
4784 elf_sym_hashes (abfd
) = sym_hash
;
4790 /* Read in any version definitions. */
4791 if (!_bfd_elf_slurp_version_tables (abfd
,
4792 info
->default_imported_symver
))
4793 goto error_free_sym
;
4795 /* Read in the symbol versions, but don't bother to convert them
4796 to internal format. */
4797 if (elf_dynversym (abfd
) != 0)
4799 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4800 bfd_size_type amt
= versymhdr
->sh_size
;
4802 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4803 goto error_free_sym
;
4804 extversym
= (Elf_External_Versym
*)
4805 _bfd_malloc_and_read (abfd
, amt
, amt
);
4806 if (extversym
== NULL
)
4807 goto error_free_sym
;
4808 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4812 /* If we are loading an as-needed shared lib, save the symbol table
4813 state before we start adding symbols. If the lib turns out
4814 to be unneeded, restore the state. */
4815 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4820 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4822 struct bfd_hash_entry
*p
;
4823 struct elf_link_hash_entry
*h
;
4825 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4827 h
= (struct elf_link_hash_entry
*) p
;
4828 entsize
+= htab
->root
.table
.entsize
;
4829 if (h
->root
.type
== bfd_link_hash_warning
)
4831 entsize
+= htab
->root
.table
.entsize
;
4832 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4834 if (h
->root
.type
== bfd_link_hash_common
)
4835 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4839 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4840 old_tab
= bfd_malloc (tabsize
+ entsize
);
4841 if (old_tab
== NULL
)
4842 goto error_free_vers
;
4844 /* Remember the current objalloc pointer, so that all mem for
4845 symbols added can later be reclaimed. */
4846 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4847 if (alloc_mark
== NULL
)
4848 goto error_free_vers
;
4850 /* Make a special call to the linker "notice" function to
4851 tell it that we are about to handle an as-needed lib. */
4852 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4853 goto error_free_vers
;
4855 /* Clone the symbol table. Remember some pointers into the
4856 symbol table, and dynamic symbol count. */
4857 old_ent
= (char *) old_tab
+ tabsize
;
4858 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4859 old_undefs
= htab
->root
.undefs
;
4860 old_undefs_tail
= htab
->root
.undefs_tail
;
4861 old_table
= htab
->root
.table
.table
;
4862 old_size
= htab
->root
.table
.size
;
4863 old_count
= htab
->root
.table
.count
;
4865 if (htab
->dynstr
!= NULL
)
4867 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4868 if (old_strtab
== NULL
)
4869 goto error_free_vers
;
4872 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4874 struct bfd_hash_entry
*p
;
4875 struct elf_link_hash_entry
*h
;
4877 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4879 h
= (struct elf_link_hash_entry
*) p
;
4880 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4881 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4882 if (h
->root
.type
== bfd_link_hash_warning
)
4884 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4885 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4886 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4888 if (h
->root
.type
== bfd_link_hash_common
)
4890 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4891 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4898 if (extversym
== NULL
)
4900 else if (extversym
+ extsymoff
< extversym_end
)
4901 ever
= extversym
+ extsymoff
;
4904 /* xgettext:c-format */
4905 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4906 abfd
, (long) extsymoff
,
4907 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4908 bfd_set_error (bfd_error_bad_value
);
4909 goto error_free_vers
;
4912 if (!bfd_link_relocatable (info
)
4913 && bfd_get_lto_type (abfd
) == lto_slim_ir_object
)
4916 (_("%pB: plugin needed to handle lto object"), abfd
);
4919 for (isym
= isymbuf
, isymend
= PTR_ADD (isymbuf
, extsymcount
);
4921 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4925 asection
*sec
, *new_sec
;
4928 bool must_copy_name
= false;
4929 struct elf_link_hash_entry
*h
;
4930 struct elf_link_hash_entry
*hi
;
4932 bool size_change_ok
;
4933 bool type_change_ok
;
4939 unsigned int old_alignment
;
4940 unsigned int shindex
;
4946 flags
= BSF_NO_FLAGS
;
4948 value
= isym
->st_value
;
4949 common
= bed
->common_definition (isym
);
4950 if (common
&& info
->inhibit_common_definition
)
4952 /* Treat common symbol as undefined for --no-define-common. */
4953 isym
->st_shndx
= SHN_UNDEF
;
4958 bind
= ELF_ST_BIND (isym
->st_info
);
4962 /* This should be impossible, since ELF requires that all
4963 global symbols follow all local symbols, and that sh_info
4964 point to the first global symbol. Unfortunately, Irix 5
4966 if (elf_bad_symtab (abfd
))
4969 /* If we aren't prepared to handle locals within the globals
4970 then we'll likely segfault on a NULL symbol hash if the
4971 symbol is ever referenced in relocations. */
4972 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4973 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4974 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4975 " (>= sh_info of %lu)"),
4976 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4979 /* Dynamic object relocations are not processed by ld, so
4980 ld won't run into the problem mentioned above. */
4983 bfd_set_error (bfd_error_bad_value
);
4984 goto error_free_vers
;
4987 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4995 case STB_GNU_UNIQUE
:
4996 flags
= BSF_GNU_UNIQUE
;
5000 /* Leave it up to the processor backend. */
5004 if (isym
->st_shndx
== SHN_UNDEF
)
5005 sec
= bfd_und_section_ptr
;
5006 else if (isym
->st_shndx
== SHN_ABS
)
5007 sec
= bfd_abs_section_ptr
;
5008 else if (isym
->st_shndx
== SHN_COMMON
)
5010 sec
= bfd_com_section_ptr
;
5011 /* What ELF calls the size we call the value. What ELF
5012 calls the value we call the alignment. */
5013 value
= isym
->st_size
;
5017 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
5019 sec
= bfd_abs_section_ptr
;
5020 else if (discarded_section (sec
))
5022 /* Symbols from discarded section are undefined. We keep
5024 sec
= bfd_und_section_ptr
;
5026 isym
->st_shndx
= SHN_UNDEF
;
5028 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
5032 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5035 goto error_free_vers
;
5037 if (isym
->st_shndx
== SHN_COMMON
5038 && (abfd
->flags
& BFD_PLUGIN
) != 0)
5040 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
5044 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
5046 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
5048 goto error_free_vers
;
5052 else if (isym
->st_shndx
== SHN_COMMON
5053 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
5054 && !bfd_link_relocatable (info
))
5056 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
5060 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
5061 | SEC_LINKER_CREATED
);
5062 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
5064 goto error_free_vers
;
5068 else if (bed
->elf_add_symbol_hook
)
5070 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
5072 goto error_free_vers
;
5074 /* The hook function sets the name to NULL if this symbol
5075 should be skipped for some reason. */
5080 /* Sanity check that all possibilities were handled. */
5084 /* Silently discard TLS symbols from --just-syms. There's
5085 no way to combine a static TLS block with a new TLS block
5086 for this executable. */
5087 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
5088 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
5091 if (bfd_is_und_section (sec
)
5092 || bfd_is_com_section (sec
))
5097 size_change_ok
= false;
5098 type_change_ok
= bed
->type_change_ok
;
5105 if (is_elf_hash_table (&htab
->root
))
5107 Elf_Internal_Versym iver
;
5108 unsigned int vernum
= 0;
5113 if (info
->default_imported_symver
)
5114 /* Use the default symbol version created earlier. */
5115 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
5119 else if (ever
>= extversym_end
)
5121 /* xgettext:c-format */
5122 _bfd_error_handler (_("%pB: not enough version information"),
5124 bfd_set_error (bfd_error_bad_value
);
5125 goto error_free_vers
;
5128 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
5130 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
5132 /* If this is a hidden symbol, or if it is not version
5133 1, we append the version name to the symbol name.
5134 However, we do not modify a non-hidden absolute symbol
5135 if it is not a function, because it might be the version
5136 symbol itself. FIXME: What if it isn't? */
5137 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
5139 && (!bfd_is_abs_section (sec
)
5140 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
5143 size_t namelen
, verlen
, newlen
;
5146 if (isym
->st_shndx
!= SHN_UNDEF
)
5148 if (vernum
> elf_tdata (abfd
)->cverdefs
)
5150 else if (vernum
> 1)
5152 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
5159 /* xgettext:c-format */
5160 (_("%pB: %s: invalid version %u (max %d)"),
5162 elf_tdata (abfd
)->cverdefs
);
5163 bfd_set_error (bfd_error_bad_value
);
5164 goto error_free_vers
;
5169 /* We cannot simply test for the number of
5170 entries in the VERNEED section since the
5171 numbers for the needed versions do not start
5173 Elf_Internal_Verneed
*t
;
5176 for (t
= elf_tdata (abfd
)->verref
;
5180 Elf_Internal_Vernaux
*a
;
5182 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5184 if (a
->vna_other
== vernum
)
5186 verstr
= a
->vna_nodename
;
5196 /* xgettext:c-format */
5197 (_("%pB: %s: invalid needed version %d"),
5198 abfd
, name
, vernum
);
5199 bfd_set_error (bfd_error_bad_value
);
5200 goto error_free_vers
;
5204 namelen
= strlen (name
);
5205 verlen
= strlen (verstr
);
5206 newlen
= namelen
+ verlen
+ 2;
5207 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5208 && isym
->st_shndx
!= SHN_UNDEF
)
5211 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
5212 if (newname
== NULL
)
5213 goto error_free_vers
;
5214 memcpy (newname
, name
, namelen
);
5215 p
= newname
+ namelen
;
5217 /* If this is a defined non-hidden version symbol,
5218 we add another @ to the name. This indicates the
5219 default version of the symbol. */
5220 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5221 && isym
->st_shndx
!= SHN_UNDEF
)
5223 memcpy (p
, verstr
, verlen
+ 1);
5226 /* Since bfd_hash_alloc is used for "name", the string
5227 must be copied if added to first_hash. The string
5228 memory can be freed when an --as-needed library is
5230 must_copy_name
= true;
5233 /* If this symbol has default visibility and the user has
5234 requested we not re-export it, then mark it as hidden. */
5235 if (!bfd_is_und_section (sec
)
5238 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
5239 isym
->st_other
= (STV_HIDDEN
5240 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
5242 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
5243 sym_hash
, &old_bfd
, &old_weak
,
5244 &old_alignment
, &skip
, &override
,
5245 &type_change_ok
, &size_change_ok
,
5247 goto error_free_vers
;
5253 while (h
->root
.type
== bfd_link_hash_indirect
5254 || h
->root
.type
== bfd_link_hash_warning
)
5255 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5257 /* Override a definition only if the new symbol matches the
5259 if (override
&& matched
)
5262 if (htab
->first_hash
!= NULL
5263 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5264 && h
->root
.non_ir_ref_regular
)
5266 /* When reloading --as-needed shared objects for new
5267 symbols added from IR inputs, if this shared object
5268 has the first definition, use it. */
5269 struct elf_link_first_hash_entry
*e
5270 = ((struct elf_link_first_hash_entry
*)
5271 bfd_hash_lookup (htab
->first_hash
, name
, false,
5273 if (e
!= NULL
&& e
->abfd
== abfd
)
5278 if (h
->versioned
!= unversioned
5279 && elf_tdata (abfd
)->verdef
!= NULL
5282 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
5285 if (! (_bfd_generic_link_add_one_symbol
5286 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
5287 NULL
, false, bed
->collect
,
5288 (struct bfd_link_hash_entry
**) sym_hash
)))
5289 goto error_free_vers
;
5292 /* We need to make sure that indirect symbol dynamic flags are
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
;
5301 /* Setting the index to -3 tells elf_link_output_extsym that
5302 this symbol is defined in a discarded section. */
5303 if (discarded
&& is_elf_hash_table (&htab
->root
))
5306 new_weak
= (flags
& BSF_WEAK
) != 0;
5310 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
5311 && is_elf_hash_table (&htab
->root
)
5312 && h
->u
.alias
== NULL
)
5314 /* Keep a list of all weak defined non function symbols from
5315 a dynamic object, using the alias field. Later in this
5316 function we will set the alias field to the correct
5317 value. We only put non-function symbols from dynamic
5318 objects on this list, because that happens to be the only
5319 time we need to know the normal symbol corresponding to a
5320 weak symbol, and the information is time consuming to
5321 figure out. If the alias field is not already NULL,
5322 then this symbol was already defined by some previous
5323 dynamic object, and we will be using that previous
5324 definition anyhow. */
5330 /* Set the alignment of a common symbol. */
5331 if ((common
|| bfd_is_com_section (sec
))
5332 && h
->root
.type
== bfd_link_hash_common
)
5337 align
= bfd_log2 (isym
->st_value
);
5340 /* The new symbol is a common symbol in a shared object.
5341 We need to get the alignment from the section. */
5342 align
= new_sec
->alignment_power
;
5344 if (align
> old_alignment
)
5345 h
->root
.u
.c
.p
->alignment_power
= align
;
5347 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5350 if (is_elf_hash_table (&htab
->root
))
5352 /* Set a flag in the hash table entry indicating the type of
5353 reference or definition we just found. A dynamic symbol
5354 is one which is referenced or defined by both a regular
5355 object and a shared object. */
5356 bool dynsym
= false;
5358 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5359 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5361 /* Except for this flag to track nonweak references. */
5363 && bind
!= STB_WEAK
)
5364 h
->ref_ir_nonweak
= 1;
5371 if (bind
!= STB_WEAK
)
5372 h
->ref_regular_nonweak
= 1;
5389 hi
->ref_dynamic
= 1;
5394 hi
->def_dynamic
= 1;
5398 /* If an indirect symbol has been forced local, don't
5399 make the real symbol dynamic. */
5400 if (h
!= hi
&& hi
->forced_local
)
5404 if (bfd_link_dll (info
)
5414 && weakdef (h
)->dynindx
!= -1))
5418 /* Check to see if we need to add an indirect symbol for
5419 the default name. */
5421 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5423 && hi
->versioned
== versioned_hidden
))
5424 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5425 sec
, value
, &old_bfd
, &dynsym
))
5426 goto error_free_vers
;
5428 /* Check the alignment when a common symbol is involved. This
5429 can change when a common symbol is overridden by a normal
5430 definition or a common symbol is ignored due to the old
5431 normal definition. We need to make sure the maximum
5432 alignment is maintained. */
5433 if ((old_alignment
|| common
)
5434 && h
->root
.type
!= bfd_link_hash_common
)
5436 unsigned int common_align
;
5437 unsigned int normal_align
;
5438 unsigned int symbol_align
;
5442 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5443 || h
->root
.type
== bfd_link_hash_defweak
);
5445 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5446 if (h
->root
.u
.def
.section
->owner
!= NULL
5447 && (h
->root
.u
.def
.section
->owner
->flags
5448 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5450 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5451 if (normal_align
> symbol_align
)
5452 normal_align
= symbol_align
;
5455 normal_align
= symbol_align
;
5459 common_align
= old_alignment
;
5460 common_bfd
= old_bfd
;
5465 common_align
= bfd_log2 (isym
->st_value
);
5467 normal_bfd
= old_bfd
;
5470 if (normal_align
< common_align
)
5472 /* PR binutils/2735 */
5473 if (normal_bfd
== NULL
)
5475 /* xgettext:c-format */
5476 (_("warning: alignment %u of common symbol `%s' in %pB is"
5477 " greater than the alignment (%u) of its section %pA"),
5478 1 << common_align
, name
, common_bfd
,
5479 1 << normal_align
, h
->root
.u
.def
.section
);
5482 /* xgettext:c-format */
5483 (_("warning: alignment %u of normal symbol `%s' in %pB"
5484 " is smaller than %u used by the common definition in %pB"),
5485 1 << normal_align
, name
, normal_bfd
,
5486 1 << common_align
, common_bfd
);
5488 /* PR 30499: make sure that users understand that this warning is serious. */
5490 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5494 /* Remember the symbol size if it isn't undefined. */
5495 if (isym
->st_size
!= 0
5496 && isym
->st_shndx
!= SHN_UNDEF
5497 && (definition
|| h
->size
== 0))
5500 && h
->size
!= isym
->st_size
5501 && ! size_change_ok
)
5504 /* xgettext:c-format */
5505 (_("warning: size of symbol `%s' changed"
5506 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5507 name
, (uint64_t) h
->size
, old_bfd
,
5508 (uint64_t) isym
->st_size
, abfd
);
5510 /* PR 30499: make sure that users understand that this warning is serious. */
5512 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5515 h
->size
= isym
->st_size
;
5518 /* If this is a common symbol, then we always want H->SIZE
5519 to be the size of the common symbol. The code just above
5520 won't fix the size if a common symbol becomes larger. We
5521 don't warn about a size change here, because that is
5522 covered by --warn-common. Allow changes between different
5524 if (h
->root
.type
== bfd_link_hash_common
)
5525 h
->size
= h
->root
.u
.c
.size
;
5527 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5528 && ((definition
&& !new_weak
)
5529 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5530 || h
->type
== STT_NOTYPE
))
5532 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5534 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5536 if (type
== STT_GNU_IFUNC
5537 && (abfd
->flags
& DYNAMIC
) != 0)
5540 if (h
->type
!= type
)
5542 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5543 /* xgettext:c-format */
5545 (_("warning: type of symbol `%s' changed"
5546 " from %d to %d in %pB"),
5547 name
, h
->type
, type
, abfd
);
5553 /* Merge st_other field. */
5554 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5555 definition
, dynamic
);
5557 /* We don't want to make debug symbol dynamic. */
5559 && (sec
->flags
& SEC_DEBUGGING
)
5560 && !bfd_link_relocatable (info
))
5563 /* Nor should we make plugin symbols dynamic. */
5564 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5569 h
->target_internal
= isym
->st_target_internal
;
5570 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5573 /* Don't add indirect symbols for .symver x, x@FOO aliases
5574 in IR. Since all data or text symbols in IR have the
5575 same type, value and section, we can't tell if a symbol
5576 is an alias of another symbol by their types, values and
5580 && (abfd
->flags
& BFD_PLUGIN
) == 0)
5582 char *p
= strchr (name
, ELF_VER_CHR
);
5583 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5585 /* Queue non-default versions so that .symver x, x@FOO
5586 aliases can be checked. */
5589 size_t amt
= ((isymend
- isym
+ 1)
5590 * sizeof (struct elf_link_hash_entry
*));
5592 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5594 goto error_free_vers
;
5596 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5600 if (dynsym
&& h
->dynindx
== -1)
5602 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5603 goto error_free_vers
;
5605 && weakdef (h
)->dynindx
== -1)
5607 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5608 goto error_free_vers
;
5611 else if (h
->dynindx
!= -1)
5612 /* If the symbol already has a dynamic index, but
5613 visibility says it should not be visible, turn it into
5615 switch (ELF_ST_VISIBILITY (h
->other
))
5619 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
5627 && h
->root
.type
!= bfd_link_hash_indirect
)
5630 && h
->ref_regular_nonweak
)
5632 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5633 && h
->ref_ir_nonweak
5634 && !info
->lto_all_symbols_read
)
5635 || (h
->ref_dynamic_nonweak
5636 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5637 && !on_needed_list (elf_dt_name (abfd
),
5638 htab
->needed
, NULL
)))
5640 const char *soname
= elf_dt_name (abfd
);
5642 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5643 h
->root
.root
.string
);
5645 /* A symbol from a library loaded via DT_NEEDED of some
5646 other library is referenced by a regular object.
5647 Add a DT_NEEDED entry for it. Issue an error if
5648 --no-add-needed is used and the reference was not
5651 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5654 /* xgettext:c-format */
5655 (_("%pB: undefined reference to symbol '%s'"),
5657 bfd_set_error (bfd_error_missing_dso
);
5658 goto error_free_vers
;
5661 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5662 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5664 /* Create dynamic sections for backends that require
5665 that be done before setup_gnu_properties. */
5666 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5671 && h
->root
.u
.def
.section
->owner
== abfd
)
5672 /* Add this symbol to first hash if this shared
5673 object has the first definition. */
5674 elf_link_add_to_first_hash (abfd
, info
, name
, must_copy_name
);
5679 if (info
->lto_plugin_active
5680 && !bfd_link_relocatable (info
)
5681 && (abfd
->flags
& BFD_PLUGIN
) == 0
5687 if (bed
->s
->arch_size
== 32)
5692 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5693 referenced in regular objects so that linker plugin will get
5694 the correct symbol resolution. */
5696 sym_hash
= elf_sym_hashes (abfd
);
5697 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5699 Elf_Internal_Rela
*internal_relocs
;
5700 Elf_Internal_Rela
*rel
, *relend
;
5702 /* Don't check relocations in excluded sections. */
5703 if ((s
->flags
& SEC_RELOC
) == 0
5704 || s
->reloc_count
== 0
5705 || (s
->flags
& SEC_EXCLUDE
) != 0
5706 || ((info
->strip
== strip_all
5707 || info
->strip
== strip_debugger
)
5708 && (s
->flags
& SEC_DEBUGGING
) != 0))
5711 internal_relocs
= _bfd_elf_link_info_read_relocs
5712 (abfd
, info
, s
, NULL
, NULL
,
5713 _bfd_elf_link_keep_memory (info
));
5714 if (internal_relocs
== NULL
)
5715 goto error_free_vers
;
5717 rel
= internal_relocs
;
5718 relend
= rel
+ s
->reloc_count
;
5719 for ( ; rel
< relend
; rel
++)
5721 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5722 struct elf_link_hash_entry
*h
;
5724 /* Skip local symbols. */
5725 if (r_symndx
< extsymoff
)
5728 h
= sym_hash
[r_symndx
- extsymoff
];
5730 h
->root
.non_ir_ref_regular
= 1;
5733 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5734 free (internal_relocs
);
5743 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5747 /* Restore the symbol table. */
5748 old_ent
= (char *) old_tab
+ tabsize
;
5749 memset (elf_sym_hashes (abfd
), 0,
5750 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5751 htab
->root
.table
.table
= old_table
;
5752 htab
->root
.table
.size
= old_size
;
5753 htab
->root
.table
.count
= old_count
;
5754 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5755 htab
->root
.undefs
= old_undefs
;
5756 htab
->root
.undefs_tail
= old_undefs_tail
;
5757 if (htab
->dynstr
!= NULL
)
5758 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5761 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5763 struct bfd_hash_entry
*p
;
5764 struct elf_link_hash_entry
*h
;
5765 unsigned int non_ir_ref_dynamic
;
5767 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5769 /* Preserve non_ir_ref_dynamic so that this symbol
5770 will be exported when the dynamic lib becomes needed
5771 in the second pass. */
5772 h
= (struct elf_link_hash_entry
*) p
;
5773 if (h
->root
.type
== bfd_link_hash_warning
)
5774 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5775 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5777 h
= (struct elf_link_hash_entry
*) p
;
5778 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5779 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5780 if (h
->root
.type
== bfd_link_hash_warning
)
5782 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5783 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5784 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5786 if (h
->root
.type
== bfd_link_hash_common
)
5788 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5789 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5791 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5795 /* Make a special call to the linker "notice" function to
5796 tell it that symbols added for crefs may need to be removed. */
5797 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5798 goto error_free_vers
;
5801 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5803 free (nondeflt_vers
);
5807 if (old_tab
!= NULL
)
5809 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5810 goto error_free_vers
;
5815 /* Now that all the symbols from this input file are created, if
5816 not performing a relocatable link, handle .symver foo, foo@BAR
5817 such that any relocs against foo become foo@BAR. */
5818 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5822 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5824 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5825 char *shortname
, *p
;
5828 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5830 || (h
->root
.type
!= bfd_link_hash_defined
5831 && h
->root
.type
!= bfd_link_hash_defweak
))
5834 amt
= p
- h
->root
.root
.string
;
5835 shortname
= (char *) bfd_malloc (amt
+ 1);
5837 goto error_free_vers
;
5838 memcpy (shortname
, h
->root
.root
.string
, amt
);
5839 shortname
[amt
] = '\0';
5841 hi
= (struct elf_link_hash_entry
*)
5842 bfd_link_hash_lookup (&htab
->root
, shortname
,
5843 false, false, false);
5845 && hi
->root
.type
== h
->root
.type
5846 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5847 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5849 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
5850 hi
->root
.type
= bfd_link_hash_indirect
;
5851 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5852 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5853 sym_hash
= elf_sym_hashes (abfd
);
5855 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5856 if (sym_hash
[symidx
] == hi
)
5858 sym_hash
[symidx
] = h
;
5864 free (nondeflt_vers
);
5865 nondeflt_vers
= NULL
;
5868 /* Now set the alias field correctly for all the weak defined
5869 symbols we found. The only way to do this is to search all the
5870 symbols. Since we only need the information for non functions in
5871 dynamic objects, that's the only time we actually put anything on
5872 the list WEAKS. We need this information so that if a regular
5873 object refers to a symbol defined weakly in a dynamic object, the
5874 real symbol in the dynamic object is also put in the dynamic
5875 symbols; we also must arrange for both symbols to point to the
5876 same memory location. We could handle the general case of symbol
5877 aliasing, but a general symbol alias can only be generated in
5878 assembler code, handling it correctly would be very time
5879 consuming, and other ELF linkers don't handle general aliasing
5883 struct elf_link_hash_entry
**hpp
;
5884 struct elf_link_hash_entry
**hppend
;
5885 struct elf_link_hash_entry
**sorted_sym_hash
;
5886 struct elf_link_hash_entry
*h
;
5887 size_t sym_count
, amt
;
5889 /* Since we have to search the whole symbol list for each weak
5890 defined symbol, search time for N weak defined symbols will be
5891 O(N^2). Binary search will cut it down to O(NlogN). */
5892 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5893 sorted_sym_hash
= bfd_malloc (amt
);
5894 if (sorted_sym_hash
== NULL
)
5896 sym_hash
= sorted_sym_hash
;
5897 hpp
= elf_sym_hashes (abfd
);
5898 hppend
= hpp
+ extsymcount
;
5900 for (; hpp
< hppend
; hpp
++)
5904 && h
->root
.type
== bfd_link_hash_defined
5905 && !bed
->is_function_type (h
->type
))
5913 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5916 while (weaks
!= NULL
)
5918 struct elf_link_hash_entry
*hlook
;
5921 size_t i
, j
, idx
= 0;
5924 weaks
= hlook
->u
.alias
;
5925 hlook
->u
.alias
= NULL
;
5927 if (hlook
->root
.type
!= bfd_link_hash_defined
5928 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5931 slook
= hlook
->root
.u
.def
.section
;
5932 vlook
= hlook
->root
.u
.def
.value
;
5938 bfd_signed_vma vdiff
;
5940 h
= sorted_sym_hash
[idx
];
5941 vdiff
= vlook
- h
->root
.u
.def
.value
;
5948 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5958 /* We didn't find a value/section match. */
5962 /* With multiple aliases, or when the weak symbol is already
5963 strongly defined, we have multiple matching symbols and
5964 the binary search above may land on any of them. Step
5965 one past the matching symbol(s). */
5968 h
= sorted_sym_hash
[idx
];
5969 if (h
->root
.u
.def
.section
!= slook
5970 || h
->root
.u
.def
.value
!= vlook
)
5974 /* Now look back over the aliases. Since we sorted by size
5975 as well as value and section, we'll choose the one with
5976 the largest size. */
5979 h
= sorted_sym_hash
[idx
];
5981 /* Stop if value or section doesn't match. */
5982 if (h
->root
.u
.def
.section
!= slook
5983 || h
->root
.u
.def
.value
!= vlook
)
5985 else if (h
!= hlook
)
5987 struct elf_link_hash_entry
*t
;
5990 hlook
->is_weakalias
= 1;
5992 if (t
->u
.alias
!= NULL
)
5993 while (t
->u
.alias
!= h
)
5997 /* If the weak definition is in the list of dynamic
5998 symbols, make sure the real definition is put
6000 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
6002 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6005 free (sorted_sym_hash
);
6010 /* If the real definition is in the list of dynamic
6011 symbols, make sure the weak definition is put
6012 there as well. If we don't do this, then the
6013 dynamic loader might not merge the entries for the
6014 real definition and the weak definition. */
6015 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
6017 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
6018 goto err_free_sym_hash
;
6025 free (sorted_sym_hash
);
6028 if (bed
->check_directives
6029 && !(*bed
->check_directives
) (abfd
, info
))
6032 /* If this is a non-traditional link, try to optimize the handling
6033 of the .stab/.stabstr sections. */
6035 && ! info
->traditional_format
6036 && is_elf_hash_table (&htab
->root
)
6037 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
6041 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
6042 if (stabstr
!= NULL
)
6044 bfd_size_type string_offset
= 0;
6047 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
6048 if (startswith (stab
->name
, ".stab")
6049 && (!stab
->name
[5] ||
6050 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
6051 && (stab
->flags
& SEC_MERGE
) == 0
6052 && !bfd_is_abs_section (stab
->output_section
))
6054 struct bfd_elf_section_data
*secdata
;
6056 secdata
= elf_section_data (stab
);
6057 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
6058 stabstr
, &secdata
->sec_info
,
6061 if (secdata
->sec_info
)
6062 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
6067 if (dynamic
&& add_needed
)
6069 /* Add this bfd to the loaded list. */
6070 struct elf_link_loaded_list
*n
;
6072 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
6076 n
->next
= htab
->dyn_loaded
;
6077 htab
->dyn_loaded
= n
;
6079 if (dynamic
&& !add_needed
6080 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
6081 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
6088 free (nondeflt_vers
);
6096 /* Return the linker hash table entry of a symbol that might be
6097 satisfied by an archive symbol. Return -1 on error. */
6099 struct bfd_link_hash_entry
*
6100 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
6101 struct bfd_link_info
*info
,
6104 struct bfd_link_hash_entry
*h
;
6108 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
6112 /* If this is a default version (the name contains @@), look up the
6113 symbol again with only one `@' as well as without the version.
6114 The effect is that references to the symbol with and without the
6115 version will be matched by the default symbol in the archive. */
6117 p
= strchr (name
, ELF_VER_CHR
);
6118 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
6120 /* Add this symbol to first hash if this archive has the first
6122 if (is_elf_hash_table (info
->hash
))
6123 elf_link_add_to_first_hash (abfd
, info
, name
, false);
6127 /* First check with only one `@'. */
6128 len
= strlen (name
);
6129 copy
= (char *) bfd_alloc (abfd
, len
);
6131 return (struct bfd_link_hash_entry
*) -1;
6133 first
= p
- name
+ 1;
6134 memcpy (copy
, name
, first
);
6135 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
6137 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
6140 /* We also need to check references to the symbol without the
6142 copy
[first
- 1] = '\0';
6143 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
6146 bfd_release (abfd
, copy
);
6150 /* Add symbols from an ELF archive file to the linker hash table. We
6151 don't use _bfd_generic_link_add_archive_symbols because we need to
6152 handle versioned symbols.
6154 Fortunately, ELF archive handling is simpler than that done by
6155 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6156 oddities. In ELF, if we find a symbol in the archive map, and the
6157 symbol is currently undefined, we know that we must pull in that
6160 Unfortunately, we do have to make multiple passes over the symbol
6161 table until nothing further is resolved. */
6164 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6167 unsigned char *included
= NULL
;
6171 const struct elf_backend_data
*bed
;
6172 struct bfd_link_hash_entry
* (*archive_symbol_lookup
)
6173 (bfd
*, struct bfd_link_info
*, const char *);
6175 if (! bfd_has_map (abfd
))
6177 /* An empty archive is a special case. */
6178 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
6180 bfd_set_error (bfd_error_no_armap
);
6184 /* Keep track of all symbols we know to be already defined, and all
6185 files we know to be already included. This is to speed up the
6186 second and subsequent passes. */
6187 c
= bfd_ardata (abfd
)->symdef_count
;
6190 amt
= c
* sizeof (*included
);
6191 included
= (unsigned char *) bfd_zmalloc (amt
);
6192 if (included
== NULL
)
6195 symdefs
= bfd_ardata (abfd
)->symdefs
;
6196 bed
= get_elf_backend_data (abfd
);
6197 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
6210 symdefend
= symdef
+ c
;
6211 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
6213 struct bfd_link_hash_entry
*h
;
6215 struct bfd_link_hash_entry
*undefs_tail
;
6220 if (symdef
->file_offset
== last
)
6226 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
6227 if (h
== (struct bfd_link_hash_entry
*) -1)
6233 if (h
->type
== bfd_link_hash_undefined
)
6235 /* If the archive element has already been loaded then one
6236 of the symbols defined by that element might have been
6237 made undefined due to being in a discarded section. */
6238 if (is_elf_hash_table (info
->hash
)
6239 && ((struct elf_link_hash_entry
*) h
)->indx
== -3)
6242 else if (h
->type
== bfd_link_hash_common
)
6244 /* We currently have a common symbol. The archive map contains
6245 a reference to this symbol, so we may want to include it. We
6246 only want to include it however, if this archive element
6247 contains a definition of the symbol, not just another common
6250 Unfortunately some archivers (including GNU ar) will put
6251 declarations of common symbols into their archive maps, as
6252 well as real definitions, so we cannot just go by the archive
6253 map alone. Instead we must read in the element's symbol
6254 table and check that to see what kind of symbol definition
6256 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
6261 if (h
->type
!= bfd_link_hash_undefweak
)
6262 /* Symbol must be defined. Don't check it again. */
6265 if (!is_elf_hash_table (info
->hash
))
6267 struct elf_link_hash_entry
*eh
6268 = (struct elf_link_hash_entry
*) h
;
6269 /* Ignore the archive if the symbol isn't referenced by a
6270 regular object or isn't defined in a shared object. */
6271 if (!eh
->ref_regular
|| !eh
->def_dynamic
)
6273 /* Ignore the dynamic definition if symbol is first
6274 defined in this archive. */
6275 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
6276 if (htab
->first_hash
== NULL
)
6278 struct elf_link_first_hash_entry
*e
6279 = ((struct elf_link_first_hash_entry
*)
6280 bfd_hash_lookup (htab
->first_hash
, symdef
->name
,
6282 if (e
== NULL
|| e
->abfd
!= abfd
)
6286 /* We need to include this archive member. */
6287 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
,
6289 if (element
== NULL
)
6292 if (! bfd_check_format (element
, bfd_object
))
6295 undefs_tail
= info
->hash
->undefs_tail
;
6297 if (!(*info
->callbacks
6298 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
6300 if (!bfd_link_add_symbols (element
, info
))
6303 /* If there are any new undefined symbols, we need to make
6304 another pass through the archive in order to see whether
6305 they can be defined. FIXME: This isn't perfect, because
6306 common symbols wind up on undefs_tail and because an
6307 undefined symbol which is defined later on in this pass
6308 does not require another pass. This isn't a bug, but it
6309 does make the code less efficient than it could be. */
6310 if (undefs_tail
!= info
->hash
->undefs_tail
)
6313 /* Look backward to mark all symbols from this object file
6314 which we have already seen in this pass. */
6318 included
[mark
] = true;
6323 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
6325 /* We mark subsequent symbols from this object file as we go
6326 on through the loop. */
6327 last
= symdef
->file_offset
;
6340 /* Given an ELF BFD, add symbols to the global hash table as
6344 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6346 switch (bfd_get_format (abfd
))
6349 return elf_link_add_object_symbols (abfd
, info
);
6351 return elf_link_add_archive_symbols (abfd
, info
);
6353 bfd_set_error (bfd_error_wrong_format
);
6358 struct hash_codes_info
6360 unsigned long *hashcodes
;
6364 /* This function will be called though elf_link_hash_traverse to store
6365 all hash value of the exported symbols in an array. */
6368 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6370 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
6375 /* Ignore indirect symbols. These are added by the versioning code. */
6376 if (h
->dynindx
== -1)
6379 name
= h
->root
.root
.string
;
6380 if (h
->versioned
>= versioned
)
6382 char *p
= strchr (name
, ELF_VER_CHR
);
6385 alc
= (char *) bfd_malloc (p
- name
+ 1);
6391 memcpy (alc
, name
, p
- name
);
6392 alc
[p
- name
] = '\0';
6397 /* Compute the hash value. */
6398 ha
= bfd_elf_hash (name
);
6400 /* Store the found hash value in the array given as the argument. */
6401 *(inf
->hashcodes
)++ = ha
;
6403 /* And store it in the struct so that we can put it in the hash table
6405 h
->u
.elf_hash_value
= ha
;
6411 struct collect_gnu_hash_codes
6414 const struct elf_backend_data
*bed
;
6415 unsigned long int nsyms
;
6416 unsigned long int maskbits
;
6417 unsigned long int *hashcodes
;
6418 unsigned long int *hashval
;
6419 unsigned long int *indx
;
6420 unsigned long int *counts
;
6424 long int min_dynindx
;
6425 unsigned long int bucketcount
;
6426 unsigned long int symindx
;
6427 long int local_indx
;
6428 long int shift1
, shift2
;
6429 unsigned long int mask
;
6433 /* This function will be called though elf_link_hash_traverse to store
6434 all hash value of the exported symbols in an array. */
6437 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6439 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6444 /* Ignore indirect symbols. These are added by the versioning code. */
6445 if (h
->dynindx
== -1)
6448 /* Ignore also local symbols and undefined symbols. */
6449 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6452 name
= h
->root
.root
.string
;
6453 if (h
->versioned
>= versioned
)
6455 char *p
= strchr (name
, ELF_VER_CHR
);
6458 alc
= (char *) bfd_malloc (p
- name
+ 1);
6464 memcpy (alc
, name
, p
- name
);
6465 alc
[p
- name
] = '\0';
6470 /* Compute the hash value. */
6471 ha
= bfd_elf_gnu_hash (name
);
6473 /* Store the found hash value in the array for compute_bucket_count,
6474 and also for .dynsym reordering purposes. */
6475 s
->hashcodes
[s
->nsyms
] = ha
;
6476 s
->hashval
[h
->dynindx
] = ha
;
6478 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6479 s
->min_dynindx
= h
->dynindx
;
6485 /* This function will be called though elf_link_hash_traverse to do
6486 final dynamic symbol renumbering in case of .gnu.hash.
6487 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6488 to the translation table. */
6491 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6493 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6494 unsigned long int bucket
;
6495 unsigned long int val
;
6497 /* Ignore indirect symbols. */
6498 if (h
->dynindx
== -1)
6501 /* Ignore also local symbols and undefined symbols. */
6502 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6504 if (h
->dynindx
>= s
->min_dynindx
)
6506 if (s
->bed
->record_xhash_symbol
!= NULL
)
6508 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6512 h
->dynindx
= s
->local_indx
++;
6517 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6518 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6519 & ((s
->maskbits
>> s
->shift1
) - 1);
6520 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6522 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6523 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6524 if (s
->counts
[bucket
] == 1)
6525 /* Last element terminates the chain. */
6527 bfd_put_32 (s
->output_bfd
, val
,
6528 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6529 --s
->counts
[bucket
];
6530 if (s
->bed
->record_xhash_symbol
!= NULL
)
6532 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6534 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6537 h
->dynindx
= s
->indx
[bucket
]++;
6541 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6544 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6546 return !(h
->forced_local
6547 || h
->root
.type
== bfd_link_hash_undefined
6548 || h
->root
.type
== bfd_link_hash_undefweak
6549 || ((h
->root
.type
== bfd_link_hash_defined
6550 || h
->root
.type
== bfd_link_hash_defweak
)
6551 && h
->root
.u
.def
.section
->output_section
== NULL
));
6554 /* Array used to determine the number of hash table buckets to use
6555 based on the number of symbols there are. If there are fewer than
6556 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6557 fewer than 37 we use 17 buckets, and so forth. We never use more
6558 than 32771 buckets. */
6560 static const size_t elf_buckets
[] =
6562 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6566 /* Compute bucket count for hashing table. We do not use a static set
6567 of possible tables sizes anymore. Instead we determine for all
6568 possible reasonable sizes of the table the outcome (i.e., the
6569 number of collisions etc) and choose the best solution. The
6570 weighting functions are not too simple to allow the table to grow
6571 without bounds. Instead one of the weighting factors is the size.
6572 Therefore the result is always a good payoff between few collisions
6573 (= short chain lengths) and table size. */
6575 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6576 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6577 unsigned long int nsyms
,
6580 size_t best_size
= 0;
6581 unsigned long int i
;
6587 uint64_t best_chlen
= ~((uint64_t) 0);
6588 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6589 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6590 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6591 unsigned long int *counts
;
6593 unsigned int no_improvement_count
= 0;
6595 /* Possible optimization parameters: if we have NSYMS symbols we say
6596 that the hashing table must at least have NSYMS/4 and at most
6598 minsize
= nsyms
/ 4;
6601 best_size
= maxsize
= nsyms
* 2;
6606 if ((best_size
& 31) == 0)
6610 /* Create array where we count the collisions in. We must use bfd_malloc
6611 since the size could be large. */
6613 amt
*= sizeof (unsigned long int);
6614 counts
= (unsigned long int *) bfd_malloc (amt
);
6618 /* Compute the "optimal" size for the hash table. The criteria is a
6619 minimal chain length. The minor criteria is (of course) the size
6621 for (i
= minsize
; i
< maxsize
; ++i
)
6623 /* Walk through the array of hashcodes and count the collisions. */
6625 unsigned long int j
;
6626 unsigned long int fact
;
6628 if (gnu_hash
&& (i
& 31) == 0)
6631 memset (counts
, '\0', i
* sizeof (unsigned long int));
6633 /* Determine how often each hash bucket is used. */
6634 for (j
= 0; j
< nsyms
; ++j
)
6635 ++counts
[hashcodes
[j
] % i
];
6637 /* For the weight function we need some information about the
6638 pagesize on the target. This is information need not be 100%
6639 accurate. Since this information is not available (so far) we
6640 define it here to a reasonable default value. If it is crucial
6641 to have a better value some day simply define this value. */
6642 # ifndef BFD_TARGET_PAGESIZE
6643 # define BFD_TARGET_PAGESIZE (4096)
6646 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6648 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6651 /* Variant 1: optimize for short chains. We add the squares
6652 of all the chain lengths (which favors many small chain
6653 over a few long chains). */
6654 for (j
= 0; j
< i
; ++j
)
6655 max
+= counts
[j
] * counts
[j
];
6657 /* This adds penalties for the overall size of the table. */
6658 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6661 /* Variant 2: Optimize a lot more for small table. Here we
6662 also add squares of the size but we also add penalties for
6663 empty slots (the +1 term). */
6664 for (j
= 0; j
< i
; ++j
)
6665 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6667 /* The overall size of the table is considered, but not as
6668 strong as in variant 1, where it is squared. */
6669 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6673 /* Compare with current best results. */
6674 if (max
< best_chlen
)
6678 no_improvement_count
= 0;
6680 /* PR 11843: Avoid futile long searches for the best bucket size
6681 when there are a large number of symbols. */
6682 else if (++no_improvement_count
== 100)
6690 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6692 best_size
= elf_buckets
[i
];
6693 if (nsyms
< elf_buckets
[i
+ 1])
6696 if (gnu_hash
&& best_size
< 2)
6703 /* Size any SHT_GROUP section for ld -r. */
6706 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6711 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6712 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6713 && (s
= ibfd
->sections
) != NULL
6714 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6715 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6720 /* Set a default stack segment size. The value in INFO wins. If it
6721 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6722 undefined it is initialized. */
6725 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6726 struct bfd_link_info
*info
,
6727 const char *legacy_symbol
,
6728 bfd_vma default_size
)
6730 struct elf_link_hash_entry
*h
= NULL
;
6732 /* Look for legacy symbol. */
6734 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6735 false, false, false);
6736 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6737 || h
->root
.type
== bfd_link_hash_defweak
)
6739 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6741 /* The symbol has no type if specified on the command line. */
6742 h
->type
= STT_OBJECT
;
6743 if (info
->stacksize
)
6744 /* xgettext:c-format */
6745 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6746 output_bfd
, legacy_symbol
);
6747 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6748 /* xgettext:c-format */
6749 _bfd_error_handler (_("%pB: %s not absolute"),
6750 output_bfd
, legacy_symbol
);
6752 info
->stacksize
= h
->root
.u
.def
.value
;
6755 if (!info
->stacksize
)
6756 /* If the user didn't set a size, or explicitly inhibit the
6757 size, set it now. */
6758 info
->stacksize
= default_size
;
6760 /* Provide the legacy symbol, if it is referenced. */
6761 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6762 || h
->root
.type
== bfd_link_hash_undefweak
))
6764 struct bfd_link_hash_entry
*bh
= NULL
;
6766 if (!(_bfd_generic_link_add_one_symbol
6767 (info
, output_bfd
, legacy_symbol
,
6768 BSF_GLOBAL
, bfd_abs_section_ptr
,
6769 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6770 NULL
, false, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6773 h
= (struct elf_link_hash_entry
*) bh
;
6775 h
->type
= STT_OBJECT
;
6781 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6783 struct elf_gc_sweep_symbol_info
6785 struct bfd_link_info
*info
;
6786 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6791 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6794 && (((h
->root
.type
== bfd_link_hash_defined
6795 || h
->root
.type
== bfd_link_hash_defweak
)
6796 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6797 && h
->root
.u
.def
.section
->gc_mark
))
6798 || h
->root
.type
== bfd_link_hash_undefined
6799 || h
->root
.type
== bfd_link_hash_undefweak
))
6801 struct elf_gc_sweep_symbol_info
*inf
;
6803 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6804 (*inf
->hide_symbol
) (inf
->info
, h
, true);
6807 h
->ref_regular_nonweak
= 0;
6813 /* Set up the sizes and contents of the ELF dynamic sections. This is
6814 called by the ELF linker emulation before_allocation routine. We
6815 must set the sizes of the sections before the linker sets the
6816 addresses of the various sections. */
6819 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6822 const char *filter_shlib
,
6824 const char *depaudit
,
6825 const char * const *auxiliary_filters
,
6826 struct bfd_link_info
*info
,
6827 asection
**sinterpptr
)
6830 const struct elf_backend_data
*bed
;
6834 if (!is_elf_hash_table (info
->hash
))
6837 /* Any syms created from now on start with -1 in
6838 got.refcount/offset and plt.refcount/offset. */
6839 elf_hash_table (info
)->init_got_refcount
6840 = elf_hash_table (info
)->init_got_offset
;
6841 elf_hash_table (info
)->init_plt_refcount
6842 = elf_hash_table (info
)->init_plt_offset
;
6844 bed
= get_elf_backend_data (output_bfd
);
6846 /* The backend may have to create some sections regardless of whether
6847 we're dynamic or not. */
6848 if (bed
->elf_backend_early_size_sections
6849 && !bed
->elf_backend_early_size_sections (output_bfd
, info
))
6852 dynobj
= elf_hash_table (info
)->dynobj
;
6854 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6856 struct bfd_elf_version_tree
*verdefs
;
6857 struct elf_info_failed asvinfo
;
6858 struct bfd_elf_version_tree
*t
;
6859 struct bfd_elf_version_expr
*d
;
6863 /* If we are supposed to export all symbols into the dynamic symbol
6864 table (this is not the normal case), then do so. */
6865 if (info
->export_dynamic
6866 || (bfd_link_executable (info
) && info
->dynamic
))
6868 struct elf_info_failed eif
;
6872 elf_link_hash_traverse (elf_hash_table (info
),
6873 _bfd_elf_export_symbol
,
6881 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6883 if (soname_indx
== (size_t) -1
6884 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6888 soname_indx
= (size_t) -1;
6890 /* Make all global versions with definition. */
6891 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6892 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6893 if (!d
->symver
&& d
->literal
)
6895 const char *verstr
, *name
;
6896 size_t namelen
, verlen
, newlen
;
6897 char *newname
, *p
, leading_char
;
6898 struct elf_link_hash_entry
*newh
;
6900 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6902 namelen
= strlen (name
) + (leading_char
!= '\0');
6904 verlen
= strlen (verstr
);
6905 newlen
= namelen
+ verlen
+ 3;
6907 newname
= (char *) bfd_malloc (newlen
);
6908 if (newname
== NULL
)
6910 newname
[0] = leading_char
;
6911 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6913 /* Check the hidden versioned definition. */
6914 p
= newname
+ namelen
;
6916 memcpy (p
, verstr
, verlen
+ 1);
6917 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6918 newname
, false, false,
6921 || (newh
->root
.type
!= bfd_link_hash_defined
6922 && newh
->root
.type
!= bfd_link_hash_defweak
))
6924 /* Check the default versioned definition. */
6926 memcpy (p
, verstr
, verlen
+ 1);
6927 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6928 newname
, false, false,
6933 /* Mark this version if there is a definition and it is
6934 not defined in a shared object. */
6936 && !newh
->def_dynamic
6937 && (newh
->root
.type
== bfd_link_hash_defined
6938 || newh
->root
.type
== bfd_link_hash_defweak
))
6942 /* Attach all the symbols to their version information. */
6943 asvinfo
.info
= info
;
6944 asvinfo
.failed
= false;
6946 elf_link_hash_traverse (elf_hash_table (info
),
6947 _bfd_elf_link_assign_sym_version
,
6952 if (!info
->allow_undefined_version
)
6954 /* Check if all global versions have a definition. */
6955 bool all_defined
= true;
6956 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6957 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6958 if (d
->literal
&& !d
->symver
&& !d
->script
)
6961 (_("%s: undefined version: %s"),
6962 d
->pattern
, t
->name
);
6963 all_defined
= false;
6968 bfd_set_error (bfd_error_bad_value
);
6973 /* Set up the version definition section. */
6974 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6975 BFD_ASSERT (s
!= NULL
);
6977 /* We may have created additional version definitions if we are
6978 just linking a regular application. */
6979 verdefs
= info
->version_info
;
6981 /* Skip anonymous version tag. */
6982 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6983 verdefs
= verdefs
->next
;
6985 if (verdefs
== NULL
&& !info
->create_default_symver
)
6986 s
->flags
|= SEC_EXCLUDE
;
6992 Elf_Internal_Verdef def
;
6993 Elf_Internal_Verdaux defaux
;
6994 struct bfd_link_hash_entry
*bh
;
6995 struct elf_link_hash_entry
*h
;
7001 /* Make space for the base version. */
7002 size
+= sizeof (Elf_External_Verdef
);
7003 size
+= sizeof (Elf_External_Verdaux
);
7006 /* Make space for the default version. */
7007 if (info
->create_default_symver
)
7009 size
+= sizeof (Elf_External_Verdef
);
7013 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
7015 struct bfd_elf_version_deps
*n
;
7017 /* Don't emit base version twice. */
7021 size
+= sizeof (Elf_External_Verdef
);
7022 size
+= sizeof (Elf_External_Verdaux
);
7025 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
7026 size
+= sizeof (Elf_External_Verdaux
);
7030 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7031 if (s
->contents
== NULL
&& s
->size
!= 0)
7034 /* Fill in the version definition section. */
7038 def
.vd_version
= VER_DEF_CURRENT
;
7039 def
.vd_flags
= VER_FLG_BASE
;
7042 if (info
->create_default_symver
)
7044 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
7045 def
.vd_next
= sizeof (Elf_External_Verdef
);
7049 def
.vd_aux
= sizeof (Elf_External_Verdef
);
7050 def
.vd_next
= (sizeof (Elf_External_Verdef
)
7051 + sizeof (Elf_External_Verdaux
));
7054 if (soname_indx
!= (size_t) -1)
7056 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
7058 def
.vd_hash
= bfd_elf_hash (soname
);
7059 defaux
.vda_name
= soname_indx
;
7066 name
= lbasename (bfd_get_filename (output_bfd
));
7067 def
.vd_hash
= bfd_elf_hash (name
);
7068 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7070 if (indx
== (size_t) -1)
7072 defaux
.vda_name
= indx
;
7074 defaux
.vda_next
= 0;
7076 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
7077 (Elf_External_Verdef
*) p
);
7078 p
+= sizeof (Elf_External_Verdef
);
7079 if (info
->create_default_symver
)
7081 /* Add a symbol representing this version. */
7083 if (! (_bfd_generic_link_add_one_symbol
7084 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
7086 get_elf_backend_data (dynobj
)->collect
, &bh
)))
7088 h
= (struct elf_link_hash_entry
*) bh
;
7091 h
->type
= STT_OBJECT
;
7092 h
->verinfo
.vertree
= NULL
;
7094 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
7097 /* Create a duplicate of the base version with the same
7098 aux block, but different flags. */
7101 def
.vd_aux
= sizeof (Elf_External_Verdef
);
7103 def
.vd_next
= (sizeof (Elf_External_Verdef
)
7104 + sizeof (Elf_External_Verdaux
));
7107 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
7108 (Elf_External_Verdef
*) p
);
7109 p
+= sizeof (Elf_External_Verdef
);
7111 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7112 (Elf_External_Verdaux
*) p
);
7113 p
+= sizeof (Elf_External_Verdaux
);
7115 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
7118 struct bfd_elf_version_deps
*n
;
7120 /* Don't emit the base version twice. */
7125 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
7128 /* Add a symbol representing this version. */
7130 if (! (_bfd_generic_link_add_one_symbol
7131 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
7133 get_elf_backend_data (dynobj
)->collect
, &bh
)))
7135 h
= (struct elf_link_hash_entry
*) bh
;
7138 h
->type
= STT_OBJECT
;
7139 h
->verinfo
.vertree
= t
;
7141 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
7144 def
.vd_version
= VER_DEF_CURRENT
;
7146 if (t
->globals
.list
== NULL
7147 && t
->locals
.list
== NULL
7149 def
.vd_flags
|= VER_FLG_WEAK
;
7150 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
7151 def
.vd_cnt
= cdeps
+ 1;
7152 def
.vd_hash
= bfd_elf_hash (t
->name
);
7153 def
.vd_aux
= sizeof (Elf_External_Verdef
);
7156 /* If a basever node is next, it *must* be the last node in
7157 the chain, otherwise Verdef construction breaks. */
7158 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
7159 BFD_ASSERT (t
->next
->next
== NULL
);
7161 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
7162 def
.vd_next
= (sizeof (Elf_External_Verdef
)
7163 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
7165 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
7166 (Elf_External_Verdef
*) p
);
7167 p
+= sizeof (Elf_External_Verdef
);
7169 defaux
.vda_name
= h
->dynstr_index
;
7170 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
7172 defaux
.vda_next
= 0;
7173 if (t
->deps
!= NULL
)
7174 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
7175 t
->name_indx
= defaux
.vda_name
;
7177 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7178 (Elf_External_Verdaux
*) p
);
7179 p
+= sizeof (Elf_External_Verdaux
);
7181 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
7183 if (n
->version_needed
== NULL
)
7185 /* This can happen if there was an error in the
7187 defaux
.vda_name
= 0;
7191 defaux
.vda_name
= n
->version_needed
->name_indx
;
7192 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
7195 if (n
->next
== NULL
)
7196 defaux
.vda_next
= 0;
7198 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
7200 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7201 (Elf_External_Verdaux
*) p
);
7202 p
+= sizeof (Elf_External_Verdaux
);
7206 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
7210 if (info
->gc_sections
&& bed
->can_gc_sections
)
7212 struct elf_gc_sweep_symbol_info sweep_info
;
7214 /* Remove the symbols that were in the swept sections from the
7215 dynamic symbol table. */
7216 sweep_info
.info
= info
;
7217 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
7218 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
7222 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7225 struct elf_find_verdep_info sinfo
;
7227 /* Work out the size of the version reference section. */
7229 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
7230 BFD_ASSERT (s
!= NULL
);
7233 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
7234 if (sinfo
.vers
== 0)
7236 sinfo
.failed
= false;
7238 elf_link_hash_traverse (elf_hash_table (info
),
7239 _bfd_elf_link_find_version_dependencies
,
7244 bed
->elf_backend_add_glibc_version_dependency (&sinfo
);
7248 if (elf_tdata (output_bfd
)->verref
== NULL
)
7249 s
->flags
|= SEC_EXCLUDE
;
7252 Elf_Internal_Verneed
*vn
;
7257 /* Build the version dependency section. */
7260 for (vn
= elf_tdata (output_bfd
)->verref
;
7262 vn
= vn
->vn_nextref
)
7264 Elf_Internal_Vernaux
*a
;
7266 size
+= sizeof (Elf_External_Verneed
);
7268 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7269 size
+= sizeof (Elf_External_Vernaux
);
7273 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7274 if (s
->contents
== NULL
)
7278 for (vn
= elf_tdata (output_bfd
)->verref
;
7280 vn
= vn
->vn_nextref
)
7283 Elf_Internal_Vernaux
*a
;
7287 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7290 vn
->vn_version
= VER_NEED_CURRENT
;
7292 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7293 elf_dt_name (vn
->vn_bfd
) != NULL
7294 ? elf_dt_name (vn
->vn_bfd
)
7295 : lbasename (bfd_get_filename
7298 if (indx
== (size_t) -1)
7301 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
7302 if (vn
->vn_nextref
== NULL
)
7305 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
7306 + caux
* sizeof (Elf_External_Vernaux
));
7308 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
7309 (Elf_External_Verneed
*) p
);
7310 p
+= sizeof (Elf_External_Verneed
);
7312 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7314 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
7315 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7316 a
->vna_nodename
, false);
7317 if (indx
== (size_t) -1)
7320 if (a
->vna_nextptr
== NULL
)
7323 a
->vna_next
= sizeof (Elf_External_Vernaux
);
7325 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
7326 (Elf_External_Vernaux
*) p
);
7327 p
+= sizeof (Elf_External_Vernaux
);
7331 elf_tdata (output_bfd
)->cverrefs
= crefs
;
7335 if (bfd_link_relocatable (info
)
7336 && !_bfd_elf_size_group_sections (info
))
7339 /* Determine any GNU_STACK segment requirements, after the backend
7340 has had a chance to set a default segment size. */
7341 if (info
->execstack
)
7343 /* If the user has explicitly requested warnings, then generate one even
7344 though the choice is the result of another command line option. */
7345 if (info
->warn_execstack
== 1)
7347 if (info
->error_execstack
)
7351 error: creating an executable stack because of -z execstack command line option"));
7357 warning: enabling an executable stack because of -z execstack command line option"));
7360 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
7362 else if (info
->noexecstack
)
7363 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
7367 asection
*notesec
= NULL
;
7368 bfd
*noteobj
= NULL
;
7369 bfd
*emptyobj
= NULL
;
7372 for (inputobj
= info
->input_bfds
;
7374 inputobj
= inputobj
->link
.next
)
7379 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
7381 s
= inputobj
->sections
;
7382 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
7385 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
7389 if (s
->flags
& SEC_CODE
)
7393 /* There is no point in scanning the remaining bfds. */
7397 else if (bed
->default_execstack
&& info
->default_execstack
)
7400 emptyobj
= inputobj
;
7404 if (notesec
|| info
->stacksize
> 0)
7408 if (info
->warn_execstack
!= 0)
7410 /* PR 29072: Because an executable stack is a serious
7411 security risk, make sure that the user knows that it is
7412 being enabled despite the fact that it was not requested
7413 on the command line. */
7416 if (info
->error_execstack
)
7418 _bfd_error_handler (_("\
7419 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7420 bfd_get_filename (noteobj
));
7424 _bfd_error_handler (_("\
7425 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7426 bfd_get_filename (noteobj
));
7430 if (info
->error_execstack
)
7432 _bfd_error_handler (_("\
7433 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7434 bfd_get_filename (emptyobj
));
7438 _bfd_error_handler (_("\
7439 warning: %s: missing .note.GNU-stack section implies executable stack"),
7440 bfd_get_filename (emptyobj
));
7441 _bfd_error_handler (_("\
7442 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7446 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
7449 if (notesec
&& exec
&& bfd_link_relocatable (info
)
7450 && notesec
->output_section
!= bfd_abs_section_ptr
)
7451 notesec
->output_section
->flags
|= SEC_CODE
;
7454 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7456 struct elf_info_failed eif
;
7457 struct elf_link_hash_entry
*h
;
7461 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7462 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7466 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7468 info
->flags
|= DF_SYMBOLIC
;
7476 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7478 if (indx
== (size_t) -1)
7481 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7482 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7486 if (filter_shlib
!= NULL
)
7490 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7491 filter_shlib
, true);
7492 if (indx
== (size_t) -1
7493 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7497 if (auxiliary_filters
!= NULL
)
7499 const char * const *p
;
7501 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7505 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7507 if (indx
== (size_t) -1
7508 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7517 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7519 if (indx
== (size_t) -1
7520 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7524 if (depaudit
!= NULL
)
7528 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7530 if (indx
== (size_t) -1
7531 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7538 /* Find all symbols which were defined in a dynamic object and make
7539 the backend pick a reasonable value for them. */
7540 elf_link_hash_traverse (elf_hash_table (info
),
7541 _bfd_elf_adjust_dynamic_symbol
,
7546 /* Add some entries to the .dynamic section. We fill in some of the
7547 values later, in bfd_elf_final_link, but we must add the entries
7548 now so that we know the final size of the .dynamic section. */
7550 /* If there are initialization and/or finalization functions to
7551 call then add the corresponding DT_INIT/DT_FINI entries. */
7552 h
= (info
->init_function
7553 ? elf_link_hash_lookup (elf_hash_table (info
),
7554 info
->init_function
, false,
7561 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7564 h
= (info
->fini_function
7565 ? elf_link_hash_lookup (elf_hash_table (info
),
7566 info
->fini_function
, false,
7573 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7577 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7578 if (s
!= NULL
&& s
->linker_has_input
)
7580 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7581 if (! bfd_link_executable (info
))
7586 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7587 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7588 && (o
= sub
->sections
) != NULL
7589 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7590 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7591 if (elf_section_data (o
)->this_hdr
.sh_type
7592 == SHT_PREINIT_ARRAY
)
7595 (_("%pB: .preinit_array section is not allowed in DSO"),
7600 bfd_set_error (bfd_error_nonrepresentable_section
);
7604 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7605 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7608 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7609 if (s
!= NULL
&& s
->linker_has_input
)
7611 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7612 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7615 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7616 if (s
!= NULL
&& s
->linker_has_input
)
7618 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7619 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7623 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7624 /* If .dynstr is excluded from the link, we don't want any of
7625 these tags. Strictly, we should be checking each section
7626 individually; This quick check covers for the case where
7627 someone does a /DISCARD/ : { *(*) }. */
7628 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7630 bfd_size_type strsize
;
7632 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7633 if ((info
->emit_hash
7634 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7635 || (info
->emit_gnu_hash
7636 && (bed
->record_xhash_symbol
== NULL
7637 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7638 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7639 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7640 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7641 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7643 || (info
->gnu_flags_1
7644 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_FLAGS_1
,
7645 info
->gnu_flags_1
)))
7650 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7653 /* The backend must work out the sizes of all the other dynamic
7655 if (bed
->elf_backend_late_size_sections
!= NULL
7656 && !bed
->elf_backend_late_size_sections (output_bfd
, info
))
7659 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7661 if (elf_tdata (output_bfd
)->cverdefs
)
7663 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7665 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7666 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7670 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7672 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7675 else if (info
->flags
& DF_BIND_NOW
)
7677 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7683 if (bfd_link_executable (info
))
7684 info
->flags_1
&= ~ (DF_1_INITFIRST
7687 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7691 if (elf_tdata (output_bfd
)->cverrefs
)
7693 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7695 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7696 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7700 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7701 && elf_tdata (output_bfd
)->cverdefs
== 0)
7702 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7706 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7707 s
->flags
|= SEC_EXCLUDE
;
7713 /* Find the first non-excluded output section. We'll use its
7714 section symbol for some emitted relocs. */
7716 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7719 asection
*found
= NULL
;
7721 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7722 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7723 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7726 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7729 elf_hash_table (info
)->text_index_section
= found
;
7732 /* Find two non-excluded output sections, one for code, one for data.
7733 We'll use their section symbols for some emitted relocs. */
7735 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7738 asection
*found
= NULL
;
7740 /* Data first, since setting text_index_section changes
7741 _bfd_elf_omit_section_dynsym_default. */
7742 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7743 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7744 && !(s
->flags
& SEC_READONLY
)
7745 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7748 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7751 elf_hash_table (info
)->data_index_section
= found
;
7753 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7754 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7755 && (s
->flags
& SEC_READONLY
)
7756 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7761 elf_hash_table (info
)->text_index_section
= found
;
7764 #define GNU_HASH_SECTION_NAME(bed) \
7765 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7768 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7770 const struct elf_backend_data
*bed
;
7771 unsigned long section_sym_count
;
7772 bfd_size_type dynsymcount
= 0;
7774 if (!is_elf_hash_table (info
->hash
))
7777 bed
= get_elf_backend_data (output_bfd
);
7778 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7780 /* Assign dynsym indices. In a shared library we generate a section
7781 symbol for each output section, which come first. Next come all
7782 of the back-end allocated local dynamic syms, followed by the rest
7783 of the global symbols.
7785 This is usually not needed for static binaries, however backends
7786 can request to always do it, e.g. the MIPS backend uses dynamic
7787 symbol counts to lay out GOT, which will be produced in the
7788 presence of GOT relocations even in static binaries (holding fixed
7789 data in that case, to satisfy those relocations). */
7791 if (elf_hash_table (info
)->dynamic_sections_created
7792 || bed
->always_renumber_dynsyms
)
7793 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7794 §ion_sym_count
);
7796 if (elf_hash_table (info
)->dynamic_sections_created
)
7800 unsigned int dtagcount
;
7802 dynobj
= elf_hash_table (info
)->dynobj
;
7804 /* Work out the size of the symbol version section. */
7805 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7806 BFD_ASSERT (s
!= NULL
);
7807 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7809 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7810 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7811 if (s
->contents
== NULL
)
7814 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7818 /* Set the size of the .dynsym and .hash sections. We counted
7819 the number of dynamic symbols in elf_link_add_object_symbols.
7820 We will build the contents of .dynsym and .hash when we build
7821 the final symbol table, because until then we do not know the
7822 correct value to give the symbols. We built the .dynstr
7823 section as we went along in elf_link_add_object_symbols. */
7824 s
= elf_hash_table (info
)->dynsym
;
7825 BFD_ASSERT (s
!= NULL
);
7826 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7828 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7829 if (s
->contents
== NULL
)
7832 /* The first entry in .dynsym is a dummy symbol. Clear all the
7833 section syms, in case we don't output them all. */
7834 ++section_sym_count
;
7835 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7837 elf_hash_table (info
)->bucketcount
= 0;
7839 /* Compute the size of the hashing table. As a side effect this
7840 computes the hash values for all the names we export. */
7841 if (info
->emit_hash
)
7843 unsigned long int *hashcodes
;
7844 struct hash_codes_info hashinf
;
7846 unsigned long int nsyms
;
7848 size_t hash_entry_size
;
7850 /* Compute the hash values for all exported symbols. At the same
7851 time store the values in an array so that we could use them for
7853 amt
= dynsymcount
* sizeof (unsigned long int);
7854 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7855 if (hashcodes
== NULL
)
7857 hashinf
.hashcodes
= hashcodes
;
7858 hashinf
.error
= false;
7860 /* Put all hash values in HASHCODES. */
7861 elf_link_hash_traverse (elf_hash_table (info
),
7862 elf_collect_hash_codes
, &hashinf
);
7869 nsyms
= hashinf
.hashcodes
- hashcodes
;
7871 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7874 if (bucketcount
== 0 && nsyms
> 0)
7877 elf_hash_table (info
)->bucketcount
= bucketcount
;
7879 s
= bfd_get_linker_section (dynobj
, ".hash");
7880 BFD_ASSERT (s
!= NULL
);
7881 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7882 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7883 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7884 if (s
->contents
== NULL
)
7887 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7888 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7889 s
->contents
+ hash_entry_size
);
7892 if (info
->emit_gnu_hash
)
7895 unsigned char *contents
;
7896 struct collect_gnu_hash_codes cinfo
;
7900 memset (&cinfo
, 0, sizeof (cinfo
));
7902 /* Compute the hash values for all exported symbols. At the same
7903 time store the values in an array so that we could use them for
7905 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7906 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7907 if (cinfo
.hashcodes
== NULL
)
7910 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7911 cinfo
.min_dynindx
= -1;
7912 cinfo
.output_bfd
= output_bfd
;
7915 /* Put all hash values in HASHCODES. */
7916 elf_link_hash_traverse (elf_hash_table (info
),
7917 elf_collect_gnu_hash_codes
, &cinfo
);
7920 free (cinfo
.hashcodes
);
7925 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7927 if (bucketcount
== 0)
7929 free (cinfo
.hashcodes
);
7933 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7934 BFD_ASSERT (s
!= NULL
);
7936 if (cinfo
.nsyms
== 0)
7938 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7939 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7940 free (cinfo
.hashcodes
);
7941 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7942 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7943 if (contents
== NULL
)
7945 s
->contents
= contents
;
7946 /* 1 empty bucket. */
7947 bfd_put_32 (output_bfd
, 1, contents
);
7948 /* SYMIDX above the special symbol 0. */
7949 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7950 /* Just one word for bitmask. */
7951 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7952 /* Only hash fn bloom filter. */
7953 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7954 /* No hashes are valid - empty bitmask. */
7955 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7956 /* No hashes in the only bucket. */
7957 bfd_put_32 (output_bfd
, 0,
7958 contents
+ 16 + bed
->s
->arch_size
/ 8);
7962 unsigned long int maskwords
, maskbitslog2
, x
;
7963 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7967 while ((x
>>= 1) != 0)
7969 if (maskbitslog2
< 3)
7971 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7972 maskbitslog2
= maskbitslog2
+ 3;
7974 maskbitslog2
= maskbitslog2
+ 2;
7975 if (bed
->s
->arch_size
== 64)
7977 if (maskbitslog2
== 5)
7983 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7984 cinfo
.shift2
= maskbitslog2
;
7985 cinfo
.maskbits
= 1 << maskbitslog2
;
7986 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7987 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7988 amt
+= maskwords
* sizeof (bfd_vma
);
7989 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7990 if (cinfo
.bitmask
== NULL
)
7992 free (cinfo
.hashcodes
);
7996 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7997 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7998 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7999 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
8001 /* Determine how often each hash bucket is used. */
8002 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
8003 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
8004 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
8006 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
8007 if (cinfo
.counts
[i
] != 0)
8009 cinfo
.indx
[i
] = cnt
;
8010 cnt
+= cinfo
.counts
[i
];
8012 BFD_ASSERT (cnt
== dynsymcount
);
8013 cinfo
.bucketcount
= bucketcount
;
8014 cinfo
.local_indx
= cinfo
.min_dynindx
;
8016 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
8017 s
->size
+= cinfo
.maskbits
/ 8;
8018 if (bed
->record_xhash_symbol
!= NULL
)
8019 s
->size
+= cinfo
.nsyms
* 4;
8020 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
8021 if (contents
== NULL
)
8023 free (cinfo
.bitmask
);
8024 free (cinfo
.hashcodes
);
8028 s
->contents
= contents
;
8029 bfd_put_32 (output_bfd
, bucketcount
, contents
);
8030 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
8031 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
8032 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
8033 contents
+= 16 + cinfo
.maskbits
/ 8;
8035 for (i
= 0; i
< bucketcount
; ++i
)
8037 if (cinfo
.counts
[i
] == 0)
8038 bfd_put_32 (output_bfd
, 0, contents
);
8040 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
8044 cinfo
.contents
= contents
;
8046 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
8047 /* Renumber dynamic symbols, if populating .gnu.hash section.
8048 If using .MIPS.xhash, populate the translation table. */
8049 elf_link_hash_traverse (elf_hash_table (info
),
8050 elf_gnu_hash_process_symidx
, &cinfo
);
8052 contents
= s
->contents
+ 16;
8053 for (i
= 0; i
< maskwords
; ++i
)
8055 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
8057 contents
+= bed
->s
->arch_size
/ 8;
8060 free (cinfo
.bitmask
);
8061 free (cinfo
.hashcodes
);
8065 s
= bfd_get_linker_section (dynobj
, ".dynstr");
8066 BFD_ASSERT (s
!= NULL
);
8068 elf_finalize_dynstr (output_bfd
, info
);
8070 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
8072 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
8073 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
8080 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
8083 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
8086 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
8087 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
8090 /* Finish SHF_MERGE section merging. */
8093 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
8098 if (!is_elf_hash_table (info
->hash
))
8101 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
8102 if ((ibfd
->flags
& DYNAMIC
) == 0
8103 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
8104 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
8105 == get_elf_backend_data (obfd
)->s
->elfclass
))
8106 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
8107 if ((sec
->flags
& SEC_MERGE
) != 0
8108 && !bfd_is_abs_section (sec
->output_section
))
8110 struct bfd_elf_section_data
*secdata
;
8112 secdata
= elf_section_data (sec
);
8113 if (! _bfd_add_merge_section (obfd
,
8114 &elf_hash_table (info
)->merge_info
,
8115 sec
, &secdata
->sec_info
))
8117 else if (secdata
->sec_info
)
8118 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
8121 if (elf_hash_table (info
)->merge_info
!= NULL
)
8122 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
8123 merge_sections_remove_hook
);
8127 /* Create an entry in an ELF linker hash table. */
8129 struct bfd_hash_entry
*
8130 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
8131 struct bfd_hash_table
*table
,
8134 /* Allocate the structure if it has not already been allocated by a
8138 entry
= (struct bfd_hash_entry
*)
8139 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
8144 /* Call the allocation method of the superclass. */
8145 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
8148 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
8149 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
8151 /* Set local fields. */
8154 ret
->got
= htab
->init_got_refcount
;
8155 ret
->plt
= htab
->init_plt_refcount
;
8156 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
8157 - offsetof (struct elf_link_hash_entry
, size
)));
8158 /* Assume that we have been called by a non-ELF symbol reader.
8159 This flag is then reset by the code which reads an ELF input
8160 file. This ensures that a symbol created by a non-ELF symbol
8161 reader will have the flag set correctly. */
8168 /* Copy data from an indirect symbol to its direct symbol, hiding the
8169 old indirect symbol. Also used for copying flags to a weakdef. */
8172 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
8173 struct elf_link_hash_entry
*dir
,
8174 struct elf_link_hash_entry
*ind
)
8176 struct elf_link_hash_table
*htab
;
8178 if (ind
->dyn_relocs
!= NULL
)
8180 if (dir
->dyn_relocs
!= NULL
)
8182 struct elf_dyn_relocs
**pp
;
8183 struct elf_dyn_relocs
*p
;
8185 /* Add reloc counts against the indirect sym to the direct sym
8186 list. Merge any entries against the same section. */
8187 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
8189 struct elf_dyn_relocs
*q
;
8191 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
8192 if (q
->sec
== p
->sec
)
8194 q
->pc_count
+= p
->pc_count
;
8195 q
->count
+= p
->count
;
8202 *pp
= dir
->dyn_relocs
;
8205 dir
->dyn_relocs
= ind
->dyn_relocs
;
8206 ind
->dyn_relocs
= NULL
;
8209 /* Copy down any references that we may have already seen to the
8210 symbol which just became indirect. */
8212 if (dir
->versioned
!= versioned_hidden
)
8213 dir
->ref_dynamic
|= ind
->ref_dynamic
;
8214 dir
->ref_regular
|= ind
->ref_regular
;
8215 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
8216 dir
->non_got_ref
|= ind
->non_got_ref
;
8217 dir
->needs_plt
|= ind
->needs_plt
;
8218 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
8220 if (ind
->root
.type
!= bfd_link_hash_indirect
)
8223 /* Copy over the global and procedure linkage table refcount entries.
8224 These may have been already set up by a check_relocs routine. */
8225 htab
= elf_hash_table (info
);
8226 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
8228 if (dir
->got
.refcount
< 0)
8229 dir
->got
.refcount
= 0;
8230 dir
->got
.refcount
+= ind
->got
.refcount
;
8231 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
8234 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
8236 if (dir
->plt
.refcount
< 0)
8237 dir
->plt
.refcount
= 0;
8238 dir
->plt
.refcount
+= ind
->plt
.refcount
;
8239 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
8242 if (ind
->dynindx
!= -1)
8244 if (dir
->dynindx
!= -1)
8245 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
8246 dir
->dynindx
= ind
->dynindx
;
8247 dir
->dynstr_index
= ind
->dynstr_index
;
8249 ind
->dynstr_index
= 0;
8254 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
8255 struct elf_link_hash_entry
*h
,
8258 /* STT_GNU_IFUNC symbol must go through PLT. */
8259 if (h
->type
!= STT_GNU_IFUNC
)
8261 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
8266 h
->forced_local
= 1;
8267 if (h
->dynindx
!= -1)
8269 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
8272 h
->dynstr_index
= 0;
8277 /* Hide a symbol. */
8280 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
8281 struct bfd_link_info
*info
,
8282 struct bfd_link_hash_entry
*h
)
8284 if (is_elf_hash_table (info
->hash
))
8286 const struct elf_backend_data
*bed
8287 = get_elf_backend_data (output_bfd
);
8288 struct elf_link_hash_entry
*eh
8289 = (struct elf_link_hash_entry
*) h
;
8290 bed
->elf_backend_hide_symbol (info
, eh
, true);
8291 eh
->def_dynamic
= 0;
8292 eh
->ref_dynamic
= 0;
8293 eh
->dynamic_def
= 0;
8297 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8301 _bfd_elf_link_hash_table_init
8302 (struct elf_link_hash_table
*table
,
8304 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
8305 struct bfd_hash_table
*,
8307 unsigned int entsize
,
8308 enum elf_target_id target_id
)
8311 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
8313 table
->init_got_refcount
.refcount
= can_refcount
- 1;
8314 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
8315 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
8316 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
8317 /* The first dynamic symbol is a dummy. */
8318 table
->dynsymcount
= 1;
8320 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
8322 table
->root
.type
= bfd_link_elf_hash_table
;
8323 table
->hash_table_id
= target_id
;
8324 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
8329 /* Create an ELF linker hash table. */
8331 struct bfd_link_hash_table
*
8332 _bfd_elf_link_hash_table_create (bfd
*abfd
)
8334 struct elf_link_hash_table
*ret
;
8335 size_t amt
= sizeof (struct elf_link_hash_table
);
8337 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
8341 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
8342 sizeof (struct elf_link_hash_entry
),
8348 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
8353 /* Destroy an ELF linker hash table. */
8356 _bfd_elf_link_hash_table_free (bfd
*obfd
)
8358 struct elf_link_hash_table
*htab
;
8360 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
8361 if (htab
->dynstr
!= NULL
)
8362 _bfd_elf_strtab_free (htab
->dynstr
);
8363 _bfd_merge_sections_free (htab
->merge_info
);
8364 /* NB: htab->dynamic->contents is always allocated by bfd_realloc. */
8365 if (htab
->dynamic
!= NULL
)
8366 free (htab
->dynamic
->contents
);
8367 if (htab
->first_hash
!= NULL
)
8369 bfd_hash_table_free (htab
->first_hash
);
8370 free (htab
->first_hash
);
8372 _bfd_generic_link_hash_table_free (obfd
);
8375 /* This is a hook for the ELF emulation code in the generic linker to
8376 tell the backend linker what file name to use for the DT_NEEDED
8377 entry for a dynamic object. */
8380 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
8382 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8383 && bfd_get_format (abfd
) == bfd_object
)
8384 elf_dt_name (abfd
) = name
;
8388 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
8391 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8392 && bfd_get_format (abfd
) == bfd_object
)
8393 lib_class
= elf_dyn_lib_class (abfd
);
8400 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
8402 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8403 && bfd_get_format (abfd
) == bfd_object
)
8404 elf_dyn_lib_class (abfd
) = lib_class
;
8407 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8408 the linker ELF emulation code. */
8410 struct bfd_link_needed_list
*
8411 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8412 struct bfd_link_info
*info
)
8414 if (! is_elf_hash_table (info
->hash
))
8416 return elf_hash_table (info
)->needed
;
8419 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8420 hook for the linker ELF emulation code. */
8422 struct bfd_link_needed_list
*
8423 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8424 struct bfd_link_info
*info
)
8426 if (! is_elf_hash_table (info
->hash
))
8428 return elf_hash_table (info
)->runpath
;
8431 /* Get the name actually used for a dynamic object for a link. This
8432 is the SONAME entry if there is one. Otherwise, it is the string
8433 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8436 bfd_elf_get_dt_soname (bfd
*abfd
)
8438 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8439 && bfd_get_format (abfd
) == bfd_object
)
8440 return elf_dt_name (abfd
);
8444 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8445 the ELF linker emulation code. */
8448 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
8449 struct bfd_link_needed_list
**pneeded
)
8452 bfd_byte
*dynbuf
= NULL
;
8453 unsigned int elfsec
;
8454 unsigned long shlink
;
8455 bfd_byte
*extdyn
, *extdynend
;
8457 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
8461 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
8462 || bfd_get_format (abfd
) != bfd_object
)
8465 s
= bfd_get_section_by_name (abfd
, ".dynamic");
8466 if (s
== NULL
|| s
->size
== 0 || (s
->flags
& SEC_HAS_CONTENTS
) == 0)
8469 if (!_bfd_elf_mmap_section_contents (abfd
, s
, &dynbuf
))
8472 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8473 if (elfsec
== SHN_BAD
)
8476 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8478 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8479 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8481 for (extdyn
= dynbuf
, extdynend
= dynbuf
+ s
->size
;
8482 (size_t) (extdynend
- extdyn
) >= extdynsize
;
8483 extdyn
+= extdynsize
)
8485 Elf_Internal_Dyn dyn
;
8487 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8489 if (dyn
.d_tag
== DT_NULL
)
8492 if (dyn
.d_tag
== DT_NEEDED
)
8495 struct bfd_link_needed_list
*l
;
8496 unsigned int tagv
= dyn
.d_un
.d_val
;
8499 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8504 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8515 _bfd_elf_munmap_section_contents (s
, dynbuf
);
8520 _bfd_elf_munmap_section_contents (s
, dynbuf
);
8524 struct elf_symbuf_symbol
8526 unsigned long st_name
; /* Symbol name, index in string tbl */
8527 unsigned char st_info
; /* Type and binding attributes */
8528 unsigned char st_other
; /* Visibilty, and target specific */
8531 struct elf_symbuf_head
8533 struct elf_symbuf_symbol
*ssym
;
8535 unsigned int st_shndx
;
8542 Elf_Internal_Sym
*isym
;
8543 struct elf_symbuf_symbol
*ssym
;
8549 /* Sort references to symbols by ascending section number. */
8552 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8554 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8555 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8557 if (s1
->st_shndx
!= s2
->st_shndx
)
8558 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8559 /* Final sort by the address of the sym in the symbuf ensures
8562 return s1
> s2
? 1 : -1;
8567 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8569 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8570 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8571 int ret
= strcmp (s1
->name
, s2
->name
);
8574 if (s1
->u
.p
!= s2
->u
.p
)
8575 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8579 static struct elf_symbuf_head
*
8580 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8582 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8583 struct elf_symbuf_symbol
*ssym
;
8584 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8585 size_t i
, shndx_count
, total_size
, amt
;
8587 amt
= symcount
* sizeof (*indbuf
);
8588 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8592 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8593 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8594 *ind
++ = &isymbuf
[i
];
8597 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8598 elf_sort_elf_symbol
);
8601 if (indbufend
> indbuf
)
8602 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8603 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8606 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8607 + (indbufend
- indbuf
) * sizeof (*ssym
));
8608 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8609 if (ssymbuf
== NULL
)
8615 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8616 ssymbuf
->ssym
= NULL
;
8617 ssymbuf
->count
= shndx_count
;
8618 ssymbuf
->st_shndx
= 0;
8619 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8621 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8624 ssymhead
->ssym
= ssym
;
8625 ssymhead
->count
= 0;
8626 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8628 ssym
->st_name
= (*ind
)->st_name
;
8629 ssym
->st_info
= (*ind
)->st_info
;
8630 ssym
->st_other
= (*ind
)->st_other
;
8633 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8634 && (uintptr_t) ssym
- (uintptr_t) ssymbuf
== total_size
);
8640 /* Check if 2 sections define the same set of local and global
8644 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8645 struct bfd_link_info
*info
)
8648 const struct elf_backend_data
*bed1
, *bed2
;
8649 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8650 size_t symcount1
, symcount2
;
8651 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8652 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8653 Elf_Internal_Sym
*isym
, *isymend
;
8654 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8655 size_t count1
, count2
, sec_count1
, sec_count2
, i
;
8656 unsigned int shndx1
, shndx2
;
8658 bool ignore_section_symbol_p
;
8663 /* Both sections have to be in ELF. */
8664 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8665 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8668 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8671 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8672 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8673 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8676 bed1
= get_elf_backend_data (bfd1
);
8677 bed2
= get_elf_backend_data (bfd2
);
8678 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8679 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8680 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8681 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8683 if (symcount1
== 0 || symcount2
== 0)
8689 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8690 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8692 /* Ignore section symbols only when matching non-debugging sections
8693 or linkonce section with comdat section. */
8694 ignore_section_symbol_p
8695 = ((sec1
->flags
& SEC_DEBUGGING
) == 0
8696 || ((elf_section_flags (sec1
) & SHF_GROUP
)
8697 != (elf_section_flags (sec2
) & SHF_GROUP
)));
8699 if (ssymbuf1
== NULL
)
8701 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8703 if (isymbuf1
== NULL
)
8706 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8708 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8709 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8713 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8715 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8717 if (isymbuf2
== NULL
)
8720 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8722 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8723 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8727 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8729 /* Optimized faster version. */
8731 struct elf_symbol
*symp
;
8732 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8735 hi
= ssymbuf1
->count
;
8741 mid
= (lo
+ hi
) / 2;
8742 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8744 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8748 count1
= ssymbuf1
[mid
].count
;
8753 if (ignore_section_symbol_p
)
8755 for (i
= 0; i
< count1
; i
++)
8756 if (ELF_ST_TYPE (ssymbuf1
->ssym
[i
].st_info
) == STT_SECTION
)
8758 count1
-= sec_count1
;
8762 hi
= ssymbuf2
->count
;
8768 mid
= (lo
+ hi
) / 2;
8769 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8771 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8775 count2
= ssymbuf2
[mid
].count
;
8780 if (ignore_section_symbol_p
)
8782 for (i
= 0; i
< count2
; i
++)
8783 if (ELF_ST_TYPE (ssymbuf2
->ssym
[i
].st_info
) == STT_SECTION
)
8785 count2
-= sec_count2
;
8788 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8792 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8794 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8795 if (symtable1
== NULL
|| symtable2
== NULL
)
8799 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
+ sec_count1
;
8800 ssym
< ssymend
; ssym
++)
8802 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8804 symp
->u
.ssym
= ssym
;
8805 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8812 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
+ sec_count2
;
8813 ssym
< ssymend
; ssym
++)
8815 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8817 symp
->u
.ssym
= ssym
;
8818 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8824 /* Sort symbol by name. */
8825 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8826 elf_sym_name_compare
);
8827 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8828 elf_sym_name_compare
);
8830 for (i
= 0; i
< count1
; i
++)
8831 /* Two symbols must have the same binding, type and name. */
8832 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8833 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8834 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8841 symtable1
= (struct elf_symbol
*)
8842 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8843 symtable2
= (struct elf_symbol
*)
8844 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8845 if (symtable1
== NULL
|| symtable2
== NULL
)
8848 /* Count definitions in the section. */
8850 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8851 if (isym
->st_shndx
== shndx1
8852 && (!ignore_section_symbol_p
8853 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8854 symtable1
[count1
++].u
.isym
= isym
;
8857 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8858 if (isym
->st_shndx
== shndx2
8859 && (!ignore_section_symbol_p
8860 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8861 symtable2
[count2
++].u
.isym
= isym
;
8863 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8866 for (i
= 0; i
< count1
; i
++)
8868 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8869 symtable1
[i
].u
.isym
->st_name
);
8871 for (i
= 0; i
< count2
; i
++)
8873 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8874 symtable2
[i
].u
.isym
->st_name
);
8876 /* Sort symbol by name. */
8877 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8878 elf_sym_name_compare
);
8879 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8880 elf_sym_name_compare
);
8882 for (i
= 0; i
< count1
; i
++)
8883 /* Two symbols must have the same binding, type and name. */
8884 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8885 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8886 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8900 /* Return TRUE if 2 section types are compatible. */
8903 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8904 bfd
*bbfd
, const asection
*bsec
)
8908 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8909 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8912 return elf_section_type (asec
) == elf_section_type (bsec
);
8915 /* Final phase of ELF linker. */
8917 /* A structure we use to avoid passing large numbers of arguments. */
8919 struct elf_final_link_info
8921 /* General link information. */
8922 struct bfd_link_info
*info
;
8925 /* Symbol string table. */
8926 struct elf_strtab_hash
*symstrtab
;
8927 /* .hash section. */
8929 /* symbol version section (.gnu.version). */
8930 asection
*symver_sec
;
8931 /* Buffer large enough to hold contents of any section. */
8933 /* Buffer large enough to hold external relocs of any section. */
8934 void *external_relocs
;
8935 /* Buffer large enough to hold internal relocs of any section. */
8936 Elf_Internal_Rela
*internal_relocs
;
8937 /* Buffer large enough to hold external local symbols of any input
8939 bfd_byte
*external_syms
;
8940 /* And a buffer for symbol section indices. */
8941 Elf_External_Sym_Shndx
*locsym_shndx
;
8942 /* Buffer large enough to hold internal local symbols of any input
8944 Elf_Internal_Sym
*internal_syms
;
8945 /* Array large enough to hold a symbol index for each local symbol
8946 of any input BFD. */
8948 /* Array large enough to hold a section pointer for each local
8949 symbol of any input BFD. */
8950 asection
**sections
;
8951 /* Buffer for SHT_SYMTAB_SHNDX section. */
8952 Elf_External_Sym_Shndx
*symshndxbuf
;
8953 /* Number of STT_FILE syms seen. */
8954 size_t filesym_count
;
8955 /* Local symbol hash table. */
8956 struct bfd_hash_table local_hash_table
;
8959 struct local_hash_entry
8961 /* Base hash table entry structure. */
8962 struct bfd_hash_entry root
;
8963 /* Size of the local symbol name. */
8965 /* Number of the duplicated local symbol names. */
8969 /* Create an entry in the local symbol hash table. */
8971 static struct bfd_hash_entry
*
8972 local_hash_newfunc (struct bfd_hash_entry
*entry
,
8973 struct bfd_hash_table
*table
,
8977 /* Allocate the structure if it has not already been allocated by a
8981 entry
= bfd_hash_allocate (table
,
8982 sizeof (struct local_hash_entry
));
8987 /* Call the allocation method of the superclass. */
8988 entry
= bfd_hash_newfunc (entry
, table
, string
);
8991 ((struct local_hash_entry
*) entry
)->count
= 0;
8992 ((struct local_hash_entry
*) entry
)->size
= 0;
8998 /* This struct is used to pass information to elf_link_output_extsym. */
9000 struct elf_outext_info
9005 struct elf_final_link_info
*flinfo
;
9009 /* Support for evaluating a complex relocation.
9011 Complex relocations are generalized, self-describing relocations. The
9012 implementation of them consists of two parts: complex symbols, and the
9013 relocations themselves.
9015 The relocations use a reserved elf-wide relocation type code (R_RELC
9016 external / BFD_RELOC_RELC internal) and an encoding of relocation field
9017 information (start bit, end bit, word width, etc) into the addend. This
9018 information is extracted from CGEN-generated operand tables within gas.
9020 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9021 internal) representing prefix-notation expressions, including but not
9022 limited to those sorts of expressions normally encoded as addends in the
9023 addend field. The symbol mangling format is:
9026 | <unary-operator> ':' <node>
9027 | <binary-operator> ':' <node> ':' <node>
9030 <literal> := 's' <digits=N> ':' <N character symbol name>
9031 | 'S' <digits=N> ':' <N character section name>
9035 <binary-operator> := as in C
9036 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
9039 set_symbol_value (bfd
*bfd_with_globals
,
9040 Elf_Internal_Sym
*isymbuf
,
9045 struct elf_link_hash_entry
**sym_hashes
;
9046 struct elf_link_hash_entry
*h
;
9047 size_t extsymoff
= locsymcount
;
9049 if (symidx
< locsymcount
)
9051 Elf_Internal_Sym
*sym
;
9053 sym
= isymbuf
+ symidx
;
9054 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
9056 /* It is a local symbol: move it to the
9057 "absolute" section and give it a value. */
9058 sym
->st_shndx
= SHN_ABS
;
9059 sym
->st_value
= val
;
9062 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
9066 /* It is a global symbol: set its link type
9067 to "defined" and give it a value. */
9069 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
9070 h
= sym_hashes
[symidx
- extsymoff
];
9071 while (h
->root
.type
== bfd_link_hash_indirect
9072 || h
->root
.type
== bfd_link_hash_warning
)
9073 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9074 h
->root
.type
= bfd_link_hash_defined
;
9075 h
->root
.u
.def
.value
= val
;
9076 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
9080 resolve_symbol (const char *name
,
9082 struct elf_final_link_info
*flinfo
,
9084 Elf_Internal_Sym
*isymbuf
,
9087 Elf_Internal_Sym
*sym
;
9088 struct bfd_link_hash_entry
*global_entry
;
9089 const char *candidate
= NULL
;
9090 Elf_Internal_Shdr
*symtab_hdr
;
9093 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
9095 for (i
= 0; i
< locsymcount
; ++ i
)
9099 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
9102 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
9103 symtab_hdr
->sh_link
,
9106 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9107 name
, candidate
, (unsigned long) sym
->st_value
);
9109 if (candidate
&& strcmp (candidate
, name
) == 0)
9111 asection
*sec
= flinfo
->sections
[i
];
9113 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
9114 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
9116 printf ("Found symbol with value %8.8lx\n",
9117 (unsigned long) *result
);
9123 /* Hmm, haven't found it yet. perhaps it is a global. */
9124 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
9125 false, false, true);
9129 if (global_entry
->type
== bfd_link_hash_defined
9130 || global_entry
->type
== bfd_link_hash_defweak
)
9132 *result
= (global_entry
->u
.def
.value
9133 + global_entry
->u
.def
.section
->output_section
->vma
9134 + global_entry
->u
.def
.section
->output_offset
);
9136 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9137 global_entry
->root
.string
, (unsigned long) *result
);
9145 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
9146 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
9147 names like "foo.end" which is the end address of section "foo". */
9150 resolve_section (const char *name
,
9158 for (curr
= sections
; curr
; curr
= curr
->next
)
9159 if (strcmp (curr
->name
, name
) == 0)
9161 *result
= curr
->vma
;
9165 /* Hmm. still haven't found it. try pseudo-section names. */
9166 /* FIXME: This could be coded more efficiently... */
9167 for (curr
= sections
; curr
; curr
= curr
->next
)
9169 len
= strlen (curr
->name
);
9170 if (len
> strlen (name
))
9173 if (strncmp (curr
->name
, name
, len
) == 0)
9175 if (startswith (name
+ len
, ".end"))
9177 *result
= (curr
->vma
9178 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
9182 /* Insert more pseudo-section names here, if you like. */
9190 undefined_reference (const char *reftype
, const char *name
)
9192 /* xgettext:c-format */
9193 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9195 bfd_set_error (bfd_error_bad_value
);
9199 eval_symbol (bfd_vma
*result
,
9202 struct elf_final_link_info
*flinfo
,
9204 Elf_Internal_Sym
*isymbuf
,
9213 const char *sym
= *symp
;
9215 bool symbol_is_section
= false;
9220 if (len
< 1 || len
> sizeof (symbuf
))
9222 bfd_set_error (bfd_error_invalid_operation
);
9235 *result
= strtoul (sym
, (char **) symp
, 16);
9239 symbol_is_section
= true;
9243 symlen
= strtol (sym
, (char **) symp
, 10);
9244 sym
= *symp
+ 1; /* Skip the trailing ':'. */
9246 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
9248 bfd_set_error (bfd_error_invalid_operation
);
9252 memcpy (symbuf
, sym
, symlen
);
9253 symbuf
[symlen
] = '\0';
9254 *symp
= sym
+ symlen
;
9256 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9257 the symbol as a section, or vice-versa. so we're pretty liberal in our
9258 interpretation here; section means "try section first", not "must be a
9259 section", and likewise with symbol. */
9261 if (symbol_is_section
)
9263 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
9264 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
9265 isymbuf
, locsymcount
))
9267 undefined_reference ("section", symbuf
);
9273 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
9274 isymbuf
, locsymcount
)
9275 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
9278 undefined_reference ("symbol", symbuf
);
9285 /* All that remains are operators. */
9287 #define UNARY_OP(op) \
9288 if (startswith (sym, #op)) \
9290 sym += strlen (#op); \
9294 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9295 isymbuf, locsymcount, signed_p)) \
9298 *result = op ((bfd_signed_vma) a); \
9304 #define BINARY_OP_HEAD(op) \
9305 if (startswith (sym, #op)) \
9307 sym += strlen (#op); \
9311 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9312 isymbuf, locsymcount, signed_p)) \
9315 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9316 isymbuf, locsymcount, signed_p)) \
9318 #define BINARY_OP_TAIL(op) \
9320 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9325 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9329 BINARY_OP_HEAD (<<);
9330 if (b
>= sizeof (a
) * CHAR_BIT
)
9336 BINARY_OP_TAIL (<<);
9337 BINARY_OP_HEAD (>>);
9338 if (b
>= sizeof (a
) * CHAR_BIT
)
9340 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
9343 BINARY_OP_TAIL (>>);
9356 _bfd_error_handler (_("division by zero"));
9357 bfd_set_error (bfd_error_bad_value
);
9364 _bfd_error_handler (_("division by zero"));
9365 bfd_set_error (bfd_error_bad_value
);
9378 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
9379 bfd_set_error (bfd_error_invalid_operation
);
9385 put_value (bfd_vma size
,
9386 unsigned long chunksz
,
9391 location
+= (size
- chunksz
);
9393 for (; size
; size
-= chunksz
, location
-= chunksz
)
9398 bfd_put_8 (input_bfd
, x
, location
);
9402 bfd_put_16 (input_bfd
, x
, location
);
9406 bfd_put_32 (input_bfd
, x
, location
);
9407 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9413 bfd_put_64 (input_bfd
, x
, location
);
9414 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9427 get_value (bfd_vma size
,
9428 unsigned long chunksz
,
9435 /* Sanity checks. */
9436 BFD_ASSERT (chunksz
<= sizeof (x
)
9439 && (size
% chunksz
) == 0
9440 && input_bfd
!= NULL
9441 && location
!= NULL
);
9443 if (chunksz
== sizeof (x
))
9445 BFD_ASSERT (size
== chunksz
);
9447 /* Make sure that we do not perform an undefined shift operation.
9448 We know that size == chunksz so there will only be one iteration
9449 of the loop below. */
9453 shift
= 8 * chunksz
;
9455 for (; size
; size
-= chunksz
, location
+= chunksz
)
9460 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
9463 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
9466 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
9470 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
9481 decode_complex_addend (unsigned long *start
, /* in bits */
9482 unsigned long *oplen
, /* in bits */
9483 unsigned long *len
, /* in bits */
9484 unsigned long *wordsz
, /* in bytes */
9485 unsigned long *chunksz
, /* in bytes */
9486 unsigned long *lsb0_p
,
9487 unsigned long *signed_p
,
9488 unsigned long *trunc_p
,
9489 unsigned long encoded
)
9491 * start
= encoded
& 0x3F;
9492 * len
= (encoded
>> 6) & 0x3F;
9493 * oplen
= (encoded
>> 12) & 0x3F;
9494 * wordsz
= (encoded
>> 18) & 0xF;
9495 * chunksz
= (encoded
>> 22) & 0xF;
9496 * lsb0_p
= (encoded
>> 27) & 1;
9497 * signed_p
= (encoded
>> 28) & 1;
9498 * trunc_p
= (encoded
>> 29) & 1;
9501 bfd_reloc_status_type
9502 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
9503 asection
*input_section
,
9505 Elf_Internal_Rela
*rel
,
9508 bfd_vma shift
, x
, mask
;
9509 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9510 bfd_reloc_status_type r
;
9511 bfd_size_type octets
;
9513 /* Perform this reloc, since it is complex.
9514 (this is not to say that it necessarily refers to a complex
9515 symbol; merely that it is a self-describing CGEN based reloc.
9516 i.e. the addend has the complete reloc information (bit start, end,
9517 word size, etc) encoded within it.). */
9519 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9520 &chunksz
, &lsb0_p
, &signed_p
,
9521 &trunc_p
, rel
->r_addend
);
9523 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9526 shift
= (start
+ 1) - len
;
9528 shift
= (8 * wordsz
) - (start
+ len
);
9530 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9531 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9534 printf ("Doing complex reloc: "
9535 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9536 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9537 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9538 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9539 oplen
, (unsigned long) x
, (unsigned long) mask
,
9540 (unsigned long) relocation
);
9545 /* Now do an overflow check. */
9546 r
= bfd_check_overflow ((signed_p
9547 ? complain_overflow_signed
9548 : complain_overflow_unsigned
),
9549 len
, 0, (8 * wordsz
),
9553 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9556 printf (" relocation: %8.8lx\n"
9557 " shifted mask: %8.8lx\n"
9558 " shifted/masked reloc: %8.8lx\n"
9559 " result: %8.8lx\n",
9560 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9561 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9563 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9567 /* Functions to read r_offset from external (target order) reloc
9568 entry. Faster than bfd_getl32 et al, because we let the compiler
9569 know the value is aligned. */
9572 ext32l_r_offset (const void *p
)
9579 const union aligned32
*a
9580 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9582 uint32_t aval
= ( (uint32_t) a
->c
[0]
9583 | (uint32_t) a
->c
[1] << 8
9584 | (uint32_t) a
->c
[2] << 16
9585 | (uint32_t) a
->c
[3] << 24);
9590 ext32b_r_offset (const void *p
)
9597 const union aligned32
*a
9598 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9600 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9601 | (uint32_t) a
->c
[1] << 16
9602 | (uint32_t) a
->c
[2] << 8
9603 | (uint32_t) a
->c
[3]);
9608 ext64l_r_offset (const void *p
)
9615 const union aligned64
*a
9616 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9618 uint64_t aval
= ( (uint64_t) a
->c
[0]
9619 | (uint64_t) a
->c
[1] << 8
9620 | (uint64_t) a
->c
[2] << 16
9621 | (uint64_t) a
->c
[3] << 24
9622 | (uint64_t) a
->c
[4] << 32
9623 | (uint64_t) a
->c
[5] << 40
9624 | (uint64_t) a
->c
[6] << 48
9625 | (uint64_t) a
->c
[7] << 56);
9630 ext64b_r_offset (const void *p
)
9637 const union aligned64
*a
9638 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9640 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9641 | (uint64_t) a
->c
[1] << 48
9642 | (uint64_t) a
->c
[2] << 40
9643 | (uint64_t) a
->c
[3] << 32
9644 | (uint64_t) a
->c
[4] << 24
9645 | (uint64_t) a
->c
[5] << 16
9646 | (uint64_t) a
->c
[6] << 8
9647 | (uint64_t) a
->c
[7]);
9651 /* When performing a relocatable link, the input relocations are
9652 preserved. But, if they reference global symbols, the indices
9653 referenced must be updated. Update all the relocations found in
9657 elf_link_adjust_relocs (bfd
*abfd
,
9659 struct bfd_elf_section_reloc_data
*reldata
,
9661 struct bfd_link_info
*info
)
9664 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9666 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9667 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9668 bfd_vma r_type_mask
;
9670 unsigned int count
= reldata
->count
;
9671 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9673 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9675 swap_in
= bed
->s
->swap_reloc_in
;
9676 swap_out
= bed
->s
->swap_reloc_out
;
9678 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9680 swap_in
= bed
->s
->swap_reloca_in
;
9681 swap_out
= bed
->s
->swap_reloca_out
;
9686 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9689 if (bed
->s
->arch_size
== 32)
9696 r_type_mask
= 0xffffffff;
9700 erela
= reldata
->hdr
->contents
;
9701 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9703 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9706 if (*rel_hash
== NULL
)
9709 if ((*rel_hash
)->indx
== -2
9710 && info
->gc_sections
9711 && ! info
->gc_keep_exported
)
9713 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9714 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9716 (*rel_hash
)->root
.root
.string
);
9717 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9719 bfd_set_error (bfd_error_invalid_operation
);
9722 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9724 (*swap_in
) (abfd
, erela
, irela
);
9725 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9726 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9727 | (irela
[j
].r_info
& r_type_mask
));
9728 (*swap_out
) (abfd
, irela
, erela
);
9731 if (bed
->elf_backend_update_relocs
)
9732 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9734 if (sort
&& count
!= 0)
9736 bfd_vma (*ext_r_off
) (const void *);
9739 bfd_byte
*base
, *end
, *p
, *loc
;
9740 bfd_byte
*buf
= NULL
;
9742 if (bed
->s
->arch_size
== 32)
9744 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9745 ext_r_off
= ext32l_r_offset
;
9746 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9747 ext_r_off
= ext32b_r_offset
;
9753 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9754 ext_r_off
= ext64l_r_offset
;
9755 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9756 ext_r_off
= ext64b_r_offset
;
9761 /* Must use a stable sort here. A modified insertion sort,
9762 since the relocs are mostly sorted already. */
9763 elt_size
= reldata
->hdr
->sh_entsize
;
9764 base
= reldata
->hdr
->contents
;
9765 end
= base
+ count
* elt_size
;
9766 if (elt_size
> sizeof (Elf64_External_Rela
))
9769 /* Ensure the first element is lowest. This acts as a sentinel,
9770 speeding the main loop below. */
9771 r_off
= (*ext_r_off
) (base
);
9772 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9774 bfd_vma r_off2
= (*ext_r_off
) (p
);
9783 /* Don't just swap *base and *loc as that changes the order
9784 of the original base[0] and base[1] if they happen to
9785 have the same r_offset. */
9786 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9787 memcpy (onebuf
, loc
, elt_size
);
9788 memmove (base
+ elt_size
, base
, loc
- base
);
9789 memcpy (base
, onebuf
, elt_size
);
9792 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9794 /* base to p is sorted, *p is next to insert. */
9795 r_off
= (*ext_r_off
) (p
);
9796 /* Search the sorted region for location to insert. */
9798 while (r_off
< (*ext_r_off
) (loc
))
9803 /* Chances are there is a run of relocs to insert here,
9804 from one of more input files. Files are not always
9805 linked in order due to the way elf_link_input_bfd is
9806 called. See pr17666. */
9807 size_t sortlen
= p
- loc
;
9808 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9809 size_t runlen
= elt_size
;
9810 bfd_vma r_off_runend
= r_off
;
9811 bfd_vma r_off_runend_next
;
9812 size_t buf_size
= 96 * 1024;
9813 while (p
+ runlen
< end
9814 && (sortlen
<= buf_size
9815 || runlen
+ elt_size
<= buf_size
)
9816 /* run must not break the ordering of base..loc+1 */
9817 && r_off2
> (r_off_runend_next
= (*ext_r_off
) (p
+ runlen
))
9818 /* run must be already sorted */
9819 && r_off_runend_next
>= r_off_runend
)
9822 r_off_runend
= r_off_runend_next
;
9826 buf
= bfd_malloc (buf_size
);
9830 if (runlen
< sortlen
)
9832 memcpy (buf
, p
, runlen
);
9833 memmove (loc
+ runlen
, loc
, sortlen
);
9834 memcpy (loc
, buf
, runlen
);
9838 memcpy (buf
, loc
, sortlen
);
9839 memmove (loc
, p
, runlen
);
9840 memcpy (loc
+ runlen
, buf
, sortlen
);
9842 p
+= runlen
- elt_size
;
9845 /* Hashes are no longer valid. */
9846 free (reldata
->hashes
);
9847 reldata
->hashes
= NULL
;
9853 struct elf_link_sort_rela
9859 enum elf_reloc_type_class type
;
9860 /* We use this as an array of size int_rels_per_ext_rel. */
9861 Elf_Internal_Rela rela
[1];
9864 /* qsort stability here and for cmp2 is only an issue if multiple
9865 dynamic relocations are emitted at the same address. But targets
9866 that apply a series of dynamic relocations each operating on the
9867 result of the prior relocation can't use -z combreloc as
9868 implemented anyway. Such schemes tend to be broken by sorting on
9869 symbol index. That leaves dynamic NONE relocs as the only other
9870 case where ld might emit multiple relocs at the same address, and
9871 those are only emitted due to target bugs. */
9874 elf_link_sort_cmp1 (const void *A
, const void *B
)
9876 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9877 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9878 int relativea
, relativeb
;
9880 relativea
= a
->type
== reloc_class_relative
;
9881 relativeb
= b
->type
== reloc_class_relative
;
9883 if (relativea
< relativeb
)
9885 if (relativea
> relativeb
)
9887 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9889 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9891 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9893 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9899 elf_link_sort_cmp2 (const void *A
, const void *B
)
9901 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9902 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9904 if (a
->type
< b
->type
)
9906 if (a
->type
> b
->type
)
9908 if (a
->u
.offset
< b
->u
.offset
)
9910 if (a
->u
.offset
> b
->u
.offset
)
9912 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9914 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9920 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9922 asection
*dynamic_relocs
;
9925 bfd_size_type count
, size
;
9926 size_t i
, ret
, sort_elt
, ext_size
;
9927 bfd_byte
*sort
, *s_non_relative
, *p
;
9928 struct elf_link_sort_rela
*sq
;
9929 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9930 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9931 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9932 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9933 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9934 struct bfd_link_order
*lo
;
9938 /* Find a dynamic reloc section. */
9939 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9940 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9941 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9942 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9944 bool use_rela_initialised
= false;
9946 /* This is just here to stop gcc from complaining.
9947 Its initialization checking code is not perfect. */
9950 /* Both sections are present. Examine the sizes
9951 of the indirect sections to help us choose. */
9952 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9953 if (lo
->type
== bfd_indirect_link_order
)
9955 asection
*o
= lo
->u
.indirect
.section
;
9957 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9959 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9960 /* Section size is divisible by both rel and rela sizes.
9961 It is of no help to us. */
9965 /* Section size is only divisible by rela. */
9966 if (use_rela_initialised
&& !use_rela
)
9968 _bfd_error_handler (_("%pB: unable to sort relocs - "
9969 "they are in more than one size"),
9971 bfd_set_error (bfd_error_invalid_operation
);
9977 use_rela_initialised
= true;
9981 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9983 /* Section size is only divisible by rel. */
9984 if (use_rela_initialised
&& use_rela
)
9986 _bfd_error_handler (_("%pB: unable to sort relocs - "
9987 "they are in more than one size"),
9989 bfd_set_error (bfd_error_invalid_operation
);
9995 use_rela_initialised
= true;
10000 /* The section size is not divisible by either -
10001 something is wrong. */
10002 _bfd_error_handler (_("%pB: unable to sort relocs - "
10003 "they are of an unknown size"), abfd
);
10004 bfd_set_error (bfd_error_invalid_operation
);
10009 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10010 if (lo
->type
== bfd_indirect_link_order
)
10012 asection
*o
= lo
->u
.indirect
.section
;
10014 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
10016 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
10017 /* Section size is divisible by both rel and rela sizes.
10018 It is of no help to us. */
10022 /* Section size is only divisible by rela. */
10023 if (use_rela_initialised
&& !use_rela
)
10025 _bfd_error_handler (_("%pB: unable to sort relocs - "
10026 "they are in more than one size"),
10028 bfd_set_error (bfd_error_invalid_operation
);
10034 use_rela_initialised
= true;
10038 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
10040 /* Section size is only divisible by rel. */
10041 if (use_rela_initialised
&& use_rela
)
10043 _bfd_error_handler (_("%pB: unable to sort relocs - "
10044 "they are in more than one size"),
10046 bfd_set_error (bfd_error_invalid_operation
);
10052 use_rela_initialised
= true;
10057 /* The section size is not divisible by either -
10058 something is wrong. */
10059 _bfd_error_handler (_("%pB: unable to sort relocs - "
10060 "they are of an unknown size"), abfd
);
10061 bfd_set_error (bfd_error_invalid_operation
);
10066 if (! use_rela_initialised
)
10067 /* Make a guess. */
10070 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
10072 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
10079 dynamic_relocs
= rela_dyn
;
10080 ext_size
= bed
->s
->sizeof_rela
;
10081 swap_in
= bed
->s
->swap_reloca_in
;
10082 swap_out
= bed
->s
->swap_reloca_out
;
10086 dynamic_relocs
= rel_dyn
;
10087 ext_size
= bed
->s
->sizeof_rel
;
10088 swap_in
= bed
->s
->swap_reloc_in
;
10089 swap_out
= bed
->s
->swap_reloc_out
;
10093 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10094 if (lo
->type
== bfd_indirect_link_order
)
10095 size
+= lo
->u
.indirect
.section
->size
;
10097 if (size
!= dynamic_relocs
->size
)
10100 sort_elt
= (sizeof (struct elf_link_sort_rela
)
10101 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
10103 count
= dynamic_relocs
->size
/ ext_size
;
10106 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
10110 (*info
->callbacks
->warning
)
10111 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
10115 if (bed
->s
->arch_size
== 32)
10116 r_sym_mask
= ~(bfd_vma
) 0xff;
10118 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
10120 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10121 if (lo
->type
== bfd_indirect_link_order
)
10123 bfd_byte
*erel
, *erelend
;
10124 asection
*o
= lo
->u
.indirect
.section
;
10126 if (o
->contents
== NULL
&& o
->size
!= 0)
10128 /* This is a reloc section that is being handled as a normal
10129 section. See bfd_section_from_shdr. We can't combine
10130 relocs in this case. */
10134 erel
= o
->contents
;
10135 erelend
= o
->contents
+ o
->size
;
10136 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
10138 while (erel
< erelend
)
10140 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
10142 (*swap_in
) (abfd
, erel
, s
->rela
);
10143 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
10144 s
->u
.sym_mask
= r_sym_mask
;
10150 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
10152 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
10154 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
10155 if (s
->type
!= reloc_class_relative
)
10159 s_non_relative
= p
;
10161 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
10162 for (; i
< count
; i
++, p
+= sort_elt
)
10164 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
10165 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
10167 sp
->u
.offset
= sq
->rela
->r_offset
;
10170 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
10172 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
10173 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
10175 /* We have plt relocs in .rela.dyn. */
10176 sq
= (struct elf_link_sort_rela
*) sort
;
10177 for (i
= 0; i
< count
; i
++)
10178 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
10180 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
10182 struct bfd_link_order
**plo
;
10183 /* Put srelplt link_order last. This is so the output_offset
10184 set in the next loop is correct for DT_JMPREL. */
10185 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
10186 if ((*plo
)->type
== bfd_indirect_link_order
10187 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
10193 plo
= &(*plo
)->next
;
10196 dynamic_relocs
->map_tail
.link_order
= lo
;
10201 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10202 if (lo
->type
== bfd_indirect_link_order
)
10204 bfd_byte
*erel
, *erelend
;
10205 asection
*o
= lo
->u
.indirect
.section
;
10207 erel
= o
->contents
;
10208 erelend
= o
->contents
+ o
->size
;
10209 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
10210 while (erel
< erelend
)
10212 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
10213 (*swap_out
) (abfd
, s
->rela
, erel
);
10220 *psec
= dynamic_relocs
;
10224 /* Add a symbol to the output symbol string table. */
10227 elf_link_output_symstrtab (void *finf
,
10229 Elf_Internal_Sym
*elfsym
,
10230 asection
*input_sec
,
10231 struct elf_link_hash_entry
*h
)
10233 struct elf_final_link_info
*flinfo
= finf
;
10234 int (*output_symbol_hook
)
10235 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
10236 struct elf_link_hash_entry
*);
10237 struct elf_link_hash_table
*hash_table
;
10238 const struct elf_backend_data
*bed
;
10239 bfd_size_type strtabsize
;
10241 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10243 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10244 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
10245 if (output_symbol_hook
!= NULL
)
10247 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
10252 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
10253 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
10254 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
10255 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
10257 if (name
== NULL
|| *name
== '\0')
10258 elfsym
->st_name
= (unsigned long) -1;
10261 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10262 to get the final offset for st_name. */
10263 char *versioned_name
= (char *) name
;
10266 if (h
->versioned
== versioned
&& h
->def_dynamic
)
10268 /* Keep only one '@' for versioned symbols defined in
10270 char *version
= strrchr (name
, ELF_VER_CHR
);
10271 char *base_end
= strchr (name
, ELF_VER_CHR
);
10272 if (version
!= base_end
)
10275 size_t len
= strlen (name
);
10276 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
10277 if (versioned_name
== NULL
)
10279 base_len
= base_end
- name
;
10280 memcpy (versioned_name
, name
, base_len
);
10281 memcpy (versioned_name
+ base_len
, version
,
10286 else if (flinfo
->info
->unique_symbol
10287 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
10289 struct local_hash_entry
*lh
;
10293 switch (ELF_ST_TYPE (elfsym
->st_info
))
10299 lh
= (struct local_hash_entry
*) bfd_hash_lookup
10300 (&flinfo
->local_hash_table
, name
, true, false);
10303 /* Always append ".COUNT" to local symbols to avoid
10304 potential conflicts with local symbol "XXX.COUNT". */
10305 sprintf (buf
, "%lx", lh
->count
);
10306 base_len
= lh
->size
;
10309 base_len
= strlen (name
);
10310 lh
->size
= base_len
;
10312 count_len
= strlen (buf
);
10313 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
10314 base_len
+ count_len
+ 2);
10315 if (versioned_name
== NULL
)
10317 memcpy (versioned_name
, name
, base_len
);
10318 versioned_name
[base_len
] = '.';
10319 memcpy (versioned_name
+ base_len
+ 1, buf
,
10326 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
10327 versioned_name
, false);
10328 if (elfsym
->st_name
== (unsigned long) -1)
10332 hash_table
= elf_hash_table (flinfo
->info
);
10333 strtabsize
= hash_table
->strtabsize
;
10334 if (strtabsize
<= flinfo
->output_bfd
->symcount
)
10336 strtabsize
+= strtabsize
;
10337 hash_table
->strtabsize
= strtabsize
;
10338 strtabsize
*= sizeof (*hash_table
->strtab
);
10340 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
10342 if (hash_table
->strtab
== NULL
)
10345 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].sym
= *elfsym
;
10346 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].dest_index
10347 = flinfo
->output_bfd
->symcount
;
10348 flinfo
->output_bfd
->symcount
+= 1;
10353 /* Swap symbols out to the symbol table and flush the output symbols to
10357 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
10359 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
10362 const struct elf_backend_data
*bed
;
10364 Elf_Internal_Shdr
*hdr
;
10368 if (flinfo
->output_bfd
->symcount
== 0)
10371 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10373 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10375 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10376 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
10377 if (symbuf
== NULL
)
10380 if (flinfo
->symshndxbuf
)
10382 amt
= sizeof (Elf_External_Sym_Shndx
);
10383 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
10384 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
10385 if (flinfo
->symshndxbuf
== NULL
)
10392 /* Now swap out the symbols. */
10393 for (i
= 0; i
< flinfo
->output_bfd
->symcount
; i
++)
10395 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
10396 if (elfsym
->sym
.st_name
== (unsigned long) -1)
10397 elfsym
->sym
.st_name
= 0;
10399 elfsym
->sym
.st_name
10400 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
10401 elfsym
->sym
.st_name
);
10403 /* Inform the linker of the addition of this symbol. */
10405 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
10406 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
10409 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
10410 ((bfd_byte
*) symbuf
10411 + (elfsym
->dest_index
10412 * bed
->s
->sizeof_sym
)),
10413 NPTR_ADD (flinfo
->symshndxbuf
,
10414 elfsym
->dest_index
));
10417 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
10418 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
10419 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10420 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
10421 && bfd_write (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
10423 hdr
->sh_size
+= amt
;
10431 free (hash_table
->strtab
);
10432 hash_table
->strtab
= NULL
;
10437 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10440 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
10442 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
10443 && sym
->st_shndx
< SHN_LORESERVE
)
10445 /* The gABI doesn't support dynamic symbols in output sections
10448 /* xgettext:c-format */
10449 (_("%pB: too many sections: %d (>= %d)"),
10450 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
10451 bfd_set_error (bfd_error_nonrepresentable_section
);
10457 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10458 allowing an unsatisfied unversioned symbol in the DSO to match a
10459 versioned symbol that would normally require an explicit version.
10460 We also handle the case that a DSO references a hidden symbol
10461 which may be satisfied by a versioned symbol in another DSO. */
10464 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
10465 const struct elf_backend_data
*bed
,
10466 struct elf_link_hash_entry
*h
)
10469 struct elf_link_loaded_list
*loaded
;
10471 if (!is_elf_hash_table (info
->hash
))
10474 /* Check indirect symbol. */
10475 while (h
->root
.type
== bfd_link_hash_indirect
)
10476 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10478 switch (h
->root
.type
)
10484 case bfd_link_hash_undefined
:
10485 case bfd_link_hash_undefweak
:
10486 abfd
= h
->root
.u
.undef
.abfd
;
10488 || (abfd
->flags
& DYNAMIC
) == 0
10489 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
10493 case bfd_link_hash_defined
:
10494 case bfd_link_hash_defweak
:
10495 abfd
= h
->root
.u
.def
.section
->owner
;
10498 case bfd_link_hash_common
:
10499 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10502 BFD_ASSERT (abfd
!= NULL
);
10504 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10506 loaded
= loaded
->next
)
10509 Elf_Internal_Shdr
*hdr
;
10511 size_t extsymcount
;
10513 Elf_Internal_Shdr
*versymhdr
;
10514 Elf_Internal_Sym
*isym
;
10515 Elf_Internal_Sym
*isymend
;
10516 Elf_Internal_Sym
*isymbuf
;
10517 Elf_External_Versym
*ever
;
10518 Elf_External_Versym
*extversym
;
10520 input
= loaded
->abfd
;
10522 /* We check each DSO for a possible hidden versioned definition. */
10524 || elf_dynversym (input
) == 0)
10527 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10529 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10530 if (elf_bad_symtab (input
))
10532 extsymcount
= symcount
;
10537 extsymcount
= symcount
- hdr
->sh_info
;
10538 extsymoff
= hdr
->sh_info
;
10541 if (extsymcount
== 0)
10544 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10546 if (isymbuf
== NULL
)
10549 /* Read in any version definitions. */
10550 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10551 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10552 || (extversym
= (Elf_External_Versym
*)
10553 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10554 versymhdr
->sh_size
)) == NULL
)
10560 ever
= extversym
+ extsymoff
;
10561 isymend
= isymbuf
+ extsymcount
;
10562 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10565 Elf_Internal_Versym iver
;
10566 unsigned short version_index
;
10568 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10569 || isym
->st_shndx
== SHN_UNDEF
)
10572 name
= bfd_elf_string_from_elf_section (input
,
10575 if (strcmp (name
, h
->root
.root
.string
) != 0)
10578 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10580 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10581 && !(h
->def_regular
10582 && h
->forced_local
))
10584 /* If we have a non-hidden versioned sym, then it should
10585 have provided a definition for the undefined sym unless
10586 it is defined in a non-shared object and forced local.
10591 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10592 if (version_index
== 1 || version_index
== 2)
10594 /* This is the base or first version. We can use it. */
10608 /* Convert ELF common symbol TYPE. */
10611 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10613 /* Commom symbol can only appear in relocatable link. */
10614 if (!bfd_link_relocatable (info
))
10616 switch (info
->elf_stt_common
)
10620 case elf_stt_common
:
10623 case no_elf_stt_common
:
10630 /* Add an external symbol to the symbol table. This is called from
10631 the hash table traversal routine. When generating a shared object,
10632 we go through the symbol table twice. The first time we output
10633 anything that might have been forced to local scope in a version
10634 script. The second time we output the symbols that are still
10638 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10640 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10641 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10642 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10644 Elf_Internal_Sym sym
;
10645 asection
*input_sec
;
10646 const struct elf_backend_data
*bed
;
10651 if (h
->root
.type
== bfd_link_hash_warning
)
10653 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10654 if (h
->root
.type
== bfd_link_hash_new
)
10658 /* Decide whether to output this symbol in this pass. */
10659 if (eoinfo
->localsyms
)
10661 if (!h
->forced_local
)
10666 if (h
->forced_local
)
10670 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10672 if (h
->root
.type
== bfd_link_hash_undefined
)
10674 /* If we have an undefined symbol reference here then it must have
10675 come from a shared library that is being linked in. (Undefined
10676 references in regular files have already been handled unless
10677 they are in unreferenced sections which are removed by garbage
10679 bool ignore_undef
= false;
10681 /* Some symbols may be special in that the fact that they're
10682 undefined can be safely ignored - let backend determine that. */
10683 if (bed
->elf_backend_ignore_undef_symbol
)
10684 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10686 /* If we are reporting errors for this situation then do so now. */
10688 && h
->ref_dynamic_nonweak
10689 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10690 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10691 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10693 flinfo
->info
->callbacks
->undefined_symbol
10694 (flinfo
->info
, h
->root
.root
.string
,
10695 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10696 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10697 && !flinfo
->info
->warn_unresolved_syms
);
10700 /* Strip a global symbol defined in a discarded section. */
10705 /* We should also warn if a forced local symbol is referenced from
10706 shared libraries. */
10707 if (bfd_link_executable (flinfo
->info
)
10712 && h
->ref_dynamic_nonweak
10713 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10717 struct elf_link_hash_entry
*hi
= h
;
10719 /* Check indirect symbol. */
10720 while (hi
->root
.type
== bfd_link_hash_indirect
)
10721 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10723 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10724 /* xgettext:c-format */
10725 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10726 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10727 /* xgettext:c-format */
10728 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10730 /* xgettext:c-format */
10731 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10732 def_bfd
= flinfo
->output_bfd
;
10733 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10734 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10735 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10736 h
->root
.root
.string
, def_bfd
);
10737 bfd_set_error (bfd_error_bad_value
);
10738 eoinfo
->failed
= true;
10742 /* We don't want to output symbols that have never been mentioned by
10743 a regular file, or that we have been told to strip. However, if
10744 h->indx is set to -2, the symbol is used by a reloc and we must
10749 else if ((h
->def_dynamic
10751 || h
->root
.type
== bfd_link_hash_new
)
10753 && !h
->ref_regular
)
10755 else if (flinfo
->info
->strip
== strip_all
)
10757 else if (flinfo
->info
->strip
== strip_some
10758 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10759 h
->root
.root
.string
, false, false) == NULL
)
10761 else if ((h
->root
.type
== bfd_link_hash_defined
10762 || h
->root
.type
== bfd_link_hash_defweak
)
10763 && ((flinfo
->info
->strip_discarded
10764 && discarded_section (h
->root
.u
.def
.section
))
10765 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10766 && h
->root
.u
.def
.section
->owner
!= NULL
10767 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10769 else if ((h
->root
.type
== bfd_link_hash_undefined
10770 || h
->root
.type
== bfd_link_hash_undefweak
)
10771 && h
->root
.u
.undef
.abfd
!= NULL
10772 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10775 /* Remember if this symbol should be stripped. */
10776 bool should_strip
= strip
;
10778 /* Strip undefined weak symbols link if they don't have relocation. */
10780 strip
= !h
->has_reloc
&& h
->root
.type
== bfd_link_hash_undefweak
;
10784 /* If we're stripping it, and it's not a dynamic symbol, there's
10785 nothing else to do. However, if it is a forced local symbol or
10786 an ifunc symbol we need to give the backend finish_dynamic_symbol
10787 function a chance to make it dynamic. */
10789 && h
->dynindx
== -1
10790 && type
!= STT_GNU_IFUNC
10791 && !h
->forced_local
)
10795 sym
.st_size
= h
->size
;
10796 sym
.st_other
= h
->other
;
10797 switch (h
->root
.type
)
10800 case bfd_link_hash_new
:
10801 case bfd_link_hash_warning
:
10805 case bfd_link_hash_undefined
:
10806 case bfd_link_hash_undefweak
:
10807 input_sec
= bfd_und_section_ptr
;
10808 sym
.st_shndx
= SHN_UNDEF
;
10811 case bfd_link_hash_defined
:
10812 case bfd_link_hash_defweak
:
10814 input_sec
= h
->root
.u
.def
.section
;
10815 if (input_sec
->output_section
!= NULL
)
10818 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10819 input_sec
->output_section
);
10820 if (sym
.st_shndx
== SHN_BAD
)
10823 /* xgettext:c-format */
10824 (_("%pB: could not find output section %pA for input section %pA"),
10825 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10826 bfd_set_error (bfd_error_nonrepresentable_section
);
10827 eoinfo
->failed
= true;
10831 /* ELF symbols in relocatable files are section relative,
10832 but in nonrelocatable files they are virtual
10834 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10835 if (!bfd_link_relocatable (flinfo
->info
))
10837 sym
.st_value
+= input_sec
->output_section
->vma
;
10838 if (h
->type
== STT_TLS
)
10840 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10841 if (tls_sec
!= NULL
)
10842 sym
.st_value
-= tls_sec
->vma
;
10848 BFD_ASSERT (input_sec
->owner
== NULL
10849 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10850 sym
.st_shndx
= SHN_UNDEF
;
10851 input_sec
= bfd_und_section_ptr
;
10856 case bfd_link_hash_common
:
10857 input_sec
= h
->root
.u
.c
.p
->section
;
10858 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10859 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10862 case bfd_link_hash_indirect
:
10863 /* These symbols are created by symbol versioning. They point
10864 to the decorated version of the name. For example, if the
10865 symbol foo@@GNU_1.2 is the default, which should be used when
10866 foo is used with no version, then we add an indirect symbol
10867 foo which points to foo@@GNU_1.2. We ignore these symbols,
10868 since the indirected symbol is already in the hash table. */
10872 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10873 switch (h
->root
.type
)
10875 case bfd_link_hash_common
:
10876 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10878 case bfd_link_hash_defined
:
10879 case bfd_link_hash_defweak
:
10880 if (bed
->common_definition (&sym
))
10881 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10885 case bfd_link_hash_undefined
:
10886 case bfd_link_hash_undefweak
:
10892 if (h
->forced_local
)
10894 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10895 /* Turn off visibility on local symbol. */
10896 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10898 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10899 else if (h
->unique_global
&& h
->def_regular
)
10900 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10901 else if (h
->root
.type
== bfd_link_hash_undefweak
10902 || h
->root
.type
== bfd_link_hash_defweak
)
10903 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10905 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10906 sym
.st_target_internal
= h
->target_internal
;
10908 /* Give the processor backend a chance to tweak the symbol value,
10909 and also to finish up anything that needs to be done for this
10910 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10911 forced local syms when non-shared is due to a historical quirk.
10912 STT_GNU_IFUNC symbol must go through PLT. */
10913 if ((h
->type
== STT_GNU_IFUNC
10915 && !bfd_link_relocatable (flinfo
->info
))
10916 || ((h
->dynindx
!= -1
10917 || h
->forced_local
)
10918 && ((bfd_link_pic (flinfo
->info
)
10919 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10920 || h
->root
.type
!= bfd_link_hash_undefweak
))
10921 || !h
->forced_local
)
10922 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10924 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10925 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10927 eoinfo
->failed
= true;
10930 /* If a symbol is in the dynamic symbol table and isn't a
10931 should-strip symbol, also keep it in the symbol table. */
10936 /* If we are marking the symbol as undefined, and there are no
10937 non-weak references to this symbol from a regular object, then
10938 mark the symbol as weak undefined; if there are non-weak
10939 references, mark the symbol as strong. We can't do this earlier,
10940 because it might not be marked as undefined until the
10941 finish_dynamic_symbol routine gets through with it. */
10942 if (sym
.st_shndx
== SHN_UNDEF
10944 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10945 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10948 type
= ELF_ST_TYPE (sym
.st_info
);
10950 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10951 if (type
== STT_GNU_IFUNC
)
10954 if (h
->ref_regular_nonweak
)
10955 bindtype
= STB_GLOBAL
;
10957 bindtype
= STB_WEAK
;
10958 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10961 /* If this is a symbol defined in a dynamic library, don't use the
10962 symbol size from the dynamic library. Relinking an executable
10963 against a new library may introduce gratuitous changes in the
10964 executable's symbols if we keep the size. */
10965 if (sym
.st_shndx
== SHN_UNDEF
10970 /* If a non-weak symbol with non-default visibility is not defined
10971 locally, it is a fatal error. */
10972 if (!bfd_link_relocatable (flinfo
->info
)
10973 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10974 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10975 && h
->root
.type
== bfd_link_hash_undefined
10976 && !h
->def_regular
)
10980 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10981 /* xgettext:c-format */
10982 msg
= _("%pB: protected symbol `%s' isn't defined");
10983 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10984 /* xgettext:c-format */
10985 msg
= _("%pB: internal symbol `%s' isn't defined");
10987 /* xgettext:c-format */
10988 msg
= _("%pB: hidden symbol `%s' isn't defined");
10989 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10990 bfd_set_error (bfd_error_bad_value
);
10991 eoinfo
->failed
= true;
10995 /* If this symbol should be put in the .dynsym section, then put it
10996 there now. We already know the symbol index. We also fill in
10997 the entry in the .hash section. */
10998 if (h
->dynindx
!= -1
10999 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
11000 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
11001 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
11005 /* Since there is no version information in the dynamic string,
11006 if there is no version info in symbol version section, we will
11007 have a run-time problem if not linking executable, referenced
11008 by shared library, or not bound locally. */
11009 if (h
->verinfo
.verdef
== NULL
11010 && (!bfd_link_executable (flinfo
->info
)
11012 || !h
->def_regular
))
11014 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
11016 if (p
&& p
[1] != '\0')
11019 /* xgettext:c-format */
11020 (_("%pB: no symbol version section for versioned symbol `%s'"),
11021 flinfo
->output_bfd
, h
->root
.root
.string
);
11022 eoinfo
->failed
= true;
11027 sym
.st_name
= h
->dynstr_index
;
11028 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
11029 + h
->dynindx
* bed
->s
->sizeof_sym
);
11030 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
11032 eoinfo
->failed
= true;
11036 /* Inform the linker of the addition of this symbol. */
11038 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
11039 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
11041 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
11043 if (flinfo
->hash_sec
!= NULL
)
11045 size_t hash_entry_size
;
11046 bfd_byte
*bucketpos
;
11048 size_t bucketcount
;
11051 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
11052 bucket
= h
->u
.elf_hash_value
% bucketcount
;
11055 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
11056 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
11057 + (bucket
+ 2) * hash_entry_size
);
11058 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
11059 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
11061 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
11062 ((bfd_byte
*) flinfo
->hash_sec
->contents
11063 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
11066 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
11068 Elf_Internal_Versym iversym
;
11069 Elf_External_Versym
*eversym
;
11071 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
11073 if (h
->verinfo
.verdef
== NULL
11074 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
11075 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
11076 iversym
.vs_vers
= 1;
11078 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
11082 if (h
->verinfo
.vertree
== NULL
)
11083 iversym
.vs_vers
= 1;
11085 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
11086 if (flinfo
->info
->create_default_symver
)
11090 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
11091 defined locally. */
11092 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
11093 iversym
.vs_vers
|= VERSYM_HIDDEN
;
11095 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
11096 eversym
+= h
->dynindx
;
11097 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
11101 /* If the symbol is undefined, and we didn't output it to .dynsym,
11102 strip it from .symtab too. Obviously we can't do this for
11103 relocatable output or when needed for --emit-relocs. */
11104 else if (input_sec
== bfd_und_section_ptr
11106 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
11107 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
11108 && !bfd_link_relocatable (flinfo
->info
))
11111 /* Also strip others that we couldn't earlier due to dynamic symbol
11115 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
11118 /* Output a FILE symbol so that following locals are not associated
11119 with the wrong input file. We need one for forced local symbols
11120 if we've seen more than one FILE symbol or when we have exactly
11121 one FILE symbol but global symbols are present in a file other
11122 than the one with the FILE symbol. We also need one if linker
11123 defined symbols are present. In practice these conditions are
11124 always met, so just emit the FILE symbol unconditionally. */
11125 if (eoinfo
->localsyms
11126 && !eoinfo
->file_sym_done
11127 && eoinfo
->flinfo
->filesym_count
!= 0)
11129 Elf_Internal_Sym fsym
;
11131 memset (&fsym
, 0, sizeof (fsym
));
11132 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11133 fsym
.st_shndx
= SHN_ABS
;
11134 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
11135 bfd_und_section_ptr
, NULL
))
11138 eoinfo
->file_sym_done
= true;
11141 indx
= bfd_get_symcount (flinfo
->output_bfd
);
11142 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
11146 eoinfo
->failed
= true;
11151 else if (h
->indx
== -2)
11157 /* Return TRUE if special handling is done for relocs in SEC against
11158 symbols defined in discarded sections. */
11161 elf_section_ignore_discarded_relocs (asection
*sec
)
11163 const struct elf_backend_data
*bed
;
11165 switch (sec
->sec_info_type
)
11167 case SEC_INFO_TYPE_STABS
:
11168 case SEC_INFO_TYPE_EH_FRAME
:
11169 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11170 case SEC_INFO_TYPE_SFRAME
:
11176 bed
= get_elf_backend_data (sec
->owner
);
11177 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
11178 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
11184 /* Return a mask saying how ld should treat relocations in SEC against
11185 symbols defined in discarded sections. If this function returns
11186 COMPLAIN set, ld will issue a warning message. If this function
11187 returns PRETEND set, and the discarded section was link-once and the
11188 same size as the kept link-once section, ld will pretend that the
11189 symbol was actually defined in the kept section. Otherwise ld will
11190 zero the reloc (at least that is the intent, but some cooperation by
11191 the target dependent code is needed, particularly for REL targets). */
11194 _bfd_elf_default_action_discarded (asection
*sec
)
11196 const struct elf_backend_data
*bed
;
11197 bed
= get_elf_backend_data (sec
->owner
);
11199 if (sec
->flags
& SEC_DEBUGGING
)
11202 if (strcmp (".eh_frame", sec
->name
) == 0)
11205 if (bed
->elf_backend_can_make_multiple_eh_frame
11206 && strncmp (sec
->name
, ".eh_frame.", 10) == 0)
11209 if (strcmp (".sframe", sec
->name
) == 0)
11212 if (strcmp (".gcc_except_table", sec
->name
) == 0)
11215 return COMPLAIN
| PRETEND
;
11218 /* Find a match between a section and a member of a section group. */
11221 match_group_member (asection
*sec
, asection
*group
,
11222 struct bfd_link_info
*info
)
11224 asection
*first
= elf_next_in_group (group
);
11225 asection
*s
= first
;
11229 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
11232 s
= elf_next_in_group (s
);
11240 /* Check if the kept section of a discarded section SEC can be used
11241 to replace it. Return the replacement if it is OK. Otherwise return
11245 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
11249 kept
= sec
->kept_section
;
11252 if ((kept
->flags
& SEC_GROUP
) != 0)
11253 kept
= match_group_member (sec
, kept
, info
);
11256 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
11257 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
11261 /* Get the real kept section. */
11263 for (next
= kept
->kept_section
;
11265 next
= next
->kept_section
)
11269 sec
->kept_section
= kept
;
11274 /* Link an input file into the linker output file. This function
11275 handles all the sections and relocations of the input file at once.
11276 This is so that we only have to read the local symbols once, and
11277 don't have to keep them in memory. */
11280 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
11282 int (*relocate_section
)
11283 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
11284 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
11286 Elf_Internal_Shdr
*symtab_hdr
;
11287 size_t locsymcount
;
11289 Elf_Internal_Sym
*isymbuf
;
11290 Elf_Internal_Sym
*isym
;
11291 Elf_Internal_Sym
*isymend
;
11293 asection
**ppsection
;
11295 const struct elf_backend_data
*bed
;
11296 struct elf_link_hash_entry
**sym_hashes
;
11297 bfd_size_type address_size
;
11298 bfd_vma r_type_mask
;
11300 bool have_file_sym
= false;
11302 output_bfd
= flinfo
->output_bfd
;
11303 bed
= get_elf_backend_data (output_bfd
);
11304 relocate_section
= bed
->elf_backend_relocate_section
;
11306 /* If this is a dynamic object, we don't want to do anything here:
11307 we don't want the local symbols, and we don't want the section
11309 if ((input_bfd
->flags
& DYNAMIC
) != 0)
11312 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
11313 if (elf_bad_symtab (input_bfd
))
11315 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11320 locsymcount
= symtab_hdr
->sh_info
;
11321 extsymoff
= symtab_hdr
->sh_info
;
11324 /* Enable GNU OSABI features in the output BFD that are used in the input
11326 if (bed
->elf_osabi
== ELFOSABI_NONE
11327 || bed
->elf_osabi
== ELFOSABI_GNU
11328 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
11329 elf_tdata (output_bfd
)->has_gnu_osabi
11330 |= (elf_tdata (input_bfd
)->has_gnu_osabi
11331 & (bfd_link_relocatable (flinfo
->info
)
11332 ? -1 : ~elf_gnu_osabi_retain
));
11334 /* Read the local symbols. */
11335 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11336 if (isymbuf
== NULL
&& locsymcount
!= 0)
11338 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
11339 flinfo
->internal_syms
,
11340 flinfo
->external_syms
,
11341 flinfo
->locsym_shndx
);
11342 if (isymbuf
== NULL
)
11346 /* Find local symbol sections and adjust values of symbols in
11347 SEC_MERGE sections. Write out those local symbols we know are
11348 going into the output file. */
11349 isymend
= PTR_ADD (isymbuf
, locsymcount
);
11350 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
11352 isym
++, pindex
++, ppsection
++)
11356 Elf_Internal_Sym osym
;
11362 if (elf_bad_symtab (input_bfd
))
11364 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
11371 if (isym
->st_shndx
== SHN_UNDEF
)
11372 isec
= bfd_und_section_ptr
;
11373 else if (isym
->st_shndx
== SHN_ABS
)
11374 isec
= bfd_abs_section_ptr
;
11375 else if (isym
->st_shndx
== SHN_COMMON
)
11376 isec
= bfd_com_section_ptr
;
11379 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
11382 /* Don't attempt to output symbols with st_shnx in the
11383 reserved range other than SHN_ABS and SHN_COMMON. */
11384 isec
= bfd_und_section_ptr
;
11386 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
11387 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
11389 _bfd_merged_section_offset (output_bfd
, &isec
,
11390 elf_section_data (isec
)->sec_info
,
11396 /* Don't output the first, undefined, symbol. In fact, don't
11397 output any undefined local symbol. */
11398 if (isec
== bfd_und_section_ptr
)
11401 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
11403 /* We never output section symbols. Instead, we use the
11404 section symbol of the corresponding section in the output
11409 /* If we are stripping all symbols, we don't want to output this
11411 if (flinfo
->info
->strip
== strip_all
)
11414 /* If we are discarding all local symbols, we don't want to
11415 output this one. If we are generating a relocatable output
11416 file, then some of the local symbols may be required by
11417 relocs; we output them below as we discover that they are
11419 if (flinfo
->info
->discard
== discard_all
)
11422 /* If this symbol is defined in a section which we are
11423 discarding, we don't need to keep it. */
11424 if (isym
->st_shndx
< SHN_LORESERVE
11425 && (isec
->output_section
== NULL
11426 || bfd_section_removed_from_list (output_bfd
,
11427 isec
->output_section
)))
11430 /* Get the name of the symbol. */
11431 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
11436 /* See if we are discarding symbols with this name. */
11437 if ((flinfo
->info
->strip
== strip_some
11438 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, false, false)
11440 || (((flinfo
->info
->discard
== discard_sec_merge
11441 && (isec
->flags
& SEC_MERGE
)
11442 && !bfd_link_relocatable (flinfo
->info
))
11443 || flinfo
->info
->discard
== discard_l
)
11444 && bfd_is_local_label_name (input_bfd
, name
)))
11447 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
11449 if (input_bfd
->lto_output
)
11450 /* -flto puts a temp file name here. This means builds
11451 are not reproducible. Discard the symbol. */
11453 have_file_sym
= true;
11454 flinfo
->filesym_count
+= 1;
11456 if (!have_file_sym
)
11458 /* In the absence of debug info, bfd_find_nearest_line uses
11459 FILE symbols to determine the source file for local
11460 function symbols. Provide a FILE symbol here if input
11461 files lack such, so that their symbols won't be
11462 associated with a previous input file. It's not the
11463 source file, but the best we can do. */
11464 const char *filename
;
11465 have_file_sym
= true;
11466 flinfo
->filesym_count
+= 1;
11467 memset (&osym
, 0, sizeof (osym
));
11468 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11469 osym
.st_shndx
= SHN_ABS
;
11470 if (input_bfd
->lto_output
)
11473 filename
= lbasename (bfd_get_filename (input_bfd
));
11474 if (!elf_link_output_symstrtab (flinfo
, filename
, &osym
,
11475 bfd_abs_section_ptr
, NULL
))
11481 /* Adjust the section index for the output file. */
11482 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11483 isec
->output_section
);
11484 if (osym
.st_shndx
== SHN_BAD
)
11487 /* ELF symbols in relocatable files are section relative, but
11488 in executable files they are virtual addresses. Note that
11489 this code assumes that all ELF sections have an associated
11490 BFD section with a reasonable value for output_offset; below
11491 we assume that they also have a reasonable value for
11492 output_section. Any special sections must be set up to meet
11493 these requirements. */
11494 osym
.st_value
+= isec
->output_offset
;
11495 if (!bfd_link_relocatable (flinfo
->info
))
11497 osym
.st_value
+= isec
->output_section
->vma
;
11498 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
11500 /* STT_TLS symbols are relative to PT_TLS segment base. */
11501 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
11502 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
11504 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
11509 indx
= bfd_get_symcount (output_bfd
);
11510 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11517 if (bed
->s
->arch_size
== 32)
11519 r_type_mask
= 0xff;
11525 r_type_mask
= 0xffffffff;
11530 /* Relocate the contents of each section. */
11531 sym_hashes
= elf_sym_hashes (input_bfd
);
11532 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11534 bfd_byte
*contents
;
11536 if (! o
->linker_mark
)
11538 /* This section was omitted from the link. */
11542 if (!flinfo
->info
->resolve_section_groups
11543 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11545 /* Deal with the group signature symbol. */
11546 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11547 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11548 asection
*osec
= o
->output_section
;
11550 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11551 if (symndx
>= locsymcount
11552 || (elf_bad_symtab (input_bfd
)
11553 && flinfo
->sections
[symndx
] == NULL
))
11555 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
11556 while (h
->root
.type
== bfd_link_hash_indirect
11557 || h
->root
.type
== bfd_link_hash_warning
)
11558 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11559 /* Arrange for symbol to be output. */
11561 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11563 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11565 /* We'll use the output section target_index. */
11566 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11567 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11571 if (flinfo
->indices
[symndx
] == -1)
11573 /* Otherwise output the local symbol now. */
11574 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11575 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11580 name
= bfd_elf_string_from_elf_section (input_bfd
,
11581 symtab_hdr
->sh_link
,
11586 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11588 if (sym
.st_shndx
== SHN_BAD
)
11591 sym
.st_value
+= o
->output_offset
;
11593 indx
= bfd_get_symcount (output_bfd
);
11594 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11599 flinfo
->indices
[symndx
] = indx
;
11603 elf_section_data (osec
)->this_hdr
.sh_info
11604 = flinfo
->indices
[symndx
];
11608 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11609 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11612 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11614 /* Section was created by _bfd_elf_link_create_dynamic_sections
11619 /* Get the contents of the section. They have been cached by a
11620 relaxation routine. Note that o is a section in an input
11621 file, so the contents field will not have been set by any of
11622 the routines which work on output files. */
11623 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11625 contents
= elf_section_data (o
)->this_hdr
.contents
;
11626 if (bed
->caches_rawsize
11628 && o
->rawsize
< o
->size
)
11630 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11631 contents
= flinfo
->contents
;
11634 else if (!(o
->flags
& SEC_RELOC
)
11635 && !bed
->elf_backend_write_section
11636 && o
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
11637 /* A MERGE section that has no relocations doesn't need the
11638 contents anymore, they have been recorded earlier. Except
11639 if the backend has special provisions for writing sections. */
11643 contents
= flinfo
->contents
;
11644 if (! _bfd_elf_link_mmap_section_contents (input_bfd
, o
,
11649 if ((o
->flags
& SEC_RELOC
) != 0)
11651 Elf_Internal_Rela
*internal_relocs
;
11652 Elf_Internal_Rela
*rel
, *relend
;
11653 int action_discarded
;
11656 /* Get the swapped relocs. */
11658 = _bfd_elf_link_info_read_relocs (input_bfd
, flinfo
->info
, o
,
11659 flinfo
->external_relocs
,
11660 flinfo
->internal_relocs
,
11662 if (internal_relocs
== NULL
11663 && o
->reloc_count
> 0)
11666 action_discarded
= -1;
11667 if (!elf_section_ignore_discarded_relocs (o
))
11668 action_discarded
= (*bed
->action_discarded
) (o
);
11670 /* Run through the relocs evaluating complex reloc symbols and
11671 looking for relocs against symbols from discarded sections
11672 or section symbols from removed link-once sections.
11673 Complain about relocs against discarded sections. Zero
11674 relocs against removed link-once sections. */
11676 rel
= internal_relocs
;
11677 relend
= rel
+ o
->reloc_count
;
11678 for ( ; rel
< relend
; rel
++)
11680 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11681 unsigned int s_type
;
11682 asection
**ps
, *sec
;
11683 struct elf_link_hash_entry
*h
= NULL
;
11684 const char *sym_name
;
11686 if (r_symndx
== STN_UNDEF
)
11689 if (r_symndx
>= locsymcount
11690 || (elf_bad_symtab (input_bfd
)
11691 && flinfo
->sections
[r_symndx
] == NULL
))
11693 h
= sym_hashes
[r_symndx
- extsymoff
];
11695 /* Badly formatted input files can contain relocs that
11696 reference non-existant symbols. Check here so that
11697 we do not seg fault. */
11701 /* xgettext:c-format */
11702 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11703 "that references a non-existent global symbol"),
11704 input_bfd
, (uint64_t) rel
->r_info
, o
);
11705 bfd_set_error (bfd_error_bad_value
);
11709 while (h
->root
.type
== bfd_link_hash_indirect
11710 || h
->root
.type
== bfd_link_hash_warning
)
11711 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11715 /* If a plugin symbol is referenced from a non-IR file,
11716 mark the symbol as undefined. Note that the
11717 linker may attach linker created dynamic sections
11718 to the plugin bfd. Symbols defined in linker
11719 created sections are not plugin symbols. */
11720 if ((h
->root
.non_ir_ref_regular
11721 || h
->root
.non_ir_ref_dynamic
)
11722 && (h
->root
.type
== bfd_link_hash_defined
11723 || h
->root
.type
== bfd_link_hash_defweak
)
11724 && (h
->root
.u
.def
.section
->flags
11725 & SEC_LINKER_CREATED
) == 0
11726 && h
->root
.u
.def
.section
->owner
!= NULL
11727 && (h
->root
.u
.def
.section
->owner
->flags
11728 & BFD_PLUGIN
) != 0)
11730 h
->root
.type
= bfd_link_hash_undefined
;
11731 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11735 if (h
->root
.type
== bfd_link_hash_defined
11736 || h
->root
.type
== bfd_link_hash_defweak
)
11737 ps
= &h
->root
.u
.def
.section
;
11739 sym_name
= h
->root
.root
.string
;
11743 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11745 s_type
= ELF_ST_TYPE (sym
->st_info
);
11746 ps
= &flinfo
->sections
[r_symndx
];
11747 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11751 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11752 && !bfd_link_relocatable (flinfo
->info
))
11755 bfd_vma dot
= (rel
->r_offset
11756 + o
->output_offset
+ o
->output_section
->vma
);
11758 printf ("Encountered a complex symbol!");
11759 printf (" (input_bfd %s, section %s, reloc %ld\n",
11760 bfd_get_filename (input_bfd
), o
->name
,
11761 (long) (rel
- internal_relocs
));
11762 printf (" symbol: idx %8.8lx, name %s\n",
11763 r_symndx
, sym_name
);
11764 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11765 (unsigned long) rel
->r_info
,
11766 (unsigned long) rel
->r_offset
);
11768 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11769 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11772 /* Symbol evaluated OK. Update to absolute value. */
11773 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11778 if (action_discarded
!= -1 && ps
!= NULL
)
11780 /* Complain if the definition comes from a
11781 discarded section. */
11782 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11784 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11785 if (action_discarded
& COMPLAIN
)
11786 (*flinfo
->info
->callbacks
->einfo
)
11787 /* xgettext:c-format */
11788 (_("%X`%s' referenced in section `%pA' of %pB: "
11789 "defined in discarded section `%pA' of %pB\n"),
11790 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11792 /* Try to do the best we can to support buggy old
11793 versions of gcc. Pretend that the symbol is
11794 really defined in the kept linkonce section.
11795 FIXME: This is quite broken. Modifying the
11796 symbol here means we will be changing all later
11797 uses of the symbol, not just in this section. */
11798 if (action_discarded
& PRETEND
)
11802 kept
= _bfd_elf_check_kept_section (sec
,
11814 /* Relocate the section by invoking a back end routine.
11816 The back end routine is responsible for adjusting the
11817 section contents as necessary, and (if using Rela relocs
11818 and generating a relocatable output file) adjusting the
11819 reloc addend as necessary.
11821 The back end routine does not have to worry about setting
11822 the reloc address or the reloc symbol index.
11824 The back end routine is given a pointer to the swapped in
11825 internal symbols, and can access the hash table entries
11826 for the external symbols via elf_sym_hashes (input_bfd).
11828 When generating relocatable output, the back end routine
11829 must handle STB_LOCAL/STT_SECTION symbols specially. The
11830 output symbol is going to be a section symbol
11831 corresponding to the output section, which will require
11832 the addend to be adjusted. */
11834 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11835 input_bfd
, o
, contents
,
11843 || bfd_link_relocatable (flinfo
->info
)
11844 || flinfo
->info
->emitrelocations
)
11846 Elf_Internal_Rela
*irela
;
11847 Elf_Internal_Rela
*irelaend
, *irelamid
;
11848 bfd_vma last_offset
;
11849 struct elf_link_hash_entry
**rel_hash
;
11850 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11851 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11852 unsigned int next_erel
;
11854 struct bfd_elf_section_data
*esdi
, *esdo
;
11856 esdi
= elf_section_data (o
);
11857 esdo
= elf_section_data (o
->output_section
);
11858 rela_normal
= false;
11860 /* Adjust the reloc addresses and symbol indices. */
11862 irela
= internal_relocs
;
11863 irelaend
= irela
+ o
->reloc_count
;
11864 rel_hash
= PTR_ADD (esdo
->rel
.hashes
, esdo
->rel
.count
);
11865 /* We start processing the REL relocs, if any. When we reach
11866 IRELAMID in the loop, we switch to the RELA relocs. */
11868 if (esdi
->rel
.hdr
!= NULL
)
11869 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11870 * bed
->s
->int_rels_per_ext_rel
);
11871 rel_hash_list
= rel_hash
;
11872 rela_hash_list
= NULL
;
11873 last_offset
= o
->output_offset
;
11874 if (!bfd_link_relocatable (flinfo
->info
))
11875 last_offset
+= o
->output_section
->vma
;
11876 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11878 unsigned long r_symndx
;
11880 Elf_Internal_Sym sym
;
11882 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11888 if (irela
== irelamid
)
11890 rel_hash
= PTR_ADD (esdo
->rela
.hashes
, esdo
->rela
.count
);
11891 rela_hash_list
= rel_hash
;
11892 if (bed
->is_rela_normal
!= NULL
)
11893 rela_normal
= bed
->is_rela_normal (irela
);
11895 rela_normal
= bed
->rela_normal
;
11898 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11901 if (irela
->r_offset
>= (bfd_vma
) -2)
11903 /* This is a reloc for a deleted entry or somesuch.
11904 Turn it into an R_*_NONE reloc, at the same
11905 offset as the last reloc. elf_eh_frame.c and
11906 bfd_elf_discard_info rely on reloc offsets
11908 irela
->r_offset
= last_offset
;
11910 irela
->r_addend
= 0;
11914 irela
->r_offset
+= o
->output_offset
;
11916 /* Relocs in an executable have to be virtual addresses. */
11917 if (!bfd_link_relocatable (flinfo
->info
))
11918 irela
->r_offset
+= o
->output_section
->vma
;
11920 last_offset
= irela
->r_offset
;
11922 r_symndx
= irela
->r_info
>> r_sym_shift
;
11923 if (r_symndx
== STN_UNDEF
)
11926 if (r_symndx
>= locsymcount
11927 || (elf_bad_symtab (input_bfd
)
11928 && flinfo
->sections
[r_symndx
] == NULL
))
11930 struct elf_link_hash_entry
*rh
;
11931 unsigned long indx
;
11933 /* This is a reloc against a global symbol. We
11934 have not yet output all the local symbols, so
11935 we do not know the symbol index of any global
11936 symbol. We set the rel_hash entry for this
11937 reloc to point to the global hash table entry
11938 for this symbol. The symbol index is then
11939 set at the end of bfd_elf_final_link. */
11940 indx
= r_symndx
- extsymoff
;
11941 rh
= elf_sym_hashes (input_bfd
)[indx
];
11942 while (rh
->root
.type
== bfd_link_hash_indirect
11943 || rh
->root
.type
== bfd_link_hash_warning
)
11944 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11946 /* Setting the index to -2 tells
11947 elf_link_output_extsym that this symbol is
11948 used by a reloc. */
11949 BFD_ASSERT (rh
->indx
< 0);
11956 /* This is a reloc against a local symbol. */
11959 sym
= isymbuf
[r_symndx
];
11960 sec
= flinfo
->sections
[r_symndx
];
11961 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11963 /* I suppose the backend ought to fill in the
11964 section of any STT_SECTION symbol against a
11965 processor specific section. */
11966 r_symndx
= STN_UNDEF
;
11967 if (bfd_is_abs_section (sec
))
11969 else if (sec
== NULL
|| sec
->owner
== NULL
)
11971 bfd_set_error (bfd_error_bad_value
);
11976 asection
*osec
= sec
->output_section
;
11978 /* If we have discarded a section, the output
11979 section will be the absolute section. In
11980 case of discarded SEC_MERGE sections, use
11981 the kept section. relocate_section should
11982 have already handled discarded linkonce
11984 if (bfd_is_abs_section (osec
)
11985 && sec
->kept_section
!= NULL
11986 && sec
->kept_section
->output_section
!= NULL
)
11988 osec
= sec
->kept_section
->output_section
;
11989 irela
->r_addend
-= osec
->vma
;
11992 if (!bfd_is_abs_section (osec
))
11994 r_symndx
= osec
->target_index
;
11995 if (r_symndx
== STN_UNDEF
)
11997 irela
->r_addend
+= osec
->vma
;
11998 osec
= _bfd_nearby_section (output_bfd
, osec
,
12000 irela
->r_addend
-= osec
->vma
;
12001 r_symndx
= osec
->target_index
;
12006 /* Adjust the addend according to where the
12007 section winds up in the output section. */
12009 irela
->r_addend
+= sec
->output_offset
;
12013 if (flinfo
->indices
[r_symndx
] == -1)
12015 unsigned long shlink
;
12020 if (flinfo
->info
->strip
== strip_all
)
12022 /* You can't do ld -r -s. */
12023 bfd_set_error (bfd_error_invalid_operation
);
12027 /* This symbol was skipped earlier, but
12028 since it is needed by a reloc, we
12029 must output it now. */
12030 shlink
= symtab_hdr
->sh_link
;
12031 name
= (bfd_elf_string_from_elf_section
12032 (input_bfd
, shlink
, sym
.st_name
));
12036 osec
= sec
->output_section
;
12038 _bfd_elf_section_from_bfd_section (output_bfd
,
12040 if (sym
.st_shndx
== SHN_BAD
)
12043 sym
.st_value
+= sec
->output_offset
;
12044 if (!bfd_link_relocatable (flinfo
->info
))
12046 sym
.st_value
+= osec
->vma
;
12047 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
12049 struct elf_link_hash_table
*htab
12050 = elf_hash_table (flinfo
->info
);
12052 /* STT_TLS symbols are relative to PT_TLS
12054 if (htab
->tls_sec
!= NULL
)
12055 sym
.st_value
-= htab
->tls_sec
->vma
;
12058 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
12063 indx
= bfd_get_symcount (output_bfd
);
12064 ret
= elf_link_output_symstrtab (flinfo
, name
,
12070 flinfo
->indices
[r_symndx
] = indx
;
12075 r_symndx
= flinfo
->indices
[r_symndx
];
12078 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
12079 | (irela
->r_info
& r_type_mask
));
12082 /* Swap out the relocs. */
12083 input_rel_hdr
= esdi
->rel
.hdr
;
12084 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
12086 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
12091 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
12092 * bed
->s
->int_rels_per_ext_rel
);
12093 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
12096 input_rela_hdr
= esdi
->rela
.hdr
;
12097 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
12099 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
12108 /* Write out the modified section contents. */
12109 if (bed
->elf_backend_write_section
12110 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
12113 /* Section written out. */
12115 else switch (o
->sec_info_type
)
12117 case SEC_INFO_TYPE_STABS
:
12118 if (! (_bfd_write_section_stabs
12120 &elf_hash_table (flinfo
->info
)->stab_info
,
12121 o
, &elf_section_data (o
)->sec_info
, contents
)))
12124 case SEC_INFO_TYPE_MERGE
:
12125 if (! _bfd_write_merged_section (output_bfd
, o
,
12126 elf_section_data (o
)->sec_info
))
12129 case SEC_INFO_TYPE_EH_FRAME
:
12131 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
12136 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
12138 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
12144 case SEC_INFO_TYPE_SFRAME
:
12146 /* Merge .sframe sections into the ctf frame encoder
12147 context of the output_bfd's section. The final .sframe
12148 output section will be written out later. */
12149 if (!_bfd_elf_merge_section_sframe (output_bfd
, flinfo
->info
,
12156 if (! (o
->flags
& SEC_EXCLUDE
))
12158 file_ptr offset
= (file_ptr
) o
->output_offset
;
12159 bfd_size_type todo
= o
->size
;
12161 offset
*= bfd_octets_per_byte (output_bfd
, o
);
12163 if ((o
->flags
& SEC_ELF_REVERSE_COPY
)
12164 && o
->size
> address_size
)
12166 /* Reverse-copy input section to output. */
12168 if ((o
->size
& (address_size
- 1)) != 0
12169 || (o
->reloc_count
!= 0
12170 && (o
->size
* bed
->s
->int_rels_per_ext_rel
12171 != o
->reloc_count
* address_size
)))
12174 /* xgettext:c-format */
12175 (_("error: %pB: size of section %pA is not "
12176 "multiple of address size"),
12178 bfd_set_error (bfd_error_bad_value
);
12184 todo
-= address_size
;
12185 if (! bfd_set_section_contents (output_bfd
,
12193 offset
+= address_size
;
12197 else if (! bfd_set_section_contents (output_bfd
,
12207 /* Munmap the section contents for each input section. */
12208 _bfd_elf_link_munmap_section_contents (o
);
12214 /* Generate a reloc when linking an ELF file. This is a reloc
12215 requested by the linker, and does not come from any input file. This
12216 is used to build constructor and destructor tables when linking
12220 elf_reloc_link_order (bfd
*output_bfd
,
12221 struct bfd_link_info
*info
,
12222 asection
*output_section
,
12223 struct bfd_link_order
*link_order
)
12225 reloc_howto_type
*howto
;
12229 struct bfd_elf_section_reloc_data
*reldata
;
12230 struct elf_link_hash_entry
**rel_hash_ptr
;
12231 Elf_Internal_Shdr
*rel_hdr
;
12232 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
12233 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
12236 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
12238 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
12241 bfd_set_error (bfd_error_bad_value
);
12245 addend
= link_order
->u
.reloc
.p
->addend
;
12248 reldata
= &esdo
->rel
;
12249 else if (esdo
->rela
.hdr
)
12250 reldata
= &esdo
->rela
;
12257 /* Figure out the symbol index. */
12258 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
12259 if (link_order
->type
== bfd_section_reloc_link_order
)
12261 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
12262 BFD_ASSERT (indx
!= 0);
12263 *rel_hash_ptr
= NULL
;
12267 struct elf_link_hash_entry
*h
;
12269 /* Treat a reloc against a defined symbol as though it were
12270 actually against the section. */
12271 h
= ((struct elf_link_hash_entry
*)
12272 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
12273 link_order
->u
.reloc
.p
->u
.name
,
12274 false, false, true));
12276 && (h
->root
.type
== bfd_link_hash_defined
12277 || h
->root
.type
== bfd_link_hash_defweak
))
12281 section
= h
->root
.u
.def
.section
;
12282 indx
= section
->output_section
->target_index
;
12283 *rel_hash_ptr
= NULL
;
12284 /* It seems that we ought to add the symbol value to the
12285 addend here, but in practice it has already been added
12286 because it was passed to constructor_callback. */
12287 addend
+= section
->output_section
->vma
+ section
->output_offset
;
12289 else if (h
!= NULL
)
12291 /* Setting the index to -2 tells elf_link_output_extsym that
12292 this symbol is used by a reloc. */
12299 (*info
->callbacks
->unattached_reloc
)
12300 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
12305 /* If this is an inplace reloc, we must write the addend into the
12307 if (howto
->partial_inplace
&& addend
!= 0)
12309 bfd_size_type size
;
12310 bfd_reloc_status_type rstat
;
12313 const char *sym_name
;
12314 bfd_size_type octets
;
12316 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
12317 buf
= (bfd_byte
*) bfd_zmalloc (size
);
12318 if (buf
== NULL
&& size
!= 0)
12320 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
12327 case bfd_reloc_outofrange
:
12330 case bfd_reloc_overflow
:
12331 if (link_order
->type
== bfd_section_reloc_link_order
)
12332 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
12334 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
12335 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
12336 howto
->name
, addend
, NULL
, NULL
,
12341 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
12343 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
12350 /* The address of a reloc is relative to the section in a
12351 relocatable file, and is a virtual address in an executable
12353 offset
= link_order
->offset
;
12354 if (! bfd_link_relocatable (info
))
12355 offset
+= output_section
->vma
;
12357 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
12359 irel
[i
].r_offset
= offset
;
12360 irel
[i
].r_info
= 0;
12361 irel
[i
].r_addend
= 0;
12363 if (bed
->s
->arch_size
== 32)
12364 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
12366 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
12368 rel_hdr
= reldata
->hdr
;
12369 erel
= rel_hdr
->contents
;
12370 if (rel_hdr
->sh_type
== SHT_REL
)
12372 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
12373 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
12377 irel
[0].r_addend
= addend
;
12378 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
12379 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
12387 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12388 Returns TRUE upon success, FALSE otherwise. */
12391 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
12395 const struct elf_backend_data
*bed
;
12397 enum bfd_architecture arch
;
12399 asymbol
**sympp
= NULL
;
12403 elf_symbol_type
*osymbuf
;
12406 implib_bfd
= info
->out_implib_bfd
;
12407 bed
= get_elf_backend_data (abfd
);
12409 if (!bfd_set_format (implib_bfd
, bfd_object
))
12412 /* Use flag from executable but make it a relocatable object. */
12413 flags
= bfd_get_file_flags (abfd
);
12414 flags
&= ~HAS_RELOC
;
12415 if (!bfd_set_start_address (implib_bfd
, 0)
12416 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
12419 /* Copy architecture of output file to import library file. */
12420 arch
= bfd_get_arch (abfd
);
12421 mach
= bfd_get_mach (abfd
);
12422 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
12423 && (abfd
->target_defaulted
12424 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
12427 /* Get symbol table size. */
12428 symsize
= bfd_get_symtab_upper_bound (abfd
);
12432 /* Read in the symbol table. */
12433 sympp
= (asymbol
**) bfd_malloc (symsize
);
12437 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
12441 /* Allow the BFD backend to copy any private header data it
12442 understands from the output BFD to the import library BFD. */
12443 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
12446 /* Filter symbols to appear in the import library. */
12447 if (bed
->elf_backend_filter_implib_symbols
)
12448 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
12451 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
12454 bfd_set_error (bfd_error_no_symbols
);
12455 _bfd_error_handler (_("%pB: no symbol found for import library"),
12461 /* Make symbols absolute. */
12462 amt
= symcount
* sizeof (*osymbuf
);
12463 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
12464 if (osymbuf
== NULL
)
12467 for (src_count
= 0; src_count
< symcount
; src_count
++)
12469 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
12470 sizeof (*osymbuf
));
12471 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
12472 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
12473 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
12474 osymbuf
[src_count
].internal_elf_sym
.st_value
=
12475 osymbuf
[src_count
].symbol
.value
;
12476 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
12479 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
12481 /* Allow the BFD backend to copy any private data it understands
12482 from the output BFD to the import library BFD. This is done last
12483 to permit the routine to look at the filtered symbol table. */
12484 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
12487 if (!bfd_close (implib_bfd
))
12498 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
12502 if (flinfo
->symstrtab
!= NULL
)
12503 _bfd_elf_strtab_free (flinfo
->symstrtab
);
12504 free (flinfo
->contents
);
12505 free (flinfo
->external_relocs
);
12506 free (flinfo
->internal_relocs
);
12507 free (flinfo
->external_syms
);
12508 free (flinfo
->locsym_shndx
);
12509 free (flinfo
->internal_syms
);
12510 free (flinfo
->indices
);
12511 free (flinfo
->sections
);
12512 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12513 free (flinfo
->symshndxbuf
);
12514 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12516 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12517 free (esdo
->rel
.hashes
);
12518 free (esdo
->rela
.hashes
);
12522 /* Do the final step of an ELF link. */
12525 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12530 struct elf_final_link_info flinfo
;
12532 struct bfd_link_order
*p
;
12534 bfd_size_type max_contents_size
;
12535 bfd_size_type max_external_reloc_size
;
12536 bfd_size_type max_internal_reloc_count
;
12537 bfd_size_type max_sym_count
;
12538 bfd_size_type max_sym_shndx_count
;
12539 Elf_Internal_Sym elfsym
;
12541 Elf_Internal_Shdr
*symtab_hdr
;
12542 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12543 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12544 struct elf_outext_info eoinfo
;
12546 size_t relativecount
;
12547 size_t relr_entsize
;
12548 asection
*reldyn
= 0;
12550 asection
*attr_section
= NULL
;
12551 bfd_vma attr_size
= 0;
12552 const char *std_attrs_section
;
12553 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12554 bool sections_removed
;
12557 if (!is_elf_hash_table (&htab
->root
))
12560 if (bfd_link_pic (info
))
12561 abfd
->flags
|= DYNAMIC
;
12563 dynamic
= htab
->dynamic_sections_created
;
12564 dynobj
= htab
->dynobj
;
12566 emit_relocs
= (bfd_link_relocatable (info
)
12567 || info
->emitrelocations
);
12569 memset (&flinfo
, 0, sizeof (flinfo
));
12570 flinfo
.info
= info
;
12571 flinfo
.output_bfd
= abfd
;
12572 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12573 if (flinfo
.symstrtab
== NULL
)
12578 flinfo
.hash_sec
= NULL
;
12579 flinfo
.symver_sec
= NULL
;
12583 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12584 /* Note that dynsym_sec can be NULL (on VMS). */
12585 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12586 /* Note that it is OK if symver_sec is NULL. */
12589 if (info
->unique_symbol
12590 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12591 local_hash_newfunc
,
12592 sizeof (struct local_hash_entry
)))
12595 /* The object attributes have been merged. Remove the input
12596 sections from the link, and set the contents of the output
12598 sections_removed
= false;
12599 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12600 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12602 bool remove_section
= false;
12604 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12605 || strcmp (o
->name
, ".gnu.attributes") == 0)
12607 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12609 asection
*input_section
;
12611 if (p
->type
!= bfd_indirect_link_order
)
12613 input_section
= p
->u
.indirect
.section
;
12614 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12615 elf_link_input_bfd ignores this section. */
12616 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12619 attr_size
= bfd_elf_obj_attr_size (abfd
);
12620 bfd_set_section_size (o
, attr_size
);
12621 /* Skip this section later on. */
12622 o
->map_head
.link_order
= NULL
;
12626 remove_section
= true;
12628 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12630 /* Remove empty group section from linker output. */
12631 remove_section
= true;
12633 if (remove_section
)
12635 o
->flags
|= SEC_EXCLUDE
;
12636 bfd_section_list_remove (abfd
, o
);
12637 abfd
->section_count
--;
12638 sections_removed
= true;
12641 if (sections_removed
)
12642 _bfd_fix_excluded_sec_syms (abfd
, info
);
12644 /* Count up the number of relocations we will output for each output
12645 section, so that we know the sizes of the reloc sections. We
12646 also figure out some maximum sizes. */
12650 /* Mmap is used only if section size >= the minimum mmap section
12651 size. The initial max_contents_size value covers all sections
12652 smaller than the minimum mmap section size. It may be increased
12653 for compressed or linker created sections or sections whose
12654 rawsize != size. max_external_reloc_size covers all relocation
12655 sections smaller than the minimum mmap section size. */
12656 max_contents_size
= _bfd_minimum_mmap_size
;
12657 max_external_reloc_size
= _bfd_minimum_mmap_size
;
12662 max_contents_size
= 0;
12663 max_external_reloc_size
= 0;
12665 max_internal_reloc_count
= 0;
12667 max_sym_shndx_count
= 0;
12669 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12671 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12672 o
->reloc_count
= 0;
12674 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12676 unsigned int reloc_count
= 0;
12677 unsigned int additional_reloc_count
= 0;
12678 struct bfd_elf_section_data
*esdi
= NULL
;
12680 if (p
->type
== bfd_section_reloc_link_order
12681 || p
->type
== bfd_symbol_reloc_link_order
)
12683 else if (p
->type
== bfd_indirect_link_order
)
12687 sec
= p
->u
.indirect
.section
;
12689 /* Mark all sections which are to be included in the
12690 link. This will normally be every section. We need
12691 to do this so that we can identify any sections which
12692 the linker has decided to not include. */
12693 sec
->linker_mark
= true;
12695 if (sec
->flags
& SEC_MERGE
)
12699 /* Mmap is used only on non-compressed, non-linker created
12700 sections whose rawsize == size. */
12702 || sec
->compress_status
!= COMPRESS_SECTION_NONE
12703 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
12704 || sec
->rawsize
!= sec
->size
)
12707 if (sec
->rawsize
> max_contents_size
)
12708 max_contents_size
= sec
->rawsize
;
12709 if (sec
->size
> max_contents_size
)
12710 max_contents_size
= sec
->size
;
12713 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12714 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12718 /* We are interested in just local symbols, not all
12720 if (elf_bad_symtab (sec
->owner
))
12721 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12722 / bed
->s
->sizeof_sym
);
12724 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12726 if (sym_count
> max_sym_count
)
12727 max_sym_count
= sym_count
;
12729 if (sym_count
> max_sym_shndx_count
12730 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12731 max_sym_shndx_count
= sym_count
;
12733 esdi
= elf_section_data (sec
);
12735 if (esdi
->this_hdr
.sh_type
== SHT_REL
12736 || esdi
->this_hdr
.sh_type
== SHT_RELA
)
12737 /* Some backends use reloc_count in relocation sections
12738 to count particular types of relocs. Of course,
12739 reloc sections themselves can't have relocations. */
12741 else if (emit_relocs
)
12743 reloc_count
= sec
->reloc_count
;
12744 if (bed
->elf_backend_count_additional_relocs
)
12747 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12748 additional_reloc_count
+= c
;
12751 else if (bed
->elf_backend_count_relocs
)
12752 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12754 if ((sec
->flags
& SEC_RELOC
) != 0)
12757 if (!bed
->use_mmap
)
12760 size_t ext_size
= 0;
12762 if (esdi
->rel
.hdr
!= NULL
)
12763 ext_size
= esdi
->rel
.hdr
->sh_size
;
12764 if (esdi
->rela
.hdr
!= NULL
)
12765 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12767 if (ext_size
> max_external_reloc_size
)
12768 max_external_reloc_size
= ext_size
;
12770 if (sec
->reloc_count
> max_internal_reloc_count
)
12771 max_internal_reloc_count
= sec
->reloc_count
;
12776 if (reloc_count
== 0)
12779 reloc_count
+= additional_reloc_count
;
12780 o
->reloc_count
+= reloc_count
;
12782 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12786 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12787 esdo
->rel
.count
+= additional_reloc_count
;
12789 if (esdi
->rela
.hdr
)
12791 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12792 esdo
->rela
.count
+= additional_reloc_count
;
12798 esdo
->rela
.count
+= reloc_count
;
12800 esdo
->rel
.count
+= reloc_count
;
12804 if (o
->reloc_count
> 0)
12805 o
->flags
|= SEC_RELOC
;
12808 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12809 set it (this is probably a bug) and if it is set
12810 assign_section_numbers will create a reloc section. */
12811 o
->flags
&=~ SEC_RELOC
;
12814 /* If the SEC_ALLOC flag is not set, force the section VMA to
12815 zero. This is done in elf_fake_sections as well, but forcing
12816 the VMA to 0 here will ensure that relocs against these
12817 sections are handled correctly. */
12818 if ((o
->flags
& SEC_ALLOC
) == 0
12819 && ! o
->user_set_vma
)
12823 if (! bfd_link_relocatable (info
) && merged
)
12824 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12826 /* Figure out the file positions for everything but the symbol table
12827 and the relocs. We set symcount to force assign_section_numbers
12828 to create a symbol table. */
12829 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12830 BFD_ASSERT (! abfd
->output_has_begun
);
12831 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12834 /* Set sizes, and assign file positions for reloc sections. */
12835 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12837 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12838 if ((o
->flags
& SEC_RELOC
) != 0)
12841 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12845 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12849 /* _bfd_elf_compute_section_file_positions makes temporary use
12850 of target_index. Reset it. */
12851 o
->target_index
= 0;
12853 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12854 to count upwards while actually outputting the relocations. */
12855 esdo
->rel
.count
= 0;
12856 esdo
->rela
.count
= 0;
12858 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12859 && !bfd_section_is_ctf (o
))
12861 /* Cache the section contents so that they can be compressed
12862 later. Use bfd_malloc since it will be freed by
12863 bfd_compress_section_contents. */
12864 unsigned char *contents
= esdo
->this_hdr
.contents
;
12865 if (contents
!= NULL
)
12868 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12869 if (contents
== NULL
)
12871 esdo
->this_hdr
.contents
= contents
;
12875 /* We have now assigned file positions for all the sections except .symtab,
12876 .strtab, and non-loaded reloc and compressed debugging sections. We start
12877 the .symtab section at the current file position, and write directly to it.
12878 We build the .strtab section in memory. */
12879 abfd
->symcount
= 0;
12880 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12881 /* sh_name is set in prep_headers. */
12882 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12883 /* sh_flags, sh_addr and sh_size all start off zero. */
12884 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12885 /* sh_link is set in assign_section_numbers. */
12886 /* sh_info is set below. */
12887 /* sh_offset is set just below. */
12888 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12890 if (max_sym_count
< 20)
12891 max_sym_count
= 20;
12892 htab
->strtabsize
= max_sym_count
;
12893 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12894 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12895 if (htab
->strtab
== NULL
)
12897 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12899 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12900 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12902 if (info
->strip
!= strip_all
|| emit_relocs
)
12904 file_ptr off
= elf_next_file_pos (abfd
);
12906 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
12908 /* Note that at this point elf_next_file_pos (abfd) is
12909 incorrect. We do not yet know the size of the .symtab section.
12910 We correct next_file_pos below, after we do know the size. */
12912 /* Start writing out the symbol table. The first symbol is always a
12914 elfsym
.st_value
= 0;
12915 elfsym
.st_size
= 0;
12916 elfsym
.st_info
= 0;
12917 elfsym
.st_other
= 0;
12918 elfsym
.st_shndx
= SHN_UNDEF
;
12919 elfsym
.st_target_internal
= 0;
12920 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12921 bfd_und_section_ptr
, NULL
) != 1)
12924 /* Output a symbol for each section if asked or they are used for
12925 relocs. These symbols usually have no names. We store the
12926 index of each one in the index field of the section, so that
12927 we can find it again when outputting relocs. */
12929 if (bfd_keep_unused_section_symbols (abfd
) || emit_relocs
)
12931 bool name_local_sections
12932 = (bed
->elf_backend_name_local_section_symbols
12933 && bed
->elf_backend_name_local_section_symbols (abfd
));
12934 const char *name
= NULL
;
12936 elfsym
.st_size
= 0;
12937 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12938 elfsym
.st_other
= 0;
12939 elfsym
.st_value
= 0;
12940 elfsym
.st_target_internal
= 0;
12941 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12943 o
= bfd_section_from_elf_index (abfd
, i
);
12946 o
->target_index
= bfd_get_symcount (abfd
);
12947 elfsym
.st_shndx
= i
;
12948 if (!bfd_link_relocatable (info
))
12949 elfsym
.st_value
= o
->vma
;
12950 if (name_local_sections
)
12952 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12960 /* On some targets like Irix 5 the symbol split between local and global
12961 ones recorded in the sh_info field needs to be done between section
12962 and all other symbols. */
12963 if (bed
->elf_backend_elfsym_local_is_section
12964 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12965 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12967 /* Allocate some memory to hold information read in from the input
12969 if (max_contents_size
!= 0)
12971 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12972 if (flinfo
.contents
== NULL
)
12976 if (max_external_reloc_size
!= 0)
12978 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12979 if (flinfo
.external_relocs
== NULL
)
12983 if (max_internal_reloc_count
!= 0)
12985 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12986 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12987 if (flinfo
.internal_relocs
== NULL
)
12991 if (max_sym_count
!= 0)
12993 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12994 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12995 if (flinfo
.external_syms
== NULL
)
12998 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12999 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
13000 if (flinfo
.internal_syms
== NULL
)
13003 amt
= max_sym_count
* sizeof (long);
13004 flinfo
.indices
= (long int *) bfd_malloc (amt
);
13005 if (flinfo
.indices
== NULL
)
13008 amt
= max_sym_count
* sizeof (asection
*);
13009 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
13010 if (flinfo
.sections
== NULL
)
13014 if (max_sym_shndx_count
!= 0)
13016 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
13017 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
13018 if (flinfo
.locsym_shndx
== NULL
)
13024 bfd_vma base
, end
= 0; /* Both bytes. */
13027 for (sec
= htab
->tls_sec
;
13028 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
13031 bfd_size_type size
= sec
->size
;
13032 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
13035 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
13037 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
13040 size
= ord
->offset
* opb
+ ord
->size
;
13042 end
= sec
->vma
+ size
/ opb
;
13044 base
= htab
->tls_sec
->vma
;
13045 /* Only align end of TLS section if static TLS doesn't have special
13046 alignment requirements. */
13047 if (bed
->static_tls_alignment
== 1)
13048 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
13049 htab
->tls_size
= end
- base
;
13052 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
13055 /* Finish relative relocations here after regular symbol processing
13056 is finished if DT_RELR is enabled. */
13057 if (info
->enable_dt_relr
13058 && bed
->finish_relative_relocs
13059 && !bed
->finish_relative_relocs (info
))
13060 info
->callbacks
->einfo
13061 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd
);
13063 /* Since ELF permits relocations to be against local symbols, we
13064 must have the local symbols available when we do the relocations.
13065 Since we would rather only read the local symbols once, and we
13066 would rather not keep them in memory, we handle all the
13067 relocations for a single input file at the same time.
13069 Unfortunately, there is no way to know the total number of local
13070 symbols until we have seen all of them, and the local symbol
13071 indices precede the global symbol indices. This means that when
13072 we are generating relocatable output, and we see a reloc against
13073 a global symbol, we can not know the symbol index until we have
13074 finished examining all the local symbols to see which ones we are
13075 going to output. To deal with this, we keep the relocations in
13076 memory, and don't output them until the end of the link. This is
13077 an unfortunate waste of memory, but I don't see a good way around
13078 it. Fortunately, it only happens when performing a relocatable
13079 link, which is not the common case. FIXME: If keep_memory is set
13080 we could write the relocs out and then read them again; I don't
13081 know how bad the memory loss will be. */
13083 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13084 sub
->output_has_begun
= false;
13085 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
13087 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
13089 if (p
->type
== bfd_indirect_link_order
13090 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
13091 == bfd_target_elf_flavour
)
13092 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
13094 if (! sub
->output_has_begun
)
13096 if (! elf_link_input_bfd (&flinfo
, sub
))
13098 sub
->output_has_begun
= true;
13101 else if (p
->type
== bfd_section_reloc_link_order
13102 || p
->type
== bfd_symbol_reloc_link_order
)
13104 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
13109 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
13111 if (p
->type
== bfd_indirect_link_order
13112 && (bfd_get_flavour (sub
)
13113 == bfd_target_elf_flavour
)
13114 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
13115 != bed
->s
->elfclass
))
13117 const char *iclass
, *oclass
;
13119 switch (bed
->s
->elfclass
)
13121 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
13122 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
13123 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
13127 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
13129 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
13130 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
13131 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
13135 bfd_set_error (bfd_error_wrong_format
);
13137 /* xgettext:c-format */
13138 (_("%pB: file class %s incompatible with %s"),
13139 sub
, iclass
, oclass
);
13148 /* Free symbol buffer if needed. */
13149 if (!info
->reduce_memory_overheads
)
13151 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13152 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
13154 free (elf_tdata (sub
)->symbuf
);
13155 elf_tdata (sub
)->symbuf
= NULL
;
13161 /* Output any global symbols that got converted to local in a
13162 version script or due to symbol visibility. We do this in a
13163 separate step since ELF requires all local symbols to appear
13164 prior to any global symbols. FIXME: We should only do this if
13165 some global symbols were, in fact, converted to become local.
13166 FIXME: Will this work correctly with the Irix 5 linker? */
13167 eoinfo
.failed
= false;
13168 eoinfo
.flinfo
= &flinfo
;
13169 eoinfo
.localsyms
= true;
13170 eoinfo
.file_sym_done
= false;
13171 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
13175 goto return_local_hash_table
;
13178 /* If backend needs to output some local symbols not present in the hash
13179 table, do it now. */
13180 if (bed
->elf_backend_output_arch_local_syms
)
13182 if (! ((*bed
->elf_backend_output_arch_local_syms
)
13183 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
13186 goto return_local_hash_table
;
13190 /* That wrote out all the local symbols. Finish up the symbol table
13191 with the global symbols. Even if we want to strip everything we
13192 can, we still need to deal with those global symbols that got
13193 converted to local in a version script. */
13195 /* The sh_info field records the index of the first non local symbol. */
13196 if (!symtab_hdr
->sh_info
)
13197 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
13200 && htab
->dynsym
!= NULL
13201 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
13203 Elf_Internal_Sym sym
;
13204 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
13206 o
= htab
->dynsym
->output_section
;
13207 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
13209 /* Write out the section symbols for the output sections. */
13210 if (bfd_link_pic (info
)
13211 || htab
->is_relocatable_executable
)
13217 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
13219 sym
.st_target_internal
= 0;
13221 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
13227 dynindx
= elf_section_data (s
)->dynindx
;
13230 indx
= elf_section_data (s
)->this_idx
;
13231 BFD_ASSERT (indx
> 0);
13232 sym
.st_shndx
= indx
;
13233 if (! check_dynsym (abfd
, &sym
))
13236 goto return_local_hash_table
;
13238 sym
.st_value
= s
->vma
;
13239 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
13241 /* Inform the linker of the addition of this symbol. */
13243 if (info
->callbacks
->ctf_new_dynsym
)
13244 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
13246 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
13250 /* Write out the local dynsyms. */
13251 if (htab
->dynlocal
)
13253 struct elf_link_local_dynamic_entry
*e
;
13254 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
13259 /* Copy the internal symbol and turn off visibility.
13260 Note that we saved a word of storage and overwrote
13261 the original st_name with the dynstr_index. */
13263 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
13264 sym
.st_shndx
= SHN_UNDEF
;
13266 s
= bfd_section_from_elf_index (e
->input_bfd
,
13269 && s
->output_section
!= NULL
13270 && elf_section_data (s
->output_section
) != NULL
)
13273 elf_section_data (s
->output_section
)->this_idx
;
13274 if (! check_dynsym (abfd
, &sym
))
13277 goto return_local_hash_table
;
13279 sym
.st_value
= (s
->output_section
->vma
13281 + e
->isym
.st_value
);
13284 /* Inform the linker of the addition of this symbol. */
13286 if (info
->callbacks
->ctf_new_dynsym
)
13287 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
13289 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
13290 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
13295 /* We get the global symbols from the hash table. */
13296 eoinfo
.failed
= false;
13297 eoinfo
.localsyms
= false;
13298 eoinfo
.flinfo
= &flinfo
;
13299 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
13303 goto return_local_hash_table
;
13306 /* If backend needs to output some symbols not present in the hash
13307 table, do it now. */
13308 if (bed
->elf_backend_output_arch_syms
13309 && (info
->strip
!= strip_all
|| emit_relocs
))
13311 if (! ((*bed
->elf_backend_output_arch_syms
)
13312 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
13315 goto return_local_hash_table
;
13319 /* Finalize the .strtab section. */
13320 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
13322 /* Swap out the .strtab section. */
13323 if (!elf_link_swap_symbols_out (&flinfo
))
13326 goto return_local_hash_table
;
13329 /* Now we know the size of the symtab section. */
13330 if (bfd_get_symcount (abfd
) > 0)
13332 /* Finish up and write out the symbol string table (.strtab)
13334 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
13335 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
13337 if (elf_symtab_shndx_list (abfd
))
13339 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
13341 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
13343 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
13344 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
13345 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
13346 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
13347 symtab_shndx_hdr
->sh_size
= amt
;
13349 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
13352 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
13353 || (bfd_write (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
13356 goto return_local_hash_table
;
13361 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
13362 /* sh_name was set in prep_headers. */
13363 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
13364 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
13365 symstrtab_hdr
->sh_addr
= 0;
13366 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
13367 symstrtab_hdr
->sh_entsize
= 0;
13368 symstrtab_hdr
->sh_link
= 0;
13369 symstrtab_hdr
->sh_info
= 0;
13370 /* sh_offset is set just below. */
13371 symstrtab_hdr
->sh_addralign
= 1;
13373 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
13375 elf_next_file_pos (abfd
) = off
;
13377 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
13378 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
13381 goto return_local_hash_table
;
13385 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
13387 _bfd_error_handler (_("%pB: failed to generate import library"),
13388 info
->out_implib_bfd
);
13390 goto return_local_hash_table
;
13393 /* Adjust the relocs to have the correct symbol indices. */
13394 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
13396 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
13399 if ((o
->flags
& SEC_RELOC
) == 0)
13402 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
13403 if (esdo
->rel
.hdr
!= NULL
13404 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
13407 goto return_local_hash_table
;
13409 if (esdo
->rela
.hdr
!= NULL
13410 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
13413 goto return_local_hash_table
;
13416 /* Set the reloc_count field to 0 to prevent write_relocs from
13417 trying to swap the relocs out itself. */
13418 o
->reloc_count
= 0;
13422 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
13423 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
13426 if (htab
->srelrdyn
!= NULL
13427 && htab
->srelrdyn
->output_section
!= NULL
13428 && htab
->srelrdyn
->size
!= 0)
13430 asection
*s
= htab
->srelrdyn
->output_section
;
13431 relr_entsize
= elf_section_data (s
)->this_hdr
.sh_entsize
;
13432 if (relr_entsize
== 0)
13434 relr_entsize
= bed
->s
->arch_size
/ 8;
13435 elf_section_data (s
)->this_hdr
.sh_entsize
= relr_entsize
;
13439 /* If we are linking against a dynamic object, or generating a
13440 shared library, finish up the dynamic linking information. */
13443 bfd_byte
*dyncon
, *dynconend
;
13445 /* Fix up .dynamic entries. */
13447 BFD_ASSERT (o
!= NULL
);
13449 dyncon
= o
->contents
;
13450 dynconend
= PTR_ADD (o
->contents
, o
->size
);
13451 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13453 Elf_Internal_Dyn dyn
;
13456 bfd_size_type sh_size
;
13459 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13466 if (relativecount
!= 0)
13468 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
13470 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
13471 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
13473 if (dyn
.d_tag
!= DT_NULL
13474 && dynconend
- dyncon
>= bed
->s
->sizeof_dyn
)
13476 dyn
.d_un
.d_val
= relativecount
;
13482 if (relr_entsize
!= 0)
13484 if (dynconend
- dyncon
>= 3 * bed
->s
->sizeof_dyn
)
13486 asection
*s
= htab
->srelrdyn
;
13487 dyn
.d_tag
= DT_RELR
;
13489 = s
->output_section
->vma
+ s
->output_offset
;
13490 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13491 dyncon
+= bed
->s
->sizeof_dyn
;
13493 dyn
.d_tag
= DT_RELRSZ
;
13494 dyn
.d_un
.d_val
= s
->size
;
13495 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13496 dyncon
+= bed
->s
->sizeof_dyn
;
13498 dyn
.d_tag
= DT_RELRENT
;
13499 dyn
.d_un
.d_val
= relr_entsize
;
13508 name
= info
->init_function
;
13511 name
= info
->fini_function
;
13514 struct elf_link_hash_entry
*h
;
13516 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
13518 && (h
->root
.type
== bfd_link_hash_defined
13519 || h
->root
.type
== bfd_link_hash_defweak
))
13521 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
13522 o
= h
->root
.u
.def
.section
;
13523 if (o
->output_section
!= NULL
)
13524 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
13525 + o
->output_offset
);
13528 /* The symbol is imported from another shared
13529 library and does not apply to this one. */
13530 dyn
.d_un
.d_ptr
= 0;
13537 case DT_PREINIT_ARRAYSZ
:
13538 name
= ".preinit_array";
13540 case DT_INIT_ARRAYSZ
:
13541 name
= ".init_array";
13543 case DT_FINI_ARRAYSZ
:
13544 name
= ".fini_array";
13546 o
= bfd_get_section_by_name (abfd
, name
);
13550 (_("could not find section %s"), name
);
13555 (_("warning: %s section has zero size"), name
);
13556 dyn
.d_un
.d_val
= o
->size
;
13559 case DT_PREINIT_ARRAY
:
13560 name
= ".preinit_array";
13562 case DT_INIT_ARRAY
:
13563 name
= ".init_array";
13565 case DT_FINI_ARRAY
:
13566 name
= ".fini_array";
13568 o
= bfd_get_section_by_name (abfd
, name
);
13575 name
= ".gnu.hash";
13584 name
= ".gnu.version_d";
13587 name
= ".gnu.version_r";
13590 name
= ".gnu.version";
13592 o
= bfd_get_linker_section (dynobj
, name
);
13594 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13597 (_("could not find section %s"), name
);
13600 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13603 (_("warning: section '%s' is being made into a note"), name
);
13604 bfd_set_error (bfd_error_nonrepresentable_section
);
13607 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13614 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13620 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13622 Elf_Internal_Shdr
*hdr
;
13624 hdr
= elf_elfsections (abfd
)[i
];
13625 if (hdr
->sh_type
== type
13626 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13628 sh_size
+= hdr
->sh_size
;
13630 || sh_addr
> hdr
->sh_addr
)
13631 sh_addr
= hdr
->sh_addr
;
13635 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13637 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13639 /* Don't count procedure linkage table relocs in the
13640 overall reloc count. */
13641 sh_size
-= htab
->srelplt
->size
;
13643 /* If the size is zero, make the address zero too.
13644 This is to avoid a glibc bug. If the backend
13645 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13646 zero, then we'll put DT_RELA at the end of
13647 DT_JMPREL. glibc will interpret the end of
13648 DT_RELA matching the end of DT_JMPREL as the
13649 case where DT_RELA includes DT_JMPREL, and for
13650 LD_BIND_NOW will decide that processing DT_RELA
13651 will process the PLT relocs too. Net result:
13652 No PLT relocs applied. */
13655 /* If .rela.plt is the first .rela section, exclude
13656 it from DT_RELA. */
13657 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13658 + htab
->srelplt
->output_offset
) * opb
)
13659 sh_addr
+= htab
->srelplt
->size
;
13662 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13663 dyn
.d_un
.d_val
= sh_size
;
13665 dyn
.d_un
.d_ptr
= sh_addr
;
13668 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13672 /* If we have created any dynamic sections, then output them. */
13673 if (dynobj
!= NULL
)
13675 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13678 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13679 if (bfd_link_textrel_check (info
)
13680 && (o
= htab
->dynamic
) != NULL
13683 bfd_byte
*dyncon
, *dynconend
;
13685 dyncon
= o
->contents
;
13686 dynconend
= o
->contents
+ o
->size
;
13687 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13689 Elf_Internal_Dyn dyn
;
13691 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13693 if (dyn
.d_tag
== DT_TEXTREL
)
13695 if (info
->textrel_check
== textrel_check_error
)
13696 info
->callbacks
->einfo
13697 (_("%P%X: read-only segment has dynamic relocations\n"));
13698 else if (bfd_link_dll (info
))
13699 info
->callbacks
->einfo
13700 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13701 else if (bfd_link_pde (info
))
13702 info
->callbacks
->einfo
13703 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13705 info
->callbacks
->einfo
13706 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13712 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13714 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13716 || o
->output_section
== bfd_abs_section_ptr
)
13718 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13720 /* At this point, we are only interested in sections
13721 created by _bfd_elf_link_create_dynamic_sections. */
13724 if (htab
->stab_info
.stabstr
== o
)
13726 if (htab
->eh_info
.hdr_sec
== o
)
13728 if (strcmp (o
->name
, ".dynstr") != 0)
13730 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13731 * bfd_octets_per_byte (abfd
, o
));
13732 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13733 o
->contents
, octets
, o
->size
))
13738 /* The contents of the .dynstr section are actually in a
13742 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13743 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13744 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13750 if (!info
->resolve_section_groups
)
13752 bool failed
= false;
13754 BFD_ASSERT (bfd_link_relocatable (info
));
13755 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13760 /* If we have optimized stabs strings, output them. */
13761 if (htab
->stab_info
.stabstr
!= NULL
)
13763 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13767 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13770 if (! _bfd_elf_write_section_sframe (abfd
, info
))
13773 if (info
->callbacks
->emit_ctf
)
13774 info
->callbacks
->emit_ctf ();
13776 elf_final_link_free (abfd
, &flinfo
);
13780 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13781 if (contents
== NULL
)
13783 /* Bail out and fail. */
13785 goto return_local_hash_table
;
13787 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13788 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13792 return_local_hash_table
:
13793 if (info
->unique_symbol
)
13794 bfd_hash_table_free (&flinfo
.local_hash_table
);
13798 elf_final_link_free (abfd
, &flinfo
);
13800 goto return_local_hash_table
;
13803 /* Initialize COOKIE for input bfd ABFD. */
13806 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13807 struct bfd_link_info
*info
, bfd
*abfd
,
13810 Elf_Internal_Shdr
*symtab_hdr
;
13811 const struct elf_backend_data
*bed
;
13813 bed
= get_elf_backend_data (abfd
);
13814 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13816 cookie
->abfd
= abfd
;
13817 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13818 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13819 if (cookie
->bad_symtab
)
13821 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13822 cookie
->extsymoff
= 0;
13826 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13827 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13830 if (bed
->s
->arch_size
== 32)
13831 cookie
->r_sym_shift
= 8;
13833 cookie
->r_sym_shift
= 32;
13835 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13836 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13838 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13839 cookie
->locsymcount
, 0,
13841 if (cookie
->locsyms
== NULL
)
13843 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13846 if (keep_memory
|| _bfd_elf_link_keep_memory (info
))
13848 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13849 info
->cache_size
+= (cookie
->locsymcount
13850 * sizeof (Elf_External_Sym_Shndx
));
13856 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13859 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13861 Elf_Internal_Shdr
*symtab_hdr
;
13863 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13864 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13865 free (cookie
->locsyms
);
13868 /* Initialize the relocation information in COOKIE for input section SEC
13869 of input bfd ABFD. */
13872 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13873 struct bfd_link_info
*info
, bfd
*abfd
,
13874 asection
*sec
, bool keep_memory
)
13876 if (sec
->reloc_count
== 0)
13878 cookie
->rels
= NULL
;
13879 cookie
->relend
= NULL
;
13883 cookie
->rels
= _bfd_elf_link_info_read_relocs
13884 (abfd
, info
, sec
, NULL
, NULL
,
13885 keep_memory
|| _bfd_elf_link_keep_memory (info
));
13886 if (cookie
->rels
== NULL
)
13888 cookie
->rel
= cookie
->rels
;
13889 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13891 cookie
->rel
= cookie
->rels
;
13895 /* Free the memory allocated by init_reloc_cookie_rels,
13899 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13902 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13903 free (cookie
->rels
);
13906 /* Initialize the whole of COOKIE for input section SEC. */
13909 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13910 struct bfd_link_info
*info
,
13911 asection
*sec
, bool keep_memory
)
13913 if (!init_reloc_cookie (cookie
, info
, sec
->owner
, keep_memory
))
13915 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
,
13921 fini_reloc_cookie (cookie
, sec
->owner
);
13926 /* Free the memory allocated by init_reloc_cookie_for_section,
13930 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13933 fini_reloc_cookie_rels (cookie
, sec
);
13934 fini_reloc_cookie (cookie
, sec
->owner
);
13937 /* Garbage collect unused sections. */
13939 /* Default gc_mark_hook. */
13942 _bfd_elf_gc_mark_hook (asection
*sec
,
13943 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13944 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13945 struct elf_link_hash_entry
*h
,
13946 Elf_Internal_Sym
*sym
)
13950 switch (h
->root
.type
)
13952 case bfd_link_hash_defined
:
13953 case bfd_link_hash_defweak
:
13954 return h
->root
.u
.def
.section
;
13956 case bfd_link_hash_common
:
13957 return h
->root
.u
.c
.p
->section
;
13964 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13969 /* Return the debug definition section. */
13972 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13973 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13974 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13975 struct elf_link_hash_entry
*h
,
13976 Elf_Internal_Sym
*sym
)
13980 /* Return the global debug definition section. */
13981 if ((h
->root
.type
== bfd_link_hash_defined
13982 || h
->root
.type
== bfd_link_hash_defweak
)
13983 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13984 return h
->root
.u
.def
.section
;
13988 /* Return the local debug definition section. */
13989 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13991 if (isec
!= NULL
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13998 /* COOKIE->rel describes a relocation against section SEC, which is
13999 a section we've decided to keep. Return the section that contains
14000 the relocation symbol, or NULL if no section contains it. */
14003 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
14004 elf_gc_mark_hook_fn gc_mark_hook
,
14005 struct elf_reloc_cookie
*cookie
,
14008 unsigned long r_symndx
;
14009 struct elf_link_hash_entry
*h
, *hw
;
14011 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
14012 if (r_symndx
== STN_UNDEF
)
14015 if (r_symndx
>= cookie
->locsymcount
14016 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14020 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
14023 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
14027 while (h
->root
.type
== bfd_link_hash_indirect
14028 || h
->root
.type
== bfd_link_hash_warning
)
14029 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14031 was_marked
= h
->mark
;
14033 /* Keep all aliases of the symbol too. If an object symbol
14034 needs to be copied into .dynbss then all of its aliases
14035 should be present as dynamic symbols, not just the one used
14036 on the copy relocation. */
14038 while (hw
->is_weakalias
)
14044 if (!was_marked
&& h
->start_stop
&& !h
->root
.ldscript_def
)
14046 if (info
->start_stop_gc
)
14049 /* To work around a glibc bug, mark XXX input sections
14050 when there is a reference to __start_XXX or __stop_XXX
14052 else if (start_stop
!= NULL
)
14054 asection
*s
= h
->u2
.start_stop_section
;
14055 *start_stop
= true;
14060 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
14063 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
14064 &cookie
->locsyms
[r_symndx
]);
14067 /* COOKIE->rel describes a relocation against section SEC, which is
14068 a section we've decided to keep. Mark the section that contains
14069 the relocation symbol. */
14072 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
14074 elf_gc_mark_hook_fn gc_mark_hook
,
14075 struct elf_reloc_cookie
*cookie
)
14078 bool start_stop
= false;
14080 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
14081 while (rsec
!= NULL
)
14083 if (!rsec
->gc_mark
)
14085 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
14086 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
14088 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
14093 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
14098 /* The mark phase of garbage collection. For a given section, mark
14099 it and any sections in this section's group, and all the sections
14100 which define symbols to which it refers. */
14103 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
14105 elf_gc_mark_hook_fn gc_mark_hook
)
14108 asection
*group_sec
, *eh_frame
;
14112 /* Mark all the sections in the group. */
14113 group_sec
= elf_section_data (sec
)->next_in_group
;
14114 if (group_sec
&& !group_sec
->gc_mark
)
14115 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
14118 /* Look through the section relocs. */
14120 eh_frame
= elf_eh_frame_section (sec
->owner
);
14121 if ((sec
->flags
& SEC_RELOC
) != 0
14122 && sec
->reloc_count
> 0
14123 && sec
!= eh_frame
)
14125 struct elf_reloc_cookie cookie
;
14127 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
, false))
14131 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
14132 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
14137 fini_reloc_cookie_for_section (&cookie
, sec
);
14141 if (ret
&& eh_frame
&& elf_fde_list (sec
))
14143 struct elf_reloc_cookie cookie
;
14145 /* NB: When --no-keep-memory is used, the symbol table and
14146 relocation info for eh_frame are freed after they are retrieved
14147 for each text section in the input object. If an input object
14148 has many text sections, the same data is retrieved and freed
14149 many times which can take a very long time. Always keep the
14150 symbol table and relocation info for eh_frame to avoid it. */
14151 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
,
14156 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
14157 gc_mark_hook
, &cookie
))
14159 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
14163 eh_frame
= elf_section_eh_frame_entry (sec
);
14164 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
14165 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
14171 /* Scan and mark sections in a special or debug section group. */
14174 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
14176 /* Point to first section of section group. */
14178 /* Used to iterate the section group. */
14181 bool is_special_grp
= true;
14182 bool is_debug_grp
= true;
14184 /* First scan to see if group contains any section other than debug
14185 and special section. */
14186 ssec
= msec
= elf_next_in_group (grp
);
14189 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
14190 is_debug_grp
= false;
14192 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
14193 is_special_grp
= false;
14195 msec
= elf_next_in_group (msec
);
14197 while (msec
!= ssec
);
14199 /* If this is a pure debug section group or pure special section group,
14200 keep all sections in this group. */
14201 if (is_debug_grp
|| is_special_grp
)
14206 msec
= elf_next_in_group (msec
);
14208 while (msec
!= ssec
);
14212 /* Keep debug and special sections. */
14215 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
14216 elf_gc_mark_hook_fn mark_hook
)
14220 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14224 bool debug_frag_seen
;
14225 bool has_kept_debug_info
;
14227 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14229 isec
= ibfd
->sections
;
14230 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14233 /* Ensure all linker created sections are kept,
14234 see if any other section is already marked,
14235 and note if we have any fragmented debug sections. */
14236 debug_frag_seen
= some_kept
= has_kept_debug_info
= false;
14237 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14239 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
14241 else if (isec
->gc_mark
14242 && (isec
->flags
& SEC_ALLOC
) != 0
14243 && elf_section_type (isec
) != SHT_NOTE
)
14247 /* Since all sections, except for backend specific ones,
14248 have been garbage collected, call mark_hook on this
14249 section if any of its linked-to sections is marked. */
14250 asection
*linked_to_sec
;
14251 for (linked_to_sec
= elf_linked_to_section (isec
);
14252 linked_to_sec
!= NULL
&& !linked_to_sec
->linker_mark
;
14253 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
14255 if (linked_to_sec
->gc_mark
)
14257 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
14261 linked_to_sec
->linker_mark
= 1;
14263 for (linked_to_sec
= elf_linked_to_section (isec
);
14264 linked_to_sec
!= NULL
&& linked_to_sec
->linker_mark
;
14265 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
14266 linked_to_sec
->linker_mark
= 0;
14269 if (!debug_frag_seen
14270 && (isec
->flags
& SEC_DEBUGGING
)
14271 && startswith (isec
->name
, ".debug_line."))
14272 debug_frag_seen
= true;
14273 else if (strcmp (bfd_section_name (isec
),
14274 "__patchable_function_entries") == 0
14275 && elf_linked_to_section (isec
) == NULL
)
14276 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
14277 "need linked-to section "
14278 "for --gc-sections\n"),
14279 isec
->owner
, isec
);
14282 /* If no non-note alloc section in this file will be kept, then
14283 we can toss out the debug and special sections. */
14287 /* Keep debug and special sections like .comment when they are
14288 not part of a group. Also keep section groups that contain
14289 just debug sections or special sections. NB: Sections with
14290 linked-to section has been handled above. */
14291 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14293 if ((isec
->flags
& SEC_GROUP
) != 0)
14294 _bfd_elf_gc_mark_debug_special_section_group (isec
);
14295 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
14296 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
14297 && elf_next_in_group (isec
) == NULL
14298 && elf_linked_to_section (isec
) == NULL
)
14300 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
14301 has_kept_debug_info
= true;
14304 /* Look for CODE sections which are going to be discarded,
14305 and find and discard any fragmented debug sections which
14306 are associated with that code section. */
14307 if (debug_frag_seen
)
14308 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14309 if ((isec
->flags
& SEC_CODE
) != 0
14310 && isec
->gc_mark
== 0)
14315 ilen
= strlen (isec
->name
);
14317 /* Association is determined by the name of the debug
14318 section containing the name of the code section as
14319 a suffix. For example .debug_line.text.foo is a
14320 debug section associated with .text.foo. */
14321 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
14325 if (dsec
->gc_mark
== 0
14326 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
14329 dlen
= strlen (dsec
->name
);
14332 && strncmp (dsec
->name
+ (dlen
- ilen
),
14333 isec
->name
, ilen
) == 0)
14338 /* Mark debug sections referenced by kept debug sections. */
14339 if (has_kept_debug_info
)
14340 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14342 && (isec
->flags
& SEC_DEBUGGING
) != 0)
14343 if (!_bfd_elf_gc_mark (info
, isec
,
14344 elf_gc_mark_debug_section
))
14351 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
14354 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14356 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14360 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14361 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
14362 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14365 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14368 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14370 /* When any section in a section group is kept, we keep all
14371 sections in the section group. If the first member of
14372 the section group is excluded, we will also exclude the
14374 if (o
->flags
& SEC_GROUP
)
14376 asection
*first
= elf_next_in_group (o
);
14377 o
->gc_mark
= first
->gc_mark
;
14383 /* Skip sweeping sections already excluded. */
14384 if (o
->flags
& SEC_EXCLUDE
)
14387 /* Since this is early in the link process, it is simple
14388 to remove a section from the output. */
14389 o
->flags
|= SEC_EXCLUDE
;
14391 if (info
->print_gc_sections
&& o
->size
!= 0)
14392 /* xgettext:c-format */
14393 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14401 /* Propagate collected vtable information. This is called through
14402 elf_link_hash_traverse. */
14405 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
14407 /* Those that are not vtables. */
14409 || h
->u2
.vtable
== NULL
14410 || h
->u2
.vtable
->parent
== NULL
)
14413 /* Those vtables that do not have parents, we cannot merge. */
14414 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
14417 /* If we've already been done, exit. */
14418 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
14421 /* Make sure the parent's table is up to date. */
14422 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
14424 if (h
->u2
.vtable
->used
== NULL
)
14426 /* None of this table's entries were referenced. Re-use the
14428 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14429 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
14436 /* Or the parent's entries into ours. */
14437 cu
= h
->u2
.vtable
->used
;
14439 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14442 const struct elf_backend_data
*bed
;
14443 unsigned int log_file_align
;
14445 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
14446 log_file_align
= bed
->s
->log_file_align
;
14447 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
14461 struct link_info_ok
14463 struct bfd_link_info
*info
;
14468 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
,
14472 bfd_vma hstart
, hend
;
14473 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
14474 const struct elf_backend_data
*bed
;
14475 unsigned int log_file_align
;
14476 struct link_info_ok
*info
= (struct link_info_ok
*) ptr
;
14478 /* Take care of both those symbols that do not describe vtables as
14479 well as those that are not loaded. */
14481 || h
->u2
.vtable
== NULL
14482 || h
->u2
.vtable
->parent
== NULL
)
14485 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
14486 || h
->root
.type
== bfd_link_hash_defweak
);
14488 sec
= h
->root
.u
.def
.section
;
14489 hstart
= h
->root
.u
.def
.value
;
14490 hend
= hstart
+ h
->size
;
14492 relstart
= _bfd_elf_link_info_read_relocs (sec
->owner
, info
->info
,
14493 sec
, NULL
, NULL
, true);
14495 return info
->ok
= false;
14496 bed
= get_elf_backend_data (sec
->owner
);
14497 log_file_align
= bed
->s
->log_file_align
;
14499 relend
= relstart
+ sec
->reloc_count
;
14501 for (rel
= relstart
; rel
< relend
; ++rel
)
14502 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
14504 /* If the entry is in use, do nothing. */
14505 if (h
->u2
.vtable
->used
14506 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
14508 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
14509 if (h
->u2
.vtable
->used
[entry
])
14512 /* Otherwise, kill it. */
14513 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
14519 /* Mark sections containing dynamically referenced symbols. When
14520 building shared libraries, we must assume that any visible symbol is
14524 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
14526 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
14527 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
14529 if ((h
->root
.type
== bfd_link_hash_defined
14530 || h
->root
.type
== bfd_link_hash_defweak
)
14532 || h
->root
.ldscript_def
14533 || !info
->start_stop_gc
)
14534 && ((h
->ref_dynamic
&& !h
->forced_local
)
14535 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
14536 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
14537 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
14538 && (!bfd_link_executable (info
)
14539 || info
->gc_keep_exported
14540 || info
->export_dynamic
14543 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
14544 && (h
->versioned
>= versioned
14545 || !bfd_hide_sym_by_version (info
->version_info
,
14546 h
->root
.root
.string
)))))
14547 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14552 /* Keep all sections containing symbols undefined on the command-line,
14553 and the section containing the entry symbol. */
14556 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
14558 struct bfd_sym_chain
*sym
;
14560 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
14562 struct elf_link_hash_entry
*h
;
14564 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
14565 false, false, false);
14568 && (h
->root
.type
== bfd_link_hash_defined
14569 || h
->root
.type
== bfd_link_hash_defweak
)
14570 && !bfd_is_const_section (h
->root
.u
.def
.section
))
14571 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14576 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
14577 struct bfd_link_info
*info
)
14579 bfd
*ibfd
= info
->input_bfds
;
14581 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14584 struct elf_reloc_cookie cookie
;
14586 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14588 sec
= ibfd
->sections
;
14589 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14592 if (!init_reloc_cookie (&cookie
, info
, ibfd
, false))
14595 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
14597 if (startswith (bfd_section_name (sec
), ".eh_frame_entry")
14598 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
,
14601 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
14602 fini_reloc_cookie_rels (&cookie
, sec
);
14609 /* Do mark and sweep of unused sections. */
14612 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14616 elf_gc_mark_hook_fn gc_mark_hook
;
14617 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14618 struct elf_link_hash_table
*htab
;
14619 struct link_info_ok info_ok
;
14621 if (!bed
->can_gc_sections
14622 || !is_elf_hash_table (info
->hash
))
14624 _bfd_error_handler(_("warning: gc-sections option ignored"));
14628 bed
->gc_keep (info
);
14629 htab
= elf_hash_table (info
);
14631 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14632 at the .eh_frame section if we can mark the FDEs individually. */
14633 for (sub
= info
->input_bfds
;
14634 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14635 sub
= sub
->link
.next
)
14638 struct elf_reloc_cookie cookie
;
14640 sec
= sub
->sections
;
14641 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14643 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14644 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
,
14647 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14648 if (elf_section_data (sec
)->sec_info
14649 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14650 elf_eh_frame_section (sub
) = sec
;
14651 fini_reloc_cookie_for_section (&cookie
, sec
);
14652 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14656 /* Apply transitive closure to the vtable entry usage info. */
14657 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14661 /* Kill the vtable relocations that were not used. */
14662 info_ok
.info
= info
;
14664 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &info_ok
);
14668 /* Mark dynamically referenced symbols. */
14669 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14670 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14672 /* Grovel through relocs to find out who stays ... */
14673 gc_mark_hook
= bed
->gc_mark_hook
;
14674 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14678 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14679 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14680 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14684 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14687 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14688 Also treat note sections as a root, if the section is not part
14689 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14690 well as FINI_ARRAY sections for ld -r. */
14691 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14693 && (o
->flags
& SEC_EXCLUDE
) == 0
14694 && ((o
->flags
& SEC_KEEP
) != 0
14695 || (bfd_link_relocatable (info
)
14696 && ((elf_section_data (o
)->this_hdr
.sh_type
14697 == SHT_PREINIT_ARRAY
)
14698 || (elf_section_data (o
)->this_hdr
.sh_type
14700 || (elf_section_data (o
)->this_hdr
.sh_type
14701 == SHT_FINI_ARRAY
)))
14702 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14703 && elf_next_in_group (o
) == NULL
14704 && elf_linked_to_section (o
) == NULL
)
14705 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14706 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14708 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14713 /* Allow the backend to mark additional target specific sections. */
14714 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14716 /* ... and mark SEC_EXCLUDE for those that go. */
14717 return elf_gc_sweep (abfd
, info
);
14720 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14723 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14725 struct elf_link_hash_entry
*h
,
14728 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14729 struct elf_link_hash_entry
**search
, *child
;
14730 size_t extsymcount
;
14731 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14733 /* The sh_info field of the symtab header tells us where the
14734 external symbols start. We don't care about the local symbols at
14736 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14737 if (!elf_bad_symtab (abfd
))
14738 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14740 sym_hashes
= elf_sym_hashes (abfd
);
14741 sym_hashes_end
= PTR_ADD (sym_hashes
, extsymcount
);
14743 /* Hunt down the child symbol, which is in this section at the same
14744 offset as the relocation. */
14745 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14747 if ((child
= *search
) != NULL
14748 && (child
->root
.type
== bfd_link_hash_defined
14749 || child
->root
.type
== bfd_link_hash_defweak
)
14750 && child
->root
.u
.def
.section
== sec
14751 && child
->root
.u
.def
.value
== offset
)
14755 /* xgettext:c-format */
14756 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14757 abfd
, sec
, (uint64_t) offset
);
14758 bfd_set_error (bfd_error_invalid_operation
);
14762 if (!child
->u2
.vtable
)
14764 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14765 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14766 if (!child
->u2
.vtable
)
14771 /* This *should* only be the absolute section. It could potentially
14772 be that someone has defined a non-global vtable though, which
14773 would be bad. It isn't worth paging in the local symbols to be
14774 sure though; that case should simply be handled by the assembler. */
14776 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14779 child
->u2
.vtable
->parent
= h
;
14784 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14787 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14788 struct elf_link_hash_entry
*h
,
14791 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14792 unsigned int log_file_align
= bed
->s
->log_file_align
;
14796 /* xgettext:c-format */
14797 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14799 bfd_set_error (bfd_error_bad_value
);
14805 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14806 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14811 if (addend
>= h
->u2
.vtable
->size
)
14813 size_t size
, bytes
, file_align
;
14814 bool *ptr
= h
->u2
.vtable
->used
;
14816 /* While the symbol is undefined, we have to be prepared to handle
14818 file_align
= 1 << log_file_align
;
14819 if (h
->root
.type
== bfd_link_hash_undefined
)
14820 size
= addend
+ file_align
;
14824 if (addend
>= size
)
14826 /* Oops! We've got a reference past the defined end of
14827 the table. This is probably a bug -- shall we warn? */
14828 size
= addend
+ file_align
;
14831 size
= (size
+ file_align
- 1) & -file_align
;
14833 /* Allocate one extra entry for use as a "done" flag for the
14834 consolidation pass. */
14835 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bool);
14839 ptr
= (bool *) bfd_realloc (ptr
- 1, bytes
);
14845 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14847 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14851 ptr
= (bool *) bfd_zmalloc (bytes
);
14856 /* And arrange for that done flag to be at index -1. */
14857 h
->u2
.vtable
->used
= ptr
+ 1;
14858 h
->u2
.vtable
->size
= size
;
14861 h
->u2
.vtable
->used
[addend
>> log_file_align
] = true;
14866 /* Map an ELF section header flag to its corresponding string. */
14870 flagword flag_value
;
14871 } elf_flags_to_name_table
;
14873 static const elf_flags_to_name_table elf_flags_to_names
[] =
14875 { "SHF_WRITE", SHF_WRITE
},
14876 { "SHF_ALLOC", SHF_ALLOC
},
14877 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14878 { "SHF_MERGE", SHF_MERGE
},
14879 { "SHF_STRINGS", SHF_STRINGS
},
14880 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14881 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14882 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14883 { "SHF_GROUP", SHF_GROUP
},
14884 { "SHF_TLS", SHF_TLS
},
14885 { "SHF_MASKOS", SHF_MASKOS
},
14886 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14889 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14891 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14892 struct flag_info
*flaginfo
,
14895 const bfd_vma sh_flags
= elf_section_flags (section
);
14897 if (!flaginfo
->flags_initialized
)
14899 bfd
*obfd
= info
->output_bfd
;
14900 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14901 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14903 int without_hex
= 0;
14905 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14908 flagword (*lookup
) (char *);
14910 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14911 if (lookup
!= NULL
)
14913 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14917 if (tf
->with
== with_flags
)
14918 with_hex
|= hexval
;
14919 else if (tf
->with
== without_flags
)
14920 without_hex
|= hexval
;
14925 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14927 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14929 if (tf
->with
== with_flags
)
14930 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14931 else if (tf
->with
== without_flags
)
14932 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14939 info
->callbacks
->einfo
14940 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14944 flaginfo
->flags_initialized
= true;
14945 flaginfo
->only_with_flags
|= with_hex
;
14946 flaginfo
->not_with_flags
|= without_hex
;
14949 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14952 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14958 struct alloc_got_off_arg
{
14960 struct bfd_link_info
*info
;
14963 /* We need a special top-level link routine to convert got reference counts
14964 to real got offsets. */
14967 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14969 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14970 bfd
*obfd
= gofarg
->info
->output_bfd
;
14971 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14973 if (h
->got
.refcount
> 0)
14975 h
->got
.offset
= gofarg
->gotoff
;
14976 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14979 h
->got
.offset
= (bfd_vma
) -1;
14984 /* And an accompanying bit to work out final got entry offsets once
14985 we're done. Should be called from final_link. */
14988 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14989 struct bfd_link_info
*info
)
14992 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14994 struct alloc_got_off_arg gofarg
;
14996 BFD_ASSERT (abfd
== info
->output_bfd
);
14998 if (! is_elf_hash_table (info
->hash
))
15001 /* The GOT offset is relative to the .got section, but the GOT header is
15002 put into the .got.plt section, if the backend uses it. */
15003 if (bed
->want_got_plt
)
15006 gotoff
= bed
->got_header_size
;
15008 /* Do the local .got entries first. */
15009 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
15011 bfd_signed_vma
*local_got
;
15012 size_t j
, locsymcount
;
15013 Elf_Internal_Shdr
*symtab_hdr
;
15015 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
15018 local_got
= elf_local_got_refcounts (i
);
15022 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
15023 if (elf_bad_symtab (i
))
15024 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
15026 locsymcount
= symtab_hdr
->sh_info
;
15028 for (j
= 0; j
< locsymcount
; ++j
)
15030 if (local_got
[j
] > 0)
15032 local_got
[j
] = gotoff
;
15033 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
15036 local_got
[j
] = (bfd_vma
) -1;
15040 /* Then the global .got entries. .plt refcounts are handled by
15041 adjust_dynamic_symbol */
15042 gofarg
.gotoff
= gotoff
;
15043 gofarg
.info
= info
;
15044 elf_link_hash_traverse (elf_hash_table (info
),
15045 elf_gc_allocate_got_offsets
,
15050 /* Many folk need no more in the way of final link than this, once
15051 got entry reference counting is enabled. */
15054 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
15056 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
15059 /* Invoke the regular ELF backend linker to do all the work. */
15060 return bfd_elf_final_link (abfd
, info
);
15064 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
15066 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
15068 if (rcookie
->bad_symtab
)
15069 rcookie
->rel
= rcookie
->rels
;
15071 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
15073 unsigned long r_symndx
;
15075 if (! rcookie
->bad_symtab
)
15076 if (rcookie
->rel
->r_offset
> offset
)
15078 if (rcookie
->rel
->r_offset
!= offset
)
15081 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
15082 if (r_symndx
== STN_UNDEF
)
15085 if (r_symndx
>= rcookie
->locsymcount
15086 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
15088 struct elf_link_hash_entry
*h
;
15090 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
15092 while (h
->root
.type
== bfd_link_hash_indirect
15093 || h
->root
.type
== bfd_link_hash_warning
)
15094 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
15096 if ((h
->root
.type
== bfd_link_hash_defined
15097 || h
->root
.type
== bfd_link_hash_defweak
)
15098 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
15099 || h
->root
.u
.def
.section
->kept_section
!= NULL
15100 || discarded_section (h
->root
.u
.def
.section
)))
15105 /* It's not a relocation against a global symbol,
15106 but it could be a relocation against a local
15107 symbol for a discarded section. */
15109 Elf_Internal_Sym
*isym
;
15111 /* Need to: get the symbol; get the section. */
15112 isym
= &rcookie
->locsyms
[r_symndx
];
15113 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
15115 && (isec
->kept_section
!= NULL
15116 || discarded_section (isec
)))
15124 /* Discard unneeded references to discarded sections.
15125 Returns -1 on error, 1 if any section's size was changed, 0 if
15126 nothing changed. This function assumes that the relocations are in
15127 sorted order, which is true for all known assemblers. */
15130 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
15132 struct elf_reloc_cookie cookie
;
15137 if (info
->traditional_format
15138 || !is_elf_hash_table (info
->hash
))
15141 o
= bfd_get_section_by_name (output_bfd
, ".stab");
15146 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
15149 || i
->reloc_count
== 0
15150 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
15154 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15157 if (!init_reloc_cookie_for_section (&cookie
, info
, i
, false))
15160 if (_bfd_discard_section_stabs (abfd
, i
,
15161 elf_section_data (i
)->sec_info
,
15162 bfd_elf_reloc_symbol_deleted_p
,
15166 fini_reloc_cookie_for_section (&cookie
, i
);
15171 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
15172 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
15176 int eh_changed
= 0;
15177 unsigned int eh_alignment
; /* Octets. */
15179 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
15185 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15188 if (!init_reloc_cookie_for_section (&cookie
, info
, i
, false))
15191 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
15192 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
15193 bfd_elf_reloc_symbol_deleted_p
,
15197 if (i
->size
!= i
->rawsize
)
15201 fini_reloc_cookie_for_section (&cookie
, i
);
15204 eh_alignment
= ((1 << o
->alignment_power
)
15205 * bfd_octets_per_byte (output_bfd
, o
));
15206 /* Skip over zero terminator, and prevent empty sections from
15207 adding alignment padding at the end. */
15208 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
15210 i
->flags
|= SEC_EXCLUDE
;
15211 else if (i
->size
> 4)
15213 /* The last non-empty eh_frame section doesn't need padding. */
15216 /* Any prior sections must pad the last FDE out to the output
15217 section alignment. Otherwise we might have zero padding
15218 between sections, which would be seen as a terminator. */
15219 for (; i
!= NULL
; i
= i
->map_tail
.s
)
15221 /* All but the last zero terminator should have been removed. */
15226 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
15227 if (i
->size
!= size
)
15235 elf_link_hash_traverse (elf_hash_table (info
),
15236 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
15239 o
= bfd_get_section_by_name (output_bfd
, ".sframe");
15244 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
15250 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15253 if (!init_reloc_cookie_for_section (&cookie
, info
, i
, false))
15256 if (_bfd_elf_parse_sframe (abfd
, info
, i
, &cookie
))
15258 if (_bfd_elf_discard_section_sframe (i
,
15259 bfd_elf_reloc_symbol_deleted_p
,
15262 if (i
->size
!= i
->rawsize
)
15266 fini_reloc_cookie_for_section (&cookie
, i
);
15268 /* Update the reference to the output .sframe section. Used to
15269 determine later if PT_GNU_SFRAME segment is to be generated. */
15270 if (!_bfd_elf_set_section_sframe (output_bfd
, info
))
15274 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
15276 const struct elf_backend_data
*bed
;
15279 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15281 s
= abfd
->sections
;
15282 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
15285 bed
= get_elf_backend_data (abfd
);
15287 if (bed
->elf_backend_discard_info
!= NULL
)
15289 if (!init_reloc_cookie (&cookie
, info
, abfd
, false))
15292 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
15295 fini_reloc_cookie (&cookie
, abfd
);
15299 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
15300 _bfd_elf_end_eh_frame_parsing (info
);
15302 if (info
->eh_frame_hdr_type
15303 && !bfd_link_relocatable (info
)
15304 && _bfd_elf_discard_section_eh_frame_hdr (info
))
15311 _bfd_elf_section_already_linked (bfd
*abfd
,
15313 struct bfd_link_info
*info
)
15316 const char *name
, *key
;
15317 struct bfd_section_already_linked
*l
;
15318 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
15320 if (sec
->output_section
== bfd_abs_section_ptr
)
15323 flags
= sec
->flags
;
15325 /* Return if it isn't a linkonce section. A comdat group section
15326 also has SEC_LINK_ONCE set. */
15327 if ((flags
& SEC_LINK_ONCE
) == 0)
15330 /* Don't put group member sections on our list of already linked
15331 sections. They are handled as a group via their group section. */
15332 if (elf_sec_group (sec
) != NULL
)
15335 /* For a SHT_GROUP section, use the group signature as the key. */
15337 if ((flags
& SEC_GROUP
) != 0
15338 && elf_next_in_group (sec
) != NULL
15339 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
15340 key
= elf_group_name (elf_next_in_group (sec
));
15343 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15344 if (startswith (name
, ".gnu.linkonce.")
15345 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
15348 /* Must be a user linkonce section that doesn't follow gcc's
15349 naming convention. In this case we won't be matching
15350 single member groups. */
15354 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
15356 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15358 /* We may have 2 different types of sections on the list: group
15359 sections with a signature of <key> (<key> is some string),
15360 and linkonce sections named .gnu.linkonce.<type>.<key>.
15361 Match like sections. LTO plugin sections are an exception.
15362 They are always named .gnu.linkonce.t.<key> and match either
15363 type of section. */
15364 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
15365 && ((flags
& SEC_GROUP
) != 0
15366 || strcmp (name
, l
->sec
->name
) == 0))
15367 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
15368 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
15370 /* The section has already been linked. See if we should
15371 issue a warning. */
15372 if (!_bfd_handle_already_linked (sec
, l
, info
))
15375 if (flags
& SEC_GROUP
)
15377 asection
*first
= elf_next_in_group (sec
);
15378 asection
*s
= first
;
15382 s
->output_section
= bfd_abs_section_ptr
;
15383 /* Record which group discards it. */
15384 s
->kept_section
= l
->sec
;
15385 s
= elf_next_in_group (s
);
15386 /* These lists are circular. */
15396 /* A single member comdat group section may be discarded by a
15397 linkonce section and vice versa. */
15398 if ((flags
& SEC_GROUP
) != 0)
15400 asection
*first
= elf_next_in_group (sec
);
15402 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
15403 /* Check this single member group against linkonce sections. */
15404 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15405 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15406 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
15408 first
->output_section
= bfd_abs_section_ptr
;
15409 first
->kept_section
= l
->sec
;
15410 sec
->output_section
= bfd_abs_section_ptr
;
15415 /* Check this linkonce section against single member groups. */
15416 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15417 if (l
->sec
->flags
& SEC_GROUP
)
15419 asection
*first
= elf_next_in_group (l
->sec
);
15422 && elf_next_in_group (first
) == first
15423 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
15425 sec
->output_section
= bfd_abs_section_ptr
;
15426 sec
->kept_section
= first
;
15431 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15432 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15433 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15434 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15435 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15436 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15437 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15438 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15439 The reverse order cannot happen as there is never a bfd with only the
15440 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15441 matter as here were are looking only for cross-bfd sections. */
15443 if ((flags
& SEC_GROUP
) == 0 && startswith (name
, ".gnu.linkonce.r."))
15444 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15445 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15446 && startswith (l
->sec
->name
, ".gnu.linkonce.t."))
15448 if (abfd
!= l
->sec
->owner
)
15449 sec
->output_section
= bfd_abs_section_ptr
;
15453 /* This is the first section with this name. Record it. */
15454 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
15455 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
15456 return sec
->output_section
== bfd_abs_section_ptr
;
15460 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
15462 return sym
->st_shndx
== SHN_COMMON
;
15466 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
15472 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
15474 return bfd_com_section_ptr
;
15478 _bfd_elf_default_got_elt_size (bfd
*abfd
,
15479 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
15480 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
15481 bfd
*ibfd ATTRIBUTE_UNUSED
,
15482 unsigned long symndx ATTRIBUTE_UNUSED
)
15484 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15485 return bed
->s
->arch_size
/ 8;
15488 /* Routines to support the creation of dynamic relocs. */
15490 /* Returns the name of the dynamic reloc section associated with SEC. */
15492 static const char *
15493 get_dynamic_reloc_section_name (bfd
* abfd
,
15498 const char *old_name
= bfd_section_name (sec
);
15499 const char *prefix
= is_rela
? ".rela" : ".rel";
15501 if (old_name
== NULL
)
15504 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
15505 sprintf (name
, "%s%s", prefix
, old_name
);
15510 /* Returns the dynamic reloc section associated with SEC.
15511 If necessary compute the name of the dynamic reloc section based
15512 on SEC's name (looked up in ABFD's string table) and the setting
15516 _bfd_elf_get_dynamic_reloc_section (bfd
*abfd
,
15520 asection
*reloc_sec
= elf_section_data (sec
)->sreloc
;
15522 if (reloc_sec
== NULL
)
15524 const char *name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15528 reloc_sec
= bfd_get_linker_section (abfd
, name
);
15530 if (reloc_sec
!= NULL
)
15531 elf_section_data (sec
)->sreloc
= reloc_sec
;
15538 /* Returns the dynamic reloc section associated with SEC. If the
15539 section does not exist it is created and attached to the DYNOBJ
15540 bfd and stored in the SRELOC field of SEC's elf_section_data
15543 ALIGNMENT is the alignment for the newly created section and
15544 IS_RELA defines whether the name should be .rela.<SEC's name>
15545 or .rel.<SEC's name>. The section name is looked up in the
15546 string table associated with ABFD. */
15549 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
15551 unsigned int alignment
,
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
);
15564 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
15566 if (reloc_sec
== NULL
)
15568 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
15569 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
15570 if ((sec
->flags
& SEC_ALLOC
) != 0)
15571 flags
|= SEC_ALLOC
| SEC_LOAD
;
15573 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
15574 if (reloc_sec
!= NULL
)
15576 /* _bfd_elf_get_sec_type_attr chooses a section type by
15577 name. Override as it may be wrong, eg. for a user
15578 section named "auto" we'll get ".relauto" which is
15579 seen to be a .rela section. */
15580 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
15581 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
15586 elf_section_data (sec
)->sreloc
= reloc_sec
;
15592 /* Copy the ELF symbol type and other attributes for a linker script
15593 assignment from HSRC to HDEST. Generally this should be treated as
15594 if we found a strong non-dynamic definition for HDEST (except that
15595 ld ignores multiple definition errors). */
15597 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
15598 struct bfd_link_hash_entry
*hdest
,
15599 struct bfd_link_hash_entry
*hsrc
)
15601 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
15602 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
15603 Elf_Internal_Sym isym
;
15605 ehdest
->type
= ehsrc
->type
;
15606 ehdest
->target_internal
= ehsrc
->target_internal
;
15608 isym
.st_other
= ehsrc
->other
;
15609 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, true, false);
15612 /* Append a RELA relocation REL to section S in BFD. */
15615 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15617 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15618 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
15619 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
15620 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
15623 /* Append a REL relocation REL to section S in BFD. */
15626 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15628 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15629 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
15630 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
15631 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
15634 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15636 struct bfd_link_hash_entry
*
15637 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
15638 const char *symbol
, asection
*sec
)
15640 struct elf_link_hash_entry
*h
;
15642 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
15643 false, false, true);
15644 /* NB: Common symbols will be turned into definition later. */
15646 && !h
->root
.ldscript_def
15647 && (h
->root
.type
== bfd_link_hash_undefined
15648 || h
->root
.type
== bfd_link_hash_undefweak
15649 || ((h
->ref_regular
|| h
->def_dynamic
)
15651 && h
->root
.type
!= bfd_link_hash_common
)))
15653 bool was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15654 h
->verinfo
.verdef
= NULL
;
15655 h
->root
.type
= bfd_link_hash_defined
;
15656 h
->root
.u
.def
.section
= sec
;
15657 h
->root
.u
.def
.value
= 0;
15658 h
->def_regular
= 1;
15659 h
->def_dynamic
= 0;
15661 h
->u2
.start_stop_section
= sec
;
15662 if (symbol
[0] == '.')
15664 /* .startof. and .sizeof. symbols are local. */
15665 const struct elf_backend_data
*bed
;
15666 bed
= get_elf_backend_data (info
->output_bfd
);
15667 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
15671 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15672 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15673 | info
->start_stop_visibility
);
15675 bfd_elf_link_record_dynamic_symbol (info
, h
);
15682 /* Find dynamic relocs for H that apply to read-only sections. */
15685 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15687 struct elf_dyn_relocs
*p
;
15689 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15691 asection
*s
= p
->sec
->output_section
;
15693 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15699 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15700 read-only sections. */
15703 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15707 if (h
->root
.type
== bfd_link_hash_indirect
)
15710 sec
= _bfd_elf_readonly_dynrelocs (h
);
15713 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15715 info
->flags
|= DF_TEXTREL
;
15716 /* xgettext:c-format */
15717 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15718 "in read-only section `%pA'\n"),
15719 sec
->owner
, h
->root
.root
.string
, sec
);
15721 if (bfd_link_textrel_check (info
))
15722 /* xgettext:c-format */
15723 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15724 "in read-only section `%pA'\n"),
15725 sec
->owner
, h
->root
.root
.string
, sec
);
15727 /* Not an error, just cut short the traversal. */
15733 /* Add dynamic tags. */
15736 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15737 bool need_dynamic_reloc
)
15739 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15741 if (htab
->dynamic_sections_created
)
15743 /* Add some entries to the .dynamic section. We fill in the
15744 values later, in finish_dynamic_sections, but we must add
15745 the entries now so that we get the correct size for the
15746 .dynamic section. The DT_DEBUG entry is filled in by the
15747 dynamic linker and used by the debugger. */
15748 #define add_dynamic_entry(TAG, VAL) \
15749 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15751 const struct elf_backend_data
*bed
15752 = get_elf_backend_data (output_bfd
);
15754 if (bfd_link_executable (info
))
15756 if (!add_dynamic_entry (DT_DEBUG
, 0))
15760 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15762 /* DT_PLTGOT is used by prelink even if there is no PLT
15764 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15768 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15770 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15771 || !add_dynamic_entry (DT_PLTREL
,
15772 (bed
->rela_plts_and_copies_p
15773 ? DT_RELA
: DT_REL
))
15774 || !add_dynamic_entry (DT_JMPREL
, 0))
15778 if (htab
->tlsdesc_plt
15779 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15780 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15783 if (need_dynamic_reloc
)
15785 if (bed
->rela_plts_and_copies_p
)
15787 if (!add_dynamic_entry (DT_RELA
, 0)
15788 || !add_dynamic_entry (DT_RELASZ
, 0)
15789 || !add_dynamic_entry (DT_RELAENT
,
15790 bed
->s
->sizeof_rela
))
15795 if (!add_dynamic_entry (DT_REL
, 0)
15796 || !add_dynamic_entry (DT_RELSZ
, 0)
15797 || !add_dynamic_entry (DT_RELENT
,
15798 bed
->s
->sizeof_rel
))
15802 /* If any dynamic relocs apply to a read-only section,
15803 then we need a DT_TEXTREL entry. */
15804 if ((info
->flags
& DF_TEXTREL
) == 0)
15805 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15808 if ((info
->flags
& DF_TEXTREL
) != 0)
15810 if (htab
->ifunc_resolvers
)
15811 info
->callbacks
->einfo
15812 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15813 "may result in a segfault at runtime; recompile with %s\n"),
15814 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15816 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15821 #undef add_dynamic_entry