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
2948 Elf_Internal_Rela
*irela
;
2949 Elf_Internal_Rela
*irelaend
;
2951 struct bfd_elf_section_reloc_data
*output_reldata
;
2952 asection
*output_section
;
2953 const struct elf_backend_data
*bed
;
2954 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2955 struct bfd_elf_section_data
*esdo
;
2957 output_section
= input_section
->output_section
;
2959 bed
= get_elf_backend_data (output_bfd
);
2960 esdo
= elf_section_data (output_section
);
2961 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2963 output_reldata
= &esdo
->rel
;
2964 swap_out
= bed
->s
->swap_reloc_out
;
2966 else if (esdo
->rela
.hdr
2967 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2969 output_reldata
= &esdo
->rela
;
2970 swap_out
= bed
->s
->swap_reloca_out
;
2975 /* xgettext:c-format */
2976 (_("%pB: relocation size mismatch in %pB section %pA"),
2977 output_bfd
, input_section
->owner
, input_section
);
2978 bfd_set_error (bfd_error_wrong_format
);
2982 erel
= output_reldata
->hdr
->contents
;
2983 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2984 irela
= internal_relocs
;
2985 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2986 * bed
->s
->int_rels_per_ext_rel
);
2987 while (irela
< irelaend
)
2989 (*swap_out
) (output_bfd
, irela
, erel
);
2990 irela
+= bed
->s
->int_rels_per_ext_rel
;
2991 erel
+= input_rel_hdr
->sh_entsize
;
2994 /* Bump the counter, so that we know where to add the next set of
2996 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
3001 /* Make weak undefined symbols in PIE dynamic. */
3004 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
3005 struct elf_link_hash_entry
*h
)
3007 if (bfd_link_pie (info
)
3009 && h
->root
.type
== bfd_link_hash_undefweak
)
3010 return bfd_elf_link_record_dynamic_symbol (info
, h
);
3015 /* Fix up the flags for a symbol. This handles various cases which
3016 can only be fixed after all the input files are seen. This is
3017 currently called by both adjust_dynamic_symbol and
3018 assign_sym_version, which is unnecessary but perhaps more robust in
3019 the face of future changes. */
3022 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
3023 struct elf_info_failed
*eif
)
3025 const struct elf_backend_data
*bed
;
3027 /* If this symbol was mentioned in a non-ELF file, try to set
3028 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3029 permit a non-ELF file to correctly refer to a symbol defined in
3030 an ELF dynamic object. */
3033 while (h
->root
.type
== bfd_link_hash_indirect
)
3034 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3036 if (h
->root
.type
!= bfd_link_hash_defined
3037 && h
->root
.type
!= bfd_link_hash_defweak
)
3040 h
->ref_regular_nonweak
= 1;
3044 if (h
->root
.u
.def
.section
->owner
!= NULL
3045 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3046 == bfd_target_elf_flavour
))
3049 h
->ref_regular_nonweak
= 1;
3055 if (h
->dynindx
== -1
3059 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3068 /* Unfortunately, NON_ELF is only correct if the symbol
3069 was first seen in a non-ELF file. Fortunately, if the symbol
3070 was first seen in an ELF file, we're probably OK unless the
3071 symbol was defined in a non-ELF file. Catch that case here.
3072 FIXME: We're still in trouble if the symbol was first seen in
3073 a dynamic object, and then later in a non-ELF regular object. */
3074 if ((h
->root
.type
== bfd_link_hash_defined
3075 || h
->root
.type
== bfd_link_hash_defweak
)
3077 && (h
->root
.u
.def
.section
->owner
!= NULL
3078 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3079 != bfd_target_elf_flavour
)
3080 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3081 && !h
->def_dynamic
)))
3085 /* Backend specific symbol fixup. */
3086 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3087 if (bed
->elf_backend_fixup_symbol
3088 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
3091 /* If this is a final link, and the symbol was defined as a common
3092 symbol in a regular object file, and there was no definition in
3093 any dynamic object, then the linker will have allocated space for
3094 the symbol in a common section but the DEF_REGULAR
3095 flag will not have been set. */
3096 if (h
->root
.type
== bfd_link_hash_defined
3100 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
3103 /* Symbols defined in discarded sections shouldn't be dynamic. */
3104 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
3105 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3107 /* If a weak undefined symbol has non-default visibility, we also
3108 hide it from the dynamic linker. */
3109 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
3110 && h
->root
.type
== bfd_link_hash_undefweak
)
3111 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3113 /* A hidden versioned symbol in executable should be forced local if
3114 it is is locally defined, not referenced by shared library and not
3116 else if (bfd_link_executable (eif
->info
)
3117 && h
->versioned
== versioned_hidden
3118 && !eif
->info
->export_dynamic
3122 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3124 /* If -Bsymbolic was used (which means to bind references to global
3125 symbols to the definition within the shared object), and this
3126 symbol was defined in a regular object, then it actually doesn't
3127 need a PLT entry. Likewise, if the symbol has non-default
3128 visibility. If the symbol has hidden or internal visibility, we
3129 will force it local. */
3130 else if (h
->needs_plt
3131 && bfd_link_pic (eif
->info
)
3132 && is_elf_hash_table (eif
->info
->hash
)
3133 && (SYMBOLIC_BIND (eif
->info
, h
)
3134 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
3139 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3140 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
3141 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3144 /* If this is a weak defined symbol in a dynamic object, and we know
3145 the real definition in the dynamic object, copy interesting flags
3146 over to the real definition. */
3147 if (h
->is_weakalias
)
3149 struct elf_link_hash_entry
*def
= weakdef (h
);
3151 /* If the real definition is defined by a regular object file,
3152 don't do anything special. See the longer description in
3153 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3154 bfd_link_hash_defined as it was when put on the alias list
3155 then it must have originally been a versioned symbol (for
3156 which a non-versioned indirect symbol is created) and later
3157 a definition for the non-versioned symbol is found. In that
3158 case the indirection is flipped with the versioned symbol
3159 becoming an indirect pointing at the non-versioned symbol.
3160 Thus, not an alias any more. */
3161 if (def
->def_regular
3162 || def
->root
.type
!= bfd_link_hash_defined
)
3165 while ((h
= h
->u
.alias
) != def
)
3166 h
->is_weakalias
= 0;
3170 while (h
->root
.type
== bfd_link_hash_indirect
)
3171 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3172 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3173 || h
->root
.type
== bfd_link_hash_defweak
);
3174 BFD_ASSERT (def
->def_dynamic
);
3175 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
3182 /* Make the backend pick a good value for a dynamic symbol. This is
3183 called via elf_link_hash_traverse, and also calls itself
3187 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
3189 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3190 struct elf_link_hash_table
*htab
;
3191 const struct elf_backend_data
*bed
;
3193 if (! is_elf_hash_table (eif
->info
->hash
))
3196 /* Ignore indirect symbols. These are added by the versioning code. */
3197 if (h
->root
.type
== bfd_link_hash_indirect
)
3200 /* Fix the symbol flags. */
3201 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
3204 htab
= elf_hash_table (eif
->info
);
3205 bed
= get_elf_backend_data (htab
->dynobj
);
3207 if (h
->root
.type
== bfd_link_hash_undefweak
)
3209 if (eif
->info
->dynamic_undefined_weak
== 0)
3210 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3211 else if (eif
->info
->dynamic_undefined_weak
> 0
3213 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3214 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
3215 h
->root
.root
.string
))
3217 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3225 /* If this symbol does not require a PLT entry, and it is not
3226 defined by a dynamic object, or is not referenced by a regular
3227 object, ignore it. We do have to handle a weak defined symbol,
3228 even if no regular object refers to it, if we decided to add it
3229 to the dynamic symbol table. FIXME: Do we normally need to worry
3230 about symbols which are defined by one dynamic object and
3231 referenced by another one? */
3233 && h
->type
!= STT_GNU_IFUNC
3237 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3239 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3243 /* If we've already adjusted this symbol, don't do it again. This
3244 can happen via a recursive call. */
3245 if (h
->dynamic_adjusted
)
3248 /* Don't look at this symbol again. Note that we must set this
3249 after checking the above conditions, because we may look at a
3250 symbol once, decide not to do anything, and then get called
3251 recursively later after REF_REGULAR is set below. */
3252 h
->dynamic_adjusted
= 1;
3254 /* If this is a weak definition, and we know a real definition, and
3255 the real symbol is not itself defined by a regular object file,
3256 then get a good value for the real definition. We handle the
3257 real symbol first, for the convenience of the backend routine.
3259 Note that there is a confusing case here. If the real definition
3260 is defined by a regular object file, we don't get the real symbol
3261 from the dynamic object, but we do get the weak symbol. If the
3262 processor backend uses a COPY reloc, then if some routine in the
3263 dynamic object changes the real symbol, we will not see that
3264 change in the corresponding weak symbol. This is the way other
3265 ELF linkers work as well, and seems to be a result of the shared
3268 I will clarify this issue. Most SVR4 shared libraries define the
3269 variable _timezone and define timezone as a weak synonym. The
3270 tzset call changes _timezone. If you write
3271 extern int timezone;
3273 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3274 you might expect that, since timezone is a synonym for _timezone,
3275 the same number will print both times. However, if the processor
3276 backend uses a COPY reloc, then actually timezone will be copied
3277 into your process image, and, since you define _timezone
3278 yourself, _timezone will not. Thus timezone and _timezone will
3279 wind up at different memory locations. The tzset call will set
3280 _timezone, leaving timezone unchanged. */
3282 if (h
->is_weakalias
)
3284 struct elf_link_hash_entry
*def
= weakdef (h
);
3286 /* If we get to this point, there is an implicit reference to
3287 the alias by a regular object file via the weak symbol H. */
3288 def
->ref_regular
= 1;
3290 /* Ensure that the backend adjust_dynamic_symbol function sees
3291 the strong alias before H by recursively calling ourselves. */
3292 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3296 /* If a symbol has no type and no size and does not require a PLT
3297 entry, then we are probably about to do the wrong thing here: we
3298 are probably going to create a COPY reloc for an empty object.
3299 This case can arise when a shared object is built with assembly
3300 code, and the assembly code fails to set the symbol type. */
3302 && h
->type
== STT_NOTYPE
3305 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3306 h
->root
.root
.string
);
3308 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3317 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3321 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3322 struct elf_link_hash_entry
*h
,
3325 unsigned int power_of_two
;
3327 asection
*sec
= h
->root
.u
.def
.section
;
3329 /* The section alignment of the definition is the maximum alignment
3330 requirement of symbols defined in the section. Since we don't
3331 know the symbol alignment requirement, we start with the
3332 maximum alignment and check low bits of the symbol address
3333 for the minimum alignment. */
3334 power_of_two
= bfd_section_alignment (sec
);
3335 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3336 while ((h
->root
.u
.def
.value
& mask
) != 0)
3342 if (power_of_two
> bfd_section_alignment (dynbss
))
3344 /* Adjust the section alignment if needed. */
3345 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3349 /* We make sure that the symbol will be aligned properly. */
3350 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3352 /* Define the symbol as being at this point in DYNBSS. */
3353 h
->root
.u
.def
.section
= dynbss
;
3354 h
->root
.u
.def
.value
= dynbss
->size
;
3356 /* Increment the size of DYNBSS to make room for the symbol. */
3357 dynbss
->size
+= h
->size
;
3359 /* No error if extern_protected_data is true. */
3360 if (h
->protected_def
3361 && (!info
->extern_protected_data
3362 || (info
->extern_protected_data
< 0
3363 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3364 info
->callbacks
->einfo
3365 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3366 h
->root
.root
.string
);
3371 /* Adjust all external symbols pointing into SEC_MERGE sections
3372 to reflect the object merging within the sections. */
3375 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3379 if ((h
->root
.type
== bfd_link_hash_defined
3380 || h
->root
.type
== bfd_link_hash_defweak
)
3381 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3382 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3384 bfd
*output_bfd
= (bfd
*) data
;
3386 h
->root
.u
.def
.value
=
3387 _bfd_merged_section_offset (output_bfd
,
3388 &h
->root
.u
.def
.section
,
3389 elf_section_data (sec
)->sec_info
,
3390 h
->root
.u
.def
.value
);
3396 /* Returns false if the symbol referred to by H should be considered
3397 to resolve local to the current module, and true if it should be
3398 considered to bind dynamically. */
3401 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3402 struct bfd_link_info
*info
,
3403 bool not_local_protected
)
3405 bool binding_stays_local_p
;
3406 const struct elf_backend_data
*bed
;
3407 struct elf_link_hash_table
*hash_table
;
3412 while (h
->root
.type
== bfd_link_hash_indirect
3413 || h
->root
.type
== bfd_link_hash_warning
)
3414 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3416 /* If it was forced local, then clearly it's not dynamic. */
3417 if (h
->dynindx
== -1)
3419 if (h
->forced_local
)
3422 /* Identify the cases where name binding rules say that a
3423 visible symbol resolves locally. */
3424 binding_stays_local_p
= (bfd_link_executable (info
)
3425 || SYMBOLIC_BIND (info
, h
));
3427 switch (ELF_ST_VISIBILITY (h
->other
))
3434 hash_table
= elf_hash_table (info
);
3435 if (!is_elf_hash_table (&hash_table
->root
))
3438 bed
= get_elf_backend_data (hash_table
->dynobj
);
3440 /* Proper resolution for function pointer equality may require
3441 that these symbols perhaps be resolved dynamically, even though
3442 we should be resolving them to the current module. */
3443 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3444 binding_stays_local_p
= true;
3451 /* If it isn't defined locally, then clearly it's dynamic. */
3452 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3455 /* Otherwise, the symbol is dynamic if binding rules don't tell
3456 us that it remains local. */
3457 return !binding_stays_local_p
;
3460 /* Return true if the symbol referred to by H should be considered
3461 to resolve local to the current module, and false otherwise. Differs
3462 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3463 undefined symbols. The two functions are virtually identical except
3464 for the place where dynindx == -1 is tested. If that test is true,
3465 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3466 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3468 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3469 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3470 treatment of undefined weak symbols. For those that do not make
3471 undefined weak symbols dynamic, both functions may return false. */
3474 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3475 struct bfd_link_info
*info
,
3476 bool local_protected
)
3478 const struct elf_backend_data
*bed
;
3479 struct elf_link_hash_table
*hash_table
;
3481 /* If it's a local sym, of course we resolve locally. */
3485 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3486 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3487 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3490 /* Forced local symbols resolve locally. */
3491 if (h
->forced_local
)
3494 /* Common symbols that become definitions don't get the DEF_REGULAR
3495 flag set, so test it first, and don't bail out. */
3496 if (ELF_COMMON_DEF_P (h
))
3498 /* If we don't have a definition in a regular file, then we can't
3499 resolve locally. The sym is either undefined or dynamic. */
3500 else if (!h
->def_regular
)
3503 /* Non-dynamic symbols resolve locally. */
3504 if (h
->dynindx
== -1)
3507 /* At this point, we know the symbol is defined and dynamic. In an
3508 executable it must resolve locally, likewise when building symbolic
3509 shared libraries. */
3510 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3513 /* Now deal with defined dynamic symbols in shared libraries. Ones
3514 with default visibility might not resolve locally. */
3515 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3518 hash_table
= elf_hash_table (info
);
3519 if (!is_elf_hash_table (&hash_table
->root
))
3522 /* STV_PROTECTED symbols with indirect external access are local. */
3523 if (info
->indirect_extern_access
> 0)
3526 bed
= get_elf_backend_data (hash_table
->dynobj
);
3528 /* If extern_protected_data is false, STV_PROTECTED non-function
3529 symbols are local. */
3530 if ((!info
->extern_protected_data
3531 || (info
->extern_protected_data
< 0
3532 && !bed
->extern_protected_data
))
3533 && !bed
->is_function_type (h
->type
))
3536 /* Function pointer equality tests may require that STV_PROTECTED
3537 symbols be treated as dynamic symbols. If the address of a
3538 function not defined in an executable is set to that function's
3539 plt entry in the executable, then the address of the function in
3540 a shared library must also be the plt entry in the executable. */
3541 return local_protected
;
3544 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3545 aligned. Returns the first TLS output section. */
3547 struct bfd_section
*
3548 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3550 struct bfd_section
*sec
, *tls
;
3551 unsigned int align
= 0;
3553 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3554 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3558 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3559 if (sec
->alignment_power
> align
)
3560 align
= sec
->alignment_power
;
3562 elf_hash_table (info
)->tls_sec
= tls
;
3564 /* Ensure the alignment of the first section (usually .tdata) is the largest
3565 alignment, so that the tls segment starts aligned. */
3567 tls
->alignment_power
= align
;
3572 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3574 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3575 Elf_Internal_Sym
*sym
)
3577 const struct elf_backend_data
*bed
;
3579 /* Local symbols do not count, but target specific ones might. */
3580 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3581 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3584 bed
= get_elf_backend_data (abfd
);
3585 /* Function symbols do not count. */
3586 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3589 /* If the section is undefined, then so is the symbol. */
3590 if (sym
->st_shndx
== SHN_UNDEF
)
3593 /* If the symbol is defined in the common section, then
3594 it is a common definition and so does not count. */
3595 if (bed
->common_definition (sym
))
3598 /* If the symbol is in a target specific section then we
3599 must rely upon the backend to tell us what it is. */
3600 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3601 /* FIXME - this function is not coded yet:
3603 return _bfd_is_global_symbol_definition (abfd, sym);
3605 Instead for now assume that the definition is not global,
3606 Even if this is wrong, at least the linker will behave
3607 in the same way that it used to do. */
3613 /* Search the symbol table of the archive element of the archive ABFD
3614 whose archive map contains a mention of SYMDEF, and determine if
3615 the symbol is defined in this element. */
3617 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3619 Elf_Internal_Shdr
* hdr
;
3623 Elf_Internal_Sym
*isymbuf
;
3624 Elf_Internal_Sym
*isym
;
3625 Elf_Internal_Sym
*isymend
;
3628 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
, NULL
);
3632 if (! bfd_check_format (abfd
, bfd_object
))
3635 /* Select the appropriate symbol table. If we don't know if the
3636 object file is an IR object, give linker LTO plugin a chance to
3637 get the correct symbol table. */
3638 if (abfd
->plugin_format
== bfd_plugin_yes
3639 #if BFD_SUPPORTS_PLUGINS
3640 || (abfd
->plugin_format
== bfd_plugin_unknown
3641 && bfd_link_plugin_object_p (abfd
))
3645 /* Use the IR symbol table if the object has been claimed by
3647 abfd
= abfd
->plugin_dummy_bfd
;
3648 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3652 if (elf_use_dt_symtab_p (abfd
))
3654 bfd_set_error (bfd_error_wrong_format
);
3658 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3659 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3661 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3664 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3666 /* The sh_info field of the symtab header tells us where the
3667 external symbols start. We don't care about the local symbols. */
3668 if (elf_bad_symtab (abfd
))
3670 extsymcount
= symcount
;
3675 extsymcount
= symcount
- hdr
->sh_info
;
3676 extsymoff
= hdr
->sh_info
;
3679 if (extsymcount
== 0)
3682 /* Read in the symbol table. */
3683 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3685 if (isymbuf
== NULL
)
3688 /* Scan the symbol table looking for SYMDEF. */
3690 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3694 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3699 if (strcmp (name
, symdef
->name
) == 0)
3701 result
= is_global_data_symbol_definition (abfd
, isym
);
3711 /* Add an entry to the .dynamic table. */
3714 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3718 struct elf_link_hash_table
*hash_table
;
3719 const struct elf_backend_data
*bed
;
3721 bfd_size_type newsize
;
3722 bfd_byte
*newcontents
;
3723 Elf_Internal_Dyn dyn
;
3725 hash_table
= elf_hash_table (info
);
3726 if (! is_elf_hash_table (&hash_table
->root
))
3729 if (tag
== DT_RELA
|| tag
== DT_REL
)
3730 hash_table
->dynamic_relocs
= true;
3732 bed
= get_elf_backend_data (hash_table
->dynobj
);
3733 s
= hash_table
->dynamic
;
3734 BFD_ASSERT (s
!= NULL
);
3736 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3737 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3738 if (newcontents
== NULL
)
3742 dyn
.d_un
.d_val
= val
;
3743 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3746 s
->contents
= newcontents
;
3751 /* Strip zero-sized dynamic sections. */
3754 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3756 struct elf_link_hash_table
*hash_table
;
3757 const struct elf_backend_data
*bed
;
3758 asection
*s
, *sdynamic
, **pp
;
3759 asection
*rela_dyn
, *rel_dyn
;
3760 Elf_Internal_Dyn dyn
;
3761 bfd_byte
*extdyn
, *next
;
3762 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3763 bool strip_zero_sized
;
3764 bool strip_zero_sized_plt
;
3766 if (bfd_link_relocatable (info
))
3769 hash_table
= elf_hash_table (info
);
3770 if (!is_elf_hash_table (&hash_table
->root
))
3773 if (!hash_table
->dynobj
)
3776 sdynamic
= hash_table
->dynamic
;
3780 bed
= get_elf_backend_data (hash_table
->dynobj
);
3781 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3783 strip_zero_sized
= false;
3784 strip_zero_sized_plt
= false;
3786 /* Strip zero-sized dynamic sections. */
3787 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3788 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3789 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3793 || s
== hash_table
->srelplt
->output_section
3794 || s
== hash_table
->splt
->output_section
))
3797 info
->output_bfd
->section_count
--;
3798 strip_zero_sized
= true;
3803 else if (s
== hash_table
->splt
->output_section
)
3805 s
= hash_table
->splt
;
3806 strip_zero_sized_plt
= true;
3809 s
= hash_table
->srelplt
;
3810 s
->flags
|= SEC_EXCLUDE
;
3811 s
->output_section
= bfd_abs_section_ptr
;
3816 if (strip_zero_sized_plt
&& sdynamic
->size
!= 0)
3817 for (extdyn
= sdynamic
->contents
;
3818 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3821 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3822 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3830 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3831 the procedure linkage table (the .plt section) has been
3833 memmove (extdyn
, next
,
3834 sdynamic
->size
- (next
- sdynamic
->contents
));
3839 if (strip_zero_sized
)
3841 /* Regenerate program headers. */
3842 elf_seg_map (info
->output_bfd
) = NULL
;
3843 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
,
3850 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3851 1 if a DT_NEEDED tag already exists, and 0 on success. */
3854 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3856 struct elf_link_hash_table
*hash_table
;
3860 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3863 hash_table
= elf_hash_table (info
);
3864 soname
= elf_dt_name (abfd
);
3865 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, false);
3866 if (strindex
== (size_t) -1)
3869 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3872 const struct elf_backend_data
*bed
;
3875 bed
= get_elf_backend_data (hash_table
->dynobj
);
3876 sdyn
= hash_table
->dynamic
;
3877 if (sdyn
!= NULL
&& sdyn
->size
!= 0)
3878 for (extdyn
= sdyn
->contents
;
3879 extdyn
< sdyn
->contents
+ sdyn
->size
;
3880 extdyn
+= bed
->s
->sizeof_dyn
)
3882 Elf_Internal_Dyn dyn
;
3884 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3885 if (dyn
.d_tag
== DT_NEEDED
3886 && dyn
.d_un
.d_val
== strindex
)
3888 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3894 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3897 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3903 /* Return true if SONAME is on the needed list between NEEDED and STOP
3904 (or the end of list if STOP is NULL), and needed by a library that
3908 on_needed_list (const char *soname
,
3909 struct bfd_link_needed_list
*needed
,
3910 struct bfd_link_needed_list
*stop
)
3912 struct bfd_link_needed_list
*look
;
3913 for (look
= needed
; look
!= stop
; look
= look
->next
)
3914 if (strcmp (soname
, look
->name
) == 0
3915 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3916 /* If needed by a library that itself is not directly
3917 needed, recursively check whether that library is
3918 indirectly needed. Since we add DT_NEEDED entries to
3919 the end of the list, library dependencies appear after
3920 the library. Therefore search prior to the current
3921 LOOK, preventing possible infinite recursion. */
3922 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3928 /* Sort symbol by value, section, size, and type. */
3930 elf_sort_symbol (const void *arg1
, const void *arg2
)
3932 const struct elf_link_hash_entry
*h1
;
3933 const struct elf_link_hash_entry
*h2
;
3934 bfd_signed_vma vdiff
;
3939 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3940 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3941 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3943 return vdiff
> 0 ? 1 : -1;
3945 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3949 /* Sort so that sized symbols are selected over zero size symbols. */
3950 vdiff
= h1
->size
- h2
->size
;
3952 return vdiff
> 0 ? 1 : -1;
3954 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3955 if (h1
->type
!= h2
->type
)
3956 return h1
->type
- h2
->type
;
3958 /* If symbols are properly sized and typed, and multiple strong
3959 aliases are not defined in a shared library by the user we
3960 shouldn't get here. Unfortunately linker script symbols like
3961 __bss_start sometimes match a user symbol defined at the start of
3962 .bss without proper size and type. We'd like to preference the
3963 user symbol over reserved system symbols. Sort on leading
3965 n1
= h1
->root
.root
.string
;
3966 n2
= h2
->root
.root
.string
;
3979 /* Final sort on name selects user symbols like '_u' over reserved
3980 system symbols like '_Z' and also will avoid qsort instability. */
3984 /* This function is used to adjust offsets into .dynstr for
3985 dynamic symbols. This is called via elf_link_hash_traverse. */
3988 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3990 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3992 if (h
->dynindx
!= -1)
3993 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3997 /* Assign string offsets in .dynstr, update all structures referencing
4001 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
4003 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
4004 struct elf_link_local_dynamic_entry
*entry
;
4005 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
4006 bfd
*dynobj
= hash_table
->dynobj
;
4009 const struct elf_backend_data
*bed
;
4012 _bfd_elf_strtab_finalize (dynstr
);
4013 size
= _bfd_elf_strtab_size (dynstr
);
4015 /* Allow the linker to examine the dynsymtab now it's fully populated. */
4017 if (info
->callbacks
->examine_strtab
)
4018 info
->callbacks
->examine_strtab (dynstr
);
4020 bed
= get_elf_backend_data (dynobj
);
4021 sdyn
= hash_table
->dynamic
;
4022 BFD_ASSERT (sdyn
!= NULL
);
4024 /* Update all .dynamic entries referencing .dynstr strings. */
4025 for (extdyn
= sdyn
->contents
;
4026 extdyn
< PTR_ADD (sdyn
->contents
, sdyn
->size
);
4027 extdyn
+= bed
->s
->sizeof_dyn
)
4029 Elf_Internal_Dyn dyn
;
4031 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
4035 dyn
.d_un
.d_val
= size
;
4045 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
4050 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
4053 /* Now update local dynamic symbols. */
4054 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
4055 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
4056 entry
->isym
.st_name
);
4058 /* And the rest of dynamic symbols. */
4059 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
4061 /* Adjust version definitions. */
4062 if (elf_tdata (output_bfd
)->cverdefs
)
4067 Elf_Internal_Verdef def
;
4068 Elf_Internal_Verdaux defaux
;
4070 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
4074 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
4076 p
+= sizeof (Elf_External_Verdef
);
4077 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
4079 for (i
= 0; i
< def
.vd_cnt
; ++i
)
4081 _bfd_elf_swap_verdaux_in (output_bfd
,
4082 (Elf_External_Verdaux
*) p
, &defaux
);
4083 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
4085 _bfd_elf_swap_verdaux_out (output_bfd
,
4086 &defaux
, (Elf_External_Verdaux
*) p
);
4087 p
+= sizeof (Elf_External_Verdaux
);
4090 while (def
.vd_next
);
4093 /* Adjust version references. */
4094 if (elf_tdata (output_bfd
)->verref
)
4099 Elf_Internal_Verneed need
;
4100 Elf_Internal_Vernaux needaux
;
4102 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
4106 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
4108 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
4109 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
4110 (Elf_External_Verneed
*) p
);
4111 p
+= sizeof (Elf_External_Verneed
);
4112 for (i
= 0; i
< need
.vn_cnt
; ++i
)
4114 _bfd_elf_swap_vernaux_in (output_bfd
,
4115 (Elf_External_Vernaux
*) p
, &needaux
);
4116 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
4118 _bfd_elf_swap_vernaux_out (output_bfd
,
4120 (Elf_External_Vernaux
*) p
);
4121 p
+= sizeof (Elf_External_Vernaux
);
4124 while (need
.vn_next
);
4130 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4131 The default is to only match when the INPUT and OUTPUT are exactly
4135 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
4136 const bfd_target
*output
)
4138 return input
== output
;
4141 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4142 This version is used when different targets for the same architecture
4143 are virtually identical. */
4146 _bfd_elf_relocs_compatible (const bfd_target
*input
,
4147 const bfd_target
*output
)
4149 const struct elf_backend_data
*obed
, *ibed
;
4151 if (input
== output
)
4154 ibed
= xvec_get_elf_backend_data (input
);
4155 obed
= xvec_get_elf_backend_data (output
);
4157 if (ibed
->arch
!= obed
->arch
)
4160 /* If both backends are using this function, deem them compatible. */
4161 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
4164 /* Make a special call to the linker "notice" function to tell it that
4165 we are about to handle an as-needed lib, or have finished
4166 processing the lib. */
4169 _bfd_elf_notice_as_needed (bfd
*ibfd
,
4170 struct bfd_link_info
*info
,
4171 enum notice_asneeded_action act
)
4173 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
4176 /* Call ACTION on each relocation in an ELF object file. */
4179 _bfd_elf_link_iterate_on_relocs
4180 (bfd
*abfd
, struct bfd_link_info
*info
,
4181 bool (*action
) (bfd
*, struct bfd_link_info
*, asection
*,
4182 const Elf_Internal_Rela
*))
4184 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4185 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4187 /* If this object is the same format as the output object, and it is
4188 not a shared library, then let the backend look through the
4191 This is required to build global offset table entries and to
4192 arrange for dynamic relocs. It is not required for the
4193 particular common case of linking non PIC code, even when linking
4194 against shared libraries, but unfortunately there is no way of
4195 knowing whether an object file has been compiled PIC or not.
4196 Looking through the relocs is not particularly time consuming.
4197 The problem is that we must either (1) keep the relocs in memory,
4198 which causes the linker to require additional runtime memory or
4199 (2) read the relocs twice from the input file, which wastes time.
4200 This would be a good case for using mmap.
4202 I have no idea how to handle linking PIC code into a file of a
4203 different format. It probably can't be done. */
4204 if ((abfd
->flags
& DYNAMIC
) == 0
4205 && is_elf_hash_table (&htab
->root
)
4206 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4207 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4211 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4213 Elf_Internal_Rela
*internal_relocs
;
4216 /* Don't check relocations in excluded sections. Don't do
4217 anything special with non-loaded, non-alloced sections.
4218 In particular, any relocs in such sections should not
4219 affect GOT and PLT reference counting (ie. we don't
4220 allow them to create GOT or PLT entries), there's no
4221 possibility or desire to optimize TLS relocs, and
4222 there's not much point in propagating relocs to shared
4223 libs that the dynamic linker won't relocate. */
4224 if ((o
->flags
& SEC_ALLOC
) == 0
4225 || (o
->flags
& SEC_RELOC
) == 0
4226 || (o
->flags
& SEC_EXCLUDE
) != 0
4227 || o
->reloc_count
== 0
4228 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4229 && (o
->flags
& SEC_DEBUGGING
) != 0)
4230 || bfd_is_abs_section (o
->output_section
))
4233 internal_relocs
= _bfd_elf_link_info_read_relocs
4234 (abfd
, info
, o
, NULL
, NULL
,
4235 _bfd_elf_link_keep_memory (info
));
4236 if (internal_relocs
== NULL
)
4239 ok
= action (abfd
, info
, o
, internal_relocs
);
4241 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4242 free (internal_relocs
);
4252 /* Check relocations in an ELF object file. This is called after
4253 all input files have been opened. */
4256 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
4258 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4259 if (bed
->check_relocs
!= NULL
)
4260 return _bfd_elf_link_iterate_on_relocs (abfd
, info
,
4265 /* An entry in the first definition hash table. */
4267 struct elf_link_first_hash_entry
4269 struct bfd_hash_entry root
;
4270 /* The object of the first definition. */
4274 /* The function to create a new entry in the first definition hash
4277 static struct bfd_hash_entry
*
4278 elf_link_first_hash_newfunc (struct bfd_hash_entry
*entry
,
4279 struct bfd_hash_table
*table
,
4282 struct elf_link_first_hash_entry
*ret
=
4283 (struct elf_link_first_hash_entry
*) entry
;
4285 /* Allocate the structure if it has not already been allocated by a
4288 ret
= (struct elf_link_first_hash_entry
*)
4289 bfd_hash_allocate (table
,
4290 sizeof (struct elf_link_first_hash_entry
));
4294 /* Call the allocation method of the superclass. */
4295 ret
= ((struct elf_link_first_hash_entry
*)
4296 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
,
4301 return (struct bfd_hash_entry
*) ret
;
4304 /* Add the symbol NAME from ABFD to first hash. */
4307 elf_link_add_to_first_hash (bfd
*abfd
, struct bfd_link_info
*info
,
4308 const char *name
, bool copy
)
4310 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4311 /* Skip if there is no first hash. */
4312 if (htab
->first_hash
== NULL
)
4315 struct elf_link_first_hash_entry
*e
4316 = ((struct elf_link_first_hash_entry
*)
4317 bfd_hash_lookup (htab
->first_hash
, name
, true, copy
));
4319 info
->callbacks
->einfo
4320 (_("%F%P: %pB: failed to add %s to first hash\n"), abfd
, name
);
4322 if (e
->abfd
== NULL
)
4323 /* Store ABFD in abfd. */
4327 /* Add symbols from an ELF object file to the linker hash table. */
4330 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4332 Elf_Internal_Ehdr
*ehdr
;
4333 Elf_Internal_Shdr
*hdr
;
4337 struct elf_link_hash_entry
**sym_hash
;
4339 Elf_External_Versym
*extversym
= NULL
;
4340 Elf_External_Versym
*extversym_end
= NULL
;
4341 Elf_External_Versym
*ever
;
4342 struct elf_link_hash_entry
*weaks
;
4343 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4344 size_t nondeflt_vers_cnt
= 0;
4345 Elf_Internal_Sym
*isymbuf
= NULL
;
4346 Elf_Internal_Sym
*isym
;
4347 Elf_Internal_Sym
*isymend
;
4348 const struct elf_backend_data
*bed
;
4350 struct elf_link_hash_table
*htab
;
4351 void *alloc_mark
= NULL
;
4352 struct bfd_hash_entry
**old_table
= NULL
;
4353 unsigned int old_size
= 0;
4354 unsigned int old_count
= 0;
4355 void *old_tab
= NULL
;
4357 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4358 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4359 void *old_strtab
= NULL
;
4364 htab
= elf_hash_table (info
);
4365 bed
= get_elf_backend_data (abfd
);
4367 if (elf_use_dt_symtab_p (abfd
))
4369 bfd_set_error (bfd_error_wrong_format
);
4373 if ((abfd
->flags
& DYNAMIC
) == 0)
4376 if ((abfd
->flags
& BFD_PLUGIN
) != 0
4377 && is_elf_hash_table (&htab
->root
)
4378 && htab
->first_hash
== NULL
)
4380 /* Initialize first_hash for an IR input. */
4381 htab
->first_hash
= (struct bfd_hash_table
*)
4382 bfd_malloc (sizeof (struct bfd_hash_table
));
4383 if (htab
->first_hash
== NULL
4384 || !bfd_hash_table_init
4385 (htab
->first_hash
, elf_link_first_hash_newfunc
,
4386 sizeof (struct elf_link_first_hash_entry
)))
4387 info
->callbacks
->einfo
4388 (_("%F%P: first_hash failed to create: %E\n"));
4395 /* You can't use -r against a dynamic object. Also, there's no
4396 hope of using a dynamic object which does not exactly match
4397 the format of the output file. */
4398 if (bfd_link_relocatable (info
)
4399 || !is_elf_hash_table (&htab
->root
)
4400 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4402 if (bfd_link_relocatable (info
))
4403 bfd_set_error (bfd_error_invalid_operation
);
4405 bfd_set_error (bfd_error_wrong_format
);
4410 ehdr
= elf_elfheader (abfd
);
4411 if (info
->warn_alternate_em
4412 && bed
->elf_machine_code
!= ehdr
->e_machine
4413 && ((bed
->elf_machine_alt1
!= 0
4414 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4415 || (bed
->elf_machine_alt2
!= 0
4416 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4418 /* xgettext:c-format */
4419 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4420 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4422 /* As a GNU extension, any input sections which are named
4423 .gnu.warning.SYMBOL are treated as warning symbols for the given
4424 symbol. This differs from .gnu.warning sections, which generate
4425 warnings when they are included in an output file. */
4426 /* PR 12761: Also generate this warning when building shared libraries. */
4427 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4431 name
= bfd_section_name (s
);
4432 if (startswith (name
, ".gnu.warning."))
4437 name
+= sizeof ".gnu.warning." - 1;
4439 /* If this is a shared object, then look up the symbol
4440 in the hash table. If it is there, and it is already
4441 been defined, then we will not be using the entry
4442 from this shared object, so we don't need to warn.
4443 FIXME: If we see the definition in a regular object
4444 later on, we will warn, but we shouldn't. The only
4445 fix is to keep track of what warnings we are supposed
4446 to emit, and then handle them all at the end of the
4450 struct elf_link_hash_entry
*h
;
4452 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
4454 /* FIXME: What about bfd_link_hash_common? */
4456 && (h
->root
.type
== bfd_link_hash_defined
4457 || h
->root
.type
== bfd_link_hash_defweak
))
4462 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4466 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4471 if (! (_bfd_generic_link_add_one_symbol
4472 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4473 false, bed
->collect
, NULL
)))
4476 if (bfd_link_executable (info
))
4478 /* Clobber the section size so that the warning does
4479 not get copied into the output file. */
4482 /* Also set SEC_EXCLUDE, so that symbols defined in
4483 the warning section don't get copied to the output. */
4484 s
->flags
|= SEC_EXCLUDE
;
4489 just_syms
= ((s
= abfd
->sections
) != NULL
4490 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4495 /* If we are creating a shared library, create all the dynamic
4496 sections immediately. We need to attach them to something,
4497 so we attach them to this BFD, provided it is the right
4498 format and is not from ld --just-symbols. Always create the
4499 dynamic sections for -E/--dynamic-list. FIXME: If there
4500 are no input BFD's of the same format as the output, we can't
4501 make a shared library. */
4503 && (bfd_link_pic (info
)
4504 || (!bfd_link_relocatable (info
)
4506 && (info
->export_dynamic
|| info
->dynamic
)))
4507 && is_elf_hash_table (&htab
->root
)
4508 && info
->output_bfd
->xvec
== abfd
->xvec
4509 && !htab
->dynamic_sections_created
)
4511 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4515 else if (!is_elf_hash_table (&htab
->root
))
4519 const char *soname
= NULL
;
4521 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4522 const Elf_Internal_Phdr
*phdr
;
4523 struct elf_link_loaded_list
*loaded_lib
;
4525 /* ld --just-symbols and dynamic objects don't mix very well.
4526 ld shouldn't allow it. */
4530 /* If this dynamic lib was specified on the command line with
4531 --as-needed in effect, then we don't want to add a DT_NEEDED
4532 tag unless the lib is actually used. Similary for libs brought
4533 in by another lib's DT_NEEDED. When --no-add-needed is used
4534 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4535 any dynamic library in DT_NEEDED tags in the dynamic lib at
4537 add_needed
= (elf_dyn_lib_class (abfd
)
4538 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4539 | DYN_NO_NEEDED
)) == 0;
4541 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4542 if (s
!= NULL
&& s
->size
!= 0 && (s
->flags
& SEC_HAS_CONTENTS
) != 0)
4546 unsigned int elfsec
;
4547 unsigned long shlink
;
4549 if (!_bfd_elf_mmap_section_contents (abfd
, s
, &dynbuf
))
4552 _bfd_elf_munmap_section_contents (s
, dynbuf
);
4556 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4557 if (elfsec
== SHN_BAD
)
4558 goto error_free_dyn
;
4559 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4561 for (extdyn
= dynbuf
;
4562 (size_t) (dynbuf
+ s
->size
- extdyn
) >= bed
->s
->sizeof_dyn
;
4563 extdyn
+= bed
->s
->sizeof_dyn
)
4565 Elf_Internal_Dyn dyn
;
4567 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4568 if (dyn
.d_tag
== DT_SONAME
)
4570 unsigned int tagv
= dyn
.d_un
.d_val
;
4571 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4573 goto error_free_dyn
;
4575 if (dyn
.d_tag
== DT_NEEDED
)
4577 struct bfd_link_needed_list
*n
, **pn
;
4579 unsigned int tagv
= dyn
.d_un
.d_val
;
4580 size_t amt
= sizeof (struct bfd_link_needed_list
);
4582 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4583 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4584 if (n
== NULL
|| fnm
== NULL
)
4585 goto error_free_dyn
;
4586 amt
= strlen (fnm
) + 1;
4587 anm
= (char *) bfd_alloc (abfd
, amt
);
4589 goto error_free_dyn
;
4590 memcpy (anm
, fnm
, amt
);
4594 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4598 if (dyn
.d_tag
== DT_RUNPATH
)
4600 struct bfd_link_needed_list
*n
, **pn
;
4602 unsigned int tagv
= dyn
.d_un
.d_val
;
4603 size_t amt
= sizeof (struct bfd_link_needed_list
);
4605 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4606 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4607 if (n
== NULL
|| fnm
== NULL
)
4608 goto error_free_dyn
;
4609 amt
= strlen (fnm
) + 1;
4610 anm
= (char *) bfd_alloc (abfd
, amt
);
4612 goto error_free_dyn
;
4613 memcpy (anm
, fnm
, amt
);
4617 for (pn
= & runpath
;
4623 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4624 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4626 struct bfd_link_needed_list
*n
, **pn
;
4628 unsigned int tagv
= dyn
.d_un
.d_val
;
4629 size_t amt
= sizeof (struct bfd_link_needed_list
);
4631 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4632 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4633 if (n
== NULL
|| fnm
== NULL
)
4634 goto error_free_dyn
;
4635 amt
= strlen (fnm
) + 1;
4636 anm
= (char *) bfd_alloc (abfd
, amt
);
4638 goto error_free_dyn
;
4639 memcpy (anm
, fnm
, amt
);
4649 if (dyn
.d_tag
== DT_AUDIT
)
4651 unsigned int tagv
= dyn
.d_un
.d_val
;
4652 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4654 if (dyn
.d_tag
== DT_FLAGS_1
)
4655 elf_tdata (abfd
)->is_pie
= (dyn
.d_un
.d_val
& DF_1_PIE
) != 0;
4658 _bfd_elf_munmap_section_contents (s
, dynbuf
);
4661 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4662 frees all more recently bfd_alloc'd blocks as well. */
4668 struct bfd_link_needed_list
**pn
;
4669 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4674 /* If we have a PT_GNU_RELRO program header, mark as read-only
4675 all sections contained fully therein. This makes relro
4676 shared library sections appear as they will at run-time. */
4677 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4678 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4679 if (phdr
->p_type
== PT_GNU_RELRO
)
4681 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4683 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4685 if ((s
->flags
& SEC_ALLOC
) != 0
4686 && s
->vma
* opb
>= phdr
->p_vaddr
4687 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4688 s
->flags
|= SEC_READONLY
;
4693 /* We do not want to include any of the sections in a dynamic
4694 object in the output file. We hack by simply clobbering the
4695 list of sections in the BFD. This could be handled more
4696 cleanly by, say, a new section flag; the existing
4697 SEC_NEVER_LOAD flag is not the one we want, because that one
4698 still implies that the section takes up space in the output
4700 bfd_section_list_clear (abfd
);
4702 /* Find the name to use in a DT_NEEDED entry that refers to this
4703 object. If the object has a DT_SONAME entry, we use it.
4704 Otherwise, if the generic linker stuck something in
4705 elf_dt_name, we use that. Otherwise, we just use the file
4707 if (soname
== NULL
|| *soname
== '\0')
4709 soname
= elf_dt_name (abfd
);
4710 if (soname
== NULL
|| *soname
== '\0')
4711 soname
= bfd_get_filename (abfd
);
4714 /* Save the SONAME because sometimes the linker emulation code
4715 will need to know it. */
4716 elf_dt_name (abfd
) = soname
;
4718 /* If we have already included this dynamic object in the
4719 link, just ignore it. There is no reason to include a
4720 particular dynamic object more than once. */
4721 for (loaded_lib
= htab
->dyn_loaded
;
4723 loaded_lib
= loaded_lib
->next
)
4725 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4729 /* Create dynamic sections for backends that require that be done
4730 before setup_gnu_properties. */
4732 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4735 /* Save the DT_AUDIT entry for the linker emulation code. */
4736 elf_dt_audit (abfd
) = audit
;
4739 /* If this is a dynamic object, we always link against the .dynsym
4740 symbol table, not the .symtab symbol table. The dynamic linker
4741 will only see the .dynsym symbol table, so there is no reason to
4742 look at .symtab for a dynamic object. */
4744 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4745 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4747 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4749 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4751 /* The sh_info field of the symtab header tells us where the
4752 external symbols start. We don't care about the local symbols at
4754 if (elf_bad_symtab (abfd
))
4756 extsymcount
= symcount
;
4761 extsymcount
= symcount
- hdr
->sh_info
;
4762 extsymoff
= hdr
->sh_info
;
4765 sym_hash
= elf_sym_hashes (abfd
);
4766 if (extsymcount
!= 0)
4768 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4770 if (isymbuf
== NULL
)
4773 if (sym_hash
== NULL
)
4775 /* We store a pointer to the hash table entry for each
4777 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4778 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4779 if (sym_hash
== NULL
)
4780 goto error_free_sym
;
4781 elf_sym_hashes (abfd
) = sym_hash
;
4787 /* Read in any version definitions. */
4788 if (!_bfd_elf_slurp_version_tables (abfd
,
4789 info
->default_imported_symver
))
4790 goto error_free_sym
;
4792 /* Read in the symbol versions, but don't bother to convert them
4793 to internal format. */
4794 if (elf_dynversym (abfd
) != 0)
4796 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4797 bfd_size_type amt
= versymhdr
->sh_size
;
4799 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4800 goto error_free_sym
;
4801 extversym
= (Elf_External_Versym
*)
4802 _bfd_malloc_and_read (abfd
, amt
, amt
);
4803 if (extversym
== NULL
)
4804 goto error_free_sym
;
4805 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4809 /* If we are loading an as-needed shared lib, save the symbol table
4810 state before we start adding symbols. If the lib turns out
4811 to be unneeded, restore the state. */
4812 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4817 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4819 struct bfd_hash_entry
*p
;
4820 struct elf_link_hash_entry
*h
;
4822 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4824 h
= (struct elf_link_hash_entry
*) p
;
4825 entsize
+= htab
->root
.table
.entsize
;
4826 if (h
->root
.type
== bfd_link_hash_warning
)
4828 entsize
+= htab
->root
.table
.entsize
;
4829 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4831 if (h
->root
.type
== bfd_link_hash_common
)
4832 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4836 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4837 old_tab
= bfd_malloc (tabsize
+ entsize
);
4838 if (old_tab
== NULL
)
4839 goto error_free_vers
;
4841 /* Remember the current objalloc pointer, so that all mem for
4842 symbols added can later be reclaimed. */
4843 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4844 if (alloc_mark
== NULL
)
4845 goto error_free_vers
;
4847 /* Make a special call to the linker "notice" function to
4848 tell it that we are about to handle an as-needed lib. */
4849 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4850 goto error_free_vers
;
4852 /* Clone the symbol table. Remember some pointers into the
4853 symbol table, and dynamic symbol count. */
4854 old_ent
= (char *) old_tab
+ tabsize
;
4855 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4856 old_undefs
= htab
->root
.undefs
;
4857 old_undefs_tail
= htab
->root
.undefs_tail
;
4858 old_table
= htab
->root
.table
.table
;
4859 old_size
= htab
->root
.table
.size
;
4860 old_count
= htab
->root
.table
.count
;
4862 if (htab
->dynstr
!= NULL
)
4864 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4865 if (old_strtab
== NULL
)
4866 goto error_free_vers
;
4869 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4871 struct bfd_hash_entry
*p
;
4872 struct elf_link_hash_entry
*h
;
4874 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4876 h
= (struct elf_link_hash_entry
*) p
;
4877 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4878 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4879 if (h
->root
.type
== bfd_link_hash_warning
)
4881 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4882 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4883 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4885 if (h
->root
.type
== bfd_link_hash_common
)
4887 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4888 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4895 if (extversym
== NULL
)
4897 else if (extversym
+ extsymoff
< extversym_end
)
4898 ever
= extversym
+ extsymoff
;
4901 /* xgettext:c-format */
4902 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4903 abfd
, (long) extsymoff
,
4904 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4905 bfd_set_error (bfd_error_bad_value
);
4906 goto error_free_vers
;
4909 if (!bfd_link_relocatable (info
)
4910 && bfd_get_lto_type (abfd
) == lto_slim_ir_object
)
4913 (_("%pB: plugin needed to handle lto object"), abfd
);
4916 for (isym
= isymbuf
, isymend
= PTR_ADD (isymbuf
, extsymcount
);
4918 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4922 asection
*sec
, *new_sec
;
4925 bool must_copy_name
= false;
4926 struct elf_link_hash_entry
*h
;
4927 struct elf_link_hash_entry
*hi
;
4929 bool size_change_ok
;
4930 bool type_change_ok
;
4936 unsigned int old_alignment
;
4937 unsigned int shindex
;
4943 flags
= BSF_NO_FLAGS
;
4945 value
= isym
->st_value
;
4946 common
= bed
->common_definition (isym
);
4947 if (common
&& info
->inhibit_common_definition
)
4949 /* Treat common symbol as undefined for --no-define-common. */
4950 isym
->st_shndx
= SHN_UNDEF
;
4955 bind
= ELF_ST_BIND (isym
->st_info
);
4959 /* This should be impossible, since ELF requires that all
4960 global symbols follow all local symbols, and that sh_info
4961 point to the first global symbol. Unfortunately, Irix 5
4963 if (elf_bad_symtab (abfd
))
4966 /* If we aren't prepared to handle locals within the globals
4967 then we'll likely segfault on a NULL symbol hash if the
4968 symbol is ever referenced in relocations. */
4969 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4970 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4971 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4972 " (>= sh_info of %lu)"),
4973 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4976 /* Dynamic object relocations are not processed by ld, so
4977 ld won't run into the problem mentioned above. */
4980 bfd_set_error (bfd_error_bad_value
);
4981 goto error_free_vers
;
4984 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4992 case STB_GNU_UNIQUE
:
4993 flags
= BSF_GNU_UNIQUE
;
4997 /* Leave it up to the processor backend. */
5001 if (isym
->st_shndx
== SHN_UNDEF
)
5002 sec
= bfd_und_section_ptr
;
5003 else if (isym
->st_shndx
== SHN_ABS
)
5004 sec
= bfd_abs_section_ptr
;
5005 else if (isym
->st_shndx
== SHN_COMMON
)
5007 sec
= bfd_com_section_ptr
;
5008 /* What ELF calls the size we call the value. What ELF
5009 calls the value we call the alignment. */
5010 value
= isym
->st_size
;
5014 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
5016 sec
= bfd_abs_section_ptr
;
5017 else if (discarded_section (sec
))
5019 /* Symbols from discarded section are undefined. We keep
5021 sec
= bfd_und_section_ptr
;
5023 isym
->st_shndx
= SHN_UNDEF
;
5025 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
5029 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5032 goto error_free_vers
;
5034 if (isym
->st_shndx
== SHN_COMMON
5035 && (abfd
->flags
& BFD_PLUGIN
) != 0)
5037 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
5041 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
5043 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
5045 goto error_free_vers
;
5049 else if (isym
->st_shndx
== SHN_COMMON
5050 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
5051 && !bfd_link_relocatable (info
))
5053 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
5057 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
5058 | SEC_LINKER_CREATED
);
5059 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
5061 goto error_free_vers
;
5065 else if (bed
->elf_add_symbol_hook
)
5067 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
5069 goto error_free_vers
;
5071 /* The hook function sets the name to NULL if this symbol
5072 should be skipped for some reason. */
5077 /* Sanity check that all possibilities were handled. */
5081 /* Silently discard TLS symbols from --just-syms. There's
5082 no way to combine a static TLS block with a new TLS block
5083 for this executable. */
5084 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
5085 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
5088 if (bfd_is_und_section (sec
)
5089 || bfd_is_com_section (sec
))
5094 size_change_ok
= false;
5095 type_change_ok
= bed
->type_change_ok
;
5102 if (is_elf_hash_table (&htab
->root
))
5104 Elf_Internal_Versym iver
;
5105 unsigned int vernum
= 0;
5110 if (info
->default_imported_symver
)
5111 /* Use the default symbol version created earlier. */
5112 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
5116 else if (ever
>= extversym_end
)
5118 /* xgettext:c-format */
5119 _bfd_error_handler (_("%pB: not enough version information"),
5121 bfd_set_error (bfd_error_bad_value
);
5122 goto error_free_vers
;
5125 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
5127 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
5129 /* If this is a hidden symbol, or if it is not version
5130 1, we append the version name to the symbol name.
5131 However, we do not modify a non-hidden absolute symbol
5132 if it is not a function, because it might be the version
5133 symbol itself. FIXME: What if it isn't? */
5134 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
5136 && (!bfd_is_abs_section (sec
)
5137 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
5140 size_t namelen
, verlen
, newlen
;
5143 if (isym
->st_shndx
!= SHN_UNDEF
)
5145 if (vernum
> elf_tdata (abfd
)->cverdefs
)
5147 else if (vernum
> 1)
5149 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
5156 /* xgettext:c-format */
5157 (_("%pB: %s: invalid version %u (max %d)"),
5159 elf_tdata (abfd
)->cverdefs
);
5160 bfd_set_error (bfd_error_bad_value
);
5161 goto error_free_vers
;
5166 /* We cannot simply test for the number of
5167 entries in the VERNEED section since the
5168 numbers for the needed versions do not start
5170 Elf_Internal_Verneed
*t
;
5173 for (t
= elf_tdata (abfd
)->verref
;
5177 Elf_Internal_Vernaux
*a
;
5179 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5181 if (a
->vna_other
== vernum
)
5183 verstr
= a
->vna_nodename
;
5193 /* xgettext:c-format */
5194 (_("%pB: %s: invalid needed version %d"),
5195 abfd
, name
, vernum
);
5196 bfd_set_error (bfd_error_bad_value
);
5197 goto error_free_vers
;
5201 namelen
= strlen (name
);
5202 verlen
= strlen (verstr
);
5203 newlen
= namelen
+ verlen
+ 2;
5204 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5205 && isym
->st_shndx
!= SHN_UNDEF
)
5208 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
5209 if (newname
== NULL
)
5210 goto error_free_vers
;
5211 memcpy (newname
, name
, namelen
);
5212 p
= newname
+ namelen
;
5214 /* If this is a defined non-hidden version symbol,
5215 we add another @ to the name. This indicates the
5216 default version of the symbol. */
5217 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5218 && isym
->st_shndx
!= SHN_UNDEF
)
5220 memcpy (p
, verstr
, verlen
+ 1);
5223 /* Since bfd_hash_alloc is used for "name", the string
5224 must be copied if added to first_hash. The string
5225 memory can be freed when an --as-needed library is
5227 must_copy_name
= true;
5230 /* If this symbol has default visibility and the user has
5231 requested we not re-export it, then mark it as hidden. */
5232 if (!bfd_is_und_section (sec
)
5235 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
5236 isym
->st_other
= (STV_HIDDEN
5237 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
5239 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
5240 sym_hash
, &old_bfd
, &old_weak
,
5241 &old_alignment
, &skip
, &override
,
5242 &type_change_ok
, &size_change_ok
,
5244 goto error_free_vers
;
5250 while (h
->root
.type
== bfd_link_hash_indirect
5251 || h
->root
.type
== bfd_link_hash_warning
)
5252 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5254 /* Override a definition only if the new symbol matches the
5256 if (override
&& matched
)
5259 if (htab
->first_hash
!= NULL
5260 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5261 && h
->root
.non_ir_ref_regular
)
5263 /* When reloading --as-needed shared objects for new
5264 symbols added from IR inputs, if this shared object
5265 has the first definition, use it. */
5266 struct elf_link_first_hash_entry
*e
5267 = ((struct elf_link_first_hash_entry
*)
5268 bfd_hash_lookup (htab
->first_hash
, name
, false,
5270 if (e
!= NULL
&& e
->abfd
== abfd
)
5275 if (h
->versioned
!= unversioned
5276 && elf_tdata (abfd
)->verdef
!= NULL
5279 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
5282 if (! (_bfd_generic_link_add_one_symbol
5283 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
5284 NULL
, false, bed
->collect
,
5285 (struct bfd_link_hash_entry
**) sym_hash
)))
5286 goto error_free_vers
;
5289 /* We need to make sure that indirect symbol dynamic flags are
5292 while (h
->root
.type
== bfd_link_hash_indirect
5293 || h
->root
.type
== bfd_link_hash_warning
)
5294 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5298 /* Setting the index to -3 tells elf_link_output_extsym that
5299 this symbol is defined in a discarded section. */
5300 if (discarded
&& is_elf_hash_table (&htab
->root
))
5303 new_weak
= (flags
& BSF_WEAK
) != 0;
5307 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
5308 && is_elf_hash_table (&htab
->root
)
5309 && h
->u
.alias
== NULL
)
5311 /* Keep a list of all weak defined non function symbols from
5312 a dynamic object, using the alias field. Later in this
5313 function we will set the alias field to the correct
5314 value. We only put non-function symbols from dynamic
5315 objects on this list, because that happens to be the only
5316 time we need to know the normal symbol corresponding to a
5317 weak symbol, and the information is time consuming to
5318 figure out. If the alias field is not already NULL,
5319 then this symbol was already defined by some previous
5320 dynamic object, and we will be using that previous
5321 definition anyhow. */
5327 /* Set the alignment of a common symbol. */
5328 if ((common
|| bfd_is_com_section (sec
))
5329 && h
->root
.type
== bfd_link_hash_common
)
5334 align
= bfd_log2 (isym
->st_value
);
5337 /* The new symbol is a common symbol in a shared object.
5338 We need to get the alignment from the section. */
5339 align
= new_sec
->alignment_power
;
5341 if (align
> old_alignment
)
5342 h
->root
.u
.c
.p
->alignment_power
= align
;
5344 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5347 if (is_elf_hash_table (&htab
->root
))
5349 /* Set a flag in the hash table entry indicating the type of
5350 reference or definition we just found. A dynamic symbol
5351 is one which is referenced or defined by both a regular
5352 object and a shared object. */
5353 bool dynsym
= false;
5355 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5356 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5358 /* Except for this flag to track nonweak references. */
5360 && bind
!= STB_WEAK
)
5361 h
->ref_ir_nonweak
= 1;
5368 if (bind
!= STB_WEAK
)
5369 h
->ref_regular_nonweak
= 1;
5386 hi
->ref_dynamic
= 1;
5391 hi
->def_dynamic
= 1;
5395 /* If an indirect symbol has been forced local, don't
5396 make the real symbol dynamic. */
5397 if (h
!= hi
&& hi
->forced_local
)
5401 if (bfd_link_dll (info
)
5411 && weakdef (h
)->dynindx
!= -1))
5415 /* Check to see if we need to add an indirect symbol for
5416 the default name. */
5418 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5420 && hi
->versioned
== versioned_hidden
))
5421 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5422 sec
, value
, &old_bfd
, &dynsym
))
5423 goto error_free_vers
;
5425 /* Check the alignment when a common symbol is involved. This
5426 can change when a common symbol is overridden by a normal
5427 definition or a common symbol is ignored due to the old
5428 normal definition. We need to make sure the maximum
5429 alignment is maintained. */
5430 if ((old_alignment
|| common
)
5431 && h
->root
.type
!= bfd_link_hash_common
)
5433 unsigned int common_align
;
5434 unsigned int normal_align
;
5435 unsigned int symbol_align
;
5439 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5440 || h
->root
.type
== bfd_link_hash_defweak
);
5442 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5443 if (h
->root
.u
.def
.section
->owner
!= NULL
5444 && (h
->root
.u
.def
.section
->owner
->flags
5445 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5447 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5448 if (normal_align
> symbol_align
)
5449 normal_align
= symbol_align
;
5452 normal_align
= symbol_align
;
5456 common_align
= old_alignment
;
5457 common_bfd
= old_bfd
;
5462 common_align
= bfd_log2 (isym
->st_value
);
5464 normal_bfd
= old_bfd
;
5467 if (normal_align
< common_align
)
5469 /* PR binutils/2735 */
5470 if (normal_bfd
== NULL
)
5472 /* xgettext:c-format */
5473 (_("warning: alignment %u of common symbol `%s' in %pB is"
5474 " greater than the alignment (%u) of its section %pA"),
5475 1 << common_align
, name
, common_bfd
,
5476 1 << normal_align
, h
->root
.u
.def
.section
);
5479 /* xgettext:c-format */
5480 (_("warning: alignment %u of normal symbol `%s' in %pB"
5481 " is smaller than %u used by the common definition in %pB"),
5482 1 << normal_align
, name
, normal_bfd
,
5483 1 << common_align
, common_bfd
);
5485 /* PR 30499: make sure that users understand that this warning is serious. */
5487 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5491 /* Remember the symbol size if it isn't undefined. */
5492 if (isym
->st_size
!= 0
5493 && isym
->st_shndx
!= SHN_UNDEF
5494 && (definition
|| h
->size
== 0))
5497 && h
->size
!= isym
->st_size
5498 && ! size_change_ok
)
5501 /* xgettext:c-format */
5502 (_("warning: size of symbol `%s' changed"
5503 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5504 name
, (uint64_t) h
->size
, old_bfd
,
5505 (uint64_t) isym
->st_size
, abfd
);
5507 /* PR 30499: make sure that users understand that this warning is serious. */
5509 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5512 h
->size
= isym
->st_size
;
5515 /* If this is a common symbol, then we always want H->SIZE
5516 to be the size of the common symbol. The code just above
5517 won't fix the size if a common symbol becomes larger. We
5518 don't warn about a size change here, because that is
5519 covered by --warn-common. Allow changes between different
5521 if (h
->root
.type
== bfd_link_hash_common
)
5522 h
->size
= h
->root
.u
.c
.size
;
5524 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5525 && ((definition
&& !new_weak
)
5526 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5527 || h
->type
== STT_NOTYPE
))
5529 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5531 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5533 if (type
== STT_GNU_IFUNC
5534 && (abfd
->flags
& DYNAMIC
) != 0)
5537 if (h
->type
!= type
)
5539 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5540 /* xgettext:c-format */
5542 (_("warning: type of symbol `%s' changed"
5543 " from %d to %d in %pB"),
5544 name
, h
->type
, type
, abfd
);
5550 /* Merge st_other field. */
5551 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5552 definition
, dynamic
);
5554 /* We don't want to make debug symbol dynamic. */
5556 && (sec
->flags
& SEC_DEBUGGING
)
5557 && !bfd_link_relocatable (info
))
5560 /* Nor should we make plugin symbols dynamic. */
5561 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5566 h
->target_internal
= isym
->st_target_internal
;
5567 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5570 /* Don't add indirect symbols for .symver x, x@FOO aliases
5571 in IR. Since all data or text symbols in IR have the
5572 same type, value and section, we can't tell if a symbol
5573 is an alias of another symbol by their types, values and
5577 && (abfd
->flags
& BFD_PLUGIN
) == 0)
5579 char *p
= strchr (name
, ELF_VER_CHR
);
5580 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5582 /* Queue non-default versions so that .symver x, x@FOO
5583 aliases can be checked. */
5586 size_t amt
= ((isymend
- isym
+ 1)
5587 * sizeof (struct elf_link_hash_entry
*));
5589 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5591 goto error_free_vers
;
5593 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5597 if (dynsym
&& h
->dynindx
== -1)
5599 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5600 goto error_free_vers
;
5602 && weakdef (h
)->dynindx
== -1)
5604 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5605 goto error_free_vers
;
5608 else if (h
->dynindx
!= -1)
5609 /* If the symbol already has a dynamic index, but
5610 visibility says it should not be visible, turn it into
5612 switch (ELF_ST_VISIBILITY (h
->other
))
5616 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
5624 && h
->root
.type
!= bfd_link_hash_indirect
)
5627 && h
->ref_regular_nonweak
)
5629 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5630 && h
->ref_ir_nonweak
5631 && !info
->lto_all_symbols_read
)
5632 || (h
->ref_dynamic_nonweak
5633 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5634 && !on_needed_list (elf_dt_name (abfd
),
5635 htab
->needed
, NULL
)))
5637 const char *soname
= elf_dt_name (abfd
);
5639 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5640 h
->root
.root
.string
);
5642 /* A symbol from a library loaded via DT_NEEDED of some
5643 other library is referenced by a regular object.
5644 Add a DT_NEEDED entry for it. Issue an error if
5645 --no-add-needed is used and the reference was not
5648 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5651 /* xgettext:c-format */
5652 (_("%pB: undefined reference to symbol '%s'"),
5654 bfd_set_error (bfd_error_missing_dso
);
5655 goto error_free_vers
;
5658 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5659 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5661 /* Create dynamic sections for backends that require
5662 that be done before setup_gnu_properties. */
5663 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5668 && h
->root
.u
.def
.section
->owner
== abfd
)
5669 /* Add this symbol to first hash if this shared
5670 object has the first definition. */
5671 elf_link_add_to_first_hash (abfd
, info
, name
, must_copy_name
);
5676 if (info
->lto_plugin_active
5677 && !bfd_link_relocatable (info
)
5678 && (abfd
->flags
& BFD_PLUGIN
) == 0
5684 if (bed
->s
->arch_size
== 32)
5689 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5690 referenced in regular objects so that linker plugin will get
5691 the correct symbol resolution. */
5693 sym_hash
= elf_sym_hashes (abfd
);
5694 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5696 Elf_Internal_Rela
*internal_relocs
;
5697 Elf_Internal_Rela
*rel
, *relend
;
5699 /* Don't check relocations in excluded sections. */
5700 if ((s
->flags
& SEC_RELOC
) == 0
5701 || s
->reloc_count
== 0
5702 || (s
->flags
& SEC_EXCLUDE
) != 0
5703 || ((info
->strip
== strip_all
5704 || info
->strip
== strip_debugger
)
5705 && (s
->flags
& SEC_DEBUGGING
) != 0))
5708 internal_relocs
= _bfd_elf_link_info_read_relocs
5709 (abfd
, info
, s
, NULL
, NULL
,
5710 _bfd_elf_link_keep_memory (info
));
5711 if (internal_relocs
== NULL
)
5712 goto error_free_vers
;
5714 rel
= internal_relocs
;
5715 relend
= rel
+ s
->reloc_count
;
5716 for ( ; rel
< relend
; rel
++)
5718 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5719 struct elf_link_hash_entry
*h
;
5721 /* Skip local symbols. */
5722 if (r_symndx
< extsymoff
)
5725 h
= sym_hash
[r_symndx
- extsymoff
];
5727 h
->root
.non_ir_ref_regular
= 1;
5730 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5731 free (internal_relocs
);
5740 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5744 /* Restore the symbol table. */
5745 old_ent
= (char *) old_tab
+ tabsize
;
5746 memset (elf_sym_hashes (abfd
), 0,
5747 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5748 htab
->root
.table
.table
= old_table
;
5749 htab
->root
.table
.size
= old_size
;
5750 htab
->root
.table
.count
= old_count
;
5751 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5752 htab
->root
.undefs
= old_undefs
;
5753 htab
->root
.undefs_tail
= old_undefs_tail
;
5754 if (htab
->dynstr
!= NULL
)
5755 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5758 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5760 struct bfd_hash_entry
*p
;
5761 struct elf_link_hash_entry
*h
;
5762 unsigned int non_ir_ref_dynamic
;
5764 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5766 /* Preserve non_ir_ref_dynamic so that this symbol
5767 will be exported when the dynamic lib becomes needed
5768 in the second pass. */
5769 h
= (struct elf_link_hash_entry
*) p
;
5770 if (h
->root
.type
== bfd_link_hash_warning
)
5771 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5772 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5774 h
= (struct elf_link_hash_entry
*) p
;
5775 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5776 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5777 if (h
->root
.type
== bfd_link_hash_warning
)
5779 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5780 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5781 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5783 if (h
->root
.type
== bfd_link_hash_common
)
5785 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5786 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5788 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5792 /* Make a special call to the linker "notice" function to
5793 tell it that symbols added for crefs may need to be removed. */
5794 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5795 goto error_free_vers
;
5798 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5800 free (nondeflt_vers
);
5804 if (old_tab
!= NULL
)
5806 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5807 goto error_free_vers
;
5812 /* Now that all the symbols from this input file are created, if
5813 not performing a relocatable link, handle .symver foo, foo@BAR
5814 such that any relocs against foo become foo@BAR. */
5815 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5819 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5821 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5822 char *shortname
, *p
;
5825 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5827 || (h
->root
.type
!= bfd_link_hash_defined
5828 && h
->root
.type
!= bfd_link_hash_defweak
))
5831 amt
= p
- h
->root
.root
.string
;
5832 shortname
= (char *) bfd_malloc (amt
+ 1);
5834 goto error_free_vers
;
5835 memcpy (shortname
, h
->root
.root
.string
, amt
);
5836 shortname
[amt
] = '\0';
5838 hi
= (struct elf_link_hash_entry
*)
5839 bfd_link_hash_lookup (&htab
->root
, shortname
,
5840 false, false, false);
5842 && hi
->root
.type
== h
->root
.type
5843 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5844 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5846 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
5847 hi
->root
.type
= bfd_link_hash_indirect
;
5848 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5849 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5850 sym_hash
= elf_sym_hashes (abfd
);
5852 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5853 if (sym_hash
[symidx
] == hi
)
5855 sym_hash
[symidx
] = h
;
5861 free (nondeflt_vers
);
5862 nondeflt_vers
= NULL
;
5865 /* Now set the alias field correctly for all the weak defined
5866 symbols we found. The only way to do this is to search all the
5867 symbols. Since we only need the information for non functions in
5868 dynamic objects, that's the only time we actually put anything on
5869 the list WEAKS. We need this information so that if a regular
5870 object refers to a symbol defined weakly in a dynamic object, the
5871 real symbol in the dynamic object is also put in the dynamic
5872 symbols; we also must arrange for both symbols to point to the
5873 same memory location. We could handle the general case of symbol
5874 aliasing, but a general symbol alias can only be generated in
5875 assembler code, handling it correctly would be very time
5876 consuming, and other ELF linkers don't handle general aliasing
5880 struct elf_link_hash_entry
**hpp
;
5881 struct elf_link_hash_entry
**hppend
;
5882 struct elf_link_hash_entry
**sorted_sym_hash
;
5883 struct elf_link_hash_entry
*h
;
5884 size_t sym_count
, amt
;
5886 /* Since we have to search the whole symbol list for each weak
5887 defined symbol, search time for N weak defined symbols will be
5888 O(N^2). Binary search will cut it down to O(NlogN). */
5889 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5890 sorted_sym_hash
= bfd_malloc (amt
);
5891 if (sorted_sym_hash
== NULL
)
5893 sym_hash
= sorted_sym_hash
;
5894 hpp
= elf_sym_hashes (abfd
);
5895 hppend
= hpp
+ extsymcount
;
5897 for (; hpp
< hppend
; hpp
++)
5901 && h
->root
.type
== bfd_link_hash_defined
5902 && !bed
->is_function_type (h
->type
))
5910 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5913 while (weaks
!= NULL
)
5915 struct elf_link_hash_entry
*hlook
;
5918 size_t i
, j
, idx
= 0;
5921 weaks
= hlook
->u
.alias
;
5922 hlook
->u
.alias
= NULL
;
5924 if (hlook
->root
.type
!= bfd_link_hash_defined
5925 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5928 slook
= hlook
->root
.u
.def
.section
;
5929 vlook
= hlook
->root
.u
.def
.value
;
5935 bfd_signed_vma vdiff
;
5937 h
= sorted_sym_hash
[idx
];
5938 vdiff
= vlook
- h
->root
.u
.def
.value
;
5945 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5955 /* We didn't find a value/section match. */
5959 /* With multiple aliases, or when the weak symbol is already
5960 strongly defined, we have multiple matching symbols and
5961 the binary search above may land on any of them. Step
5962 one past the matching symbol(s). */
5965 h
= sorted_sym_hash
[idx
];
5966 if (h
->root
.u
.def
.section
!= slook
5967 || h
->root
.u
.def
.value
!= vlook
)
5971 /* Now look back over the aliases. Since we sorted by size
5972 as well as value and section, we'll choose the one with
5973 the largest size. */
5976 h
= sorted_sym_hash
[idx
];
5978 /* Stop if value or section doesn't match. */
5979 if (h
->root
.u
.def
.section
!= slook
5980 || h
->root
.u
.def
.value
!= vlook
)
5982 else if (h
!= hlook
)
5984 struct elf_link_hash_entry
*t
;
5987 hlook
->is_weakalias
= 1;
5989 if (t
->u
.alias
!= NULL
)
5990 while (t
->u
.alias
!= h
)
5994 /* If the weak definition is in the list of dynamic
5995 symbols, make sure the real definition is put
5997 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5999 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6002 free (sorted_sym_hash
);
6007 /* If the real definition is in the list of dynamic
6008 symbols, make sure the weak definition is put
6009 there as well. If we don't do this, then the
6010 dynamic loader might not merge the entries for the
6011 real definition and the weak definition. */
6012 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
6014 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
6015 goto err_free_sym_hash
;
6022 free (sorted_sym_hash
);
6025 if (bed
->check_directives
6026 && !(*bed
->check_directives
) (abfd
, info
))
6029 /* If this is a non-traditional link, try to optimize the handling
6030 of the .stab/.stabstr sections. */
6032 && ! info
->traditional_format
6033 && is_elf_hash_table (&htab
->root
)
6034 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
6038 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
6039 if (stabstr
!= NULL
)
6041 bfd_size_type string_offset
= 0;
6044 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
6045 if (startswith (stab
->name
, ".stab")
6046 && (!stab
->name
[5] ||
6047 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
6048 && (stab
->flags
& SEC_MERGE
) == 0
6049 && !bfd_is_abs_section (stab
->output_section
))
6051 struct bfd_elf_section_data
*secdata
;
6053 secdata
= elf_section_data (stab
);
6054 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
6055 stabstr
, &secdata
->sec_info
,
6058 if (secdata
->sec_info
)
6059 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
6064 if (dynamic
&& add_needed
)
6066 /* Add this bfd to the loaded list. */
6067 struct elf_link_loaded_list
*n
;
6069 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
6073 n
->next
= htab
->dyn_loaded
;
6074 htab
->dyn_loaded
= n
;
6076 if (dynamic
&& !add_needed
6077 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
6078 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
6085 free (nondeflt_vers
);
6093 /* Return the linker hash table entry of a symbol that might be
6094 satisfied by an archive symbol. Return -1 on error. */
6096 struct bfd_link_hash_entry
*
6097 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
6098 struct bfd_link_info
*info
,
6101 struct bfd_link_hash_entry
*h
;
6105 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
6109 /* If this is a default version (the name contains @@), look up the
6110 symbol again with only one `@' as well as without the version.
6111 The effect is that references to the symbol with and without the
6112 version will be matched by the default symbol in the archive. */
6114 p
= strchr (name
, ELF_VER_CHR
);
6115 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
6117 /* Add this symbol to first hash if this archive has the first
6119 if (is_elf_hash_table (info
->hash
))
6120 elf_link_add_to_first_hash (abfd
, info
, name
, false);
6124 /* First check with only one `@'. */
6125 len
= strlen (name
);
6126 copy
= (char *) bfd_alloc (abfd
, len
);
6128 return (struct bfd_link_hash_entry
*) -1;
6130 first
= p
- name
+ 1;
6131 memcpy (copy
, name
, first
);
6132 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
6134 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
6137 /* We also need to check references to the symbol without the
6139 copy
[first
- 1] = '\0';
6140 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
6143 bfd_release (abfd
, copy
);
6147 /* Add symbols from an ELF archive file to the linker hash table. We
6148 don't use _bfd_generic_link_add_archive_symbols because we need to
6149 handle versioned symbols.
6151 Fortunately, ELF archive handling is simpler than that done by
6152 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6153 oddities. In ELF, if we find a symbol in the archive map, and the
6154 symbol is currently undefined, we know that we must pull in that
6157 Unfortunately, we do have to make multiple passes over the symbol
6158 table until nothing further is resolved. */
6161 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6164 unsigned char *included
= NULL
;
6168 const struct elf_backend_data
*bed
;
6169 struct bfd_link_hash_entry
* (*archive_symbol_lookup
)
6170 (bfd
*, struct bfd_link_info
*, const char *);
6172 if (! bfd_has_map (abfd
))
6174 /* An empty archive is a special case. */
6175 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
6177 bfd_set_error (bfd_error_no_armap
);
6181 /* Keep track of all symbols we know to be already defined, and all
6182 files we know to be already included. This is to speed up the
6183 second and subsequent passes. */
6184 c
= bfd_ardata (abfd
)->symdef_count
;
6187 amt
= c
* sizeof (*included
);
6188 included
= (unsigned char *) bfd_zmalloc (amt
);
6189 if (included
== NULL
)
6192 symdefs
= bfd_ardata (abfd
)->symdefs
;
6193 bed
= get_elf_backend_data (abfd
);
6194 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
6207 symdefend
= symdef
+ c
;
6208 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
6210 struct bfd_link_hash_entry
*h
;
6212 struct bfd_link_hash_entry
*undefs_tail
;
6217 if (symdef
->file_offset
== last
)
6223 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
6224 if (h
== (struct bfd_link_hash_entry
*) -1)
6230 if (h
->type
== bfd_link_hash_undefined
)
6232 /* If the archive element has already been loaded then one
6233 of the symbols defined by that element might have been
6234 made undefined due to being in a discarded section. */
6235 if (is_elf_hash_table (info
->hash
)
6236 && ((struct elf_link_hash_entry
*) h
)->indx
== -3)
6239 else if (h
->type
== bfd_link_hash_common
)
6241 /* We currently have a common symbol. The archive map contains
6242 a reference to this symbol, so we may want to include it. We
6243 only want to include it however, if this archive element
6244 contains a definition of the symbol, not just another common
6247 Unfortunately some archivers (including GNU ar) will put
6248 declarations of common symbols into their archive maps, as
6249 well as real definitions, so we cannot just go by the archive
6250 map alone. Instead we must read in the element's symbol
6251 table and check that to see what kind of symbol definition
6253 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
6258 if (h
->type
!= bfd_link_hash_undefweak
)
6259 /* Symbol must be defined. Don't check it again. */
6262 if (!is_elf_hash_table (info
->hash
))
6264 /* Ignore the archive if the symbol isn't defined in a
6266 if (!((struct elf_link_hash_entry
*) h
)->def_dynamic
)
6268 /* Ignore the dynamic definition if symbol is first
6269 defined in this archive. */
6270 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
6271 if (htab
->first_hash
== NULL
)
6273 struct elf_link_first_hash_entry
*e
6274 = ((struct elf_link_first_hash_entry
*)
6275 bfd_hash_lookup (htab
->first_hash
, symdef
->name
,
6277 if (e
== NULL
|| e
->abfd
!= abfd
)
6281 /* We need to include this archive member. */
6282 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
,
6284 if (element
== NULL
)
6287 if (! bfd_check_format (element
, bfd_object
))
6290 undefs_tail
= info
->hash
->undefs_tail
;
6292 if (!(*info
->callbacks
6293 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
6295 if (!bfd_link_add_symbols (element
, info
))
6298 /* If there are any new undefined symbols, we need to make
6299 another pass through the archive in order to see whether
6300 they can be defined. FIXME: This isn't perfect, because
6301 common symbols wind up on undefs_tail and because an
6302 undefined symbol which is defined later on in this pass
6303 does not require another pass. This isn't a bug, but it
6304 does make the code less efficient than it could be. */
6305 if (undefs_tail
!= info
->hash
->undefs_tail
)
6308 /* Look backward to mark all symbols from this object file
6309 which we have already seen in this pass. */
6313 included
[mark
] = true;
6318 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
6320 /* We mark subsequent symbols from this object file as we go
6321 on through the loop. */
6322 last
= symdef
->file_offset
;
6335 /* Given an ELF BFD, add symbols to the global hash table as
6339 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6341 switch (bfd_get_format (abfd
))
6344 return elf_link_add_object_symbols (abfd
, info
);
6346 return elf_link_add_archive_symbols (abfd
, info
);
6348 bfd_set_error (bfd_error_wrong_format
);
6353 struct hash_codes_info
6355 unsigned long *hashcodes
;
6359 /* This function will be called though elf_link_hash_traverse to store
6360 all hash value of the exported symbols in an array. */
6363 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6365 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
6370 /* Ignore indirect symbols. These are added by the versioning code. */
6371 if (h
->dynindx
== -1)
6374 name
= h
->root
.root
.string
;
6375 if (h
->versioned
>= versioned
)
6377 char *p
= strchr (name
, ELF_VER_CHR
);
6380 alc
= (char *) bfd_malloc (p
- name
+ 1);
6386 memcpy (alc
, name
, p
- name
);
6387 alc
[p
- name
] = '\0';
6392 /* Compute the hash value. */
6393 ha
= bfd_elf_hash (name
);
6395 /* Store the found hash value in the array given as the argument. */
6396 *(inf
->hashcodes
)++ = ha
;
6398 /* And store it in the struct so that we can put it in the hash table
6400 h
->u
.elf_hash_value
= ha
;
6406 struct collect_gnu_hash_codes
6409 const struct elf_backend_data
*bed
;
6410 unsigned long int nsyms
;
6411 unsigned long int maskbits
;
6412 unsigned long int *hashcodes
;
6413 unsigned long int *hashval
;
6414 unsigned long int *indx
;
6415 unsigned long int *counts
;
6419 long int min_dynindx
;
6420 unsigned long int bucketcount
;
6421 unsigned long int symindx
;
6422 long int local_indx
;
6423 long int shift1
, shift2
;
6424 unsigned long int mask
;
6428 /* This function will be called though elf_link_hash_traverse to store
6429 all hash value of the exported symbols in an array. */
6432 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6434 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6439 /* Ignore indirect symbols. These are added by the versioning code. */
6440 if (h
->dynindx
== -1)
6443 /* Ignore also local symbols and undefined symbols. */
6444 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6447 name
= h
->root
.root
.string
;
6448 if (h
->versioned
>= versioned
)
6450 char *p
= strchr (name
, ELF_VER_CHR
);
6453 alc
= (char *) bfd_malloc (p
- name
+ 1);
6459 memcpy (alc
, name
, p
- name
);
6460 alc
[p
- name
] = '\0';
6465 /* Compute the hash value. */
6466 ha
= bfd_elf_gnu_hash (name
);
6468 /* Store the found hash value in the array for compute_bucket_count,
6469 and also for .dynsym reordering purposes. */
6470 s
->hashcodes
[s
->nsyms
] = ha
;
6471 s
->hashval
[h
->dynindx
] = ha
;
6473 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6474 s
->min_dynindx
= h
->dynindx
;
6480 /* This function will be called though elf_link_hash_traverse to do
6481 final dynamic symbol renumbering in case of .gnu.hash.
6482 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6483 to the translation table. */
6486 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6488 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6489 unsigned long int bucket
;
6490 unsigned long int val
;
6492 /* Ignore indirect symbols. */
6493 if (h
->dynindx
== -1)
6496 /* Ignore also local symbols and undefined symbols. */
6497 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6499 if (h
->dynindx
>= s
->min_dynindx
)
6501 if (s
->bed
->record_xhash_symbol
!= NULL
)
6503 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6507 h
->dynindx
= s
->local_indx
++;
6512 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6513 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6514 & ((s
->maskbits
>> s
->shift1
) - 1);
6515 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6517 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6518 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6519 if (s
->counts
[bucket
] == 1)
6520 /* Last element terminates the chain. */
6522 bfd_put_32 (s
->output_bfd
, val
,
6523 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6524 --s
->counts
[bucket
];
6525 if (s
->bed
->record_xhash_symbol
!= NULL
)
6527 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6529 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6532 h
->dynindx
= s
->indx
[bucket
]++;
6536 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6539 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6541 return !(h
->forced_local
6542 || h
->root
.type
== bfd_link_hash_undefined
6543 || h
->root
.type
== bfd_link_hash_undefweak
6544 || ((h
->root
.type
== bfd_link_hash_defined
6545 || h
->root
.type
== bfd_link_hash_defweak
)
6546 && h
->root
.u
.def
.section
->output_section
== NULL
));
6549 /* Array used to determine the number of hash table buckets to use
6550 based on the number of symbols there are. If there are fewer than
6551 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6552 fewer than 37 we use 17 buckets, and so forth. We never use more
6553 than 32771 buckets. */
6555 static const size_t elf_buckets
[] =
6557 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6561 /* Compute bucket count for hashing table. We do not use a static set
6562 of possible tables sizes anymore. Instead we determine for all
6563 possible reasonable sizes of the table the outcome (i.e., the
6564 number of collisions etc) and choose the best solution. The
6565 weighting functions are not too simple to allow the table to grow
6566 without bounds. Instead one of the weighting factors is the size.
6567 Therefore the result is always a good payoff between few collisions
6568 (= short chain lengths) and table size. */
6570 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6571 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6572 unsigned long int nsyms
,
6575 size_t best_size
= 0;
6576 unsigned long int i
;
6582 uint64_t best_chlen
= ~((uint64_t) 0);
6583 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6584 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6585 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6586 unsigned long int *counts
;
6588 unsigned int no_improvement_count
= 0;
6590 /* Possible optimization parameters: if we have NSYMS symbols we say
6591 that the hashing table must at least have NSYMS/4 and at most
6593 minsize
= nsyms
/ 4;
6596 best_size
= maxsize
= nsyms
* 2;
6601 if ((best_size
& 31) == 0)
6605 /* Create array where we count the collisions in. We must use bfd_malloc
6606 since the size could be large. */
6608 amt
*= sizeof (unsigned long int);
6609 counts
= (unsigned long int *) bfd_malloc (amt
);
6613 /* Compute the "optimal" size for the hash table. The criteria is a
6614 minimal chain length. The minor criteria is (of course) the size
6616 for (i
= minsize
; i
< maxsize
; ++i
)
6618 /* Walk through the array of hashcodes and count the collisions. */
6620 unsigned long int j
;
6621 unsigned long int fact
;
6623 if (gnu_hash
&& (i
& 31) == 0)
6626 memset (counts
, '\0', i
* sizeof (unsigned long int));
6628 /* Determine how often each hash bucket is used. */
6629 for (j
= 0; j
< nsyms
; ++j
)
6630 ++counts
[hashcodes
[j
] % i
];
6632 /* For the weight function we need some information about the
6633 pagesize on the target. This is information need not be 100%
6634 accurate. Since this information is not available (so far) we
6635 define it here to a reasonable default value. If it is crucial
6636 to have a better value some day simply define this value. */
6637 # ifndef BFD_TARGET_PAGESIZE
6638 # define BFD_TARGET_PAGESIZE (4096)
6641 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6643 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6646 /* Variant 1: optimize for short chains. We add the squares
6647 of all the chain lengths (which favors many small chain
6648 over a few long chains). */
6649 for (j
= 0; j
< i
; ++j
)
6650 max
+= counts
[j
] * counts
[j
];
6652 /* This adds penalties for the overall size of the table. */
6653 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6656 /* Variant 2: Optimize a lot more for small table. Here we
6657 also add squares of the size but we also add penalties for
6658 empty slots (the +1 term). */
6659 for (j
= 0; j
< i
; ++j
)
6660 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6662 /* The overall size of the table is considered, but not as
6663 strong as in variant 1, where it is squared. */
6664 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6668 /* Compare with current best results. */
6669 if (max
< best_chlen
)
6673 no_improvement_count
= 0;
6675 /* PR 11843: Avoid futile long searches for the best bucket size
6676 when there are a large number of symbols. */
6677 else if (++no_improvement_count
== 100)
6685 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6687 best_size
= elf_buckets
[i
];
6688 if (nsyms
< elf_buckets
[i
+ 1])
6691 if (gnu_hash
&& best_size
< 2)
6698 /* Size any SHT_GROUP section for ld -r. */
6701 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6706 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6707 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6708 && (s
= ibfd
->sections
) != NULL
6709 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6710 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6715 /* Set a default stack segment size. The value in INFO wins. If it
6716 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6717 undefined it is initialized. */
6720 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6721 struct bfd_link_info
*info
,
6722 const char *legacy_symbol
,
6723 bfd_vma default_size
)
6725 struct elf_link_hash_entry
*h
= NULL
;
6727 /* Look for legacy symbol. */
6729 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6730 false, false, false);
6731 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6732 || h
->root
.type
== bfd_link_hash_defweak
)
6734 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6736 /* The symbol has no type if specified on the command line. */
6737 h
->type
= STT_OBJECT
;
6738 if (info
->stacksize
)
6739 /* xgettext:c-format */
6740 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6741 output_bfd
, legacy_symbol
);
6742 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6743 /* xgettext:c-format */
6744 _bfd_error_handler (_("%pB: %s not absolute"),
6745 output_bfd
, legacy_symbol
);
6747 info
->stacksize
= h
->root
.u
.def
.value
;
6750 if (!info
->stacksize
)
6751 /* If the user didn't set a size, or explicitly inhibit the
6752 size, set it now. */
6753 info
->stacksize
= default_size
;
6755 /* Provide the legacy symbol, if it is referenced. */
6756 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6757 || h
->root
.type
== bfd_link_hash_undefweak
))
6759 struct bfd_link_hash_entry
*bh
= NULL
;
6761 if (!(_bfd_generic_link_add_one_symbol
6762 (info
, output_bfd
, legacy_symbol
,
6763 BSF_GLOBAL
, bfd_abs_section_ptr
,
6764 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6765 NULL
, false, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6768 h
= (struct elf_link_hash_entry
*) bh
;
6770 h
->type
= STT_OBJECT
;
6776 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6778 struct elf_gc_sweep_symbol_info
6780 struct bfd_link_info
*info
;
6781 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6786 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6789 && (((h
->root
.type
== bfd_link_hash_defined
6790 || h
->root
.type
== bfd_link_hash_defweak
)
6791 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6792 && h
->root
.u
.def
.section
->gc_mark
))
6793 || h
->root
.type
== bfd_link_hash_undefined
6794 || h
->root
.type
== bfd_link_hash_undefweak
))
6796 struct elf_gc_sweep_symbol_info
*inf
;
6798 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6799 (*inf
->hide_symbol
) (inf
->info
, h
, true);
6802 h
->ref_regular_nonweak
= 0;
6808 /* Set up the sizes and contents of the ELF dynamic sections. This is
6809 called by the ELF linker emulation before_allocation routine. We
6810 must set the sizes of the sections before the linker sets the
6811 addresses of the various sections. */
6814 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6817 const char *filter_shlib
,
6819 const char *depaudit
,
6820 const char * const *auxiliary_filters
,
6821 struct bfd_link_info
*info
,
6822 asection
**sinterpptr
)
6825 const struct elf_backend_data
*bed
;
6829 if (!is_elf_hash_table (info
->hash
))
6832 /* Any syms created from now on start with -1 in
6833 got.refcount/offset and plt.refcount/offset. */
6834 elf_hash_table (info
)->init_got_refcount
6835 = elf_hash_table (info
)->init_got_offset
;
6836 elf_hash_table (info
)->init_plt_refcount
6837 = elf_hash_table (info
)->init_plt_offset
;
6839 bed
= get_elf_backend_data (output_bfd
);
6841 /* The backend may have to create some sections regardless of whether
6842 we're dynamic or not. */
6843 if (bed
->elf_backend_early_size_sections
6844 && !bed
->elf_backend_early_size_sections (output_bfd
, info
))
6847 dynobj
= elf_hash_table (info
)->dynobj
;
6849 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6851 struct bfd_elf_version_tree
*verdefs
;
6852 struct elf_info_failed asvinfo
;
6853 struct bfd_elf_version_tree
*t
;
6854 struct bfd_elf_version_expr
*d
;
6858 /* If we are supposed to export all symbols into the dynamic symbol
6859 table (this is not the normal case), then do so. */
6860 if (info
->export_dynamic
6861 || (bfd_link_executable (info
) && info
->dynamic
))
6863 struct elf_info_failed eif
;
6867 elf_link_hash_traverse (elf_hash_table (info
),
6868 _bfd_elf_export_symbol
,
6876 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6878 if (soname_indx
== (size_t) -1
6879 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6883 soname_indx
= (size_t) -1;
6885 /* Make all global versions with definition. */
6886 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6887 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6888 if (!d
->symver
&& d
->literal
)
6890 const char *verstr
, *name
;
6891 size_t namelen
, verlen
, newlen
;
6892 char *newname
, *p
, leading_char
;
6893 struct elf_link_hash_entry
*newh
;
6895 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6897 namelen
= strlen (name
) + (leading_char
!= '\0');
6899 verlen
= strlen (verstr
);
6900 newlen
= namelen
+ verlen
+ 3;
6902 newname
= (char *) bfd_malloc (newlen
);
6903 if (newname
== NULL
)
6905 newname
[0] = leading_char
;
6906 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6908 /* Check the hidden versioned definition. */
6909 p
= newname
+ namelen
;
6911 memcpy (p
, verstr
, verlen
+ 1);
6912 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6913 newname
, false, false,
6916 || (newh
->root
.type
!= bfd_link_hash_defined
6917 && newh
->root
.type
!= bfd_link_hash_defweak
))
6919 /* Check the default versioned definition. */
6921 memcpy (p
, verstr
, verlen
+ 1);
6922 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6923 newname
, false, false,
6928 /* Mark this version if there is a definition and it is
6929 not defined in a shared object. */
6931 && !newh
->def_dynamic
6932 && (newh
->root
.type
== bfd_link_hash_defined
6933 || newh
->root
.type
== bfd_link_hash_defweak
))
6937 /* Attach all the symbols to their version information. */
6938 asvinfo
.info
= info
;
6939 asvinfo
.failed
= false;
6941 elf_link_hash_traverse (elf_hash_table (info
),
6942 _bfd_elf_link_assign_sym_version
,
6947 if (!info
->allow_undefined_version
)
6949 /* Check if all global versions have a definition. */
6950 bool all_defined
= true;
6951 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6952 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6953 if (d
->literal
&& !d
->symver
&& !d
->script
)
6956 (_("%s: undefined version: %s"),
6957 d
->pattern
, t
->name
);
6958 all_defined
= false;
6963 bfd_set_error (bfd_error_bad_value
);
6968 /* Set up the version definition section. */
6969 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6970 BFD_ASSERT (s
!= NULL
);
6972 /* We may have created additional version definitions if we are
6973 just linking a regular application. */
6974 verdefs
= info
->version_info
;
6976 /* Skip anonymous version tag. */
6977 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6978 verdefs
= verdefs
->next
;
6980 if (verdefs
== NULL
&& !info
->create_default_symver
)
6981 s
->flags
|= SEC_EXCLUDE
;
6987 Elf_Internal_Verdef def
;
6988 Elf_Internal_Verdaux defaux
;
6989 struct bfd_link_hash_entry
*bh
;
6990 struct elf_link_hash_entry
*h
;
6996 /* Make space for the base version. */
6997 size
+= sizeof (Elf_External_Verdef
);
6998 size
+= sizeof (Elf_External_Verdaux
);
7001 /* Make space for the default version. */
7002 if (info
->create_default_symver
)
7004 size
+= sizeof (Elf_External_Verdef
);
7008 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
7010 struct bfd_elf_version_deps
*n
;
7012 /* Don't emit base version twice. */
7016 size
+= sizeof (Elf_External_Verdef
);
7017 size
+= sizeof (Elf_External_Verdaux
);
7020 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
7021 size
+= sizeof (Elf_External_Verdaux
);
7025 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7026 if (s
->contents
== NULL
&& s
->size
!= 0)
7029 /* Fill in the version definition section. */
7033 def
.vd_version
= VER_DEF_CURRENT
;
7034 def
.vd_flags
= VER_FLG_BASE
;
7037 if (info
->create_default_symver
)
7039 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
7040 def
.vd_next
= sizeof (Elf_External_Verdef
);
7044 def
.vd_aux
= sizeof (Elf_External_Verdef
);
7045 def
.vd_next
= (sizeof (Elf_External_Verdef
)
7046 + sizeof (Elf_External_Verdaux
));
7049 if (soname_indx
!= (size_t) -1)
7051 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
7053 def
.vd_hash
= bfd_elf_hash (soname
);
7054 defaux
.vda_name
= soname_indx
;
7061 name
= lbasename (bfd_get_filename (output_bfd
));
7062 def
.vd_hash
= bfd_elf_hash (name
);
7063 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7065 if (indx
== (size_t) -1)
7067 defaux
.vda_name
= indx
;
7069 defaux
.vda_next
= 0;
7071 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
7072 (Elf_External_Verdef
*) p
);
7073 p
+= sizeof (Elf_External_Verdef
);
7074 if (info
->create_default_symver
)
7076 /* Add a symbol representing this version. */
7078 if (! (_bfd_generic_link_add_one_symbol
7079 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
7081 get_elf_backend_data (dynobj
)->collect
, &bh
)))
7083 h
= (struct elf_link_hash_entry
*) bh
;
7086 h
->type
= STT_OBJECT
;
7087 h
->verinfo
.vertree
= NULL
;
7089 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
7092 /* Create a duplicate of the base version with the same
7093 aux block, but different flags. */
7096 def
.vd_aux
= sizeof (Elf_External_Verdef
);
7098 def
.vd_next
= (sizeof (Elf_External_Verdef
)
7099 + sizeof (Elf_External_Verdaux
));
7102 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
7103 (Elf_External_Verdef
*) p
);
7104 p
+= sizeof (Elf_External_Verdef
);
7106 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7107 (Elf_External_Verdaux
*) p
);
7108 p
+= sizeof (Elf_External_Verdaux
);
7110 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
7113 struct bfd_elf_version_deps
*n
;
7115 /* Don't emit the base version twice. */
7120 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
7123 /* Add a symbol representing this version. */
7125 if (! (_bfd_generic_link_add_one_symbol
7126 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
7128 get_elf_backend_data (dynobj
)->collect
, &bh
)))
7130 h
= (struct elf_link_hash_entry
*) bh
;
7133 h
->type
= STT_OBJECT
;
7134 h
->verinfo
.vertree
= t
;
7136 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
7139 def
.vd_version
= VER_DEF_CURRENT
;
7141 if (t
->globals
.list
== NULL
7142 && t
->locals
.list
== NULL
7144 def
.vd_flags
|= VER_FLG_WEAK
;
7145 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
7146 def
.vd_cnt
= cdeps
+ 1;
7147 def
.vd_hash
= bfd_elf_hash (t
->name
);
7148 def
.vd_aux
= sizeof (Elf_External_Verdef
);
7151 /* If a basever node is next, it *must* be the last node in
7152 the chain, otherwise Verdef construction breaks. */
7153 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
7154 BFD_ASSERT (t
->next
->next
== NULL
);
7156 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
7157 def
.vd_next
= (sizeof (Elf_External_Verdef
)
7158 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
7160 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
7161 (Elf_External_Verdef
*) p
);
7162 p
+= sizeof (Elf_External_Verdef
);
7164 defaux
.vda_name
= h
->dynstr_index
;
7165 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
7167 defaux
.vda_next
= 0;
7168 if (t
->deps
!= NULL
)
7169 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
7170 t
->name_indx
= defaux
.vda_name
;
7172 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7173 (Elf_External_Verdaux
*) p
);
7174 p
+= sizeof (Elf_External_Verdaux
);
7176 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
7178 if (n
->version_needed
== NULL
)
7180 /* This can happen if there was an error in the
7182 defaux
.vda_name
= 0;
7186 defaux
.vda_name
= n
->version_needed
->name_indx
;
7187 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
7190 if (n
->next
== NULL
)
7191 defaux
.vda_next
= 0;
7193 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
7195 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7196 (Elf_External_Verdaux
*) p
);
7197 p
+= sizeof (Elf_External_Verdaux
);
7201 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
7205 if (info
->gc_sections
&& bed
->can_gc_sections
)
7207 struct elf_gc_sweep_symbol_info sweep_info
;
7209 /* Remove the symbols that were in the swept sections from the
7210 dynamic symbol table. */
7211 sweep_info
.info
= info
;
7212 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
7213 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
7217 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7220 struct elf_find_verdep_info sinfo
;
7222 /* Work out the size of the version reference section. */
7224 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
7225 BFD_ASSERT (s
!= NULL
);
7228 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
7229 if (sinfo
.vers
== 0)
7231 sinfo
.failed
= false;
7233 elf_link_hash_traverse (elf_hash_table (info
),
7234 _bfd_elf_link_find_version_dependencies
,
7239 bed
->elf_backend_add_glibc_version_dependency (&sinfo
);
7243 if (elf_tdata (output_bfd
)->verref
== NULL
)
7244 s
->flags
|= SEC_EXCLUDE
;
7247 Elf_Internal_Verneed
*vn
;
7252 /* Build the version dependency section. */
7255 for (vn
= elf_tdata (output_bfd
)->verref
;
7257 vn
= vn
->vn_nextref
)
7259 Elf_Internal_Vernaux
*a
;
7261 size
+= sizeof (Elf_External_Verneed
);
7263 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7264 size
+= sizeof (Elf_External_Vernaux
);
7268 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7269 if (s
->contents
== NULL
)
7273 for (vn
= elf_tdata (output_bfd
)->verref
;
7275 vn
= vn
->vn_nextref
)
7278 Elf_Internal_Vernaux
*a
;
7282 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7285 vn
->vn_version
= VER_NEED_CURRENT
;
7287 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7288 elf_dt_name (vn
->vn_bfd
) != NULL
7289 ? elf_dt_name (vn
->vn_bfd
)
7290 : lbasename (bfd_get_filename
7293 if (indx
== (size_t) -1)
7296 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
7297 if (vn
->vn_nextref
== NULL
)
7300 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
7301 + caux
* sizeof (Elf_External_Vernaux
));
7303 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
7304 (Elf_External_Verneed
*) p
);
7305 p
+= sizeof (Elf_External_Verneed
);
7307 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7309 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
7310 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7311 a
->vna_nodename
, false);
7312 if (indx
== (size_t) -1)
7315 if (a
->vna_nextptr
== NULL
)
7318 a
->vna_next
= sizeof (Elf_External_Vernaux
);
7320 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
7321 (Elf_External_Vernaux
*) p
);
7322 p
+= sizeof (Elf_External_Vernaux
);
7326 elf_tdata (output_bfd
)->cverrefs
= crefs
;
7330 if (bfd_link_relocatable (info
)
7331 && !_bfd_elf_size_group_sections (info
))
7334 /* Determine any GNU_STACK segment requirements, after the backend
7335 has had a chance to set a default segment size. */
7336 if (info
->execstack
)
7338 /* If the user has explicitly requested warnings, then generate one even
7339 though the choice is the result of another command line option. */
7340 if (info
->warn_execstack
== 1)
7342 if (info
->error_execstack
)
7346 error: creating an executable stack because of -z execstack command line option"));
7352 warning: enabling an executable stack because of -z execstack command line option"));
7355 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
7357 else if (info
->noexecstack
)
7358 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
7362 asection
*notesec
= NULL
;
7363 bfd
*noteobj
= NULL
;
7364 bfd
*emptyobj
= NULL
;
7367 for (inputobj
= info
->input_bfds
;
7369 inputobj
= inputobj
->link
.next
)
7374 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
7376 s
= inputobj
->sections
;
7377 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
7380 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
7384 if (s
->flags
& SEC_CODE
)
7388 /* There is no point in scanning the remaining bfds. */
7392 else if (bed
->default_execstack
&& info
->default_execstack
)
7395 emptyobj
= inputobj
;
7399 if (notesec
|| info
->stacksize
> 0)
7403 if (info
->warn_execstack
!= 0)
7405 /* PR 29072: Because an executable stack is a serious
7406 security risk, make sure that the user knows that it is
7407 being enabled despite the fact that it was not requested
7408 on the command line. */
7411 if (info
->error_execstack
)
7413 _bfd_error_handler (_("\
7414 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7415 bfd_get_filename (noteobj
));
7419 _bfd_error_handler (_("\
7420 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7421 bfd_get_filename (noteobj
));
7425 if (info
->error_execstack
)
7427 _bfd_error_handler (_("\
7428 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7429 bfd_get_filename (emptyobj
));
7433 _bfd_error_handler (_("\
7434 warning: %s: missing .note.GNU-stack section implies executable stack"),
7435 bfd_get_filename (emptyobj
));
7436 _bfd_error_handler (_("\
7437 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7441 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
7444 if (notesec
&& exec
&& bfd_link_relocatable (info
)
7445 && notesec
->output_section
!= bfd_abs_section_ptr
)
7446 notesec
->output_section
->flags
|= SEC_CODE
;
7449 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7451 struct elf_info_failed eif
;
7452 struct elf_link_hash_entry
*h
;
7456 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7457 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7461 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7463 info
->flags
|= DF_SYMBOLIC
;
7471 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7473 if (indx
== (size_t) -1)
7476 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7477 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7481 if (filter_shlib
!= NULL
)
7485 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7486 filter_shlib
, true);
7487 if (indx
== (size_t) -1
7488 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7492 if (auxiliary_filters
!= NULL
)
7494 const char * const *p
;
7496 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7500 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7502 if (indx
== (size_t) -1
7503 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7512 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7514 if (indx
== (size_t) -1
7515 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7519 if (depaudit
!= NULL
)
7523 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7525 if (indx
== (size_t) -1
7526 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7533 /* Find all symbols which were defined in a dynamic object and make
7534 the backend pick a reasonable value for them. */
7535 elf_link_hash_traverse (elf_hash_table (info
),
7536 _bfd_elf_adjust_dynamic_symbol
,
7541 /* Add some entries to the .dynamic section. We fill in some of the
7542 values later, in bfd_elf_final_link, but we must add the entries
7543 now so that we know the final size of the .dynamic section. */
7545 /* If there are initialization and/or finalization functions to
7546 call then add the corresponding DT_INIT/DT_FINI entries. */
7547 h
= (info
->init_function
7548 ? elf_link_hash_lookup (elf_hash_table (info
),
7549 info
->init_function
, false,
7556 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7559 h
= (info
->fini_function
7560 ? elf_link_hash_lookup (elf_hash_table (info
),
7561 info
->fini_function
, false,
7568 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7572 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7573 if (s
!= NULL
&& s
->linker_has_input
)
7575 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7576 if (! bfd_link_executable (info
))
7581 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7582 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7583 && (o
= sub
->sections
) != NULL
7584 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7585 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7586 if (elf_section_data (o
)->this_hdr
.sh_type
7587 == SHT_PREINIT_ARRAY
)
7590 (_("%pB: .preinit_array section is not allowed in DSO"),
7595 bfd_set_error (bfd_error_nonrepresentable_section
);
7599 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7600 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7603 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7604 if (s
!= NULL
&& s
->linker_has_input
)
7606 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7607 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7610 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7611 if (s
!= NULL
&& s
->linker_has_input
)
7613 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7614 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7618 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7619 /* If .dynstr is excluded from the link, we don't want any of
7620 these tags. Strictly, we should be checking each section
7621 individually; This quick check covers for the case where
7622 someone does a /DISCARD/ : { *(*) }. */
7623 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7625 bfd_size_type strsize
;
7627 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7628 if ((info
->emit_hash
7629 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7630 || (info
->emit_gnu_hash
7631 && (bed
->record_xhash_symbol
== NULL
7632 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7633 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7634 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7635 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7636 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7638 || (info
->gnu_flags_1
7639 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_FLAGS_1
,
7640 info
->gnu_flags_1
)))
7645 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7648 /* The backend must work out the sizes of all the other dynamic
7650 if (bed
->elf_backend_late_size_sections
!= NULL
7651 && !bed
->elf_backend_late_size_sections (output_bfd
, info
))
7654 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7656 if (elf_tdata (output_bfd
)->cverdefs
)
7658 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7660 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7661 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7665 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7667 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7670 else if (info
->flags
& DF_BIND_NOW
)
7672 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7678 if (bfd_link_executable (info
))
7679 info
->flags_1
&= ~ (DF_1_INITFIRST
7682 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7686 if (elf_tdata (output_bfd
)->cverrefs
)
7688 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7690 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7691 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7695 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7696 && elf_tdata (output_bfd
)->cverdefs
== 0)
7697 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7701 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7702 s
->flags
|= SEC_EXCLUDE
;
7708 /* Find the first non-excluded output section. We'll use its
7709 section symbol for some emitted relocs. */
7711 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7714 asection
*found
= NULL
;
7716 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7717 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7718 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7721 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7724 elf_hash_table (info
)->text_index_section
= found
;
7727 /* Find two non-excluded output sections, one for code, one for data.
7728 We'll use their section symbols for some emitted relocs. */
7730 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7733 asection
*found
= NULL
;
7735 /* Data first, since setting text_index_section changes
7736 _bfd_elf_omit_section_dynsym_default. */
7737 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7738 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7739 && !(s
->flags
& SEC_READONLY
)
7740 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7743 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7746 elf_hash_table (info
)->data_index_section
= found
;
7748 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7749 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7750 && (s
->flags
& SEC_READONLY
)
7751 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7756 elf_hash_table (info
)->text_index_section
= found
;
7759 #define GNU_HASH_SECTION_NAME(bed) \
7760 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7763 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7765 const struct elf_backend_data
*bed
;
7766 unsigned long section_sym_count
;
7767 bfd_size_type dynsymcount
= 0;
7769 if (!is_elf_hash_table (info
->hash
))
7772 bed
= get_elf_backend_data (output_bfd
);
7773 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7775 /* Assign dynsym indices. In a shared library we generate a section
7776 symbol for each output section, which come first. Next come all
7777 of the back-end allocated local dynamic syms, followed by the rest
7778 of the global symbols.
7780 This is usually not needed for static binaries, however backends
7781 can request to always do it, e.g. the MIPS backend uses dynamic
7782 symbol counts to lay out GOT, which will be produced in the
7783 presence of GOT relocations even in static binaries (holding fixed
7784 data in that case, to satisfy those relocations). */
7786 if (elf_hash_table (info
)->dynamic_sections_created
7787 || bed
->always_renumber_dynsyms
)
7788 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7789 §ion_sym_count
);
7791 if (elf_hash_table (info
)->dynamic_sections_created
)
7795 unsigned int dtagcount
;
7797 dynobj
= elf_hash_table (info
)->dynobj
;
7799 /* Work out the size of the symbol version section. */
7800 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7801 BFD_ASSERT (s
!= NULL
);
7802 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7804 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7805 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7806 if (s
->contents
== NULL
)
7809 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7813 /* Set the size of the .dynsym and .hash sections. We counted
7814 the number of dynamic symbols in elf_link_add_object_symbols.
7815 We will build the contents of .dynsym and .hash when we build
7816 the final symbol table, because until then we do not know the
7817 correct value to give the symbols. We built the .dynstr
7818 section as we went along in elf_link_add_object_symbols. */
7819 s
= elf_hash_table (info
)->dynsym
;
7820 BFD_ASSERT (s
!= NULL
);
7821 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7823 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7824 if (s
->contents
== NULL
)
7827 /* The first entry in .dynsym is a dummy symbol. Clear all the
7828 section syms, in case we don't output them all. */
7829 ++section_sym_count
;
7830 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7832 elf_hash_table (info
)->bucketcount
= 0;
7834 /* Compute the size of the hashing table. As a side effect this
7835 computes the hash values for all the names we export. */
7836 if (info
->emit_hash
)
7838 unsigned long int *hashcodes
;
7839 struct hash_codes_info hashinf
;
7841 unsigned long int nsyms
;
7843 size_t hash_entry_size
;
7845 /* Compute the hash values for all exported symbols. At the same
7846 time store the values in an array so that we could use them for
7848 amt
= dynsymcount
* sizeof (unsigned long int);
7849 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7850 if (hashcodes
== NULL
)
7852 hashinf
.hashcodes
= hashcodes
;
7853 hashinf
.error
= false;
7855 /* Put all hash values in HASHCODES. */
7856 elf_link_hash_traverse (elf_hash_table (info
),
7857 elf_collect_hash_codes
, &hashinf
);
7864 nsyms
= hashinf
.hashcodes
- hashcodes
;
7866 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7869 if (bucketcount
== 0 && nsyms
> 0)
7872 elf_hash_table (info
)->bucketcount
= bucketcount
;
7874 s
= bfd_get_linker_section (dynobj
, ".hash");
7875 BFD_ASSERT (s
!= NULL
);
7876 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7877 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7878 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7879 if (s
->contents
== NULL
)
7882 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7883 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7884 s
->contents
+ hash_entry_size
);
7887 if (info
->emit_gnu_hash
)
7890 unsigned char *contents
;
7891 struct collect_gnu_hash_codes cinfo
;
7895 memset (&cinfo
, 0, sizeof (cinfo
));
7897 /* Compute the hash values for all exported symbols. At the same
7898 time store the values in an array so that we could use them for
7900 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7901 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7902 if (cinfo
.hashcodes
== NULL
)
7905 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7906 cinfo
.min_dynindx
= -1;
7907 cinfo
.output_bfd
= output_bfd
;
7910 /* Put all hash values in HASHCODES. */
7911 elf_link_hash_traverse (elf_hash_table (info
),
7912 elf_collect_gnu_hash_codes
, &cinfo
);
7915 free (cinfo
.hashcodes
);
7920 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7922 if (bucketcount
== 0)
7924 free (cinfo
.hashcodes
);
7928 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7929 BFD_ASSERT (s
!= NULL
);
7931 if (cinfo
.nsyms
== 0)
7933 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7934 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7935 free (cinfo
.hashcodes
);
7936 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7937 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7938 if (contents
== NULL
)
7940 s
->contents
= contents
;
7941 /* 1 empty bucket. */
7942 bfd_put_32 (output_bfd
, 1, contents
);
7943 /* SYMIDX above the special symbol 0. */
7944 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7945 /* Just one word for bitmask. */
7946 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7947 /* Only hash fn bloom filter. */
7948 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7949 /* No hashes are valid - empty bitmask. */
7950 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7951 /* No hashes in the only bucket. */
7952 bfd_put_32 (output_bfd
, 0,
7953 contents
+ 16 + bed
->s
->arch_size
/ 8);
7957 unsigned long int maskwords
, maskbitslog2
, x
;
7958 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7962 while ((x
>>= 1) != 0)
7964 if (maskbitslog2
< 3)
7966 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7967 maskbitslog2
= maskbitslog2
+ 3;
7969 maskbitslog2
= maskbitslog2
+ 2;
7970 if (bed
->s
->arch_size
== 64)
7972 if (maskbitslog2
== 5)
7978 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7979 cinfo
.shift2
= maskbitslog2
;
7980 cinfo
.maskbits
= 1 << maskbitslog2
;
7981 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7982 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7983 amt
+= maskwords
* sizeof (bfd_vma
);
7984 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
7985 if (cinfo
.bitmask
== NULL
)
7987 free (cinfo
.hashcodes
);
7991 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
7992 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
7993 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
7994 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
7996 /* Determine how often each hash bucket is used. */
7997 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
7998 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
7999 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
8001 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
8002 if (cinfo
.counts
[i
] != 0)
8004 cinfo
.indx
[i
] = cnt
;
8005 cnt
+= cinfo
.counts
[i
];
8007 BFD_ASSERT (cnt
== dynsymcount
);
8008 cinfo
.bucketcount
= bucketcount
;
8009 cinfo
.local_indx
= cinfo
.min_dynindx
;
8011 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
8012 s
->size
+= cinfo
.maskbits
/ 8;
8013 if (bed
->record_xhash_symbol
!= NULL
)
8014 s
->size
+= cinfo
.nsyms
* 4;
8015 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
8016 if (contents
== NULL
)
8018 free (cinfo
.bitmask
);
8019 free (cinfo
.hashcodes
);
8023 s
->contents
= contents
;
8024 bfd_put_32 (output_bfd
, bucketcount
, contents
);
8025 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
8026 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
8027 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
8028 contents
+= 16 + cinfo
.maskbits
/ 8;
8030 for (i
= 0; i
< bucketcount
; ++i
)
8032 if (cinfo
.counts
[i
] == 0)
8033 bfd_put_32 (output_bfd
, 0, contents
);
8035 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
8039 cinfo
.contents
= contents
;
8041 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
8042 /* Renumber dynamic symbols, if populating .gnu.hash section.
8043 If using .MIPS.xhash, populate the translation table. */
8044 elf_link_hash_traverse (elf_hash_table (info
),
8045 elf_gnu_hash_process_symidx
, &cinfo
);
8047 contents
= s
->contents
+ 16;
8048 for (i
= 0; i
< maskwords
; ++i
)
8050 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
8052 contents
+= bed
->s
->arch_size
/ 8;
8055 free (cinfo
.bitmask
);
8056 free (cinfo
.hashcodes
);
8060 s
= bfd_get_linker_section (dynobj
, ".dynstr");
8061 BFD_ASSERT (s
!= NULL
);
8063 elf_finalize_dynstr (output_bfd
, info
);
8065 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
8067 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
8068 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
8075 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
8078 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
8081 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
8082 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
8085 /* Finish SHF_MERGE section merging. */
8088 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
8093 if (!is_elf_hash_table (info
->hash
))
8096 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
8097 if ((ibfd
->flags
& DYNAMIC
) == 0
8098 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
8099 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
8100 == get_elf_backend_data (obfd
)->s
->elfclass
))
8101 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
8102 if ((sec
->flags
& SEC_MERGE
) != 0
8103 && !bfd_is_abs_section (sec
->output_section
))
8105 struct bfd_elf_section_data
*secdata
;
8107 secdata
= elf_section_data (sec
);
8108 if (! _bfd_add_merge_section (obfd
,
8109 &elf_hash_table (info
)->merge_info
,
8110 sec
, &secdata
->sec_info
))
8112 else if (secdata
->sec_info
)
8113 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
8116 if (elf_hash_table (info
)->merge_info
!= NULL
)
8117 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
8118 merge_sections_remove_hook
);
8122 /* Create an entry in an ELF linker hash table. */
8124 struct bfd_hash_entry
*
8125 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
8126 struct bfd_hash_table
*table
,
8129 /* Allocate the structure if it has not already been allocated by a
8133 entry
= (struct bfd_hash_entry
*)
8134 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
8139 /* Call the allocation method of the superclass. */
8140 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
8143 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
8144 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
8146 /* Set local fields. */
8149 ret
->got
= htab
->init_got_refcount
;
8150 ret
->plt
= htab
->init_plt_refcount
;
8151 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
8152 - offsetof (struct elf_link_hash_entry
, size
)));
8153 /* Assume that we have been called by a non-ELF symbol reader.
8154 This flag is then reset by the code which reads an ELF input
8155 file. This ensures that a symbol created by a non-ELF symbol
8156 reader will have the flag set correctly. */
8163 /* Copy data from an indirect symbol to its direct symbol, hiding the
8164 old indirect symbol. Also used for copying flags to a weakdef. */
8167 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
8168 struct elf_link_hash_entry
*dir
,
8169 struct elf_link_hash_entry
*ind
)
8171 struct elf_link_hash_table
*htab
;
8173 if (ind
->dyn_relocs
!= NULL
)
8175 if (dir
->dyn_relocs
!= NULL
)
8177 struct elf_dyn_relocs
**pp
;
8178 struct elf_dyn_relocs
*p
;
8180 /* Add reloc counts against the indirect sym to the direct sym
8181 list. Merge any entries against the same section. */
8182 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
8184 struct elf_dyn_relocs
*q
;
8186 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
8187 if (q
->sec
== p
->sec
)
8189 q
->pc_count
+= p
->pc_count
;
8190 q
->count
+= p
->count
;
8197 *pp
= dir
->dyn_relocs
;
8200 dir
->dyn_relocs
= ind
->dyn_relocs
;
8201 ind
->dyn_relocs
= NULL
;
8204 /* Copy down any references that we may have already seen to the
8205 symbol which just became indirect. */
8207 if (dir
->versioned
!= versioned_hidden
)
8208 dir
->ref_dynamic
|= ind
->ref_dynamic
;
8209 dir
->ref_regular
|= ind
->ref_regular
;
8210 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
8211 dir
->non_got_ref
|= ind
->non_got_ref
;
8212 dir
->needs_plt
|= ind
->needs_plt
;
8213 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
8215 if (ind
->root
.type
!= bfd_link_hash_indirect
)
8218 /* Copy over the global and procedure linkage table refcount entries.
8219 These may have been already set up by a check_relocs routine. */
8220 htab
= elf_hash_table (info
);
8221 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
8223 if (dir
->got
.refcount
< 0)
8224 dir
->got
.refcount
= 0;
8225 dir
->got
.refcount
+= ind
->got
.refcount
;
8226 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
8229 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
8231 if (dir
->plt
.refcount
< 0)
8232 dir
->plt
.refcount
= 0;
8233 dir
->plt
.refcount
+= ind
->plt
.refcount
;
8234 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
8237 if (ind
->dynindx
!= -1)
8239 if (dir
->dynindx
!= -1)
8240 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
8241 dir
->dynindx
= ind
->dynindx
;
8242 dir
->dynstr_index
= ind
->dynstr_index
;
8244 ind
->dynstr_index
= 0;
8249 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
8250 struct elf_link_hash_entry
*h
,
8253 /* STT_GNU_IFUNC symbol must go through PLT. */
8254 if (h
->type
!= STT_GNU_IFUNC
)
8256 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
8261 h
->forced_local
= 1;
8262 if (h
->dynindx
!= -1)
8264 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
8267 h
->dynstr_index
= 0;
8272 /* Hide a symbol. */
8275 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
8276 struct bfd_link_info
*info
,
8277 struct bfd_link_hash_entry
*h
)
8279 if (is_elf_hash_table (info
->hash
))
8281 const struct elf_backend_data
*bed
8282 = get_elf_backend_data (output_bfd
);
8283 struct elf_link_hash_entry
*eh
8284 = (struct elf_link_hash_entry
*) h
;
8285 bed
->elf_backend_hide_symbol (info
, eh
, true);
8286 eh
->def_dynamic
= 0;
8287 eh
->ref_dynamic
= 0;
8288 eh
->dynamic_def
= 0;
8292 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8296 _bfd_elf_link_hash_table_init
8297 (struct elf_link_hash_table
*table
,
8299 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
8300 struct bfd_hash_table
*,
8302 unsigned int entsize
,
8303 enum elf_target_id target_id
)
8306 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
8308 table
->init_got_refcount
.refcount
= can_refcount
- 1;
8309 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
8310 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
8311 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
8312 /* The first dynamic symbol is a dummy. */
8313 table
->dynsymcount
= 1;
8315 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
8317 table
->root
.type
= bfd_link_elf_hash_table
;
8318 table
->hash_table_id
= target_id
;
8319 table
->target_os
= get_elf_backend_data (abfd
)->target_os
;
8324 /* Create an ELF linker hash table. */
8326 struct bfd_link_hash_table
*
8327 _bfd_elf_link_hash_table_create (bfd
*abfd
)
8329 struct elf_link_hash_table
*ret
;
8330 size_t amt
= sizeof (struct elf_link_hash_table
);
8332 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
8336 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
8337 sizeof (struct elf_link_hash_entry
),
8343 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
8348 /* Destroy an ELF linker hash table. */
8351 _bfd_elf_link_hash_table_free (bfd
*obfd
)
8353 struct elf_link_hash_table
*htab
;
8355 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
8356 if (htab
->dynstr
!= NULL
)
8357 _bfd_elf_strtab_free (htab
->dynstr
);
8358 _bfd_merge_sections_free (htab
->merge_info
);
8359 /* NB: htab->dynamic->contents is always allocated by bfd_realloc. */
8360 if (htab
->dynamic
!= NULL
)
8361 free (htab
->dynamic
->contents
);
8362 if (htab
->first_hash
!= NULL
)
8364 bfd_hash_table_free (htab
->first_hash
);
8365 free (htab
->first_hash
);
8367 _bfd_generic_link_hash_table_free (obfd
);
8370 /* This is a hook for the ELF emulation code in the generic linker to
8371 tell the backend linker what file name to use for the DT_NEEDED
8372 entry for a dynamic object. */
8375 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
8377 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8378 && bfd_get_format (abfd
) == bfd_object
)
8379 elf_dt_name (abfd
) = name
;
8383 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
8386 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8387 && bfd_get_format (abfd
) == bfd_object
)
8388 lib_class
= elf_dyn_lib_class (abfd
);
8395 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
8397 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8398 && bfd_get_format (abfd
) == bfd_object
)
8399 elf_dyn_lib_class (abfd
) = lib_class
;
8402 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8403 the linker ELF emulation code. */
8405 struct bfd_link_needed_list
*
8406 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8407 struct bfd_link_info
*info
)
8409 if (! is_elf_hash_table (info
->hash
))
8411 return elf_hash_table (info
)->needed
;
8414 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8415 hook for the linker ELF emulation code. */
8417 struct bfd_link_needed_list
*
8418 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8419 struct bfd_link_info
*info
)
8421 if (! is_elf_hash_table (info
->hash
))
8423 return elf_hash_table (info
)->runpath
;
8426 /* Get the name actually used for a dynamic object for a link. This
8427 is the SONAME entry if there is one. Otherwise, it is the string
8428 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8431 bfd_elf_get_dt_soname (bfd
*abfd
)
8433 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8434 && bfd_get_format (abfd
) == bfd_object
)
8435 return elf_dt_name (abfd
);
8439 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8440 the ELF linker emulation code. */
8443 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
8444 struct bfd_link_needed_list
**pneeded
)
8447 bfd_byte
*dynbuf
= NULL
;
8448 unsigned int elfsec
;
8449 unsigned long shlink
;
8450 bfd_byte
*extdyn
, *extdynend
;
8452 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
8456 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
8457 || bfd_get_format (abfd
) != bfd_object
)
8460 s
= bfd_get_section_by_name (abfd
, ".dynamic");
8461 if (s
== NULL
|| s
->size
== 0 || (s
->flags
& SEC_HAS_CONTENTS
) == 0)
8464 if (!_bfd_elf_mmap_section_contents (abfd
, s
, &dynbuf
))
8467 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8468 if (elfsec
== SHN_BAD
)
8471 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8473 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8474 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8476 for (extdyn
= dynbuf
, extdynend
= dynbuf
+ s
->size
;
8477 (size_t) (extdynend
- extdyn
) >= extdynsize
;
8478 extdyn
+= extdynsize
)
8480 Elf_Internal_Dyn dyn
;
8482 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8484 if (dyn
.d_tag
== DT_NULL
)
8487 if (dyn
.d_tag
== DT_NEEDED
)
8490 struct bfd_link_needed_list
*l
;
8491 unsigned int tagv
= dyn
.d_un
.d_val
;
8494 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8499 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8510 _bfd_elf_munmap_section_contents (s
, dynbuf
);
8515 _bfd_elf_munmap_section_contents (s
, dynbuf
);
8519 struct elf_symbuf_symbol
8521 unsigned long st_name
; /* Symbol name, index in string tbl */
8522 unsigned char st_info
; /* Type and binding attributes */
8523 unsigned char st_other
; /* Visibilty, and target specific */
8526 struct elf_symbuf_head
8528 struct elf_symbuf_symbol
*ssym
;
8530 unsigned int st_shndx
;
8537 Elf_Internal_Sym
*isym
;
8538 struct elf_symbuf_symbol
*ssym
;
8544 /* Sort references to symbols by ascending section number. */
8547 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8549 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8550 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8552 if (s1
->st_shndx
!= s2
->st_shndx
)
8553 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8554 /* Final sort by the address of the sym in the symbuf ensures
8557 return s1
> s2
? 1 : -1;
8562 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8564 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8565 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8566 int ret
= strcmp (s1
->name
, s2
->name
);
8569 if (s1
->u
.p
!= s2
->u
.p
)
8570 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8574 static struct elf_symbuf_head
*
8575 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8577 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8578 struct elf_symbuf_symbol
*ssym
;
8579 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8580 size_t i
, shndx_count
, total_size
, amt
;
8582 amt
= symcount
* sizeof (*indbuf
);
8583 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8587 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8588 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8589 *ind
++ = &isymbuf
[i
];
8592 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8593 elf_sort_elf_symbol
);
8596 if (indbufend
> indbuf
)
8597 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8598 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8601 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8602 + (indbufend
- indbuf
) * sizeof (*ssym
));
8603 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8604 if (ssymbuf
== NULL
)
8610 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8611 ssymbuf
->ssym
= NULL
;
8612 ssymbuf
->count
= shndx_count
;
8613 ssymbuf
->st_shndx
= 0;
8614 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8616 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8619 ssymhead
->ssym
= ssym
;
8620 ssymhead
->count
= 0;
8621 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8623 ssym
->st_name
= (*ind
)->st_name
;
8624 ssym
->st_info
= (*ind
)->st_info
;
8625 ssym
->st_other
= (*ind
)->st_other
;
8628 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8629 && (uintptr_t) ssym
- (uintptr_t) ssymbuf
== total_size
);
8635 /* Check if 2 sections define the same set of local and global
8639 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8640 struct bfd_link_info
*info
)
8643 const struct elf_backend_data
*bed1
, *bed2
;
8644 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8645 size_t symcount1
, symcount2
;
8646 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8647 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8648 Elf_Internal_Sym
*isym
, *isymend
;
8649 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8650 size_t count1
, count2
, sec_count1
, sec_count2
, i
;
8651 unsigned int shndx1
, shndx2
;
8653 bool ignore_section_symbol_p
;
8658 /* Both sections have to be in ELF. */
8659 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8660 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8663 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8666 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8667 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8668 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8671 bed1
= get_elf_backend_data (bfd1
);
8672 bed2
= get_elf_backend_data (bfd2
);
8673 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8674 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8675 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8676 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8678 if (symcount1
== 0 || symcount2
== 0)
8684 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8685 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8687 /* Ignore section symbols only when matching non-debugging sections
8688 or linkonce section with comdat section. */
8689 ignore_section_symbol_p
8690 = ((sec1
->flags
& SEC_DEBUGGING
) == 0
8691 || ((elf_section_flags (sec1
) & SHF_GROUP
)
8692 != (elf_section_flags (sec2
) & SHF_GROUP
)));
8694 if (ssymbuf1
== NULL
)
8696 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8698 if (isymbuf1
== NULL
)
8701 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8703 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8704 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8708 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8710 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8712 if (isymbuf2
== NULL
)
8715 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8717 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8718 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8722 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8724 /* Optimized faster version. */
8726 struct elf_symbol
*symp
;
8727 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8730 hi
= ssymbuf1
->count
;
8736 mid
= (lo
+ hi
) / 2;
8737 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8739 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8743 count1
= ssymbuf1
[mid
].count
;
8748 if (ignore_section_symbol_p
)
8750 for (i
= 0; i
< count1
; i
++)
8751 if (ELF_ST_TYPE (ssymbuf1
->ssym
[i
].st_info
) == STT_SECTION
)
8753 count1
-= sec_count1
;
8757 hi
= ssymbuf2
->count
;
8763 mid
= (lo
+ hi
) / 2;
8764 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8766 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8770 count2
= ssymbuf2
[mid
].count
;
8775 if (ignore_section_symbol_p
)
8777 for (i
= 0; i
< count2
; i
++)
8778 if (ELF_ST_TYPE (ssymbuf2
->ssym
[i
].st_info
) == STT_SECTION
)
8780 count2
-= sec_count2
;
8783 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8787 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8789 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8790 if (symtable1
== NULL
|| symtable2
== NULL
)
8794 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
+ sec_count1
;
8795 ssym
< ssymend
; ssym
++)
8797 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8799 symp
->u
.ssym
= ssym
;
8800 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8807 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
+ sec_count2
;
8808 ssym
< ssymend
; ssym
++)
8810 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8812 symp
->u
.ssym
= ssym
;
8813 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8819 /* Sort symbol by name. */
8820 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8821 elf_sym_name_compare
);
8822 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8823 elf_sym_name_compare
);
8825 for (i
= 0; i
< count1
; i
++)
8826 /* Two symbols must have the same binding, type and name. */
8827 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8828 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8829 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8836 symtable1
= (struct elf_symbol
*)
8837 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8838 symtable2
= (struct elf_symbol
*)
8839 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8840 if (symtable1
== NULL
|| symtable2
== NULL
)
8843 /* Count definitions in the section. */
8845 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8846 if (isym
->st_shndx
== shndx1
8847 && (!ignore_section_symbol_p
8848 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8849 symtable1
[count1
++].u
.isym
= isym
;
8852 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8853 if (isym
->st_shndx
== shndx2
8854 && (!ignore_section_symbol_p
8855 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8856 symtable2
[count2
++].u
.isym
= isym
;
8858 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8861 for (i
= 0; i
< count1
; i
++)
8863 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8864 symtable1
[i
].u
.isym
->st_name
);
8866 for (i
= 0; i
< count2
; i
++)
8868 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8869 symtable2
[i
].u
.isym
->st_name
);
8871 /* Sort symbol by name. */
8872 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8873 elf_sym_name_compare
);
8874 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8875 elf_sym_name_compare
);
8877 for (i
= 0; i
< count1
; i
++)
8878 /* Two symbols must have the same binding, type and name. */
8879 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8880 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8881 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8895 /* Return TRUE if 2 section types are compatible. */
8898 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8899 bfd
*bbfd
, const asection
*bsec
)
8903 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8904 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8907 return elf_section_type (asec
) == elf_section_type (bsec
);
8910 /* Final phase of ELF linker. */
8912 /* A structure we use to avoid passing large numbers of arguments. */
8914 struct elf_final_link_info
8916 /* General link information. */
8917 struct bfd_link_info
*info
;
8920 /* Symbol string table. */
8921 struct elf_strtab_hash
*symstrtab
;
8922 /* .hash section. */
8924 /* symbol version section (.gnu.version). */
8925 asection
*symver_sec
;
8926 /* Buffer large enough to hold contents of any section. */
8928 /* Buffer large enough to hold external relocs of any section. */
8929 void *external_relocs
;
8930 /* Buffer large enough to hold internal relocs of any section. */
8931 Elf_Internal_Rela
*internal_relocs
;
8932 /* Buffer large enough to hold external local symbols of any input
8934 bfd_byte
*external_syms
;
8935 /* And a buffer for symbol section indices. */
8936 Elf_External_Sym_Shndx
*locsym_shndx
;
8937 /* Buffer large enough to hold internal local symbols of any input
8939 Elf_Internal_Sym
*internal_syms
;
8940 /* Array large enough to hold a symbol index for each local symbol
8941 of any input BFD. */
8943 /* Array large enough to hold a section pointer for each local
8944 symbol of any input BFD. */
8945 asection
**sections
;
8946 /* Buffer for SHT_SYMTAB_SHNDX section. */
8947 Elf_External_Sym_Shndx
*symshndxbuf
;
8948 /* Number of STT_FILE syms seen. */
8949 size_t filesym_count
;
8950 /* Local symbol hash table. */
8951 struct bfd_hash_table local_hash_table
;
8954 struct local_hash_entry
8956 /* Base hash table entry structure. */
8957 struct bfd_hash_entry root
;
8958 /* Size of the local symbol name. */
8960 /* Number of the duplicated local symbol names. */
8964 /* Create an entry in the local symbol hash table. */
8966 static struct bfd_hash_entry
*
8967 local_hash_newfunc (struct bfd_hash_entry
*entry
,
8968 struct bfd_hash_table
*table
,
8972 /* Allocate the structure if it has not already been allocated by a
8976 entry
= bfd_hash_allocate (table
,
8977 sizeof (struct local_hash_entry
));
8982 /* Call the allocation method of the superclass. */
8983 entry
= bfd_hash_newfunc (entry
, table
, string
);
8986 ((struct local_hash_entry
*) entry
)->count
= 0;
8987 ((struct local_hash_entry
*) entry
)->size
= 0;
8993 /* This struct is used to pass information to elf_link_output_extsym. */
8995 struct elf_outext_info
9000 struct elf_final_link_info
*flinfo
;
9004 /* Support for evaluating a complex relocation.
9006 Complex relocations are generalized, self-describing relocations. The
9007 implementation of them consists of two parts: complex symbols, and the
9008 relocations themselves.
9010 The relocations use a reserved elf-wide relocation type code (R_RELC
9011 external / BFD_RELOC_RELC internal) and an encoding of relocation field
9012 information (start bit, end bit, word width, etc) into the addend. This
9013 information is extracted from CGEN-generated operand tables within gas.
9015 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9016 internal) representing prefix-notation expressions, including but not
9017 limited to those sorts of expressions normally encoded as addends in the
9018 addend field. The symbol mangling format is:
9021 | <unary-operator> ':' <node>
9022 | <binary-operator> ':' <node> ':' <node>
9025 <literal> := 's' <digits=N> ':' <N character symbol name>
9026 | 'S' <digits=N> ':' <N character section name>
9030 <binary-operator> := as in C
9031 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
9034 set_symbol_value (bfd
*bfd_with_globals
,
9035 Elf_Internal_Sym
*isymbuf
,
9040 struct elf_link_hash_entry
**sym_hashes
;
9041 struct elf_link_hash_entry
*h
;
9042 size_t extsymoff
= locsymcount
;
9044 if (symidx
< locsymcount
)
9046 Elf_Internal_Sym
*sym
;
9048 sym
= isymbuf
+ symidx
;
9049 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
9051 /* It is a local symbol: move it to the
9052 "absolute" section and give it a value. */
9053 sym
->st_shndx
= SHN_ABS
;
9054 sym
->st_value
= val
;
9057 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
9061 /* It is a global symbol: set its link type
9062 to "defined" and give it a value. */
9064 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
9065 h
= sym_hashes
[symidx
- extsymoff
];
9066 while (h
->root
.type
== bfd_link_hash_indirect
9067 || h
->root
.type
== bfd_link_hash_warning
)
9068 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9069 h
->root
.type
= bfd_link_hash_defined
;
9070 h
->root
.u
.def
.value
= val
;
9071 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
9075 resolve_symbol (const char *name
,
9077 struct elf_final_link_info
*flinfo
,
9079 Elf_Internal_Sym
*isymbuf
,
9082 Elf_Internal_Sym
*sym
;
9083 struct bfd_link_hash_entry
*global_entry
;
9084 const char *candidate
= NULL
;
9085 Elf_Internal_Shdr
*symtab_hdr
;
9088 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
9090 for (i
= 0; i
< locsymcount
; ++ i
)
9094 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
9097 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
9098 symtab_hdr
->sh_link
,
9101 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9102 name
, candidate
, (unsigned long) sym
->st_value
);
9104 if (candidate
&& strcmp (candidate
, name
) == 0)
9106 asection
*sec
= flinfo
->sections
[i
];
9108 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
9109 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
9111 printf ("Found symbol with value %8.8lx\n",
9112 (unsigned long) *result
);
9118 /* Hmm, haven't found it yet. perhaps it is a global. */
9119 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
9120 false, false, true);
9124 if (global_entry
->type
== bfd_link_hash_defined
9125 || global_entry
->type
== bfd_link_hash_defweak
)
9127 *result
= (global_entry
->u
.def
.value
9128 + global_entry
->u
.def
.section
->output_section
->vma
9129 + global_entry
->u
.def
.section
->output_offset
);
9131 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9132 global_entry
->root
.string
, (unsigned long) *result
);
9140 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
9141 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
9142 names like "foo.end" which is the end address of section "foo". */
9145 resolve_section (const char *name
,
9153 for (curr
= sections
; curr
; curr
= curr
->next
)
9154 if (strcmp (curr
->name
, name
) == 0)
9156 *result
= curr
->vma
;
9160 /* Hmm. still haven't found it. try pseudo-section names. */
9161 /* FIXME: This could be coded more efficiently... */
9162 for (curr
= sections
; curr
; curr
= curr
->next
)
9164 len
= strlen (curr
->name
);
9165 if (len
> strlen (name
))
9168 if (strncmp (curr
->name
, name
, len
) == 0)
9170 if (startswith (name
+ len
, ".end"))
9172 *result
= (curr
->vma
9173 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
9177 /* Insert more pseudo-section names here, if you like. */
9185 undefined_reference (const char *reftype
, const char *name
)
9187 /* xgettext:c-format */
9188 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9190 bfd_set_error (bfd_error_bad_value
);
9194 eval_symbol (bfd_vma
*result
,
9197 struct elf_final_link_info
*flinfo
,
9199 Elf_Internal_Sym
*isymbuf
,
9208 const char *sym
= *symp
;
9210 bool symbol_is_section
= false;
9215 if (len
< 1 || len
> sizeof (symbuf
))
9217 bfd_set_error (bfd_error_invalid_operation
);
9230 *result
= strtoul (sym
, (char **) symp
, 16);
9234 symbol_is_section
= true;
9238 symlen
= strtol (sym
, (char **) symp
, 10);
9239 sym
= *symp
+ 1; /* Skip the trailing ':'. */
9241 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
9243 bfd_set_error (bfd_error_invalid_operation
);
9247 memcpy (symbuf
, sym
, symlen
);
9248 symbuf
[symlen
] = '\0';
9249 *symp
= sym
+ symlen
;
9251 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9252 the symbol as a section, or vice-versa. so we're pretty liberal in our
9253 interpretation here; section means "try section first", not "must be a
9254 section", and likewise with symbol. */
9256 if (symbol_is_section
)
9258 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
9259 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
9260 isymbuf
, locsymcount
))
9262 undefined_reference ("section", symbuf
);
9268 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
9269 isymbuf
, locsymcount
)
9270 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
9273 undefined_reference ("symbol", symbuf
);
9280 /* All that remains are operators. */
9282 #define UNARY_OP(op) \
9283 if (startswith (sym, #op)) \
9285 sym += strlen (#op); \
9289 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9290 isymbuf, locsymcount, signed_p)) \
9293 *result = op ((bfd_signed_vma) a); \
9299 #define BINARY_OP_HEAD(op) \
9300 if (startswith (sym, #op)) \
9302 sym += strlen (#op); \
9306 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9307 isymbuf, locsymcount, signed_p)) \
9310 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9311 isymbuf, locsymcount, signed_p)) \
9313 #define BINARY_OP_TAIL(op) \
9315 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9320 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9324 BINARY_OP_HEAD (<<);
9325 if (b
>= sizeof (a
) * CHAR_BIT
)
9331 BINARY_OP_TAIL (<<);
9332 BINARY_OP_HEAD (>>);
9333 if (b
>= sizeof (a
) * CHAR_BIT
)
9335 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
9338 BINARY_OP_TAIL (>>);
9351 _bfd_error_handler (_("division by zero"));
9352 bfd_set_error (bfd_error_bad_value
);
9359 _bfd_error_handler (_("division by zero"));
9360 bfd_set_error (bfd_error_bad_value
);
9373 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
9374 bfd_set_error (bfd_error_invalid_operation
);
9380 put_value (bfd_vma size
,
9381 unsigned long chunksz
,
9386 location
+= (size
- chunksz
);
9388 for (; size
; size
-= chunksz
, location
-= chunksz
)
9393 bfd_put_8 (input_bfd
, x
, location
);
9397 bfd_put_16 (input_bfd
, x
, location
);
9401 bfd_put_32 (input_bfd
, x
, location
);
9402 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9408 bfd_put_64 (input_bfd
, x
, location
);
9409 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9422 get_value (bfd_vma size
,
9423 unsigned long chunksz
,
9430 /* Sanity checks. */
9431 BFD_ASSERT (chunksz
<= sizeof (x
)
9434 && (size
% chunksz
) == 0
9435 && input_bfd
!= NULL
9436 && location
!= NULL
);
9438 if (chunksz
== sizeof (x
))
9440 BFD_ASSERT (size
== chunksz
);
9442 /* Make sure that we do not perform an undefined shift operation.
9443 We know that size == chunksz so there will only be one iteration
9444 of the loop below. */
9448 shift
= 8 * chunksz
;
9450 for (; size
; size
-= chunksz
, location
+= chunksz
)
9455 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
9458 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
9461 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
9465 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
9476 decode_complex_addend (unsigned long *start
, /* in bits */
9477 unsigned long *oplen
, /* in bits */
9478 unsigned long *len
, /* in bits */
9479 unsigned long *wordsz
, /* in bytes */
9480 unsigned long *chunksz
, /* in bytes */
9481 unsigned long *lsb0_p
,
9482 unsigned long *signed_p
,
9483 unsigned long *trunc_p
,
9484 unsigned long encoded
)
9486 * start
= encoded
& 0x3F;
9487 * len
= (encoded
>> 6) & 0x3F;
9488 * oplen
= (encoded
>> 12) & 0x3F;
9489 * wordsz
= (encoded
>> 18) & 0xF;
9490 * chunksz
= (encoded
>> 22) & 0xF;
9491 * lsb0_p
= (encoded
>> 27) & 1;
9492 * signed_p
= (encoded
>> 28) & 1;
9493 * trunc_p
= (encoded
>> 29) & 1;
9496 bfd_reloc_status_type
9497 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
9498 asection
*input_section
,
9500 Elf_Internal_Rela
*rel
,
9503 bfd_vma shift
, x
, mask
;
9504 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9505 bfd_reloc_status_type r
;
9506 bfd_size_type octets
;
9508 /* Perform this reloc, since it is complex.
9509 (this is not to say that it necessarily refers to a complex
9510 symbol; merely that it is a self-describing CGEN based reloc.
9511 i.e. the addend has the complete reloc information (bit start, end,
9512 word size, etc) encoded within it.). */
9514 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9515 &chunksz
, &lsb0_p
, &signed_p
,
9516 &trunc_p
, rel
->r_addend
);
9518 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9521 shift
= (start
+ 1) - len
;
9523 shift
= (8 * wordsz
) - (start
+ len
);
9525 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9526 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9529 printf ("Doing complex reloc: "
9530 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9531 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9532 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9533 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9534 oplen
, (unsigned long) x
, (unsigned long) mask
,
9535 (unsigned long) relocation
);
9540 /* Now do an overflow check. */
9541 r
= bfd_check_overflow ((signed_p
9542 ? complain_overflow_signed
9543 : complain_overflow_unsigned
),
9544 len
, 0, (8 * wordsz
),
9548 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9551 printf (" relocation: %8.8lx\n"
9552 " shifted mask: %8.8lx\n"
9553 " shifted/masked reloc: %8.8lx\n"
9554 " result: %8.8lx\n",
9555 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9556 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9558 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9562 /* Functions to read r_offset from external (target order) reloc
9563 entry. Faster than bfd_getl32 et al, because we let the compiler
9564 know the value is aligned. */
9567 ext32l_r_offset (const void *p
)
9574 const union aligned32
*a
9575 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9577 uint32_t aval
= ( (uint32_t) a
->c
[0]
9578 | (uint32_t) a
->c
[1] << 8
9579 | (uint32_t) a
->c
[2] << 16
9580 | (uint32_t) a
->c
[3] << 24);
9585 ext32b_r_offset (const void *p
)
9592 const union aligned32
*a
9593 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9595 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9596 | (uint32_t) a
->c
[1] << 16
9597 | (uint32_t) a
->c
[2] << 8
9598 | (uint32_t) a
->c
[3]);
9603 ext64l_r_offset (const void *p
)
9610 const union aligned64
*a
9611 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9613 uint64_t aval
= ( (uint64_t) a
->c
[0]
9614 | (uint64_t) a
->c
[1] << 8
9615 | (uint64_t) a
->c
[2] << 16
9616 | (uint64_t) a
->c
[3] << 24
9617 | (uint64_t) a
->c
[4] << 32
9618 | (uint64_t) a
->c
[5] << 40
9619 | (uint64_t) a
->c
[6] << 48
9620 | (uint64_t) a
->c
[7] << 56);
9625 ext64b_r_offset (const void *p
)
9632 const union aligned64
*a
9633 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9635 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9636 | (uint64_t) a
->c
[1] << 48
9637 | (uint64_t) a
->c
[2] << 40
9638 | (uint64_t) a
->c
[3] << 32
9639 | (uint64_t) a
->c
[4] << 24
9640 | (uint64_t) a
->c
[5] << 16
9641 | (uint64_t) a
->c
[6] << 8
9642 | (uint64_t) a
->c
[7]);
9646 /* When performing a relocatable link, the input relocations are
9647 preserved. But, if they reference global symbols, the indices
9648 referenced must be updated. Update all the relocations found in
9652 elf_link_adjust_relocs (bfd
*abfd
,
9654 struct bfd_elf_section_reloc_data
*reldata
,
9656 struct bfd_link_info
*info
)
9659 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9661 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9662 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9663 bfd_vma r_type_mask
;
9665 unsigned int count
= reldata
->count
;
9666 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9668 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9670 swap_in
= bed
->s
->swap_reloc_in
;
9671 swap_out
= bed
->s
->swap_reloc_out
;
9673 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9675 swap_in
= bed
->s
->swap_reloca_in
;
9676 swap_out
= bed
->s
->swap_reloca_out
;
9681 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9684 if (bed
->s
->arch_size
== 32)
9691 r_type_mask
= 0xffffffff;
9695 erela
= reldata
->hdr
->contents
;
9696 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9698 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9701 if (*rel_hash
== NULL
)
9704 if ((*rel_hash
)->indx
== -2
9705 && info
->gc_sections
9706 && ! info
->gc_keep_exported
)
9708 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9709 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9711 (*rel_hash
)->root
.root
.string
);
9712 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9714 bfd_set_error (bfd_error_invalid_operation
);
9717 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9719 (*swap_in
) (abfd
, erela
, irela
);
9720 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9721 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9722 | (irela
[j
].r_info
& r_type_mask
));
9723 (*swap_out
) (abfd
, irela
, erela
);
9726 if (bed
->elf_backend_update_relocs
)
9727 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9729 if (sort
&& count
!= 0)
9731 bfd_vma (*ext_r_off
) (const void *);
9734 bfd_byte
*base
, *end
, *p
, *loc
;
9735 bfd_byte
*buf
= NULL
;
9737 if (bed
->s
->arch_size
== 32)
9739 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9740 ext_r_off
= ext32l_r_offset
;
9741 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9742 ext_r_off
= ext32b_r_offset
;
9748 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9749 ext_r_off
= ext64l_r_offset
;
9750 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9751 ext_r_off
= ext64b_r_offset
;
9756 /* Must use a stable sort here. A modified insertion sort,
9757 since the relocs are mostly sorted already. */
9758 elt_size
= reldata
->hdr
->sh_entsize
;
9759 base
= reldata
->hdr
->contents
;
9760 end
= base
+ count
* elt_size
;
9761 if (elt_size
> sizeof (Elf64_External_Rela
))
9764 /* Ensure the first element is lowest. This acts as a sentinel,
9765 speeding the main loop below. */
9766 r_off
= (*ext_r_off
) (base
);
9767 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9769 bfd_vma r_off2
= (*ext_r_off
) (p
);
9778 /* Don't just swap *base and *loc as that changes the order
9779 of the original base[0] and base[1] if they happen to
9780 have the same r_offset. */
9781 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9782 memcpy (onebuf
, loc
, elt_size
);
9783 memmove (base
+ elt_size
, base
, loc
- base
);
9784 memcpy (base
, onebuf
, elt_size
);
9787 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9789 /* base to p is sorted, *p is next to insert. */
9790 r_off
= (*ext_r_off
) (p
);
9791 /* Search the sorted region for location to insert. */
9793 while (r_off
< (*ext_r_off
) (loc
))
9798 /* Chances are there is a run of relocs to insert here,
9799 from one of more input files. Files are not always
9800 linked in order due to the way elf_link_input_bfd is
9801 called. See pr17666. */
9802 size_t sortlen
= p
- loc
;
9803 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9804 size_t runlen
= elt_size
;
9805 bfd_vma r_off_runend
= r_off
;
9806 bfd_vma r_off_runend_next
;
9807 size_t buf_size
= 96 * 1024;
9808 while (p
+ runlen
< end
9809 && (sortlen
<= buf_size
9810 || runlen
+ elt_size
<= buf_size
)
9811 /* run must not break the ordering of base..loc+1 */
9812 && r_off2
> (r_off_runend_next
= (*ext_r_off
) (p
+ runlen
))
9813 /* run must be already sorted */
9814 && r_off_runend_next
>= r_off_runend
)
9817 r_off_runend
= r_off_runend_next
;
9821 buf
= bfd_malloc (buf_size
);
9825 if (runlen
< sortlen
)
9827 memcpy (buf
, p
, runlen
);
9828 memmove (loc
+ runlen
, loc
, sortlen
);
9829 memcpy (loc
, buf
, runlen
);
9833 memcpy (buf
, loc
, sortlen
);
9834 memmove (loc
, p
, runlen
);
9835 memcpy (loc
+ runlen
, buf
, sortlen
);
9837 p
+= runlen
- elt_size
;
9840 /* Hashes are no longer valid. */
9841 free (reldata
->hashes
);
9842 reldata
->hashes
= NULL
;
9848 struct elf_link_sort_rela
9854 enum elf_reloc_type_class type
;
9855 /* We use this as an array of size int_rels_per_ext_rel. */
9856 Elf_Internal_Rela rela
[1];
9859 /* qsort stability here and for cmp2 is only an issue if multiple
9860 dynamic relocations are emitted at the same address. But targets
9861 that apply a series of dynamic relocations each operating on the
9862 result of the prior relocation can't use -z combreloc as
9863 implemented anyway. Such schemes tend to be broken by sorting on
9864 symbol index. That leaves dynamic NONE relocs as the only other
9865 case where ld might emit multiple relocs at the same address, and
9866 those are only emitted due to target bugs. */
9869 elf_link_sort_cmp1 (const void *A
, const void *B
)
9871 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9872 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9873 int relativea
, relativeb
;
9875 relativea
= a
->type
== reloc_class_relative
;
9876 relativeb
= b
->type
== reloc_class_relative
;
9878 if (relativea
< relativeb
)
9880 if (relativea
> relativeb
)
9882 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9884 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9886 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9888 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9894 elf_link_sort_cmp2 (const void *A
, const void *B
)
9896 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9897 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9899 if (a
->type
< b
->type
)
9901 if (a
->type
> b
->type
)
9903 if (a
->u
.offset
< b
->u
.offset
)
9905 if (a
->u
.offset
> b
->u
.offset
)
9907 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9909 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9915 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9917 asection
*dynamic_relocs
;
9920 bfd_size_type count
, size
;
9921 size_t i
, ret
, sort_elt
, ext_size
;
9922 bfd_byte
*sort
, *s_non_relative
, *p
;
9923 struct elf_link_sort_rela
*sq
;
9924 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9925 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9926 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9927 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9928 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9929 struct bfd_link_order
*lo
;
9933 /* Find a dynamic reloc section. */
9934 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9935 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9936 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9937 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9939 bool use_rela_initialised
= false;
9941 /* This is just here to stop gcc from complaining.
9942 Its initialization checking code is not perfect. */
9945 /* Both sections are present. Examine the sizes
9946 of the indirect sections to help us choose. */
9947 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9948 if (lo
->type
== bfd_indirect_link_order
)
9950 asection
*o
= lo
->u
.indirect
.section
;
9952 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9954 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9955 /* Section size is divisible by both rel and rela sizes.
9956 It is of no help to us. */
9960 /* Section size is only divisible by rela. */
9961 if (use_rela_initialised
&& !use_rela
)
9963 _bfd_error_handler (_("%pB: unable to sort relocs - "
9964 "they are in more than one size"),
9966 bfd_set_error (bfd_error_invalid_operation
);
9972 use_rela_initialised
= true;
9976 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9978 /* Section size is only divisible by rel. */
9979 if (use_rela_initialised
&& use_rela
)
9981 _bfd_error_handler (_("%pB: unable to sort relocs - "
9982 "they are in more than one size"),
9984 bfd_set_error (bfd_error_invalid_operation
);
9990 use_rela_initialised
= true;
9995 /* The section size is not divisible by either -
9996 something is wrong. */
9997 _bfd_error_handler (_("%pB: unable to sort relocs - "
9998 "they are of an unknown size"), abfd
);
9999 bfd_set_error (bfd_error_invalid_operation
);
10004 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10005 if (lo
->type
== bfd_indirect_link_order
)
10007 asection
*o
= lo
->u
.indirect
.section
;
10009 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
10011 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
10012 /* Section size is divisible by both rel and rela sizes.
10013 It is of no help to us. */
10017 /* Section size is only divisible by rela. */
10018 if (use_rela_initialised
&& !use_rela
)
10020 _bfd_error_handler (_("%pB: unable to sort relocs - "
10021 "they are in more than one size"),
10023 bfd_set_error (bfd_error_invalid_operation
);
10029 use_rela_initialised
= true;
10033 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
10035 /* Section size is only divisible by rel. */
10036 if (use_rela_initialised
&& use_rela
)
10038 _bfd_error_handler (_("%pB: unable to sort relocs - "
10039 "they are in more than one size"),
10041 bfd_set_error (bfd_error_invalid_operation
);
10047 use_rela_initialised
= true;
10052 /* The section size is not divisible by either -
10053 something is wrong. */
10054 _bfd_error_handler (_("%pB: unable to sort relocs - "
10055 "they are of an unknown size"), abfd
);
10056 bfd_set_error (bfd_error_invalid_operation
);
10061 if (! use_rela_initialised
)
10062 /* Make a guess. */
10065 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
10067 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
10074 dynamic_relocs
= rela_dyn
;
10075 ext_size
= bed
->s
->sizeof_rela
;
10076 swap_in
= bed
->s
->swap_reloca_in
;
10077 swap_out
= bed
->s
->swap_reloca_out
;
10081 dynamic_relocs
= rel_dyn
;
10082 ext_size
= bed
->s
->sizeof_rel
;
10083 swap_in
= bed
->s
->swap_reloc_in
;
10084 swap_out
= bed
->s
->swap_reloc_out
;
10088 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10089 if (lo
->type
== bfd_indirect_link_order
)
10090 size
+= lo
->u
.indirect
.section
->size
;
10092 if (size
!= dynamic_relocs
->size
)
10095 sort_elt
= (sizeof (struct elf_link_sort_rela
)
10096 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
10098 count
= dynamic_relocs
->size
/ ext_size
;
10101 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
10105 (*info
->callbacks
->warning
)
10106 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
10110 if (bed
->s
->arch_size
== 32)
10111 r_sym_mask
= ~(bfd_vma
) 0xff;
10113 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
10115 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10116 if (lo
->type
== bfd_indirect_link_order
)
10118 bfd_byte
*erel
, *erelend
;
10119 asection
*o
= lo
->u
.indirect
.section
;
10121 if (o
->contents
== NULL
&& o
->size
!= 0)
10123 /* This is a reloc section that is being handled as a normal
10124 section. See bfd_section_from_shdr. We can't combine
10125 relocs in this case. */
10129 erel
= o
->contents
;
10130 erelend
= o
->contents
+ o
->size
;
10131 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
10133 while (erel
< erelend
)
10135 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
10137 (*swap_in
) (abfd
, erel
, s
->rela
);
10138 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
10139 s
->u
.sym_mask
= r_sym_mask
;
10145 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
10147 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
10149 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
10150 if (s
->type
!= reloc_class_relative
)
10154 s_non_relative
= p
;
10156 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
10157 for (; i
< count
; i
++, p
+= sort_elt
)
10159 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
10160 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
10162 sp
->u
.offset
= sq
->rela
->r_offset
;
10165 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
10167 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
10168 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
10170 /* We have plt relocs in .rela.dyn. */
10171 sq
= (struct elf_link_sort_rela
*) sort
;
10172 for (i
= 0; i
< count
; i
++)
10173 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
10175 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
10177 struct bfd_link_order
**plo
;
10178 /* Put srelplt link_order last. This is so the output_offset
10179 set in the next loop is correct for DT_JMPREL. */
10180 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
10181 if ((*plo
)->type
== bfd_indirect_link_order
10182 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
10188 plo
= &(*plo
)->next
;
10191 dynamic_relocs
->map_tail
.link_order
= lo
;
10196 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10197 if (lo
->type
== bfd_indirect_link_order
)
10199 bfd_byte
*erel
, *erelend
;
10200 asection
*o
= lo
->u
.indirect
.section
;
10202 erel
= o
->contents
;
10203 erelend
= o
->contents
+ o
->size
;
10204 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
10205 while (erel
< erelend
)
10207 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
10208 (*swap_out
) (abfd
, s
->rela
, erel
);
10215 *psec
= dynamic_relocs
;
10219 /* Add a symbol to the output symbol string table. */
10222 elf_link_output_symstrtab (void *finf
,
10224 Elf_Internal_Sym
*elfsym
,
10225 asection
*input_sec
,
10226 struct elf_link_hash_entry
*h
)
10228 struct elf_final_link_info
*flinfo
= finf
;
10229 int (*output_symbol_hook
)
10230 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
10231 struct elf_link_hash_entry
*);
10232 struct elf_link_hash_table
*hash_table
;
10233 const struct elf_backend_data
*bed
;
10234 bfd_size_type strtabsize
;
10236 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10238 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10239 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
10240 if (output_symbol_hook
!= NULL
)
10242 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
10247 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
10248 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
10249 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
10250 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
10252 if (name
== NULL
|| *name
== '\0')
10253 elfsym
->st_name
= (unsigned long) -1;
10256 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10257 to get the final offset for st_name. */
10258 char *versioned_name
= (char *) name
;
10261 if (h
->versioned
== versioned
&& h
->def_dynamic
)
10263 /* Keep only one '@' for versioned symbols defined in
10265 char *version
= strrchr (name
, ELF_VER_CHR
);
10266 char *base_end
= strchr (name
, ELF_VER_CHR
);
10267 if (version
!= base_end
)
10270 size_t len
= strlen (name
);
10271 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
10272 if (versioned_name
== NULL
)
10274 base_len
= base_end
- name
;
10275 memcpy (versioned_name
, name
, base_len
);
10276 memcpy (versioned_name
+ base_len
, version
,
10281 else if (flinfo
->info
->unique_symbol
10282 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
10284 struct local_hash_entry
*lh
;
10288 switch (ELF_ST_TYPE (elfsym
->st_info
))
10294 lh
= (struct local_hash_entry
*) bfd_hash_lookup
10295 (&flinfo
->local_hash_table
, name
, true, false);
10298 /* Always append ".COUNT" to local symbols to avoid
10299 potential conflicts with local symbol "XXX.COUNT". */
10300 sprintf (buf
, "%lx", lh
->count
);
10301 base_len
= lh
->size
;
10304 base_len
= strlen (name
);
10305 lh
->size
= base_len
;
10307 count_len
= strlen (buf
);
10308 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
10309 base_len
+ count_len
+ 2);
10310 if (versioned_name
== NULL
)
10312 memcpy (versioned_name
, name
, base_len
);
10313 versioned_name
[base_len
] = '.';
10314 memcpy (versioned_name
+ base_len
+ 1, buf
,
10321 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
10322 versioned_name
, false);
10323 if (elfsym
->st_name
== (unsigned long) -1)
10327 hash_table
= elf_hash_table (flinfo
->info
);
10328 strtabsize
= hash_table
->strtabsize
;
10329 if (strtabsize
<= flinfo
->output_bfd
->symcount
)
10331 strtabsize
+= strtabsize
;
10332 hash_table
->strtabsize
= strtabsize
;
10333 strtabsize
*= sizeof (*hash_table
->strtab
);
10335 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
10337 if (hash_table
->strtab
== NULL
)
10340 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].sym
= *elfsym
;
10341 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].dest_index
10342 = flinfo
->output_bfd
->symcount
;
10343 flinfo
->output_bfd
->symcount
+= 1;
10348 /* Swap symbols out to the symbol table and flush the output symbols to
10352 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
10354 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
10357 const struct elf_backend_data
*bed
;
10359 Elf_Internal_Shdr
*hdr
;
10363 if (flinfo
->output_bfd
->symcount
== 0)
10366 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10368 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10370 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10371 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
10372 if (symbuf
== NULL
)
10375 if (flinfo
->symshndxbuf
)
10377 amt
= sizeof (Elf_External_Sym_Shndx
);
10378 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
10379 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
10380 if (flinfo
->symshndxbuf
== NULL
)
10387 /* Now swap out the symbols. */
10388 for (i
= 0; i
< flinfo
->output_bfd
->symcount
; i
++)
10390 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
10391 if (elfsym
->sym
.st_name
== (unsigned long) -1)
10392 elfsym
->sym
.st_name
= 0;
10394 elfsym
->sym
.st_name
10395 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
10396 elfsym
->sym
.st_name
);
10398 /* Inform the linker of the addition of this symbol. */
10400 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
10401 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
10404 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
10405 ((bfd_byte
*) symbuf
10406 + (elfsym
->dest_index
10407 * bed
->s
->sizeof_sym
)),
10408 NPTR_ADD (flinfo
->symshndxbuf
,
10409 elfsym
->dest_index
));
10412 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
10413 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
10414 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10415 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
10416 && bfd_write (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
10418 hdr
->sh_size
+= amt
;
10426 free (hash_table
->strtab
);
10427 hash_table
->strtab
= NULL
;
10432 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10435 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
10437 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
10438 && sym
->st_shndx
< SHN_LORESERVE
)
10440 /* The gABI doesn't support dynamic symbols in output sections
10443 /* xgettext:c-format */
10444 (_("%pB: too many sections: %d (>= %d)"),
10445 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
10446 bfd_set_error (bfd_error_nonrepresentable_section
);
10452 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10453 allowing an unsatisfied unversioned symbol in the DSO to match a
10454 versioned symbol that would normally require an explicit version.
10455 We also handle the case that a DSO references a hidden symbol
10456 which may be satisfied by a versioned symbol in another DSO. */
10459 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
10460 const struct elf_backend_data
*bed
,
10461 struct elf_link_hash_entry
*h
)
10464 struct elf_link_loaded_list
*loaded
;
10466 if (!is_elf_hash_table (info
->hash
))
10469 /* Check indirect symbol. */
10470 while (h
->root
.type
== bfd_link_hash_indirect
)
10471 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10473 switch (h
->root
.type
)
10479 case bfd_link_hash_undefined
:
10480 case bfd_link_hash_undefweak
:
10481 abfd
= h
->root
.u
.undef
.abfd
;
10483 || (abfd
->flags
& DYNAMIC
) == 0
10484 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
10488 case bfd_link_hash_defined
:
10489 case bfd_link_hash_defweak
:
10490 abfd
= h
->root
.u
.def
.section
->owner
;
10493 case bfd_link_hash_common
:
10494 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10497 BFD_ASSERT (abfd
!= NULL
);
10499 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10501 loaded
= loaded
->next
)
10504 Elf_Internal_Shdr
*hdr
;
10506 size_t extsymcount
;
10508 Elf_Internal_Shdr
*versymhdr
;
10509 Elf_Internal_Sym
*isym
;
10510 Elf_Internal_Sym
*isymend
;
10511 Elf_Internal_Sym
*isymbuf
;
10512 Elf_External_Versym
*ever
;
10513 Elf_External_Versym
*extversym
;
10515 input
= loaded
->abfd
;
10517 /* We check each DSO for a possible hidden versioned definition. */
10519 || elf_dynversym (input
) == 0)
10522 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10524 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10525 if (elf_bad_symtab (input
))
10527 extsymcount
= symcount
;
10532 extsymcount
= symcount
- hdr
->sh_info
;
10533 extsymoff
= hdr
->sh_info
;
10536 if (extsymcount
== 0)
10539 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10541 if (isymbuf
== NULL
)
10544 /* Read in any version definitions. */
10545 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10546 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10547 || (extversym
= (Elf_External_Versym
*)
10548 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10549 versymhdr
->sh_size
)) == NULL
)
10555 ever
= extversym
+ extsymoff
;
10556 isymend
= isymbuf
+ extsymcount
;
10557 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10560 Elf_Internal_Versym iver
;
10561 unsigned short version_index
;
10563 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10564 || isym
->st_shndx
== SHN_UNDEF
)
10567 name
= bfd_elf_string_from_elf_section (input
,
10570 if (strcmp (name
, h
->root
.root
.string
) != 0)
10573 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10575 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10576 && !(h
->def_regular
10577 && h
->forced_local
))
10579 /* If we have a non-hidden versioned sym, then it should
10580 have provided a definition for the undefined sym unless
10581 it is defined in a non-shared object and forced local.
10586 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10587 if (version_index
== 1 || version_index
== 2)
10589 /* This is the base or first version. We can use it. */
10603 /* Convert ELF common symbol TYPE. */
10606 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10608 /* Commom symbol can only appear in relocatable link. */
10609 if (!bfd_link_relocatable (info
))
10611 switch (info
->elf_stt_common
)
10615 case elf_stt_common
:
10618 case no_elf_stt_common
:
10625 /* Add an external symbol to the symbol table. This is called from
10626 the hash table traversal routine. When generating a shared object,
10627 we go through the symbol table twice. The first time we output
10628 anything that might have been forced to local scope in a version
10629 script. The second time we output the symbols that are still
10633 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10635 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10636 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10637 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10639 Elf_Internal_Sym sym
;
10640 asection
*input_sec
;
10641 const struct elf_backend_data
*bed
;
10646 if (h
->root
.type
== bfd_link_hash_warning
)
10648 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10649 if (h
->root
.type
== bfd_link_hash_new
)
10653 /* Decide whether to output this symbol in this pass. */
10654 if (eoinfo
->localsyms
)
10656 if (!h
->forced_local
)
10661 if (h
->forced_local
)
10665 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10667 if (h
->root
.type
== bfd_link_hash_undefined
)
10669 /* If we have an undefined symbol reference here then it must have
10670 come from a shared library that is being linked in. (Undefined
10671 references in regular files have already been handled unless
10672 they are in unreferenced sections which are removed by garbage
10674 bool ignore_undef
= false;
10676 /* Some symbols may be special in that the fact that they're
10677 undefined can be safely ignored - let backend determine that. */
10678 if (bed
->elf_backend_ignore_undef_symbol
)
10679 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10681 /* If we are reporting errors for this situation then do so now. */
10683 && h
->ref_dynamic_nonweak
10684 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10685 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10686 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10688 flinfo
->info
->callbacks
->undefined_symbol
10689 (flinfo
->info
, h
->root
.root
.string
,
10690 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10691 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10692 && !flinfo
->info
->warn_unresolved_syms
);
10695 /* Strip a global symbol defined in a discarded section. */
10700 /* We should also warn if a forced local symbol is referenced from
10701 shared libraries. */
10702 if (bfd_link_executable (flinfo
->info
)
10707 && h
->ref_dynamic_nonweak
10708 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10712 struct elf_link_hash_entry
*hi
= h
;
10714 /* Check indirect symbol. */
10715 while (hi
->root
.type
== bfd_link_hash_indirect
)
10716 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10718 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10719 /* xgettext:c-format */
10720 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10721 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10722 /* xgettext:c-format */
10723 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10725 /* xgettext:c-format */
10726 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10727 def_bfd
= flinfo
->output_bfd
;
10728 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10729 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10730 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10731 h
->root
.root
.string
, def_bfd
);
10732 bfd_set_error (bfd_error_bad_value
);
10733 eoinfo
->failed
= true;
10737 /* We don't want to output symbols that have never been mentioned by
10738 a regular file, or that we have been told to strip. However, if
10739 h->indx is set to -2, the symbol is used by a reloc and we must
10744 else if ((h
->def_dynamic
10746 || h
->root
.type
== bfd_link_hash_new
)
10748 && !h
->ref_regular
)
10750 else if (flinfo
->info
->strip
== strip_all
)
10752 else if (flinfo
->info
->strip
== strip_some
10753 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10754 h
->root
.root
.string
, false, false) == NULL
)
10756 else if ((h
->root
.type
== bfd_link_hash_defined
10757 || h
->root
.type
== bfd_link_hash_defweak
)
10758 && ((flinfo
->info
->strip_discarded
10759 && discarded_section (h
->root
.u
.def
.section
))
10760 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10761 && h
->root
.u
.def
.section
->owner
!= NULL
10762 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10764 else if ((h
->root
.type
== bfd_link_hash_undefined
10765 || h
->root
.type
== bfd_link_hash_undefweak
)
10766 && h
->root
.u
.undef
.abfd
!= NULL
10767 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10772 /* If we're stripping it, and it's not a dynamic symbol, there's
10773 nothing else to do. However, if it is a forced local symbol or
10774 an ifunc symbol we need to give the backend finish_dynamic_symbol
10775 function a chance to make it dynamic. */
10777 && h
->dynindx
== -1
10778 && type
!= STT_GNU_IFUNC
10779 && !h
->forced_local
)
10783 sym
.st_size
= h
->size
;
10784 sym
.st_other
= h
->other
;
10785 switch (h
->root
.type
)
10788 case bfd_link_hash_new
:
10789 case bfd_link_hash_warning
:
10793 case bfd_link_hash_undefined
:
10794 case bfd_link_hash_undefweak
:
10795 input_sec
= bfd_und_section_ptr
;
10796 sym
.st_shndx
= SHN_UNDEF
;
10799 case bfd_link_hash_defined
:
10800 case bfd_link_hash_defweak
:
10802 input_sec
= h
->root
.u
.def
.section
;
10803 if (input_sec
->output_section
!= NULL
)
10806 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10807 input_sec
->output_section
);
10808 if (sym
.st_shndx
== SHN_BAD
)
10811 /* xgettext:c-format */
10812 (_("%pB: could not find output section %pA for input section %pA"),
10813 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10814 bfd_set_error (bfd_error_nonrepresentable_section
);
10815 eoinfo
->failed
= true;
10819 /* ELF symbols in relocatable files are section relative,
10820 but in nonrelocatable files they are virtual
10822 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10823 if (!bfd_link_relocatable (flinfo
->info
))
10825 sym
.st_value
+= input_sec
->output_section
->vma
;
10826 if (h
->type
== STT_TLS
)
10828 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10829 if (tls_sec
!= NULL
)
10830 sym
.st_value
-= tls_sec
->vma
;
10836 BFD_ASSERT (input_sec
->owner
== NULL
10837 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10838 sym
.st_shndx
= SHN_UNDEF
;
10839 input_sec
= bfd_und_section_ptr
;
10844 case bfd_link_hash_common
:
10845 input_sec
= h
->root
.u
.c
.p
->section
;
10846 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10847 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10850 case bfd_link_hash_indirect
:
10851 /* These symbols are created by symbol versioning. They point
10852 to the decorated version of the name. For example, if the
10853 symbol foo@@GNU_1.2 is the default, which should be used when
10854 foo is used with no version, then we add an indirect symbol
10855 foo which points to foo@@GNU_1.2. We ignore these symbols,
10856 since the indirected symbol is already in the hash table. */
10860 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10861 switch (h
->root
.type
)
10863 case bfd_link_hash_common
:
10864 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10866 case bfd_link_hash_defined
:
10867 case bfd_link_hash_defweak
:
10868 if (bed
->common_definition (&sym
))
10869 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10873 case bfd_link_hash_undefined
:
10874 case bfd_link_hash_undefweak
:
10880 if (h
->forced_local
)
10882 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10883 /* Turn off visibility on local symbol. */
10884 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10886 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10887 else if (h
->unique_global
&& h
->def_regular
)
10888 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10889 else if (h
->root
.type
== bfd_link_hash_undefweak
10890 || h
->root
.type
== bfd_link_hash_defweak
)
10891 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10893 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10894 sym
.st_target_internal
= h
->target_internal
;
10896 /* Give the processor backend a chance to tweak the symbol value,
10897 and also to finish up anything that needs to be done for this
10898 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10899 forced local syms when non-shared is due to a historical quirk.
10900 STT_GNU_IFUNC symbol must go through PLT. */
10901 if ((h
->type
== STT_GNU_IFUNC
10903 && !bfd_link_relocatable (flinfo
->info
))
10904 || ((h
->dynindx
!= -1
10905 || h
->forced_local
)
10906 && ((bfd_link_pic (flinfo
->info
)
10907 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10908 || h
->root
.type
!= bfd_link_hash_undefweak
))
10909 || !h
->forced_local
)
10910 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10912 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10913 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10915 eoinfo
->failed
= true;
10920 /* If we are marking the symbol as undefined, and there are no
10921 non-weak references to this symbol from a regular object, then
10922 mark the symbol as weak undefined; if there are non-weak
10923 references, mark the symbol as strong. We can't do this earlier,
10924 because it might not be marked as undefined until the
10925 finish_dynamic_symbol routine gets through with it. */
10926 if (sym
.st_shndx
== SHN_UNDEF
10928 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10929 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10932 type
= ELF_ST_TYPE (sym
.st_info
);
10934 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10935 if (type
== STT_GNU_IFUNC
)
10938 if (h
->ref_regular_nonweak
)
10939 bindtype
= STB_GLOBAL
;
10941 bindtype
= STB_WEAK
;
10942 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10945 /* If this is a symbol defined in a dynamic library, don't use the
10946 symbol size from the dynamic library. Relinking an executable
10947 against a new library may introduce gratuitous changes in the
10948 executable's symbols if we keep the size. */
10949 if (sym
.st_shndx
== SHN_UNDEF
10954 /* If a non-weak symbol with non-default visibility is not defined
10955 locally, it is a fatal error. */
10956 if (!bfd_link_relocatable (flinfo
->info
)
10957 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
10958 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
10959 && h
->root
.type
== bfd_link_hash_undefined
10960 && !h
->def_regular
)
10964 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
10965 /* xgettext:c-format */
10966 msg
= _("%pB: protected symbol `%s' isn't defined");
10967 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
10968 /* xgettext:c-format */
10969 msg
= _("%pB: internal symbol `%s' isn't defined");
10971 /* xgettext:c-format */
10972 msg
= _("%pB: hidden symbol `%s' isn't defined");
10973 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
10974 bfd_set_error (bfd_error_bad_value
);
10975 eoinfo
->failed
= true;
10979 /* If this symbol should be put in the .dynsym section, then put it
10980 there now. We already know the symbol index. We also fill in
10981 the entry in the .hash section. */
10982 if (h
->dynindx
!= -1
10983 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
10984 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
10985 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
10989 /* Since there is no version information in the dynamic string,
10990 if there is no version info in symbol version section, we will
10991 have a run-time problem if not linking executable, referenced
10992 by shared library, or not bound locally. */
10993 if (h
->verinfo
.verdef
== NULL
10994 && (!bfd_link_executable (flinfo
->info
)
10996 || !h
->def_regular
))
10998 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
11000 if (p
&& p
[1] != '\0')
11003 /* xgettext:c-format */
11004 (_("%pB: no symbol version section for versioned symbol `%s'"),
11005 flinfo
->output_bfd
, h
->root
.root
.string
);
11006 eoinfo
->failed
= true;
11011 sym
.st_name
= h
->dynstr_index
;
11012 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
11013 + h
->dynindx
* bed
->s
->sizeof_sym
);
11014 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
11016 eoinfo
->failed
= true;
11020 /* Inform the linker of the addition of this symbol. */
11022 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
11023 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
11025 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
11027 if (flinfo
->hash_sec
!= NULL
)
11029 size_t hash_entry_size
;
11030 bfd_byte
*bucketpos
;
11032 size_t bucketcount
;
11035 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
11036 bucket
= h
->u
.elf_hash_value
% bucketcount
;
11039 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
11040 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
11041 + (bucket
+ 2) * hash_entry_size
);
11042 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
11043 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
11045 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
11046 ((bfd_byte
*) flinfo
->hash_sec
->contents
11047 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
11050 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
11052 Elf_Internal_Versym iversym
;
11053 Elf_External_Versym
*eversym
;
11055 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
11057 if (h
->verinfo
.verdef
== NULL
11058 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
11059 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
11060 iversym
.vs_vers
= 1;
11062 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
11066 if (h
->verinfo
.vertree
== NULL
)
11067 iversym
.vs_vers
= 1;
11069 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
11070 if (flinfo
->info
->create_default_symver
)
11074 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
11075 defined locally. */
11076 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
11077 iversym
.vs_vers
|= VERSYM_HIDDEN
;
11079 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
11080 eversym
+= h
->dynindx
;
11081 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
11085 /* If the symbol is undefined, and we didn't output it to .dynsym,
11086 strip it from .symtab too. Obviously we can't do this for
11087 relocatable output or when needed for --emit-relocs. */
11088 else if (input_sec
== bfd_und_section_ptr
11090 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
11091 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
11092 && !bfd_link_relocatable (flinfo
->info
))
11095 /* Also strip others that we couldn't earlier due to dynamic symbol
11099 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
11102 /* Output a FILE symbol so that following locals are not associated
11103 with the wrong input file. We need one for forced local symbols
11104 if we've seen more than one FILE symbol or when we have exactly
11105 one FILE symbol but global symbols are present in a file other
11106 than the one with the FILE symbol. We also need one if linker
11107 defined symbols are present. In practice these conditions are
11108 always met, so just emit the FILE symbol unconditionally. */
11109 if (eoinfo
->localsyms
11110 && !eoinfo
->file_sym_done
11111 && eoinfo
->flinfo
->filesym_count
!= 0)
11113 Elf_Internal_Sym fsym
;
11115 memset (&fsym
, 0, sizeof (fsym
));
11116 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11117 fsym
.st_shndx
= SHN_ABS
;
11118 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
11119 bfd_und_section_ptr
, NULL
))
11122 eoinfo
->file_sym_done
= true;
11125 indx
= bfd_get_symcount (flinfo
->output_bfd
);
11126 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
11130 eoinfo
->failed
= true;
11135 else if (h
->indx
== -2)
11141 /* Return TRUE if special handling is done for relocs in SEC against
11142 symbols defined in discarded sections. */
11145 elf_section_ignore_discarded_relocs (asection
*sec
)
11147 const struct elf_backend_data
*bed
;
11149 switch (sec
->sec_info_type
)
11151 case SEC_INFO_TYPE_STABS
:
11152 case SEC_INFO_TYPE_EH_FRAME
:
11153 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11154 case SEC_INFO_TYPE_SFRAME
:
11160 bed
= get_elf_backend_data (sec
->owner
);
11161 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
11162 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
11168 /* Return a mask saying how ld should treat relocations in SEC against
11169 symbols defined in discarded sections. If this function returns
11170 COMPLAIN set, ld will issue a warning message. If this function
11171 returns PRETEND set, and the discarded section was link-once and the
11172 same size as the kept link-once section, ld will pretend that the
11173 symbol was actually defined in the kept section. Otherwise ld will
11174 zero the reloc (at least that is the intent, but some cooperation by
11175 the target dependent code is needed, particularly for REL targets). */
11178 _bfd_elf_default_action_discarded (asection
*sec
)
11180 const struct elf_backend_data
*bed
;
11181 bed
= get_elf_backend_data (sec
->owner
);
11183 if (sec
->flags
& SEC_DEBUGGING
)
11186 if (strcmp (".eh_frame", sec
->name
) == 0)
11189 if (bed
->elf_backend_can_make_multiple_eh_frame
11190 && strncmp (sec
->name
, ".eh_frame.", 10) == 0)
11193 if (strcmp (".sframe", sec
->name
) == 0)
11196 if (strcmp (".gcc_except_table", sec
->name
) == 0)
11199 return COMPLAIN
| PRETEND
;
11202 /* Find a match between a section and a member of a section group. */
11205 match_group_member (asection
*sec
, asection
*group
,
11206 struct bfd_link_info
*info
)
11208 asection
*first
= elf_next_in_group (group
);
11209 asection
*s
= first
;
11213 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
11216 s
= elf_next_in_group (s
);
11224 /* Check if the kept section of a discarded section SEC can be used
11225 to replace it. Return the replacement if it is OK. Otherwise return
11229 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
11233 kept
= sec
->kept_section
;
11236 if ((kept
->flags
& SEC_GROUP
) != 0)
11237 kept
= match_group_member (sec
, kept
, info
);
11240 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
11241 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
11245 /* Get the real kept section. */
11247 for (next
= kept
->kept_section
;
11249 next
= next
->kept_section
)
11253 sec
->kept_section
= kept
;
11258 /* Link an input file into the linker output file. This function
11259 handles all the sections and relocations of the input file at once.
11260 This is so that we only have to read the local symbols once, and
11261 don't have to keep them in memory. */
11264 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
11266 int (*relocate_section
)
11267 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
11268 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
11270 Elf_Internal_Shdr
*symtab_hdr
;
11271 size_t locsymcount
;
11273 Elf_Internal_Sym
*isymbuf
;
11274 Elf_Internal_Sym
*isym
;
11275 Elf_Internal_Sym
*isymend
;
11277 asection
**ppsection
;
11279 const struct elf_backend_data
*bed
;
11280 struct elf_link_hash_entry
**sym_hashes
;
11281 bfd_size_type address_size
;
11282 bfd_vma r_type_mask
;
11284 bool have_file_sym
= false;
11286 output_bfd
= flinfo
->output_bfd
;
11287 bed
= get_elf_backend_data (output_bfd
);
11288 relocate_section
= bed
->elf_backend_relocate_section
;
11290 /* If this is a dynamic object, we don't want to do anything here:
11291 we don't want the local symbols, and we don't want the section
11293 if ((input_bfd
->flags
& DYNAMIC
) != 0)
11296 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
11297 if (elf_bad_symtab (input_bfd
))
11299 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11304 locsymcount
= symtab_hdr
->sh_info
;
11305 extsymoff
= symtab_hdr
->sh_info
;
11308 /* Enable GNU OSABI features in the output BFD that are used in the input
11310 if (bed
->elf_osabi
== ELFOSABI_NONE
11311 || bed
->elf_osabi
== ELFOSABI_GNU
11312 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
11313 elf_tdata (output_bfd
)->has_gnu_osabi
11314 |= (elf_tdata (input_bfd
)->has_gnu_osabi
11315 & (bfd_link_relocatable (flinfo
->info
)
11316 ? -1 : ~elf_gnu_osabi_retain
));
11318 /* Read the local symbols. */
11319 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11320 if (isymbuf
== NULL
&& locsymcount
!= 0)
11322 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
11323 flinfo
->internal_syms
,
11324 flinfo
->external_syms
,
11325 flinfo
->locsym_shndx
);
11326 if (isymbuf
== NULL
)
11330 /* Find local symbol sections and adjust values of symbols in
11331 SEC_MERGE sections. Write out those local symbols we know are
11332 going into the output file. */
11333 isymend
= PTR_ADD (isymbuf
, locsymcount
);
11334 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
11336 isym
++, pindex
++, ppsection
++)
11340 Elf_Internal_Sym osym
;
11346 if (elf_bad_symtab (input_bfd
))
11348 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
11355 if (isym
->st_shndx
== SHN_UNDEF
)
11356 isec
= bfd_und_section_ptr
;
11357 else if (isym
->st_shndx
== SHN_ABS
)
11358 isec
= bfd_abs_section_ptr
;
11359 else if (isym
->st_shndx
== SHN_COMMON
)
11360 isec
= bfd_com_section_ptr
;
11363 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
11366 /* Don't attempt to output symbols with st_shnx in the
11367 reserved range other than SHN_ABS and SHN_COMMON. */
11368 isec
= bfd_und_section_ptr
;
11370 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
11371 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
11373 _bfd_merged_section_offset (output_bfd
, &isec
,
11374 elf_section_data (isec
)->sec_info
,
11380 /* Don't output the first, undefined, symbol. In fact, don't
11381 output any undefined local symbol. */
11382 if (isec
== bfd_und_section_ptr
)
11385 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
11387 /* We never output section symbols. Instead, we use the
11388 section symbol of the corresponding section in the output
11393 /* If we are stripping all symbols, we don't want to output this
11395 if (flinfo
->info
->strip
== strip_all
)
11398 /* If we are discarding all local symbols, we don't want to
11399 output this one. If we are generating a relocatable output
11400 file, then some of the local symbols may be required by
11401 relocs; we output them below as we discover that they are
11403 if (flinfo
->info
->discard
== discard_all
)
11406 /* If this symbol is defined in a section which we are
11407 discarding, we don't need to keep it. */
11408 if (isym
->st_shndx
< SHN_LORESERVE
11409 && (isec
->output_section
== NULL
11410 || bfd_section_removed_from_list (output_bfd
,
11411 isec
->output_section
)))
11414 /* Get the name of the symbol. */
11415 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
11420 /* See if we are discarding symbols with this name. */
11421 if ((flinfo
->info
->strip
== strip_some
11422 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, false, false)
11424 || (((flinfo
->info
->discard
== discard_sec_merge
11425 && (isec
->flags
& SEC_MERGE
)
11426 && !bfd_link_relocatable (flinfo
->info
))
11427 || flinfo
->info
->discard
== discard_l
)
11428 && bfd_is_local_label_name (input_bfd
, name
)))
11431 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
11433 if (input_bfd
->lto_output
)
11434 /* -flto puts a temp file name here. This means builds
11435 are not reproducible. Discard the symbol. */
11437 have_file_sym
= true;
11438 flinfo
->filesym_count
+= 1;
11440 if (!have_file_sym
)
11442 /* In the absence of debug info, bfd_find_nearest_line uses
11443 FILE symbols to determine the source file for local
11444 function symbols. Provide a FILE symbol here if input
11445 files lack such, so that their symbols won't be
11446 associated with a previous input file. It's not the
11447 source file, but the best we can do. */
11448 const char *filename
;
11449 have_file_sym
= true;
11450 flinfo
->filesym_count
+= 1;
11451 memset (&osym
, 0, sizeof (osym
));
11452 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11453 osym
.st_shndx
= SHN_ABS
;
11454 if (input_bfd
->lto_output
)
11457 filename
= lbasename (bfd_get_filename (input_bfd
));
11458 if (!elf_link_output_symstrtab (flinfo
, filename
, &osym
,
11459 bfd_abs_section_ptr
, NULL
))
11465 /* Adjust the section index for the output file. */
11466 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11467 isec
->output_section
);
11468 if (osym
.st_shndx
== SHN_BAD
)
11471 /* ELF symbols in relocatable files are section relative, but
11472 in executable files they are virtual addresses. Note that
11473 this code assumes that all ELF sections have an associated
11474 BFD section with a reasonable value for output_offset; below
11475 we assume that they also have a reasonable value for
11476 output_section. Any special sections must be set up to meet
11477 these requirements. */
11478 osym
.st_value
+= isec
->output_offset
;
11479 if (!bfd_link_relocatable (flinfo
->info
))
11481 osym
.st_value
+= isec
->output_section
->vma
;
11482 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
11484 /* STT_TLS symbols are relative to PT_TLS segment base. */
11485 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
11486 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
11488 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
11493 indx
= bfd_get_symcount (output_bfd
);
11494 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11501 if (bed
->s
->arch_size
== 32)
11503 r_type_mask
= 0xff;
11509 r_type_mask
= 0xffffffff;
11514 /* Relocate the contents of each section. */
11515 sym_hashes
= elf_sym_hashes (input_bfd
);
11516 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11518 bfd_byte
*contents
;
11520 if (! o
->linker_mark
)
11522 /* This section was omitted from the link. */
11526 if (!flinfo
->info
->resolve_section_groups
11527 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11529 /* Deal with the group signature symbol. */
11530 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11531 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11532 asection
*osec
= o
->output_section
;
11534 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11535 if (symndx
>= locsymcount
11536 || (elf_bad_symtab (input_bfd
)
11537 && flinfo
->sections
[symndx
] == NULL
))
11539 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
11540 while (h
->root
.type
== bfd_link_hash_indirect
11541 || h
->root
.type
== bfd_link_hash_warning
)
11542 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11543 /* Arrange for symbol to be output. */
11545 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11547 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11549 /* We'll use the output section target_index. */
11550 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11551 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11555 if (flinfo
->indices
[symndx
] == -1)
11557 /* Otherwise output the local symbol now. */
11558 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11559 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11564 name
= bfd_elf_string_from_elf_section (input_bfd
,
11565 symtab_hdr
->sh_link
,
11570 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11572 if (sym
.st_shndx
== SHN_BAD
)
11575 sym
.st_value
+= o
->output_offset
;
11577 indx
= bfd_get_symcount (output_bfd
);
11578 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11583 flinfo
->indices
[symndx
] = indx
;
11587 elf_section_data (osec
)->this_hdr
.sh_info
11588 = flinfo
->indices
[symndx
];
11592 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11593 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11596 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11598 /* Section was created by _bfd_elf_link_create_dynamic_sections
11603 /* Get the contents of the section. They have been cached by a
11604 relaxation routine. Note that o is a section in an input
11605 file, so the contents field will not have been set by any of
11606 the routines which work on output files. */
11607 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11609 contents
= elf_section_data (o
)->this_hdr
.contents
;
11610 if (bed
->caches_rawsize
11612 && o
->rawsize
< o
->size
)
11614 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11615 contents
= flinfo
->contents
;
11618 else if (!(o
->flags
& SEC_RELOC
)
11619 && !bed
->elf_backend_write_section
11620 && o
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
11621 /* A MERGE section that has no relocations doesn't need the
11622 contents anymore, they have been recorded earlier. Except
11623 if the backend has special provisions for writing sections. */
11627 contents
= flinfo
->contents
;
11628 if (! _bfd_elf_link_mmap_section_contents (input_bfd
, o
,
11633 if ((o
->flags
& SEC_RELOC
) != 0)
11635 Elf_Internal_Rela
*internal_relocs
;
11636 Elf_Internal_Rela
*rel
, *relend
;
11637 int action_discarded
;
11640 /* Get the swapped relocs. */
11642 = _bfd_elf_link_info_read_relocs (input_bfd
, flinfo
->info
, o
,
11643 flinfo
->external_relocs
,
11644 flinfo
->internal_relocs
,
11646 if (internal_relocs
== NULL
11647 && o
->reloc_count
> 0)
11650 action_discarded
= -1;
11651 if (!elf_section_ignore_discarded_relocs (o
))
11652 action_discarded
= (*bed
->action_discarded
) (o
);
11654 /* Run through the relocs evaluating complex reloc symbols and
11655 looking for relocs against symbols from discarded sections
11656 or section symbols from removed link-once sections.
11657 Complain about relocs against discarded sections. Zero
11658 relocs against removed link-once sections. */
11660 rel
= internal_relocs
;
11661 relend
= rel
+ o
->reloc_count
;
11662 for ( ; rel
< relend
; rel
++)
11664 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11665 unsigned int s_type
;
11666 asection
**ps
, *sec
;
11667 struct elf_link_hash_entry
*h
= NULL
;
11668 const char *sym_name
;
11670 if (r_symndx
== STN_UNDEF
)
11673 if (r_symndx
>= locsymcount
11674 || (elf_bad_symtab (input_bfd
)
11675 && flinfo
->sections
[r_symndx
] == NULL
))
11677 h
= sym_hashes
[r_symndx
- extsymoff
];
11679 /* Badly formatted input files can contain relocs that
11680 reference non-existant symbols. Check here so that
11681 we do not seg fault. */
11685 /* xgettext:c-format */
11686 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11687 "that references a non-existent global symbol"),
11688 input_bfd
, (uint64_t) rel
->r_info
, o
);
11689 bfd_set_error (bfd_error_bad_value
);
11693 while (h
->root
.type
== bfd_link_hash_indirect
11694 || h
->root
.type
== bfd_link_hash_warning
)
11695 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11699 /* If a plugin symbol is referenced from a non-IR file,
11700 mark the symbol as undefined. Note that the
11701 linker may attach linker created dynamic sections
11702 to the plugin bfd. Symbols defined in linker
11703 created sections are not plugin symbols. */
11704 if ((h
->root
.non_ir_ref_regular
11705 || h
->root
.non_ir_ref_dynamic
)
11706 && (h
->root
.type
== bfd_link_hash_defined
11707 || h
->root
.type
== bfd_link_hash_defweak
)
11708 && (h
->root
.u
.def
.section
->flags
11709 & SEC_LINKER_CREATED
) == 0
11710 && h
->root
.u
.def
.section
->owner
!= NULL
11711 && (h
->root
.u
.def
.section
->owner
->flags
11712 & BFD_PLUGIN
) != 0)
11714 h
->root
.type
= bfd_link_hash_undefined
;
11715 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11719 if (h
->root
.type
== bfd_link_hash_defined
11720 || h
->root
.type
== bfd_link_hash_defweak
)
11721 ps
= &h
->root
.u
.def
.section
;
11723 sym_name
= h
->root
.root
.string
;
11727 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11729 s_type
= ELF_ST_TYPE (sym
->st_info
);
11730 ps
= &flinfo
->sections
[r_symndx
];
11731 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11735 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11736 && !bfd_link_relocatable (flinfo
->info
))
11739 bfd_vma dot
= (rel
->r_offset
11740 + o
->output_offset
+ o
->output_section
->vma
);
11742 printf ("Encountered a complex symbol!");
11743 printf (" (input_bfd %s, section %s, reloc %ld\n",
11744 bfd_get_filename (input_bfd
), o
->name
,
11745 (long) (rel
- internal_relocs
));
11746 printf (" symbol: idx %8.8lx, name %s\n",
11747 r_symndx
, sym_name
);
11748 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11749 (unsigned long) rel
->r_info
,
11750 (unsigned long) rel
->r_offset
);
11752 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11753 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11756 /* Symbol evaluated OK. Update to absolute value. */
11757 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11762 if (action_discarded
!= -1 && ps
!= NULL
)
11764 /* Complain if the definition comes from a
11765 discarded section. */
11766 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11768 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11769 if (action_discarded
& COMPLAIN
)
11770 (*flinfo
->info
->callbacks
->einfo
)
11771 /* xgettext:c-format */
11772 (_("%X`%s' referenced in section `%pA' of %pB: "
11773 "defined in discarded section `%pA' of %pB\n"),
11774 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11776 /* Try to do the best we can to support buggy old
11777 versions of gcc. Pretend that the symbol is
11778 really defined in the kept linkonce section.
11779 FIXME: This is quite broken. Modifying the
11780 symbol here means we will be changing all later
11781 uses of the symbol, not just in this section. */
11782 if (action_discarded
& PRETEND
)
11786 kept
= _bfd_elf_check_kept_section (sec
,
11798 /* Relocate the section by invoking a back end routine.
11800 The back end routine is responsible for adjusting the
11801 section contents as necessary, and (if using Rela relocs
11802 and generating a relocatable output file) adjusting the
11803 reloc addend as necessary.
11805 The back end routine does not have to worry about setting
11806 the reloc address or the reloc symbol index.
11808 The back end routine is given a pointer to the swapped in
11809 internal symbols, and can access the hash table entries
11810 for the external symbols via elf_sym_hashes (input_bfd).
11812 When generating relocatable output, the back end routine
11813 must handle STB_LOCAL/STT_SECTION symbols specially. The
11814 output symbol is going to be a section symbol
11815 corresponding to the output section, which will require
11816 the addend to be adjusted. */
11818 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11819 input_bfd
, o
, contents
,
11827 || bfd_link_relocatable (flinfo
->info
)
11828 || flinfo
->info
->emitrelocations
)
11830 Elf_Internal_Rela
*irela
;
11831 Elf_Internal_Rela
*irelaend
, *irelamid
;
11832 bfd_vma last_offset
;
11833 struct elf_link_hash_entry
**rel_hash
;
11834 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11835 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11836 unsigned int next_erel
;
11838 struct bfd_elf_section_data
*esdi
, *esdo
;
11840 esdi
= elf_section_data (o
);
11841 esdo
= elf_section_data (o
->output_section
);
11842 rela_normal
= false;
11844 /* Adjust the reloc addresses and symbol indices. */
11846 irela
= internal_relocs
;
11847 irelaend
= irela
+ o
->reloc_count
;
11848 rel_hash
= PTR_ADD (esdo
->rel
.hashes
, esdo
->rel
.count
);
11849 /* We start processing the REL relocs, if any. When we reach
11850 IRELAMID in the loop, we switch to the RELA relocs. */
11852 if (esdi
->rel
.hdr
!= NULL
)
11853 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11854 * bed
->s
->int_rels_per_ext_rel
);
11855 rel_hash_list
= rel_hash
;
11856 rela_hash_list
= NULL
;
11857 last_offset
= o
->output_offset
;
11858 if (!bfd_link_relocatable (flinfo
->info
))
11859 last_offset
+= o
->output_section
->vma
;
11860 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11862 unsigned long r_symndx
;
11864 Elf_Internal_Sym sym
;
11866 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11872 if (irela
== irelamid
)
11874 rel_hash
= PTR_ADD (esdo
->rela
.hashes
, esdo
->rela
.count
);
11875 rela_hash_list
= rel_hash
;
11876 if (bed
->is_rela_normal
!= NULL
)
11877 rela_normal
= bed
->is_rela_normal (irela
);
11879 rela_normal
= bed
->rela_normal
;
11882 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11885 if (irela
->r_offset
>= (bfd_vma
) -2)
11887 /* This is a reloc for a deleted entry or somesuch.
11888 Turn it into an R_*_NONE reloc, at the same
11889 offset as the last reloc. elf_eh_frame.c and
11890 bfd_elf_discard_info rely on reloc offsets
11892 irela
->r_offset
= last_offset
;
11894 irela
->r_addend
= 0;
11898 irela
->r_offset
+= o
->output_offset
;
11900 /* Relocs in an executable have to be virtual addresses. */
11901 if (!bfd_link_relocatable (flinfo
->info
))
11902 irela
->r_offset
+= o
->output_section
->vma
;
11904 last_offset
= irela
->r_offset
;
11906 r_symndx
= irela
->r_info
>> r_sym_shift
;
11907 if (r_symndx
== STN_UNDEF
)
11910 if (r_symndx
>= locsymcount
11911 || (elf_bad_symtab (input_bfd
)
11912 && flinfo
->sections
[r_symndx
] == NULL
))
11914 struct elf_link_hash_entry
*rh
;
11915 unsigned long indx
;
11917 /* This is a reloc against a global symbol. We
11918 have not yet output all the local symbols, so
11919 we do not know the symbol index of any global
11920 symbol. We set the rel_hash entry for this
11921 reloc to point to the global hash table entry
11922 for this symbol. The symbol index is then
11923 set at the end of bfd_elf_final_link. */
11924 indx
= r_symndx
- extsymoff
;
11925 rh
= elf_sym_hashes (input_bfd
)[indx
];
11926 while (rh
->root
.type
== bfd_link_hash_indirect
11927 || rh
->root
.type
== bfd_link_hash_warning
)
11928 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11930 /* Setting the index to -2 tells
11931 elf_link_output_extsym that this symbol is
11932 used by a reloc. */
11933 BFD_ASSERT (rh
->indx
< 0);
11940 /* This is a reloc against a local symbol. */
11943 sym
= isymbuf
[r_symndx
];
11944 sec
= flinfo
->sections
[r_symndx
];
11945 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11947 /* I suppose the backend ought to fill in the
11948 section of any STT_SECTION symbol against a
11949 processor specific section. */
11950 r_symndx
= STN_UNDEF
;
11951 if (bfd_is_abs_section (sec
))
11953 else if (sec
== NULL
|| sec
->owner
== NULL
)
11955 bfd_set_error (bfd_error_bad_value
);
11960 asection
*osec
= sec
->output_section
;
11962 /* If we have discarded a section, the output
11963 section will be the absolute section. In
11964 case of discarded SEC_MERGE sections, use
11965 the kept section. relocate_section should
11966 have already handled discarded linkonce
11968 if (bfd_is_abs_section (osec
)
11969 && sec
->kept_section
!= NULL
11970 && sec
->kept_section
->output_section
!= NULL
)
11972 osec
= sec
->kept_section
->output_section
;
11973 irela
->r_addend
-= osec
->vma
;
11976 if (!bfd_is_abs_section (osec
))
11978 r_symndx
= osec
->target_index
;
11979 if (r_symndx
== STN_UNDEF
)
11981 irela
->r_addend
+= osec
->vma
;
11982 osec
= _bfd_nearby_section (output_bfd
, osec
,
11984 irela
->r_addend
-= osec
->vma
;
11985 r_symndx
= osec
->target_index
;
11990 /* Adjust the addend according to where the
11991 section winds up in the output section. */
11993 irela
->r_addend
+= sec
->output_offset
;
11997 if (flinfo
->indices
[r_symndx
] == -1)
11999 unsigned long shlink
;
12004 if (flinfo
->info
->strip
== strip_all
)
12006 /* You can't do ld -r -s. */
12007 bfd_set_error (bfd_error_invalid_operation
);
12011 /* This symbol was skipped earlier, but
12012 since it is needed by a reloc, we
12013 must output it now. */
12014 shlink
= symtab_hdr
->sh_link
;
12015 name
= (bfd_elf_string_from_elf_section
12016 (input_bfd
, shlink
, sym
.st_name
));
12020 osec
= sec
->output_section
;
12022 _bfd_elf_section_from_bfd_section (output_bfd
,
12024 if (sym
.st_shndx
== SHN_BAD
)
12027 sym
.st_value
+= sec
->output_offset
;
12028 if (!bfd_link_relocatable (flinfo
->info
))
12030 sym
.st_value
+= osec
->vma
;
12031 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
12033 struct elf_link_hash_table
*htab
12034 = elf_hash_table (flinfo
->info
);
12036 /* STT_TLS symbols are relative to PT_TLS
12038 if (htab
->tls_sec
!= NULL
)
12039 sym
.st_value
-= htab
->tls_sec
->vma
;
12042 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
12047 indx
= bfd_get_symcount (output_bfd
);
12048 ret
= elf_link_output_symstrtab (flinfo
, name
,
12054 flinfo
->indices
[r_symndx
] = indx
;
12059 r_symndx
= flinfo
->indices
[r_symndx
];
12062 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
12063 | (irela
->r_info
& r_type_mask
));
12066 /* Swap out the relocs. */
12067 input_rel_hdr
= esdi
->rel
.hdr
;
12068 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
12070 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
12075 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
12076 * bed
->s
->int_rels_per_ext_rel
);
12077 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
12080 input_rela_hdr
= esdi
->rela
.hdr
;
12081 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
12083 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
12092 /* Write out the modified section contents. */
12093 if (bed
->elf_backend_write_section
12094 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
12097 /* Section written out. */
12099 else switch (o
->sec_info_type
)
12101 case SEC_INFO_TYPE_STABS
:
12102 if (! (_bfd_write_section_stabs
12104 &elf_hash_table (flinfo
->info
)->stab_info
,
12105 o
, &elf_section_data (o
)->sec_info
, contents
)))
12108 case SEC_INFO_TYPE_MERGE
:
12109 if (! _bfd_write_merged_section (output_bfd
, o
,
12110 elf_section_data (o
)->sec_info
))
12113 case SEC_INFO_TYPE_EH_FRAME
:
12115 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
12120 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
12122 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
12128 case SEC_INFO_TYPE_SFRAME
:
12130 /* Merge .sframe sections into the ctf frame encoder
12131 context of the output_bfd's section. The final .sframe
12132 output section will be written out later. */
12133 if (!_bfd_elf_merge_section_sframe (output_bfd
, flinfo
->info
,
12140 if (! (o
->flags
& SEC_EXCLUDE
))
12142 file_ptr offset
= (file_ptr
) o
->output_offset
;
12143 bfd_size_type todo
= o
->size
;
12145 offset
*= bfd_octets_per_byte (output_bfd
, o
);
12147 if ((o
->flags
& SEC_ELF_REVERSE_COPY
)
12148 && o
->size
> address_size
)
12150 /* Reverse-copy input section to output. */
12152 if ((o
->size
& (address_size
- 1)) != 0
12153 || (o
->reloc_count
!= 0
12154 && (o
->size
* bed
->s
->int_rels_per_ext_rel
12155 != o
->reloc_count
* address_size
)))
12158 /* xgettext:c-format */
12159 (_("error: %pB: size of section %pA is not "
12160 "multiple of address size"),
12162 bfd_set_error (bfd_error_bad_value
);
12168 todo
-= address_size
;
12169 if (! bfd_set_section_contents (output_bfd
,
12177 offset
+= address_size
;
12181 else if (! bfd_set_section_contents (output_bfd
,
12191 /* Munmap the section contents for each input section. */
12192 _bfd_elf_link_munmap_section_contents (o
);
12198 /* Generate a reloc when linking an ELF file. This is a reloc
12199 requested by the linker, and does not come from any input file. This
12200 is used to build constructor and destructor tables when linking
12204 elf_reloc_link_order (bfd
*output_bfd
,
12205 struct bfd_link_info
*info
,
12206 asection
*output_section
,
12207 struct bfd_link_order
*link_order
)
12209 reloc_howto_type
*howto
;
12213 struct bfd_elf_section_reloc_data
*reldata
;
12214 struct elf_link_hash_entry
**rel_hash_ptr
;
12215 Elf_Internal_Shdr
*rel_hdr
;
12216 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
12217 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
12220 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
12222 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
12225 bfd_set_error (bfd_error_bad_value
);
12229 addend
= link_order
->u
.reloc
.p
->addend
;
12232 reldata
= &esdo
->rel
;
12233 else if (esdo
->rela
.hdr
)
12234 reldata
= &esdo
->rela
;
12241 /* Figure out the symbol index. */
12242 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
12243 if (link_order
->type
== bfd_section_reloc_link_order
)
12245 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
12246 BFD_ASSERT (indx
!= 0);
12247 *rel_hash_ptr
= NULL
;
12251 struct elf_link_hash_entry
*h
;
12253 /* Treat a reloc against a defined symbol as though it were
12254 actually against the section. */
12255 h
= ((struct elf_link_hash_entry
*)
12256 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
12257 link_order
->u
.reloc
.p
->u
.name
,
12258 false, false, true));
12260 && (h
->root
.type
== bfd_link_hash_defined
12261 || h
->root
.type
== bfd_link_hash_defweak
))
12265 section
= h
->root
.u
.def
.section
;
12266 indx
= section
->output_section
->target_index
;
12267 *rel_hash_ptr
= NULL
;
12268 /* It seems that we ought to add the symbol value to the
12269 addend here, but in practice it has already been added
12270 because it was passed to constructor_callback. */
12271 addend
+= section
->output_section
->vma
+ section
->output_offset
;
12273 else if (h
!= NULL
)
12275 /* Setting the index to -2 tells elf_link_output_extsym that
12276 this symbol is used by a reloc. */
12283 (*info
->callbacks
->unattached_reloc
)
12284 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
12289 /* If this is an inplace reloc, we must write the addend into the
12291 if (howto
->partial_inplace
&& addend
!= 0)
12293 bfd_size_type size
;
12294 bfd_reloc_status_type rstat
;
12297 const char *sym_name
;
12298 bfd_size_type octets
;
12300 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
12301 buf
= (bfd_byte
*) bfd_zmalloc (size
);
12302 if (buf
== NULL
&& size
!= 0)
12304 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
12311 case bfd_reloc_outofrange
:
12314 case bfd_reloc_overflow
:
12315 if (link_order
->type
== bfd_section_reloc_link_order
)
12316 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
12318 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
12319 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
12320 howto
->name
, addend
, NULL
, NULL
,
12325 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
12327 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
12334 /* The address of a reloc is relative to the section in a
12335 relocatable file, and is a virtual address in an executable
12337 offset
= link_order
->offset
;
12338 if (! bfd_link_relocatable (info
))
12339 offset
+= output_section
->vma
;
12341 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
12343 irel
[i
].r_offset
= offset
;
12344 irel
[i
].r_info
= 0;
12345 irel
[i
].r_addend
= 0;
12347 if (bed
->s
->arch_size
== 32)
12348 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
12350 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
12352 rel_hdr
= reldata
->hdr
;
12353 erel
= rel_hdr
->contents
;
12354 if (rel_hdr
->sh_type
== SHT_REL
)
12356 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
12357 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
12361 irel
[0].r_addend
= addend
;
12362 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
12363 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
12371 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12372 Returns TRUE upon success, FALSE otherwise. */
12375 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
12379 const struct elf_backend_data
*bed
;
12381 enum bfd_architecture arch
;
12383 asymbol
**sympp
= NULL
;
12387 elf_symbol_type
*osymbuf
;
12390 implib_bfd
= info
->out_implib_bfd
;
12391 bed
= get_elf_backend_data (abfd
);
12393 if (!bfd_set_format (implib_bfd
, bfd_object
))
12396 /* Use flag from executable but make it a relocatable object. */
12397 flags
= bfd_get_file_flags (abfd
);
12398 flags
&= ~HAS_RELOC
;
12399 if (!bfd_set_start_address (implib_bfd
, 0)
12400 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
12403 /* Copy architecture of output file to import library file. */
12404 arch
= bfd_get_arch (abfd
);
12405 mach
= bfd_get_mach (abfd
);
12406 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
12407 && (abfd
->target_defaulted
12408 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
12411 /* Get symbol table size. */
12412 symsize
= bfd_get_symtab_upper_bound (abfd
);
12416 /* Read in the symbol table. */
12417 sympp
= (asymbol
**) bfd_malloc (symsize
);
12421 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
12425 /* Allow the BFD backend to copy any private header data it
12426 understands from the output BFD to the import library BFD. */
12427 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
12430 /* Filter symbols to appear in the import library. */
12431 if (bed
->elf_backend_filter_implib_symbols
)
12432 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
12435 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
12438 bfd_set_error (bfd_error_no_symbols
);
12439 _bfd_error_handler (_("%pB: no symbol found for import library"),
12445 /* Make symbols absolute. */
12446 amt
= symcount
* sizeof (*osymbuf
);
12447 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
12448 if (osymbuf
== NULL
)
12451 for (src_count
= 0; src_count
< symcount
; src_count
++)
12453 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
12454 sizeof (*osymbuf
));
12455 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
12456 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
12457 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
12458 osymbuf
[src_count
].internal_elf_sym
.st_value
=
12459 osymbuf
[src_count
].symbol
.value
;
12460 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
12463 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
12465 /* Allow the BFD backend to copy any private data it understands
12466 from the output BFD to the import library BFD. This is done last
12467 to permit the routine to look at the filtered symbol table. */
12468 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
12471 if (!bfd_close (implib_bfd
))
12482 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
12486 if (flinfo
->symstrtab
!= NULL
)
12487 _bfd_elf_strtab_free (flinfo
->symstrtab
);
12488 free (flinfo
->contents
);
12489 free (flinfo
->external_relocs
);
12490 free (flinfo
->internal_relocs
);
12491 free (flinfo
->external_syms
);
12492 free (flinfo
->locsym_shndx
);
12493 free (flinfo
->internal_syms
);
12494 free (flinfo
->indices
);
12495 free (flinfo
->sections
);
12496 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12497 free (flinfo
->symshndxbuf
);
12498 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12500 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12501 free (esdo
->rel
.hashes
);
12502 free (esdo
->rela
.hashes
);
12506 /* Do the final step of an ELF link. */
12509 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12514 struct elf_final_link_info flinfo
;
12516 struct bfd_link_order
*p
;
12518 bfd_size_type max_contents_size
;
12519 bfd_size_type max_external_reloc_size
;
12520 bfd_size_type max_internal_reloc_count
;
12521 bfd_size_type max_sym_count
;
12522 bfd_size_type max_sym_shndx_count
;
12523 Elf_Internal_Sym elfsym
;
12525 Elf_Internal_Shdr
*symtab_hdr
;
12526 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12527 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12528 struct elf_outext_info eoinfo
;
12530 size_t relativecount
;
12531 size_t relr_entsize
;
12532 asection
*reldyn
= 0;
12534 asection
*attr_section
= NULL
;
12535 bfd_vma attr_size
= 0;
12536 const char *std_attrs_section
;
12537 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12538 bool sections_removed
;
12541 if (!is_elf_hash_table (&htab
->root
))
12544 if (bfd_link_pic (info
))
12545 abfd
->flags
|= DYNAMIC
;
12547 dynamic
= htab
->dynamic_sections_created
;
12548 dynobj
= htab
->dynobj
;
12550 emit_relocs
= (bfd_link_relocatable (info
)
12551 || info
->emitrelocations
);
12553 memset (&flinfo
, 0, sizeof (flinfo
));
12554 flinfo
.info
= info
;
12555 flinfo
.output_bfd
= abfd
;
12556 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12557 if (flinfo
.symstrtab
== NULL
)
12562 flinfo
.hash_sec
= NULL
;
12563 flinfo
.symver_sec
= NULL
;
12567 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12568 /* Note that dynsym_sec can be NULL (on VMS). */
12569 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12570 /* Note that it is OK if symver_sec is NULL. */
12573 if (info
->unique_symbol
12574 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12575 local_hash_newfunc
,
12576 sizeof (struct local_hash_entry
)))
12579 /* The object attributes have been merged. Remove the input
12580 sections from the link, and set the contents of the output
12582 sections_removed
= false;
12583 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12584 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12586 bool remove_section
= false;
12588 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12589 || strcmp (o
->name
, ".gnu.attributes") == 0)
12591 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12593 asection
*input_section
;
12595 if (p
->type
!= bfd_indirect_link_order
)
12597 input_section
= p
->u
.indirect
.section
;
12598 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12599 elf_link_input_bfd ignores this section. */
12600 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12603 attr_size
= bfd_elf_obj_attr_size (abfd
);
12604 bfd_set_section_size (o
, attr_size
);
12605 /* Skip this section later on. */
12606 o
->map_head
.link_order
= NULL
;
12610 remove_section
= true;
12612 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12614 /* Remove empty group section from linker output. */
12615 remove_section
= true;
12617 if (remove_section
)
12619 o
->flags
|= SEC_EXCLUDE
;
12620 bfd_section_list_remove (abfd
, o
);
12621 abfd
->section_count
--;
12622 sections_removed
= true;
12625 if (sections_removed
)
12626 _bfd_fix_excluded_sec_syms (abfd
, info
);
12628 /* Count up the number of relocations we will output for each output
12629 section, so that we know the sizes of the reloc sections. We
12630 also figure out some maximum sizes. */
12634 /* Mmap is used only if section size >= the minimum mmap section
12635 size. The initial max_contents_size value covers all sections
12636 smaller than the minimum mmap section size. It may be increased
12637 for compressed or linker created sections or sections whose
12638 rawsize != size. max_external_reloc_size covers all relocation
12639 sections smaller than the minimum mmap section size. */
12640 max_contents_size
= _bfd_minimum_mmap_size
;
12641 max_external_reloc_size
= _bfd_minimum_mmap_size
;
12646 max_contents_size
= 0;
12647 max_external_reloc_size
= 0;
12649 max_internal_reloc_count
= 0;
12651 max_sym_shndx_count
= 0;
12653 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12655 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12656 o
->reloc_count
= 0;
12658 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12660 unsigned int reloc_count
= 0;
12661 unsigned int additional_reloc_count
= 0;
12662 struct bfd_elf_section_data
*esdi
= NULL
;
12664 if (p
->type
== bfd_section_reloc_link_order
12665 || p
->type
== bfd_symbol_reloc_link_order
)
12667 else if (p
->type
== bfd_indirect_link_order
)
12671 sec
= p
->u
.indirect
.section
;
12673 /* Mark all sections which are to be included in the
12674 link. This will normally be every section. We need
12675 to do this so that we can identify any sections which
12676 the linker has decided to not include. */
12677 sec
->linker_mark
= true;
12679 if (sec
->flags
& SEC_MERGE
)
12683 /* Mmap is used only on non-compressed, non-linker created
12684 sections whose rawsize == size. */
12686 || sec
->compress_status
!= COMPRESS_SECTION_NONE
12687 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
12688 || sec
->rawsize
!= sec
->size
)
12691 if (sec
->rawsize
> max_contents_size
)
12692 max_contents_size
= sec
->rawsize
;
12693 if (sec
->size
> max_contents_size
)
12694 max_contents_size
= sec
->size
;
12697 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12698 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12702 /* We are interested in just local symbols, not all
12704 if (elf_bad_symtab (sec
->owner
))
12705 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12706 / bed
->s
->sizeof_sym
);
12708 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12710 if (sym_count
> max_sym_count
)
12711 max_sym_count
= sym_count
;
12713 if (sym_count
> max_sym_shndx_count
12714 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12715 max_sym_shndx_count
= sym_count
;
12717 esdi
= elf_section_data (sec
);
12719 if (esdi
->this_hdr
.sh_type
== SHT_REL
12720 || esdi
->this_hdr
.sh_type
== SHT_RELA
)
12721 /* Some backends use reloc_count in relocation sections
12722 to count particular types of relocs. Of course,
12723 reloc sections themselves can't have relocations. */
12725 else if (emit_relocs
)
12727 reloc_count
= sec
->reloc_count
;
12728 if (bed
->elf_backend_count_additional_relocs
)
12731 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12732 additional_reloc_count
+= c
;
12735 else if (bed
->elf_backend_count_relocs
)
12736 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12738 if ((sec
->flags
& SEC_RELOC
) != 0)
12741 if (!bed
->use_mmap
)
12744 size_t ext_size
= 0;
12746 if (esdi
->rel
.hdr
!= NULL
)
12747 ext_size
= esdi
->rel
.hdr
->sh_size
;
12748 if (esdi
->rela
.hdr
!= NULL
)
12749 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12751 if (ext_size
> max_external_reloc_size
)
12752 max_external_reloc_size
= ext_size
;
12754 if (sec
->reloc_count
> max_internal_reloc_count
)
12755 max_internal_reloc_count
= sec
->reloc_count
;
12760 if (reloc_count
== 0)
12763 reloc_count
+= additional_reloc_count
;
12764 o
->reloc_count
+= reloc_count
;
12766 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12770 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12771 esdo
->rel
.count
+= additional_reloc_count
;
12773 if (esdi
->rela
.hdr
)
12775 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12776 esdo
->rela
.count
+= additional_reloc_count
;
12782 esdo
->rela
.count
+= reloc_count
;
12784 esdo
->rel
.count
+= reloc_count
;
12788 if (o
->reloc_count
> 0)
12789 o
->flags
|= SEC_RELOC
;
12792 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12793 set it (this is probably a bug) and if it is set
12794 assign_section_numbers will create a reloc section. */
12795 o
->flags
&=~ SEC_RELOC
;
12798 /* If the SEC_ALLOC flag is not set, force the section VMA to
12799 zero. This is done in elf_fake_sections as well, but forcing
12800 the VMA to 0 here will ensure that relocs against these
12801 sections are handled correctly. */
12802 if ((o
->flags
& SEC_ALLOC
) == 0
12803 && ! o
->user_set_vma
)
12807 if (! bfd_link_relocatable (info
) && merged
)
12808 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12810 /* Figure out the file positions for everything but the symbol table
12811 and the relocs. We set symcount to force assign_section_numbers
12812 to create a symbol table. */
12813 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12814 BFD_ASSERT (! abfd
->output_has_begun
);
12815 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12818 /* Set sizes, and assign file positions for reloc sections. */
12819 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12821 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12822 if ((o
->flags
& SEC_RELOC
) != 0)
12825 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12829 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12833 /* _bfd_elf_compute_section_file_positions makes temporary use
12834 of target_index. Reset it. */
12835 o
->target_index
= 0;
12837 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12838 to count upwards while actually outputting the relocations. */
12839 esdo
->rel
.count
= 0;
12840 esdo
->rela
.count
= 0;
12842 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12843 && !bfd_section_is_ctf (o
))
12845 /* Cache the section contents so that they can be compressed
12846 later. Use bfd_malloc since it will be freed by
12847 bfd_compress_section_contents. */
12848 unsigned char *contents
= esdo
->this_hdr
.contents
;
12849 if (contents
!= NULL
)
12852 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12853 if (contents
== NULL
)
12855 esdo
->this_hdr
.contents
= contents
;
12859 /* We have now assigned file positions for all the sections except .symtab,
12860 .strtab, and non-loaded reloc and compressed debugging sections. We start
12861 the .symtab section at the current file position, and write directly to it.
12862 We build the .strtab section in memory. */
12863 abfd
->symcount
= 0;
12864 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12865 /* sh_name is set in prep_headers. */
12866 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12867 /* sh_flags, sh_addr and sh_size all start off zero. */
12868 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12869 /* sh_link is set in assign_section_numbers. */
12870 /* sh_info is set below. */
12871 /* sh_offset is set just below. */
12872 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12874 if (max_sym_count
< 20)
12875 max_sym_count
= 20;
12876 htab
->strtabsize
= max_sym_count
;
12877 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12878 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12879 if (htab
->strtab
== NULL
)
12881 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12883 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12884 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12886 if (info
->strip
!= strip_all
|| emit_relocs
)
12888 file_ptr off
= elf_next_file_pos (abfd
);
12890 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true);
12892 /* Note that at this point elf_next_file_pos (abfd) is
12893 incorrect. We do not yet know the size of the .symtab section.
12894 We correct next_file_pos below, after we do know the size. */
12896 /* Start writing out the symbol table. The first symbol is always a
12898 elfsym
.st_value
= 0;
12899 elfsym
.st_size
= 0;
12900 elfsym
.st_info
= 0;
12901 elfsym
.st_other
= 0;
12902 elfsym
.st_shndx
= SHN_UNDEF
;
12903 elfsym
.st_target_internal
= 0;
12904 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12905 bfd_und_section_ptr
, NULL
) != 1)
12908 /* Output a symbol for each section if asked or they are used for
12909 relocs. These symbols usually have no names. We store the
12910 index of each one in the index field of the section, so that
12911 we can find it again when outputting relocs. */
12913 if (bfd_keep_unused_section_symbols (abfd
) || emit_relocs
)
12915 bool name_local_sections
12916 = (bed
->elf_backend_name_local_section_symbols
12917 && bed
->elf_backend_name_local_section_symbols (abfd
));
12918 const char *name
= NULL
;
12920 elfsym
.st_size
= 0;
12921 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12922 elfsym
.st_other
= 0;
12923 elfsym
.st_value
= 0;
12924 elfsym
.st_target_internal
= 0;
12925 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12927 o
= bfd_section_from_elf_index (abfd
, i
);
12930 o
->target_index
= bfd_get_symcount (abfd
);
12931 elfsym
.st_shndx
= i
;
12932 if (!bfd_link_relocatable (info
))
12933 elfsym
.st_value
= o
->vma
;
12934 if (name_local_sections
)
12936 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12944 /* On some targets like Irix 5 the symbol split between local and global
12945 ones recorded in the sh_info field needs to be done between section
12946 and all other symbols. */
12947 if (bed
->elf_backend_elfsym_local_is_section
12948 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12949 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12951 /* Allocate some memory to hold information read in from the input
12953 if (max_contents_size
!= 0)
12955 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12956 if (flinfo
.contents
== NULL
)
12960 if (max_external_reloc_size
!= 0)
12962 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
12963 if (flinfo
.external_relocs
== NULL
)
12967 if (max_internal_reloc_count
!= 0)
12969 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
12970 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
12971 if (flinfo
.internal_relocs
== NULL
)
12975 if (max_sym_count
!= 0)
12977 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
12978 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
12979 if (flinfo
.external_syms
== NULL
)
12982 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
12983 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
12984 if (flinfo
.internal_syms
== NULL
)
12987 amt
= max_sym_count
* sizeof (long);
12988 flinfo
.indices
= (long int *) bfd_malloc (amt
);
12989 if (flinfo
.indices
== NULL
)
12992 amt
= max_sym_count
* sizeof (asection
*);
12993 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
12994 if (flinfo
.sections
== NULL
)
12998 if (max_sym_shndx_count
!= 0)
13000 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
13001 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
13002 if (flinfo
.locsym_shndx
== NULL
)
13008 bfd_vma base
, end
= 0; /* Both bytes. */
13011 for (sec
= htab
->tls_sec
;
13012 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
13015 bfd_size_type size
= sec
->size
;
13016 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
13019 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
13021 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
13024 size
= ord
->offset
* opb
+ ord
->size
;
13026 end
= sec
->vma
+ size
/ opb
;
13028 base
= htab
->tls_sec
->vma
;
13029 /* Only align end of TLS section if static TLS doesn't have special
13030 alignment requirements. */
13031 if (bed
->static_tls_alignment
== 1)
13032 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
13033 htab
->tls_size
= end
- base
;
13036 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
13039 /* Finish relative relocations here after regular symbol processing
13040 is finished if DT_RELR is enabled. */
13041 if (info
->enable_dt_relr
13042 && bed
->finish_relative_relocs
13043 && !bed
->finish_relative_relocs (info
))
13044 info
->callbacks
->einfo
13045 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd
);
13047 /* Since ELF permits relocations to be against local symbols, we
13048 must have the local symbols available when we do the relocations.
13049 Since we would rather only read the local symbols once, and we
13050 would rather not keep them in memory, we handle all the
13051 relocations for a single input file at the same time.
13053 Unfortunately, there is no way to know the total number of local
13054 symbols until we have seen all of them, and the local symbol
13055 indices precede the global symbol indices. This means that when
13056 we are generating relocatable output, and we see a reloc against
13057 a global symbol, we can not know the symbol index until we have
13058 finished examining all the local symbols to see which ones we are
13059 going to output. To deal with this, we keep the relocations in
13060 memory, and don't output them until the end of the link. This is
13061 an unfortunate waste of memory, but I don't see a good way around
13062 it. Fortunately, it only happens when performing a relocatable
13063 link, which is not the common case. FIXME: If keep_memory is set
13064 we could write the relocs out and then read them again; I don't
13065 know how bad the memory loss will be. */
13067 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13068 sub
->output_has_begun
= false;
13069 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
13071 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
13073 if (p
->type
== bfd_indirect_link_order
13074 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
13075 == bfd_target_elf_flavour
)
13076 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
13078 if (! sub
->output_has_begun
)
13080 if (! elf_link_input_bfd (&flinfo
, sub
))
13082 sub
->output_has_begun
= true;
13085 else if (p
->type
== bfd_section_reloc_link_order
13086 || p
->type
== bfd_symbol_reloc_link_order
)
13088 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
13093 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
13095 if (p
->type
== bfd_indirect_link_order
13096 && (bfd_get_flavour (sub
)
13097 == bfd_target_elf_flavour
)
13098 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
13099 != bed
->s
->elfclass
))
13101 const char *iclass
, *oclass
;
13103 switch (bed
->s
->elfclass
)
13105 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
13106 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
13107 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
13111 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
13113 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
13114 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
13115 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
13119 bfd_set_error (bfd_error_wrong_format
);
13121 /* xgettext:c-format */
13122 (_("%pB: file class %s incompatible with %s"),
13123 sub
, iclass
, oclass
);
13132 /* Free symbol buffer if needed. */
13133 if (!info
->reduce_memory_overheads
)
13135 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13136 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
13138 free (elf_tdata (sub
)->symbuf
);
13139 elf_tdata (sub
)->symbuf
= NULL
;
13145 /* Output any global symbols that got converted to local in a
13146 version script or due to symbol visibility. We do this in a
13147 separate step since ELF requires all local symbols to appear
13148 prior to any global symbols. FIXME: We should only do this if
13149 some global symbols were, in fact, converted to become local.
13150 FIXME: Will this work correctly with the Irix 5 linker? */
13151 eoinfo
.failed
= false;
13152 eoinfo
.flinfo
= &flinfo
;
13153 eoinfo
.localsyms
= true;
13154 eoinfo
.file_sym_done
= false;
13155 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
13159 goto return_local_hash_table
;
13162 /* If backend needs to output some local symbols not present in the hash
13163 table, do it now. */
13164 if (bed
->elf_backend_output_arch_local_syms
)
13166 if (! ((*bed
->elf_backend_output_arch_local_syms
)
13167 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
13170 goto return_local_hash_table
;
13174 /* That wrote out all the local symbols. Finish up the symbol table
13175 with the global symbols. Even if we want to strip everything we
13176 can, we still need to deal with those global symbols that got
13177 converted to local in a version script. */
13179 /* The sh_info field records the index of the first non local symbol. */
13180 if (!symtab_hdr
->sh_info
)
13181 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
13184 && htab
->dynsym
!= NULL
13185 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
13187 Elf_Internal_Sym sym
;
13188 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
13190 o
= htab
->dynsym
->output_section
;
13191 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
13193 /* Write out the section symbols for the output sections. */
13194 if (bfd_link_pic (info
)
13195 || htab
->is_relocatable_executable
)
13201 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
13203 sym
.st_target_internal
= 0;
13205 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
13211 dynindx
= elf_section_data (s
)->dynindx
;
13214 indx
= elf_section_data (s
)->this_idx
;
13215 BFD_ASSERT (indx
> 0);
13216 sym
.st_shndx
= indx
;
13217 if (! check_dynsym (abfd
, &sym
))
13220 goto return_local_hash_table
;
13222 sym
.st_value
= s
->vma
;
13223 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
13225 /* Inform the linker of the addition of this symbol. */
13227 if (info
->callbacks
->ctf_new_dynsym
)
13228 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
13230 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
13234 /* Write out the local dynsyms. */
13235 if (htab
->dynlocal
)
13237 struct elf_link_local_dynamic_entry
*e
;
13238 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
13243 /* Copy the internal symbol and turn off visibility.
13244 Note that we saved a word of storage and overwrote
13245 the original st_name with the dynstr_index. */
13247 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
13248 sym
.st_shndx
= SHN_UNDEF
;
13250 s
= bfd_section_from_elf_index (e
->input_bfd
,
13253 && s
->output_section
!= NULL
13254 && elf_section_data (s
->output_section
) != NULL
)
13257 elf_section_data (s
->output_section
)->this_idx
;
13258 if (! check_dynsym (abfd
, &sym
))
13261 goto return_local_hash_table
;
13263 sym
.st_value
= (s
->output_section
->vma
13265 + e
->isym
.st_value
);
13268 /* Inform the linker of the addition of this symbol. */
13270 if (info
->callbacks
->ctf_new_dynsym
)
13271 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
13273 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
13274 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
13279 /* We get the global symbols from the hash table. */
13280 eoinfo
.failed
= false;
13281 eoinfo
.localsyms
= false;
13282 eoinfo
.flinfo
= &flinfo
;
13283 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
13287 goto return_local_hash_table
;
13290 /* If backend needs to output some symbols not present in the hash
13291 table, do it now. */
13292 if (bed
->elf_backend_output_arch_syms
13293 && (info
->strip
!= strip_all
|| emit_relocs
))
13295 if (! ((*bed
->elf_backend_output_arch_syms
)
13296 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
13299 goto return_local_hash_table
;
13303 /* Finalize the .strtab section. */
13304 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
13306 /* Swap out the .strtab section. */
13307 if (!elf_link_swap_symbols_out (&flinfo
))
13310 goto return_local_hash_table
;
13313 /* Now we know the size of the symtab section. */
13314 if (bfd_get_symcount (abfd
) > 0)
13316 /* Finish up and write out the symbol string table (.strtab)
13318 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
13319 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
13321 if (elf_symtab_shndx_list (abfd
))
13323 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
13325 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
13327 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
13328 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
13329 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
13330 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
13331 symtab_shndx_hdr
->sh_size
= amt
;
13333 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
13336 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
13337 || (bfd_write (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
13340 goto return_local_hash_table
;
13345 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
13346 /* sh_name was set in prep_headers. */
13347 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
13348 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
13349 symstrtab_hdr
->sh_addr
= 0;
13350 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
13351 symstrtab_hdr
->sh_entsize
= 0;
13352 symstrtab_hdr
->sh_link
= 0;
13353 symstrtab_hdr
->sh_info
= 0;
13354 /* sh_offset is set just below. */
13355 symstrtab_hdr
->sh_addralign
= 1;
13357 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
13359 elf_next_file_pos (abfd
) = off
;
13361 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
13362 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
13365 goto return_local_hash_table
;
13369 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
13371 _bfd_error_handler (_("%pB: failed to generate import library"),
13372 info
->out_implib_bfd
);
13374 goto return_local_hash_table
;
13377 /* Adjust the relocs to have the correct symbol indices. */
13378 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
13380 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
13383 if ((o
->flags
& SEC_RELOC
) == 0)
13386 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
13387 if (esdo
->rel
.hdr
!= NULL
13388 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
13391 goto return_local_hash_table
;
13393 if (esdo
->rela
.hdr
!= NULL
13394 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
13397 goto return_local_hash_table
;
13400 /* Set the reloc_count field to 0 to prevent write_relocs from
13401 trying to swap the relocs out itself. */
13402 o
->reloc_count
= 0;
13406 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
13407 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
13410 if (htab
->srelrdyn
!= NULL
13411 && htab
->srelrdyn
->output_section
!= NULL
13412 && htab
->srelrdyn
->size
!= 0)
13414 asection
*s
= htab
->srelrdyn
->output_section
;
13415 relr_entsize
= elf_section_data (s
)->this_hdr
.sh_entsize
;
13416 if (relr_entsize
== 0)
13418 relr_entsize
= bed
->s
->arch_size
/ 8;
13419 elf_section_data (s
)->this_hdr
.sh_entsize
= relr_entsize
;
13423 /* If we are linking against a dynamic object, or generating a
13424 shared library, finish up the dynamic linking information. */
13427 bfd_byte
*dyncon
, *dynconend
;
13429 /* Fix up .dynamic entries. */
13431 BFD_ASSERT (o
!= NULL
);
13433 dyncon
= o
->contents
;
13434 dynconend
= PTR_ADD (o
->contents
, o
->size
);
13435 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13437 Elf_Internal_Dyn dyn
;
13440 bfd_size_type sh_size
;
13443 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13450 if (relativecount
!= 0)
13452 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
13454 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
13455 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
13457 if (dyn
.d_tag
!= DT_NULL
13458 && dynconend
- dyncon
>= bed
->s
->sizeof_dyn
)
13460 dyn
.d_un
.d_val
= relativecount
;
13466 if (relr_entsize
!= 0)
13468 if (dynconend
- dyncon
>= 3 * bed
->s
->sizeof_dyn
)
13470 asection
*s
= htab
->srelrdyn
;
13471 dyn
.d_tag
= DT_RELR
;
13473 = s
->output_section
->vma
+ s
->output_offset
;
13474 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13475 dyncon
+= bed
->s
->sizeof_dyn
;
13477 dyn
.d_tag
= DT_RELRSZ
;
13478 dyn
.d_un
.d_val
= s
->size
;
13479 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13480 dyncon
+= bed
->s
->sizeof_dyn
;
13482 dyn
.d_tag
= DT_RELRENT
;
13483 dyn
.d_un
.d_val
= relr_entsize
;
13492 name
= info
->init_function
;
13495 name
= info
->fini_function
;
13498 struct elf_link_hash_entry
*h
;
13500 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
13502 && (h
->root
.type
== bfd_link_hash_defined
13503 || h
->root
.type
== bfd_link_hash_defweak
))
13505 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
13506 o
= h
->root
.u
.def
.section
;
13507 if (o
->output_section
!= NULL
)
13508 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
13509 + o
->output_offset
);
13512 /* The symbol is imported from another shared
13513 library and does not apply to this one. */
13514 dyn
.d_un
.d_ptr
= 0;
13521 case DT_PREINIT_ARRAYSZ
:
13522 name
= ".preinit_array";
13524 case DT_INIT_ARRAYSZ
:
13525 name
= ".init_array";
13527 case DT_FINI_ARRAYSZ
:
13528 name
= ".fini_array";
13530 o
= bfd_get_section_by_name (abfd
, name
);
13534 (_("could not find section %s"), name
);
13539 (_("warning: %s section has zero size"), name
);
13540 dyn
.d_un
.d_val
= o
->size
;
13543 case DT_PREINIT_ARRAY
:
13544 name
= ".preinit_array";
13546 case DT_INIT_ARRAY
:
13547 name
= ".init_array";
13549 case DT_FINI_ARRAY
:
13550 name
= ".fini_array";
13552 o
= bfd_get_section_by_name (abfd
, name
);
13559 name
= ".gnu.hash";
13568 name
= ".gnu.version_d";
13571 name
= ".gnu.version_r";
13574 name
= ".gnu.version";
13576 o
= bfd_get_linker_section (dynobj
, name
);
13578 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13581 (_("could not find section %s"), name
);
13584 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13587 (_("warning: section '%s' is being made into a note"), name
);
13588 bfd_set_error (bfd_error_nonrepresentable_section
);
13591 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13598 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13604 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13606 Elf_Internal_Shdr
*hdr
;
13608 hdr
= elf_elfsections (abfd
)[i
];
13609 if (hdr
->sh_type
== type
13610 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13612 sh_size
+= hdr
->sh_size
;
13614 || sh_addr
> hdr
->sh_addr
)
13615 sh_addr
= hdr
->sh_addr
;
13619 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13621 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13623 /* Don't count procedure linkage table relocs in the
13624 overall reloc count. */
13625 sh_size
-= htab
->srelplt
->size
;
13627 /* If the size is zero, make the address zero too.
13628 This is to avoid a glibc bug. If the backend
13629 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13630 zero, then we'll put DT_RELA at the end of
13631 DT_JMPREL. glibc will interpret the end of
13632 DT_RELA matching the end of DT_JMPREL as the
13633 case where DT_RELA includes DT_JMPREL, and for
13634 LD_BIND_NOW will decide that processing DT_RELA
13635 will process the PLT relocs too. Net result:
13636 No PLT relocs applied. */
13639 /* If .rela.plt is the first .rela section, exclude
13640 it from DT_RELA. */
13641 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13642 + htab
->srelplt
->output_offset
) * opb
)
13643 sh_addr
+= htab
->srelplt
->size
;
13646 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13647 dyn
.d_un
.d_val
= sh_size
;
13649 dyn
.d_un
.d_ptr
= sh_addr
;
13652 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13656 /* If we have created any dynamic sections, then output them. */
13657 if (dynobj
!= NULL
)
13659 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13662 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13663 if (bfd_link_textrel_check (info
)
13664 && (o
= htab
->dynamic
) != NULL
13667 bfd_byte
*dyncon
, *dynconend
;
13669 dyncon
= o
->contents
;
13670 dynconend
= o
->contents
+ o
->size
;
13671 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13673 Elf_Internal_Dyn dyn
;
13675 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13677 if (dyn
.d_tag
== DT_TEXTREL
)
13679 if (info
->textrel_check
== textrel_check_error
)
13680 info
->callbacks
->einfo
13681 (_("%P%X: read-only segment has dynamic relocations\n"));
13682 else if (bfd_link_dll (info
))
13683 info
->callbacks
->einfo
13684 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13685 else if (bfd_link_pde (info
))
13686 info
->callbacks
->einfo
13687 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13689 info
->callbacks
->einfo
13690 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13696 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13698 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13700 || o
->output_section
== bfd_abs_section_ptr
)
13702 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13704 /* At this point, we are only interested in sections
13705 created by _bfd_elf_link_create_dynamic_sections. */
13708 if (htab
->stab_info
.stabstr
== o
)
13710 if (htab
->eh_info
.hdr_sec
== o
)
13712 if (strcmp (o
->name
, ".dynstr") != 0)
13714 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13715 * bfd_octets_per_byte (abfd
, o
));
13716 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13717 o
->contents
, octets
, o
->size
))
13722 /* The contents of the .dynstr section are actually in a
13726 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13727 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13728 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13734 if (!info
->resolve_section_groups
)
13736 bool failed
= false;
13738 BFD_ASSERT (bfd_link_relocatable (info
));
13739 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13744 /* If we have optimized stabs strings, output them. */
13745 if (htab
->stab_info
.stabstr
!= NULL
)
13747 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13751 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13754 if (! _bfd_elf_write_section_sframe (abfd
, info
))
13757 if (info
->callbacks
->emit_ctf
)
13758 info
->callbacks
->emit_ctf ();
13760 elf_final_link_free (abfd
, &flinfo
);
13764 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13765 if (contents
== NULL
)
13767 /* Bail out and fail. */
13769 goto return_local_hash_table
;
13771 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13772 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13776 return_local_hash_table
:
13777 if (info
->unique_symbol
)
13778 bfd_hash_table_free (&flinfo
.local_hash_table
);
13782 elf_final_link_free (abfd
, &flinfo
);
13784 goto return_local_hash_table
;
13787 /* Initialize COOKIE for input bfd ABFD. */
13790 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13791 struct bfd_link_info
*info
, bfd
*abfd
,
13794 Elf_Internal_Shdr
*symtab_hdr
;
13795 const struct elf_backend_data
*bed
;
13797 bed
= get_elf_backend_data (abfd
);
13798 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13800 cookie
->abfd
= abfd
;
13801 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13802 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13803 if (cookie
->bad_symtab
)
13805 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13806 cookie
->extsymoff
= 0;
13810 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13811 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13814 if (bed
->s
->arch_size
== 32)
13815 cookie
->r_sym_shift
= 8;
13817 cookie
->r_sym_shift
= 32;
13819 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13820 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13822 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13823 cookie
->locsymcount
, 0,
13825 if (cookie
->locsyms
== NULL
)
13827 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13830 if (keep_memory
|| _bfd_elf_link_keep_memory (info
))
13832 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13833 info
->cache_size
+= (cookie
->locsymcount
13834 * sizeof (Elf_External_Sym_Shndx
));
13840 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13843 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13845 Elf_Internal_Shdr
*symtab_hdr
;
13847 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13848 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13849 free (cookie
->locsyms
);
13852 /* Initialize the relocation information in COOKIE for input section SEC
13853 of input bfd ABFD. */
13856 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13857 struct bfd_link_info
*info
, bfd
*abfd
,
13858 asection
*sec
, bool keep_memory
)
13860 if (sec
->reloc_count
== 0)
13862 cookie
->rels
= NULL
;
13863 cookie
->relend
= NULL
;
13867 cookie
->rels
= _bfd_elf_link_info_read_relocs
13868 (abfd
, info
, sec
, NULL
, NULL
,
13869 keep_memory
|| _bfd_elf_link_keep_memory (info
));
13870 if (cookie
->rels
== NULL
)
13872 cookie
->rel
= cookie
->rels
;
13873 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13875 cookie
->rel
= cookie
->rels
;
13879 /* Free the memory allocated by init_reloc_cookie_rels,
13883 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13886 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13887 free (cookie
->rels
);
13890 /* Initialize the whole of COOKIE for input section SEC. */
13893 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13894 struct bfd_link_info
*info
,
13895 asection
*sec
, bool keep_memory
)
13897 if (!init_reloc_cookie (cookie
, info
, sec
->owner
, keep_memory
))
13899 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
,
13905 fini_reloc_cookie (cookie
, sec
->owner
);
13910 /* Free the memory allocated by init_reloc_cookie_for_section,
13914 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13917 fini_reloc_cookie_rels (cookie
, sec
);
13918 fini_reloc_cookie (cookie
, sec
->owner
);
13921 /* Garbage collect unused sections. */
13923 /* Default gc_mark_hook. */
13926 _bfd_elf_gc_mark_hook (asection
*sec
,
13927 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13928 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13929 struct elf_link_hash_entry
*h
,
13930 Elf_Internal_Sym
*sym
)
13934 switch (h
->root
.type
)
13936 case bfd_link_hash_defined
:
13937 case bfd_link_hash_defweak
:
13938 return h
->root
.u
.def
.section
;
13940 case bfd_link_hash_common
:
13941 return h
->root
.u
.c
.p
->section
;
13948 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13953 /* Return the debug definition section. */
13956 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13957 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13958 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13959 struct elf_link_hash_entry
*h
,
13960 Elf_Internal_Sym
*sym
)
13964 /* Return the global debug definition section. */
13965 if ((h
->root
.type
== bfd_link_hash_defined
13966 || h
->root
.type
== bfd_link_hash_defweak
)
13967 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13968 return h
->root
.u
.def
.section
;
13972 /* Return the local debug definition section. */
13973 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13975 if (isec
!= NULL
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13982 /* COOKIE->rel describes a relocation against section SEC, which is
13983 a section we've decided to keep. Return the section that contains
13984 the relocation symbol, or NULL if no section contains it. */
13987 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13988 elf_gc_mark_hook_fn gc_mark_hook
,
13989 struct elf_reloc_cookie
*cookie
,
13992 unsigned long r_symndx
;
13993 struct elf_link_hash_entry
*h
, *hw
;
13995 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13996 if (r_symndx
== STN_UNDEF
)
13999 if (r_symndx
>= cookie
->locsymcount
14000 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14004 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
14007 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
14011 while (h
->root
.type
== bfd_link_hash_indirect
14012 || h
->root
.type
== bfd_link_hash_warning
)
14013 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14015 was_marked
= h
->mark
;
14017 /* Keep all aliases of the symbol too. If an object symbol
14018 needs to be copied into .dynbss then all of its aliases
14019 should be present as dynamic symbols, not just the one used
14020 on the copy relocation. */
14022 while (hw
->is_weakalias
)
14028 if (!was_marked
&& h
->start_stop
&& !h
->root
.ldscript_def
)
14030 if (info
->start_stop_gc
)
14033 /* To work around a glibc bug, mark XXX input sections
14034 when there is a reference to __start_XXX or __stop_XXX
14036 else if (start_stop
!= NULL
)
14038 asection
*s
= h
->u2
.start_stop_section
;
14039 *start_stop
= true;
14044 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
14047 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
14048 &cookie
->locsyms
[r_symndx
]);
14051 /* COOKIE->rel describes a relocation against section SEC, which is
14052 a section we've decided to keep. Mark the section that contains
14053 the relocation symbol. */
14056 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
14058 elf_gc_mark_hook_fn gc_mark_hook
,
14059 struct elf_reloc_cookie
*cookie
)
14062 bool start_stop
= false;
14064 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
14065 while (rsec
!= NULL
)
14067 if (!rsec
->gc_mark
)
14069 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
14070 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
14072 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
14077 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
14082 /* The mark phase of garbage collection. For a given section, mark
14083 it and any sections in this section's group, and all the sections
14084 which define symbols to which it refers. */
14087 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
14089 elf_gc_mark_hook_fn gc_mark_hook
)
14092 asection
*group_sec
, *eh_frame
;
14096 /* Mark all the sections in the group. */
14097 group_sec
= elf_section_data (sec
)->next_in_group
;
14098 if (group_sec
&& !group_sec
->gc_mark
)
14099 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
14102 /* Look through the section relocs. */
14104 eh_frame
= elf_eh_frame_section (sec
->owner
);
14105 if ((sec
->flags
& SEC_RELOC
) != 0
14106 && sec
->reloc_count
> 0
14107 && sec
!= eh_frame
)
14109 struct elf_reloc_cookie cookie
;
14111 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
, false))
14115 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
14116 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
14121 fini_reloc_cookie_for_section (&cookie
, sec
);
14125 if (ret
&& eh_frame
&& elf_fde_list (sec
))
14127 struct elf_reloc_cookie cookie
;
14129 /* NB: When --no-keep-memory is used, the symbol table and
14130 relocation info for eh_frame are freed after they are retrieved
14131 for each text section in the input object. If an input object
14132 has many text sections, the same data is retrieved and freed
14133 many times which can take a very long time. Always keep the
14134 symbol table and relocation info for eh_frame to avoid it. */
14135 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
,
14140 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
14141 gc_mark_hook
, &cookie
))
14143 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
14147 eh_frame
= elf_section_eh_frame_entry (sec
);
14148 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
14149 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
14155 /* Scan and mark sections in a special or debug section group. */
14158 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
14160 /* Point to first section of section group. */
14162 /* Used to iterate the section group. */
14165 bool is_special_grp
= true;
14166 bool is_debug_grp
= true;
14168 /* First scan to see if group contains any section other than debug
14169 and special section. */
14170 ssec
= msec
= elf_next_in_group (grp
);
14173 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
14174 is_debug_grp
= false;
14176 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
14177 is_special_grp
= false;
14179 msec
= elf_next_in_group (msec
);
14181 while (msec
!= ssec
);
14183 /* If this is a pure debug section group or pure special section group,
14184 keep all sections in this group. */
14185 if (is_debug_grp
|| is_special_grp
)
14190 msec
= elf_next_in_group (msec
);
14192 while (msec
!= ssec
);
14196 /* Keep debug and special sections. */
14199 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
14200 elf_gc_mark_hook_fn mark_hook
)
14204 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14208 bool debug_frag_seen
;
14209 bool has_kept_debug_info
;
14211 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14213 isec
= ibfd
->sections
;
14214 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14217 /* Ensure all linker created sections are kept,
14218 see if any other section is already marked,
14219 and note if we have any fragmented debug sections. */
14220 debug_frag_seen
= some_kept
= has_kept_debug_info
= false;
14221 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14223 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
14225 else if (isec
->gc_mark
14226 && (isec
->flags
& SEC_ALLOC
) != 0
14227 && elf_section_type (isec
) != SHT_NOTE
)
14231 /* Since all sections, except for backend specific ones,
14232 have been garbage collected, call mark_hook on this
14233 section if any of its linked-to sections is marked. */
14234 asection
*linked_to_sec
;
14235 for (linked_to_sec
= elf_linked_to_section (isec
);
14236 linked_to_sec
!= NULL
&& !linked_to_sec
->linker_mark
;
14237 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
14239 if (linked_to_sec
->gc_mark
)
14241 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
14245 linked_to_sec
->linker_mark
= 1;
14247 for (linked_to_sec
= elf_linked_to_section (isec
);
14248 linked_to_sec
!= NULL
&& linked_to_sec
->linker_mark
;
14249 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
14250 linked_to_sec
->linker_mark
= 0;
14253 if (!debug_frag_seen
14254 && (isec
->flags
& SEC_DEBUGGING
)
14255 && startswith (isec
->name
, ".debug_line."))
14256 debug_frag_seen
= true;
14257 else if (strcmp (bfd_section_name (isec
),
14258 "__patchable_function_entries") == 0
14259 && elf_linked_to_section (isec
) == NULL
)
14260 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
14261 "need linked-to section "
14262 "for --gc-sections\n"),
14263 isec
->owner
, isec
);
14266 /* If no non-note alloc section in this file will be kept, then
14267 we can toss out the debug and special sections. */
14271 /* Keep debug and special sections like .comment when they are
14272 not part of a group. Also keep section groups that contain
14273 just debug sections or special sections. NB: Sections with
14274 linked-to section has been handled above. */
14275 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14277 if ((isec
->flags
& SEC_GROUP
) != 0)
14278 _bfd_elf_gc_mark_debug_special_section_group (isec
);
14279 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
14280 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
14281 && elf_next_in_group (isec
) == NULL
14282 && elf_linked_to_section (isec
) == NULL
)
14284 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
14285 has_kept_debug_info
= true;
14288 /* Look for CODE sections which are going to be discarded,
14289 and find and discard any fragmented debug sections which
14290 are associated with that code section. */
14291 if (debug_frag_seen
)
14292 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14293 if ((isec
->flags
& SEC_CODE
) != 0
14294 && isec
->gc_mark
== 0)
14299 ilen
= strlen (isec
->name
);
14301 /* Association is determined by the name of the debug
14302 section containing the name of the code section as
14303 a suffix. For example .debug_line.text.foo is a
14304 debug section associated with .text.foo. */
14305 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
14309 if (dsec
->gc_mark
== 0
14310 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
14313 dlen
= strlen (dsec
->name
);
14316 && strncmp (dsec
->name
+ (dlen
- ilen
),
14317 isec
->name
, ilen
) == 0)
14322 /* Mark debug sections referenced by kept debug sections. */
14323 if (has_kept_debug_info
)
14324 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14326 && (isec
->flags
& SEC_DEBUGGING
) != 0)
14327 if (!_bfd_elf_gc_mark (info
, isec
,
14328 elf_gc_mark_debug_section
))
14335 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
14338 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14340 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14344 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14345 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
14346 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14349 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14352 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14354 /* When any section in a section group is kept, we keep all
14355 sections in the section group. If the first member of
14356 the section group is excluded, we will also exclude the
14358 if (o
->flags
& SEC_GROUP
)
14360 asection
*first
= elf_next_in_group (o
);
14361 o
->gc_mark
= first
->gc_mark
;
14367 /* Skip sweeping sections already excluded. */
14368 if (o
->flags
& SEC_EXCLUDE
)
14371 /* Since this is early in the link process, it is simple
14372 to remove a section from the output. */
14373 o
->flags
|= SEC_EXCLUDE
;
14375 if (info
->print_gc_sections
&& o
->size
!= 0)
14376 /* xgettext:c-format */
14377 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14385 /* Propagate collected vtable information. This is called through
14386 elf_link_hash_traverse. */
14389 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
14391 /* Those that are not vtables. */
14393 || h
->u2
.vtable
== NULL
14394 || h
->u2
.vtable
->parent
== NULL
)
14397 /* Those vtables that do not have parents, we cannot merge. */
14398 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
14401 /* If we've already been done, exit. */
14402 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
14405 /* Make sure the parent's table is up to date. */
14406 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
14408 if (h
->u2
.vtable
->used
== NULL
)
14410 /* None of this table's entries were referenced. Re-use the
14412 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14413 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
14420 /* Or the parent's entries into ours. */
14421 cu
= h
->u2
.vtable
->used
;
14423 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14426 const struct elf_backend_data
*bed
;
14427 unsigned int log_file_align
;
14429 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
14430 log_file_align
= bed
->s
->log_file_align
;
14431 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
14445 struct link_info_ok
14447 struct bfd_link_info
*info
;
14452 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
,
14456 bfd_vma hstart
, hend
;
14457 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
14458 const struct elf_backend_data
*bed
;
14459 unsigned int log_file_align
;
14460 struct link_info_ok
*info
= (struct link_info_ok
*) ptr
;
14462 /* Take care of both those symbols that do not describe vtables as
14463 well as those that are not loaded. */
14465 || h
->u2
.vtable
== NULL
14466 || h
->u2
.vtable
->parent
== NULL
)
14469 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
14470 || h
->root
.type
== bfd_link_hash_defweak
);
14472 sec
= h
->root
.u
.def
.section
;
14473 hstart
= h
->root
.u
.def
.value
;
14474 hend
= hstart
+ h
->size
;
14476 relstart
= _bfd_elf_link_info_read_relocs (sec
->owner
, info
->info
,
14477 sec
, NULL
, NULL
, true);
14479 return info
->ok
= false;
14480 bed
= get_elf_backend_data (sec
->owner
);
14481 log_file_align
= bed
->s
->log_file_align
;
14483 relend
= relstart
+ sec
->reloc_count
;
14485 for (rel
= relstart
; rel
< relend
; ++rel
)
14486 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
14488 /* If the entry is in use, do nothing. */
14489 if (h
->u2
.vtable
->used
14490 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
14492 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
14493 if (h
->u2
.vtable
->used
[entry
])
14496 /* Otherwise, kill it. */
14497 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
14503 /* Mark sections containing dynamically referenced symbols. When
14504 building shared libraries, we must assume that any visible symbol is
14508 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
14510 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
14511 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
14513 if ((h
->root
.type
== bfd_link_hash_defined
14514 || h
->root
.type
== bfd_link_hash_defweak
)
14516 || h
->root
.ldscript_def
14517 || !info
->start_stop_gc
)
14518 && ((h
->ref_dynamic
&& !h
->forced_local
)
14519 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
14520 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
14521 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
14522 && (!bfd_link_executable (info
)
14523 || info
->gc_keep_exported
14524 || info
->export_dynamic
14527 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
14528 && (h
->versioned
>= versioned
14529 || !bfd_hide_sym_by_version (info
->version_info
,
14530 h
->root
.root
.string
)))))
14531 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14536 /* Keep all sections containing symbols undefined on the command-line,
14537 and the section containing the entry symbol. */
14540 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
14542 struct bfd_sym_chain
*sym
;
14544 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
14546 struct elf_link_hash_entry
*h
;
14548 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
14549 false, false, false);
14552 && (h
->root
.type
== bfd_link_hash_defined
14553 || h
->root
.type
== bfd_link_hash_defweak
)
14554 && !bfd_is_const_section (h
->root
.u
.def
.section
))
14555 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14560 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
14561 struct bfd_link_info
*info
)
14563 bfd
*ibfd
= info
->input_bfds
;
14565 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14568 struct elf_reloc_cookie cookie
;
14570 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14572 sec
= ibfd
->sections
;
14573 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14576 if (!init_reloc_cookie (&cookie
, info
, ibfd
, false))
14579 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
14581 if (startswith (bfd_section_name (sec
), ".eh_frame_entry")
14582 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
,
14585 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
14586 fini_reloc_cookie_rels (&cookie
, sec
);
14593 /* Do mark and sweep of unused sections. */
14596 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14600 elf_gc_mark_hook_fn gc_mark_hook
;
14601 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14602 struct elf_link_hash_table
*htab
;
14603 struct link_info_ok info_ok
;
14605 if (!bed
->can_gc_sections
14606 || !is_elf_hash_table (info
->hash
))
14608 _bfd_error_handler(_("warning: gc-sections option ignored"));
14612 bed
->gc_keep (info
);
14613 htab
= elf_hash_table (info
);
14615 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14616 at the .eh_frame section if we can mark the FDEs individually. */
14617 for (sub
= info
->input_bfds
;
14618 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14619 sub
= sub
->link
.next
)
14622 struct elf_reloc_cookie cookie
;
14624 sec
= sub
->sections
;
14625 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14627 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14628 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
,
14631 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14632 if (elf_section_data (sec
)->sec_info
14633 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14634 elf_eh_frame_section (sub
) = sec
;
14635 fini_reloc_cookie_for_section (&cookie
, sec
);
14636 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14640 /* Apply transitive closure to the vtable entry usage info. */
14641 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14645 /* Kill the vtable relocations that were not used. */
14646 info_ok
.info
= info
;
14648 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &info_ok
);
14652 /* Mark dynamically referenced symbols. */
14653 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14654 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14656 /* Grovel through relocs to find out who stays ... */
14657 gc_mark_hook
= bed
->gc_mark_hook
;
14658 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14662 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14663 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14664 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14668 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14671 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14672 Also treat note sections as a root, if the section is not part
14673 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14674 well as FINI_ARRAY sections for ld -r. */
14675 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14677 && (o
->flags
& SEC_EXCLUDE
) == 0
14678 && ((o
->flags
& SEC_KEEP
) != 0
14679 || (bfd_link_relocatable (info
)
14680 && ((elf_section_data (o
)->this_hdr
.sh_type
14681 == SHT_PREINIT_ARRAY
)
14682 || (elf_section_data (o
)->this_hdr
.sh_type
14684 || (elf_section_data (o
)->this_hdr
.sh_type
14685 == SHT_FINI_ARRAY
)))
14686 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14687 && elf_next_in_group (o
) == NULL
14688 && elf_linked_to_section (o
) == NULL
)
14689 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14690 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14692 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14697 /* Allow the backend to mark additional target specific sections. */
14698 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14700 /* ... and mark SEC_EXCLUDE for those that go. */
14701 return elf_gc_sweep (abfd
, info
);
14704 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14707 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14709 struct elf_link_hash_entry
*h
,
14712 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14713 struct elf_link_hash_entry
**search
, *child
;
14714 size_t extsymcount
;
14715 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14717 /* The sh_info field of the symtab header tells us where the
14718 external symbols start. We don't care about the local symbols at
14720 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14721 if (!elf_bad_symtab (abfd
))
14722 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14724 sym_hashes
= elf_sym_hashes (abfd
);
14725 sym_hashes_end
= PTR_ADD (sym_hashes
, extsymcount
);
14727 /* Hunt down the child symbol, which is in this section at the same
14728 offset as the relocation. */
14729 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14731 if ((child
= *search
) != NULL
14732 && (child
->root
.type
== bfd_link_hash_defined
14733 || child
->root
.type
== bfd_link_hash_defweak
)
14734 && child
->root
.u
.def
.section
== sec
14735 && child
->root
.u
.def
.value
== offset
)
14739 /* xgettext:c-format */
14740 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14741 abfd
, sec
, (uint64_t) offset
);
14742 bfd_set_error (bfd_error_invalid_operation
);
14746 if (!child
->u2
.vtable
)
14748 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14749 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14750 if (!child
->u2
.vtable
)
14755 /* This *should* only be the absolute section. It could potentially
14756 be that someone has defined a non-global vtable though, which
14757 would be bad. It isn't worth paging in the local symbols to be
14758 sure though; that case should simply be handled by the assembler. */
14760 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14763 child
->u2
.vtable
->parent
= h
;
14768 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14771 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14772 struct elf_link_hash_entry
*h
,
14775 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14776 unsigned int log_file_align
= bed
->s
->log_file_align
;
14780 /* xgettext:c-format */
14781 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14783 bfd_set_error (bfd_error_bad_value
);
14789 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14790 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14795 if (addend
>= h
->u2
.vtable
->size
)
14797 size_t size
, bytes
, file_align
;
14798 bool *ptr
= h
->u2
.vtable
->used
;
14800 /* While the symbol is undefined, we have to be prepared to handle
14802 file_align
= 1 << log_file_align
;
14803 if (h
->root
.type
== bfd_link_hash_undefined
)
14804 size
= addend
+ file_align
;
14808 if (addend
>= size
)
14810 /* Oops! We've got a reference past the defined end of
14811 the table. This is probably a bug -- shall we warn? */
14812 size
= addend
+ file_align
;
14815 size
= (size
+ file_align
- 1) & -file_align
;
14817 /* Allocate one extra entry for use as a "done" flag for the
14818 consolidation pass. */
14819 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bool);
14823 ptr
= (bool *) bfd_realloc (ptr
- 1, bytes
);
14829 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14831 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14835 ptr
= (bool *) bfd_zmalloc (bytes
);
14840 /* And arrange for that done flag to be at index -1. */
14841 h
->u2
.vtable
->used
= ptr
+ 1;
14842 h
->u2
.vtable
->size
= size
;
14845 h
->u2
.vtable
->used
[addend
>> log_file_align
] = true;
14850 /* Map an ELF section header flag to its corresponding string. */
14854 flagword flag_value
;
14855 } elf_flags_to_name_table
;
14857 static const elf_flags_to_name_table elf_flags_to_names
[] =
14859 { "SHF_WRITE", SHF_WRITE
},
14860 { "SHF_ALLOC", SHF_ALLOC
},
14861 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14862 { "SHF_MERGE", SHF_MERGE
},
14863 { "SHF_STRINGS", SHF_STRINGS
},
14864 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14865 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14866 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14867 { "SHF_GROUP", SHF_GROUP
},
14868 { "SHF_TLS", SHF_TLS
},
14869 { "SHF_MASKOS", SHF_MASKOS
},
14870 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14873 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14875 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14876 struct flag_info
*flaginfo
,
14879 const bfd_vma sh_flags
= elf_section_flags (section
);
14881 if (!flaginfo
->flags_initialized
)
14883 bfd
*obfd
= info
->output_bfd
;
14884 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14885 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14887 int without_hex
= 0;
14889 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14892 flagword (*lookup
) (char *);
14894 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14895 if (lookup
!= NULL
)
14897 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14901 if (tf
->with
== with_flags
)
14902 with_hex
|= hexval
;
14903 else if (tf
->with
== without_flags
)
14904 without_hex
|= hexval
;
14909 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14911 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14913 if (tf
->with
== with_flags
)
14914 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14915 else if (tf
->with
== without_flags
)
14916 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14923 info
->callbacks
->einfo
14924 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14928 flaginfo
->flags_initialized
= true;
14929 flaginfo
->only_with_flags
|= with_hex
;
14930 flaginfo
->not_with_flags
|= without_hex
;
14933 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14936 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14942 struct alloc_got_off_arg
{
14944 struct bfd_link_info
*info
;
14947 /* We need a special top-level link routine to convert got reference counts
14948 to real got offsets. */
14951 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14953 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14954 bfd
*obfd
= gofarg
->info
->output_bfd
;
14955 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14957 if (h
->got
.refcount
> 0)
14959 h
->got
.offset
= gofarg
->gotoff
;
14960 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14963 h
->got
.offset
= (bfd_vma
) -1;
14968 /* And an accompanying bit to work out final got entry offsets once
14969 we're done. Should be called from final_link. */
14972 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14973 struct bfd_link_info
*info
)
14976 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14978 struct alloc_got_off_arg gofarg
;
14980 BFD_ASSERT (abfd
== info
->output_bfd
);
14982 if (! is_elf_hash_table (info
->hash
))
14985 /* The GOT offset is relative to the .got section, but the GOT header is
14986 put into the .got.plt section, if the backend uses it. */
14987 if (bed
->want_got_plt
)
14990 gotoff
= bed
->got_header_size
;
14992 /* Do the local .got entries first. */
14993 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14995 bfd_signed_vma
*local_got
;
14996 size_t j
, locsymcount
;
14997 Elf_Internal_Shdr
*symtab_hdr
;
14999 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
15002 local_got
= elf_local_got_refcounts (i
);
15006 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
15007 if (elf_bad_symtab (i
))
15008 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
15010 locsymcount
= symtab_hdr
->sh_info
;
15012 for (j
= 0; j
< locsymcount
; ++j
)
15014 if (local_got
[j
] > 0)
15016 local_got
[j
] = gotoff
;
15017 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
15020 local_got
[j
] = (bfd_vma
) -1;
15024 /* Then the global .got entries. .plt refcounts are handled by
15025 adjust_dynamic_symbol */
15026 gofarg
.gotoff
= gotoff
;
15027 gofarg
.info
= info
;
15028 elf_link_hash_traverse (elf_hash_table (info
),
15029 elf_gc_allocate_got_offsets
,
15034 /* Many folk need no more in the way of final link than this, once
15035 got entry reference counting is enabled. */
15038 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
15040 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
15043 /* Invoke the regular ELF backend linker to do all the work. */
15044 return bfd_elf_final_link (abfd
, info
);
15048 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
15050 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
15052 if (rcookie
->bad_symtab
)
15053 rcookie
->rel
= rcookie
->rels
;
15055 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
15057 unsigned long r_symndx
;
15059 if (! rcookie
->bad_symtab
)
15060 if (rcookie
->rel
->r_offset
> offset
)
15062 if (rcookie
->rel
->r_offset
!= offset
)
15065 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
15066 if (r_symndx
== STN_UNDEF
)
15069 if (r_symndx
>= rcookie
->locsymcount
15070 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
15072 struct elf_link_hash_entry
*h
;
15074 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
15076 while (h
->root
.type
== bfd_link_hash_indirect
15077 || h
->root
.type
== bfd_link_hash_warning
)
15078 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
15080 if ((h
->root
.type
== bfd_link_hash_defined
15081 || h
->root
.type
== bfd_link_hash_defweak
)
15082 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
15083 || h
->root
.u
.def
.section
->kept_section
!= NULL
15084 || discarded_section (h
->root
.u
.def
.section
)))
15089 /* It's not a relocation against a global symbol,
15090 but it could be a relocation against a local
15091 symbol for a discarded section. */
15093 Elf_Internal_Sym
*isym
;
15095 /* Need to: get the symbol; get the section. */
15096 isym
= &rcookie
->locsyms
[r_symndx
];
15097 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
15099 && (isec
->kept_section
!= NULL
15100 || discarded_section (isec
)))
15108 /* Discard unneeded references to discarded sections.
15109 Returns -1 on error, 1 if any section's size was changed, 0 if
15110 nothing changed. This function assumes that the relocations are in
15111 sorted order, which is true for all known assemblers. */
15114 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
15116 struct elf_reloc_cookie cookie
;
15121 if (info
->traditional_format
15122 || !is_elf_hash_table (info
->hash
))
15125 o
= bfd_get_section_by_name (output_bfd
, ".stab");
15130 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
15133 || i
->reloc_count
== 0
15134 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
15138 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15141 if (!init_reloc_cookie_for_section (&cookie
, info
, i
, false))
15144 if (_bfd_discard_section_stabs (abfd
, i
,
15145 elf_section_data (i
)->sec_info
,
15146 bfd_elf_reloc_symbol_deleted_p
,
15150 fini_reloc_cookie_for_section (&cookie
, i
);
15155 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
15156 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
15160 int eh_changed
= 0;
15161 unsigned int eh_alignment
; /* Octets. */
15163 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
15169 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15172 if (!init_reloc_cookie_for_section (&cookie
, info
, i
, false))
15175 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
15176 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
15177 bfd_elf_reloc_symbol_deleted_p
,
15181 if (i
->size
!= i
->rawsize
)
15185 fini_reloc_cookie_for_section (&cookie
, i
);
15188 eh_alignment
= ((1 << o
->alignment_power
)
15189 * bfd_octets_per_byte (output_bfd
, o
));
15190 /* Skip over zero terminator, and prevent empty sections from
15191 adding alignment padding at the end. */
15192 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
15194 i
->flags
|= SEC_EXCLUDE
;
15195 else if (i
->size
> 4)
15197 /* The last non-empty eh_frame section doesn't need padding. */
15200 /* Any prior sections must pad the last FDE out to the output
15201 section alignment. Otherwise we might have zero padding
15202 between sections, which would be seen as a terminator. */
15203 for (; i
!= NULL
; i
= i
->map_tail
.s
)
15205 /* All but the last zero terminator should have been removed. */
15210 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
15211 if (i
->size
!= size
)
15219 elf_link_hash_traverse (elf_hash_table (info
),
15220 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
15223 o
= bfd_get_section_by_name (output_bfd
, ".sframe");
15228 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
15234 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15237 if (!init_reloc_cookie_for_section (&cookie
, info
, i
, false))
15240 if (_bfd_elf_parse_sframe (abfd
, info
, i
, &cookie
))
15242 if (_bfd_elf_discard_section_sframe (i
,
15243 bfd_elf_reloc_symbol_deleted_p
,
15246 if (i
->size
!= i
->rawsize
)
15250 fini_reloc_cookie_for_section (&cookie
, i
);
15252 /* Update the reference to the output .sframe section. Used to
15253 determine later if PT_GNU_SFRAME segment is to be generated. */
15254 if (!_bfd_elf_set_section_sframe (output_bfd
, info
))
15258 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
15260 const struct elf_backend_data
*bed
;
15263 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15265 s
= abfd
->sections
;
15266 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
15269 bed
= get_elf_backend_data (abfd
);
15271 if (bed
->elf_backend_discard_info
!= NULL
)
15273 if (!init_reloc_cookie (&cookie
, info
, abfd
, false))
15276 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
15279 fini_reloc_cookie (&cookie
, abfd
);
15283 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
15284 _bfd_elf_end_eh_frame_parsing (info
);
15286 if (info
->eh_frame_hdr_type
15287 && !bfd_link_relocatable (info
)
15288 && _bfd_elf_discard_section_eh_frame_hdr (info
))
15295 _bfd_elf_section_already_linked (bfd
*abfd
,
15297 struct bfd_link_info
*info
)
15300 const char *name
, *key
;
15301 struct bfd_section_already_linked
*l
;
15302 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
15304 if (sec
->output_section
== bfd_abs_section_ptr
)
15307 flags
= sec
->flags
;
15309 /* Return if it isn't a linkonce section. A comdat group section
15310 also has SEC_LINK_ONCE set. */
15311 if ((flags
& SEC_LINK_ONCE
) == 0)
15314 /* Don't put group member sections on our list of already linked
15315 sections. They are handled as a group via their group section. */
15316 if (elf_sec_group (sec
) != NULL
)
15319 /* For a SHT_GROUP section, use the group signature as the key. */
15321 if ((flags
& SEC_GROUP
) != 0
15322 && elf_next_in_group (sec
) != NULL
15323 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
15324 key
= elf_group_name (elf_next_in_group (sec
));
15327 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15328 if (startswith (name
, ".gnu.linkonce.")
15329 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
15332 /* Must be a user linkonce section that doesn't follow gcc's
15333 naming convention. In this case we won't be matching
15334 single member groups. */
15338 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
15340 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15342 /* We may have 2 different types of sections on the list: group
15343 sections with a signature of <key> (<key> is some string),
15344 and linkonce sections named .gnu.linkonce.<type>.<key>.
15345 Match like sections. LTO plugin sections are an exception.
15346 They are always named .gnu.linkonce.t.<key> and match either
15347 type of section. */
15348 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
15349 && ((flags
& SEC_GROUP
) != 0
15350 || strcmp (name
, l
->sec
->name
) == 0))
15351 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
15352 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
15354 /* The section has already been linked. See if we should
15355 issue a warning. */
15356 if (!_bfd_handle_already_linked (sec
, l
, info
))
15359 if (flags
& SEC_GROUP
)
15361 asection
*first
= elf_next_in_group (sec
);
15362 asection
*s
= first
;
15366 s
->output_section
= bfd_abs_section_ptr
;
15367 /* Record which group discards it. */
15368 s
->kept_section
= l
->sec
;
15369 s
= elf_next_in_group (s
);
15370 /* These lists are circular. */
15380 /* A single member comdat group section may be discarded by a
15381 linkonce section and vice versa. */
15382 if ((flags
& SEC_GROUP
) != 0)
15384 asection
*first
= elf_next_in_group (sec
);
15386 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
15387 /* Check this single member group against linkonce sections. */
15388 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15389 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15390 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
15392 first
->output_section
= bfd_abs_section_ptr
;
15393 first
->kept_section
= l
->sec
;
15394 sec
->output_section
= bfd_abs_section_ptr
;
15399 /* Check this linkonce section against single member groups. */
15400 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15401 if (l
->sec
->flags
& SEC_GROUP
)
15403 asection
*first
= elf_next_in_group (l
->sec
);
15406 && elf_next_in_group (first
) == first
15407 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
15409 sec
->output_section
= bfd_abs_section_ptr
;
15410 sec
->kept_section
= first
;
15415 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15416 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15417 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15418 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15419 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15420 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15421 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15422 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15423 The reverse order cannot happen as there is never a bfd with only the
15424 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15425 matter as here were are looking only for cross-bfd sections. */
15427 if ((flags
& SEC_GROUP
) == 0 && startswith (name
, ".gnu.linkonce.r."))
15428 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15429 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15430 && startswith (l
->sec
->name
, ".gnu.linkonce.t."))
15432 if (abfd
!= l
->sec
->owner
)
15433 sec
->output_section
= bfd_abs_section_ptr
;
15437 /* This is the first section with this name. Record it. */
15438 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
15439 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
15440 return sec
->output_section
== bfd_abs_section_ptr
;
15444 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
15446 return sym
->st_shndx
== SHN_COMMON
;
15450 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
15456 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
15458 return bfd_com_section_ptr
;
15462 _bfd_elf_default_got_elt_size (bfd
*abfd
,
15463 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
15464 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
15465 bfd
*ibfd ATTRIBUTE_UNUSED
,
15466 unsigned long symndx ATTRIBUTE_UNUSED
)
15468 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15469 return bed
->s
->arch_size
/ 8;
15472 /* Routines to support the creation of dynamic relocs. */
15474 /* Returns the name of the dynamic reloc section associated with SEC. */
15476 static const char *
15477 get_dynamic_reloc_section_name (bfd
* abfd
,
15482 const char *old_name
= bfd_section_name (sec
);
15483 const char *prefix
= is_rela
? ".rela" : ".rel";
15485 if (old_name
== NULL
)
15488 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
15489 sprintf (name
, "%s%s", prefix
, old_name
);
15494 /* Returns the dynamic reloc section associated with SEC.
15495 If necessary compute the name of the dynamic reloc section based
15496 on SEC's name (looked up in ABFD's string table) and the setting
15500 _bfd_elf_get_dynamic_reloc_section (bfd
*abfd
,
15504 asection
*reloc_sec
= elf_section_data (sec
)->sreloc
;
15506 if (reloc_sec
== NULL
)
15508 const char *name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15512 reloc_sec
= bfd_get_linker_section (abfd
, name
);
15514 if (reloc_sec
!= NULL
)
15515 elf_section_data (sec
)->sreloc
= reloc_sec
;
15522 /* Returns the dynamic reloc section associated with SEC. If the
15523 section does not exist it is created and attached to the DYNOBJ
15524 bfd and stored in the SRELOC field of SEC's elf_section_data
15527 ALIGNMENT is the alignment for the newly created section and
15528 IS_RELA defines whether the name should be .rela.<SEC's name>
15529 or .rel.<SEC's name>. The section name is looked up in the
15530 string table associated with ABFD. */
15533 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
15535 unsigned int alignment
,
15539 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
15541 if (reloc_sec
== NULL
)
15543 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15548 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
15550 if (reloc_sec
== NULL
)
15552 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
15553 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
15554 if ((sec
->flags
& SEC_ALLOC
) != 0)
15555 flags
|= SEC_ALLOC
| SEC_LOAD
;
15557 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
15558 if (reloc_sec
!= NULL
)
15560 /* _bfd_elf_get_sec_type_attr chooses a section type by
15561 name. Override as it may be wrong, eg. for a user
15562 section named "auto" we'll get ".relauto" which is
15563 seen to be a .rela section. */
15564 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
15565 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
15570 elf_section_data (sec
)->sreloc
= reloc_sec
;
15576 /* Copy the ELF symbol type and other attributes for a linker script
15577 assignment from HSRC to HDEST. Generally this should be treated as
15578 if we found a strong non-dynamic definition for HDEST (except that
15579 ld ignores multiple definition errors). */
15581 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
15582 struct bfd_link_hash_entry
*hdest
,
15583 struct bfd_link_hash_entry
*hsrc
)
15585 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
15586 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
15587 Elf_Internal_Sym isym
;
15589 ehdest
->type
= ehsrc
->type
;
15590 ehdest
->target_internal
= ehsrc
->target_internal
;
15592 isym
.st_other
= ehsrc
->other
;
15593 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, true, false);
15596 /* Append a RELA relocation REL to section S in BFD. */
15599 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15601 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15602 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
15603 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
15604 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
15607 /* Append a REL relocation REL to section S in BFD. */
15610 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15612 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15613 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
15614 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
15615 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
15618 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15620 struct bfd_link_hash_entry
*
15621 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
15622 const char *symbol
, asection
*sec
)
15624 struct elf_link_hash_entry
*h
;
15626 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
15627 false, false, true);
15628 /* NB: Common symbols will be turned into definition later. */
15630 && !h
->root
.ldscript_def
15631 && (h
->root
.type
== bfd_link_hash_undefined
15632 || h
->root
.type
== bfd_link_hash_undefweak
15633 || ((h
->ref_regular
|| h
->def_dynamic
)
15635 && h
->root
.type
!= bfd_link_hash_common
)))
15637 bool was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15638 h
->verinfo
.verdef
= NULL
;
15639 h
->root
.type
= bfd_link_hash_defined
;
15640 h
->root
.u
.def
.section
= sec
;
15641 h
->root
.u
.def
.value
= 0;
15642 h
->def_regular
= 1;
15643 h
->def_dynamic
= 0;
15645 h
->u2
.start_stop_section
= sec
;
15646 if (symbol
[0] == '.')
15648 /* .startof. and .sizeof. symbols are local. */
15649 const struct elf_backend_data
*bed
;
15650 bed
= get_elf_backend_data (info
->output_bfd
);
15651 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
15655 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15656 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15657 | info
->start_stop_visibility
);
15659 bfd_elf_link_record_dynamic_symbol (info
, h
);
15666 /* Find dynamic relocs for H that apply to read-only sections. */
15669 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15671 struct elf_dyn_relocs
*p
;
15673 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15675 asection
*s
= p
->sec
->output_section
;
15677 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15683 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15684 read-only sections. */
15687 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15691 if (h
->root
.type
== bfd_link_hash_indirect
)
15694 sec
= _bfd_elf_readonly_dynrelocs (h
);
15697 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15699 info
->flags
|= DF_TEXTREL
;
15700 /* xgettext:c-format */
15701 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15702 "in read-only section `%pA'\n"),
15703 sec
->owner
, h
->root
.root
.string
, sec
);
15705 if (bfd_link_textrel_check (info
))
15706 /* xgettext:c-format */
15707 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15708 "in read-only section `%pA'\n"),
15709 sec
->owner
, h
->root
.root
.string
, sec
);
15711 /* Not an error, just cut short the traversal. */
15717 /* Add dynamic tags. */
15720 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15721 bool need_dynamic_reloc
)
15723 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15725 if (htab
->dynamic_sections_created
)
15727 /* Add some entries to the .dynamic section. We fill in the
15728 values later, in finish_dynamic_sections, but we must add
15729 the entries now so that we get the correct size for the
15730 .dynamic section. The DT_DEBUG entry is filled in by the
15731 dynamic linker and used by the debugger. */
15732 #define add_dynamic_entry(TAG, VAL) \
15733 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15735 const struct elf_backend_data
*bed
15736 = get_elf_backend_data (output_bfd
);
15738 if (bfd_link_executable (info
))
15740 if (!add_dynamic_entry (DT_DEBUG
, 0))
15744 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15746 /* DT_PLTGOT is used by prelink even if there is no PLT
15748 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15752 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15754 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15755 || !add_dynamic_entry (DT_PLTREL
,
15756 (bed
->rela_plts_and_copies_p
15757 ? DT_RELA
: DT_REL
))
15758 || !add_dynamic_entry (DT_JMPREL
, 0))
15762 if (htab
->tlsdesc_plt
15763 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15764 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15767 if (need_dynamic_reloc
)
15769 if (bed
->rela_plts_and_copies_p
)
15771 if (!add_dynamic_entry (DT_RELA
, 0)
15772 || !add_dynamic_entry (DT_RELASZ
, 0)
15773 || !add_dynamic_entry (DT_RELAENT
,
15774 bed
->s
->sizeof_rela
))
15779 if (!add_dynamic_entry (DT_REL
, 0)
15780 || !add_dynamic_entry (DT_RELSZ
, 0)
15781 || !add_dynamic_entry (DT_RELENT
,
15782 bed
->s
->sizeof_rel
))
15786 /* If any dynamic relocs apply to a read-only section,
15787 then we need a DT_TEXTREL entry. */
15788 if ((info
->flags
& DF_TEXTREL
) == 0)
15789 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15792 if ((info
->flags
& DF_TEXTREL
) != 0)
15794 if (htab
->ifunc_resolvers
)
15795 info
->callbacks
->einfo
15796 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15797 "may result in a segfault at runtime; recompile with %s\n"),
15798 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15800 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15805 #undef add_dynamic_entry