1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2025 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
40 /* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
43 struct elf_info_failed
45 struct bfd_link_info
*info
;
49 static bool _bfd_elf_fix_symbol_flags
50 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
52 /* Return false if linker should avoid caching relocation information
53 and symbol tables of input files in memory. */
56 _bfd_elf_link_keep_memory (struct bfd_link_info
*info
)
59 /* Don't cache symbol nor relocation tables if they are mapped in.
60 NB: Since the --no-keep-memory linker option causes:
62 https://sourceware.org/bugzilla/show_bug.cgi?id=31458
64 this is opt-in by each backend. */
65 const struct elf_backend_data
*bed
66 = get_elf_backend_data (info
->output_bfd
);
67 if (bed
!= NULL
&& bed
->use_mmap
)
73 if (!info
->keep_memory
)
76 if (info
->max_cache_size
== (bfd_size_type
) -1)
79 abfd
= info
->input_bfds
;
80 size
= info
->cache_size
;
83 if (size
>= info
->max_cache_size
)
85 /* Over the limit. Reduce the memory usage. */
86 info
->keep_memory
= false;
91 size
+= abfd
->alloc_size
;
92 abfd
= abfd
->link
.next
;
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. GLIBC_MINOR_BASE is the
2251 pointer to the glibc minor base version. */
2253 static Elf_Internal_Verneed
*
2254 elf_link_add_glibc_verneed (struct elf_find_verdep_info
*rinfo
,
2255 Elf_Internal_Verneed
*glibc_verref
,
2256 const char *version_dep
,
2257 int *glibc_minor_base
)
2259 Elf_Internal_Verneed
*t
;
2260 Elf_Internal_Vernaux
*a
;
2262 int minor_version
= -1;
2264 if (glibc_verref
!= NULL
)
2268 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2270 /* Return if VERSION_DEP dependency has been added. */
2271 if (a
->vna_nodename
== version_dep
2272 || strcmp (a
->vna_nodename
, version_dep
) == 0)
2278 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2282 const char *soname
= bfd_elf_get_dt_soname (t
->vn_bfd
);
2283 if (soname
!= NULL
&& startswith (soname
, "libc.so."))
2287 /* 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 (startswith (a
->vna_nodename
, "GLIBC_2."))
2301 minor_version
= strtol (a
->vna_nodename
+ 8, NULL
, 10);
2302 if (minor_version
< *glibc_minor_base
)
2303 *glibc_minor_base
= minor_version
;
2307 /* Skip if it isn't linked against glibc. */
2308 if (minor_version
< 0)
2312 /* Skip if 2.GLIBC_MINOR_BASE includes VERSION_DEP. */
2313 if (startswith (version_dep
, "GLIBC_2."))
2315 minor_version
= strtol (version_dep
+ 8, NULL
, 10);
2316 if (minor_version
<= *glibc_minor_base
)
2321 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2324 rinfo
->failed
= true;
2328 a
->vna_nodename
= version_dep
;
2330 a
->vna_nextptr
= t
->vn_auxptr
;
2331 a
->vna_other
= rinfo
->vers
+ 1;
2339 /* Add VERSION_DEP to the list of version dependencies when linked
2343 _bfd_elf_link_add_glibc_version_dependency
2344 (struct elf_find_verdep_info
*rinfo
,
2345 const char *version_dep
[])
2347 Elf_Internal_Verneed
*t
= NULL
;
2348 int glibc_minor_base
= INT_MAX
;
2352 t
= elf_link_add_glibc_verneed (rinfo
, t
, *version_dep
,
2354 /* Return if there is no glibc version reference. */
2359 while (*version_dep
!= NULL
);
2362 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2363 linked against glibc. */
2366 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info
*rinfo
)
2368 if (rinfo
->info
->enable_dt_relr
)
2370 const char *version
[] =
2372 "GLIBC_ABI_DT_RELR",
2375 _bfd_elf_link_add_glibc_version_dependency (rinfo
, version
);
2379 /* Look through the symbols which are defined in other shared
2380 libraries and referenced here. Update the list of version
2381 dependencies. This will be put into the .gnu.version_r section.
2382 This function is called via elf_link_hash_traverse. */
2385 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2388 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2389 Elf_Internal_Verneed
*t
;
2390 Elf_Internal_Vernaux
*a
;
2393 /* We only care about symbols defined in shared objects with version
2398 || h
->verinfo
.verdef
== NULL
2399 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2400 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2403 /* See if we already know about this version. */
2404 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2408 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2411 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2412 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2418 /* This is a new version. Add it to tree we are building. */
2423 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2426 rinfo
->failed
= true;
2430 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2431 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2432 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2436 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2439 rinfo
->failed
= true;
2443 /* Note that we are copying a string pointer here, and testing it
2444 above. If bfd_elf_string_from_elf_section is ever changed to
2445 discard the string data when low in memory, this will have to be
2447 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2449 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2450 a
->vna_nextptr
= t
->vn_auxptr
;
2452 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2455 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2462 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2463 hidden. Set *T_P to NULL if there is no match. */
2466 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info
*info
,
2467 struct elf_link_hash_entry
*h
,
2468 const char *version_p
,
2469 struct bfd_elf_version_tree
**t_p
,
2472 struct bfd_elf_version_tree
*t
;
2474 /* Look for the version. If we find it, it is no longer weak. */
2475 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
2477 if (strcmp (t
->name
, version_p
) == 0)
2481 struct bfd_elf_version_expr
*d
;
2483 len
= version_p
- h
->root
.root
.string
;
2484 alc
= (char *) bfd_malloc (len
);
2487 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2488 alc
[len
- 1] = '\0';
2489 if (alc
[len
- 2] == ELF_VER_CHR
)
2490 alc
[len
- 2] = '\0';
2492 h
->verinfo
.vertree
= t
;
2496 if (t
->globals
.list
!= NULL
)
2497 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2499 /* See if there is anything to force this symbol to
2501 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2503 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2506 && ! info
->export_dynamic
)
2520 /* Return TRUE if the symbol H is hidden by version script. */
2523 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info
*info
,
2524 struct elf_link_hash_entry
*h
)
2528 const struct elf_backend_data
*bed
2529 = get_elf_backend_data (info
->output_bfd
);
2531 /* Version script only hides symbols defined in regular objects. */
2532 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2535 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2536 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2538 struct bfd_elf_version_tree
*t
;
2541 if (*p
== ELF_VER_CHR
)
2545 && _bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
)
2549 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2554 /* If we don't have a version for this symbol, see if we can find
2556 if (h
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
2559 = bfd_find_version_for_sym (info
->version_info
,
2560 h
->root
.root
.string
, &hide
);
2561 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2563 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2571 /* Figure out appropriate versions for all the symbols. We may not
2572 have the version number script until we have read all of the input
2573 files, so until that point we don't know which symbols should be
2574 local. This function is called via elf_link_hash_traverse. */
2577 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2579 struct elf_info_failed
*sinfo
;
2580 struct bfd_link_info
*info
;
2581 const struct elf_backend_data
*bed
;
2582 struct elf_info_failed eif
;
2586 sinfo
= (struct elf_info_failed
*) data
;
2589 /* Fix the symbol flags. */
2592 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2595 sinfo
->failed
= true;
2599 bed
= get_elf_backend_data (info
->output_bfd
);
2601 /* We only need version numbers for symbols defined in regular
2603 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2605 /* Hide symbols defined in discarded input sections. */
2606 if ((h
->root
.type
== bfd_link_hash_defined
2607 || h
->root
.type
== bfd_link_hash_defweak
)
2608 && discarded_section (h
->root
.u
.def
.section
))
2609 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2614 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2615 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2617 struct bfd_elf_version_tree
*t
;
2620 if (*p
== ELF_VER_CHR
)
2623 /* If there is no version string, we can just return out. */
2627 if (!_bfd_elf_link_hide_versioned_symbol (info
, h
, p
, &t
, &hide
))
2629 sinfo
->failed
= true;
2634 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2636 /* If we are building an application, we need to create a
2637 version node for this version. */
2638 if (t
== NULL
&& bfd_link_executable (info
))
2640 struct bfd_elf_version_tree
**pp
;
2643 /* If we aren't going to export this symbol, we don't need
2644 to worry about it. */
2645 if (h
->dynindx
== -1)
2648 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2652 sinfo
->failed
= true;
2657 t
->name_indx
= (unsigned int) -1;
2661 /* Don't count anonymous version tag. */
2662 if (sinfo
->info
->version_info
!= NULL
2663 && sinfo
->info
->version_info
->vernum
== 0)
2665 for (pp
= &sinfo
->info
->version_info
;
2669 t
->vernum
= version_index
;
2673 h
->verinfo
.vertree
= t
;
2677 /* We could not find the version for a symbol when
2678 generating a shared archive. Return an error. */
2680 /* xgettext:c-format */
2681 (_("%pB: version node not found for symbol %s"),
2682 info
->output_bfd
, h
->root
.root
.string
);
2683 bfd_set_error (bfd_error_bad_value
);
2684 sinfo
->failed
= true;
2689 /* If we don't have a version for this symbol, see if we can find
2692 && h
->verinfo
.vertree
== NULL
2693 && sinfo
->info
->version_info
!= NULL
)
2696 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2697 h
->root
.root
.string
, &hide
);
2698 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2699 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
2705 /* Read and swap the relocs from the section indicated by SHDR. This
2706 may be either a REL or a RELA section. The relocations are
2707 translated into RELA relocations and stored in INTERNAL_RELOCS,
2708 which should have already been allocated to contain enough space.
2709 The *EXTERNAL_RELOCS_P are a buffer where the external form of the
2710 relocations should be stored. If *EXTERNAL_RELOCS_ADDR is NULL,
2711 *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
2712 memory address and size. Otherwise, *EXTERNAL_RELOCS_ADDR is
2713 unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
2715 Returns FALSE if something goes wrong. */
2718 elf_link_read_relocs_from_section (bfd
*abfd
,
2719 const asection
*sec
,
2720 Elf_Internal_Shdr
*shdr
,
2721 void **external_relocs_addr
,
2722 size_t *external_relocs_size
,
2723 Elf_Internal_Rela
*internal_relocs
)
2725 const struct elf_backend_data
*bed
;
2726 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2727 const bfd_byte
*erela
;
2728 const bfd_byte
*erelaend
;
2729 Elf_Internal_Rela
*irela
;
2730 Elf_Internal_Shdr
*symtab_hdr
;
2732 void *external_relocs
= *external_relocs_addr
;
2734 /* Position ourselves at the start of the section. */
2735 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2738 /* Read the relocations. */
2739 *external_relocs_size
= shdr
->sh_size
;
2740 if (!_bfd_mmap_read_temporary (&external_relocs
,
2741 external_relocs_size
,
2742 external_relocs_addr
, abfd
, true))
2745 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2746 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2748 bed
= get_elf_backend_data (abfd
);
2750 /* Convert the external relocations to the internal format. */
2751 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2752 swap_in
= bed
->s
->swap_reloc_in
;
2753 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2754 swap_in
= bed
->s
->swap_reloca_in
;
2757 bfd_set_error (bfd_error_wrong_format
);
2761 erela
= (const bfd_byte
*) external_relocs
;
2762 /* Setting erelaend like this and comparing with <= handles case of
2763 a fuzzed object with sh_size not a multiple of sh_entsize. */
2764 erelaend
= erela
+ shdr
->sh_size
- shdr
->sh_entsize
;
2765 irela
= internal_relocs
;
2766 while (erela
<= erelaend
)
2770 (*swap_in
) (abfd
, erela
, irela
);
2771 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2772 if (bed
->s
->arch_size
== 64)
2776 if ((size_t) r_symndx
>= nsyms
)
2779 /* xgettext:c-format */
2780 (_("%pB: bad reloc symbol index (%#" PRIx64
" >= %#lx)"
2781 " for offset %#" PRIx64
" in section `%pA'"),
2782 abfd
, (uint64_t) r_symndx
, (unsigned long) nsyms
,
2783 (uint64_t) irela
->r_offset
, sec
);
2784 bfd_set_error (bfd_error_bad_value
);
2788 else if (r_symndx
!= STN_UNDEF
)
2791 /* xgettext:c-format */
2792 (_("%pB: non-zero symbol index (%#" PRIx64
")"
2793 " for offset %#" PRIx64
" in section `%pA'"
2794 " when the object file has no symbol table"),
2795 abfd
, (uint64_t) r_symndx
,
2796 (uint64_t) irela
->r_offset
, sec
);
2797 bfd_set_error (bfd_error_bad_value
);
2800 irela
+= bed
->s
->int_rels_per_ext_rel
;
2801 erela
+= shdr
->sh_entsize
;
2807 /* Read and swap the relocs for a section O. They may have been
2808 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2809 not NULL, they are used as buffers to read into. They are known to
2810 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2811 the return value is allocated using either malloc or bfd_alloc,
2812 according to the KEEP_MEMORY argument. If O has two relocation
2813 sections (both REL and RELA relocations), then the REL_HDR
2814 relocations will appear first in INTERNAL_RELOCS, followed by the
2815 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2816 update cache_size. */
2819 _bfd_elf_link_info_read_relocs (bfd
*abfd
,
2820 struct bfd_link_info
*info
,
2822 void *external_relocs
,
2823 Elf_Internal_Rela
*internal_relocs
,
2826 void *alloc1
= NULL
;
2828 Elf_Internal_Rela
*alloc2
= NULL
;
2829 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2830 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2831 Elf_Internal_Rela
*internal_rela_relocs
;
2833 if (esdo
->relocs
!= NULL
)
2834 return esdo
->relocs
;
2836 if (o
->reloc_count
== 0)
2839 if (internal_relocs
== NULL
)
2843 size
= (bfd_size_type
) o
->reloc_count
* sizeof (Elf_Internal_Rela
);
2844 if (keep_memory
&& info
)
2845 info
->cache_size
+= size
;
2846 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2847 if (internal_relocs
== NULL
)
2851 alloc1
= external_relocs
;
2852 internal_rela_relocs
= internal_relocs
;
2855 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2856 &alloc1
, &alloc1_size
,
2859 external_relocs
= (((bfd_byte
*) external_relocs
)
2860 + esdo
->rel
.hdr
->sh_size
);
2861 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2862 * bed
->s
->int_rels_per_ext_rel
);
2866 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2867 &alloc1
, &alloc1_size
,
2868 internal_rela_relocs
)))
2871 /* Cache the results for next time, if we can. */
2873 esdo
->relocs
= internal_relocs
;
2875 _bfd_munmap_temporary (alloc1
, alloc1_size
);
2877 /* Don't free alloc2, since if it was allocated we are passing it
2878 back (under the name of internal_relocs). */
2880 return internal_relocs
;
2883 _bfd_munmap_temporary (alloc1
, alloc1_size
);
2888 /* This is similar to _bfd_elf_link_info_read_relocs, except for that
2889 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2890 struct bfd_link_info. */
2893 _bfd_elf_link_read_relocs (bfd
*abfd
,
2895 void *external_relocs
,
2896 Elf_Internal_Rela
*internal_relocs
,
2899 return _bfd_elf_link_info_read_relocs (abfd
, NULL
, o
, external_relocs
,
2900 internal_relocs
, keep_memory
);
2904 /* Compute the size of, and allocate space for, REL_HDR which is the
2905 section header for a section containing relocations for O. */
2908 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2909 struct bfd_elf_section_reloc_data
*reldata
)
2911 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2913 /* That allows us to calculate the size of the section. */
2914 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2916 /* The contents field must last into write_object_contents, so we
2917 allocate it with bfd_alloc rather than malloc. Also since we
2918 cannot be sure that the contents will actually be filled in,
2919 we zero the allocated space. */
2920 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2921 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2924 if (reldata
->hashes
== NULL
&& reldata
->count
)
2926 struct elf_link_hash_entry
**p
;
2928 p
= ((struct elf_link_hash_entry
**)
2929 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2933 reldata
->hashes
= p
;
2939 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2940 originated from the section given by INPUT_REL_HDR) to the
2944 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2945 asection
*input_section
,
2946 Elf_Internal_Shdr
*input_rel_hdr
,
2947 Elf_Internal_Rela
*internal_relocs
,
2948 struct elf_link_hash_entry
**rel_hash
)
2950 Elf_Internal_Rela
*irela
;
2951 Elf_Internal_Rela
*irelaend
;
2953 struct bfd_elf_section_reloc_data
*output_reldata
;
2954 asection
*output_section
;
2955 const struct elf_backend_data
*bed
;
2956 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2957 struct bfd_elf_section_data
*esdo
;
2959 output_section
= input_section
->output_section
;
2961 bed
= get_elf_backend_data (output_bfd
);
2962 esdo
= elf_section_data (output_section
);
2963 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2965 output_reldata
= &esdo
->rel
;
2966 swap_out
= bed
->s
->swap_reloc_out
;
2968 else if (esdo
->rela
.hdr
2969 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2971 output_reldata
= &esdo
->rela
;
2972 swap_out
= bed
->s
->swap_reloca_out
;
2977 /* xgettext:c-format */
2978 (_("%pB: relocation size mismatch in %pB section %pA"),
2979 output_bfd
, input_section
->owner
, input_section
);
2980 bfd_set_error (bfd_error_wrong_format
);
2984 erel
= output_reldata
->hdr
->contents
;
2985 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2986 irela
= internal_relocs
;
2987 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2988 * bed
->s
->int_rels_per_ext_rel
);
2989 while (irela
< irelaend
)
2991 if (rel_hash
&& *rel_hash
)
2992 (*rel_hash
)->has_reloc
= 1;
2993 (*swap_out
) (output_bfd
, irela
, erel
);
2994 irela
+= bed
->s
->int_rels_per_ext_rel
;
2995 erel
+= input_rel_hdr
->sh_entsize
;
3000 /* Bump the counter, so that we know where to add the next set of
3002 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
3007 /* Make weak undefined symbols in PIE dynamic. */
3010 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
3011 struct elf_link_hash_entry
*h
)
3013 if (bfd_link_pie (info
)
3015 && h
->root
.type
== bfd_link_hash_undefweak
)
3016 return bfd_elf_link_record_dynamic_symbol (info
, h
);
3021 /* Fix up the flags for a symbol. This handles various cases which
3022 can only be fixed after all the input files are seen. This is
3023 currently called by both adjust_dynamic_symbol and
3024 assign_sym_version, which is unnecessary but perhaps more robust in
3025 the face of future changes. */
3028 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
3029 struct elf_info_failed
*eif
)
3031 const struct elf_backend_data
*bed
;
3033 /* If this symbol was mentioned in a non-ELF file, try to set
3034 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3035 permit a non-ELF file to correctly refer to a symbol defined in
3036 an ELF dynamic object. */
3039 while (h
->root
.type
== bfd_link_hash_indirect
)
3040 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3042 if (h
->root
.type
!= bfd_link_hash_defined
3043 && h
->root
.type
!= bfd_link_hash_defweak
)
3046 h
->ref_regular_nonweak
= 1;
3050 if (h
->root
.u
.def
.section
->owner
!= NULL
3051 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3052 == bfd_target_elf_flavour
))
3055 h
->ref_regular_nonweak
= 1;
3061 if (h
->dynindx
== -1
3065 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3074 /* Unfortunately, NON_ELF is only correct if the symbol
3075 was first seen in a non-ELF file. Fortunately, if the symbol
3076 was first seen in an ELF file, we're probably OK unless the
3077 symbol was defined in a non-ELF file. Catch that case here.
3078 FIXME: We're still in trouble if the symbol was first seen in
3079 a dynamic object, and then later in a non-ELF regular object. */
3080 if ((h
->root
.type
== bfd_link_hash_defined
3081 || h
->root
.type
== bfd_link_hash_defweak
)
3083 && (h
->root
.u
.def
.section
->owner
!= NULL
3084 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
3085 != bfd_target_elf_flavour
)
3086 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
3087 && !h
->def_dynamic
)))
3091 /* Backend specific symbol fixup. */
3092 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
3093 if (bed
->elf_backend_fixup_symbol
3094 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
3097 /* If this is a final link, and the symbol was defined as a common
3098 symbol in a regular object file, and there was no definition in
3099 any dynamic object, then the linker will have allocated space for
3100 the symbol in a common section but the DEF_REGULAR
3101 flag will not have been set. */
3102 if (h
->root
.type
== bfd_link_hash_defined
3106 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
3109 /* Symbols defined in discarded sections shouldn't be dynamic. */
3110 if (h
->root
.type
== bfd_link_hash_undefined
&& h
->indx
== -3)
3111 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3113 /* If a weak undefined symbol has non-default visibility, we also
3114 hide it from the dynamic linker. */
3115 else if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
3116 && h
->root
.type
== bfd_link_hash_undefweak
)
3117 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3119 /* A hidden versioned symbol in executable should be forced local if
3120 it is is locally defined, not referenced by shared library and not
3122 else if (bfd_link_executable (eif
->info
)
3123 && h
->versioned
== versioned_hidden
3124 && !eif
->info
->export_dynamic
3128 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3130 /* If -Bsymbolic was used (which means to bind references to global
3131 symbols to the definition within the shared object), and this
3132 symbol was defined in a regular object, then it actually doesn't
3133 need a PLT entry. Likewise, if the symbol has non-default
3134 visibility. If the symbol has hidden or internal visibility, we
3135 will force it local. */
3136 else if (h
->needs_plt
3137 && bfd_link_pic (eif
->info
)
3138 && is_elf_hash_table (eif
->info
->hash
)
3139 && (SYMBOLIC_BIND (eif
->info
, h
)
3140 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
3145 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
3146 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
3147 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
3150 /* If this is a weak defined symbol in a dynamic object, and we know
3151 the real definition in the dynamic object, copy interesting flags
3152 over to the real definition. */
3153 if (h
->is_weakalias
)
3155 struct elf_link_hash_entry
*def
= weakdef (h
);
3157 /* If the real definition is defined by a regular object file,
3158 don't do anything special. See the longer description in
3159 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3160 bfd_link_hash_defined as it was when put on the alias list
3161 then it must have originally been a versioned symbol (for
3162 which a non-versioned indirect symbol is created) and later
3163 a definition for the non-versioned symbol is found. In that
3164 case the indirection is flipped with the versioned symbol
3165 becoming an indirect pointing at the non-versioned symbol.
3166 Thus, not an alias any more. */
3167 if (def
->def_regular
3168 || def
->root
.type
!= bfd_link_hash_defined
)
3171 while ((h
= h
->u
.alias
) != def
)
3172 h
->is_weakalias
= 0;
3176 while (h
->root
.type
== bfd_link_hash_indirect
)
3177 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3178 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
3179 || h
->root
.type
== bfd_link_hash_defweak
);
3180 BFD_ASSERT (def
->def_dynamic
);
3181 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, def
, h
);
3188 /* Make the backend pick a good value for a dynamic symbol. This is
3189 called via elf_link_hash_traverse, and also calls itself
3193 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
3195 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
3196 struct elf_link_hash_table
*htab
;
3197 const struct elf_backend_data
*bed
;
3199 if (! is_elf_hash_table (eif
->info
->hash
))
3202 /* Ignore indirect symbols. These are added by the versioning code. */
3203 if (h
->root
.type
== bfd_link_hash_indirect
)
3206 /* Fix the symbol flags. */
3207 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
3210 htab
= elf_hash_table (eif
->info
);
3211 bed
= get_elf_backend_data (htab
->dynobj
);
3213 if (h
->root
.type
== bfd_link_hash_undefweak
)
3215 if (eif
->info
->dynamic_undefined_weak
== 0)
3216 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, true);
3217 else if (eif
->info
->dynamic_undefined_weak
> 0
3219 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3220 && !bfd_hide_sym_by_version (eif
->info
->version_info
,
3221 h
->root
.root
.string
))
3223 if (!bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
3231 /* If this symbol does not require a PLT entry, and it is not
3232 defined by a dynamic object, or is not referenced by a regular
3233 object, ignore it. We do have to handle a weak defined symbol,
3234 even if no regular object refers to it, if we decided to add it
3235 to the dynamic symbol table. FIXME: Do we normally need to worry
3236 about symbols which are defined by one dynamic object and
3237 referenced by another one? */
3239 && h
->type
!= STT_GNU_IFUNC
3243 && (!h
->is_weakalias
|| weakdef (h
)->dynindx
== -1))))
3245 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
3249 /* If we've already adjusted this symbol, don't do it again. This
3250 can happen via a recursive call. */
3251 if (h
->dynamic_adjusted
)
3254 /* Don't look at this symbol again. Note that we must set this
3255 after checking the above conditions, because we may look at a
3256 symbol once, decide not to do anything, and then get called
3257 recursively later after REF_REGULAR is set below. */
3258 h
->dynamic_adjusted
= 1;
3260 /* If this is a weak definition, and we know a real definition, and
3261 the real symbol is not itself defined by a regular object file,
3262 then get a good value for the real definition. We handle the
3263 real symbol first, for the convenience of the backend routine.
3265 Note that there is a confusing case here. If the real definition
3266 is defined by a regular object file, we don't get the real symbol
3267 from the dynamic object, but we do get the weak symbol. If the
3268 processor backend uses a COPY reloc, then if some routine in the
3269 dynamic object changes the real symbol, we will not see that
3270 change in the corresponding weak symbol. This is the way other
3271 ELF linkers work as well, and seems to be a result of the shared
3274 I will clarify this issue. Most SVR4 shared libraries define the
3275 variable _timezone and define timezone as a weak synonym. The
3276 tzset call changes _timezone. If you write
3277 extern int timezone;
3279 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3280 you might expect that, since timezone is a synonym for _timezone,
3281 the same number will print both times. However, if the processor
3282 backend uses a COPY reloc, then actually timezone will be copied
3283 into your process image, and, since you define _timezone
3284 yourself, _timezone will not. Thus timezone and _timezone will
3285 wind up at different memory locations. The tzset call will set
3286 _timezone, leaving timezone unchanged. */
3288 if (h
->is_weakalias
)
3290 struct elf_link_hash_entry
*def
= weakdef (h
);
3292 /* If we get to this point, there is an implicit reference to
3293 the alias by a regular object file via the weak symbol H. */
3294 def
->ref_regular
= 1;
3296 /* Ensure that the backend adjust_dynamic_symbol function sees
3297 the strong alias before H by recursively calling ourselves. */
3298 if (!_bfd_elf_adjust_dynamic_symbol (def
, eif
))
3302 /* If a symbol has no type and no size and does not require a PLT
3303 entry, then we are probably about to do the wrong thing here: we
3304 are probably going to create a COPY reloc for an empty object.
3305 This case can arise when a shared object is built with assembly
3306 code, and the assembly code fails to set the symbol type. */
3308 && h
->type
== STT_NOTYPE
3311 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3312 h
->root
.root
.string
);
3314 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
3323 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3327 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
3328 struct elf_link_hash_entry
*h
,
3331 unsigned int power_of_two
;
3333 asection
*sec
= h
->root
.u
.def
.section
;
3335 /* The section alignment of the definition is the maximum alignment
3336 requirement of symbols defined in the section. Since we don't
3337 know the symbol alignment requirement, we start with the
3338 maximum alignment and check low bits of the symbol address
3339 for the minimum alignment. */
3340 power_of_two
= bfd_section_alignment (sec
);
3341 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
3342 while ((h
->root
.u
.def
.value
& mask
) != 0)
3348 if (power_of_two
> bfd_section_alignment (dynbss
))
3350 /* Adjust the section alignment if needed. */
3351 if (!bfd_set_section_alignment (dynbss
, power_of_two
))
3355 /* We make sure that the symbol will be aligned properly. */
3356 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
3358 /* Define the symbol as being at this point in DYNBSS. */
3359 h
->root
.u
.def
.section
= dynbss
;
3360 h
->root
.u
.def
.value
= dynbss
->size
;
3362 /* Increment the size of DYNBSS to make room for the symbol. */
3363 dynbss
->size
+= h
->size
;
3365 /* No error if extern_protected_data is true. */
3366 if (h
->protected_def
3367 && (!info
->extern_protected_data
3368 || (info
->extern_protected_data
< 0
3369 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
3370 info
->callbacks
->einfo
3371 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3372 h
->root
.root
.string
);
3377 /* Adjust all external symbols pointing into SEC_MERGE sections
3378 to reflect the object merging within the sections. */
3381 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
3385 if ((h
->root
.type
== bfd_link_hash_defined
3386 || h
->root
.type
== bfd_link_hash_defweak
)
3387 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
3388 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
3390 bfd
*output_bfd
= (bfd
*) data
;
3392 h
->root
.u
.def
.value
=
3393 _bfd_merged_section_offset (output_bfd
,
3394 &h
->root
.u
.def
.section
,
3395 elf_section_data (sec
)->sec_info
,
3396 h
->root
.u
.def
.value
);
3402 /* Returns false if the symbol referred to by H should be considered
3403 to resolve local to the current module, and true if it should be
3404 considered to bind dynamically. */
3407 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
3408 struct bfd_link_info
*info
,
3409 bool not_local_protected
)
3411 bool binding_stays_local_p
;
3412 const struct elf_backend_data
*bed
;
3413 struct elf_link_hash_table
*hash_table
;
3418 while (h
->root
.type
== bfd_link_hash_indirect
3419 || h
->root
.type
== bfd_link_hash_warning
)
3420 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3422 /* If it was forced local, then clearly it's not dynamic. */
3423 if (h
->dynindx
== -1)
3425 if (h
->forced_local
)
3428 /* Identify the cases where name binding rules say that a
3429 visible symbol resolves locally. */
3430 binding_stays_local_p
= (bfd_link_executable (info
)
3431 || SYMBOLIC_BIND (info
, h
));
3433 switch (ELF_ST_VISIBILITY (h
->other
))
3440 hash_table
= elf_hash_table (info
);
3441 if (!is_elf_hash_table (&hash_table
->root
))
3444 bed
= get_elf_backend_data (hash_table
->dynobj
);
3446 /* Proper resolution for function pointer equality may require
3447 that these symbols perhaps be resolved dynamically, even though
3448 we should be resolving them to the current module. */
3449 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
3450 binding_stays_local_p
= true;
3457 /* If it isn't defined locally, then clearly it's dynamic. */
3458 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
3461 /* Otherwise, the symbol is dynamic if binding rules don't tell
3462 us that it remains local. */
3463 return !binding_stays_local_p
;
3466 /* Return true if the symbol referred to by H should be considered
3467 to resolve local to the current module, and false otherwise. Differs
3468 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3469 undefined symbols. The two functions are virtually identical except
3470 for the place where dynindx == -1 is tested. If that test is true,
3471 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3472 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3474 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3475 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3476 treatment of undefined weak symbols. For those that do not make
3477 undefined weak symbols dynamic, both functions may return false. */
3480 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
3481 struct bfd_link_info
*info
,
3482 bool local_protected
)
3484 const struct elf_backend_data
*bed
;
3485 struct elf_link_hash_table
*hash_table
;
3487 /* If it's a local sym, of course we resolve locally. */
3491 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3492 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
3493 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
3496 /* Forced local symbols resolve locally. */
3497 if (h
->forced_local
)
3500 /* Common symbols that become definitions don't get the DEF_REGULAR
3501 flag set, so test it first, and don't bail out. */
3502 if (ELF_COMMON_DEF_P (h
))
3504 /* If we don't have a definition in a regular file, then we can't
3505 resolve locally. The sym is either undefined or dynamic. */
3506 else if (!h
->def_regular
)
3509 /* Non-dynamic symbols resolve locally. */
3510 if (h
->dynindx
== -1)
3513 /* At this point, we know the symbol is defined and dynamic. In an
3514 executable it must resolve locally, likewise when building symbolic
3515 shared libraries. */
3516 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3519 /* Now deal with defined dynamic symbols in shared libraries. Ones
3520 with default visibility might not resolve locally. */
3521 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3524 hash_table
= elf_hash_table (info
);
3525 if (!is_elf_hash_table (&hash_table
->root
))
3528 /* STV_PROTECTED symbols with indirect external access are local. */
3529 if (info
->indirect_extern_access
> 0)
3532 bed
= get_elf_backend_data (hash_table
->dynobj
);
3534 /* If extern_protected_data is false, STV_PROTECTED non-function
3535 symbols are local. */
3536 if ((!info
->extern_protected_data
3537 || (info
->extern_protected_data
< 0
3538 && !bed
->extern_protected_data
))
3539 && !bed
->is_function_type (h
->type
))
3542 /* Function pointer equality tests may require that STV_PROTECTED
3543 symbols be treated as dynamic symbols. If the address of a
3544 function not defined in an executable is set to that function's
3545 plt entry in the executable, then the address of the function in
3546 a shared library must also be the plt entry in the executable. */
3547 return local_protected
;
3550 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3551 aligned. Returns the first TLS output section. */
3553 struct bfd_section
*
3554 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3556 struct bfd_section
*sec
, *tls
;
3557 unsigned int align
= 0;
3559 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3560 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3564 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3565 if (sec
->alignment_power
> align
)
3566 align
= sec
->alignment_power
;
3568 elf_hash_table (info
)->tls_sec
= tls
;
3570 /* Ensure the alignment of the first section (usually .tdata) is the largest
3571 alignment, so that the tls segment starts aligned. */
3573 tls
->alignment_power
= align
;
3578 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3580 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3581 Elf_Internal_Sym
*sym
)
3583 const struct elf_backend_data
*bed
;
3585 /* Local symbols do not count, but target specific ones might. */
3586 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3587 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3590 bed
= get_elf_backend_data (abfd
);
3591 /* Function symbols do not count. */
3592 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3595 /* If the section is undefined, then so is the symbol. */
3596 if (sym
->st_shndx
== SHN_UNDEF
)
3599 /* If the symbol is defined in the common section, then
3600 it is a common definition and so does not count. */
3601 if (bed
->common_definition (sym
))
3604 /* If the symbol is in a target specific section then we
3605 must rely upon the backend to tell us what it is. */
3606 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3607 /* FIXME - this function is not coded yet:
3609 return _bfd_is_global_symbol_definition (abfd, sym);
3611 Instead for now assume that the definition is not global,
3612 Even if this is wrong, at least the linker will behave
3613 in the same way that it used to do. */
3619 /* Search the symbol table of the archive element of the archive ABFD
3620 whose archive map contains a mention of SYMDEF, and determine if
3621 the symbol is defined in this element. */
3623 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3625 Elf_Internal_Shdr
* hdr
;
3629 Elf_Internal_Sym
*isymbuf
;
3630 Elf_Internal_Sym
*isym
;
3631 Elf_Internal_Sym
*isymend
;
3634 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
, NULL
);
3638 if (! bfd_check_format (abfd
, bfd_object
))
3641 /* Select the appropriate symbol table. If we don't know if the
3642 object file is an IR object, give linker LTO plugin a chance to
3643 get the correct symbol table. */
3644 if (abfd
->plugin_format
== bfd_plugin_yes
3645 || abfd
->plugin_format
== bfd_plugin_yes_unused
3646 #if BFD_SUPPORTS_PLUGINS
3647 || (abfd
->plugin_format
== bfd_plugin_unknown
3648 && bfd_link_plugin_object_p (abfd
))
3652 /* Use the IR symbol table if the object has been claimed by
3654 abfd
= abfd
->plugin_dummy_bfd
;
3655 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3659 if (elf_use_dt_symtab_p (abfd
))
3661 bfd_set_error (bfd_error_wrong_format
);
3665 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3666 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3668 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3671 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3673 /* The sh_info field of the symtab header tells us where the
3674 external symbols start. We don't care about the local symbols. */
3675 if (elf_bad_symtab (abfd
))
3677 extsymcount
= symcount
;
3682 extsymcount
= symcount
- hdr
->sh_info
;
3683 extsymoff
= hdr
->sh_info
;
3686 if (extsymcount
== 0)
3689 /* Read in the symbol table. */
3690 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3692 if (isymbuf
== NULL
)
3695 /* Scan the symbol table looking for SYMDEF. */
3697 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3701 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3706 if (strcmp (name
, symdef
->name
) == 0)
3708 result
= is_global_data_symbol_definition (abfd
, isym
);
3718 /* Add an entry to the .dynamic table. */
3721 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3725 struct elf_link_hash_table
*hash_table
;
3726 const struct elf_backend_data
*bed
;
3728 bfd_size_type newsize
;
3729 bfd_byte
*newcontents
;
3730 Elf_Internal_Dyn dyn
;
3732 hash_table
= elf_hash_table (info
);
3733 if (! is_elf_hash_table (&hash_table
->root
))
3736 if (tag
== DT_RELA
|| tag
== DT_REL
)
3737 hash_table
->dynamic_relocs
= true;
3739 bed
= get_elf_backend_data (hash_table
->dynobj
);
3740 s
= hash_table
->dynamic
;
3741 BFD_ASSERT (s
!= NULL
);
3743 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3744 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3745 if (newcontents
== NULL
)
3749 dyn
.d_un
.d_val
= val
;
3750 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3753 s
->contents
= newcontents
;
3758 /* Strip zero-sized dynamic sections. */
3761 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info
*info
)
3763 struct elf_link_hash_table
*hash_table
;
3764 const struct elf_backend_data
*bed
;
3765 asection
*s
, *sdynamic
, **pp
;
3766 asection
*rela_dyn
, *rel_dyn
;
3767 Elf_Internal_Dyn dyn
;
3768 bfd_byte
*extdyn
, *next
;
3769 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
3770 bool strip_zero_sized
;
3771 bool strip_zero_sized_plt
;
3773 if (bfd_link_relocatable (info
))
3776 hash_table
= elf_hash_table (info
);
3777 if (!is_elf_hash_table (&hash_table
->root
))
3780 if (!hash_table
->dynobj
)
3783 sdynamic
= hash_table
->dynamic
;
3787 bed
= get_elf_backend_data (hash_table
->dynobj
);
3788 swap_dyn_in
= bed
->s
->swap_dyn_in
;
3790 strip_zero_sized
= false;
3791 strip_zero_sized_plt
= false;
3793 /* Strip zero-sized dynamic sections. */
3794 rela_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rela.dyn");
3795 rel_dyn
= bfd_get_section_by_name (info
->output_bfd
, ".rel.dyn");
3796 for (pp
= &info
->output_bfd
->sections
; (s
= *pp
) != NULL
;)
3800 || s
== hash_table
->srelplt
->output_section
3801 || s
== hash_table
->splt
->output_section
))
3804 info
->output_bfd
->section_count
--;
3805 strip_zero_sized
= true;
3810 else if (s
== hash_table
->splt
->output_section
)
3812 s
= hash_table
->splt
;
3813 strip_zero_sized_plt
= true;
3816 s
= hash_table
->srelplt
;
3817 s
->flags
|= SEC_EXCLUDE
;
3818 s
->output_section
= bfd_abs_section_ptr
;
3823 if (strip_zero_sized_plt
&& sdynamic
->size
!= 0)
3824 for (extdyn
= sdynamic
->contents
;
3825 extdyn
< sdynamic
->contents
+ sdynamic
->size
;
3828 next
= extdyn
+ bed
->s
->sizeof_dyn
;
3829 swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3837 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3838 the procedure linkage table (the .plt section) has been
3840 memmove (extdyn
, next
,
3841 sdynamic
->size
- (next
- sdynamic
->contents
));
3846 if (strip_zero_sized
)
3848 /* Regenerate program headers. */
3849 elf_seg_map (info
->output_bfd
) = NULL
;
3850 return _bfd_elf_map_sections_to_segments (info
->output_bfd
, info
,
3857 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
3858 1 if a DT_NEEDED tag already exists, and 0 on success. */
3861 bfd_elf_add_dt_needed_tag (bfd
*abfd
, struct bfd_link_info
*info
)
3863 struct elf_link_hash_table
*hash_table
;
3867 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3870 hash_table
= elf_hash_table (info
);
3871 soname
= elf_dt_name (abfd
);
3872 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, false);
3873 if (strindex
== (size_t) -1)
3876 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3879 const struct elf_backend_data
*bed
;
3882 bed
= get_elf_backend_data (hash_table
->dynobj
);
3883 sdyn
= hash_table
->dynamic
;
3884 if (sdyn
!= NULL
&& sdyn
->size
!= 0)
3885 for (extdyn
= sdyn
->contents
;
3886 extdyn
< sdyn
->contents
+ sdyn
->size
;
3887 extdyn
+= bed
->s
->sizeof_dyn
)
3889 Elf_Internal_Dyn dyn
;
3891 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3892 if (dyn
.d_tag
== DT_NEEDED
3893 && dyn
.d_un
.d_val
== strindex
)
3895 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3901 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3904 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3910 /* Return true if SONAME is on the needed list between NEEDED and STOP
3911 (or the end of list if STOP is NULL), and needed by a library that
3915 on_needed_list (const char *soname
,
3916 struct bfd_link_needed_list
*needed
,
3917 struct bfd_link_needed_list
*stop
)
3919 struct bfd_link_needed_list
*look
;
3920 for (look
= needed
; look
!= stop
; look
= look
->next
)
3921 if (strcmp (soname
, look
->name
) == 0
3922 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3923 /* If needed by a library that itself is not directly
3924 needed, recursively check whether that library is
3925 indirectly needed. Since we add DT_NEEDED entries to
3926 the end of the list, library dependencies appear after
3927 the library. Therefore search prior to the current
3928 LOOK, preventing possible infinite recursion. */
3929 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3935 /* Sort symbol by value, section, size, and type. */
3937 elf_sort_symbol (const void *arg1
, const void *arg2
)
3939 const struct elf_link_hash_entry
*h1
;
3940 const struct elf_link_hash_entry
*h2
;
3941 bfd_signed_vma vdiff
;
3946 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3947 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3948 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3950 return vdiff
> 0 ? 1 : -1;
3952 sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3956 /* Sort so that sized symbols are selected over zero size symbols. */
3957 vdiff
= h1
->size
- h2
->size
;
3959 return vdiff
> 0 ? 1 : -1;
3961 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3962 if (h1
->type
!= h2
->type
)
3963 return h1
->type
- h2
->type
;
3965 /* If symbols are properly sized and typed, and multiple strong
3966 aliases are not defined in a shared library by the user we
3967 shouldn't get here. Unfortunately linker script symbols like
3968 __bss_start sometimes match a user symbol defined at the start of
3969 .bss without proper size and type. We'd like to preference the
3970 user symbol over reserved system symbols. Sort on leading
3972 n1
= h1
->root
.root
.string
;
3973 n2
= h2
->root
.root
.string
;
3986 /* Final sort on name selects user symbols like '_u' over reserved
3987 system symbols like '_Z' and also will avoid qsort instability. */
3991 /* This function is used to adjust offsets into .dynstr for
3992 dynamic symbols. This is called via elf_link_hash_traverse. */
3995 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3997 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3999 if (h
->dynindx
!= -1)
4000 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
4004 /* Assign string offsets in .dynstr, update all structures referencing
4008 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
4010 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
4011 struct elf_link_local_dynamic_entry
*entry
;
4012 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
4013 bfd
*dynobj
= hash_table
->dynobj
;
4016 const struct elf_backend_data
*bed
;
4019 _bfd_elf_strtab_finalize (dynstr
);
4020 size
= _bfd_elf_strtab_size (dynstr
);
4022 /* Allow the linker to examine the dynsymtab now it's fully populated. */
4024 if (info
->callbacks
->examine_strtab
)
4025 info
->callbacks
->examine_strtab (dynstr
);
4027 bed
= get_elf_backend_data (dynobj
);
4028 sdyn
= hash_table
->dynamic
;
4029 BFD_ASSERT (sdyn
!= NULL
);
4031 /* Update all .dynamic entries referencing .dynstr strings. */
4032 for (extdyn
= sdyn
->contents
;
4033 extdyn
< PTR_ADD (sdyn
->contents
, sdyn
->size
);
4034 extdyn
+= bed
->s
->sizeof_dyn
)
4036 Elf_Internal_Dyn dyn
;
4038 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
4042 dyn
.d_un
.d_val
= size
;
4052 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
4057 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
4060 /* Now update local dynamic symbols. */
4061 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
4062 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
4063 entry
->isym
.st_name
);
4065 /* And the rest of dynamic symbols. */
4066 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
4068 /* Adjust version definitions. */
4069 if (elf_tdata (output_bfd
)->cverdefs
)
4074 Elf_Internal_Verdef def
;
4075 Elf_Internal_Verdaux defaux
;
4077 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
4081 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
4083 p
+= sizeof (Elf_External_Verdef
);
4084 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
4086 for (i
= 0; i
< def
.vd_cnt
; ++i
)
4088 _bfd_elf_swap_verdaux_in (output_bfd
,
4089 (Elf_External_Verdaux
*) p
, &defaux
);
4090 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
4092 _bfd_elf_swap_verdaux_out (output_bfd
,
4093 &defaux
, (Elf_External_Verdaux
*) p
);
4094 p
+= sizeof (Elf_External_Verdaux
);
4097 while (def
.vd_next
);
4100 /* Adjust version references. */
4101 if (elf_tdata (output_bfd
)->verref
)
4106 Elf_Internal_Verneed need
;
4107 Elf_Internal_Vernaux needaux
;
4109 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
4113 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
4115 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
4116 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
4117 (Elf_External_Verneed
*) p
);
4118 p
+= sizeof (Elf_External_Verneed
);
4119 for (i
= 0; i
< need
.vn_cnt
; ++i
)
4121 _bfd_elf_swap_vernaux_in (output_bfd
,
4122 (Elf_External_Vernaux
*) p
, &needaux
);
4123 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
4125 _bfd_elf_swap_vernaux_out (output_bfd
,
4127 (Elf_External_Vernaux
*) p
);
4128 p
+= sizeof (Elf_External_Vernaux
);
4131 while (need
.vn_next
);
4137 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4138 The default is to only match when the INPUT and OUTPUT are exactly
4142 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
4143 const bfd_target
*output
)
4145 return input
== output
;
4148 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4149 This version is used when different targets for the same architecture
4150 are virtually identical. */
4153 _bfd_elf_relocs_compatible (const bfd_target
*input
,
4154 const bfd_target
*output
)
4156 const struct elf_backend_data
*obed
, *ibed
;
4158 if (input
== output
)
4161 ibed
= xvec_get_elf_backend_data (input
);
4162 obed
= xvec_get_elf_backend_data (output
);
4164 if (ibed
->arch
!= obed
->arch
)
4167 /* If both backends are using this function, deem them compatible. */
4168 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
4171 /* Make a special call to the linker "notice" function to tell it that
4172 we are about to handle an as-needed lib, or have finished
4173 processing the lib. */
4176 _bfd_elf_notice_as_needed (bfd
*ibfd
,
4177 struct bfd_link_info
*info
,
4178 enum notice_asneeded_action act
)
4180 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
4183 /* Call ACTION on each relocation in an ELF object file. */
4186 _bfd_elf_link_iterate_on_relocs
4187 (bfd
*abfd
, struct bfd_link_info
*info
,
4188 bool (*action
) (bfd
*, struct bfd_link_info
*, asection
*,
4189 const Elf_Internal_Rela
*))
4191 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4192 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4194 /* If this object is the same format as the output object, and it is
4195 not a shared library, then let the backend look through the
4198 This is required to build global offset table entries and to
4199 arrange for dynamic relocs. It is not required for the
4200 particular common case of linking non PIC code, even when linking
4201 against shared libraries, but unfortunately there is no way of
4202 knowing whether an object file has been compiled PIC or not.
4203 Looking through the relocs is not particularly time consuming.
4204 The problem is that we must either (1) keep the relocs in memory,
4205 which causes the linker to require additional runtime memory or
4206 (2) read the relocs twice from the input file, which wastes time.
4207 This would be a good case for using mmap.
4209 I have no idea how to handle linking PIC code into a file of a
4210 different format. It probably can't be done. */
4211 if ((abfd
->flags
& DYNAMIC
) == 0
4212 && is_elf_hash_table (&htab
->root
)
4213 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4214 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4218 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4220 Elf_Internal_Rela
*internal_relocs
;
4223 /* Don't check relocations in excluded sections. Don't do
4224 anything special with non-loaded, non-alloced sections.
4225 In particular, any relocs in such sections should not
4226 affect GOT and PLT reference counting (ie. we don't
4227 allow them to create GOT or PLT entries), there's no
4228 possibility or desire to optimize TLS relocs, and
4229 there's not much point in propagating relocs to shared
4230 libs that the dynamic linker won't relocate. */
4231 if ((o
->flags
& SEC_ALLOC
) == 0
4232 || (o
->flags
& SEC_RELOC
) == 0
4233 || (o
->flags
& SEC_EXCLUDE
) != 0
4234 || o
->reloc_count
== 0
4235 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4236 && (o
->flags
& SEC_DEBUGGING
) != 0)
4237 || bfd_is_abs_section (o
->output_section
))
4240 internal_relocs
= _bfd_elf_link_info_read_relocs
4241 (abfd
, info
, o
, NULL
, NULL
,
4242 _bfd_elf_link_keep_memory (info
));
4243 if (internal_relocs
== NULL
)
4246 ok
= action (abfd
, info
, o
, internal_relocs
);
4248 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4249 free (internal_relocs
);
4259 /* Check relocations in an ELF object file. This is called after
4260 all input files have been opened. */
4263 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
4265 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4266 if (bed
->check_relocs
!= NULL
)
4267 return _bfd_elf_link_iterate_on_relocs (abfd
, info
,
4272 /* An entry in the first definition hash table. */
4274 struct elf_link_first_hash_entry
4276 struct bfd_hash_entry root
;
4277 /* The object of the first definition. */
4281 /* The function to create a new entry in the first definition hash
4284 static struct bfd_hash_entry
*
4285 elf_link_first_hash_newfunc (struct bfd_hash_entry
*entry
,
4286 struct bfd_hash_table
*table
,
4289 struct elf_link_first_hash_entry
*ret
=
4290 (struct elf_link_first_hash_entry
*) entry
;
4292 /* Allocate the structure if it has not already been allocated by a
4295 ret
= (struct elf_link_first_hash_entry
*)
4296 bfd_hash_allocate (table
,
4297 sizeof (struct elf_link_first_hash_entry
));
4301 /* Call the allocation method of the superclass. */
4302 ret
= ((struct elf_link_first_hash_entry
*)
4303 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
,
4308 return (struct bfd_hash_entry
*) ret
;
4311 /* Add the symbol NAME from ABFD to first hash. */
4314 elf_link_add_to_first_hash (bfd
*abfd
, struct bfd_link_info
*info
,
4315 const char *name
, bool copy
)
4317 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
4318 /* Skip if there is no first hash. */
4319 if (htab
->first_hash
== NULL
)
4322 struct elf_link_first_hash_entry
*e
4323 = ((struct elf_link_first_hash_entry
*)
4324 bfd_hash_lookup (htab
->first_hash
, name
, true, copy
));
4326 info
->callbacks
->einfo
4327 (_("%F%P: %pB: failed to add %s to first hash\n"), abfd
, name
);
4329 if (e
->abfd
== NULL
)
4330 /* Store ABFD in abfd. */
4334 /* Add symbols from an ELF object file to the linker hash table. */
4337 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4339 Elf_Internal_Ehdr
*ehdr
;
4340 Elf_Internal_Shdr
*hdr
;
4344 struct elf_link_hash_entry
**sym_hash
;
4346 Elf_External_Versym
*extversym
= NULL
;
4347 Elf_External_Versym
*extversym_end
= NULL
;
4348 Elf_External_Versym
*ever
;
4349 struct elf_link_hash_entry
*weaks
;
4350 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
4351 size_t nondeflt_vers_cnt
= 0;
4352 Elf_Internal_Sym
*isymbuf
= NULL
;
4353 Elf_Internal_Sym
*isym
;
4354 Elf_Internal_Sym
*isymend
;
4355 const struct elf_backend_data
*bed
;
4357 struct elf_link_hash_table
*htab
;
4358 void *alloc_mark
= NULL
;
4359 struct bfd_hash_entry
**old_table
= NULL
;
4360 unsigned int old_size
= 0;
4361 unsigned int old_count
= 0;
4362 void *old_tab
= NULL
;
4364 struct bfd_link_hash_entry
*old_undefs
= NULL
;
4365 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
4366 void *old_strtab
= NULL
;
4371 htab
= elf_hash_table (info
);
4372 bed
= get_elf_backend_data (abfd
);
4374 if (elf_use_dt_symtab_p (abfd
))
4376 bfd_set_error (bfd_error_wrong_format
);
4380 if ((abfd
->flags
& DYNAMIC
) == 0)
4383 if ((abfd
->flags
& BFD_PLUGIN
) != 0
4384 && is_elf_hash_table (&htab
->root
)
4385 && htab
->first_hash
== NULL
)
4387 /* Initialize first_hash for an IR input. */
4388 htab
->first_hash
= (struct bfd_hash_table
*)
4389 bfd_malloc (sizeof (struct bfd_hash_table
));
4390 if (htab
->first_hash
== NULL
4391 || !bfd_hash_table_init
4392 (htab
->first_hash
, elf_link_first_hash_newfunc
,
4393 sizeof (struct elf_link_first_hash_entry
)))
4394 info
->callbacks
->einfo
4395 (_("%F%P: first_hash failed to create: %E\n"));
4402 /* You can't use -r against a dynamic object. Also, there's no
4403 hope of using a dynamic object which does not exactly match
4404 the format of the output file. */
4405 if (bfd_link_relocatable (info
)
4406 || !is_elf_hash_table (&htab
->root
)
4407 || info
->output_bfd
->xvec
!= abfd
->xvec
)
4409 if (bfd_link_relocatable (info
))
4410 bfd_set_error (bfd_error_invalid_operation
);
4412 bfd_set_error (bfd_error_wrong_format
);
4417 ehdr
= elf_elfheader (abfd
);
4418 if (info
->warn_alternate_em
4419 && bed
->elf_machine_code
!= ehdr
->e_machine
4420 && ((bed
->elf_machine_alt1
!= 0
4421 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
4422 || (bed
->elf_machine_alt2
!= 0
4423 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
4425 /* xgettext:c-format */
4426 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
4427 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
4429 /* As a GNU extension, any input sections which are named
4430 .gnu.warning.SYMBOL are treated as warning symbols for the given
4431 symbol. This differs from .gnu.warning sections, which generate
4432 warnings when they are included in an output file. */
4433 /* PR 12761: Also generate this warning when building shared libraries. */
4434 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4438 name
= bfd_section_name (s
);
4439 if (startswith (name
, ".gnu.warning."))
4444 name
+= sizeof ".gnu.warning." - 1;
4446 /* If this is a shared object, then look up the symbol
4447 in the hash table. If it is there, and it is already
4448 been defined, then we will not be using the entry
4449 from this shared object, so we don't need to warn.
4450 FIXME: If we see the definition in a regular object
4451 later on, we will warn, but we shouldn't. The only
4452 fix is to keep track of what warnings we are supposed
4453 to emit, and then handle them all at the end of the
4457 struct elf_link_hash_entry
*h
;
4459 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
4461 /* FIXME: What about bfd_link_hash_common? */
4463 && (h
->root
.type
== bfd_link_hash_defined
4464 || h
->root
.type
== bfd_link_hash_defweak
))
4469 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
4473 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
4478 if (! (_bfd_generic_link_add_one_symbol
4479 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
4480 false, bed
->collect
, NULL
)))
4483 if (bfd_link_executable (info
))
4485 /* Clobber the section size so that the warning does
4486 not get copied into the output file. */
4489 /* Also set SEC_EXCLUDE, so that symbols defined in
4490 the warning section don't get copied to the output. */
4491 s
->flags
|= SEC_EXCLUDE
;
4496 just_syms
= ((s
= abfd
->sections
) != NULL
4497 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
4502 /* If we are creating a shared library, create all the dynamic
4503 sections immediately. We need to attach them to something,
4504 so we attach them to this BFD, provided it is the right
4505 format and is not from ld --just-symbols. Always create the
4506 dynamic sections for -E/--dynamic-list. FIXME: If there
4507 are no input BFD's of the same format as the output, we can't
4508 make a shared library. */
4510 && (bfd_link_pic (info
)
4511 || (!bfd_link_relocatable (info
)
4513 && (info
->export_dynamic
|| info
->dynamic
)))
4514 && is_elf_hash_table (&htab
->root
)
4515 && info
->output_bfd
->xvec
== abfd
->xvec
4516 && !htab
->dynamic_sections_created
)
4518 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
4522 else if (!is_elf_hash_table (&htab
->root
))
4526 const char *soname
= NULL
;
4528 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
4529 const Elf_Internal_Phdr
*phdr
;
4530 struct elf_link_loaded_list
*loaded_lib
;
4532 /* ld --just-symbols and dynamic objects don't mix very well.
4533 ld shouldn't allow it. */
4537 /* If this dynamic lib was specified on the command line with
4538 --as-needed in effect, then we don't want to add a DT_NEEDED
4539 tag unless the lib is actually used. Similary for libs brought
4540 in by another lib's DT_NEEDED. When --no-add-needed is used
4541 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4542 any dynamic library in DT_NEEDED tags in the dynamic lib at
4544 add_needed
= (elf_dyn_lib_class (abfd
)
4545 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
4546 | DYN_NO_NEEDED
)) == 0;
4548 s
= bfd_get_section_by_name (abfd
, ".dynamic");
4549 if (s
!= NULL
&& s
->size
!= 0 && (s
->flags
& SEC_HAS_CONTENTS
) != 0)
4553 unsigned int elfsec
;
4554 unsigned long shlink
;
4556 if (!_bfd_elf_mmap_section_contents (abfd
, s
, &dynbuf
))
4559 _bfd_elf_munmap_section_contents (s
, dynbuf
);
4563 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
4564 if (elfsec
== SHN_BAD
)
4565 goto error_free_dyn
;
4566 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4568 for (extdyn
= dynbuf
;
4569 (size_t) (dynbuf
+ s
->size
- extdyn
) >= bed
->s
->sizeof_dyn
;
4570 extdyn
+= bed
->s
->sizeof_dyn
)
4572 Elf_Internal_Dyn dyn
;
4574 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
4575 if (dyn
.d_tag
== DT_SONAME
)
4577 unsigned int tagv
= dyn
.d_un
.d_val
;
4578 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4580 goto error_free_dyn
;
4582 if (dyn
.d_tag
== DT_NEEDED
)
4584 struct bfd_link_needed_list
*n
, **pn
;
4586 unsigned int tagv
= dyn
.d_un
.d_val
;
4587 size_t amt
= sizeof (struct bfd_link_needed_list
);
4589 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4590 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4591 if (n
== NULL
|| fnm
== NULL
)
4592 goto error_free_dyn
;
4593 amt
= strlen (fnm
) + 1;
4594 anm
= (char *) bfd_alloc (abfd
, amt
);
4596 goto error_free_dyn
;
4597 memcpy (anm
, fnm
, amt
);
4601 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4605 if (dyn
.d_tag
== DT_RUNPATH
)
4607 struct bfd_link_needed_list
*n
, **pn
;
4609 unsigned int tagv
= dyn
.d_un
.d_val
;
4610 size_t amt
= sizeof (struct bfd_link_needed_list
);
4612 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4613 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4614 if (n
== NULL
|| fnm
== NULL
)
4615 goto error_free_dyn
;
4616 amt
= strlen (fnm
) + 1;
4617 anm
= (char *) bfd_alloc (abfd
, amt
);
4619 goto error_free_dyn
;
4620 memcpy (anm
, fnm
, amt
);
4624 for (pn
= & runpath
;
4630 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4631 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
4633 struct bfd_link_needed_list
*n
, **pn
;
4635 unsigned int tagv
= dyn
.d_un
.d_val
;
4636 size_t amt
= sizeof (struct bfd_link_needed_list
);
4638 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
4639 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4640 if (n
== NULL
|| fnm
== NULL
)
4641 goto error_free_dyn
;
4642 amt
= strlen (fnm
) + 1;
4643 anm
= (char *) bfd_alloc (abfd
, amt
);
4645 goto error_free_dyn
;
4646 memcpy (anm
, fnm
, amt
);
4656 if (dyn
.d_tag
== DT_AUDIT
)
4658 unsigned int tagv
= dyn
.d_un
.d_val
;
4659 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
4661 if (dyn
.d_tag
== DT_FLAGS_1
)
4662 elf_tdata (abfd
)->is_pie
= (dyn
.d_un
.d_val
& DF_1_PIE
) != 0;
4665 _bfd_elf_munmap_section_contents (s
, dynbuf
);
4668 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4669 frees all more recently bfd_alloc'd blocks as well. */
4675 struct bfd_link_needed_list
**pn
;
4676 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
4681 /* If we have a PT_GNU_RELRO program header, mark as read-only
4682 all sections contained fully therein. This makes relro
4683 shared library sections appear as they will at run-time. */
4684 phdr
= elf_tdata (abfd
)->phdr
+ elf_elfheader (abfd
)->e_phnum
;
4685 while (phdr
-- > elf_tdata (abfd
)->phdr
)
4686 if (phdr
->p_type
== PT_GNU_RELRO
)
4688 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4690 unsigned int opb
= bfd_octets_per_byte (abfd
, s
);
4692 if ((s
->flags
& SEC_ALLOC
) != 0
4693 && s
->vma
* opb
>= phdr
->p_vaddr
4694 && s
->vma
* opb
+ s
->size
<= phdr
->p_vaddr
+ phdr
->p_memsz
)
4695 s
->flags
|= SEC_READONLY
;
4700 /* We do not want to include any of the sections in a dynamic
4701 object in the output file. We hack by simply clobbering the
4702 list of sections in the BFD. This could be handled more
4703 cleanly by, say, a new section flag; the existing
4704 SEC_NEVER_LOAD flag is not the one we want, because that one
4705 still implies that the section takes up space in the output
4707 bfd_section_list_clear (abfd
);
4709 /* Find the name to use in a DT_NEEDED entry that refers to this
4710 object. If the object has a DT_SONAME entry, we use it.
4711 Otherwise, if the generic linker stuck something in
4712 elf_dt_name, we use that. Otherwise, we just use the file
4714 if (soname
== NULL
|| *soname
== '\0')
4716 soname
= elf_dt_name (abfd
);
4717 if (soname
== NULL
|| *soname
== '\0')
4718 soname
= bfd_get_filename (abfd
);
4721 /* Save the SONAME because sometimes the linker emulation code
4722 will need to know it. */
4723 elf_dt_name (abfd
) = soname
;
4725 /* If we have already included this dynamic object in the
4726 link, just ignore it. There is no reason to include a
4727 particular dynamic object more than once. */
4728 for (loaded_lib
= htab
->dyn_loaded
;
4730 loaded_lib
= loaded_lib
->next
)
4732 if (strcmp (elf_dt_name (loaded_lib
->abfd
), soname
) == 0)
4736 /* Create dynamic sections for backends that require that be done
4737 before setup_gnu_properties. */
4739 && !_bfd_elf_link_create_dynamic_sections (abfd
, info
))
4742 /* Save the DT_AUDIT entry for the linker emulation code. */
4743 elf_dt_audit (abfd
) = audit
;
4746 /* If this is a dynamic object, we always link against the .dynsym
4747 symbol table, not the .symtab symbol table. The dynamic linker
4748 will only see the .dynsym symbol table, so there is no reason to
4749 look at .symtab for a dynamic object. */
4751 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
4752 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4754 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4756 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
4758 /* The sh_info field of the symtab header tells us where the
4759 external symbols start. We don't care about the local symbols at
4761 if (elf_bad_symtab (abfd
))
4763 extsymcount
= symcount
;
4768 extsymcount
= symcount
- hdr
->sh_info
;
4769 extsymoff
= hdr
->sh_info
;
4772 sym_hash
= elf_sym_hashes (abfd
);
4773 if (extsymcount
!= 0)
4775 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
4777 if (isymbuf
== NULL
)
4780 if (sym_hash
== NULL
)
4782 /* We store a pointer to the hash table entry for each
4784 size_t amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4785 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
4786 if (sym_hash
== NULL
)
4787 goto error_free_sym
;
4788 elf_sym_hashes (abfd
) = sym_hash
;
4794 /* Read in any version definitions. */
4795 if (!_bfd_elf_slurp_version_tables (abfd
,
4796 info
->default_imported_symver
))
4797 goto error_free_sym
;
4799 /* Read in the symbol versions, but don't bother to convert them
4800 to internal format. */
4801 if (elf_dynversym (abfd
) != 0)
4803 Elf_Internal_Shdr
*versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4804 bfd_size_type amt
= versymhdr
->sh_size
;
4806 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0)
4807 goto error_free_sym
;
4808 extversym
= (Elf_External_Versym
*)
4809 _bfd_malloc_and_read (abfd
, amt
, amt
);
4810 if (extversym
== NULL
)
4811 goto error_free_sym
;
4812 extversym_end
= extversym
+ amt
/ sizeof (*extversym
);
4816 /* If we are loading an as-needed shared lib, save the symbol table
4817 state before we start adding symbols. If the lib turns out
4818 to be unneeded, restore the state. */
4819 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4824 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4826 struct bfd_hash_entry
*p
;
4827 struct elf_link_hash_entry
*h
;
4829 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4831 h
= (struct elf_link_hash_entry
*) p
;
4832 entsize
+= htab
->root
.table
.entsize
;
4833 if (h
->root
.type
== bfd_link_hash_warning
)
4835 entsize
+= htab
->root
.table
.entsize
;
4836 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4838 if (h
->root
.type
== bfd_link_hash_common
)
4839 entsize
+= sizeof (*h
->root
.u
.c
.p
);
4843 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4844 old_tab
= bfd_malloc (tabsize
+ entsize
);
4845 if (old_tab
== NULL
)
4846 goto error_free_vers
;
4848 /* Remember the current objalloc pointer, so that all mem for
4849 symbols added can later be reclaimed. */
4850 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4851 if (alloc_mark
== NULL
)
4852 goto error_free_vers
;
4854 /* Make a special call to the linker "notice" function to
4855 tell it that we are about to handle an as-needed lib. */
4856 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4857 goto error_free_vers
;
4859 /* Clone the symbol table. Remember some pointers into the
4860 symbol table, and dynamic symbol count. */
4861 old_ent
= (char *) old_tab
+ tabsize
;
4862 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4863 old_undefs
= htab
->root
.undefs
;
4864 old_undefs_tail
= htab
->root
.undefs_tail
;
4865 old_table
= htab
->root
.table
.table
;
4866 old_size
= htab
->root
.table
.size
;
4867 old_count
= htab
->root
.table
.count
;
4869 if (htab
->dynstr
!= NULL
)
4871 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4872 if (old_strtab
== NULL
)
4873 goto error_free_vers
;
4876 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4878 struct bfd_hash_entry
*p
;
4879 struct elf_link_hash_entry
*h
;
4881 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4883 h
= (struct elf_link_hash_entry
*) p
;
4884 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4885 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4886 if (h
->root
.type
== bfd_link_hash_warning
)
4888 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4889 memcpy (old_ent
, h
, htab
->root
.table
.entsize
);
4890 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4892 if (h
->root
.type
== bfd_link_hash_common
)
4894 memcpy (old_ent
, h
->root
.u
.c
.p
, sizeof (*h
->root
.u
.c
.p
));
4895 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
4902 if (extversym
== NULL
)
4904 else if (extversym
+ extsymoff
< extversym_end
)
4905 ever
= extversym
+ extsymoff
;
4908 /* xgettext:c-format */
4909 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4910 abfd
, (long) extsymoff
,
4911 (long) (extversym_end
- extversym
) / sizeof (* extversym
));
4912 bfd_set_error (bfd_error_bad_value
);
4913 goto error_free_vers
;
4916 if (!bfd_link_relocatable (info
)
4917 && bfd_get_lto_type (abfd
) == lto_slim_ir_object
)
4920 (_("%pB: plugin needed to handle lto object"), abfd
);
4923 for (isym
= isymbuf
, isymend
= PTR_ADD (isymbuf
, extsymcount
);
4925 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4929 asection
*sec
, *new_sec
;
4932 bool must_copy_name
= false;
4933 struct elf_link_hash_entry
*h
;
4934 struct elf_link_hash_entry
*hi
;
4936 bool size_change_ok
;
4937 bool type_change_ok
;
4943 unsigned int old_alignment
;
4944 unsigned int shindex
;
4950 flags
= BSF_NO_FLAGS
;
4952 value
= isym
->st_value
;
4953 common
= bed
->common_definition (isym
);
4954 if (common
&& info
->inhibit_common_definition
)
4956 /* Treat common symbol as undefined for --no-define-common. */
4957 isym
->st_shndx
= SHN_UNDEF
;
4962 bind
= ELF_ST_BIND (isym
->st_info
);
4966 /* This should be impossible, since ELF requires that all
4967 global symbols follow all local symbols, and that sh_info
4968 point to the first global symbol. Unfortunately, Irix 5
4970 if (elf_bad_symtab (abfd
))
4973 /* If we aren't prepared to handle locals within the globals
4974 then we'll likely segfault on a NULL symbol hash if the
4975 symbol is ever referenced in relocations. */
4976 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
4977 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, hdr
->sh_name
);
4978 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4979 " (>= sh_info of %lu)"),
4980 abfd
, name
, (long) (isym
- isymbuf
+ extsymoff
),
4983 /* Dynamic object relocations are not processed by ld, so
4984 ld won't run into the problem mentioned above. */
4987 bfd_set_error (bfd_error_bad_value
);
4988 goto error_free_vers
;
4991 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4999 case STB_GNU_UNIQUE
:
5000 flags
= BSF_GNU_UNIQUE
;
5004 /* Leave it up to the processor backend. */
5008 if (isym
->st_shndx
== SHN_UNDEF
)
5009 sec
= bfd_und_section_ptr
;
5010 else if (isym
->st_shndx
== SHN_ABS
)
5011 sec
= bfd_abs_section_ptr
;
5012 else if (isym
->st_shndx
== SHN_COMMON
)
5014 sec
= bfd_com_section_ptr
;
5015 /* What ELF calls the size we call the value. What ELF
5016 calls the value we call the alignment. */
5017 value
= isym
->st_size
;
5021 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
5023 sec
= bfd_abs_section_ptr
;
5024 else if (discarded_section (sec
))
5026 /* Symbols from discarded section are undefined. We keep
5028 sec
= bfd_und_section_ptr
;
5030 isym
->st_shndx
= SHN_UNDEF
;
5032 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
5036 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5039 goto error_free_vers
;
5041 if (isym
->st_shndx
== SHN_COMMON
5042 && (abfd
->flags
& BFD_PLUGIN
) != 0)
5044 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
5048 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
5050 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
5052 goto error_free_vers
;
5056 else if (isym
->st_shndx
== SHN_COMMON
5057 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
5058 && !bfd_link_relocatable (info
))
5060 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
5064 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
5065 | SEC_LINKER_CREATED
);
5066 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
5068 goto error_free_vers
;
5072 else if (bed
->elf_add_symbol_hook
)
5074 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
5076 goto error_free_vers
;
5078 /* The hook function sets the name to NULL if this symbol
5079 should be skipped for some reason. */
5084 /* Sanity check that all possibilities were handled. */
5088 /* Silently discard TLS symbols from --just-syms. There's
5089 no way to combine a static TLS block with a new TLS block
5090 for this executable. */
5091 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
5092 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
5095 if (bfd_is_und_section (sec
)
5096 || bfd_is_com_section (sec
))
5101 size_change_ok
= false;
5102 type_change_ok
= bed
->type_change_ok
;
5109 if (is_elf_hash_table (&htab
->root
))
5111 Elf_Internal_Versym iver
;
5112 unsigned int vernum
= 0;
5117 if (info
->default_imported_symver
)
5118 /* Use the default symbol version created earlier. */
5119 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
5123 else if (ever
>= extversym_end
)
5125 /* xgettext:c-format */
5126 _bfd_error_handler (_("%pB: not enough version information"),
5128 bfd_set_error (bfd_error_bad_value
);
5129 goto error_free_vers
;
5132 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
5134 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
5136 /* If this is a hidden symbol, or if it is not version
5137 1, we append the version name to the symbol name.
5138 However, we do not modify a non-hidden absolute symbol
5139 if it is not a function, because it might be the version
5140 symbol itself. FIXME: What if it isn't? */
5141 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
5143 && (!bfd_is_abs_section (sec
)
5144 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
5147 size_t namelen
, verlen
, newlen
;
5150 if (isym
->st_shndx
!= SHN_UNDEF
)
5152 if (vernum
> elf_tdata (abfd
)->cverdefs
)
5154 else if (vernum
> 1)
5156 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
5163 /* xgettext:c-format */
5164 (_("%pB: %s: invalid version %u (max %d)"),
5166 elf_tdata (abfd
)->cverdefs
);
5167 bfd_set_error (bfd_error_bad_value
);
5168 goto error_free_vers
;
5173 /* We cannot simply test for the number of
5174 entries in the VERNEED section since the
5175 numbers for the needed versions do not start
5177 Elf_Internal_Verneed
*t
;
5180 for (t
= elf_tdata (abfd
)->verref
;
5184 Elf_Internal_Vernaux
*a
;
5186 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5188 if (a
->vna_other
== vernum
)
5190 verstr
= a
->vna_nodename
;
5200 /* xgettext:c-format */
5201 (_("%pB: %s: invalid needed version %d"),
5202 abfd
, name
, vernum
);
5203 bfd_set_error (bfd_error_bad_value
);
5204 goto error_free_vers
;
5208 namelen
= strlen (name
);
5209 verlen
= strlen (verstr
);
5210 newlen
= namelen
+ verlen
+ 2;
5211 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5212 && isym
->st_shndx
!= SHN_UNDEF
)
5215 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
5216 if (newname
== NULL
)
5217 goto error_free_vers
;
5218 memcpy (newname
, name
, namelen
);
5219 p
= newname
+ namelen
;
5221 /* If this is a defined non-hidden version symbol,
5222 we add another @ to the name. This indicates the
5223 default version of the symbol. */
5224 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
5225 && isym
->st_shndx
!= SHN_UNDEF
)
5227 memcpy (p
, verstr
, verlen
+ 1);
5230 /* Since bfd_hash_alloc is used for "name", the string
5231 must be copied if added to first_hash. The string
5232 memory can be freed when an --as-needed library is
5234 must_copy_name
= true;
5237 /* If this symbol has default visibility and the user has
5238 requested we not re-export it, then mark it as hidden. */
5239 if (!bfd_is_und_section (sec
)
5242 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
5243 isym
->st_other
= (STV_HIDDEN
5244 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
5246 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
5247 sym_hash
, &old_bfd
, &old_weak
,
5248 &old_alignment
, &skip
, &override
,
5249 &type_change_ok
, &size_change_ok
,
5251 goto error_free_vers
;
5257 while (h
->root
.type
== bfd_link_hash_indirect
5258 || h
->root
.type
== bfd_link_hash_warning
)
5259 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5261 /* Override a definition only if the new symbol matches the
5263 if (override
&& matched
)
5266 if (htab
->first_hash
!= NULL
5267 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5268 && h
->root
.non_ir_ref_regular
)
5270 /* When reloading --as-needed shared objects for new
5271 symbols added from IR inputs, if this shared object
5272 has the first definition, use it. */
5273 struct elf_link_first_hash_entry
*e
5274 = ((struct elf_link_first_hash_entry
*)
5275 bfd_hash_lookup (htab
->first_hash
, name
, false,
5277 if (e
!= NULL
&& e
->abfd
== abfd
)
5282 if (h
->versioned
!= unversioned
5283 && elf_tdata (abfd
)->verdef
!= NULL
5286 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
5289 if (! (_bfd_generic_link_add_one_symbol
5290 (info
, override
? override
: abfd
, name
, flags
, sec
, value
,
5291 NULL
, false, bed
->collect
,
5292 (struct bfd_link_hash_entry
**) sym_hash
)))
5293 goto error_free_vers
;
5296 /* We need to make sure that indirect symbol dynamic flags are
5299 while (h
->root
.type
== bfd_link_hash_indirect
5300 || h
->root
.type
== bfd_link_hash_warning
)
5301 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5305 /* Setting the index to -3 tells elf_link_output_extsym that
5306 this symbol is defined in a discarded section. */
5307 if (discarded
&& is_elf_hash_table (&htab
->root
))
5310 new_weak
= (flags
& BSF_WEAK
) != 0;
5314 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
5315 && is_elf_hash_table (&htab
->root
)
5316 && h
->u
.alias
== NULL
)
5318 /* Keep a list of all weak defined non function symbols from
5319 a dynamic object, using the alias field. Later in this
5320 function we will set the alias field to the correct
5321 value. We only put non-function symbols from dynamic
5322 objects on this list, because that happens to be the only
5323 time we need to know the normal symbol corresponding to a
5324 weak symbol, and the information is time consuming to
5325 figure out. If the alias field is not already NULL,
5326 then this symbol was already defined by some previous
5327 dynamic object, and we will be using that previous
5328 definition anyhow. */
5334 /* Set the alignment of a common symbol. */
5335 if ((common
|| bfd_is_com_section (sec
))
5336 && h
->root
.type
== bfd_link_hash_common
)
5341 align
= bfd_log2 (isym
->st_value
);
5344 /* The new symbol is a common symbol in a shared object.
5345 We need to get the alignment from the section. */
5346 align
= new_sec
->alignment_power
;
5348 if (align
> old_alignment
)
5349 h
->root
.u
.c
.p
->alignment_power
= align
;
5351 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
5354 if (is_elf_hash_table (&htab
->root
))
5356 /* Set a flag in the hash table entry indicating the type of
5357 reference or definition we just found. A dynamic symbol
5358 is one which is referenced or defined by both a regular
5359 object and a shared object. */
5360 bool dynsym
= false;
5362 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5363 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5365 /* Except for this flag to track nonweak references. */
5367 && bind
!= STB_WEAK
)
5368 h
->ref_ir_nonweak
= 1;
5375 if (bind
!= STB_WEAK
)
5376 h
->ref_regular_nonweak
= 1;
5393 hi
->ref_dynamic
= 1;
5398 hi
->def_dynamic
= 1;
5402 /* If an indirect symbol has been forced local, don't
5403 make the real symbol dynamic. */
5404 if (h
!= hi
&& hi
->forced_local
)
5408 if (bfd_link_dll (info
)
5418 && weakdef (h
)->dynindx
!= -1))
5422 /* Check to see if we need to add an indirect symbol for
5423 the default name. */
5425 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
5427 && hi
->versioned
== versioned_hidden
))
5428 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
5429 sec
, value
, &old_bfd
, &dynsym
))
5430 goto error_free_vers
;
5432 /* Check the alignment when a common symbol is involved. This
5433 can change when a common symbol is overridden by a normal
5434 definition or a common symbol is ignored due to the old
5435 normal definition. We need to make sure the maximum
5436 alignment is maintained. */
5437 if ((old_alignment
|| common
)
5438 && h
->root
.type
!= bfd_link_hash_common
)
5440 unsigned int common_align
;
5441 unsigned int normal_align
;
5442 unsigned int symbol_align
;
5446 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
5447 || h
->root
.type
== bfd_link_hash_defweak
);
5449 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
5450 if (h
->root
.u
.def
.section
->owner
!= NULL
5451 && (h
->root
.u
.def
.section
->owner
->flags
5452 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
5454 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
5455 if (normal_align
> symbol_align
)
5456 normal_align
= symbol_align
;
5459 normal_align
= symbol_align
;
5463 common_align
= old_alignment
;
5464 common_bfd
= old_bfd
;
5469 common_align
= bfd_log2 (isym
->st_value
);
5471 normal_bfd
= old_bfd
;
5474 if (normal_align
< common_align
)
5476 /* PR binutils/2735 */
5477 if (normal_bfd
== NULL
)
5479 /* xgettext:c-format */
5480 (_("warning: alignment %u of common symbol `%s' in %pB is"
5481 " greater than the alignment (%u) of its section %pA"),
5482 1 << common_align
, name
, common_bfd
,
5483 1 << normal_align
, h
->root
.u
.def
.section
);
5486 /* xgettext:c-format */
5487 (_("warning: alignment %u of normal symbol `%s' in %pB"
5488 " is smaller than %u used by the common definition in %pB"),
5489 1 << normal_align
, name
, normal_bfd
,
5490 1 << common_align
, common_bfd
);
5492 /* PR 30499: make sure that users understand that this warning is serious. */
5494 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
5498 /* Remember the symbol size if it isn't undefined. */
5499 if (isym
->st_size
!= 0
5500 && isym
->st_shndx
!= SHN_UNDEF
5501 && (definition
|| h
->size
== 0))
5504 && h
->size
!= isym
->st_size
5505 && ! size_change_ok
)
5508 /* xgettext:c-format */
5509 (_("warning: size of symbol `%s' changed"
5510 " from %" PRIu64
" in %pB to %" PRIu64
" in %pB"),
5511 name
, (uint64_t) h
->size
, old_bfd
,
5512 (uint64_t) isym
->st_size
, abfd
);
5514 /* PR 30499: make sure that users understand that this warning is serious. */
5516 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5519 h
->size
= isym
->st_size
;
5522 /* If this is a common symbol, then we always want H->SIZE
5523 to be the size of the common symbol. The code just above
5524 won't fix the size if a common symbol becomes larger. We
5525 don't warn about a size change here, because that is
5526 covered by --warn-common. Allow changes between different
5528 if (h
->root
.type
== bfd_link_hash_common
)
5529 h
->size
= h
->root
.u
.c
.size
;
5531 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
5532 && ((definition
&& !new_weak
)
5533 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
5534 || h
->type
== STT_NOTYPE
))
5536 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
5538 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5540 if (type
== STT_GNU_IFUNC
5541 && (abfd
->flags
& DYNAMIC
) != 0)
5544 if (h
->type
!= type
)
5546 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
5547 /* xgettext:c-format */
5549 (_("warning: type of symbol `%s' changed"
5550 " from %d to %d in %pB"),
5551 name
, h
->type
, type
, abfd
);
5557 /* Merge st_other field. */
5558 elf_merge_st_other (abfd
, h
, isym
->st_other
, sec
,
5559 definition
, dynamic
);
5561 /* We don't want to make debug symbol dynamic. */
5563 && (sec
->flags
& SEC_DEBUGGING
)
5564 && !bfd_link_relocatable (info
))
5567 /* Nor should we make plugin symbols dynamic. */
5568 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
5573 h
->target_internal
= isym
->st_target_internal
;
5574 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
5577 /* Don't add indirect symbols for .symver x, x@FOO aliases
5578 in IR. Since all data or text symbols in IR have the
5579 same type, value and section, we can't tell if a symbol
5580 is an alias of another symbol by their types, values and
5584 && (abfd
->flags
& BFD_PLUGIN
) == 0)
5586 char *p
= strchr (name
, ELF_VER_CHR
);
5587 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
5589 /* Queue non-default versions so that .symver x, x@FOO
5590 aliases can be checked. */
5593 size_t amt
= ((isymend
- isym
+ 1)
5594 * sizeof (struct elf_link_hash_entry
*));
5596 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
5598 goto error_free_vers
;
5600 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
5604 if (dynsym
&& h
->dynindx
== -1)
5606 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5607 goto error_free_vers
;
5609 && weakdef (h
)->dynindx
== -1)
5611 if (!bfd_elf_link_record_dynamic_symbol (info
, weakdef (h
)))
5612 goto error_free_vers
;
5615 else if (h
->dynindx
!= -1)
5616 /* If the symbol already has a dynamic index, but
5617 visibility says it should not be visible, turn it into
5619 switch (ELF_ST_VISIBILITY (h
->other
))
5623 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
5631 && h
->root
.type
!= bfd_link_hash_indirect
)
5634 && h
->ref_regular_nonweak
)
5636 && (old_bfd
->flags
& BFD_PLUGIN
) != 0
5637 && h
->ref_ir_nonweak
5638 && !info
->lto_all_symbols_read
)
5639 || (h
->ref_dynamic_nonweak
5640 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
5641 && !on_needed_list (elf_dt_name (abfd
),
5642 htab
->needed
, NULL
)))
5644 const char *soname
= elf_dt_name (abfd
);
5646 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
5647 h
->root
.root
.string
);
5649 /* A symbol from a library loaded via DT_NEEDED of some
5650 other library is referenced by a regular object.
5651 Add a DT_NEEDED entry for it. Issue an error if
5652 --no-add-needed is used and the reference was not
5655 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
5658 /* xgettext:c-format */
5659 (_("%pB: undefined reference to symbol '%s'"),
5661 bfd_set_error (bfd_error_missing_dso
);
5662 goto error_free_vers
;
5665 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
5666 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
5668 /* Create dynamic sections for backends that require
5669 that be done before setup_gnu_properties. */
5670 if (!_bfd_elf_link_create_dynamic_sections (abfd
, info
))
5671 goto error_free_vers
;
5675 && h
->root
.u
.def
.section
->owner
== abfd
)
5676 /* Add this symbol to first hash if this shared
5677 object has the first definition. */
5678 elf_link_add_to_first_hash (abfd
, info
, name
, must_copy_name
);
5683 if (info
->lto_plugin_active
5684 && !bfd_link_relocatable (info
)
5685 && (abfd
->flags
& BFD_PLUGIN
) == 0
5688 && is_elf_hash_table (&htab
->root
))
5692 if (bed
->s
->arch_size
== 32)
5697 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5698 referenced in regular objects so that linker plugin will get
5699 the correct symbol resolution. */
5701 sym_hash
= elf_sym_hashes (abfd
);
5702 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5704 Elf_Internal_Rela
*internal_relocs
;
5705 Elf_Internal_Rela
*rel
, *relend
;
5707 /* Don't check relocations in excluded sections. */
5708 if ((s
->flags
& SEC_RELOC
) == 0
5709 || s
->reloc_count
== 0
5710 || (s
->flags
& SEC_EXCLUDE
) != 0
5711 || (s
->flags
& SEC_DEBUGGING
) != 0)
5714 internal_relocs
= _bfd_elf_link_info_read_relocs
5715 (abfd
, info
, s
, NULL
, NULL
,
5716 _bfd_elf_link_keep_memory (info
));
5717 if (internal_relocs
== NULL
)
5718 goto error_free_vers
;
5720 rel
= internal_relocs
;
5721 relend
= rel
+ s
->reloc_count
;
5722 for ( ; rel
< relend
; rel
++)
5724 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
5725 struct elf_link_hash_entry
*h
;
5727 /* Skip local symbols. */
5728 if (r_symndx
< extsymoff
)
5731 h
= sym_hash
[r_symndx
- extsymoff
];
5733 h
->root
.non_ir_ref_regular
= 1;
5736 if (elf_section_data (s
)->relocs
!= internal_relocs
)
5737 free (internal_relocs
);
5746 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
5750 /* Restore the symbol table. */
5751 old_ent
= (char *) old_tab
+ tabsize
;
5752 memset (elf_sym_hashes (abfd
), 0,
5753 extsymcount
* sizeof (struct elf_link_hash_entry
*));
5754 htab
->root
.table
.table
= old_table
;
5755 htab
->root
.table
.size
= old_size
;
5756 htab
->root
.table
.count
= old_count
;
5757 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
5758 htab
->root
.undefs
= old_undefs
;
5759 htab
->root
.undefs_tail
= old_undefs_tail
;
5760 if (htab
->dynstr
!= NULL
)
5761 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
5764 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
5766 struct bfd_hash_entry
*p
;
5767 struct elf_link_hash_entry
*h
;
5768 unsigned int non_ir_ref_dynamic
;
5770 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
5772 /* Preserve non_ir_ref_dynamic so that this symbol
5773 will be exported when the dynamic lib becomes needed
5774 in the second pass. */
5775 h
= (struct elf_link_hash_entry
*) p
;
5776 if (h
->root
.type
== bfd_link_hash_warning
)
5777 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5778 non_ir_ref_dynamic
= h
->root
.non_ir_ref_dynamic
;
5780 h
= (struct elf_link_hash_entry
*) p
;
5781 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5782 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5783 if (h
->root
.type
== bfd_link_hash_warning
)
5785 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5786 memcpy (h
, old_ent
, htab
->root
.table
.entsize
);
5787 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
5789 if (h
->root
.type
== bfd_link_hash_common
)
5791 memcpy (h
->root
.u
.c
.p
, old_ent
, sizeof (*h
->root
.u
.c
.p
));
5792 old_ent
= (char *) old_ent
+ sizeof (*h
->root
.u
.c
.p
);
5794 h
->root
.non_ir_ref_dynamic
= non_ir_ref_dynamic
;
5798 /* Make a special call to the linker "notice" function to
5799 tell it that symbols added for crefs may need to be removed. */
5800 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
5801 goto error_free_vers
;
5804 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
5806 free (nondeflt_vers
);
5812 if (old_tab
!= NULL
)
5814 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
5815 goto error_free_vers
;
5820 /* Now that all the symbols from this input file are created, if
5821 not performing a relocatable link, handle .symver foo, foo@BAR
5822 such that any relocs against foo become foo@BAR. */
5823 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
5827 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
5829 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
5830 char *shortname
, *p
;
5833 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
5835 || (h
->root
.type
!= bfd_link_hash_defined
5836 && h
->root
.type
!= bfd_link_hash_defweak
))
5839 amt
= p
- h
->root
.root
.string
;
5840 shortname
= (char *) bfd_malloc (amt
+ 1);
5842 goto error_free_vers
;
5843 memcpy (shortname
, h
->root
.root
.string
, amt
);
5844 shortname
[amt
] = '\0';
5846 hi
= (struct elf_link_hash_entry
*)
5847 bfd_link_hash_lookup (&htab
->root
, shortname
,
5848 false, false, false);
5850 && hi
->root
.type
== h
->root
.type
5851 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
5852 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
5854 (*bed
->elf_backend_hide_symbol
) (info
, hi
, true);
5855 hi
->root
.type
= bfd_link_hash_indirect
;
5856 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
5857 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
5858 sym_hash
= elf_sym_hashes (abfd
);
5860 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
5861 if (sym_hash
[symidx
] == hi
)
5863 sym_hash
[symidx
] = h
;
5870 free (nondeflt_vers
);
5871 nondeflt_vers
= NULL
;
5873 /* Now set the alias field correctly for all the weak defined
5874 symbols we found. The only way to do this is to search all the
5875 symbols. Since we only need the information for non functions in
5876 dynamic objects, that's the only time we actually put anything on
5877 the list WEAKS. We need this information so that if a regular
5878 object refers to a symbol defined weakly in a dynamic object, the
5879 real symbol in the dynamic object is also put in the dynamic
5880 symbols; we also must arrange for both symbols to point to the
5881 same memory location. We could handle the general case of symbol
5882 aliasing, but a general symbol alias can only be generated in
5883 assembler code, handling it correctly would be very time
5884 consuming, and other ELF linkers don't handle general aliasing
5888 struct elf_link_hash_entry
**hpp
;
5889 struct elf_link_hash_entry
**hppend
;
5890 struct elf_link_hash_entry
**sorted_sym_hash
;
5891 struct elf_link_hash_entry
*h
;
5892 size_t sym_count
, amt
;
5894 /* Since we have to search the whole symbol list for each weak
5895 defined symbol, search time for N weak defined symbols will be
5896 O(N^2). Binary search will cut it down to O(NlogN). */
5897 amt
= extsymcount
* sizeof (*sorted_sym_hash
);
5898 sorted_sym_hash
= bfd_malloc (amt
);
5899 if (sorted_sym_hash
== NULL
)
5901 sym_hash
= sorted_sym_hash
;
5902 hpp
= elf_sym_hashes (abfd
);
5903 hppend
= hpp
+ extsymcount
;
5905 for (; hpp
< hppend
; hpp
++)
5909 && h
->root
.type
== bfd_link_hash_defined
5910 && !bed
->is_function_type (h
->type
))
5918 qsort (sorted_sym_hash
, sym_count
, sizeof (*sorted_sym_hash
),
5921 while (weaks
!= NULL
)
5923 struct elf_link_hash_entry
*hlook
;
5926 size_t i
, j
, idx
= 0;
5929 weaks
= hlook
->u
.alias
;
5930 hlook
->u
.alias
= NULL
;
5932 if (hlook
->root
.type
!= bfd_link_hash_defined
5933 && hlook
->root
.type
!= bfd_link_hash_defweak
)
5936 slook
= hlook
->root
.u
.def
.section
;
5937 vlook
= hlook
->root
.u
.def
.value
;
5943 bfd_signed_vma vdiff
;
5945 h
= sorted_sym_hash
[idx
];
5946 vdiff
= vlook
- h
->root
.u
.def
.value
;
5953 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5963 /* We didn't find a value/section match. */
5967 /* With multiple aliases, or when the weak symbol is already
5968 strongly defined, we have multiple matching symbols and
5969 the binary search above may land on any of them. Step
5970 one past the matching symbol(s). */
5973 h
= sorted_sym_hash
[idx
];
5974 if (h
->root
.u
.def
.section
!= slook
5975 || h
->root
.u
.def
.value
!= vlook
)
5979 /* Now look back over the aliases. Since we sorted by size
5980 as well as value and section, we'll choose the one with
5981 the largest size. */
5984 h
= sorted_sym_hash
[idx
];
5986 /* Stop if value or section doesn't match. */
5987 if (h
->root
.u
.def
.section
!= slook
5988 || h
->root
.u
.def
.value
!= vlook
)
5990 else if (h
!= hlook
)
5992 struct elf_link_hash_entry
*t
;
5995 hlook
->is_weakalias
= 1;
5997 if (t
->u
.alias
!= NULL
)
5998 while (t
->u
.alias
!= h
)
6002 /* If the weak definition is in the list of dynamic
6003 symbols, make sure the real definition is put
6005 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
6007 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6010 free (sorted_sym_hash
);
6015 /* If the real definition is in the list of dynamic
6016 symbols, make sure the weak definition is put
6017 there as well. If we don't do this, then the
6018 dynamic loader might not merge the entries for the
6019 real definition and the weak definition. */
6020 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
6022 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
6023 goto err_free_sym_hash
;
6030 free (sorted_sym_hash
);
6033 if (bed
->check_directives
6034 && !(*bed
->check_directives
) (abfd
, info
))
6037 /* If this is a non-traditional link, try to optimize the handling
6038 of the .stab/.stabstr sections. */
6040 && ! info
->traditional_format
6041 && is_elf_hash_table (&htab
->root
)
6042 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
6046 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
6047 if (stabstr
!= NULL
)
6049 bfd_size_type string_offset
= 0;
6052 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
6053 if (startswith (stab
->name
, ".stab")
6054 && (!stab
->name
[5] ||
6055 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
6056 && (stab
->flags
& SEC_MERGE
) == 0
6057 && !bfd_is_abs_section (stab
->output_section
))
6059 struct bfd_elf_section_data
*secdata
;
6061 secdata
= elf_section_data (stab
);
6062 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
6063 stabstr
, &secdata
->sec_info
,
6066 if (secdata
->sec_info
)
6067 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
6072 if (dynamic
&& add_needed
)
6074 /* Add this bfd to the loaded list. */
6075 struct elf_link_loaded_list
*n
;
6077 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
6081 n
->next
= htab
->dyn_loaded
;
6082 htab
->dyn_loaded
= n
;
6084 if (dynamic
&& !add_needed
6085 && (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) != 0)
6086 elf_dyn_lib_class (abfd
) |= DYN_NO_NEEDED
;
6093 free (nondeflt_vers
);
6101 /* Return the linker hash table entry of a symbol that might be
6102 satisfied by an archive symbol. Return -1 on error. */
6104 struct bfd_link_hash_entry
*
6105 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
6106 struct bfd_link_info
*info
,
6109 struct bfd_link_hash_entry
*h
;
6113 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
6117 /* If this is a default version (the name contains @@), look up the
6118 symbol again with only one `@' as well as without the version.
6119 The effect is that references to the symbol with and without the
6120 version will be matched by the default symbol in the archive. */
6122 p
= strchr (name
, ELF_VER_CHR
);
6123 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
6125 /* Add this symbol to first hash if this archive has the first
6127 if (is_elf_hash_table (info
->hash
))
6128 elf_link_add_to_first_hash (abfd
, info
, name
, false);
6132 /* First check with only one `@'. */
6133 len
= strlen (name
);
6134 copy
= (char *) bfd_alloc (abfd
, len
);
6136 return (struct bfd_link_hash_entry
*) -1;
6138 first
= p
- name
+ 1;
6139 memcpy (copy
, name
, first
);
6140 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
6142 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
6145 /* We also need to check references to the symbol without the
6147 copy
[first
- 1] = '\0';
6148 h
= bfd_link_hash_lookup (info
->hash
, copy
, false, false, true);
6151 bfd_release (abfd
, copy
);
6155 /* Add symbols from an ELF archive file to the linker hash table. We
6156 don't use _bfd_generic_link_add_archive_symbols because we need to
6157 handle versioned symbols.
6159 Fortunately, ELF archive handling is simpler than that done by
6160 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6161 oddities. In ELF, if we find a symbol in the archive map, and the
6162 symbol is currently undefined, we know that we must pull in that
6165 Unfortunately, we do have to make multiple passes over the symbol
6166 table until nothing further is resolved. */
6169 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6172 unsigned char *included
= NULL
;
6176 const struct elf_backend_data
*bed
;
6177 struct bfd_link_hash_entry
* (*archive_symbol_lookup
)
6178 (bfd
*, struct bfd_link_info
*, const char *);
6180 if (! bfd_has_map (abfd
))
6182 /* An empty archive is a special case. */
6183 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
6185 bfd_set_error (bfd_error_no_armap
);
6189 /* Keep track of all symbols we know to be already defined, and all
6190 files we know to be already included. This is to speed up the
6191 second and subsequent passes. */
6192 c
= bfd_ardata (abfd
)->symdef_count
;
6195 amt
= c
* sizeof (*included
);
6196 included
= (unsigned char *) bfd_zmalloc (amt
);
6197 if (included
== NULL
)
6200 symdefs
= bfd_ardata (abfd
)->symdefs
;
6201 bed
= get_elf_backend_data (abfd
);
6202 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
6215 symdefend
= symdef
+ c
;
6216 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
6218 struct bfd_link_hash_entry
*h
;
6220 struct bfd_link_hash_entry
*undefs_tail
;
6225 if (symdef
->file_offset
== last
)
6231 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
6232 if (h
== (struct bfd_link_hash_entry
*) -1)
6238 if (h
->type
== bfd_link_hash_undefined
)
6240 /* If the archive element has already been loaded then one
6241 of the symbols defined by that element might have been
6242 made undefined due to being in a discarded section. */
6243 if (is_elf_hash_table (info
->hash
)
6244 && ((struct elf_link_hash_entry
*) h
)->indx
== -3)
6247 else if (h
->type
== bfd_link_hash_common
)
6249 /* We currently have a common symbol. The archive map contains
6250 a reference to this symbol, so we may want to include it. We
6251 only want to include it however, if this archive element
6252 contains a definition of the symbol, not just another common
6255 Unfortunately some archivers (including GNU ar) will put
6256 declarations of common symbols into their archive maps, as
6257 well as real definitions, so we cannot just go by the archive
6258 map alone. Instead we must read in the element's symbol
6259 table and check that to see what kind of symbol definition
6261 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
6266 if (h
->type
!= bfd_link_hash_undefweak
)
6267 /* Symbol must be defined. Don't check it again. */
6270 if (!is_elf_hash_table (info
->hash
))
6272 struct elf_link_hash_entry
*eh
6273 = (struct elf_link_hash_entry
*) h
;
6274 /* Ignore the archive if the symbol isn't referenced by a
6275 regular object or isn't defined in a shared object. */
6276 if (!eh
->ref_regular
|| !eh
->def_dynamic
)
6278 /* Ignore the dynamic definition if symbol is first
6279 defined in this archive. */
6280 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
6281 if (htab
->first_hash
== NULL
)
6283 struct elf_link_first_hash_entry
*e
6284 = ((struct elf_link_first_hash_entry
*)
6285 bfd_hash_lookup (htab
->first_hash
, symdef
->name
,
6287 if (e
== NULL
|| e
->abfd
!= abfd
)
6291 /* We need to include this archive member. */
6292 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
,
6294 if (element
== NULL
)
6297 if (! bfd_check_format (element
, bfd_object
))
6300 undefs_tail
= info
->hash
->undefs_tail
;
6302 if (!(*info
->callbacks
6303 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
6305 if (!bfd_link_add_symbols (element
, info
))
6308 /* If there are any new undefined symbols, we need to make
6309 another pass through the archive in order to see whether
6310 they can be defined. FIXME: This isn't perfect, because
6311 common symbols wind up on undefs_tail and because an
6312 undefined symbol which is defined later on in this pass
6313 does not require another pass. This isn't a bug, but it
6314 does make the code less efficient than it could be. */
6315 if (undefs_tail
!= info
->hash
->undefs_tail
)
6318 /* Look backward to mark all symbols from this object file
6319 which we have already seen in this pass. */
6323 included
[mark
] = true;
6328 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
6330 /* We mark subsequent symbols from this object file as we go
6331 on through the loop. */
6332 last
= symdef
->file_offset
;
6345 /* Given an ELF BFD, add symbols to the global hash table as
6349 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
6351 switch (bfd_get_format (abfd
))
6354 return elf_link_add_object_symbols (abfd
, info
);
6356 return elf_link_add_archive_symbols (abfd
, info
);
6358 bfd_set_error (bfd_error_wrong_format
);
6363 struct hash_codes_info
6365 unsigned long *hashcodes
;
6369 /* This function will be called though elf_link_hash_traverse to store
6370 all hash value of the exported symbols in an array. */
6373 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6375 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
6380 /* Ignore indirect symbols. These are added by the versioning code. */
6381 if (h
->dynindx
== -1)
6384 name
= h
->root
.root
.string
;
6385 if (h
->versioned
>= versioned
)
6387 char *p
= strchr (name
, ELF_VER_CHR
);
6390 alc
= (char *) bfd_malloc (p
- name
+ 1);
6396 memcpy (alc
, name
, p
- name
);
6397 alc
[p
- name
] = '\0';
6402 /* Compute the hash value. */
6403 ha
= bfd_elf_hash (name
);
6405 /* Store the found hash value in the array given as the argument. */
6406 *(inf
->hashcodes
)++ = ha
;
6408 /* And store it in the struct so that we can put it in the hash table
6410 h
->u
.elf_hash_value
= ha
;
6416 struct collect_gnu_hash_codes
6419 const struct elf_backend_data
*bed
;
6420 unsigned long int nsyms
;
6421 unsigned long int maskbits
;
6422 unsigned long int *hashcodes
;
6423 unsigned long int *hashval
;
6424 unsigned long int *indx
;
6425 unsigned long int *counts
;
6429 long int min_dynindx
;
6430 unsigned long int bucketcount
;
6431 unsigned long int symindx
;
6432 long int local_indx
;
6433 long int shift1
, shift2
;
6434 unsigned long int mask
;
6438 /* This function will be called though elf_link_hash_traverse to store
6439 all hash value of the exported symbols in an array. */
6442 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
6444 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6449 /* Ignore indirect symbols. These are added by the versioning code. */
6450 if (h
->dynindx
== -1)
6453 /* Ignore also local symbols and undefined symbols. */
6454 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6457 name
= h
->root
.root
.string
;
6458 if (h
->versioned
>= versioned
)
6460 char *p
= strchr (name
, ELF_VER_CHR
);
6463 alc
= (char *) bfd_malloc (p
- name
+ 1);
6469 memcpy (alc
, name
, p
- name
);
6470 alc
[p
- name
] = '\0';
6475 /* Compute the hash value. */
6476 ha
= bfd_elf_gnu_hash (name
);
6478 /* Store the found hash value in the array for compute_bucket_count,
6479 and also for .dynsym reordering purposes. */
6480 s
->hashcodes
[s
->nsyms
] = ha
;
6481 s
->hashval
[h
->dynindx
] = ha
;
6483 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
6484 s
->min_dynindx
= h
->dynindx
;
6490 /* This function will be called though elf_link_hash_traverse to do
6491 final dynamic symbol renumbering in case of .gnu.hash.
6492 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6493 to the translation table. */
6496 elf_gnu_hash_process_symidx (struct elf_link_hash_entry
*h
, void *data
)
6498 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
6499 unsigned long int bucket
;
6500 unsigned long int val
;
6502 /* Ignore indirect symbols. */
6503 if (h
->dynindx
== -1)
6506 /* Ignore also local symbols and undefined symbols. */
6507 if (! (*s
->bed
->elf_hash_symbol
) (h
))
6509 if (h
->dynindx
>= s
->min_dynindx
)
6511 if (s
->bed
->record_xhash_symbol
!= NULL
)
6513 (*s
->bed
->record_xhash_symbol
) (h
, 0);
6517 h
->dynindx
= s
->local_indx
++;
6522 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
6523 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
6524 & ((s
->maskbits
>> s
->shift1
) - 1);
6525 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
6527 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
6528 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
6529 if (s
->counts
[bucket
] == 1)
6530 /* Last element terminates the chain. */
6532 bfd_put_32 (s
->output_bfd
, val
,
6533 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
6534 --s
->counts
[bucket
];
6535 if (s
->bed
->record_xhash_symbol
!= NULL
)
6537 bfd_vma xlat_loc
= s
->xlat
+ (s
->indx
[bucket
]++ - s
->symindx
) * 4;
6539 (*s
->bed
->record_xhash_symbol
) (h
, xlat_loc
);
6542 h
->dynindx
= s
->indx
[bucket
]++;
6546 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6549 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
6551 return !(h
->forced_local
6552 || h
->root
.type
== bfd_link_hash_undefined
6553 || h
->root
.type
== bfd_link_hash_undefweak
6554 || ((h
->root
.type
== bfd_link_hash_defined
6555 || h
->root
.type
== bfd_link_hash_defweak
)
6556 && h
->root
.u
.def
.section
->output_section
== NULL
));
6559 /* Array used to determine the number of hash table buckets to use
6560 based on the number of symbols there are. If there are fewer than
6561 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6562 fewer than 37 we use 17 buckets, and so forth. We never use more
6563 than 32771 buckets. */
6565 static const size_t elf_buckets
[] =
6567 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6571 /* Compute bucket count for hashing table. We do not use a static set
6572 of possible tables sizes anymore. Instead we determine for all
6573 possible reasonable sizes of the table the outcome (i.e., the
6574 number of collisions etc) and choose the best solution. The
6575 weighting functions are not too simple to allow the table to grow
6576 without bounds. Instead one of the weighting factors is the size.
6577 Therefore the result is always a good payoff between few collisions
6578 (= short chain lengths) and table size. */
6580 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6581 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
6582 unsigned long int nsyms
,
6585 size_t best_size
= 0;
6586 unsigned long int i
;
6592 uint64_t best_chlen
= ~((uint64_t) 0);
6593 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
6594 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
6595 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
6596 unsigned long int *counts
;
6598 unsigned int no_improvement_count
= 0;
6600 /* Possible optimization parameters: if we have NSYMS symbols we say
6601 that the hashing table must at least have NSYMS/4 and at most
6603 minsize
= nsyms
/ 4;
6606 best_size
= maxsize
= nsyms
* 2;
6611 if ((best_size
& 31) == 0)
6615 /* Create array where we count the collisions in. We must use bfd_malloc
6616 since the size could be large. */
6618 amt
*= sizeof (unsigned long int);
6619 counts
= (unsigned long int *) bfd_malloc (amt
);
6623 /* Compute the "optimal" size for the hash table. The criteria is a
6624 minimal chain length. The minor criteria is (of course) the size
6626 for (i
= minsize
; i
< maxsize
; ++i
)
6628 /* Walk through the array of hashcodes and count the collisions. */
6630 unsigned long int j
;
6631 unsigned long int fact
;
6633 if (gnu_hash
&& (i
& 31) == 0)
6636 memset (counts
, '\0', i
* sizeof (unsigned long int));
6638 /* Determine how often each hash bucket is used. */
6639 for (j
= 0; j
< nsyms
; ++j
)
6640 ++counts
[hashcodes
[j
] % i
];
6642 /* For the weight function we need some information about the
6643 pagesize on the target. This is information need not be 100%
6644 accurate. Since this information is not available (so far) we
6645 define it here to a reasonable default value. If it is crucial
6646 to have a better value some day simply define this value. */
6647 # ifndef BFD_TARGET_PAGESIZE
6648 # define BFD_TARGET_PAGESIZE (4096)
6651 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6653 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
6656 /* Variant 1: optimize for short chains. We add the squares
6657 of all the chain lengths (which favors many small chain
6658 over a few long chains). */
6659 for (j
= 0; j
< i
; ++j
)
6660 max
+= counts
[j
] * counts
[j
];
6662 /* This adds penalties for the overall size of the table. */
6663 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6666 /* Variant 2: Optimize a lot more for small table. Here we
6667 also add squares of the size but we also add penalties for
6668 empty slots (the +1 term). */
6669 for (j
= 0; j
< i
; ++j
)
6670 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
6672 /* The overall size of the table is considered, but not as
6673 strong as in variant 1, where it is squared. */
6674 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
6678 /* Compare with current best results. */
6679 if (max
< best_chlen
)
6683 no_improvement_count
= 0;
6685 /* PR 11843: Avoid futile long searches for the best bucket size
6686 when there are a large number of symbols. */
6687 else if (++no_improvement_count
== 100)
6695 for (i
= 0; elf_buckets
[i
] != 0; i
++)
6697 best_size
= elf_buckets
[i
];
6698 if (nsyms
< elf_buckets
[i
+ 1])
6701 if (gnu_hash
&& best_size
< 2)
6708 /* Size any SHT_GROUP section for ld -r. */
6711 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
6716 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6717 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6718 && (s
= ibfd
->sections
) != NULL
6719 && s
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
6720 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
6725 /* Set a default stack segment size. The value in INFO wins. If it
6726 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6727 undefined it is initialized. */
6730 bfd_elf_stack_segment_size (bfd
*output_bfd
,
6731 struct bfd_link_info
*info
,
6732 const char *legacy_symbol
,
6733 bfd_vma default_size
)
6735 struct elf_link_hash_entry
*h
= NULL
;
6737 /* Look for legacy symbol. */
6739 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
6740 false, false, false);
6741 if (h
&& (h
->root
.type
== bfd_link_hash_defined
6742 || h
->root
.type
== bfd_link_hash_defweak
)
6744 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
6746 /* The symbol has no type if specified on the command line. */
6747 h
->type
= STT_OBJECT
;
6748 if (info
->stacksize
)
6749 /* xgettext:c-format */
6750 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6751 output_bfd
, legacy_symbol
);
6752 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
6753 /* xgettext:c-format */
6754 _bfd_error_handler (_("%pB: %s not absolute"),
6755 output_bfd
, legacy_symbol
);
6757 info
->stacksize
= h
->root
.u
.def
.value
;
6760 if (!info
->stacksize
)
6761 /* If the user didn't set a size, or explicitly inhibit the
6762 size, set it now. */
6763 info
->stacksize
= default_size
;
6765 /* Provide the legacy symbol, if it is referenced. */
6766 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
6767 || h
->root
.type
== bfd_link_hash_undefweak
))
6769 struct bfd_link_hash_entry
*bh
= NULL
;
6771 if (!(_bfd_generic_link_add_one_symbol
6772 (info
, output_bfd
, legacy_symbol
,
6773 BSF_GLOBAL
, bfd_abs_section_ptr
,
6774 info
->stacksize
>= 0 ? info
->stacksize
: 0,
6775 NULL
, false, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
6778 h
= (struct elf_link_hash_entry
*) bh
;
6780 h
->type
= STT_OBJECT
;
6786 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6788 struct elf_gc_sweep_symbol_info
6790 struct bfd_link_info
*info
;
6791 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
6796 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
6799 && (((h
->root
.type
== bfd_link_hash_defined
6800 || h
->root
.type
== bfd_link_hash_defweak
)
6801 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
6802 && h
->root
.u
.def
.section
->gc_mark
))
6803 || h
->root
.type
== bfd_link_hash_undefined
6804 || h
->root
.type
== bfd_link_hash_undefweak
))
6806 struct elf_gc_sweep_symbol_info
*inf
;
6808 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
6809 (*inf
->hide_symbol
) (inf
->info
, h
, true);
6812 h
->ref_regular_nonweak
= 0;
6818 /* Set up the sizes and contents of the ELF dynamic sections. This is
6819 called by the ELF linker emulation before_allocation routine. We
6820 must set the sizes of the sections before the linker sets the
6821 addresses of the various sections. */
6824 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
6827 const char *filter_shlib
,
6829 const char *depaudit
,
6830 const char * const *auxiliary_filters
,
6831 struct bfd_link_info
*info
,
6832 asection
**sinterpptr
)
6835 const struct elf_backend_data
*bed
;
6839 if (!is_elf_hash_table (info
->hash
))
6842 /* Any syms created from now on start with -1 in
6843 got.refcount/offset and plt.refcount/offset. */
6844 elf_hash_table (info
)->init_got_refcount
6845 = elf_hash_table (info
)->init_got_offset
;
6846 elf_hash_table (info
)->init_plt_refcount
6847 = elf_hash_table (info
)->init_plt_offset
;
6849 bed
= get_elf_backend_data (output_bfd
);
6851 /* The backend may have to create some sections regardless of whether
6852 we're dynamic or not. */
6853 if (bed
->elf_backend_early_size_sections
6854 && !bed
->elf_backend_early_size_sections (output_bfd
, info
))
6857 dynobj
= elf_hash_table (info
)->dynobj
;
6859 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6861 struct bfd_elf_version_tree
*verdefs
;
6862 struct elf_info_failed asvinfo
;
6863 struct bfd_elf_version_tree
*t
;
6864 struct bfd_elf_version_expr
*d
;
6868 /* If we are supposed to export all symbols into the dynamic symbol
6869 table (this is not the normal case), then do so. */
6870 if (info
->export_dynamic
6871 || (bfd_link_executable (info
) && info
->dynamic
))
6873 struct elf_info_failed eif
;
6877 elf_link_hash_traverse (elf_hash_table (info
),
6878 _bfd_elf_export_symbol
,
6886 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6888 if (soname_indx
== (size_t) -1
6889 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
6893 soname_indx
= (size_t) -1;
6895 /* Make all global versions with definition. */
6896 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6897 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6898 if (!d
->symver
&& d
->literal
)
6900 const char *verstr
, *name
;
6901 size_t namelen
, verlen
, newlen
;
6902 char *newname
, *p
, leading_char
;
6903 struct elf_link_hash_entry
*newh
;
6905 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6907 namelen
= strlen (name
) + (leading_char
!= '\0');
6909 verlen
= strlen (verstr
);
6910 newlen
= namelen
+ verlen
+ 3;
6912 newname
= (char *) bfd_malloc (newlen
);
6913 if (newname
== NULL
)
6915 newname
[0] = leading_char
;
6916 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6918 /* Check the hidden versioned definition. */
6919 p
= newname
+ namelen
;
6921 memcpy (p
, verstr
, verlen
+ 1);
6922 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6923 newname
, false, false,
6926 || (newh
->root
.type
!= bfd_link_hash_defined
6927 && newh
->root
.type
!= bfd_link_hash_defweak
))
6929 /* Check the default versioned definition. */
6931 memcpy (p
, verstr
, verlen
+ 1);
6932 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6933 newname
, false, false,
6938 /* Mark this version if there is a definition and it is
6939 not defined in a shared object. */
6941 && !newh
->def_dynamic
6942 && (newh
->root
.type
== bfd_link_hash_defined
6943 || newh
->root
.type
== bfd_link_hash_defweak
))
6947 /* Attach all the symbols to their version information. */
6948 asvinfo
.info
= info
;
6949 asvinfo
.failed
= false;
6951 elf_link_hash_traverse (elf_hash_table (info
),
6952 _bfd_elf_link_assign_sym_version
,
6957 if (!info
->allow_undefined_version
)
6959 /* Check if all global versions have a definition. */
6960 bool all_defined
= true;
6961 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6962 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6963 if (d
->literal
&& !d
->symver
&& !d
->script
)
6966 (_("%s: undefined version: %s"),
6967 d
->pattern
, t
->name
);
6968 all_defined
= false;
6973 bfd_set_error (bfd_error_bad_value
);
6978 /* Set up the version definition section. */
6979 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6980 BFD_ASSERT (s
!= NULL
);
6982 /* We may have created additional version definitions if we are
6983 just linking a regular application. */
6984 verdefs
= info
->version_info
;
6986 /* Skip anonymous version tag. */
6987 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6988 verdefs
= verdefs
->next
;
6990 if (verdefs
== NULL
&& !info
->create_default_symver
)
6991 s
->flags
|= SEC_EXCLUDE
;
6997 Elf_Internal_Verdef def
;
6998 Elf_Internal_Verdaux defaux
;
6999 struct bfd_link_hash_entry
*bh
;
7000 struct elf_link_hash_entry
*h
;
7006 /* Make space for the base version. */
7007 size
+= sizeof (Elf_External_Verdef
);
7008 size
+= sizeof (Elf_External_Verdaux
);
7011 /* Make space for the default version. */
7012 if (info
->create_default_symver
)
7014 size
+= sizeof (Elf_External_Verdef
);
7018 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
7020 struct bfd_elf_version_deps
*n
;
7022 /* Don't emit base version twice. */
7026 size
+= sizeof (Elf_External_Verdef
);
7027 size
+= sizeof (Elf_External_Verdaux
);
7030 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
7031 size
+= sizeof (Elf_External_Verdaux
);
7035 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7036 if (s
->contents
== NULL
&& s
->size
!= 0)
7040 /* Fill in the version definition section. */
7044 def
.vd_version
= VER_DEF_CURRENT
;
7045 def
.vd_flags
= VER_FLG_BASE
;
7048 if (info
->create_default_symver
)
7050 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
7051 def
.vd_next
= sizeof (Elf_External_Verdef
);
7055 def
.vd_aux
= sizeof (Elf_External_Verdef
);
7056 def
.vd_next
= (sizeof (Elf_External_Verdef
)
7057 + sizeof (Elf_External_Verdaux
));
7060 if (soname_indx
!= (size_t) -1)
7062 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
7064 def
.vd_hash
= bfd_elf_hash (soname
);
7065 defaux
.vda_name
= soname_indx
;
7072 name
= lbasename (bfd_get_filename (output_bfd
));
7073 def
.vd_hash
= bfd_elf_hash (name
);
7074 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7076 if (indx
== (size_t) -1)
7078 defaux
.vda_name
= indx
;
7080 defaux
.vda_next
= 0;
7082 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
7083 (Elf_External_Verdef
*) p
);
7084 p
+= sizeof (Elf_External_Verdef
);
7085 if (info
->create_default_symver
)
7087 /* Add a symbol representing this version. */
7089 if (! (_bfd_generic_link_add_one_symbol
7090 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
7092 get_elf_backend_data (dynobj
)->collect
, &bh
)))
7094 h
= (struct elf_link_hash_entry
*) bh
;
7097 h
->type
= STT_OBJECT
;
7098 h
->verinfo
.vertree
= NULL
;
7100 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
7103 /* Create a duplicate of the base version with the same
7104 aux block, but different flags. */
7107 def
.vd_aux
= sizeof (Elf_External_Verdef
);
7109 def
.vd_next
= (sizeof (Elf_External_Verdef
)
7110 + sizeof (Elf_External_Verdaux
));
7113 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
7114 (Elf_External_Verdef
*) p
);
7115 p
+= sizeof (Elf_External_Verdef
);
7117 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7118 (Elf_External_Verdaux
*) p
);
7119 p
+= sizeof (Elf_External_Verdaux
);
7121 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
7124 struct bfd_elf_version_deps
*n
;
7126 /* Don't emit the base version twice. */
7131 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
7134 /* Add a symbol representing this version. */
7136 if (! (_bfd_generic_link_add_one_symbol
7137 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
7139 get_elf_backend_data (dynobj
)->collect
, &bh
)))
7141 h
= (struct elf_link_hash_entry
*) bh
;
7144 h
->type
= STT_OBJECT
;
7145 h
->verinfo
.vertree
= t
;
7147 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
7150 def
.vd_version
= VER_DEF_CURRENT
;
7152 if (t
->globals
.list
== NULL
7153 && t
->locals
.list
== NULL
7155 def
.vd_flags
|= VER_FLG_WEAK
;
7156 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
7157 def
.vd_cnt
= cdeps
+ 1;
7158 def
.vd_hash
= bfd_elf_hash (t
->name
);
7159 def
.vd_aux
= sizeof (Elf_External_Verdef
);
7162 /* If a basever node is next, it *must* be the last node in
7163 the chain, otherwise Verdef construction breaks. */
7164 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
7165 BFD_ASSERT (t
->next
->next
== NULL
);
7167 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
7168 def
.vd_next
= (sizeof (Elf_External_Verdef
)
7169 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
7171 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
7172 (Elf_External_Verdef
*) p
);
7173 p
+= sizeof (Elf_External_Verdef
);
7175 defaux
.vda_name
= h
->dynstr_index
;
7176 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
7178 defaux
.vda_next
= 0;
7179 if (t
->deps
!= NULL
)
7180 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
7181 t
->name_indx
= defaux
.vda_name
;
7183 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7184 (Elf_External_Verdaux
*) p
);
7185 p
+= sizeof (Elf_External_Verdaux
);
7187 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
7189 if (n
->version_needed
== NULL
)
7191 /* This can happen if there was an error in the
7193 defaux
.vda_name
= 0;
7197 defaux
.vda_name
= n
->version_needed
->name_indx
;
7198 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
7201 if (n
->next
== NULL
)
7202 defaux
.vda_next
= 0;
7204 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
7206 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
7207 (Elf_External_Verdaux
*) p
);
7208 p
+= sizeof (Elf_External_Verdaux
);
7212 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
7216 if (info
->gc_sections
&& bed
->can_gc_sections
)
7218 struct elf_gc_sweep_symbol_info sweep_info
;
7220 /* Remove the symbols that were in the swept sections from the
7221 dynamic symbol table. */
7222 sweep_info
.info
= info
;
7223 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
7224 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
7228 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7231 struct elf_find_verdep_info sinfo
;
7233 /* Work out the size of the version reference section. */
7235 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
7236 BFD_ASSERT (s
!= NULL
);
7239 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
7240 if (sinfo
.vers
== 0)
7242 sinfo
.failed
= false;
7244 elf_link_hash_traverse (elf_hash_table (info
),
7245 _bfd_elf_link_find_version_dependencies
,
7250 bed
->elf_backend_add_glibc_version_dependency (&sinfo
);
7254 if (elf_tdata (output_bfd
)->verref
== NULL
)
7255 s
->flags
|= SEC_EXCLUDE
;
7258 Elf_Internal_Verneed
*vn
;
7263 /* Build the version dependency section. */
7266 for (vn
= elf_tdata (output_bfd
)->verref
;
7268 vn
= vn
->vn_nextref
)
7270 Elf_Internal_Vernaux
*a
;
7272 size
+= sizeof (Elf_External_Verneed
);
7274 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7275 size
+= sizeof (Elf_External_Vernaux
);
7279 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7280 if (s
->contents
== NULL
)
7285 for (vn
= elf_tdata (output_bfd
)->verref
;
7287 vn
= vn
->vn_nextref
)
7290 Elf_Internal_Vernaux
*a
;
7294 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7297 vn
->vn_version
= VER_NEED_CURRENT
;
7299 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7300 elf_dt_name (vn
->vn_bfd
) != NULL
7301 ? elf_dt_name (vn
->vn_bfd
)
7302 : lbasename (bfd_get_filename
7305 if (indx
== (size_t) -1)
7308 vn
->vn_aux
= sizeof (Elf_External_Verneed
);
7309 if (vn
->vn_nextref
== NULL
)
7312 vn
->vn_next
= (sizeof (Elf_External_Verneed
)
7313 + caux
* sizeof (Elf_External_Vernaux
));
7315 _bfd_elf_swap_verneed_out (output_bfd
, vn
,
7316 (Elf_External_Verneed
*) p
);
7317 p
+= sizeof (Elf_External_Verneed
);
7319 for (a
= vn
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
7321 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
7322 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7323 a
->vna_nodename
, false);
7324 if (indx
== (size_t) -1)
7327 if (a
->vna_nextptr
== NULL
)
7330 a
->vna_next
= sizeof (Elf_External_Vernaux
);
7332 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
7333 (Elf_External_Vernaux
*) p
);
7334 p
+= sizeof (Elf_External_Vernaux
);
7338 elf_tdata (output_bfd
)->cverrefs
= crefs
;
7342 if (bfd_link_relocatable (info
)
7343 && !_bfd_elf_size_group_sections (info
))
7346 /* Determine any GNU_STACK segment requirements, after the backend
7347 has had a chance to set a default segment size. */
7348 if (info
->execstack
)
7350 /* If the user has explicitly requested warnings, then generate one even
7351 though the choice is the result of another command line option. */
7352 if (info
->warn_execstack
== 1)
7354 if (info
->error_execstack
)
7358 error: creating an executable stack because of -z execstack command line option"));
7364 warning: enabling an executable stack because of -z execstack command line option"));
7367 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
7369 else if (info
->noexecstack
)
7370 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
7374 asection
*notesec
= NULL
;
7375 bfd
*noteobj
= NULL
;
7376 bfd
*emptyobj
= NULL
;
7379 for (inputobj
= info
->input_bfds
;
7381 inputobj
= inputobj
->link
.next
)
7386 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
7388 s
= inputobj
->sections
;
7389 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
7392 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
7396 if (s
->flags
& SEC_CODE
)
7400 /* There is no point in scanning the remaining bfds. */
7404 else if (bed
->default_execstack
&& info
->default_execstack
)
7407 emptyobj
= inputobj
;
7411 if (notesec
|| info
->stacksize
> 0)
7415 if (info
->warn_execstack
!= 0)
7417 /* PR 29072: Because an executable stack is a serious
7418 security risk, make sure that the user knows that it is
7419 being enabled despite the fact that it was not requested
7420 on the command line. */
7423 if (info
->error_execstack
)
7425 _bfd_error_handler (_("\
7426 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7427 bfd_get_filename (noteobj
));
7431 _bfd_error_handler (_("\
7432 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7433 bfd_get_filename (noteobj
));
7437 if (info
->error_execstack
)
7439 _bfd_error_handler (_("\
7440 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7441 bfd_get_filename (emptyobj
));
7445 _bfd_error_handler (_("\
7446 warning: %s: missing .note.GNU-stack section implies executable stack"),
7447 bfd_get_filename (emptyobj
));
7448 _bfd_error_handler (_("\
7449 NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7453 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
7456 if (notesec
&& exec
&& bfd_link_relocatable (info
)
7457 && notesec
->output_section
!= bfd_abs_section_ptr
)
7458 notesec
->output_section
->flags
|= SEC_CODE
;
7461 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7463 struct elf_info_failed eif
;
7464 struct elf_link_hash_entry
*h
;
7468 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
7469 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
7473 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
7475 info
->flags
|= DF_SYMBOLIC
;
7483 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
7485 if (indx
== (size_t) -1)
7488 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
7489 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
7493 if (filter_shlib
!= NULL
)
7497 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7498 filter_shlib
, true);
7499 if (indx
== (size_t) -1
7500 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
7504 if (auxiliary_filters
!= NULL
)
7506 const char * const *p
;
7508 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
7512 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
7514 if (indx
== (size_t) -1
7515 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
7524 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
7526 if (indx
== (size_t) -1
7527 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
7531 if (depaudit
!= NULL
)
7535 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
7537 if (indx
== (size_t) -1
7538 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
7545 /* Find all symbols which were defined in a dynamic object and make
7546 the backend pick a reasonable value for them. */
7547 elf_link_hash_traverse (elf_hash_table (info
),
7548 _bfd_elf_adjust_dynamic_symbol
,
7553 /* Add some entries to the .dynamic section. We fill in some of the
7554 values later, in bfd_elf_final_link, but we must add the entries
7555 now so that we know the final size of the .dynamic section. */
7557 /* If there are initialization and/or finalization functions to
7558 call then add the corresponding DT_INIT/DT_FINI entries. */
7559 h
= (info
->init_function
7560 ? elf_link_hash_lookup (elf_hash_table (info
),
7561 info
->init_function
, false,
7568 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
7571 h
= (info
->fini_function
7572 ? elf_link_hash_lookup (elf_hash_table (info
),
7573 info
->fini_function
, false,
7580 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
7584 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
7585 if (s
!= NULL
&& s
->linker_has_input
)
7587 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7588 if (! bfd_link_executable (info
))
7593 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
7594 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
7595 && (o
= sub
->sections
) != NULL
7596 && o
->sec_info_type
!= SEC_INFO_TYPE_JUST_SYMS
)
7597 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
7598 if (elf_section_data (o
)->this_hdr
.sh_type
7599 == SHT_PREINIT_ARRAY
)
7602 (_("%pB: .preinit_array section is not allowed in DSO"),
7607 bfd_set_error (bfd_error_nonrepresentable_section
);
7611 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
7612 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
7615 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
7616 if (s
!= NULL
&& s
->linker_has_input
)
7618 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
7619 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
7622 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
7623 if (s
!= NULL
&& s
->linker_has_input
)
7625 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
7626 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
7630 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
7631 /* If .dynstr is excluded from the link, we don't want any of
7632 these tags. Strictly, we should be checking each section
7633 individually; This quick check covers for the case where
7634 someone does a /DISCARD/ : { *(*) }. */
7635 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
7637 bfd_size_type strsize
;
7639 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
7640 if ((info
->emit_hash
7641 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
7642 || (info
->emit_gnu_hash
7643 && (bed
->record_xhash_symbol
== NULL
7644 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0)))
7645 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
7646 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
7647 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
7648 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
7650 || (info
->gnu_flags_1
7651 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_FLAGS_1
,
7652 info
->gnu_flags_1
)))
7657 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
7660 /* The backend must work out the sizes of all the other dynamic
7662 if (bed
->elf_backend_late_size_sections
!= NULL
7663 && !bed
->elf_backend_late_size_sections (output_bfd
, info
))
7666 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
7668 if (elf_tdata (output_bfd
)->cverdefs
)
7670 unsigned int crefs
= elf_tdata (output_bfd
)->cverdefs
;
7672 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
7673 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, crefs
))
7677 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
7679 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
7682 else if (info
->flags
& DF_BIND_NOW
)
7684 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
7690 if (bfd_link_executable (info
))
7691 info
->flags_1
&= ~ (DF_1_INITFIRST
7694 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
7698 if (elf_tdata (output_bfd
)->cverrefs
)
7700 unsigned int crefs
= elf_tdata (output_bfd
)->cverrefs
;
7702 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
7703 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
7707 if ((elf_tdata (output_bfd
)->cverrefs
== 0
7708 && elf_tdata (output_bfd
)->cverdefs
== 0)
7709 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
, NULL
) <= 1)
7713 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7714 s
->flags
|= SEC_EXCLUDE
;
7720 /* Find the first non-excluded output section. We'll use its
7721 section symbol for some emitted relocs. */
7723 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
7726 asection
*found
= NULL
;
7728 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7729 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7730 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7733 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7736 elf_hash_table (info
)->text_index_section
= found
;
7739 /* Find two non-excluded output sections, one for code, one for data.
7740 We'll use their section symbols for some emitted relocs. */
7742 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
7745 asection
*found
= NULL
;
7747 /* Data first, since setting text_index_section changes
7748 _bfd_elf_omit_section_dynsym_default. */
7749 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7750 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7751 && !(s
->flags
& SEC_READONLY
)
7752 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7755 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
7758 elf_hash_table (info
)->data_index_section
= found
;
7760 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
7761 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
7762 && (s
->flags
& SEC_READONLY
)
7763 && !_bfd_elf_omit_section_dynsym_default (output_bfd
, info
, s
))
7768 elf_hash_table (info
)->text_index_section
= found
;
7771 #define GNU_HASH_SECTION_NAME(bed) \
7772 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7775 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
7777 const struct elf_backend_data
*bed
;
7778 unsigned long section_sym_count
;
7779 bfd_size_type dynsymcount
= 0;
7781 if (!is_elf_hash_table (info
->hash
))
7784 bed
= get_elf_backend_data (output_bfd
);
7785 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
7787 /* Assign dynsym indices. In a shared library we generate a section
7788 symbol for each output section, which come first. Next come all
7789 of the back-end allocated local dynamic syms, followed by the rest
7790 of the global symbols.
7792 This is usually not needed for static binaries, however backends
7793 can request to always do it, e.g. the MIPS backend uses dynamic
7794 symbol counts to lay out GOT, which will be produced in the
7795 presence of GOT relocations even in static binaries (holding fixed
7796 data in that case, to satisfy those relocations). */
7798 if (elf_hash_table (info
)->dynamic_sections_created
7799 || bed
->always_renumber_dynsyms
)
7800 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
7801 §ion_sym_count
);
7803 if (elf_hash_table (info
)->dynamic_sections_created
)
7807 unsigned int dtagcount
;
7809 dynobj
= elf_hash_table (info
)->dynobj
;
7811 /* Work out the size of the symbol version section. */
7812 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
7813 BFD_ASSERT (s
!= NULL
);
7814 if ((s
->flags
& SEC_EXCLUDE
) == 0)
7816 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
7817 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7818 if (s
->contents
== NULL
)
7822 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
7826 /* Set the size of the .dynsym and .hash sections. We counted
7827 the number of dynamic symbols in elf_link_add_object_symbols.
7828 We will build the contents of .dynsym and .hash when we build
7829 the final symbol table, because until then we do not know the
7830 correct value to give the symbols. We built the .dynstr
7831 section as we went along in elf_link_add_object_symbols. */
7832 s
= elf_hash_table (info
)->dynsym
;
7833 BFD_ASSERT (s
!= NULL
);
7834 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
7836 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
7837 if (s
->contents
== NULL
)
7841 /* The first entry in .dynsym is a dummy symbol. Clear all the
7842 section syms, in case we don't output them all. */
7843 ++section_sym_count
;
7844 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
7846 elf_hash_table (info
)->bucketcount
= 0;
7848 /* Compute the size of the hashing table. As a side effect this
7849 computes the hash values for all the names we export. */
7850 if (info
->emit_hash
)
7852 unsigned long int *hashcodes
;
7853 struct hash_codes_info hashinf
;
7855 unsigned long int nsyms
;
7857 size_t hash_entry_size
;
7859 /* Compute the hash values for all exported symbols. At the same
7860 time store the values in an array so that we could use them for
7862 amt
= dynsymcount
* sizeof (unsigned long int);
7863 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
7864 if (hashcodes
== NULL
)
7866 hashinf
.hashcodes
= hashcodes
;
7867 hashinf
.error
= false;
7869 /* Put all hash values in HASHCODES. */
7870 elf_link_hash_traverse (elf_hash_table (info
),
7871 elf_collect_hash_codes
, &hashinf
);
7878 nsyms
= hashinf
.hashcodes
- hashcodes
;
7880 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
7883 if (bucketcount
== 0 && nsyms
> 0)
7886 elf_hash_table (info
)->bucketcount
= bucketcount
;
7888 s
= bfd_get_linker_section (dynobj
, ".hash");
7889 BFD_ASSERT (s
!= NULL
);
7890 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
7891 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
7892 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7893 if (s
->contents
== NULL
)
7897 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
7898 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
7899 s
->contents
+ hash_entry_size
);
7902 if (info
->emit_gnu_hash
)
7905 unsigned char *contents
;
7906 struct collect_gnu_hash_codes cinfo
;
7910 memset (&cinfo
, 0, sizeof (cinfo
));
7912 /* Compute the hash values for all exported symbols. At the same
7913 time store the values in an array so that we could use them for
7915 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
7916 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
7917 if (cinfo
.hashcodes
== NULL
)
7920 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
7921 cinfo
.min_dynindx
= -1;
7922 cinfo
.output_bfd
= output_bfd
;
7925 /* Put all hash values in HASHCODES. */
7926 elf_link_hash_traverse (elf_hash_table (info
),
7927 elf_collect_gnu_hash_codes
, &cinfo
);
7930 free (cinfo
.hashcodes
);
7935 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
7937 if (bucketcount
== 0)
7939 free (cinfo
.hashcodes
);
7943 s
= bfd_get_linker_section (dynobj
, GNU_HASH_SECTION_NAME (bed
));
7944 BFD_ASSERT (s
!= NULL
);
7946 if (cinfo
.nsyms
== 0)
7948 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7949 BFD_ASSERT (cinfo
.min_dynindx
== -1);
7950 free (cinfo
.hashcodes
);
7951 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
7952 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
7953 if (contents
== NULL
)
7955 s
->contents
= contents
;
7957 /* 1 empty bucket. */
7958 bfd_put_32 (output_bfd
, 1, contents
);
7959 /* SYMIDX above the special symbol 0. */
7960 bfd_put_32 (output_bfd
, 1, contents
+ 4);
7961 /* Just one word for bitmask. */
7962 bfd_put_32 (output_bfd
, 1, contents
+ 8);
7963 /* Only hash fn bloom filter. */
7964 bfd_put_32 (output_bfd
, 0, contents
+ 12);
7965 /* No hashes are valid - empty bitmask. */
7966 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
7967 /* No hashes in the only bucket. */
7968 bfd_put_32 (output_bfd
, 0,
7969 contents
+ 16 + bed
->s
->arch_size
/ 8);
7973 unsigned long int maskwords
, maskbitslog2
, x
;
7974 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
7978 while ((x
>>= 1) != 0)
7980 if (maskbitslog2
< 3)
7982 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
7983 maskbitslog2
= maskbitslog2
+ 3;
7985 maskbitslog2
= maskbitslog2
+ 2;
7986 if (bed
->s
->arch_size
== 64)
7988 if (maskbitslog2
== 5)
7994 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
7995 cinfo
.shift2
= maskbitslog2
;
7996 cinfo
.maskbits
= 1 << maskbitslog2
;
7997 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
7998 amt
= bucketcount
* sizeof (unsigned long int) * 2;
7999 amt
+= maskwords
* sizeof (bfd_vma
);
8000 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
8001 if (cinfo
.bitmask
== NULL
)
8003 free (cinfo
.hashcodes
);
8007 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
8008 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
8009 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
8010 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
8012 /* Determine how often each hash bucket is used. */
8013 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
8014 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
8015 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
8017 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
8018 if (cinfo
.counts
[i
] != 0)
8020 cinfo
.indx
[i
] = cnt
;
8021 cnt
+= cinfo
.counts
[i
];
8023 BFD_ASSERT (cnt
== dynsymcount
);
8024 cinfo
.bucketcount
= bucketcount
;
8025 cinfo
.local_indx
= cinfo
.min_dynindx
;
8027 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
8028 s
->size
+= cinfo
.maskbits
/ 8;
8029 if (bed
->record_xhash_symbol
!= NULL
)
8030 s
->size
+= cinfo
.nsyms
* 4;
8031 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
8032 if (contents
== NULL
)
8034 free (cinfo
.bitmask
);
8035 free (cinfo
.hashcodes
);
8039 s
->contents
= contents
;
8041 bfd_put_32 (output_bfd
, bucketcount
, contents
);
8042 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
8043 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
8044 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
8045 contents
+= 16 + cinfo
.maskbits
/ 8;
8047 for (i
= 0; i
< bucketcount
; ++i
)
8049 if (cinfo
.counts
[i
] == 0)
8050 bfd_put_32 (output_bfd
, 0, contents
);
8052 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
8056 cinfo
.contents
= contents
;
8058 cinfo
.xlat
= contents
+ cinfo
.nsyms
* 4 - s
->contents
;
8059 /* Renumber dynamic symbols, if populating .gnu.hash section.
8060 If using .MIPS.xhash, populate the translation table. */
8061 elf_link_hash_traverse (elf_hash_table (info
),
8062 elf_gnu_hash_process_symidx
, &cinfo
);
8064 contents
= s
->contents
+ 16;
8065 for (i
= 0; i
< maskwords
; ++i
)
8067 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
8069 contents
+= bed
->s
->arch_size
/ 8;
8072 free (cinfo
.bitmask
);
8073 free (cinfo
.hashcodes
);
8077 s
= bfd_get_linker_section (dynobj
, ".dynstr");
8078 BFD_ASSERT (s
!= NULL
);
8080 elf_finalize_dynstr (output_bfd
, info
);
8082 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
8084 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
8085 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
8092 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
8095 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
8098 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
8099 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
8102 /* Finish SHF_MERGE section merging. */
8105 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
8110 if (ENABLE_CHECKING
&& !is_elf_hash_table (info
->hash
))
8113 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
8114 if ((ibfd
->flags
& DYNAMIC
) == 0
8115 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
8116 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
8117 == get_elf_backend_data (obfd
)->s
->elfclass
))
8118 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
8119 if ((sec
->flags
& SEC_MERGE
) != 0
8120 && !bfd_is_abs_section (sec
->output_section
))
8122 struct bfd_elf_section_data
*secdata
;
8124 secdata
= elf_section_data (sec
);
8125 if (! _bfd_add_merge_section (obfd
,
8126 &elf_hash_table (info
)->merge_info
,
8127 sec
, &secdata
->sec_info
))
8129 else if (secdata
->sec_info
)
8130 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
8133 if (elf_hash_table (info
)->merge_info
!= NULL
)
8134 return _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
8135 merge_sections_remove_hook
);
8139 /* Create an entry in an ELF linker hash table. */
8141 struct bfd_hash_entry
*
8142 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
8143 struct bfd_hash_table
*table
,
8146 /* Allocate the structure if it has not already been allocated by a
8150 entry
= (struct bfd_hash_entry
*)
8151 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
8156 /* Call the allocation method of the superclass. */
8157 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
8160 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
8161 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
8163 /* Set local fields. */
8166 ret
->got
= htab
->init_got_refcount
;
8167 ret
->plt
= htab
->init_plt_refcount
;
8168 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
8169 - offsetof (struct elf_link_hash_entry
, size
)));
8170 /* Assume that we have been called by a non-ELF symbol reader.
8171 This flag is then reset by the code which reads an ELF input
8172 file. This ensures that a symbol created by a non-ELF symbol
8173 reader will have the flag set correctly. */
8180 /* Copy data from an indirect symbol to its direct symbol, hiding the
8181 old indirect symbol. Also used for copying flags to a weakdef. */
8184 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
8185 struct elf_link_hash_entry
*dir
,
8186 struct elf_link_hash_entry
*ind
)
8188 struct elf_link_hash_table
*htab
;
8190 if (ind
->dyn_relocs
!= NULL
)
8192 if (dir
->dyn_relocs
!= NULL
)
8194 struct elf_dyn_relocs
**pp
;
8195 struct elf_dyn_relocs
*p
;
8197 /* Add reloc counts against the indirect sym to the direct sym
8198 list. Merge any entries against the same section. */
8199 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
8201 struct elf_dyn_relocs
*q
;
8203 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
8204 if (q
->sec
== p
->sec
)
8206 q
->pc_count
+= p
->pc_count
;
8207 q
->count
+= p
->count
;
8214 *pp
= dir
->dyn_relocs
;
8217 dir
->dyn_relocs
= ind
->dyn_relocs
;
8218 ind
->dyn_relocs
= NULL
;
8221 /* Copy down any references that we may have already seen to the
8222 symbol which just became indirect. */
8224 if (dir
->versioned
!= versioned_hidden
)
8225 dir
->ref_dynamic
|= ind
->ref_dynamic
;
8226 dir
->ref_regular
|= ind
->ref_regular
;
8227 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
8228 dir
->non_got_ref
|= ind
->non_got_ref
;
8229 dir
->needs_plt
|= ind
->needs_plt
;
8230 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
8232 if (ind
->root
.type
!= bfd_link_hash_indirect
)
8235 /* Copy over the global and procedure linkage table refcount entries.
8236 These may have been already set up by a check_relocs routine. */
8237 htab
= elf_hash_table (info
);
8238 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
8240 if (dir
->got
.refcount
< 0)
8241 dir
->got
.refcount
= 0;
8242 dir
->got
.refcount
+= ind
->got
.refcount
;
8243 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
8246 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
8248 if (dir
->plt
.refcount
< 0)
8249 dir
->plt
.refcount
= 0;
8250 dir
->plt
.refcount
+= ind
->plt
.refcount
;
8251 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
8254 if (ind
->dynindx
!= -1)
8256 if (dir
->dynindx
!= -1)
8257 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
8258 dir
->dynindx
= ind
->dynindx
;
8259 dir
->dynstr_index
= ind
->dynstr_index
;
8261 ind
->dynstr_index
= 0;
8266 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
8267 struct elf_link_hash_entry
*h
,
8270 /* STT_GNU_IFUNC symbol must go through PLT. */
8271 if (h
->type
!= STT_GNU_IFUNC
)
8273 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
8278 h
->forced_local
= 1;
8279 if (h
->dynindx
!= -1)
8281 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
8284 h
->dynstr_index
= 0;
8289 /* Hide a symbol. */
8292 _bfd_elf_link_hide_symbol (bfd
*output_bfd
,
8293 struct bfd_link_info
*info
,
8294 struct bfd_link_hash_entry
*h
)
8296 if (is_elf_hash_table (info
->hash
))
8298 const struct elf_backend_data
*bed
8299 = get_elf_backend_data (output_bfd
);
8300 struct elf_link_hash_entry
*eh
8301 = (struct elf_link_hash_entry
*) h
;
8302 bed
->elf_backend_hide_symbol (info
, eh
, true);
8303 eh
->def_dynamic
= 0;
8304 eh
->ref_dynamic
= 0;
8305 eh
->dynamic_def
= 0;
8309 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8313 _bfd_elf_link_hash_table_init
8314 (struct elf_link_hash_table
*table
,
8316 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
8317 struct bfd_hash_table
*,
8319 unsigned int entsize
)
8322 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8323 int can_refcount
= bed
->can_refcount
;
8325 table
->init_got_refcount
.refcount
= can_refcount
- 1;
8326 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
8327 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
8328 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
8329 /* The first dynamic symbol is a dummy. */
8330 table
->dynsymcount
= 1;
8332 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
8334 table
->root
.type
= bfd_link_elf_hash_table
;
8335 table
->hash_table_id
= bed
->target_id
;
8336 table
->target_os
= bed
->target_os
;
8337 table
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
8342 /* Create an ELF linker hash table. */
8344 struct bfd_link_hash_table
*
8345 _bfd_elf_link_hash_table_create (bfd
*abfd
)
8347 struct elf_link_hash_table
*ret
;
8348 size_t amt
= sizeof (struct elf_link_hash_table
);
8350 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
8354 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
8355 sizeof (struct elf_link_hash_entry
)))
8364 /* Destroy an ELF linker hash table. */
8367 _bfd_elf_link_hash_table_free (bfd
*obfd
)
8369 struct elf_link_hash_table
*htab
;
8371 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
8372 if (htab
->dynstr
!= NULL
)
8373 _bfd_elf_strtab_free (htab
->dynstr
);
8374 _bfd_merge_sections_free (htab
->merge_info
);
8375 /* NB: htab->dynamic->contents is always allocated by bfd_realloc. */
8376 if (htab
->dynamic
!= NULL
)
8378 free (htab
->dynamic
->contents
);
8379 htab
->dynamic
->contents
= NULL
;
8381 if (htab
->first_hash
!= NULL
)
8383 bfd_hash_table_free (htab
->first_hash
);
8384 free (htab
->first_hash
);
8386 if (htab
->eh_info
.frame_hdr_is_compact
)
8387 free (htab
->eh_info
.u
.compact
.entries
);
8389 free (htab
->eh_info
.u
.dwarf
.array
);
8390 _bfd_generic_link_hash_table_free (obfd
);
8393 /* This is a hook for the ELF emulation code in the generic linker to
8394 tell the backend linker what file name to use for the DT_NEEDED
8395 entry for a dynamic object. */
8398 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
8400 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8401 && bfd_get_format (abfd
) == bfd_object
)
8402 elf_dt_name (abfd
) = name
;
8406 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
8409 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8410 && bfd_get_format (abfd
) == bfd_object
)
8411 lib_class
= elf_dyn_lib_class (abfd
);
8418 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
8420 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8421 && bfd_get_format (abfd
) == bfd_object
)
8422 elf_dyn_lib_class (abfd
) = lib_class
;
8425 /* Get the list of DT_NEEDED entries for a link. This is a hook for
8426 the linker ELF emulation code. */
8428 struct bfd_link_needed_list
*
8429 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8430 struct bfd_link_info
*info
)
8432 if (! is_elf_hash_table (info
->hash
))
8434 return elf_hash_table (info
)->needed
;
8437 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8438 hook for the linker ELF emulation code. */
8440 struct bfd_link_needed_list
*
8441 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
8442 struct bfd_link_info
*info
)
8444 if (! is_elf_hash_table (info
->hash
))
8446 return elf_hash_table (info
)->runpath
;
8449 /* Get the name actually used for a dynamic object for a link. This
8450 is the SONAME entry if there is one. Otherwise, it is the string
8451 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8454 bfd_elf_get_dt_soname (bfd
*abfd
)
8456 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
8457 && bfd_get_format (abfd
) == bfd_object
)
8458 return elf_dt_name (abfd
);
8462 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8463 the ELF linker emulation code. */
8466 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
8467 struct bfd_link_needed_list
**pneeded
)
8470 bfd_byte
*dynbuf
= NULL
;
8471 unsigned int elfsec
;
8472 unsigned long shlink
;
8473 bfd_byte
*extdyn
, *extdynend
;
8475 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
8479 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
8480 || bfd_get_format (abfd
) != bfd_object
)
8483 s
= bfd_get_section_by_name (abfd
, ".dynamic");
8484 if (s
== NULL
|| s
->size
== 0 || (s
->flags
& SEC_HAS_CONTENTS
) == 0)
8487 if (!_bfd_elf_mmap_section_contents (abfd
, s
, &dynbuf
))
8490 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
8491 if (elfsec
== SHN_BAD
)
8494 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
8496 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
8497 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
8499 for (extdyn
= dynbuf
, extdynend
= dynbuf
+ s
->size
;
8500 (size_t) (extdynend
- extdyn
) >= extdynsize
;
8501 extdyn
+= extdynsize
)
8503 Elf_Internal_Dyn dyn
;
8505 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
8507 if (dyn
.d_tag
== DT_NULL
)
8510 if (dyn
.d_tag
== DT_NEEDED
)
8513 struct bfd_link_needed_list
*l
;
8514 unsigned int tagv
= dyn
.d_un
.d_val
;
8517 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
8522 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
8533 _bfd_elf_munmap_section_contents (s
, dynbuf
);
8538 _bfd_elf_munmap_section_contents (s
, dynbuf
);
8542 struct elf_symbuf_symbol
8544 unsigned long st_name
; /* Symbol name, index in string tbl */
8545 unsigned char st_info
; /* Type and binding attributes */
8546 unsigned char st_other
; /* Visibilty, and target specific */
8549 struct elf_symbuf_head
8551 struct elf_symbuf_symbol
*ssym
;
8553 unsigned int st_shndx
;
8560 Elf_Internal_Sym
*isym
;
8561 struct elf_symbuf_symbol
*ssym
;
8567 /* Sort references to symbols by ascending section number. */
8570 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
8572 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
8573 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
8575 if (s1
->st_shndx
!= s2
->st_shndx
)
8576 return s1
->st_shndx
> s2
->st_shndx
? 1 : -1;
8577 /* Final sort by the address of the sym in the symbuf ensures
8580 return s1
> s2
? 1 : -1;
8585 elf_sym_name_compare (const void *arg1
, const void *arg2
)
8587 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
8588 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
8589 int ret
= strcmp (s1
->name
, s2
->name
);
8592 if (s1
->u
.p
!= s2
->u
.p
)
8593 return s1
->u
.p
> s2
->u
.p
? 1 : -1;
8597 static struct elf_symbuf_head
*
8598 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
8600 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
8601 struct elf_symbuf_symbol
*ssym
;
8602 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
8603 size_t i
, shndx_count
, total_size
, amt
;
8605 amt
= symcount
* sizeof (*indbuf
);
8606 indbuf
= (Elf_Internal_Sym
**) bfd_malloc (amt
);
8610 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
8611 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
8612 *ind
++ = &isymbuf
[i
];
8615 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
8616 elf_sort_elf_symbol
);
8619 if (indbufend
> indbuf
)
8620 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
8621 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
8624 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
8625 + (indbufend
- indbuf
) * sizeof (*ssym
));
8626 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
8627 if (ssymbuf
== NULL
)
8633 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
8634 ssymbuf
->ssym
= NULL
;
8635 ssymbuf
->count
= shndx_count
;
8636 ssymbuf
->st_shndx
= 0;
8637 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
8639 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
8642 ssymhead
->ssym
= ssym
;
8643 ssymhead
->count
= 0;
8644 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
8646 ssym
->st_name
= (*ind
)->st_name
;
8647 ssym
->st_info
= (*ind
)->st_info
;
8648 ssym
->st_other
= (*ind
)->st_other
;
8651 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
8652 && (uintptr_t) ssym
- (uintptr_t) ssymbuf
== total_size
);
8658 /* Check if 2 sections define the same set of local and global
8662 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
8663 struct bfd_link_info
*info
)
8666 const struct elf_backend_data
*bed1
, *bed2
;
8667 Elf_Internal_Shdr
*hdr1
, *hdr2
;
8668 size_t symcount1
, symcount2
;
8669 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
8670 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
8671 Elf_Internal_Sym
*isym
, *isymend
;
8672 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
8673 size_t count1
, count2
, sec_count1
, sec_count2
, i
;
8674 unsigned int shndx1
, shndx2
;
8676 bool ignore_section_symbol_p
;
8681 /* Both sections have to be in ELF. */
8682 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8683 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8686 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8689 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8690 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8691 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
8694 bed1
= get_elf_backend_data (bfd1
);
8695 bed2
= get_elf_backend_data (bfd2
);
8696 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8697 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8698 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8699 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8701 if (symcount1
== 0 || symcount2
== 0)
8707 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
8708 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
8710 /* Ignore section symbols only when matching non-debugging sections
8711 or linkonce section with comdat section. */
8712 ignore_section_symbol_p
8713 = ((sec1
->flags
& SEC_DEBUGGING
) == 0
8714 || ((elf_section_flags (sec1
) & SHF_GROUP
)
8715 != (elf_section_flags (sec2
) & SHF_GROUP
)));
8717 if (ssymbuf1
== NULL
)
8719 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8721 if (isymbuf1
== NULL
)
8724 if (info
!= NULL
&& !info
->reduce_memory_overheads
)
8726 ssymbuf1
= elf_create_symbuf (symcount1
, isymbuf1
);
8727 elf_tdata (bfd1
)->symbuf
= ssymbuf1
;
8731 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
8733 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8735 if (isymbuf2
== NULL
)
8738 if (ssymbuf1
!= NULL
&& info
!= NULL
&& !info
->reduce_memory_overheads
)
8740 ssymbuf2
= elf_create_symbuf (symcount2
, isymbuf2
);
8741 elf_tdata (bfd2
)->symbuf
= ssymbuf2
;
8745 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
8747 /* Optimized faster version. */
8749 struct elf_symbol
*symp
;
8750 struct elf_symbuf_symbol
*ssym
, *ssymend
;
8753 hi
= ssymbuf1
->count
;
8759 mid
= (lo
+ hi
) / 2;
8760 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
8762 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
8766 count1
= ssymbuf1
[mid
].count
;
8771 if (ignore_section_symbol_p
)
8773 for (i
= 0; i
< count1
; i
++)
8774 if (ELF_ST_TYPE (ssymbuf1
->ssym
[i
].st_info
) == STT_SECTION
)
8776 count1
-= sec_count1
;
8780 hi
= ssymbuf2
->count
;
8786 mid
= (lo
+ hi
) / 2;
8787 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
8789 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
8793 count2
= ssymbuf2
[mid
].count
;
8798 if (ignore_section_symbol_p
)
8800 for (i
= 0; i
< count2
; i
++)
8801 if (ELF_ST_TYPE (ssymbuf2
->ssym
[i
].st_info
) == STT_SECTION
)
8803 count2
-= sec_count2
;
8806 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8810 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
8812 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
8813 if (symtable1
== NULL
|| symtable2
== NULL
)
8817 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
+ sec_count1
;
8818 ssym
< ssymend
; ssym
++)
8820 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8822 symp
->u
.ssym
= ssym
;
8823 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8826 if (symp
->name
== NULL
)
8832 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
+ sec_count2
;
8833 ssym
< ssymend
; ssym
++)
8835 || ELF_ST_TYPE (ssym
->st_info
) != STT_SECTION
)
8837 symp
->u
.ssym
= ssym
;
8838 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8841 if (symp
->name
== NULL
)
8846 /* Sort symbol by name. */
8847 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8848 elf_sym_name_compare
);
8849 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8850 elf_sym_name_compare
);
8852 for (i
= 0; i
< count1
; i
++)
8853 /* Two symbols must have the same binding, type and name. */
8854 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
8855 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
8856 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8863 symtable1
= (struct elf_symbol
*)
8864 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
8865 symtable2
= (struct elf_symbol
*)
8866 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
8867 if (symtable1
== NULL
|| symtable2
== NULL
)
8870 /* Count definitions in the section. */
8872 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
8873 if (isym
->st_shndx
== shndx1
8874 && (!ignore_section_symbol_p
8875 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8876 symtable1
[count1
++].u
.isym
= isym
;
8879 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
8880 if (isym
->st_shndx
== shndx2
8881 && (!ignore_section_symbol_p
8882 || ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
))
8883 symtable2
[count2
++].u
.isym
= isym
;
8885 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8888 for (i
= 0; i
< count1
; i
++)
8891 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
8892 symtable1
[i
].u
.isym
->st_name
);
8893 if (symtable1
[i
].name
== NULL
)
8897 for (i
= 0; i
< count2
; i
++)
8900 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
8901 symtable2
[i
].u
.isym
->st_name
);
8902 if (symtable2
[i
].name
== NULL
)
8906 /* Sort symbol by name. */
8907 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8908 elf_sym_name_compare
);
8909 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8910 elf_sym_name_compare
);
8912 for (i
= 0; i
< count1
; i
++)
8913 /* Two symbols must have the same binding, type and name. */
8914 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
8915 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
8916 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
8930 /* Return TRUE if 2 section types are compatible. */
8933 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
8934 bfd
*bbfd
, const asection
*bsec
)
8938 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
8939 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8942 return elf_section_type (asec
) == elf_section_type (bsec
);
8945 /* Final phase of ELF linker. */
8947 /* A structure we use to avoid passing large numbers of arguments. */
8949 struct elf_final_link_info
8951 /* General link information. */
8952 struct bfd_link_info
*info
;
8955 /* Symbol string table. */
8956 struct elf_strtab_hash
*symstrtab
;
8957 /* .hash section. */
8959 /* symbol version section (.gnu.version). */
8960 asection
*symver_sec
;
8961 /* Buffer large enough to hold contents of any section. */
8963 /* Buffer large enough to hold external relocs of any section. */
8964 void *external_relocs
;
8965 /* Buffer large enough to hold internal relocs of any section. */
8966 Elf_Internal_Rela
*internal_relocs
;
8967 /* Buffer large enough to hold external local symbols of any input
8969 bfd_byte
*external_syms
;
8970 /* And a buffer for symbol section indices. */
8971 Elf_External_Sym_Shndx
*locsym_shndx
;
8972 /* Buffer large enough to hold internal local symbols of any input
8974 Elf_Internal_Sym
*internal_syms
;
8975 /* Array large enough to hold a symbol index for each local symbol
8976 of any input BFD. */
8978 /* Array large enough to hold a section pointer for each local
8979 symbol of any input BFD. */
8980 asection
**sections
;
8981 /* Buffer for SHT_SYMTAB_SHNDX section. */
8982 Elf_External_Sym_Shndx
*symshndxbuf
;
8983 /* Number of STT_FILE syms seen. */
8984 size_t filesym_count
;
8985 /* Local symbol hash table. */
8986 struct bfd_hash_table local_hash_table
;
8989 struct local_hash_entry
8991 /* Base hash table entry structure. */
8992 struct bfd_hash_entry root
;
8993 /* Size of the local symbol name. */
8995 /* Number of the duplicated local symbol names. */
8999 /* Create an entry in the local symbol hash table. */
9001 static struct bfd_hash_entry
*
9002 local_hash_newfunc (struct bfd_hash_entry
*entry
,
9003 struct bfd_hash_table
*table
,
9007 /* Allocate the structure if it has not already been allocated by a
9011 entry
= bfd_hash_allocate (table
,
9012 sizeof (struct local_hash_entry
));
9017 /* Call the allocation method of the superclass. */
9018 entry
= bfd_hash_newfunc (entry
, table
, string
);
9021 ((struct local_hash_entry
*) entry
)->count
= 0;
9022 ((struct local_hash_entry
*) entry
)->size
= 0;
9028 /* This struct is used to pass information to elf_link_output_extsym. */
9030 struct elf_outext_info
9035 struct elf_final_link_info
*flinfo
;
9039 /* Support for evaluating a complex relocation.
9041 Complex relocations are generalized, self-describing relocations. The
9042 implementation of them consists of two parts: complex symbols, and the
9043 relocations themselves.
9045 The relocations use a reserved elf-wide relocation type code (R_RELC
9046 external / BFD_RELOC_RELC internal) and an encoding of relocation field
9047 information (start bit, end bit, word width, etc) into the addend. This
9048 information is extracted from CGEN-generated operand tables within gas.
9050 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
9051 internal) representing prefix-notation expressions, including but not
9052 limited to those sorts of expressions normally encoded as addends in the
9053 addend field. The symbol mangling format is:
9056 | <unary-operator> ':' <node>
9057 | <binary-operator> ':' <node> ':' <node>
9060 <literal> := 's' <digits=N> ':' <N character symbol name>
9061 | 'S' <digits=N> ':' <N character section name>
9065 <binary-operator> := as in C
9066 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
9069 set_symbol_value (bfd
*bfd_with_globals
,
9070 Elf_Internal_Sym
*isymbuf
,
9075 struct elf_link_hash_entry
**sym_hashes
;
9076 struct elf_link_hash_entry
*h
;
9077 size_t extsymoff
= locsymcount
;
9079 if (symidx
< locsymcount
)
9081 Elf_Internal_Sym
*sym
;
9083 sym
= isymbuf
+ symidx
;
9084 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
9086 /* It is a local symbol: move it to the
9087 "absolute" section and give it a value. */
9088 sym
->st_shndx
= SHN_ABS
;
9089 sym
->st_value
= val
;
9092 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
9096 /* It is a global symbol: set its link type
9097 to "defined" and give it a value. */
9099 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
9100 h
= sym_hashes
[symidx
- extsymoff
];
9101 while (h
->root
.type
== bfd_link_hash_indirect
9102 || h
->root
.type
== bfd_link_hash_warning
)
9103 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9104 h
->root
.type
= bfd_link_hash_defined
;
9105 h
->root
.u
.def
.value
= val
;
9106 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
9110 resolve_symbol (const char *name
,
9112 struct elf_final_link_info
*flinfo
,
9114 Elf_Internal_Sym
*isymbuf
,
9117 Elf_Internal_Sym
*sym
;
9118 struct bfd_link_hash_entry
*global_entry
;
9119 const char *candidate
= NULL
;
9120 Elf_Internal_Shdr
*symtab_hdr
;
9123 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
9125 for (i
= 0; i
< locsymcount
; ++ i
)
9129 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
9132 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
9133 symtab_hdr
->sh_link
,
9136 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9137 name
, candidate
, (unsigned long) sym
->st_value
);
9139 if (candidate
&& strcmp (candidate
, name
) == 0)
9141 asection
*sec
= flinfo
->sections
[i
];
9143 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
9144 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
9146 printf ("Found symbol with value %8.8lx\n",
9147 (unsigned long) *result
);
9153 /* Hmm, haven't found it yet. perhaps it is a global. */
9154 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
9155 false, false, true);
9159 if (global_entry
->type
== bfd_link_hash_defined
9160 || global_entry
->type
== bfd_link_hash_defweak
)
9162 *result
= (global_entry
->u
.def
.value
9163 + global_entry
->u
.def
.section
->output_section
->vma
9164 + global_entry
->u
.def
.section
->output_offset
);
9166 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9167 global_entry
->root
.string
, (unsigned long) *result
);
9175 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
9176 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
9177 names like "foo.end" which is the end address of section "foo". */
9180 resolve_section (const char *name
,
9188 for (curr
= sections
; curr
; curr
= curr
->next
)
9189 if (strcmp (curr
->name
, name
) == 0)
9191 *result
= curr
->vma
;
9195 /* Hmm. still haven't found it. try pseudo-section names. */
9196 /* FIXME: This could be coded more efficiently... */
9197 for (curr
= sections
; curr
; curr
= curr
->next
)
9199 len
= strlen (curr
->name
);
9200 if (len
> strlen (name
))
9203 if (strncmp (curr
->name
, name
, len
) == 0)
9205 if (startswith (name
+ len
, ".end"))
9207 *result
= (curr
->vma
9208 + curr
->size
/ bfd_octets_per_byte (abfd
, curr
));
9212 /* Insert more pseudo-section names here, if you like. */
9220 undefined_reference (const char *reftype
, const char *name
)
9222 /* xgettext:c-format */
9223 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9225 bfd_set_error (bfd_error_bad_value
);
9229 eval_symbol (bfd_vma
*result
,
9232 struct elf_final_link_info
*flinfo
,
9234 Elf_Internal_Sym
*isymbuf
,
9243 const char *sym
= *symp
;
9245 bool symbol_is_section
= false;
9250 if (len
< 1 || len
> sizeof (symbuf
))
9252 bfd_set_error (bfd_error_invalid_operation
);
9265 *result
= strtoul (sym
, (char **) symp
, 16);
9269 symbol_is_section
= true;
9273 symlen
= strtol (sym
, (char **) symp
, 10);
9274 sym
= *symp
+ 1; /* Skip the trailing ':'. */
9276 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
9278 bfd_set_error (bfd_error_invalid_operation
);
9282 memcpy (symbuf
, sym
, symlen
);
9283 symbuf
[symlen
] = '\0';
9284 *symp
= sym
+ symlen
;
9286 /* Is it always possible, with complex symbols, that gas "mis-guessed"
9287 the symbol as a section, or vice-versa. so we're pretty liberal in our
9288 interpretation here; section means "try section first", not "must be a
9289 section", and likewise with symbol. */
9291 if (symbol_is_section
)
9293 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
9294 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
9295 isymbuf
, locsymcount
))
9297 undefined_reference ("section", symbuf
);
9303 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
9304 isymbuf
, locsymcount
)
9305 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
9308 undefined_reference ("symbol", symbuf
);
9315 /* All that remains are operators. */
9317 #define UNARY_OP(op) \
9318 if (startswith (sym, #op)) \
9320 sym += strlen (#op); \
9324 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9325 isymbuf, locsymcount, signed_p)) \
9328 *result = op ((bfd_signed_vma) a); \
9334 #define BINARY_OP_HEAD(op) \
9335 if (startswith (sym, #op)) \
9337 sym += strlen (#op); \
9341 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
9342 isymbuf, locsymcount, signed_p)) \
9345 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
9346 isymbuf, locsymcount, signed_p)) \
9348 #define BINARY_OP_TAIL(op) \
9350 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
9355 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
9359 BINARY_OP_HEAD (<<);
9360 if (b
>= sizeof (a
) * CHAR_BIT
)
9366 BINARY_OP_TAIL (<<);
9367 BINARY_OP_HEAD (>>);
9368 if (b
>= sizeof (a
) * CHAR_BIT
)
9370 *result
= signed_p
&& (bfd_signed_vma
) a
< 0 ? -1 : 0;
9373 BINARY_OP_TAIL (>>);
9386 _bfd_error_handler (_("division by zero"));
9387 bfd_set_error (bfd_error_bad_value
);
9394 _bfd_error_handler (_("division by zero"));
9395 bfd_set_error (bfd_error_bad_value
);
9408 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
9409 bfd_set_error (bfd_error_invalid_operation
);
9415 put_value (bfd_vma size
,
9416 unsigned long chunksz
,
9421 location
+= (size
- chunksz
);
9423 for (; size
; size
-= chunksz
, location
-= chunksz
)
9428 bfd_put_8 (input_bfd
, x
, location
);
9432 bfd_put_16 (input_bfd
, x
, location
);
9436 bfd_put_32 (input_bfd
, x
, location
);
9437 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9443 bfd_put_64 (input_bfd
, x
, location
);
9444 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9457 get_value (bfd_vma size
,
9458 unsigned long chunksz
,
9465 /* Sanity checks. */
9466 BFD_ASSERT (chunksz
<= sizeof (x
)
9469 && (size
% chunksz
) == 0
9470 && input_bfd
!= NULL
9471 && location
!= NULL
);
9473 if (chunksz
== sizeof (x
))
9475 BFD_ASSERT (size
== chunksz
);
9477 /* Make sure that we do not perform an undefined shift operation.
9478 We know that size == chunksz so there will only be one iteration
9479 of the loop below. */
9483 shift
= 8 * chunksz
;
9485 for (; size
; size
-= chunksz
, location
+= chunksz
)
9490 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
9493 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
9496 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
9500 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
9511 decode_complex_addend (unsigned long *start
, /* in bits */
9512 unsigned long *oplen
, /* in bits */
9513 unsigned long *len
, /* in bits */
9514 unsigned long *wordsz
, /* in bytes */
9515 unsigned long *chunksz
, /* in bytes */
9516 unsigned long *lsb0_p
,
9517 unsigned long *signed_p
,
9518 unsigned long *trunc_p
,
9519 unsigned long encoded
)
9521 * start
= encoded
& 0x3F;
9522 * len
= (encoded
>> 6) & 0x3F;
9523 * oplen
= (encoded
>> 12) & 0x3F;
9524 * wordsz
= (encoded
>> 18) & 0xF;
9525 * chunksz
= (encoded
>> 22) & 0xF;
9526 * lsb0_p
= (encoded
>> 27) & 1;
9527 * signed_p
= (encoded
>> 28) & 1;
9528 * trunc_p
= (encoded
>> 29) & 1;
9531 bfd_reloc_status_type
9532 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
9533 asection
*input_section
,
9535 Elf_Internal_Rela
*rel
,
9538 bfd_vma shift
, x
, mask
;
9539 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
9540 bfd_reloc_status_type r
;
9541 bfd_size_type octets
;
9543 /* Perform this reloc, since it is complex.
9544 (this is not to say that it necessarily refers to a complex
9545 symbol; merely that it is a self-describing CGEN based reloc.
9546 i.e. the addend has the complete reloc information (bit start, end,
9547 word size, etc) encoded within it.). */
9549 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
9550 &chunksz
, &lsb0_p
, &signed_p
,
9551 &trunc_p
, rel
->r_addend
);
9553 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
9556 shift
= (start
+ 1) - len
;
9558 shift
= (8 * wordsz
) - (start
+ len
);
9560 octets
= rel
->r_offset
* bfd_octets_per_byte (input_bfd
, input_section
);
9561 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ octets
);
9564 printf ("Doing complex reloc: "
9565 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9566 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9567 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9568 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
9569 oplen
, (unsigned long) x
, (unsigned long) mask
,
9570 (unsigned long) relocation
);
9575 /* Now do an overflow check. */
9576 r
= bfd_check_overflow ((signed_p
9577 ? complain_overflow_signed
9578 : complain_overflow_unsigned
),
9579 len
, 0, (8 * wordsz
),
9583 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
9586 printf (" relocation: %8.8lx\n"
9587 " shifted mask: %8.8lx\n"
9588 " shifted/masked reloc: %8.8lx\n"
9589 " result: %8.8lx\n",
9590 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
9591 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
9593 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ octets
);
9597 /* Functions to read r_offset from external (target order) reloc
9598 entry. Faster than bfd_getl32 et al, because we let the compiler
9599 know the value is aligned. */
9602 ext32l_r_offset (const void *p
)
9609 const union aligned32
*a
9610 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9612 uint32_t aval
= ( (uint32_t) a
->c
[0]
9613 | (uint32_t) a
->c
[1] << 8
9614 | (uint32_t) a
->c
[2] << 16
9615 | (uint32_t) a
->c
[3] << 24);
9620 ext32b_r_offset (const void *p
)
9627 const union aligned32
*a
9628 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
9630 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
9631 | (uint32_t) a
->c
[1] << 16
9632 | (uint32_t) a
->c
[2] << 8
9633 | (uint32_t) a
->c
[3]);
9638 ext64l_r_offset (const void *p
)
9645 const union aligned64
*a
9646 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9648 uint64_t aval
= ( (uint64_t) a
->c
[0]
9649 | (uint64_t) a
->c
[1] << 8
9650 | (uint64_t) a
->c
[2] << 16
9651 | (uint64_t) a
->c
[3] << 24
9652 | (uint64_t) a
->c
[4] << 32
9653 | (uint64_t) a
->c
[5] << 40
9654 | (uint64_t) a
->c
[6] << 48
9655 | (uint64_t) a
->c
[7] << 56);
9660 ext64b_r_offset (const void *p
)
9667 const union aligned64
*a
9668 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
9670 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
9671 | (uint64_t) a
->c
[1] << 48
9672 | (uint64_t) a
->c
[2] << 40
9673 | (uint64_t) a
->c
[3] << 32
9674 | (uint64_t) a
->c
[4] << 24
9675 | (uint64_t) a
->c
[5] << 16
9676 | (uint64_t) a
->c
[6] << 8
9677 | (uint64_t) a
->c
[7]);
9681 /* When performing a relocatable link, the input relocations are
9682 preserved. But, if they reference global symbols, the indices
9683 referenced must be updated. Update all the relocations found in
9687 elf_link_adjust_relocs (bfd
*abfd
,
9689 struct bfd_elf_section_reloc_data
*reldata
,
9691 struct bfd_link_info
*info
)
9694 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9696 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9697 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9698 bfd_vma r_type_mask
;
9700 unsigned int count
= reldata
->count
;
9701 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
9703 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
9705 swap_in
= bed
->s
->swap_reloc_in
;
9706 swap_out
= bed
->s
->swap_reloc_out
;
9708 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
9710 swap_in
= bed
->s
->swap_reloca_in
;
9711 swap_out
= bed
->s
->swap_reloca_out
;
9716 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
9719 if (bed
->s
->arch_size
== 32)
9726 r_type_mask
= 0xffffffff;
9730 erela
= reldata
->hdr
->contents
;
9731 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
9733 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
9736 if (*rel_hash
== NULL
)
9739 if ((*rel_hash
)->indx
== -2
9740 && info
->gc_sections
9741 && ! info
->gc_keep_exported
)
9743 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9744 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
9746 (*rel_hash
)->root
.root
.string
);
9747 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
9749 bfd_set_error (bfd_error_invalid_operation
);
9752 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
9754 (*swap_in
) (abfd
, erela
, irela
);
9755 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
9756 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
9757 | (irela
[j
].r_info
& r_type_mask
));
9758 (*swap_out
) (abfd
, irela
, erela
);
9761 if (bed
->elf_backend_update_relocs
)
9762 (*bed
->elf_backend_update_relocs
) (sec
, reldata
);
9764 if (sort
&& count
!= 0)
9766 bfd_vma (*ext_r_off
) (const void *);
9769 bfd_byte
*base
, *end
, *p
, *loc
;
9770 bfd_byte
*buf
= NULL
;
9772 if (bed
->s
->arch_size
== 32)
9774 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9775 ext_r_off
= ext32l_r_offset
;
9776 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9777 ext_r_off
= ext32b_r_offset
;
9783 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
9784 ext_r_off
= ext64l_r_offset
;
9785 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
9786 ext_r_off
= ext64b_r_offset
;
9791 /* Must use a stable sort here. A modified insertion sort,
9792 since the relocs are mostly sorted already. */
9793 elt_size
= reldata
->hdr
->sh_entsize
;
9794 base
= reldata
->hdr
->contents
;
9795 end
= base
+ count
* elt_size
;
9796 if (elt_size
> sizeof (Elf64_External_Rela
))
9799 /* Ensure the first element is lowest. This acts as a sentinel,
9800 speeding the main loop below. */
9801 r_off
= (*ext_r_off
) (base
);
9802 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
9804 bfd_vma r_off2
= (*ext_r_off
) (p
);
9813 /* Don't just swap *base and *loc as that changes the order
9814 of the original base[0] and base[1] if they happen to
9815 have the same r_offset. */
9816 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
9817 memcpy (onebuf
, loc
, elt_size
);
9818 memmove (base
+ elt_size
, base
, loc
- base
);
9819 memcpy (base
, onebuf
, elt_size
);
9822 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
9824 /* base to p is sorted, *p is next to insert. */
9825 r_off
= (*ext_r_off
) (p
);
9826 /* Search the sorted region for location to insert. */
9828 while (r_off
< (*ext_r_off
) (loc
))
9833 /* Chances are there is a run of relocs to insert here,
9834 from one of more input files. Files are not always
9835 linked in order due to the way elf_link_input_bfd is
9836 called. See pr17666. */
9837 size_t sortlen
= p
- loc
;
9838 bfd_vma r_off2
= (*ext_r_off
) (loc
);
9839 size_t runlen
= elt_size
;
9840 bfd_vma r_off_runend
= r_off
;
9841 bfd_vma r_off_runend_next
;
9842 size_t buf_size
= 96 * 1024;
9843 while (p
+ runlen
< end
9844 && (sortlen
<= buf_size
9845 || runlen
+ elt_size
<= buf_size
)
9846 /* run must not break the ordering of base..loc+1 */
9847 && r_off2
> (r_off_runend_next
= (*ext_r_off
) (p
+ runlen
))
9848 /* run must be already sorted */
9849 && r_off_runend_next
>= r_off_runend
)
9852 r_off_runend
= r_off_runend_next
;
9856 buf
= bfd_malloc (buf_size
);
9860 if (runlen
< sortlen
)
9862 memcpy (buf
, p
, runlen
);
9863 memmove (loc
+ runlen
, loc
, sortlen
);
9864 memcpy (loc
, buf
, runlen
);
9868 memcpy (buf
, loc
, sortlen
);
9869 memmove (loc
, p
, runlen
);
9870 memcpy (loc
+ runlen
, buf
, sortlen
);
9872 p
+= runlen
- elt_size
;
9875 /* Hashes are no longer valid. */
9876 free (reldata
->hashes
);
9877 reldata
->hashes
= NULL
;
9883 struct elf_link_sort_rela
9889 enum elf_reloc_type_class type
;
9890 /* We use this as an array of size int_rels_per_ext_rel. */
9891 Elf_Internal_Rela rela
[1];
9894 /* qsort stability here and for cmp2 is only an issue if multiple
9895 dynamic relocations are emitted at the same address. But targets
9896 that apply a series of dynamic relocations each operating on the
9897 result of the prior relocation can't use -z combreloc as
9898 implemented anyway. Such schemes tend to be broken by sorting on
9899 symbol index. That leaves dynamic NONE relocs as the only other
9900 case where ld might emit multiple relocs at the same address, and
9901 those are only emitted due to target bugs. */
9904 elf_link_sort_cmp1 (const void *A
, const void *B
)
9906 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9907 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9908 int relativea
, relativeb
;
9910 relativea
= a
->type
== reloc_class_relative
;
9911 relativeb
= b
->type
== reloc_class_relative
;
9913 if (relativea
< relativeb
)
9915 if (relativea
> relativeb
)
9917 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
9919 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
9921 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9923 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9929 elf_link_sort_cmp2 (const void *A
, const void *B
)
9931 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
9932 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
9934 if (a
->type
< b
->type
)
9936 if (a
->type
> b
->type
)
9938 if (a
->u
.offset
< b
->u
.offset
)
9940 if (a
->u
.offset
> b
->u
.offset
)
9942 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
9944 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
9950 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
9952 asection
*dynamic_relocs
;
9955 bfd_size_type count
, size
;
9956 size_t i
, ret
, sort_elt
, ext_size
;
9957 bfd_byte
*sort
, *s_non_relative
, *p
;
9958 struct elf_link_sort_rela
*sq
;
9959 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9960 int i2e
= bed
->s
->int_rels_per_ext_rel
;
9961 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
9962 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
9963 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
9964 struct bfd_link_order
*lo
;
9968 /* Find a dynamic reloc section. */
9969 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
9970 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
9971 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
9972 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
9974 bool use_rela_initialised
= false;
9976 /* This is just here to stop gcc from complaining.
9977 Its initialization checking code is not perfect. */
9980 /* Both sections are present. Examine the sizes
9981 of the indirect sections to help us choose. */
9982 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
9983 if (lo
->type
== bfd_indirect_link_order
)
9985 asection
*o
= lo
->u
.indirect
.section
;
9987 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
9989 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
9990 /* Section size is divisible by both rel and rela sizes.
9991 It is of no help to us. */
9995 /* Section size is only divisible by rela. */
9996 if (use_rela_initialised
&& !use_rela
)
9998 _bfd_error_handler (_("%pB: unable to sort relocs - "
9999 "they are in more than one size"),
10001 bfd_set_error (bfd_error_invalid_operation
);
10007 use_rela_initialised
= true;
10011 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
10013 /* Section size is only divisible by rel. */
10014 if (use_rela_initialised
&& use_rela
)
10016 _bfd_error_handler (_("%pB: unable to sort relocs - "
10017 "they are in more than one size"),
10019 bfd_set_error (bfd_error_invalid_operation
);
10025 use_rela_initialised
= true;
10030 /* The section size is not divisible by either -
10031 something is wrong. */
10032 _bfd_error_handler (_("%pB: unable to sort relocs - "
10033 "they are of an unknown size"), abfd
);
10034 bfd_set_error (bfd_error_invalid_operation
);
10039 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10040 if (lo
->type
== bfd_indirect_link_order
)
10042 asection
*o
= lo
->u
.indirect
.section
;
10044 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
10046 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
10047 /* Section size is divisible by both rel and rela sizes.
10048 It is of no help to us. */
10052 /* Section size is only divisible by rela. */
10053 if (use_rela_initialised
&& !use_rela
)
10055 _bfd_error_handler (_("%pB: unable to sort relocs - "
10056 "they are in more than one size"),
10058 bfd_set_error (bfd_error_invalid_operation
);
10064 use_rela_initialised
= true;
10068 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
10070 /* Section size is only divisible by rel. */
10071 if (use_rela_initialised
&& use_rela
)
10073 _bfd_error_handler (_("%pB: unable to sort relocs - "
10074 "they are in more than one size"),
10076 bfd_set_error (bfd_error_invalid_operation
);
10082 use_rela_initialised
= true;
10087 /* The section size is not divisible by either -
10088 something is wrong. */
10089 _bfd_error_handler (_("%pB: unable to sort relocs - "
10090 "they are of an unknown size"), abfd
);
10091 bfd_set_error (bfd_error_invalid_operation
);
10096 if (! use_rela_initialised
)
10097 /* Make a guess. */
10100 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
10102 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
10109 dynamic_relocs
= rela_dyn
;
10110 ext_size
= bed
->s
->sizeof_rela
;
10111 swap_in
= bed
->s
->swap_reloca_in
;
10112 swap_out
= bed
->s
->swap_reloca_out
;
10116 dynamic_relocs
= rel_dyn
;
10117 ext_size
= bed
->s
->sizeof_rel
;
10118 swap_in
= bed
->s
->swap_reloc_in
;
10119 swap_out
= bed
->s
->swap_reloc_out
;
10123 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10124 if (lo
->type
== bfd_indirect_link_order
)
10125 size
+= lo
->u
.indirect
.section
->size
;
10127 if (size
!= dynamic_relocs
->size
)
10130 sort_elt
= (sizeof (struct elf_link_sort_rela
)
10131 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
10133 count
= dynamic_relocs
->size
/ ext_size
;
10136 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
10140 (*info
->callbacks
->warning
)
10141 (info
, _("not enough memory to sort relocations"), 0, abfd
, 0, 0);
10145 if (bed
->s
->arch_size
== 32)
10146 r_sym_mask
= ~(bfd_vma
) 0xff;
10148 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
10150 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10151 if (lo
->type
== bfd_indirect_link_order
)
10153 bfd_byte
*erel
, *erelend
;
10154 asection
*o
= lo
->u
.indirect
.section
;
10156 if (o
->contents
== NULL
&& o
->size
!= 0)
10158 /* This is a reloc section that is being handled as a normal
10159 section. See bfd_section_from_shdr. We can't combine
10160 relocs in this case. */
10164 erel
= o
->contents
;
10165 erelend
= o
->contents
+ o
->size
;
10166 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
10168 while (erel
< erelend
)
10170 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
10172 (*swap_in
) (abfd
, erel
, s
->rela
);
10173 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
10174 s
->u
.sym_mask
= r_sym_mask
;
10180 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
10182 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
10184 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
10185 if (s
->type
!= reloc_class_relative
)
10189 s_non_relative
= p
;
10191 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
10192 for (; i
< count
; i
++, p
+= sort_elt
)
10194 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
10195 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
10197 sp
->u
.offset
= sq
->rela
->r_offset
;
10200 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
10202 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
10203 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
10205 /* We have plt relocs in .rela.dyn. */
10206 sq
= (struct elf_link_sort_rela
*) sort
;
10207 for (i
= 0; i
< count
; i
++)
10208 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
10210 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
10212 struct bfd_link_order
**plo
;
10213 /* Put srelplt link_order last. This is so the output_offset
10214 set in the next loop is correct for DT_JMPREL. */
10215 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
10216 if ((*plo
)->type
== bfd_indirect_link_order
10217 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
10223 plo
= &(*plo
)->next
;
10226 dynamic_relocs
->map_tail
.link_order
= lo
;
10231 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
10232 if (lo
->type
== bfd_indirect_link_order
)
10234 bfd_byte
*erel
, *erelend
;
10235 asection
*o
= lo
->u
.indirect
.section
;
10237 erel
= o
->contents
;
10238 erelend
= o
->contents
+ o
->size
;
10239 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
10240 while (erel
< erelend
)
10242 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
10243 (*swap_out
) (abfd
, s
->rela
, erel
);
10250 *psec
= dynamic_relocs
;
10254 /* Add a symbol to the output symbol string table. */
10257 elf_link_output_symstrtab (void *finf
,
10259 Elf_Internal_Sym
*elfsym
,
10260 asection
*input_sec
,
10261 struct elf_link_hash_entry
*h
)
10263 struct elf_final_link_info
*flinfo
= finf
;
10264 int (*output_symbol_hook
)
10265 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
10266 struct elf_link_hash_entry
*);
10267 struct elf_link_hash_table
*hash_table
;
10268 const struct elf_backend_data
*bed
;
10269 bfd_size_type strtabsize
;
10271 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10273 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10274 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
10275 if (output_symbol_hook
!= NULL
)
10277 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
10282 if (ELF_ST_TYPE (elfsym
->st_info
) == STT_GNU_IFUNC
)
10283 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
10284 if (ELF_ST_BIND (elfsym
->st_info
) == STB_GNU_UNIQUE
)
10285 elf_tdata (flinfo
->output_bfd
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
10287 if (name
== NULL
|| *name
== '\0')
10288 elfsym
->st_name
= (unsigned long) -1;
10291 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10292 to get the final offset for st_name. */
10293 char *versioned_name
= (char *) name
;
10296 if (h
->versioned
== versioned
&& h
->def_dynamic
)
10298 /* Keep only one '@' for versioned symbols defined in
10300 char *version
= strrchr (name
, ELF_VER_CHR
);
10301 char *base_end
= strchr (name
, ELF_VER_CHR
);
10302 if (version
!= base_end
)
10305 size_t len
= strlen (name
);
10306 versioned_name
= bfd_alloc (flinfo
->output_bfd
, len
);
10307 if (versioned_name
== NULL
)
10309 base_len
= base_end
- name
;
10310 memcpy (versioned_name
, name
, base_len
);
10311 memcpy (versioned_name
+ base_len
, version
,
10316 else if (flinfo
->info
->unique_symbol
10317 && ELF_ST_BIND (elfsym
->st_info
) == STB_LOCAL
)
10319 struct local_hash_entry
*lh
;
10323 switch (ELF_ST_TYPE (elfsym
->st_info
))
10329 lh
= (struct local_hash_entry
*) bfd_hash_lookup
10330 (&flinfo
->local_hash_table
, name
, true, false);
10333 /* Always append ".COUNT" to local symbols to avoid
10334 potential conflicts with local symbol "XXX.COUNT". */
10335 sprintf (buf
, "%lx", lh
->count
);
10336 base_len
= lh
->size
;
10339 base_len
= strlen (name
);
10340 lh
->size
= base_len
;
10342 count_len
= strlen (buf
);
10343 versioned_name
= bfd_alloc (flinfo
->output_bfd
,
10344 base_len
+ count_len
+ 2);
10345 if (versioned_name
== NULL
)
10347 memcpy (versioned_name
, name
, base_len
);
10348 versioned_name
[base_len
] = '.';
10349 memcpy (versioned_name
+ base_len
+ 1, buf
,
10356 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
10357 versioned_name
, false);
10358 if (elfsym
->st_name
== (unsigned long) -1)
10362 hash_table
= elf_hash_table (flinfo
->info
);
10363 strtabsize
= hash_table
->strtabsize
;
10364 if (strtabsize
<= flinfo
->output_bfd
->symcount
)
10366 strtabsize
+= strtabsize
;
10367 hash_table
->strtabsize
= strtabsize
;
10368 strtabsize
*= sizeof (*hash_table
->strtab
);
10370 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
10372 if (hash_table
->strtab
== NULL
)
10375 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].sym
= *elfsym
;
10376 hash_table
->strtab
[flinfo
->output_bfd
->symcount
].dest_index
10377 = flinfo
->output_bfd
->symcount
;
10378 flinfo
->output_bfd
->symcount
+= 1;
10383 /* Swap symbols out to the symbol table and flush the output symbols to
10387 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
10389 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
10392 const struct elf_backend_data
*bed
;
10394 Elf_Internal_Shdr
*hdr
;
10398 if (flinfo
->output_bfd
->symcount
== 0)
10401 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
10403 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10405 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10406 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
10407 if (symbuf
== NULL
)
10410 if (flinfo
->symshndxbuf
)
10412 amt
= sizeof (Elf_External_Sym_Shndx
);
10413 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
10414 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
10415 if (flinfo
->symshndxbuf
== NULL
)
10422 /* Now swap out the symbols. */
10423 for (i
= 0; i
< flinfo
->output_bfd
->symcount
; i
++)
10425 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
10426 if (elfsym
->sym
.st_name
== (unsigned long) -1)
10427 elfsym
->sym
.st_name
= 0;
10429 elfsym
->sym
.st_name
10430 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
10431 elfsym
->sym
.st_name
);
10433 /* Inform the linker of the addition of this symbol. */
10435 if (flinfo
->info
->callbacks
->ctf_new_symbol
)
10436 flinfo
->info
->callbacks
->ctf_new_symbol (elfsym
->dest_index
,
10439 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
10440 ((bfd_byte
*) symbuf
10441 + (elfsym
->dest_index
10442 * bed
->s
->sizeof_sym
)),
10443 NPTR_ADD (flinfo
->symshndxbuf
,
10444 elfsym
->dest_index
));
10447 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
10448 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
10449 amt
= bed
->s
->sizeof_sym
* flinfo
->output_bfd
->symcount
;
10450 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
10451 && bfd_write (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
10453 hdr
->sh_size
+= amt
;
10463 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10466 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
10468 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
10469 && sym
->st_shndx
< SHN_LORESERVE
)
10471 /* The gABI doesn't support dynamic symbols in output sections
10474 /* xgettext:c-format */
10475 (_("%pB: too many sections: %d (>= %d)"),
10476 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
10477 bfd_set_error (bfd_error_nonrepresentable_section
);
10483 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10484 allowing an unsatisfied unversioned symbol in the DSO to match a
10485 versioned symbol that would normally require an explicit version.
10486 We also handle the case that a DSO references a hidden symbol
10487 which may be satisfied by a versioned symbol in another DSO. */
10490 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
10491 const struct elf_backend_data
*bed
,
10492 struct elf_link_hash_entry
*h
)
10495 struct elf_link_loaded_list
*loaded
;
10497 if (!is_elf_hash_table (info
->hash
))
10500 /* Check indirect symbol. */
10501 while (h
->root
.type
== bfd_link_hash_indirect
)
10502 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10504 switch (h
->root
.type
)
10510 case bfd_link_hash_undefined
:
10511 case bfd_link_hash_undefweak
:
10512 abfd
= h
->root
.u
.undef
.abfd
;
10514 || (abfd
->flags
& DYNAMIC
) == 0
10515 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
10519 case bfd_link_hash_defined
:
10520 case bfd_link_hash_defweak
:
10521 abfd
= h
->root
.u
.def
.section
->owner
;
10524 case bfd_link_hash_common
:
10525 abfd
= h
->root
.u
.c
.p
->section
->owner
;
10528 BFD_ASSERT (abfd
!= NULL
);
10530 for (loaded
= elf_hash_table (info
)->dyn_loaded
;
10532 loaded
= loaded
->next
)
10535 Elf_Internal_Shdr
*hdr
;
10537 size_t extsymcount
;
10539 Elf_Internal_Shdr
*versymhdr
;
10540 Elf_Internal_Sym
*isym
;
10541 Elf_Internal_Sym
*isymend
;
10542 Elf_Internal_Sym
*isymbuf
;
10543 Elf_External_Versym
*ever
;
10544 Elf_External_Versym
*extversym
;
10546 input
= loaded
->abfd
;
10548 /* We check each DSO for a possible hidden versioned definition. */
10550 || elf_dynversym (input
) == 0)
10553 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
10555 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
10556 if (elf_bad_symtab (input
))
10558 extsymcount
= symcount
;
10563 extsymcount
= symcount
- hdr
->sh_info
;
10564 extsymoff
= hdr
->sh_info
;
10567 if (extsymcount
== 0)
10570 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
10572 if (isymbuf
== NULL
)
10575 /* Read in any version definitions. */
10576 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
10577 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
10578 || (extversym
= (Elf_External_Versym
*)
10579 _bfd_malloc_and_read (input
, versymhdr
->sh_size
,
10580 versymhdr
->sh_size
)) == NULL
)
10586 ever
= extversym
+ extsymoff
;
10587 isymend
= isymbuf
+ extsymcount
;
10588 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
10591 Elf_Internal_Versym iver
;
10592 unsigned short version_index
;
10594 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
10595 || isym
->st_shndx
== SHN_UNDEF
)
10598 name
= bfd_elf_string_from_elf_section (input
,
10601 if (strcmp (name
, h
->root
.root
.string
) != 0)
10604 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
10606 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
10607 && !(h
->def_regular
10608 && h
->forced_local
))
10610 /* If we have a non-hidden versioned sym, then it should
10611 have provided a definition for the undefined sym unless
10612 it is defined in a non-shared object and forced local.
10617 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
10618 if (version_index
== 1 || version_index
== 2)
10620 /* This is the base or first version. We can use it. */
10634 /* Convert ELF common symbol TYPE. */
10637 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
10639 /* Commom symbol can only appear in relocatable link. */
10640 if (!bfd_link_relocatable (info
))
10642 switch (info
->elf_stt_common
)
10646 case elf_stt_common
:
10649 case no_elf_stt_common
:
10656 /* Add an external symbol to the symbol table. This is called from
10657 the hash table traversal routine. When generating a shared object,
10658 we go through the symbol table twice. The first time we output
10659 anything that might have been forced to local scope in a version
10660 script. The second time we output the symbols that are still
10664 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
10666 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
10667 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
10668 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
10670 Elf_Internal_Sym sym
;
10671 asection
*input_sec
;
10672 const struct elf_backend_data
*bed
;
10677 if (h
->root
.type
== bfd_link_hash_warning
)
10679 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10680 if (h
->root
.type
== bfd_link_hash_new
)
10684 /* Decide whether to output this symbol in this pass. */
10685 if (eoinfo
->localsyms
)
10687 if (!h
->forced_local
)
10692 if (h
->forced_local
)
10696 bed
= get_elf_backend_data (flinfo
->output_bfd
);
10698 if (h
->root
.type
== bfd_link_hash_undefined
)
10700 /* If we have an undefined symbol reference here then it must have
10701 come from a shared library that is being linked in. (Undefined
10702 references in regular files have already been handled unless
10703 they are in unreferenced sections which are removed by garbage
10705 bool ignore_undef
= false;
10707 /* Some symbols may be special in that the fact that they're
10708 undefined can be safely ignored - let backend determine that. */
10709 if (bed
->elf_backend_ignore_undef_symbol
)
10710 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
10712 /* If we are reporting errors for this situation then do so now. */
10714 && h
->ref_dynamic_nonweak
10715 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
10716 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
10717 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
10719 flinfo
->info
->callbacks
->undefined_symbol
10720 (flinfo
->info
, h
->root
.root
.string
,
10721 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
, NULL
, 0,
10722 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_DIAGNOSE
10723 && !flinfo
->info
->warn_unresolved_syms
);
10726 /* Strip a global symbol defined in a discarded section. */
10731 /* We should also warn if a forced local symbol is referenced from
10732 shared libraries. */
10733 if (bfd_link_executable (flinfo
->info
)
10738 && h
->ref_dynamic_nonweak
10739 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
10743 struct elf_link_hash_entry
*hi
= h
;
10745 /* Check indirect symbol. */
10746 while (hi
->root
.type
== bfd_link_hash_indirect
)
10747 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
10749 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
10750 /* xgettext:c-format */
10751 msg
= _("%pB: internal symbol `%s' in %pB is referenced by DSO");
10752 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
10753 /* xgettext:c-format */
10754 msg
= _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
10756 /* xgettext:c-format */
10757 msg
= _("%pB: local symbol `%s' in %pB is referenced by DSO");
10758 def_bfd
= flinfo
->output_bfd
;
10759 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
10760 def_bfd
= hi
->root
.u
.def
.section
->owner
;
10761 _bfd_error_handler (msg
, flinfo
->output_bfd
,
10762 h
->root
.root
.string
, def_bfd
);
10763 bfd_set_error (bfd_error_bad_value
);
10764 eoinfo
->failed
= true;
10768 /* We don't want to output symbols that have never been mentioned by
10769 a regular file, or that we have been told to strip. However, if
10770 h->indx is set to -2, the symbol is used by a reloc and we must
10775 else if ((h
->def_dynamic
10777 || h
->root
.type
== bfd_link_hash_new
)
10779 && !h
->ref_regular
)
10781 else if (flinfo
->info
->strip
== strip_all
)
10783 else if (flinfo
->info
->strip
== strip_some
10784 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
10785 h
->root
.root
.string
, false, false) == NULL
)
10787 else if ((h
->root
.type
== bfd_link_hash_defined
10788 || h
->root
.type
== bfd_link_hash_defweak
)
10789 && ((flinfo
->info
->strip_discarded
10790 && discarded_section (h
->root
.u
.def
.section
))
10791 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
10792 && h
->root
.u
.def
.section
->owner
!= NULL
10793 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
10795 else if ((h
->root
.type
== bfd_link_hash_undefined
10796 || h
->root
.type
== bfd_link_hash_undefweak
)
10797 && h
->root
.u
.undef
.abfd
!= NULL
10798 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
10801 /* Remember if this symbol should be stripped. */
10802 bool should_strip
= strip
;
10804 /* Strip undefined weak symbols link if they don't have relocation. */
10806 strip
= !h
->has_reloc
&& h
->root
.type
== bfd_link_hash_undefweak
;
10810 /* If we're stripping it, and it's not a dynamic symbol, there's
10811 nothing else to do. However, if it is a forced local symbol or
10812 an ifunc symbol we need to give the backend finish_dynamic_symbol
10813 function a chance to make it dynamic. */
10815 && h
->dynindx
== -1
10816 && type
!= STT_GNU_IFUNC
10817 && !h
->forced_local
)
10821 sym
.st_size
= h
->size
;
10822 sym
.st_other
= h
->other
;
10823 switch (h
->root
.type
)
10826 case bfd_link_hash_new
:
10827 case bfd_link_hash_warning
:
10831 case bfd_link_hash_undefined
:
10832 case bfd_link_hash_undefweak
:
10833 input_sec
= bfd_und_section_ptr
;
10834 sym
.st_shndx
= SHN_UNDEF
;
10837 case bfd_link_hash_defined
:
10838 case bfd_link_hash_defweak
:
10840 input_sec
= h
->root
.u
.def
.section
;
10841 if (input_sec
->output_section
!= NULL
)
10844 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
10845 input_sec
->output_section
);
10846 if (sym
.st_shndx
== SHN_BAD
)
10849 /* xgettext:c-format */
10850 (_("%pB: could not find output section %pA for input section %pA"),
10851 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
10852 bfd_set_error (bfd_error_nonrepresentable_section
);
10853 eoinfo
->failed
= true;
10857 /* ELF symbols in relocatable files are section relative,
10858 but in nonrelocatable files they are virtual
10860 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
10861 if (!bfd_link_relocatable (flinfo
->info
))
10863 sym
.st_value
+= input_sec
->output_section
->vma
;
10864 if (h
->type
== STT_TLS
)
10866 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
10867 if (tls_sec
!= NULL
)
10868 sym
.st_value
-= tls_sec
->vma
;
10874 BFD_ASSERT (input_sec
->owner
== NULL
10875 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
10876 sym
.st_shndx
= SHN_UNDEF
;
10877 input_sec
= bfd_und_section_ptr
;
10882 case bfd_link_hash_common
:
10883 input_sec
= h
->root
.u
.c
.p
->section
;
10884 sym
.st_shndx
= bed
->common_section_index (input_sec
);
10885 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
10888 case bfd_link_hash_indirect
:
10889 /* These symbols are created by symbol versioning. They point
10890 to the decorated version of the name. For example, if the
10891 symbol foo@@GNU_1.2 is the default, which should be used when
10892 foo is used with no version, then we add an indirect symbol
10893 foo which points to foo@@GNU_1.2. We ignore these symbols,
10894 since the indirected symbol is already in the hash table. */
10898 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
10899 switch (h
->root
.type
)
10901 case bfd_link_hash_common
:
10902 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10904 case bfd_link_hash_defined
:
10905 case bfd_link_hash_defweak
:
10906 if (bed
->common_definition (&sym
))
10907 type
= elf_link_convert_common_type (flinfo
->info
, type
);
10911 case bfd_link_hash_undefined
:
10912 case bfd_link_hash_undefweak
:
10918 if (h
->forced_local
)
10920 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
10921 /* Turn off visibility on local symbol. */
10922 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
10924 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10925 else if (h
->unique_global
&& h
->def_regular
)
10926 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
10927 else if (h
->root
.type
== bfd_link_hash_undefweak
10928 || h
->root
.type
== bfd_link_hash_defweak
)
10929 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
10931 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
10932 sym
.st_target_internal
= h
->target_internal
;
10934 /* Give the processor backend a chance to tweak the symbol value,
10935 and also to finish up anything that needs to be done for this
10936 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10937 forced local syms when non-shared is due to a historical quirk.
10938 STT_GNU_IFUNC symbol must go through PLT. */
10939 if ((h
->type
== STT_GNU_IFUNC
10941 && !bfd_link_relocatable (flinfo
->info
))
10942 || ((h
->dynindx
!= -1
10943 || h
->forced_local
)
10944 && ((bfd_link_pic (flinfo
->info
)
10945 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
10946 || h
->root
.type
!= bfd_link_hash_undefweak
))
10947 || !h
->forced_local
)
10948 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
10950 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
10951 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
10953 eoinfo
->failed
= true;
10956 /* If a symbol is in the dynamic symbol table and isn't a
10957 should-strip symbol, also keep it in the symbol table. */
10962 /* If we are marking the symbol as undefined, and there are no
10963 non-weak references to this symbol from a regular object, then
10964 mark the symbol as weak undefined; if there are non-weak
10965 references, mark the symbol as strong. We can't do this earlier,
10966 because it might not be marked as undefined until the
10967 finish_dynamic_symbol routine gets through with it. */
10968 if (sym
.st_shndx
== SHN_UNDEF
10970 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
10971 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
10974 type
= ELF_ST_TYPE (sym
.st_info
);
10976 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10977 if (type
== STT_GNU_IFUNC
)
10980 if (h
->ref_regular_nonweak
)
10981 bindtype
= STB_GLOBAL
;
10983 bindtype
= STB_WEAK
;
10984 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
10987 /* If this is a symbol defined in a dynamic library, don't use the
10988 symbol size from the dynamic library. Relinking an executable
10989 against a new library may introduce gratuitous changes in the
10990 executable's symbols if we keep the size. */
10991 if (sym
.st_shndx
== SHN_UNDEF
10996 /* If a non-weak symbol with non-default visibility is not defined
10997 locally, it is a fatal error. */
10998 if (!bfd_link_relocatable (flinfo
->info
)
10999 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
11000 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
11001 && h
->root
.type
== bfd_link_hash_undefined
11002 && !h
->def_regular
)
11006 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
11007 /* xgettext:c-format */
11008 msg
= _("%pB: protected symbol `%s' isn't defined");
11009 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
11010 /* xgettext:c-format */
11011 msg
= _("%pB: internal symbol `%s' isn't defined");
11013 /* xgettext:c-format */
11014 msg
= _("%pB: hidden symbol `%s' isn't defined");
11015 _bfd_error_handler (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
11016 bfd_set_error (bfd_error_bad_value
);
11017 eoinfo
->failed
= true;
11021 /* If this symbol should be put in the .dynsym section, then put it
11022 there now. We already know the symbol index. We also fill in
11023 the entry in the .hash section. */
11024 if (h
->dynindx
!= -1
11025 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
11026 && elf_hash_table (flinfo
->info
)->dynsym
!= NULL
11027 && !discarded_section (elf_hash_table (flinfo
->info
)->dynsym
))
11031 /* Since there is no version information in the dynamic string,
11032 if there is no version info in symbol version section, we will
11033 have a run-time problem if not linking executable, referenced
11034 by shared library, or not bound locally. */
11035 if (h
->verinfo
.verdef
== NULL
11036 && (!bfd_link_executable (flinfo
->info
)
11038 || !h
->def_regular
))
11040 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
11042 if (p
&& p
[1] != '\0')
11045 /* xgettext:c-format */
11046 (_("%pB: no symbol version section for versioned symbol `%s'"),
11047 flinfo
->output_bfd
, h
->root
.root
.string
);
11048 eoinfo
->failed
= true;
11053 sym
.st_name
= h
->dynstr_index
;
11054 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
11055 + h
->dynindx
* bed
->s
->sizeof_sym
);
11056 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
11058 eoinfo
->failed
= true;
11062 /* Inform the linker of the addition of this symbol. */
11064 if (flinfo
->info
->callbacks
->ctf_new_dynsym
)
11065 flinfo
->info
->callbacks
->ctf_new_dynsym (h
->dynindx
, &sym
);
11067 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
11069 if (flinfo
->hash_sec
!= NULL
)
11071 size_t hash_entry_size
;
11072 bfd_byte
*bucketpos
;
11074 size_t bucketcount
;
11077 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
11078 bucket
= h
->u
.elf_hash_value
% bucketcount
;
11081 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
11082 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
11083 + (bucket
+ 2) * hash_entry_size
);
11084 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
11085 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
11087 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
11088 ((bfd_byte
*) flinfo
->hash_sec
->contents
11089 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
11092 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
11094 Elf_Internal_Versym iversym
;
11095 Elf_External_Versym
*eversym
;
11097 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
11099 if (h
->verinfo
.verdef
== NULL
11100 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
11101 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
11102 iversym
.vs_vers
= 1;
11104 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
11108 if (h
->verinfo
.vertree
== NULL
)
11109 iversym
.vs_vers
= 1;
11111 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
11112 if (flinfo
->info
->create_default_symver
)
11116 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
11117 defined locally. */
11118 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
11119 iversym
.vs_vers
|= VERSYM_HIDDEN
;
11121 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
11122 eversym
+= h
->dynindx
;
11123 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
11127 /* If the symbol is undefined, and we didn't output it to .dynsym,
11128 strip it from .symtab too. Obviously we can't do this for
11129 relocatable output or when needed for --emit-relocs. */
11130 else if (input_sec
== bfd_und_section_ptr
11132 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
11133 && (h
->mark
!= 1 || ELF_ST_BIND (sym
.st_info
) != STB_GLOBAL
)
11134 && !bfd_link_relocatable (flinfo
->info
))
11137 /* Also strip others that we couldn't earlier due to dynamic symbol
11141 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
11144 /* Output a FILE symbol so that following locals are not associated
11145 with the wrong input file. We need one for forced local symbols
11146 if we've seen more than one FILE symbol or when we have exactly
11147 one FILE symbol but global symbols are present in a file other
11148 than the one with the FILE symbol. We also need one if linker
11149 defined symbols are present. In practice these conditions are
11150 always met, so just emit the FILE symbol unconditionally. */
11151 if (eoinfo
->localsyms
11152 && !eoinfo
->file_sym_done
11153 && eoinfo
->flinfo
->filesym_count
!= 0)
11155 Elf_Internal_Sym fsym
;
11157 memset (&fsym
, 0, sizeof (fsym
));
11158 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11159 fsym
.st_shndx
= SHN_ABS
;
11160 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
11161 bfd_und_section_ptr
, NULL
))
11164 eoinfo
->file_sym_done
= true;
11167 indx
= bfd_get_symcount (flinfo
->output_bfd
);
11168 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
11172 eoinfo
->failed
= true;
11177 else if (h
->indx
== -2)
11183 /* Return TRUE if special handling is done for relocs in SEC against
11184 symbols defined in discarded sections. */
11187 elf_section_ignore_discarded_relocs (asection
*sec
)
11189 const struct elf_backend_data
*bed
;
11191 switch (sec
->sec_info_type
)
11193 case SEC_INFO_TYPE_STABS
:
11194 case SEC_INFO_TYPE_EH_FRAME
:
11195 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
11196 case SEC_INFO_TYPE_SFRAME
:
11202 bed
= get_elf_backend_data (sec
->owner
);
11203 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
11204 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
11210 /* Return a mask saying how ld should treat relocations in SEC against
11211 symbols defined in discarded sections. If this function returns
11212 COMPLAIN set, ld will issue a warning message. If this function
11213 returns PRETEND set, and the discarded section was link-once and the
11214 same size as the kept link-once section, ld will pretend that the
11215 symbol was actually defined in the kept section. Otherwise ld will
11216 zero the reloc (at least that is the intent, but some cooperation by
11217 the target dependent code is needed, particularly for REL targets). */
11220 _bfd_elf_default_action_discarded (asection
*sec
)
11222 const struct elf_backend_data
*bed
;
11223 bed
= get_elf_backend_data (sec
->owner
);
11225 if (sec
->flags
& SEC_DEBUGGING
)
11228 if (strcmp (".eh_frame", sec
->name
) == 0)
11231 if (bed
->elf_backend_can_make_multiple_eh_frame
11232 && strncmp (sec
->name
, ".eh_frame.", 10) == 0)
11235 if (strcmp (".sframe", sec
->name
) == 0)
11238 if (strcmp (".gcc_except_table", sec
->name
) == 0)
11241 return COMPLAIN
| PRETEND
;
11244 /* Find a match between a section and a member of a section group. */
11247 match_group_member (asection
*sec
, asection
*group
,
11248 struct bfd_link_info
*info
)
11250 asection
*first
= elf_next_in_group (group
);
11251 asection
*s
= first
;
11255 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
11258 s
= elf_next_in_group (s
);
11266 /* Check if the kept section of a discarded section SEC can be used
11267 to replace it. Return the replacement if it is OK. Otherwise return
11271 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
11275 kept
= sec
->kept_section
;
11278 if ((kept
->flags
& SEC_GROUP
) != 0)
11279 kept
= match_group_member (sec
, kept
, info
);
11282 if ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
11283 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
))
11287 /* Get the real kept section. */
11289 for (next
= kept
->kept_section
;
11291 next
= next
->kept_section
)
11295 sec
->kept_section
= kept
;
11300 /* Link an input file into the linker output file. This function
11301 handles all the sections and relocations of the input file at once.
11302 This is so that we only have to read the local symbols once, and
11303 don't have to keep them in memory. */
11306 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
11308 int (*relocate_section
)
11309 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
11310 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
11312 Elf_Internal_Shdr
*symtab_hdr
;
11313 size_t locsymcount
;
11315 Elf_Internal_Sym
*isymbuf
;
11316 Elf_Internal_Sym
*isym
;
11317 Elf_Internal_Sym
*isymend
;
11319 asection
**ppsection
;
11321 const struct elf_backend_data
*bed
;
11322 struct elf_link_hash_entry
**sym_hashes
;
11323 bfd_size_type address_size
;
11324 bfd_vma r_type_mask
;
11326 bool have_file_sym
= false;
11328 output_bfd
= flinfo
->output_bfd
;
11329 bed
= get_elf_backend_data (output_bfd
);
11330 relocate_section
= bed
->elf_backend_relocate_section
;
11332 /* If this is a dynamic object, we don't want to do anything here:
11333 we don't want the local symbols, and we don't want the section
11335 if ((input_bfd
->flags
& DYNAMIC
) != 0)
11338 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
11339 if (elf_bad_symtab (input_bfd
))
11341 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11346 locsymcount
= symtab_hdr
->sh_info
;
11347 extsymoff
= symtab_hdr
->sh_info
;
11350 /* Enable GNU OSABI features in the output BFD that are used in the input
11352 if (bed
->elf_osabi
== ELFOSABI_NONE
11353 || bed
->elf_osabi
== ELFOSABI_GNU
11354 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
11355 elf_tdata (output_bfd
)->has_gnu_osabi
11356 |= (elf_tdata (input_bfd
)->has_gnu_osabi
11357 & (bfd_link_relocatable (flinfo
->info
)
11358 ? -1 : ~elf_gnu_osabi_retain
));
11360 /* Read the local symbols. */
11361 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11362 if (isymbuf
== NULL
&& locsymcount
!= 0)
11364 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
11365 flinfo
->internal_syms
,
11366 flinfo
->external_syms
,
11367 flinfo
->locsym_shndx
);
11368 if (isymbuf
== NULL
)
11372 /* Find local symbol sections and adjust values of symbols in
11373 SEC_MERGE sections. Write out those local symbols we know are
11374 going into the output file. */
11375 isymend
= PTR_ADD (isymbuf
, locsymcount
);
11376 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
11378 isym
++, pindex
++, ppsection
++)
11382 Elf_Internal_Sym osym
;
11388 if (elf_bad_symtab (input_bfd
))
11390 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
11397 if (isym
->st_shndx
== SHN_UNDEF
)
11398 isec
= bfd_und_section_ptr
;
11399 else if (isym
->st_shndx
== SHN_ABS
)
11400 isec
= bfd_abs_section_ptr
;
11401 else if (isym
->st_shndx
== SHN_COMMON
)
11402 isec
= bfd_com_section_ptr
;
11405 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
11408 /* Don't attempt to output symbols with st_shnx in the
11409 reserved range other than SHN_ABS and SHN_COMMON. */
11410 isec
= bfd_und_section_ptr
;
11412 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
11413 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
11415 _bfd_merged_section_offset (output_bfd
, &isec
,
11416 elf_section_data (isec
)->sec_info
,
11422 /* Don't output the first, undefined, symbol. In fact, don't
11423 output any undefined local symbol. */
11424 if (isec
== bfd_und_section_ptr
)
11427 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
11429 /* We never output section symbols. Instead, we use the
11430 section symbol of the corresponding section in the output
11435 /* If we are stripping all symbols, we don't want to output this
11437 if (flinfo
->info
->strip
== strip_all
)
11440 /* If we are discarding all local symbols, we don't want to
11441 output this one. If we are generating a relocatable output
11442 file, then some of the local symbols may be required by
11443 relocs; we output them below as we discover that they are
11445 if (flinfo
->info
->discard
== discard_all
)
11448 /* If this symbol is defined in a section which we are
11449 discarding, we don't need to keep it. */
11450 if (isym
->st_shndx
< SHN_LORESERVE
11451 && (isec
->output_section
== NULL
11452 || bfd_section_removed_from_list (output_bfd
,
11453 isec
->output_section
)))
11456 /* Get the name of the symbol. */
11457 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
11462 /* See if we are discarding symbols with this name. */
11463 if ((flinfo
->info
->strip
== strip_some
11464 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, false, false)
11466 || (((flinfo
->info
->discard
== discard_sec_merge
11467 && (isec
->flags
& SEC_MERGE
)
11468 && !bfd_link_relocatable (flinfo
->info
))
11469 || flinfo
->info
->discard
== discard_l
)
11470 && bfd_is_local_label_name (input_bfd
, name
)))
11473 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
11475 if (input_bfd
->lto_output
)
11476 /* -flto puts a temp file name here. This means builds
11477 are not reproducible. Discard the symbol. */
11479 have_file_sym
= true;
11480 flinfo
->filesym_count
+= 1;
11482 if (!have_file_sym
)
11484 /* In the absence of debug info, bfd_find_nearest_line uses
11485 FILE symbols to determine the source file for local
11486 function symbols. Provide a FILE symbol here if input
11487 files lack such, so that their symbols won't be
11488 associated with a previous input file. It's not the
11489 source file, but the best we can do. */
11490 const char *filename
;
11491 have_file_sym
= true;
11492 flinfo
->filesym_count
+= 1;
11493 memset (&osym
, 0, sizeof (osym
));
11494 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
11495 osym
.st_shndx
= SHN_ABS
;
11496 if (input_bfd
->lto_output
)
11499 filename
= lbasename (bfd_get_filename (input_bfd
));
11500 if (!elf_link_output_symstrtab (flinfo
, filename
, &osym
,
11501 bfd_abs_section_ptr
, NULL
))
11507 /* Adjust the section index for the output file. */
11508 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11509 isec
->output_section
);
11510 if (osym
.st_shndx
== SHN_BAD
)
11513 /* ELF symbols in relocatable files are section relative, but
11514 in executable files they are virtual addresses. Note that
11515 this code assumes that all ELF sections have an associated
11516 BFD section with a reasonable value for output_offset; below
11517 we assume that they also have a reasonable value for
11518 output_section. Any special sections must be set up to meet
11519 these requirements. */
11520 osym
.st_value
+= isec
->output_offset
;
11521 if (!bfd_link_relocatable (flinfo
->info
))
11523 osym
.st_value
+= isec
->output_section
->vma
;
11524 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
11526 /* STT_TLS symbols are relative to PT_TLS segment base. */
11527 if (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
)
11528 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
11530 osym
.st_info
= ELF_ST_INFO (ELF_ST_BIND (osym
.st_info
),
11535 indx
= bfd_get_symcount (output_bfd
);
11536 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
11543 if (bed
->s
->arch_size
== 32)
11545 r_type_mask
= 0xff;
11551 r_type_mask
= 0xffffffff;
11556 /* Relocate the contents of each section. */
11557 sym_hashes
= elf_sym_hashes (input_bfd
);
11558 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
11560 bfd_byte
*contents
;
11562 if (! o
->linker_mark
)
11564 /* This section was omitted from the link. */
11568 if (!flinfo
->info
->resolve_section_groups
11569 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
11571 /* Deal with the group signature symbol. */
11572 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
11573 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
11574 asection
*osec
= o
->output_section
;
11576 BFD_ASSERT (bfd_link_relocatable (flinfo
->info
));
11577 if (symndx
>= locsymcount
11578 || (elf_bad_symtab (input_bfd
)
11579 && flinfo
->sections
[symndx
] == NULL
))
11581 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
11582 while (h
->root
.type
== bfd_link_hash_indirect
11583 || h
->root
.type
== bfd_link_hash_warning
)
11584 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11585 /* Arrange for symbol to be output. */
11587 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
11589 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
11591 /* We'll use the output section target_index. */
11592 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11593 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
11597 if (flinfo
->indices
[symndx
] == -1)
11599 /* Otherwise output the local symbol now. */
11600 Elf_Internal_Sym sym
= isymbuf
[symndx
];
11601 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
11606 name
= bfd_elf_string_from_elf_section (input_bfd
,
11607 symtab_hdr
->sh_link
,
11612 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
11614 if (sym
.st_shndx
== SHN_BAD
)
11617 sym
.st_value
+= o
->output_offset
;
11619 indx
= bfd_get_symcount (output_bfd
);
11620 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
11625 flinfo
->indices
[symndx
] = indx
;
11629 elf_section_data (osec
)->this_hdr
.sh_info
11630 = flinfo
->indices
[symndx
];
11634 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11635 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
11638 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
11640 /* Section was created by _bfd_elf_link_create_dynamic_sections
11645 /* Get the contents of the section. They have been cached by a
11646 relaxation routine. Note that o is a section in an input
11647 file, so the contents field will not have been set by any of
11648 the routines which work on output files. */
11649 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
11651 contents
= elf_section_data (o
)->this_hdr
.contents
;
11652 if (bed
->caches_rawsize
11654 && o
->rawsize
< o
->size
)
11656 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
11657 contents
= flinfo
->contents
;
11660 else if (!(o
->flags
& SEC_RELOC
)
11661 && !bed
->elf_backend_write_section
11662 && o
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
11663 /* A MERGE section that has no relocations doesn't need the
11664 contents anymore, they have been recorded earlier. Except
11665 if the backend has special provisions for writing sections. */
11669 contents
= flinfo
->contents
;
11670 if (! _bfd_elf_link_mmap_section_contents (input_bfd
, o
,
11675 if ((o
->flags
& SEC_RELOC
) != 0)
11677 Elf_Internal_Rela
*internal_relocs
;
11678 Elf_Internal_Rela
*rel
, *relend
;
11679 int action_discarded
;
11682 /* Get the swapped relocs. */
11684 = _bfd_elf_link_info_read_relocs (input_bfd
, flinfo
->info
, o
,
11685 flinfo
->external_relocs
,
11686 flinfo
->internal_relocs
,
11688 if (internal_relocs
== NULL
11689 && o
->reloc_count
> 0)
11692 action_discarded
= -1;
11693 if (!elf_section_ignore_discarded_relocs (o
))
11694 action_discarded
= (*bed
->action_discarded
) (o
);
11696 /* Run through the relocs evaluating complex reloc symbols and
11697 looking for relocs against symbols from discarded sections
11698 or section symbols from removed link-once sections.
11699 Complain about relocs against discarded sections. Zero
11700 relocs against removed link-once sections. */
11702 rel
= internal_relocs
;
11703 relend
= rel
+ o
->reloc_count
;
11704 for ( ; rel
< relend
; rel
++)
11706 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
11707 unsigned int s_type
;
11708 asection
**ps
, *sec
;
11709 struct elf_link_hash_entry
*h
= NULL
;
11710 const char *sym_name
;
11712 if (r_symndx
== STN_UNDEF
)
11715 if (r_symndx
>= locsymcount
11716 || (elf_bad_symtab (input_bfd
)
11717 && flinfo
->sections
[r_symndx
] == NULL
))
11719 h
= sym_hashes
[r_symndx
- extsymoff
];
11721 /* Badly formatted input files can contain relocs that
11722 reference non-existant symbols. Check here so that
11723 we do not seg fault. */
11727 /* xgettext:c-format */
11728 (_("error: %pB contains a reloc (%#" PRIx64
") for section %pA "
11729 "that references a non-existent global symbol"),
11730 input_bfd
, (uint64_t) rel
->r_info
, o
);
11731 bfd_set_error (bfd_error_bad_value
);
11735 while (h
->root
.type
== bfd_link_hash_indirect
11736 || h
->root
.type
== bfd_link_hash_warning
)
11737 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
11741 /* If a plugin symbol is referenced from a non-IR file,
11742 mark the symbol as undefined. Note that the
11743 linker may attach linker created dynamic sections
11744 to the plugin bfd. Symbols defined in linker
11745 created sections are not plugin symbols. */
11746 if ((h
->root
.non_ir_ref_regular
11747 || h
->root
.non_ir_ref_dynamic
)
11748 && (h
->root
.type
== bfd_link_hash_defined
11749 || h
->root
.type
== bfd_link_hash_defweak
)
11750 && (h
->root
.u
.def
.section
->flags
11751 & SEC_LINKER_CREATED
) == 0
11752 && h
->root
.u
.def
.section
->owner
!= NULL
11753 && (h
->root
.u
.def
.section
->owner
->flags
11754 & BFD_PLUGIN
) != 0)
11756 h
->root
.type
= bfd_link_hash_undefined
;
11757 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
11761 if (h
->root
.type
== bfd_link_hash_defined
11762 || h
->root
.type
== bfd_link_hash_defweak
)
11763 ps
= &h
->root
.u
.def
.section
;
11765 sym_name
= h
->root
.root
.string
;
11769 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
11771 s_type
= ELF_ST_TYPE (sym
->st_info
);
11772 ps
= &flinfo
->sections
[r_symndx
];
11773 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
11777 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
11778 && !bfd_link_relocatable (flinfo
->info
))
11781 bfd_vma dot
= (rel
->r_offset
11782 + o
->output_offset
+ o
->output_section
->vma
);
11784 printf ("Encountered a complex symbol!");
11785 printf (" (input_bfd %s, section %s, reloc %ld\n",
11786 bfd_get_filename (input_bfd
), o
->name
,
11787 (long) (rel
- internal_relocs
));
11788 printf (" symbol: idx %8.8lx, name %s\n",
11789 r_symndx
, sym_name
);
11790 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11791 (unsigned long) rel
->r_info
,
11792 (unsigned long) rel
->r_offset
);
11794 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
11795 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
11798 /* Symbol evaluated OK. Update to absolute value. */
11799 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
11804 if (action_discarded
!= -1 && ps
!= NULL
)
11806 /* Complain if the definition comes from a
11807 discarded section. */
11808 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
11810 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
11811 if (action_discarded
& COMPLAIN
)
11812 (*flinfo
->info
->callbacks
->einfo
)
11813 /* xgettext:c-format */
11814 (_("%X`%s' referenced in section `%pA' of %pB: "
11815 "defined in discarded section `%pA' of %pB\n"),
11816 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
11818 /* Try to do the best we can to support buggy old
11819 versions of gcc. Pretend that the symbol is
11820 really defined in the kept linkonce section.
11821 FIXME: This is quite broken. Modifying the
11822 symbol here means we will be changing all later
11823 uses of the symbol, not just in this section. */
11824 if (action_discarded
& PRETEND
)
11828 kept
= _bfd_elf_check_kept_section (sec
,
11840 /* Relocate the section by invoking a back end routine.
11842 The back end routine is responsible for adjusting the
11843 section contents as necessary, and (if using Rela relocs
11844 and generating a relocatable output file) adjusting the
11845 reloc addend as necessary.
11847 The back end routine does not have to worry about setting
11848 the reloc address or the reloc symbol index.
11850 The back end routine is given a pointer to the swapped in
11851 internal symbols, and can access the hash table entries
11852 for the external symbols via elf_sym_hashes (input_bfd).
11854 When generating relocatable output, the back end routine
11855 must handle STB_LOCAL/STT_SECTION symbols specially. The
11856 output symbol is going to be a section symbol
11857 corresponding to the output section, which will require
11858 the addend to be adjusted. */
11860 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
11861 input_bfd
, o
, contents
,
11869 || bfd_link_relocatable (flinfo
->info
)
11870 || flinfo
->info
->emitrelocations
)
11872 Elf_Internal_Rela
*irela
;
11873 Elf_Internal_Rela
*irelaend
, *irelamid
;
11874 bfd_vma last_offset
;
11875 struct elf_link_hash_entry
**rel_hash
;
11876 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
11877 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
11878 unsigned int next_erel
;
11880 struct bfd_elf_section_data
*esdi
, *esdo
;
11882 esdi
= elf_section_data (o
);
11883 esdo
= elf_section_data (o
->output_section
);
11884 rela_normal
= false;
11886 /* Adjust the reloc addresses and symbol indices. */
11888 irela
= internal_relocs
;
11889 irelaend
= irela
+ o
->reloc_count
;
11890 rel_hash
= PTR_ADD (esdo
->rel
.hashes
, esdo
->rel
.count
);
11891 /* We start processing the REL relocs, if any. When we reach
11892 IRELAMID in the loop, we switch to the RELA relocs. */
11894 if (esdi
->rel
.hdr
!= NULL
)
11895 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
11896 * bed
->s
->int_rels_per_ext_rel
);
11897 rel_hash_list
= rel_hash
;
11898 rela_hash_list
= NULL
;
11899 last_offset
= o
->output_offset
;
11900 if (!bfd_link_relocatable (flinfo
->info
))
11901 last_offset
+= o
->output_section
->vma
;
11902 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
11904 unsigned long r_symndx
;
11906 Elf_Internal_Sym sym
;
11908 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
11914 if (irela
== irelamid
)
11916 rel_hash
= PTR_ADD (esdo
->rela
.hashes
, esdo
->rela
.count
);
11917 rela_hash_list
= rel_hash
;
11918 rela_normal
= bed
->rela_normal
;
11921 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
11924 if (irela
->r_offset
>= (bfd_vma
) -2)
11926 /* This is a reloc for a deleted entry or somesuch.
11927 Turn it into an R_*_NONE reloc, at the same
11928 offset as the last reloc. elf_eh_frame.c and
11929 bfd_elf_discard_info rely on reloc offsets
11931 irela
->r_offset
= last_offset
;
11933 irela
->r_addend
= 0;
11937 irela
->r_offset
+= o
->output_offset
;
11939 /* Relocs in an executable have to be virtual addresses. */
11940 if (!bfd_link_relocatable (flinfo
->info
))
11941 irela
->r_offset
+= o
->output_section
->vma
;
11943 last_offset
= irela
->r_offset
;
11945 r_symndx
= irela
->r_info
>> r_sym_shift
;
11946 if (r_symndx
== STN_UNDEF
)
11949 if (r_symndx
>= locsymcount
11950 || (elf_bad_symtab (input_bfd
)
11951 && flinfo
->sections
[r_symndx
] == NULL
))
11953 struct elf_link_hash_entry
*rh
;
11954 unsigned long indx
;
11956 /* This is a reloc against a global symbol. We
11957 have not yet output all the local symbols, so
11958 we do not know the symbol index of any global
11959 symbol. We set the rel_hash entry for this
11960 reloc to point to the global hash table entry
11961 for this symbol. The symbol index is then
11962 set at the end of bfd_elf_final_link. */
11963 indx
= r_symndx
- extsymoff
;
11964 rh
= elf_sym_hashes (input_bfd
)[indx
];
11965 while (rh
->root
.type
== bfd_link_hash_indirect
11966 || rh
->root
.type
== bfd_link_hash_warning
)
11967 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
11969 /* Setting the index to -2 tells
11970 elf_link_output_extsym that this symbol is
11971 used by a reloc. */
11972 BFD_ASSERT (rh
->indx
< 0);
11979 /* This is a reloc against a local symbol. */
11982 sym
= isymbuf
[r_symndx
];
11983 sec
= flinfo
->sections
[r_symndx
];
11984 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
11986 /* I suppose the backend ought to fill in the
11987 section of any STT_SECTION symbol against a
11988 processor specific section. */
11989 r_symndx
= STN_UNDEF
;
11990 if (bfd_is_abs_section (sec
))
11992 else if (sec
== NULL
|| sec
->owner
== NULL
)
11994 bfd_set_error (bfd_error_bad_value
);
11999 asection
*osec
= sec
->output_section
;
12001 /* If we have discarded a section, the output
12002 section will be the absolute section. In
12003 case of discarded SEC_MERGE sections, use
12004 the kept section. relocate_section should
12005 have already handled discarded linkonce
12007 if (bfd_is_abs_section (osec
)
12008 && sec
->kept_section
!= NULL
12009 && sec
->kept_section
->output_section
!= NULL
)
12011 osec
= sec
->kept_section
->output_section
;
12012 irela
->r_addend
-= osec
->vma
;
12015 if (!bfd_is_abs_section (osec
))
12017 r_symndx
= osec
->target_index
;
12018 if (r_symndx
== STN_UNDEF
)
12020 irela
->r_addend
+= osec
->vma
;
12021 osec
= _bfd_nearby_section (output_bfd
, osec
,
12023 irela
->r_addend
-= osec
->vma
;
12024 r_symndx
= osec
->target_index
;
12029 /* Adjust the addend according to where the
12030 section winds up in the output section. */
12032 irela
->r_addend
+= sec
->output_offset
;
12036 if (flinfo
->indices
[r_symndx
] == -1)
12038 unsigned long shlink
;
12043 if (flinfo
->info
->strip
== strip_all
)
12045 /* You can't do ld -r -s. */
12046 bfd_set_error (bfd_error_invalid_operation
);
12050 /* This symbol was skipped earlier, but
12051 since it is needed by a reloc, we
12052 must output it now. */
12053 shlink
= symtab_hdr
->sh_link
;
12054 name
= (bfd_elf_string_from_elf_section
12055 (input_bfd
, shlink
, sym
.st_name
));
12059 osec
= sec
->output_section
;
12061 _bfd_elf_section_from_bfd_section (output_bfd
,
12063 if (sym
.st_shndx
== SHN_BAD
)
12066 sym
.st_value
+= sec
->output_offset
;
12067 if (!bfd_link_relocatable (flinfo
->info
))
12069 sym
.st_value
+= osec
->vma
;
12070 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
12072 struct elf_link_hash_table
*htab
12073 = elf_hash_table (flinfo
->info
);
12075 /* STT_TLS symbols are relative to PT_TLS
12077 if (htab
->tls_sec
!= NULL
)
12078 sym
.st_value
-= htab
->tls_sec
->vma
;
12081 = ELF_ST_INFO (ELF_ST_BIND (sym
.st_info
),
12086 indx
= bfd_get_symcount (output_bfd
);
12087 ret
= elf_link_output_symstrtab (flinfo
, name
,
12093 flinfo
->indices
[r_symndx
] = indx
;
12098 r_symndx
= flinfo
->indices
[r_symndx
];
12101 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
12102 | (irela
->r_info
& r_type_mask
));
12105 /* Swap out the relocs. */
12106 input_rel_hdr
= esdi
->rel
.hdr
;
12107 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
12109 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
12114 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
12115 * bed
->s
->int_rels_per_ext_rel
);
12116 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
12119 input_rela_hdr
= esdi
->rela
.hdr
;
12120 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
12122 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
12131 /* Write out the modified section contents. */
12132 if (bed
->elf_backend_write_section
12133 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
12136 /* Section written out. */
12138 else switch (o
->sec_info_type
)
12140 case SEC_INFO_TYPE_STABS
:
12141 if (! (_bfd_write_section_stabs
12143 &elf_hash_table (flinfo
->info
)->stab_info
,
12144 o
, &elf_section_data (o
)->sec_info
, contents
)))
12147 case SEC_INFO_TYPE_MERGE
:
12148 if (! _bfd_write_merged_section (output_bfd
, o
,
12149 elf_section_data (o
)->sec_info
))
12152 case SEC_INFO_TYPE_EH_FRAME
:
12154 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
12159 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
12161 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
12167 case SEC_INFO_TYPE_SFRAME
:
12169 /* Merge .sframe sections into the ctf frame encoder
12170 context of the output_bfd's section. The final .sframe
12171 output section will be written out later. */
12172 if (!_bfd_elf_merge_section_sframe (output_bfd
, flinfo
->info
,
12179 if (! (o
->flags
& SEC_EXCLUDE
))
12181 file_ptr offset
= (file_ptr
) o
->output_offset
;
12182 bfd_size_type todo
= o
->size
;
12184 offset
*= bfd_octets_per_byte (output_bfd
, o
);
12186 if ((o
->flags
& SEC_ELF_REVERSE_COPY
)
12187 && o
->size
> address_size
)
12189 /* Reverse-copy input section to output. */
12191 if ((o
->size
& (address_size
- 1)) != 0
12192 || (o
->reloc_count
!= 0
12193 && (o
->size
* bed
->s
->int_rels_per_ext_rel
12194 != o
->reloc_count
* address_size
)))
12197 /* xgettext:c-format */
12198 (_("error: %pB: size of section %pA is not "
12199 "multiple of address size"),
12201 bfd_set_error (bfd_error_bad_value
);
12207 todo
-= address_size
;
12208 if (! bfd_set_section_contents (output_bfd
,
12216 offset
+= address_size
;
12220 else if (! bfd_set_section_contents (output_bfd
,
12230 /* Munmap the section contents for each input section. */
12231 _bfd_elf_link_munmap_section_contents (o
);
12237 /* Generate a reloc when linking an ELF file. This is a reloc
12238 requested by the linker, and does not come from any input file. This
12239 is used to build constructor and destructor tables when linking
12243 elf_reloc_link_order (bfd
*output_bfd
,
12244 struct bfd_link_info
*info
,
12245 asection
*output_section
,
12246 struct bfd_link_order
*link_order
)
12248 reloc_howto_type
*howto
;
12252 struct bfd_elf_section_reloc_data
*reldata
;
12253 struct elf_link_hash_entry
**rel_hash_ptr
;
12254 Elf_Internal_Shdr
*rel_hdr
;
12255 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
12256 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
12259 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
12261 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
12264 bfd_set_error (bfd_error_bad_value
);
12268 addend
= link_order
->u
.reloc
.p
->addend
;
12271 reldata
= &esdo
->rel
;
12272 else if (esdo
->rela
.hdr
)
12273 reldata
= &esdo
->rela
;
12280 /* Figure out the symbol index. */
12281 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
12282 if (link_order
->type
== bfd_section_reloc_link_order
)
12284 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
12285 BFD_ASSERT (indx
!= 0);
12286 *rel_hash_ptr
= NULL
;
12290 struct elf_link_hash_entry
*h
;
12292 /* Treat a reloc against a defined symbol as though it were
12293 actually against the section. */
12294 h
= ((struct elf_link_hash_entry
*)
12295 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
12296 link_order
->u
.reloc
.p
->u
.name
,
12297 false, false, true));
12299 && (h
->root
.type
== bfd_link_hash_defined
12300 || h
->root
.type
== bfd_link_hash_defweak
))
12304 section
= h
->root
.u
.def
.section
;
12305 indx
= section
->output_section
->target_index
;
12306 *rel_hash_ptr
= NULL
;
12307 /* It seems that we ought to add the symbol value to the
12308 addend here, but in practice it has already been added
12309 because it was passed to constructor_callback. */
12310 addend
+= section
->output_section
->vma
+ section
->output_offset
;
12312 else if (h
!= NULL
)
12314 /* Setting the index to -2 tells elf_link_output_extsym that
12315 this symbol is used by a reloc. */
12322 (*info
->callbacks
->unattached_reloc
)
12323 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
12328 /* If this is an inplace reloc, we must write the addend into the
12330 if (howto
->partial_inplace
&& addend
!= 0)
12332 bfd_size_type size
;
12333 bfd_reloc_status_type rstat
;
12336 const char *sym_name
;
12337 bfd_size_type octets
;
12339 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
12340 buf
= (bfd_byte
*) bfd_zmalloc (size
);
12341 if (buf
== NULL
&& size
!= 0)
12343 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
12350 case bfd_reloc_outofrange
:
12353 case bfd_reloc_overflow
:
12354 if (link_order
->type
== bfd_section_reloc_link_order
)
12355 sym_name
= bfd_section_name (link_order
->u
.reloc
.p
->u
.section
);
12357 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
12358 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
12359 howto
->name
, addend
, NULL
, NULL
,
12364 octets
= link_order
->offset
* bfd_octets_per_byte (output_bfd
,
12366 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
12373 /* The address of a reloc is relative to the section in a
12374 relocatable file, and is a virtual address in an executable
12376 offset
= link_order
->offset
;
12377 if (! bfd_link_relocatable (info
))
12378 offset
+= output_section
->vma
;
12380 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
12382 irel
[i
].r_offset
= offset
;
12383 irel
[i
].r_info
= 0;
12384 irel
[i
].r_addend
= 0;
12386 if (bed
->s
->arch_size
== 32)
12387 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
12389 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
12391 rel_hdr
= reldata
->hdr
;
12392 erel
= rel_hdr
->contents
;
12393 if (rel_hdr
->sh_type
== SHT_REL
)
12395 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
12396 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
12400 irel
[0].r_addend
= addend
;
12401 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
12402 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
12410 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12411 Returns TRUE upon success, FALSE otherwise. */
12414 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
12418 const struct elf_backend_data
*bed
;
12420 enum bfd_architecture arch
;
12422 asymbol
**sympp
= NULL
;
12426 elf_symbol_type
*osymbuf
;
12429 implib_bfd
= info
->out_implib_bfd
;
12430 bed
= get_elf_backend_data (abfd
);
12432 if (!bfd_set_format (implib_bfd
, bfd_object
))
12435 /* Use flag from executable but make it a relocatable object. */
12436 flags
= bfd_get_file_flags (abfd
);
12437 flags
&= ~HAS_RELOC
;
12438 if (!bfd_set_start_address (implib_bfd
, 0)
12439 || !bfd_set_file_flags (implib_bfd
, flags
& ~EXEC_P
))
12442 /* Copy architecture of output file to import library file. */
12443 arch
= bfd_get_arch (abfd
);
12444 mach
= bfd_get_mach (abfd
);
12445 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
12446 && (abfd
->target_defaulted
12447 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
12450 /* Get symbol table size. */
12451 symsize
= bfd_get_symtab_upper_bound (abfd
);
12455 /* Read in the symbol table. */
12456 sympp
= (asymbol
**) bfd_malloc (symsize
);
12460 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
12464 /* Allow the BFD backend to copy any private header data it
12465 understands from the output BFD to the import library BFD. */
12466 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
12469 /* Filter symbols to appear in the import library. */
12470 if (bed
->elf_backend_filter_implib_symbols
)
12471 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
12474 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
12477 bfd_set_error (bfd_error_no_symbols
);
12478 _bfd_error_handler (_("%pB: no symbol found for import library"),
12484 /* Make symbols absolute. */
12485 amt
= symcount
* sizeof (*osymbuf
);
12486 osymbuf
= (elf_symbol_type
*) bfd_alloc (implib_bfd
, amt
);
12487 if (osymbuf
== NULL
)
12490 for (src_count
= 0; src_count
< symcount
; src_count
++)
12492 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
12493 sizeof (*osymbuf
));
12494 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
12495 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
12496 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
12497 osymbuf
[src_count
].internal_elf_sym
.st_value
=
12498 osymbuf
[src_count
].symbol
.value
;
12499 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
12502 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
12504 /* Allow the BFD backend to copy any private data it understands
12505 from the output BFD to the import library BFD. This is done last
12506 to permit the routine to look at the filtered symbol table. */
12507 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
12510 if (!bfd_close (implib_bfd
))
12521 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
12525 if (flinfo
->symstrtab
!= NULL
)
12526 _bfd_elf_strtab_free (flinfo
->symstrtab
);
12527 free (flinfo
->contents
);
12528 free (flinfo
->external_relocs
);
12529 free (flinfo
->internal_relocs
);
12530 free (flinfo
->external_syms
);
12531 free (flinfo
->locsym_shndx
);
12532 free (flinfo
->internal_syms
);
12533 free (flinfo
->indices
);
12534 free (flinfo
->sections
);
12535 if (flinfo
->symshndxbuf
!= (Elf_External_Sym_Shndx
*) -1)
12536 free (flinfo
->symshndxbuf
);
12537 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
12539 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12540 free (esdo
->rel
.hashes
);
12541 free (esdo
->rela
.hashes
);
12545 /* Do the final step of an ELF link. */
12548 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
12553 struct elf_final_link_info flinfo
;
12555 struct bfd_link_order
*p
;
12557 bfd_size_type max_contents_size
;
12558 bfd_size_type max_external_reloc_size
;
12559 bfd_size_type max_internal_reloc_count
;
12560 bfd_size_type max_sym_count
;
12561 bfd_size_type max_sym_shndx_count
;
12562 Elf_Internal_Sym elfsym
;
12564 Elf_Internal_Shdr
*symtab_hdr
;
12565 Elf_Internal_Shdr
*symtab_shndx_hdr
;
12566 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12567 struct elf_outext_info eoinfo
;
12569 size_t relativecount
;
12570 size_t relr_entsize
;
12571 asection
*reldyn
= 0;
12573 asection
*attr_section
= NULL
;
12574 bfd_vma attr_size
= 0;
12575 const char *std_attrs_section
;
12576 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
12577 bool sections_removed
;
12579 if (!is_elf_hash_table (&htab
->root
))
12582 if (bfd_link_pic (info
))
12583 abfd
->flags
|= DYNAMIC
;
12585 dynamic
= htab
->dynamic_sections_created
;
12586 dynobj
= htab
->dynobj
;
12588 emit_relocs
= (bfd_link_relocatable (info
)
12589 || info
->emitrelocations
);
12591 memset (&flinfo
, 0, sizeof (flinfo
));
12592 flinfo
.info
= info
;
12593 flinfo
.output_bfd
= abfd
;
12594 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
12595 if (flinfo
.symstrtab
== NULL
)
12600 flinfo
.hash_sec
= NULL
;
12601 flinfo
.symver_sec
= NULL
;
12605 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
12606 /* Note that dynsym_sec can be NULL (on VMS). */
12607 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
12608 /* Note that it is OK if symver_sec is NULL. */
12611 if (info
->unique_symbol
12612 && !bfd_hash_table_init (&flinfo
.local_hash_table
,
12613 local_hash_newfunc
,
12614 sizeof (struct local_hash_entry
)))
12617 /* The object attributes have been merged. Remove the input
12618 sections from the link, and set the contents of the output
12620 sections_removed
= false;
12621 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
12622 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12624 bool remove_section
= false;
12626 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
12627 || strcmp (o
->name
, ".gnu.attributes") == 0)
12629 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12631 asection
*input_section
;
12633 if (p
->type
!= bfd_indirect_link_order
)
12635 input_section
= p
->u
.indirect
.section
;
12636 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12637 elf_link_input_bfd ignores this section. */
12638 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
12641 attr_size
= bfd_elf_obj_attr_size (abfd
);
12642 bfd_set_section_size (o
, attr_size
);
12643 /* Skip this section later on. */
12644 o
->map_head
.link_order
= NULL
;
12648 remove_section
= true;
12650 else if ((o
->flags
& SEC_GROUP
) != 0 && o
->size
== 0)
12652 /* Remove empty group section from linker output. */
12653 remove_section
= true;
12655 if (remove_section
)
12657 o
->flags
|= SEC_EXCLUDE
;
12658 bfd_section_list_remove (abfd
, o
);
12659 abfd
->section_count
--;
12660 sections_removed
= true;
12663 if (sections_removed
)
12664 _bfd_fix_excluded_sec_syms (abfd
, info
);
12666 /* Count up the number of relocations we will output for each output
12667 section, so that we know the sizes of the reloc sections. We
12668 also figure out some maximum sizes. */
12672 /* Mmap is used only if section size >= the minimum mmap section
12673 size. The initial max_contents_size value covers all sections
12674 smaller than the minimum mmap section size. It may be increased
12675 for compressed or linker created sections or sections whose
12676 rawsize != size. max_external_reloc_size covers all relocation
12677 sections smaller than the minimum mmap section size. */
12678 max_contents_size
= _bfd_minimum_mmap_size
;
12679 max_external_reloc_size
= _bfd_minimum_mmap_size
;
12684 max_contents_size
= 0;
12685 max_external_reloc_size
= 0;
12687 max_internal_reloc_count
= 0;
12689 max_sym_shndx_count
= 0;
12691 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12693 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12694 o
->reloc_count
= 0;
12696 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
12698 unsigned int reloc_count
= 0;
12699 unsigned int additional_reloc_count
= 0;
12700 struct bfd_elf_section_data
*esdi
= NULL
;
12702 if (p
->type
== bfd_section_reloc_link_order
12703 || p
->type
== bfd_symbol_reloc_link_order
)
12705 else if (p
->type
== bfd_indirect_link_order
)
12709 sec
= p
->u
.indirect
.section
;
12711 /* Mark all sections which are to be included in the
12712 link. This will normally be every section. We need
12713 to do this so that we can identify any sections which
12714 the linker has decided to not include. */
12715 sec
->linker_mark
= true;
12717 if (sec
->flags
& SEC_MERGE
)
12721 /* Mmap is used only on non-compressed, non-linker created
12722 sections whose rawsize == size. */
12724 || sec
->compress_status
!= COMPRESS_SECTION_NONE
12725 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
12726 || sec
->rawsize
!= sec
->size
)
12729 if (sec
->rawsize
> max_contents_size
)
12730 max_contents_size
= sec
->rawsize
;
12731 if (sec
->size
> max_contents_size
)
12732 max_contents_size
= sec
->size
;
12735 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
12736 && (sec
->owner
->flags
& DYNAMIC
) == 0)
12740 /* We are interested in just local symbols, not all
12742 if (elf_bad_symtab (sec
->owner
))
12743 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
12744 / bed
->s
->sizeof_sym
);
12746 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
12748 if (sym_count
> max_sym_count
)
12749 max_sym_count
= sym_count
;
12751 if (sym_count
> max_sym_shndx_count
12752 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
12753 max_sym_shndx_count
= sym_count
;
12755 esdi
= elf_section_data (sec
);
12757 if (esdi
->this_hdr
.sh_type
== SHT_REL
12758 || esdi
->this_hdr
.sh_type
== SHT_RELA
)
12759 /* Some backends use reloc_count in relocation sections
12760 to count particular types of relocs. Of course,
12761 reloc sections themselves can't have relocations. */
12763 else if (emit_relocs
)
12765 reloc_count
= sec
->reloc_count
;
12766 if (bed
->elf_backend_count_additional_relocs
)
12769 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
12770 additional_reloc_count
+= c
;
12773 else if (bed
->elf_backend_count_relocs
)
12774 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
12776 if ((sec
->flags
& SEC_RELOC
) != 0)
12779 if (!bed
->use_mmap
)
12782 size_t ext_size
= 0;
12784 if (esdi
->rel
.hdr
!= NULL
)
12785 ext_size
= esdi
->rel
.hdr
->sh_size
;
12786 if (esdi
->rela
.hdr
!= NULL
)
12787 ext_size
+= esdi
->rela
.hdr
->sh_size
;
12789 if (ext_size
> max_external_reloc_size
)
12790 max_external_reloc_size
= ext_size
;
12792 if (sec
->reloc_count
> max_internal_reloc_count
)
12793 max_internal_reloc_count
= sec
->reloc_count
;
12798 if (reloc_count
== 0)
12801 reloc_count
+= additional_reloc_count
;
12802 o
->reloc_count
+= reloc_count
;
12804 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
12808 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
12809 esdo
->rel
.count
+= additional_reloc_count
;
12811 if (esdi
->rela
.hdr
)
12813 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
12814 esdo
->rela
.count
+= additional_reloc_count
;
12820 esdo
->rela
.count
+= reloc_count
;
12822 esdo
->rel
.count
+= reloc_count
;
12826 if (o
->reloc_count
> 0)
12827 o
->flags
|= SEC_RELOC
;
12830 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12831 set it (this is probably a bug) and if it is set
12832 assign_section_numbers will create a reloc section. */
12833 o
->flags
&=~ SEC_RELOC
;
12836 /* If the SEC_ALLOC flag is not set, force the section VMA to
12837 zero. This is done in elf_fake_sections as well, but forcing
12838 the VMA to 0 here will ensure that relocs against these
12839 sections are handled correctly. */
12840 if ((o
->flags
& SEC_ALLOC
) == 0
12841 && ! o
->user_set_vma
)
12845 if (! bfd_link_relocatable (info
) && merged
)
12846 elf_link_hash_traverse (htab
, _bfd_elf_link_sec_merge_syms
, abfd
);
12848 /* Figure out the file positions for everything but the symbol table
12849 and the relocs. We set symcount to force assign_section_numbers
12850 to create a symbol table. */
12851 abfd
->symcount
= info
->strip
!= strip_all
|| emit_relocs
;
12852 BFD_ASSERT (! abfd
->output_has_begun
);
12853 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
12856 /* Set sizes, and assign file positions for reloc sections. */
12857 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
12859 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
12860 if ((o
->flags
& SEC_RELOC
) != 0)
12863 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
12867 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
12871 /* _bfd_elf_compute_section_file_positions makes temporary use
12872 of target_index. Reset it. */
12873 o
->target_index
= 0;
12875 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12876 to count upwards while actually outputting the relocations. */
12877 esdo
->rel
.count
= 0;
12878 esdo
->rela
.count
= 0;
12880 if ((esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
12881 && !bfd_section_is_ctf (o
))
12883 /* Cache the section contents so that they can be compressed
12884 later. Use bfd_malloc since it will be freed by
12885 bfd_compress_section_contents. */
12886 unsigned char *contents
= esdo
->this_hdr
.contents
;
12887 if (contents
!= NULL
)
12890 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
12891 if (contents
== NULL
)
12893 esdo
->this_hdr
.contents
= contents
;
12897 /* We have now assigned file positions for all the sections except .symtab,
12898 .strtab, and non-loaded reloc and compressed debugging sections. We start
12899 the .symtab section at the current file position, and write directly to it.
12900 We build the .strtab section in memory. */
12901 abfd
->symcount
= 0;
12902 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12903 /* sh_name is set in prep_headers. */
12904 symtab_hdr
->sh_type
= SHT_SYMTAB
;
12905 /* sh_flags, sh_addr and sh_size all start off zero. */
12906 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
12907 /* sh_link is set in assign_section_numbers. */
12908 /* sh_info is set below. */
12909 /* sh_offset is set just below. */
12910 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
12912 if (max_sym_count
< 20)
12913 max_sym_count
= 20;
12914 htab
->strtabsize
= max_sym_count
;
12915 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
12916 htab
->strtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
);
12917 if (htab
->strtab
== NULL
)
12919 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12921 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
12922 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
12924 if (info
->strip
!= strip_all
|| emit_relocs
)
12926 file_ptr off
= elf_next_file_pos (abfd
);
12928 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, true, 0);
12930 /* Note that at this point elf_next_file_pos (abfd) is
12931 incorrect. We do not yet know the size of the .symtab section.
12932 We correct next_file_pos below, after we do know the size. */
12934 /* Start writing out the symbol table. The first symbol is always a
12936 elfsym
.st_value
= 0;
12937 elfsym
.st_size
= 0;
12938 elfsym
.st_info
= 0;
12939 elfsym
.st_other
= 0;
12940 elfsym
.st_shndx
= SHN_UNDEF
;
12941 elfsym
.st_target_internal
= 0;
12942 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
12943 bfd_und_section_ptr
, NULL
) != 1)
12946 /* Output a symbol for each section if asked or they are used for
12947 relocs. These symbols usually have no names. We store the
12948 index of each one in the index field of the section, so that
12949 we can find it again when outputting relocs. */
12951 if (bfd_keep_unused_section_symbols (abfd
) || emit_relocs
)
12953 bool name_local_sections
12954 = (bed
->elf_backend_name_local_section_symbols
12955 && bed
->elf_backend_name_local_section_symbols (abfd
));
12956 const char *name
= NULL
;
12958 elfsym
.st_size
= 0;
12959 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
12960 elfsym
.st_other
= 0;
12961 elfsym
.st_value
= 0;
12962 elfsym
.st_target_internal
= 0;
12963 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12965 o
= bfd_section_from_elf_index (abfd
, i
);
12968 o
->target_index
= bfd_get_symcount (abfd
);
12969 elfsym
.st_shndx
= i
;
12970 if (!bfd_link_relocatable (info
))
12971 elfsym
.st_value
= o
->vma
;
12972 if (name_local_sections
)
12974 if (elf_link_output_symstrtab (&flinfo
, name
, &elfsym
, o
,
12982 /* On some targets like Irix 5 the symbol split between local and global
12983 ones recorded in the sh_info field needs to be done between section
12984 and all other symbols. */
12985 if (bed
->elf_backend_elfsym_local_is_section
12986 && bed
->elf_backend_elfsym_local_is_section (abfd
))
12987 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
12989 /* Allocate some memory to hold information read in from the input
12991 if (max_contents_size
!= 0)
12993 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
12994 if (flinfo
.contents
== NULL
)
12998 if (max_external_reloc_size
!= 0)
13000 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
13001 if (flinfo
.external_relocs
== NULL
)
13005 if (max_internal_reloc_count
!= 0)
13007 amt
= max_internal_reloc_count
* sizeof (Elf_Internal_Rela
);
13008 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
13009 if (flinfo
.internal_relocs
== NULL
)
13013 if (max_sym_count
!= 0)
13015 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
13016 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
13017 if (flinfo
.external_syms
== NULL
)
13020 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
13021 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
13022 if (flinfo
.internal_syms
== NULL
)
13025 amt
= max_sym_count
* sizeof (long);
13026 flinfo
.indices
= (long int *) bfd_malloc (amt
);
13027 if (flinfo
.indices
== NULL
)
13030 amt
= max_sym_count
* sizeof (asection
*);
13031 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
13032 if (flinfo
.sections
== NULL
)
13036 if (max_sym_shndx_count
!= 0)
13038 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
13039 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
13040 if (flinfo
.locsym_shndx
== NULL
)
13046 bfd_vma base
, end
= 0; /* Both bytes. */
13049 for (sec
= htab
->tls_sec
;
13050 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
13053 bfd_size_type size
= sec
->size
;
13054 unsigned int opb
= bfd_octets_per_byte (abfd
, sec
);
13057 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
13059 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
13062 size
= ord
->offset
* opb
+ ord
->size
;
13064 end
= sec
->vma
+ size
/ opb
;
13066 base
= htab
->tls_sec
->vma
;
13067 /* Only align end of TLS section if static TLS doesn't have special
13068 alignment requirements. */
13069 if (bed
->static_tls_alignment
== 1)
13070 end
= align_power (end
, htab
->tls_sec
->alignment_power
);
13071 htab
->tls_size
= end
- base
;
13074 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
13077 /* Finish relative relocations here after regular symbol processing
13078 is finished if DT_RELR is enabled. */
13079 if (info
->enable_dt_relr
13080 && bed
->finish_relative_relocs
13081 && !bed
->finish_relative_relocs (info
))
13082 info
->callbacks
->einfo
13083 (_("%F%P: %pB: failed to finish relative relocations\n"), abfd
);
13085 /* Since ELF permits relocations to be against local symbols, we
13086 must have the local symbols available when we do the relocations.
13087 Since we would rather only read the local symbols once, and we
13088 would rather not keep them in memory, we handle all the
13089 relocations for a single input file at the same time.
13091 Unfortunately, there is no way to know the total number of local
13092 symbols until we have seen all of them, and the local symbol
13093 indices precede the global symbol indices. This means that when
13094 we are generating relocatable output, and we see a reloc against
13095 a global symbol, we can not know the symbol index until we have
13096 finished examining all the local symbols to see which ones we are
13097 going to output. To deal with this, we keep the relocations in
13098 memory, and don't output them until the end of the link. This is
13099 an unfortunate waste of memory, but I don't see a good way around
13100 it. Fortunately, it only happens when performing a relocatable
13101 link, which is not the common case. FIXME: If keep_memory is set
13102 we could write the relocs out and then read them again; I don't
13103 know how bad the memory loss will be. */
13105 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13106 sub
->output_has_begun
= false;
13107 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
13109 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
13111 if (p
->type
== bfd_indirect_link_order
13112 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
13113 == bfd_target_elf_flavour
)
13114 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
13116 if (! sub
->output_has_begun
)
13118 if (! elf_link_input_bfd (&flinfo
, sub
))
13120 sub
->output_has_begun
= true;
13123 else if (p
->type
== bfd_section_reloc_link_order
13124 || p
->type
== bfd_symbol_reloc_link_order
)
13126 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
13131 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
13133 if (p
->type
== bfd_indirect_link_order
13134 && (bfd_get_flavour (sub
)
13135 == bfd_target_elf_flavour
)
13136 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
13137 != bed
->s
->elfclass
))
13139 const char *iclass
, *oclass
;
13141 switch (bed
->s
->elfclass
)
13143 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
13144 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
13145 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
13149 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
13151 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
13152 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
13153 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
13157 bfd_set_error (bfd_error_wrong_format
);
13159 /* xgettext:c-format */
13160 (_("%pB: file class %s incompatible with %s"),
13161 sub
, iclass
, oclass
);
13170 /* Free symbol buffer if needed. */
13171 if (!info
->reduce_memory_overheads
)
13173 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13174 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
13176 free (elf_tdata (sub
)->symbuf
);
13177 elf_tdata (sub
)->symbuf
= NULL
;
13181 /* Output any global symbols that got converted to local in a
13182 version script or due to symbol visibility. We do this in a
13183 separate step since ELF requires all local symbols to appear
13184 prior to any global symbols. FIXME: We should only do this if
13185 some global symbols were, in fact, converted to become local.
13186 FIXME: Will this work correctly with the Irix 5 linker? */
13187 eoinfo
.failed
= false;
13188 eoinfo
.flinfo
= &flinfo
;
13189 eoinfo
.localsyms
= true;
13190 eoinfo
.file_sym_done
= false;
13191 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
13195 /* If backend needs to output some local symbols not present in the hash
13196 table, do it now. */
13197 if (bed
->elf_backend_output_arch_local_syms
)
13199 if (! ((*bed
->elf_backend_output_arch_local_syms
)
13200 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
13204 /* That wrote out all the local symbols. Finish up the symbol table
13205 with the global symbols. Even if we want to strip everything we
13206 can, we still need to deal with those global symbols that got
13207 converted to local in a version script. */
13209 /* The sh_info field records the index of the first non local symbol. */
13210 if (!symtab_hdr
->sh_info
)
13211 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
13214 && htab
->dynsym
!= NULL
13215 && htab
->dynsym
->output_section
!= bfd_abs_section_ptr
)
13217 Elf_Internal_Sym sym
;
13218 bfd_byte
*dynsym
= htab
->dynsym
->contents
;
13220 o
= htab
->dynsym
->output_section
;
13221 elf_section_data (o
)->this_hdr
.sh_info
= htab
->local_dynsymcount
+ 1;
13223 /* Write out the section symbols for the output sections. */
13224 if (bfd_link_pic (info
)
13225 || htab
->is_relocatable_executable
)
13231 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
13233 sym
.st_target_internal
= 0;
13235 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
13241 dynindx
= elf_section_data (s
)->dynindx
;
13244 indx
= elf_section_data (s
)->this_idx
;
13245 BFD_ASSERT (indx
> 0);
13246 sym
.st_shndx
= indx
;
13247 if (! check_dynsym (abfd
, &sym
))
13249 sym
.st_value
= s
->vma
;
13250 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
13252 /* Inform the linker of the addition of this symbol. */
13254 if (info
->callbacks
->ctf_new_dynsym
)
13255 info
->callbacks
->ctf_new_dynsym (dynindx
, &sym
);
13257 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
13261 /* Write out the local dynsyms. */
13262 if (htab
->dynlocal
)
13264 struct elf_link_local_dynamic_entry
*e
;
13265 for (e
= htab
->dynlocal
; e
; e
= e
->next
)
13270 /* Copy the internal symbol and turn off visibility.
13271 Note that we saved a word of storage and overwrote
13272 the original st_name with the dynstr_index. */
13274 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
13275 sym
.st_shndx
= SHN_UNDEF
;
13277 s
= bfd_section_from_elf_index (e
->input_bfd
,
13280 && s
->output_section
!= NULL
13281 && elf_section_data (s
->output_section
) != NULL
)
13284 elf_section_data (s
->output_section
)->this_idx
;
13285 if (! check_dynsym (abfd
, &sym
))
13287 sym
.st_value
= (s
->output_section
->vma
13289 + e
->isym
.st_value
);
13292 /* Inform the linker of the addition of this symbol. */
13294 if (info
->callbacks
->ctf_new_dynsym
)
13295 info
->callbacks
->ctf_new_dynsym (e
->dynindx
, &sym
);
13297 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
13298 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
13303 /* We get the global symbols from the hash table. */
13304 eoinfo
.failed
= false;
13305 eoinfo
.localsyms
= false;
13306 eoinfo
.flinfo
= &flinfo
;
13307 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
13311 /* If backend needs to output some symbols not present in the hash
13312 table, do it now. */
13313 if (bed
->elf_backend_output_arch_syms
13314 && (info
->strip
!= strip_all
|| emit_relocs
))
13316 if (! ((*bed
->elf_backend_output_arch_syms
)
13317 (abfd
, info
, &flinfo
, elf_link_output_symstrtab
)))
13321 /* Finalize the .strtab section. */
13322 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
13324 /* Swap out the .strtab section. */
13325 if (!elf_link_swap_symbols_out (&flinfo
))
13327 free (htab
->strtab
);
13328 htab
->strtab
= NULL
;
13330 /* Now we know the size of the symtab section. */
13331 if (bfd_get_symcount (abfd
) > 0)
13333 /* Finish up and write out the symbol string table (.strtab)
13335 Elf_Internal_Shdr
*symstrtab_hdr
= NULL
;
13336 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
13338 if (elf_symtab_shndx_list (abfd
))
13340 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
13342 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
13344 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
13345 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
13346 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
13347 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
13348 symtab_shndx_hdr
->sh_size
= amt
;
13350 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
13353 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
13354 || (bfd_write (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
13359 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
13360 /* sh_name was set in prep_headers. */
13361 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
13362 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
13363 symstrtab_hdr
->sh_addr
= 0;
13364 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
13365 symstrtab_hdr
->sh_entsize
= 0;
13366 symstrtab_hdr
->sh_link
= 0;
13367 symstrtab_hdr
->sh_info
= 0;
13368 /* sh_offset is set just below. */
13369 symstrtab_hdr
->sh_addralign
= 1;
13371 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
13373 elf_next_file_pos (abfd
) = off
;
13375 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
13376 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
13380 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
13382 _bfd_error_handler (_("%pB: failed to generate import library"),
13383 info
->out_implib_bfd
);
13387 /* Adjust the relocs to have the correct symbol indices. */
13388 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
13390 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
13393 if ((o
->flags
& SEC_RELOC
) == 0)
13396 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
13397 if (esdo
->rel
.hdr
!= NULL
13398 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rel
, sort
, info
))
13400 if (esdo
->rela
.hdr
!= NULL
13401 && !elf_link_adjust_relocs (abfd
, o
, &esdo
->rela
, sort
, info
))
13404 /* Set the reloc_count field to 0 to prevent write_relocs from
13405 trying to swap the relocs out itself. */
13406 o
->reloc_count
= 0;
13410 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
13411 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
13414 if (htab
->srelrdyn
!= NULL
13415 && htab
->srelrdyn
->output_section
!= NULL
13416 && htab
->srelrdyn
->size
!= 0)
13418 asection
*s
= htab
->srelrdyn
->output_section
;
13419 relr_entsize
= elf_section_data (s
)->this_hdr
.sh_entsize
;
13420 if (relr_entsize
== 0)
13422 relr_entsize
= bed
->s
->arch_size
/ 8;
13423 elf_section_data (s
)->this_hdr
.sh_entsize
= relr_entsize
;
13427 /* If we are linking against a dynamic object, or generating a
13428 shared library, finish up the dynamic linking information. */
13431 bfd_byte
*dyncon
, *dynconend
;
13433 /* Fix up .dynamic entries. */
13435 BFD_ASSERT (o
!= NULL
);
13437 dyncon
= o
->contents
;
13438 dynconend
= PTR_ADD (o
->contents
, o
->size
);
13439 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13441 Elf_Internal_Dyn dyn
;
13444 bfd_size_type sh_size
;
13447 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13454 if (relativecount
!= 0)
13456 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
13458 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
13459 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
13461 if (dyn
.d_tag
!= DT_NULL
13462 && dynconend
- dyncon
>= bed
->s
->sizeof_dyn
)
13464 dyn
.d_un
.d_val
= relativecount
;
13470 if (relr_entsize
!= 0)
13472 if (dynconend
- dyncon
>= 3 * bed
->s
->sizeof_dyn
)
13474 asection
*s
= htab
->srelrdyn
;
13475 dyn
.d_tag
= DT_RELR
;
13477 = s
->output_section
->vma
+ s
->output_offset
;
13478 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13479 dyncon
+= bed
->s
->sizeof_dyn
;
13481 dyn
.d_tag
= DT_RELRSZ
;
13482 dyn
.d_un
.d_val
= s
->size
;
13483 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13484 dyncon
+= bed
->s
->sizeof_dyn
;
13486 dyn
.d_tag
= DT_RELRENT
;
13487 dyn
.d_un
.d_val
= relr_entsize
;
13496 name
= info
->init_function
;
13499 name
= info
->fini_function
;
13502 struct elf_link_hash_entry
*h
;
13504 h
= elf_link_hash_lookup (htab
, name
, false, false, true);
13506 && (h
->root
.type
== bfd_link_hash_defined
13507 || h
->root
.type
== bfd_link_hash_defweak
))
13509 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
13510 o
= h
->root
.u
.def
.section
;
13511 if (o
->output_section
!= NULL
)
13512 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
13513 + o
->output_offset
);
13516 /* The symbol is imported from another shared
13517 library and does not apply to this one. */
13518 dyn
.d_un
.d_ptr
= 0;
13525 case DT_PREINIT_ARRAYSZ
:
13526 name
= ".preinit_array";
13528 case DT_INIT_ARRAYSZ
:
13529 name
= ".init_array";
13531 case DT_FINI_ARRAYSZ
:
13532 name
= ".fini_array";
13534 o
= bfd_get_section_by_name (abfd
, name
);
13538 (_("could not find section %s"), name
);
13543 (_("warning: %s section has zero size"), name
);
13544 dyn
.d_un
.d_val
= o
->size
;
13547 case DT_PREINIT_ARRAY
:
13548 name
= ".preinit_array";
13550 case DT_INIT_ARRAY
:
13551 name
= ".init_array";
13553 case DT_FINI_ARRAY
:
13554 name
= ".fini_array";
13556 o
= bfd_get_section_by_name (abfd
, name
);
13563 name
= ".gnu.hash";
13572 name
= ".gnu.version_d";
13575 name
= ".gnu.version_r";
13578 name
= ".gnu.version";
13580 o
= bfd_get_linker_section (dynobj
, name
);
13582 if (o
== NULL
|| bfd_is_abs_section (o
->output_section
))
13585 (_("could not find section %s"), name
);
13588 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
13591 (_("warning: section '%s' is being made into a note"), name
);
13592 bfd_set_error (bfd_error_nonrepresentable_section
);
13595 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
13602 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
13608 for (i
= 1; i
< elf_numsections (abfd
); i
++)
13610 Elf_Internal_Shdr
*hdr
;
13612 hdr
= elf_elfsections (abfd
)[i
];
13613 if (hdr
->sh_type
== type
13614 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
13616 sh_size
+= hdr
->sh_size
;
13618 || sh_addr
> hdr
->sh_addr
)
13619 sh_addr
= hdr
->sh_addr
;
13623 if (bed
->dtrel_excludes_plt
&& htab
->srelplt
!= NULL
)
13625 unsigned int opb
= bfd_octets_per_byte (abfd
, o
);
13627 /* Don't count procedure linkage table relocs in the
13628 overall reloc count. */
13629 sh_size
-= htab
->srelplt
->size
;
13631 /* If the size is zero, make the address zero too.
13632 This is to avoid a glibc bug. If the backend
13633 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13634 zero, then we'll put DT_RELA at the end of
13635 DT_JMPREL. glibc will interpret the end of
13636 DT_RELA matching the end of DT_JMPREL as the
13637 case where DT_RELA includes DT_JMPREL, and for
13638 LD_BIND_NOW will decide that processing DT_RELA
13639 will process the PLT relocs too. Net result:
13640 No PLT relocs applied. */
13643 /* If .rela.plt is the first .rela section, exclude
13644 it from DT_RELA. */
13645 else if (sh_addr
== (htab
->srelplt
->output_section
->vma
13646 + htab
->srelplt
->output_offset
) * opb
)
13647 sh_addr
+= htab
->srelplt
->size
;
13650 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
13651 dyn
.d_un
.d_val
= sh_size
;
13653 dyn
.d_un
.d_ptr
= sh_addr
;
13656 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
13660 /* If we have created any dynamic sections, then output them. */
13661 if (dynobj
!= NULL
)
13663 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
13666 /* Check for DT_TEXTREL (late, in case the backend removes it). */
13667 if (bfd_link_textrel_check (info
)
13668 && (o
= htab
->dynamic
) != NULL
13671 bfd_byte
*dyncon
, *dynconend
;
13673 dyncon
= o
->contents
;
13674 dynconend
= o
->contents
+ o
->size
;
13675 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
13677 Elf_Internal_Dyn dyn
;
13679 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
13681 if (dyn
.d_tag
== DT_TEXTREL
)
13683 if (info
->textrel_check
== textrel_check_error
)
13684 info
->callbacks
->einfo
13685 (_("%P%X: read-only segment has dynamic relocations\n"));
13686 else if (bfd_link_dll (info
))
13687 info
->callbacks
->einfo
13688 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
13689 else if (bfd_link_pde (info
))
13690 info
->callbacks
->einfo
13691 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
13693 info
->callbacks
->einfo
13694 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
13700 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
13702 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
13704 || o
->output_section
== bfd_abs_section_ptr
)
13706 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
13708 /* At this point, we are only interested in sections
13709 created by _bfd_elf_link_create_dynamic_sections. */
13712 if (htab
->stab_info
.stabstr
== o
)
13714 if (htab
->eh_info
.hdr_sec
== o
)
13716 if (strcmp (o
->name
, ".dynstr") != 0)
13718 bfd_size_type octets
= ((file_ptr
) o
->output_offset
13719 * bfd_octets_per_byte (abfd
, o
));
13720 if (!bfd_set_section_contents (abfd
, o
->output_section
,
13721 o
->contents
, octets
, o
->size
))
13726 /* The contents of the .dynstr section are actually in a
13730 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
13731 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
13732 || !_bfd_elf_strtab_emit (abfd
, htab
->dynstr
))
13738 if (!info
->resolve_section_groups
)
13740 bool failed
= false;
13742 BFD_ASSERT (bfd_link_relocatable (info
));
13743 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
13748 /* If we have optimized stabs strings, output them. */
13749 if (htab
->stab_info
.stabstr
!= NULL
)
13751 if (!_bfd_write_stab_strings (abfd
, &htab
->stab_info
))
13755 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
13758 if (! _bfd_elf_write_section_sframe (abfd
, info
))
13761 if (info
->callbacks
->emit_ctf
)
13762 info
->callbacks
->emit_ctf ();
13764 elf_final_link_free (abfd
, &flinfo
);
13768 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
13769 if (contents
== NULL
)
13771 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
13772 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
13776 if (info
->unique_symbol
)
13777 bfd_hash_table_free (&flinfo
.local_hash_table
);
13781 free (htab
->strtab
);
13782 htab
->strtab
= NULL
;
13783 elf_final_link_free (abfd
, &flinfo
);
13785 if (info
->unique_symbol
)
13786 bfd_hash_table_free (&flinfo
.local_hash_table
);
13790 /* Initialize COOKIE for input bfd ABFD. */
13793 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
13794 struct bfd_link_info
*info
, bfd
*abfd
,
13797 Elf_Internal_Shdr
*symtab_hdr
;
13798 const struct elf_backend_data
*bed
;
13800 bed
= get_elf_backend_data (abfd
);
13801 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13803 cookie
->abfd
= abfd
;
13804 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
13805 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
13806 if (cookie
->bad_symtab
)
13808 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13809 cookie
->extsymoff
= 0;
13813 cookie
->locsymcount
= symtab_hdr
->sh_info
;
13814 cookie
->extsymoff
= symtab_hdr
->sh_info
;
13817 if (bed
->s
->arch_size
== 32)
13818 cookie
->r_sym_shift
= 8;
13820 cookie
->r_sym_shift
= 32;
13822 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
13823 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
13825 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
13826 cookie
->locsymcount
, 0,
13828 if (cookie
->locsyms
== NULL
)
13830 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
13833 if (keep_memory
|| _bfd_elf_link_keep_memory (info
))
13835 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
13836 info
->cache_size
+= (cookie
->locsymcount
13837 * sizeof (Elf_Internal_Sym
));
13843 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
13846 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
13848 Elf_Internal_Shdr
*symtab_hdr
;
13850 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
13851 if (symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
13852 free (cookie
->locsyms
);
13855 /* Initialize the relocation information in COOKIE for input section SEC
13856 of input bfd ABFD. */
13859 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13860 struct bfd_link_info
*info
, bfd
*abfd
,
13861 asection
*sec
, bool keep_memory
)
13863 if (sec
->reloc_count
== 0)
13865 cookie
->rels
= NULL
;
13866 cookie
->relend
= NULL
;
13870 cookie
->rels
= _bfd_elf_link_info_read_relocs
13871 (abfd
, info
, sec
, NULL
, NULL
,
13872 keep_memory
|| _bfd_elf_link_keep_memory (info
));
13873 if (cookie
->rels
== NULL
)
13875 cookie
->rel
= cookie
->rels
;
13876 cookie
->relend
= cookie
->rels
+ sec
->reloc_count
;
13878 cookie
->rel
= cookie
->rels
;
13882 /* Free the memory allocated by init_reloc_cookie_rels,
13886 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
13889 if (elf_section_data (sec
)->relocs
!= cookie
->rels
)
13890 free (cookie
->rels
);
13893 /* Initialize the whole of COOKIE for input section SEC. */
13896 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13897 struct bfd_link_info
*info
,
13898 asection
*sec
, bool keep_memory
)
13900 if (!init_reloc_cookie (cookie
, info
, sec
->owner
, keep_memory
))
13902 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
,
13908 fini_reloc_cookie (cookie
, sec
->owner
);
13913 /* Free the memory allocated by init_reloc_cookie_for_section,
13917 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
13920 fini_reloc_cookie_rels (cookie
, sec
);
13921 fini_reloc_cookie (cookie
, sec
->owner
);
13924 /* Garbage collect unused sections. */
13926 /* Default gc_mark_hook. */
13929 _bfd_elf_gc_mark_hook (asection
*sec
,
13930 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13931 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13932 struct elf_link_hash_entry
*h
,
13933 Elf_Internal_Sym
*sym
)
13937 switch (h
->root
.type
)
13939 case bfd_link_hash_defined
:
13940 case bfd_link_hash_defweak
:
13941 return h
->root
.u
.def
.section
;
13943 case bfd_link_hash_common
:
13944 return h
->root
.u
.c
.p
->section
;
13951 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
13956 /* Return the debug definition section. */
13959 elf_gc_mark_debug_section (asection
*sec ATTRIBUTE_UNUSED
,
13960 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13961 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
13962 struct elf_link_hash_entry
*h
,
13963 Elf_Internal_Sym
*sym
)
13967 /* Return the global debug definition section. */
13968 if ((h
->root
.type
== bfd_link_hash_defined
13969 || h
->root
.type
== bfd_link_hash_defweak
)
13970 && (h
->root
.u
.def
.section
->flags
& SEC_DEBUGGING
) != 0)
13971 return h
->root
.u
.def
.section
;
13975 /* Return the local debug definition section. */
13976 asection
*isec
= bfd_section_from_elf_index (sec
->owner
,
13978 if (isec
!= NULL
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
13985 /* COOKIE->rel describes a relocation against section SEC, which is
13986 a section we've decided to keep. Return the section that contains
13987 the relocation symbol, or NULL if no section contains it. */
13990 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
13991 elf_gc_mark_hook_fn gc_mark_hook
,
13992 struct elf_reloc_cookie
*cookie
,
13995 unsigned long r_symndx
;
13996 struct elf_link_hash_entry
*h
, *hw
;
13998 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
13999 if (r_symndx
== STN_UNDEF
)
14002 if (r_symndx
>= cookie
->locsymcount
14003 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
14007 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
14010 info
->callbacks
->einfo (_("%F%P: corrupt input: %pB\n"),
14014 while (h
->root
.type
== bfd_link_hash_indirect
14015 || h
->root
.type
== bfd_link_hash_warning
)
14016 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
14018 was_marked
= h
->mark
;
14020 /* Keep all aliases of the symbol too. If an object symbol
14021 needs to be copied into .dynbss then all of its aliases
14022 should be present as dynamic symbols, not just the one used
14023 on the copy relocation. */
14025 while (hw
->is_weakalias
)
14031 if (!was_marked
&& h
->start_stop
&& !h
->root
.ldscript_def
)
14033 if (info
->start_stop_gc
)
14036 /* To work around a glibc bug, mark XXX input sections
14037 when there is a reference to __start_XXX or __stop_XXX
14039 else if (start_stop
!= NULL
)
14041 asection
*s
= h
->u2
.start_stop_section
;
14042 *start_stop
= true;
14047 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
14050 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
14051 &cookie
->locsyms
[r_symndx
]);
14054 /* COOKIE->rel describes a relocation against section SEC, which is
14055 a section we've decided to keep. Mark the section that contains
14056 the relocation symbol. */
14059 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
14061 elf_gc_mark_hook_fn gc_mark_hook
,
14062 struct elf_reloc_cookie
*cookie
)
14065 bool start_stop
= false;
14067 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
14068 while (rsec
!= NULL
)
14070 if (!rsec
->gc_mark
)
14072 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
14073 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
14075 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
14080 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
14085 /* The mark phase of garbage collection. For a given section, mark
14086 it and any sections in this section's group, and all the sections
14087 which define symbols to which it refers. */
14090 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
14092 elf_gc_mark_hook_fn gc_mark_hook
)
14095 asection
*group_sec
, *eh_frame
;
14099 /* Mark all the sections in the group. */
14100 group_sec
= elf_section_data (sec
)->next_in_group
;
14101 if (group_sec
&& !group_sec
->gc_mark
)
14102 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
14105 /* Look through the section relocs. */
14107 eh_frame
= elf_eh_frame_section (sec
->owner
);
14108 if ((sec
->flags
& SEC_RELOC
) != 0
14109 && sec
->reloc_count
> 0
14110 && sec
!= eh_frame
)
14112 struct elf_reloc_cookie cookie
;
14114 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
, false))
14118 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
14119 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
14124 fini_reloc_cookie_for_section (&cookie
, sec
);
14128 if (ret
&& eh_frame
&& elf_fde_list (sec
))
14130 struct elf_reloc_cookie cookie
;
14132 /* NB: When --no-keep-memory is used, the symbol table and
14133 relocation info for eh_frame are freed after they are retrieved
14134 for each text section in the input object. If an input object
14135 has many text sections, the same data is retrieved and freed
14136 many times which can take a very long time. Always keep the
14137 symbol table and relocation info for eh_frame to avoid it. */
14138 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
,
14143 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
14144 gc_mark_hook
, &cookie
))
14146 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
14150 eh_frame
= elf_section_eh_frame_entry (sec
);
14151 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
14152 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
14158 /* Scan and mark sections in a special or debug section group. */
14161 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
14163 /* Point to first section of section group. */
14165 /* Used to iterate the section group. */
14168 bool is_special_grp
= true;
14169 bool is_debug_grp
= true;
14171 /* First scan to see if group contains any section other than debug
14172 and special section. */
14173 ssec
= msec
= elf_next_in_group (grp
);
14176 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
14177 is_debug_grp
= false;
14179 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
14180 is_special_grp
= false;
14182 msec
= elf_next_in_group (msec
);
14184 while (msec
!= ssec
);
14186 /* If this is a pure debug section group or pure special section group,
14187 keep all sections in this group. */
14188 if (is_debug_grp
|| is_special_grp
)
14193 msec
= elf_next_in_group (msec
);
14195 while (msec
!= ssec
);
14199 /* Keep debug and special sections. */
14202 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
14203 elf_gc_mark_hook_fn mark_hook
)
14207 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14211 bool debug_frag_seen
;
14212 bool has_kept_debug_info
;
14214 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14216 isec
= ibfd
->sections
;
14217 if (isec
== NULL
|| isec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14220 /* Ensure all linker created sections are kept,
14221 see if any other section is already marked,
14222 and note if we have any fragmented debug sections. */
14223 debug_frag_seen
= some_kept
= has_kept_debug_info
= false;
14224 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14226 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
14228 else if (isec
->gc_mark
14229 && (isec
->flags
& SEC_ALLOC
) != 0
14230 && elf_section_type (isec
) != SHT_NOTE
)
14234 /* Since all sections, except for backend specific ones,
14235 have been garbage collected, call mark_hook on this
14236 section if any of its linked-to sections is marked. */
14237 asection
*linked_to_sec
;
14238 for (linked_to_sec
= elf_linked_to_section (isec
);
14239 linked_to_sec
!= NULL
&& !linked_to_sec
->linker_mark
;
14240 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
14242 if (linked_to_sec
->gc_mark
)
14244 if (!_bfd_elf_gc_mark (info
, isec
, mark_hook
))
14248 linked_to_sec
->linker_mark
= 1;
14250 for (linked_to_sec
= elf_linked_to_section (isec
);
14251 linked_to_sec
!= NULL
&& linked_to_sec
->linker_mark
;
14252 linked_to_sec
= elf_linked_to_section (linked_to_sec
))
14253 linked_to_sec
->linker_mark
= 0;
14256 if (!debug_frag_seen
14257 && (isec
->flags
& SEC_DEBUGGING
)
14258 && startswith (isec
->name
, ".debug_line."))
14259 debug_frag_seen
= true;
14260 else if (strcmp (bfd_section_name (isec
),
14261 "__patchable_function_entries") == 0
14262 && elf_linked_to_section (isec
) == NULL
)
14263 info
->callbacks
->einfo (_("%F%P: %pB(%pA): error: "
14264 "need linked-to section "
14265 "for --gc-sections\n"),
14266 isec
->owner
, isec
);
14269 /* If no non-note alloc section in this file will be kept, then
14270 we can toss out the debug and special sections. */
14274 /* Keep debug and special sections like .comment when they are
14275 not part of a group. Also keep section groups that contain
14276 just debug sections or special sections. NB: Sections with
14277 linked-to section has been handled above. */
14278 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14280 if ((isec
->flags
& SEC_GROUP
) != 0)
14281 _bfd_elf_gc_mark_debug_special_section_group (isec
);
14282 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
14283 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
14284 && elf_next_in_group (isec
) == NULL
14285 && elf_linked_to_section (isec
) == NULL
)
14287 if (isec
->gc_mark
&& (isec
->flags
& SEC_DEBUGGING
) != 0)
14288 has_kept_debug_info
= true;
14291 /* Look for CODE sections which are going to be discarded,
14292 and find and discard any fragmented debug sections which
14293 are associated with that code section. */
14294 if (debug_frag_seen
)
14295 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14296 if ((isec
->flags
& SEC_CODE
) != 0
14297 && isec
->gc_mark
== 0)
14302 ilen
= strlen (isec
->name
);
14304 /* Association is determined by the name of the debug
14305 section containing the name of the code section as
14306 a suffix. For example .debug_line.text.foo is a
14307 debug section associated with .text.foo. */
14308 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
14312 if (dsec
->gc_mark
== 0
14313 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
14316 dlen
= strlen (dsec
->name
);
14319 && strncmp (dsec
->name
+ (dlen
- ilen
),
14320 isec
->name
, ilen
) == 0)
14325 /* Mark debug sections referenced by kept debug sections. */
14326 if (has_kept_debug_info
)
14327 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
14329 && (isec
->flags
& SEC_DEBUGGING
) != 0)
14330 if (!_bfd_elf_gc_mark (info
, isec
,
14331 elf_gc_mark_debug_section
))
14338 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
14341 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14343 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14347 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14348 || elf_object_id (sub
) != elf_hash_table_id (elf_hash_table (info
))
14349 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14352 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14355 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14357 /* When any section in a section group is kept, we keep all
14358 sections in the section group. If the first member of
14359 the section group is excluded, we will also exclude the
14361 if (o
->flags
& SEC_GROUP
)
14363 asection
*first
= elf_next_in_group (o
);
14364 o
->gc_mark
= first
->gc_mark
;
14370 /* Skip sweeping sections already excluded. */
14371 if (o
->flags
& SEC_EXCLUDE
)
14374 /* Since this is early in the link process, it is simple
14375 to remove a section from the output. */
14376 o
->flags
|= SEC_EXCLUDE
;
14378 if (info
->print_gc_sections
&& o
->size
!= 0)
14379 /* xgettext:c-format */
14380 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
14388 /* Propagate collected vtable information. This is called through
14389 elf_link_hash_traverse. */
14392 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
14394 /* Those that are not vtables. */
14396 || h
->u2
.vtable
== NULL
14397 || h
->u2
.vtable
->parent
== NULL
)
14400 /* Those vtables that do not have parents, we cannot merge. */
14401 if (h
->u2
.vtable
->parent
== (struct elf_link_hash_entry
*) -1)
14404 /* If we've already been done, exit. */
14405 if (h
->u2
.vtable
->used
&& h
->u2
.vtable
->used
[-1])
14408 /* Make sure the parent's table is up to date. */
14409 elf_gc_propagate_vtable_entries_used (h
->u2
.vtable
->parent
, okp
);
14411 if (h
->u2
.vtable
->used
== NULL
)
14413 /* None of this table's entries were referenced. Re-use the
14415 h
->u2
.vtable
->used
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14416 h
->u2
.vtable
->size
= h
->u2
.vtable
->parent
->u2
.vtable
->size
;
14423 /* Or the parent's entries into ours. */
14424 cu
= h
->u2
.vtable
->used
;
14426 pu
= h
->u2
.vtable
->parent
->u2
.vtable
->used
;
14429 const struct elf_backend_data
*bed
;
14430 unsigned int log_file_align
;
14432 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
14433 log_file_align
= bed
->s
->log_file_align
;
14434 n
= h
->u2
.vtable
->parent
->u2
.vtable
->size
>> log_file_align
;
14448 struct link_info_ok
14450 struct bfd_link_info
*info
;
14455 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
,
14459 bfd_vma hstart
, hend
;
14460 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
14461 const struct elf_backend_data
*bed
;
14462 unsigned int log_file_align
;
14463 struct link_info_ok
*info
= (struct link_info_ok
*) ptr
;
14465 /* Take care of both those symbols that do not describe vtables as
14466 well as those that are not loaded. */
14468 || h
->u2
.vtable
== NULL
14469 || h
->u2
.vtable
->parent
== NULL
)
14472 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
14473 || h
->root
.type
== bfd_link_hash_defweak
);
14475 sec
= h
->root
.u
.def
.section
;
14476 hstart
= h
->root
.u
.def
.value
;
14477 hend
= hstart
+ h
->size
;
14479 relstart
= _bfd_elf_link_info_read_relocs (sec
->owner
, info
->info
,
14480 sec
, NULL
, NULL
, true);
14482 return info
->ok
= false;
14483 bed
= get_elf_backend_data (sec
->owner
);
14484 log_file_align
= bed
->s
->log_file_align
;
14486 relend
= relstart
+ sec
->reloc_count
;
14488 for (rel
= relstart
; rel
< relend
; ++rel
)
14489 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
14491 /* If the entry is in use, do nothing. */
14492 if (h
->u2
.vtable
->used
14493 && (rel
->r_offset
- hstart
) < h
->u2
.vtable
->size
)
14495 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
14496 if (h
->u2
.vtable
->used
[entry
])
14499 /* Otherwise, kill it. */
14500 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
14506 /* Mark sections containing dynamically referenced symbols. When
14507 building shared libraries, we must assume that any visible symbol is
14511 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
14513 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
14514 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
14516 if ((h
->root
.type
== bfd_link_hash_defined
14517 || h
->root
.type
== bfd_link_hash_defweak
)
14519 || h
->root
.ldscript_def
14520 || !info
->start_stop_gc
)
14521 && ((h
->ref_dynamic
&& !h
->forced_local
)
14522 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
14523 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
14524 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
14525 && (!bfd_link_executable (info
)
14526 || info
->gc_keep_exported
14527 || info
->export_dynamic
14530 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
14531 && (h
->versioned
>= versioned
14532 || !bfd_hide_sym_by_version (info
->version_info
,
14533 h
->root
.root
.string
)))))
14534 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14539 /* Keep all sections containing symbols undefined on the command-line,
14540 and the section containing the entry symbol. */
14543 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
14545 struct bfd_sym_chain
*sym
;
14547 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
14549 struct elf_link_hash_entry
*h
;
14551 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
14552 false, false, false);
14555 && (h
->root
.type
== bfd_link_hash_defined
14556 || h
->root
.type
== bfd_link_hash_defweak
)
14557 && !bfd_is_const_section (h
->root
.u
.def
.section
))
14558 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
14563 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
14564 struct bfd_link_info
*info
)
14566 bfd
*ibfd
= info
->input_bfds
;
14568 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
14571 struct elf_reloc_cookie cookie
;
14573 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
14575 sec
= ibfd
->sections
;
14576 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14579 if (!init_reloc_cookie (&cookie
, info
, ibfd
, false))
14582 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
14584 if (startswith (bfd_section_name (sec
), ".eh_frame_entry")
14585 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
,
14588 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
14589 fini_reloc_cookie_rels (&cookie
, sec
);
14596 /* Do mark and sweep of unused sections. */
14599 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
14603 elf_gc_mark_hook_fn gc_mark_hook
;
14604 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14605 struct elf_link_hash_table
*htab
;
14606 struct link_info_ok info_ok
;
14608 if (!bed
->can_gc_sections
14609 || !is_elf_hash_table (info
->hash
))
14611 _bfd_error_handler(_("warning: gc-sections option ignored"));
14615 bed
->gc_keep (info
);
14616 htab
= elf_hash_table (info
);
14618 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14619 at the .eh_frame section if we can mark the FDEs individually. */
14620 for (sub
= info
->input_bfds
;
14621 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
14622 sub
= sub
->link
.next
)
14625 struct elf_reloc_cookie cookie
;
14627 sec
= sub
->sections
;
14628 if (sec
== NULL
|| sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14630 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
14631 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
,
14634 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
14635 if (elf_section_data (sec
)->sec_info
14636 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
14637 elf_eh_frame_section (sub
) = sec
;
14638 fini_reloc_cookie_for_section (&cookie
, sec
);
14639 sec
= bfd_get_next_section_by_name (NULL
, sec
);
14643 /* Apply transitive closure to the vtable entry usage info. */
14644 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
14648 /* Kill the vtable relocations that were not used. */
14649 info_ok
.info
= info
;
14651 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &info_ok
);
14655 /* Mark dynamically referenced symbols. */
14656 if (htab
->dynamic_sections_created
|| info
->gc_keep_exported
)
14657 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
14659 /* Grovel through relocs to find out who stays ... */
14660 gc_mark_hook
= bed
->gc_mark_hook
;
14661 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
14665 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
14666 || elf_object_id (sub
) != elf_hash_table_id (htab
)
14667 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
14671 if (o
== NULL
|| o
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
14674 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14675 Also treat note sections as a root, if the section is not part
14676 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14677 well as FINI_ARRAY sections for ld -r. */
14678 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
14680 && (o
->flags
& SEC_EXCLUDE
) == 0
14681 && ((o
->flags
& SEC_KEEP
) != 0
14682 || (bfd_link_relocatable (info
)
14683 && ((elf_section_data (o
)->this_hdr
.sh_type
14684 == SHT_PREINIT_ARRAY
)
14685 || (elf_section_data (o
)->this_hdr
.sh_type
14687 || (elf_section_data (o
)->this_hdr
.sh_type
14688 == SHT_FINI_ARRAY
)))
14689 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
14690 && elf_next_in_group (o
) == NULL
14691 && elf_linked_to_section (o
) == NULL
)
14692 || ((elf_tdata (sub
)->has_gnu_osabi
& elf_gnu_osabi_retain
)
14693 && (elf_section_flags (o
) & SHF_GNU_RETAIN
))))
14695 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
14700 /* Allow the backend to mark additional target specific sections. */
14701 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
14703 /* ... and mark SEC_EXCLUDE for those that go. */
14704 return elf_gc_sweep (abfd
, info
);
14707 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14710 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
14712 struct elf_link_hash_entry
*h
,
14715 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
14716 struct elf_link_hash_entry
**search
, *child
;
14717 size_t extsymcount
;
14718 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14720 /* The sh_info field of the symtab header tells us where the
14721 external symbols start. We don't care about the local symbols at
14723 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
14724 if (!elf_bad_symtab (abfd
))
14725 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
14727 sym_hashes
= elf_sym_hashes (abfd
);
14728 sym_hashes_end
= PTR_ADD (sym_hashes
, extsymcount
);
14730 /* Hunt down the child symbol, which is in this section at the same
14731 offset as the relocation. */
14732 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
14734 if ((child
= *search
) != NULL
14735 && (child
->root
.type
== bfd_link_hash_defined
14736 || child
->root
.type
== bfd_link_hash_defweak
)
14737 && child
->root
.u
.def
.section
== sec
14738 && child
->root
.u
.def
.value
== offset
)
14742 /* xgettext:c-format */
14743 _bfd_error_handler (_("%pB: %pA+%#" PRIx64
": no symbol found for INHERIT"),
14744 abfd
, sec
, (uint64_t) offset
);
14745 bfd_set_error (bfd_error_invalid_operation
);
14749 if (!child
->u2
.vtable
)
14751 child
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14752 bfd_zalloc (abfd
, sizeof (*child
->u2
.vtable
)));
14753 if (!child
->u2
.vtable
)
14758 /* This *should* only be the absolute section. It could potentially
14759 be that someone has defined a non-global vtable though, which
14760 would be bad. It isn't worth paging in the local symbols to be
14761 sure though; that case should simply be handled by the assembler. */
14763 child
->u2
.vtable
->parent
= (struct elf_link_hash_entry
*) -1;
14766 child
->u2
.vtable
->parent
= h
;
14771 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
14774 bfd_elf_gc_record_vtentry (bfd
*abfd
, asection
*sec
,
14775 struct elf_link_hash_entry
*h
,
14778 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14779 unsigned int log_file_align
= bed
->s
->log_file_align
;
14783 /* xgettext:c-format */
14784 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14786 bfd_set_error (bfd_error_bad_value
);
14792 h
->u2
.vtable
= ((struct elf_link_virtual_table_entry
*)
14793 bfd_zalloc (abfd
, sizeof (*h
->u2
.vtable
)));
14798 if (addend
>= h
->u2
.vtable
->size
)
14800 size_t size
, bytes
, file_align
;
14801 bool *ptr
= h
->u2
.vtable
->used
;
14803 /* While the symbol is undefined, we have to be prepared to handle
14805 file_align
= 1 << log_file_align
;
14806 if (h
->root
.type
== bfd_link_hash_undefined
)
14807 size
= addend
+ file_align
;
14811 if (addend
>= size
)
14813 /* Oops! We've got a reference past the defined end of
14814 the table. This is probably a bug -- shall we warn? */
14815 size
= addend
+ file_align
;
14818 size
= (size
+ file_align
- 1) & -file_align
;
14820 /* Allocate one extra entry for use as a "done" flag for the
14821 consolidation pass. */
14822 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bool);
14826 ptr
= (bool *) bfd_realloc (ptr
- 1, bytes
);
14832 oldbytes
= (((h
->u2
.vtable
->size
>> log_file_align
) + 1)
14834 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
14838 ptr
= (bool *) bfd_zmalloc (bytes
);
14843 /* And arrange for that done flag to be at index -1. */
14844 h
->u2
.vtable
->used
= ptr
+ 1;
14845 h
->u2
.vtable
->size
= size
;
14848 h
->u2
.vtable
->used
[addend
>> log_file_align
] = true;
14853 /* Map an ELF section header flag to its corresponding string. */
14857 flagword flag_value
;
14858 } elf_flags_to_name_table
;
14860 static const elf_flags_to_name_table elf_flags_to_names
[] =
14862 { "SHF_WRITE", SHF_WRITE
},
14863 { "SHF_ALLOC", SHF_ALLOC
},
14864 { "SHF_EXECINSTR", SHF_EXECINSTR
},
14865 { "SHF_MERGE", SHF_MERGE
},
14866 { "SHF_STRINGS", SHF_STRINGS
},
14867 { "SHF_INFO_LINK", SHF_INFO_LINK
},
14868 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
14869 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
14870 { "SHF_GROUP", SHF_GROUP
},
14871 { "SHF_TLS", SHF_TLS
},
14872 { "SHF_MASKOS", SHF_MASKOS
},
14873 { "SHF_EXCLUDE", SHF_EXCLUDE
},
14876 /* Returns TRUE if the section is to be included, otherwise FALSE. */
14878 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
14879 struct flag_info
*flaginfo
,
14882 const bfd_vma sh_flags
= elf_section_flags (section
);
14884 if (!flaginfo
->flags_initialized
)
14886 bfd
*obfd
= info
->output_bfd
;
14887 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14888 struct flag_info_list
*tf
= flaginfo
->flag_list
;
14890 int without_hex
= 0;
14892 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
14895 flagword (*lookup
) (char *);
14897 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
14898 if (lookup
!= NULL
)
14900 flagword hexval
= (*lookup
) ((char *) tf
->name
);
14904 if (tf
->with
== with_flags
)
14905 with_hex
|= hexval
;
14906 else if (tf
->with
== without_flags
)
14907 without_hex
|= hexval
;
14912 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
14914 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
14916 if (tf
->with
== with_flags
)
14917 with_hex
|= elf_flags_to_names
[i
].flag_value
;
14918 else if (tf
->with
== without_flags
)
14919 without_hex
|= elf_flags_to_names
[i
].flag_value
;
14926 info
->callbacks
->einfo
14927 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
14931 flaginfo
->flags_initialized
= true;
14932 flaginfo
->only_with_flags
|= with_hex
;
14933 flaginfo
->not_with_flags
|= without_hex
;
14936 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
14939 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
14945 struct alloc_got_off_arg
{
14947 struct bfd_link_info
*info
;
14950 /* We need a special top-level link routine to convert got reference counts
14951 to real got offsets. */
14954 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
14956 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
14957 bfd
*obfd
= gofarg
->info
->output_bfd
;
14958 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
14960 if (h
->got
.refcount
> 0)
14962 h
->got
.offset
= gofarg
->gotoff
;
14963 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
14966 h
->got
.offset
= (bfd_vma
) -1;
14971 /* And an accompanying bit to work out final got entry offsets once
14972 we're done. Should be called from final_link. */
14975 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
14976 struct bfd_link_info
*info
)
14979 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
14981 struct alloc_got_off_arg gofarg
;
14983 BFD_ASSERT (abfd
== info
->output_bfd
);
14985 if (! is_elf_hash_table (info
->hash
))
14988 /* The GOT offset is relative to the .got section, but the GOT header is
14989 put into the .got.plt section, if the backend uses it. */
14990 if (bed
->want_got_plt
)
14993 gotoff
= bed
->got_header_size
;
14995 /* Do the local .got entries first. */
14996 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
14998 bfd_signed_vma
*local_got
;
14999 size_t j
, locsymcount
;
15000 Elf_Internal_Shdr
*symtab_hdr
;
15002 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
15005 local_got
= elf_local_got_refcounts (i
);
15009 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
15010 if (elf_bad_symtab (i
))
15011 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
15013 locsymcount
= symtab_hdr
->sh_info
;
15015 for (j
= 0; j
< locsymcount
; ++j
)
15017 if (local_got
[j
] > 0)
15019 local_got
[j
] = gotoff
;
15020 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
15023 local_got
[j
] = (bfd_vma
) -1;
15027 /* Then the global .got entries. .plt refcounts are handled by
15028 adjust_dynamic_symbol */
15029 gofarg
.gotoff
= gotoff
;
15030 gofarg
.info
= info
;
15031 elf_link_hash_traverse (elf_hash_table (info
),
15032 elf_gc_allocate_got_offsets
,
15037 /* Many folk need no more in the way of final link than this, once
15038 got entry reference counting is enabled. */
15041 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
15043 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
15046 /* Invoke the regular ELF backend linker to do all the work. */
15047 return bfd_elf_final_link (abfd
, info
);
15051 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
15053 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
15055 if (rcookie
->bad_symtab
)
15056 rcookie
->rel
= rcookie
->rels
;
15058 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
15060 unsigned long r_symndx
;
15062 if (! rcookie
->bad_symtab
)
15063 if (rcookie
->rel
->r_offset
> offset
)
15065 if (rcookie
->rel
->r_offset
!= offset
)
15068 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
15069 if (r_symndx
== STN_UNDEF
)
15072 if (r_symndx
>= rcookie
->locsymcount
15073 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
15075 struct elf_link_hash_entry
*h
;
15077 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
15079 while (h
->root
.type
== bfd_link_hash_indirect
15080 || h
->root
.type
== bfd_link_hash_warning
)
15081 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
15083 if ((h
->root
.type
== bfd_link_hash_defined
15084 || h
->root
.type
== bfd_link_hash_defweak
)
15085 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
15086 || h
->root
.u
.def
.section
->kept_section
!= NULL
15087 || discarded_section (h
->root
.u
.def
.section
)))
15092 /* It's not a relocation against a global symbol,
15093 but it could be a relocation against a local
15094 symbol for a discarded section. */
15096 Elf_Internal_Sym
*isym
;
15098 /* Need to: get the symbol; get the section. */
15099 isym
= &rcookie
->locsyms
[r_symndx
];
15100 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
15102 && (isec
->kept_section
!= NULL
15103 || discarded_section (isec
)))
15111 /* Discard unneeded references to discarded sections.
15112 Returns -1 on error, 1 if any section's size was changed, 0 if
15113 nothing changed. This function assumes that the relocations are in
15114 sorted order, which is true for all known assemblers. */
15117 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
15119 struct elf_reloc_cookie cookie
;
15124 if (info
->traditional_format
15125 || !is_elf_hash_table (info
->hash
))
15128 o
= bfd_get_section_by_name (output_bfd
, ".stab");
15133 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
15136 || i
->reloc_count
== 0
15137 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
15141 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15144 if (!init_reloc_cookie_for_section (&cookie
, info
, i
, false))
15147 if (_bfd_discard_section_stabs (abfd
, i
,
15148 elf_section_data (i
)->sec_info
,
15149 bfd_elf_reloc_symbol_deleted_p
,
15153 fini_reloc_cookie_for_section (&cookie
, i
);
15158 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
15159 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
15163 int eh_changed
= 0;
15164 unsigned int eh_alignment
; /* Octets. */
15166 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
15172 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15175 if (!init_reloc_cookie_for_section (&cookie
, info
, i
, false))
15178 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
15179 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
15180 bfd_elf_reloc_symbol_deleted_p
,
15184 if (i
->size
!= i
->rawsize
)
15188 fini_reloc_cookie_for_section (&cookie
, i
);
15191 eh_alignment
= ((1 << o
->alignment_power
)
15192 * bfd_octets_per_byte (output_bfd
, o
));
15193 /* Skip over zero terminator, and prevent empty sections from
15194 adding alignment padding at the end. */
15195 for (i
= o
->map_tail
.s
; i
!= NULL
; i
= i
->map_tail
.s
)
15197 i
->flags
|= SEC_EXCLUDE
;
15198 else if (i
->size
> 4)
15200 /* The last non-empty eh_frame section doesn't need padding. */
15203 /* Any prior sections must pad the last FDE out to the output
15204 section alignment. Otherwise we might have zero padding
15205 between sections, which would be seen as a terminator. */
15206 for (; i
!= NULL
; i
= i
->map_tail
.s
)
15208 /* All but the last zero terminator should have been removed. */
15213 = (i
->size
+ eh_alignment
- 1) & -eh_alignment
;
15214 if (i
->size
!= size
)
15222 elf_link_hash_traverse (elf_hash_table (info
),
15223 _bfd_elf_adjust_eh_frame_global_symbol
, NULL
);
15226 o
= bfd_get_section_by_name (output_bfd
, ".sframe");
15231 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
15237 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15240 if (!init_reloc_cookie_for_section (&cookie
, info
, i
, false))
15243 if (_bfd_elf_parse_sframe (abfd
, info
, i
, &cookie
))
15245 if (_bfd_elf_discard_section_sframe (i
,
15246 bfd_elf_reloc_symbol_deleted_p
,
15249 if (i
->size
!= i
->rawsize
)
15253 fini_reloc_cookie_for_section (&cookie
, i
);
15255 /* Update the reference to the output .sframe section. Used to
15256 determine later if PT_GNU_SFRAME segment is to be generated. */
15257 if (!_bfd_elf_set_section_sframe (output_bfd
, info
))
15261 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
15263 const struct elf_backend_data
*bed
;
15266 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
15268 s
= abfd
->sections
;
15269 if (s
== NULL
|| s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
15272 bed
= get_elf_backend_data (abfd
);
15274 if (bed
->elf_backend_discard_info
!= NULL
)
15276 if (!init_reloc_cookie (&cookie
, info
, abfd
, false))
15279 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
15282 fini_reloc_cookie (&cookie
, abfd
);
15286 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
15287 _bfd_elf_end_eh_frame_parsing (info
);
15289 if (_bfd_elf_discard_section_eh_frame_hdr (info
))
15296 _bfd_elf_section_already_linked (bfd
*abfd
,
15298 struct bfd_link_info
*info
)
15301 const char *name
, *key
;
15302 struct bfd_section_already_linked
*l
;
15303 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
15305 if (sec
->output_section
== bfd_abs_section_ptr
)
15308 flags
= sec
->flags
;
15310 /* Return if it isn't a linkonce section. A comdat group section
15311 also has SEC_LINK_ONCE set. */
15312 if ((flags
& SEC_LINK_ONCE
) == 0)
15315 /* Don't put group member sections on our list of already linked
15316 sections. They are handled as a group via their group section. */
15317 if (elf_sec_group (sec
) != NULL
)
15320 /* For a SHT_GROUP section, use the group signature as the key. */
15322 if ((flags
& SEC_GROUP
) != 0
15323 && elf_next_in_group (sec
) != NULL
15324 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
15325 key
= elf_group_name (elf_next_in_group (sec
));
15328 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
15329 if (startswith (name
, ".gnu.linkonce.")
15330 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
15333 /* Must be a user linkonce section that doesn't follow gcc's
15334 naming convention. In this case we won't be matching
15335 single member groups. */
15339 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
15341 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15343 /* We may have 2 different types of sections on the list: group
15344 sections with a signature of <key> (<key> is some string),
15345 and linkonce sections named .gnu.linkonce.<type>.<key>.
15346 Match like sections. LTO plugin sections are an exception.
15347 They are always named .gnu.linkonce.t.<key> and match either
15348 type of section. */
15349 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
15350 && ((flags
& SEC_GROUP
) != 0
15351 || strcmp (name
, l
->sec
->name
) == 0))
15352 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0
15353 || (sec
->owner
->flags
& BFD_PLUGIN
) != 0)
15355 /* The section has already been linked. See if we should
15356 issue a warning. */
15357 if (!_bfd_handle_already_linked (sec
, l
, info
))
15360 if (flags
& SEC_GROUP
)
15362 asection
*first
= elf_next_in_group (sec
);
15363 asection
*s
= first
;
15367 s
->output_section
= bfd_abs_section_ptr
;
15368 /* Record which group discards it. */
15369 s
->kept_section
= l
->sec
;
15370 s
= elf_next_in_group (s
);
15371 /* These lists are circular. */
15381 /* A single member comdat group section may be discarded by a
15382 linkonce section and vice versa. */
15383 if ((flags
& SEC_GROUP
) != 0)
15385 asection
*first
= elf_next_in_group (sec
);
15387 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
15388 /* Check this single member group against linkonce sections. */
15389 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15390 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15391 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
15393 first
->output_section
= bfd_abs_section_ptr
;
15394 first
->kept_section
= l
->sec
;
15395 sec
->output_section
= bfd_abs_section_ptr
;
15400 /* Check this linkonce section against single member groups. */
15401 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15402 if (l
->sec
->flags
& SEC_GROUP
)
15404 asection
*first
= elf_next_in_group (l
->sec
);
15407 && elf_next_in_group (first
) == first
15408 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
15410 sec
->output_section
= bfd_abs_section_ptr
;
15411 sec
->kept_section
= first
;
15416 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15417 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15418 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15419 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15420 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15421 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15422 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15423 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15424 The reverse order cannot happen as there is never a bfd with only the
15425 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15426 matter as here were are looking only for cross-bfd sections. */
15428 if ((flags
& SEC_GROUP
) == 0 && startswith (name
, ".gnu.linkonce.r."))
15429 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
15430 if ((l
->sec
->flags
& SEC_GROUP
) == 0
15431 && startswith (l
->sec
->name
, ".gnu.linkonce.t."))
15433 if (abfd
!= l
->sec
->owner
)
15434 sec
->output_section
= bfd_abs_section_ptr
;
15438 /* This is the first section with this name. Record it. */
15439 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
15440 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
15441 return sec
->output_section
== bfd_abs_section_ptr
;
15445 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
15447 return sym
->st_shndx
== SHN_COMMON
;
15451 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
15457 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
15459 return bfd_com_section_ptr
;
15463 _bfd_elf_default_got_elt_size (bfd
*abfd
,
15464 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
15465 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
15466 bfd
*ibfd ATTRIBUTE_UNUSED
,
15467 unsigned long symndx ATTRIBUTE_UNUSED
)
15469 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15470 return bed
->s
->arch_size
/ 8;
15473 /* Routines to support the creation of dynamic relocs. */
15475 /* Returns the name of the dynamic reloc section associated with SEC. */
15477 static const char *
15478 get_dynamic_reloc_section_name (bfd
* abfd
,
15483 const char *old_name
= bfd_section_name (sec
);
15484 const char *prefix
= is_rela
? ".rela" : ".rel";
15486 if (old_name
== NULL
)
15489 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
15490 sprintf (name
, "%s%s", prefix
, old_name
);
15495 /* Returns the dynamic reloc section associated with SEC.
15496 If necessary compute the name of the dynamic reloc section based
15497 on SEC's name (looked up in ABFD's string table) and the setting
15501 _bfd_elf_get_dynamic_reloc_section (bfd
*abfd
,
15505 asection
*reloc_sec
= elf_section_data (sec
)->sreloc
;
15507 if (reloc_sec
== NULL
)
15509 const char *name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15513 reloc_sec
= bfd_get_linker_section (abfd
, name
);
15515 if (reloc_sec
!= NULL
)
15516 elf_section_data (sec
)->sreloc
= reloc_sec
;
15523 /* Returns the dynamic reloc section associated with SEC. If the
15524 section does not exist it is created and attached to the DYNOBJ
15525 bfd and stored in the SRELOC field of SEC's elf_section_data
15528 ALIGNMENT is the alignment for the newly created section and
15529 IS_RELA defines whether the name should be .rela.<SEC's name>
15530 or .rel.<SEC's name>. The section name is looked up in the
15531 string table associated with ABFD. */
15534 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
15536 unsigned int alignment
,
15540 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
15542 if (reloc_sec
== NULL
)
15544 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
15549 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
15551 if (reloc_sec
== NULL
)
15553 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
15554 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
15555 if ((sec
->flags
& SEC_ALLOC
) != 0)
15556 flags
|= SEC_ALLOC
| SEC_LOAD
;
15558 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
15559 if (reloc_sec
!= NULL
)
15561 /* _bfd_elf_get_sec_type_attr chooses a section type by
15562 name. Override as it may be wrong, eg. for a user
15563 section named "auto" we'll get ".relauto" which is
15564 seen to be a .rela section. */
15565 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
15566 if (!bfd_set_section_alignment (reloc_sec
, alignment
))
15571 elf_section_data (sec
)->sreloc
= reloc_sec
;
15577 /* Copy the ELF symbol type and other attributes for a linker script
15578 assignment from HSRC to HDEST. Generally this should be treated as
15579 if we found a strong non-dynamic definition for HDEST (except that
15580 ld ignores multiple definition errors). */
15582 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
15583 struct bfd_link_hash_entry
*hdest
,
15584 struct bfd_link_hash_entry
*hsrc
)
15586 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
15587 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
15588 Elf_Internal_Sym isym
;
15590 ehdest
->type
= ehsrc
->type
;
15591 ehdest
->target_internal
= ehsrc
->target_internal
;
15593 isym
.st_other
= ehsrc
->other
;
15594 elf_merge_st_other (abfd
, ehdest
, isym
.st_other
, NULL
, true, false);
15597 /* Append a RELA relocation REL to section S in BFD. */
15600 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15602 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15603 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
15604 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
15605 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
15608 /* Append a REL relocation REL to section S in BFD. */
15611 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
15613 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
15614 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
15615 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
15616 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);
15619 /* Define __start, __stop, .startof. or .sizeof. symbol. */
15621 struct bfd_link_hash_entry
*
15622 bfd_elf_define_start_stop (struct bfd_link_info
*info
,
15623 const char *symbol
, asection
*sec
)
15625 struct elf_link_hash_entry
*h
;
15627 h
= elf_link_hash_lookup (elf_hash_table (info
), symbol
,
15628 false, false, true);
15629 /* NB: Common symbols will be turned into definition later. */
15631 && !h
->root
.ldscript_def
15632 && (h
->root
.type
== bfd_link_hash_undefined
15633 || h
->root
.type
== bfd_link_hash_undefweak
15634 || ((h
->ref_regular
|| h
->def_dynamic
)
15636 && h
->root
.type
!= bfd_link_hash_common
)))
15638 bool was_dynamic
= h
->ref_dynamic
|| h
->def_dynamic
;
15639 h
->verinfo
.verdef
= NULL
;
15640 h
->root
.type
= bfd_link_hash_defined
;
15641 h
->root
.u
.def
.section
= sec
;
15642 h
->root
.u
.def
.value
= 0;
15643 h
->def_regular
= 1;
15644 h
->def_dynamic
= 0;
15646 h
->u2
.start_stop_section
= sec
;
15647 if (symbol
[0] == '.')
15649 /* .startof. and .sizeof. symbols are local. */
15650 const struct elf_backend_data
*bed
;
15651 bed
= get_elf_backend_data (info
->output_bfd
);
15652 (*bed
->elf_backend_hide_symbol
) (info
, h
, true);
15656 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
15657 h
->other
= ((h
->other
& ~ELF_ST_VISIBILITY (-1))
15658 | info
->start_stop_visibility
);
15660 bfd_elf_link_record_dynamic_symbol (info
, h
);
15667 /* Find dynamic relocs for H that apply to read-only sections. */
15670 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
)
15672 struct elf_dyn_relocs
*p
;
15674 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
15676 asection
*s
= p
->sec
->output_section
;
15678 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
15684 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
15685 read-only sections. */
15688 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry
*h
, void *inf
)
15692 if (h
->root
.type
== bfd_link_hash_indirect
)
15695 sec
= _bfd_elf_readonly_dynrelocs (h
);
15698 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
15700 info
->flags
|= DF_TEXTREL
;
15701 /* xgettext:c-format */
15702 info
->callbacks
->minfo (_("%pB: dynamic relocation against `%pT' "
15703 "in read-only section `%pA'\n"),
15704 sec
->owner
, h
->root
.root
.string
, sec
);
15706 if (bfd_link_textrel_check (info
))
15707 /* xgettext:c-format */
15708 info
->callbacks
->einfo (_("%P: %pB: warning: relocation against `%s' "
15709 "in read-only section `%pA'\n"),
15710 sec
->owner
, h
->root
.root
.string
, sec
);
15712 /* Not an error, just cut short the traversal. */
15718 /* Add dynamic tags. */
15721 _bfd_elf_add_dynamic_tags (bfd
*output_bfd
, struct bfd_link_info
*info
,
15722 bool need_dynamic_reloc
)
15724 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
15726 if (htab
->dynamic_sections_created
)
15728 /* Add some entries to the .dynamic section. We fill in the
15729 values later, in finish_dynamic_sections, but we must add
15730 the entries now so that we get the correct size for the
15731 .dynamic section. The DT_DEBUG entry is filled in by the
15732 dynamic linker and used by the debugger. */
15733 #define add_dynamic_entry(TAG, VAL) \
15734 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15736 const struct elf_backend_data
*bed
15737 = get_elf_backend_data (output_bfd
);
15739 if (bfd_link_executable (info
))
15741 if (!add_dynamic_entry (DT_DEBUG
, 0))
15745 if (htab
->dt_pltgot_required
|| htab
->splt
->size
!= 0)
15747 /* DT_PLTGOT is used by prelink even if there is no PLT
15749 if (!add_dynamic_entry (DT_PLTGOT
, 0))
15753 if (htab
->dt_jmprel_required
|| htab
->srelplt
->size
!= 0)
15755 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
15756 || !add_dynamic_entry (DT_PLTREL
,
15757 (bed
->rela_plts_and_copies_p
15758 ? DT_RELA
: DT_REL
))
15759 || !add_dynamic_entry (DT_JMPREL
, 0))
15763 if (htab
->tlsdesc_plt
15764 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
15765 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
15768 if (need_dynamic_reloc
)
15770 if (bed
->rela_plts_and_copies_p
)
15772 if (!add_dynamic_entry (DT_RELA
, 0)
15773 || !add_dynamic_entry (DT_RELASZ
, 0)
15774 || !add_dynamic_entry (DT_RELAENT
,
15775 bed
->s
->sizeof_rela
))
15780 if (!add_dynamic_entry (DT_REL
, 0)
15781 || !add_dynamic_entry (DT_RELSZ
, 0)
15782 || !add_dynamic_entry (DT_RELENT
,
15783 bed
->s
->sizeof_rel
))
15787 /* If any dynamic relocs apply to a read-only section,
15788 then we need a DT_TEXTREL entry. */
15789 if ((info
->flags
& DF_TEXTREL
) == 0)
15790 elf_link_hash_traverse (htab
, _bfd_elf_maybe_set_textrel
,
15793 if ((info
->flags
& DF_TEXTREL
) != 0)
15795 if (htab
->ifunc_resolvers
)
15796 info
->callbacks
->einfo
15797 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15798 "may result in a segfault at runtime; recompile with %s\n"),
15799 bfd_link_dll (info
) ? "-fPIC" : "-fPIE");
15801 if (!add_dynamic_entry (DT_TEXTREL
, 0))
15806 #undef add_dynamic_entry